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>
18 #include <mono/metadata/seq-points-data.h>
20 #include <mono/mini/mini.h>
23 #include "interp-internals.h"
26 // TODO: export from marshal.c
27 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
42 SeqPoint *last_seq_point;
44 // This will hold a list of last sequence points of incoming basic blocks
45 SeqPoint **pred_seq_points;
46 guint num_pred_seq_points;
57 /* In the interpreter IR */
66 MonoMethodHeader *header;
68 const unsigned char *il_code;
69 const unsigned char *ip;
70 const unsigned char *last_ip;
71 const unsigned char *in_start;
74 StackInfo **stack_state;
77 unsigned char *is_bb_start;
78 unsigned short *new_code;
79 unsigned short *new_code_end;
80 unsigned short *new_ip;
81 unsigned short *last_new_ip;
82 unsigned int max_code_size;
85 unsigned int max_stack_height;
87 unsigned int max_vt_sp;
91 GHashTable *data_hash;
93 gboolean gen_sdb_seq_points;
94 GPtrArray *seq_points;
95 InterpBasicBlock **offset_to_bb;
96 InterpBasicBlock *entry_bb;
100 gboolean verbose_level;
103 #define MINT_TYPE_I1 0
104 #define MINT_TYPE_U1 1
105 #define MINT_TYPE_I2 2
106 #define MINT_TYPE_U2 3
107 #define MINT_TYPE_I4 4
108 #define MINT_TYPE_I8 5
109 #define MINT_TYPE_R4 6
110 #define MINT_TYPE_R8 7
111 #define MINT_TYPE_O 8
112 #define MINT_TYPE_P 9
113 #define MINT_TYPE_VT 10
115 #define STACK_TYPE_I4 0
116 #define STACK_TYPE_I8 1
117 #define STACK_TYPE_R8 2
118 #define STACK_TYPE_O 3
119 #define STACK_TYPE_VT 4
120 #define STACK_TYPE_MP 5
121 #define STACK_TYPE_F 6
123 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
125 #if SIZEOF_VOID_P == 8
126 #define STACK_TYPE_I STACK_TYPE_I8
128 #define STACK_TYPE_I STACK_TYPE_I4
131 static int stack_type [] = {
132 STACK_TYPE_I4, /*I1*/
133 STACK_TYPE_I4, /*U1*/
134 STACK_TYPE_I4, /*I2*/
135 STACK_TYPE_I4, /*U2*/
136 STACK_TYPE_I4, /*I4*/
137 STACK_TYPE_I8, /*I8*/
138 STACK_TYPE_R8, /*R4*/
139 STACK_TYPE_R8, /*R8*/
146 grow_code (TransformData *td)
148 unsigned int old_ip_offset = td->new_ip - td->new_code;
149 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
150 g_assert (old_ip_offset <= td->max_code_size);
151 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
152 td->new_code_end = td->new_code + td->max_code_size;
153 td->new_ip = td->new_code + old_ip_offset;
154 td->last_new_ip = td->new_code + old_last_ip_offset;
157 #define ENSURE_CODE(td, n) \
159 if ((td)->new_ip + (n) > (td)->new_code_end) \
163 #define ADD_CODE(td, n) \
165 if ((td)->new_ip == (td)->new_code_end) \
167 *(td)->new_ip++ = (n); \
170 #define CHECK_STACK(td, n) \
172 int stack_size = (td)->sp - (td)->stack; \
173 if (stack_size < (n)) \
174 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
175 (td)->method->klass->name, (td)->method->name, \
176 stack_size, n, (td)->ip - (td)->il_code); \
179 #define ENSURE_I4(td, sp_off) \
181 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
182 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
186 handle_branch (TransformData *td, int short_op, int long_op, int offset)
188 int shorten_branch = 0;
189 int target = td->ip + offset - td->il_code;
190 if (target < 0 || target >= td->code_size)
191 g_assert_not_reached ();
192 if (offset > 0 && td->stack_height [target] < 0) {
193 td->stack_height [target] = td->sp - td->stack;
194 if (td->stack_height [target] > 0)
195 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
196 td->vt_stack_size [target] = td->vt_sp;
199 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
200 if (offset >= -32768) {
204 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
207 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
208 if (shorten_branch) {
210 reloc->type = RELOC_SHORT_BRANCH;
213 reloc->type = RELOC_LONG_BRANCH;
215 reloc->offset = td->new_ip - td->new_code;
216 reloc->target = target;
217 g_ptr_array_add (td->relocs, reloc);
219 if (shorten_branch) {
220 ADD_CODE(td, short_op);
221 ADD_CODE(td, offset);
223 ADD_CODE(td, long_op);
224 ADD_CODE(td, * (unsigned short *)(&offset));
225 ADD_CODE(td, * ((unsigned short *)&offset + 1));
230 one_arg_branch(TransformData *td, int mint_op, int offset)
232 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
233 int long_op = mint_op + type - STACK_TYPE_I4;
234 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
237 handle_branch (td, short_op, long_op, offset);
241 two_arg_branch(TransformData *td, int mint_op, int offset)
243 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
244 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
245 int long_op = mint_op + type1 - STACK_TYPE_I4;
246 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
248 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
249 ADD_CODE(td, MINT_CONV_I8_I4);
250 td->in_offsets [td->ip - td->il_code]++;
251 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
252 ADD_CODE(td, MINT_CONV_I8_I4_SP);
253 td->in_offsets [td->ip - td->il_code]++;
254 } else if (type1 != type2) {
255 g_warning("%s.%s: branch type mismatch %d %d",
256 td->method->klass->name, td->method->name,
257 td->sp [-1].type, td->sp [-2].type);
260 handle_branch (td, short_op, long_op, offset);
264 unary_arith_op(TransformData *td, int mint_op)
266 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
272 binary_arith_op(TransformData *td, int mint_op)
274 int type1 = td->sp [-2].type;
275 int type2 = td->sp [-1].type;
277 #if SIZEOF_VOID_P == 8
278 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
279 ADD_CODE(td, MINT_CONV_I8_I4);
280 type2 = STACK_TYPE_I8;
282 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
283 ADD_CODE(td, MINT_CONV_I8_I4_SP);
284 type1 = STACK_TYPE_I8;
285 td->sp [-2].type = STACK_TYPE_I8;
288 if (type1 == STACK_TYPE_MP)
289 type1 = STACK_TYPE_I;
290 if (type2 == STACK_TYPE_MP)
291 type2 = STACK_TYPE_I;
292 if (type1 != type2) {
293 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
294 td->method->klass->name, td->method->name,
295 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
297 op = mint_op + type1 - STACK_TYPE_I4;
304 shift_op(TransformData *td, int mint_op)
306 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
308 if (td->sp [-1].type != STACK_TYPE_I4) {
309 g_warning("%s.%s: shift type mismatch %d",
310 td->method->klass->name, td->method->name,
318 mint_type(MonoType *type)
323 switch (type->type) {
327 case MONO_TYPE_BOOLEAN:
339 #if SIZEOF_VOID_P == 4
353 case MONO_TYPE_STRING:
354 case MONO_TYPE_SZARRAY:
355 case MONO_TYPE_CLASS:
356 case MONO_TYPE_OBJECT:
357 case MONO_TYPE_ARRAY:
359 case MONO_TYPE_VALUETYPE:
360 if (type->data.klass->enumtype) {
361 type = mono_class_enum_basetype (type->data.klass);
365 case MONO_TYPE_TYPEDBYREF:
367 case MONO_TYPE_GENERICINST:
368 type = &type->data.generic_class->container_class->byval_arg;
371 g_warning ("got type 0x%02x", type->type);
372 g_assert_not_reached ();
378 can_store (int stack_type, int var_type)
380 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
381 stack_type = STACK_TYPE_I;
382 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
383 var_type = STACK_TYPE_I;
384 return stack_type == var_type;
387 #define SET_SIMPLE_TYPE(s, ty) \
394 #define SET_TYPE(s, ty, k) \
401 #define PUSH_SIMPLE_TYPE(td, ty) \
405 sp_height = (td)->sp - (td)->stack; \
406 if (sp_height > (td)->max_stack_height) \
407 (td)->max_stack_height = sp_height; \
408 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
411 #define PUSH_TYPE(td, ty, k) \
415 sp_height = (td)->sp - (td)->stack; \
416 if (sp_height > (td)->max_stack_height) \
417 (td)->max_stack_height = sp_height; \
418 SET_TYPE((td)->sp - 1, ty, k); \
421 #define PUSH_VT(td, size) \
423 (td)->vt_sp += ((size) + 7) & ~7; \
424 if ((td)->vt_sp > (td)->max_vt_sp) \
425 (td)->max_vt_sp = (td)->vt_sp; \
428 #define POP_VT(td, size) \
430 (td)->vt_sp -= ((size) + 7) & ~7; \
433 #if NO_UNALIGNED_ACCESS
434 #define WRITE32(td, v) \
436 ENSURE_CODE(td, 2); \
437 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
438 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
442 #define WRITE64(td, v) \
444 ENSURE_CODE(td, 4); \
445 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
446 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
447 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
448 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
452 #define WRITE32(td, v) \
454 ENSURE_CODE(td, 2); \
455 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
459 #define WRITE64(td, v) \
461 ENSURE_CODE(td, 4); \
462 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
469 load_arg(TransformData *td, int n)
472 MonoClass *klass = NULL;
475 gboolean hasthis = mono_method_signature (td->method)->hasthis;
476 if (hasthis && n == 0)
477 type = &td->method->klass->byval_arg;
479 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
481 mt = mint_type (type);
482 if (mt == MINT_TYPE_VT) {
484 klass = mono_class_from_mono_type (type);
485 if (mono_method_signature (td->method)->pinvoke)
486 size = mono_class_native_size (klass, NULL);
488 size = mono_class_value_size (klass, NULL);
490 if (hasthis && n == 0) {
492 ADD_CODE (td, MINT_LDARG_P);
493 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
497 ADD_CODE (td, MINT_LDARG_VT);
498 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
502 if (hasthis && n == 0) {
504 ADD_CODE (td, MINT_LDARG_P);
505 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
508 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
509 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
510 if (mt == MINT_TYPE_O)
511 klass = mono_class_from_mono_type (type);
514 PUSH_TYPE(td, stack_type[mt], klass);
518 store_arg(TransformData *td, int n)
524 gboolean hasthis = mono_method_signature (td->method)->hasthis;
525 if (hasthis && n == 0)
526 type = &td->method->klass->byval_arg;
528 type = mono_method_signature (td->method)->params [n - !!hasthis];
530 mt = mint_type (type);
531 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
534 if (mono_method_signature (td->method)->pinvoke)
535 size = mono_class_native_size (klass, NULL);
537 size = mono_class_value_size (klass, NULL);
538 ADD_CODE(td, MINT_STARG_VT);
539 ADD_CODE(td, td->rtm->arg_offsets [n]);
541 if (td->sp [-1].type == STACK_TYPE_VT)
544 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
545 ADD_CODE(td, td->rtm->arg_offsets [n]);
551 store_inarg(TransformData *td, int n)
554 gboolean hasthis = mono_method_signature (td->method)->hasthis;
555 if (hasthis && n == 0)
556 type = &td->method->klass->byval_arg;
558 type = mono_method_signature (td->method)->params [n - !!hasthis];
560 int mt = mint_type (type);
561 if (hasthis && n == 0) {
562 ADD_CODE (td, MINT_STINARG_P);
566 if (mt == MINT_TYPE_VT) {
567 MonoClass *klass = mono_class_from_mono_type (type);
569 if (mono_method_signature (td->method)->pinvoke)
570 size = mono_class_native_size (klass, NULL);
572 size = mono_class_value_size (klass, NULL);
573 ADD_CODE(td, MINT_STINARG_VT);
577 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
583 load_local(TransformData *td, int n)
585 MonoType *type = td->header->locals [n];
586 int mt = mint_type (type);
587 int offset = td->rtm->local_offsets [n];
588 MonoClass *klass = NULL;
589 if (mt == MINT_TYPE_VT) {
590 klass = mono_class_from_mono_type (type);
591 gint32 size = mono_class_value_size (klass, NULL);
593 ADD_CODE(td, MINT_LDLOC_VT);
594 ADD_CODE(td, offset); /*FIX for large offset */
597 g_assert (mt < MINT_TYPE_VT);
598 if (!td->gen_sdb_seq_points &&
599 mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
600 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
601 td->last_new_ip [0] = MINT_STLOC_NP_I4;
602 } else if (!td->gen_sdb_seq_points &&
603 mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
604 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
605 td->last_new_ip [0] = MINT_STLOC_NP_O;
607 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
608 ADD_CODE(td, offset); /*FIX for large offset */
610 if (mt == MINT_TYPE_O)
611 klass = mono_class_from_mono_type (type);
613 PUSH_TYPE(td, stack_type[mt], klass);
617 store_local(TransformData *td, int n)
619 MonoType *type = td->header->locals [n];
620 int mt = mint_type (type);
621 int offset = td->rtm->local_offsets [n];
623 #if SIZEOF_VOID_P == 8
624 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
625 ADD_CODE(td, MINT_CONV_I8_I4);
626 td->sp [-1].type = STACK_TYPE_I8;
629 if (!can_store(td->sp [-1].type, stack_type [mt])) {
630 g_warning("%s.%s: Store local stack type mismatch %d %d",
631 td->method->klass->name, td->method->name,
632 stack_type [mt], td->sp [-1].type);
634 if (mt == MINT_TYPE_VT) {
635 MonoClass *klass = mono_class_from_mono_type (type);
636 gint32 size = mono_class_value_size (klass, NULL);
637 ADD_CODE(td, MINT_STLOC_VT);
638 ADD_CODE(td, offset); /*FIX for large offset */
640 if (td->sp [-1].type == STACK_TYPE_VT)
643 g_assert (mt < MINT_TYPE_VT);
644 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
645 ADD_CODE(td, offset); /*FIX for large offset */
650 #define SIMPLE_OP(td, op) \
657 get_data_item_index (TransformData *td, void *ptr)
659 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
662 return GPOINTER_TO_UINT (p) - 1;
663 if (td->max_data_items == td->n_data_items) {
664 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
665 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
667 index = td->n_data_items;
668 td->data_items [index] = ptr;
670 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
675 jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
679 if (sig->param_count > 6)
683 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
685 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
687 if (method->is_inflated)
689 if (method->string_ctor)
692 for (l = jit_classes; l; l = l->next) {
693 char *class_name = l->data;
695 if (!strcmp (method->klass->name, class_name))
704 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)
706 MonoImage *image = method->klass->image;
707 MonoMethodSignature *csignature;
709 int virtual = *td->ip == CEE_CALLVIRT;
710 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
712 guint32 vt_stack_used = 0;
713 guint32 vt_res_size = 0;
718 guint32 token = read32 (td->ip + 1);
720 if (target_method == NULL) {
723 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
725 if (method->wrapper_type != MONO_WRAPPER_NONE)
726 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
728 csignature = mono_metadata_parse_signature (image, token);
730 if (generic_context) {
731 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
732 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
735 target_method = NULL;
737 if (method->wrapper_type == MONO_WRAPPER_NONE)
738 target_method = mono_get_method_full (image, token, NULL, generic_context);
740 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
741 csignature = mono_method_signature (target_method);
743 if (generic_context) {
744 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
745 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
746 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
747 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
751 csignature = mono_method_signature (target_method);
754 if (target_method && target_method->string_ctor) {
755 /* Create the real signature */
756 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (td->mempool, csignature);
757 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
759 csignature = ctor_sig;
764 if (target_method->klass == mono_defaults.string_class) {
765 if (target_method->name [0] == 'g') {
766 if (strcmp (target_method->name, "get_Chars") == 0)
768 else if (strcmp (target_method->name, "get_Length") == 0)
771 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
772 if (!strcmp (target_method->name, "get_Rank")) {
773 op = MINT_ARRAY_RANK;
774 } else if (!strcmp (target_method->name, "get_Length")) {
776 } else if (!strcmp (target_method->name, "Address")) {
777 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
779 } else if (target_method->klass->image == mono_defaults.corlib &&
780 (strcmp (target_method->klass->name_space, "System.Diagnostics") == 0) &&
781 (strcmp (target_method->klass->name, "Debugger") == 0)) {
782 if (!strcmp (target_method->name, "Break") && csignature->param_count == 0) {
783 if (mini_should_insert_breakpoint (method))
789 if (constrained_class) {
790 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
791 /* Use the corresponding method from the base type to avoid boxing */
792 MonoType *base_type = mono_class_enum_basetype (constrained_class);
793 g_assert (base_type);
794 constrained_class = mono_class_from_mono_type (base_type);
795 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
796 g_assert (target_method);
800 if (constrained_class) {
801 mono_class_setup_vtable (constrained_class);
803 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);
805 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
807 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
809 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
810 mono_class_setup_vtable (target_method->klass);
812 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)) {
813 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
814 /* managed pointer on the stack, we need to deref that puppy */
815 ADD_CODE (td, MINT_LDIND_I);
816 ADD_CODE (td, csignature->param_count);
818 ADD_CODE (td, MINT_BOX);
819 ADD_CODE (td, get_data_item_index (td, constrained_class));
820 ADD_CODE (td, csignature->param_count);
821 } else if (!constrained_class->valuetype) {
822 /* managed pointer on the stack, we need to deref that puppy */
823 ADD_CODE (td, MINT_LDIND_I);
824 ADD_CODE (td, csignature->param_count);
826 if (target_method->klass->valuetype) {
829 /* Interface method */
832 mono_class_setup_vtable (constrained_class);
833 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
835 g_error ("type load error: constrained_class");
836 slot = mono_method_get_vtable_slot (target_method);
838 g_error ("type load error: target_method->klass");
839 target_method = constrained_class->vtable [ioffset + slot];
841 if (target_method->klass == mono_defaults.enum_class) {
842 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
843 /* managed pointer on the stack, we need to deref that puppy */
844 ADD_CODE (td, MINT_LDIND_I);
845 ADD_CODE (td, csignature->param_count);
847 ADD_CODE (td, MINT_BOX);
848 ADD_CODE (td, get_data_item_index (td, constrained_class));
849 ADD_CODE (td, csignature->param_count);
857 mono_class_init (target_method->klass);
859 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
860 if (!calli && op == -1 && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
861 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
862 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0 &&
863 !(target_method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING)) {
864 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
865 MonoMethodHeader *mheader = mono_method_get_header (target_method);
867 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
869 if (td->verbose_level)
870 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
872 for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
875 ADD_CODE(td, MINT_BR_S);
876 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
877 ADD_CODE(td, offset);
878 if (!is_bb_start [td->ip + 5 - td->il_code])
879 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
883 /* mheader might not exist if this is a delegate invoc, etc */
884 if (mheader && *mheader->code == CEE_RET && called_inited) {
885 if (td->verbose_level)
886 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
887 for (i = 0; i < csignature->param_count; i++) {
888 ADD_CODE (td, MINT_POP); /*FIX: vt */
891 if (csignature->hasthis) {
893 ADD_CODE(td, MINT_CKNULL);
894 ADD_CODE (td, MINT_POP);
897 td->sp -= csignature->param_count + csignature->hasthis;
903 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
904 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
905 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
906 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
907 target_method = mono_marshal_get_synchronized_wrapper (target_method);
909 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
910 td->sp -= csignature->param_count + !!csignature->hasthis;
911 for (i = 0; i < csignature->param_count; ++i) {
912 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
914 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
915 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
916 size = mono_class_native_size (klass, NULL);
918 size = mono_class_value_size (klass, NULL);
919 size = (size + 7) & ~7;
920 vt_stack_used += size;
924 /* need to handle typedbyref ... */
925 if (csignature->ret->type != MONO_TYPE_VOID) {
926 int mt = mint_type(csignature->ret);
927 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
928 if (mt == MINT_TYPE_VT) {
929 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
930 vt_res_size = mono_class_native_size (klass, NULL);
932 vt_res_size = mono_class_value_size (klass, NULL);
933 PUSH_VT(td, vt_res_size);
935 PUSH_TYPE(td, stack_type[mt], klass);
941 #if SIZEOF_VOID_P == 8
942 if (op == MINT_LDLEN)
943 ADD_CODE (td, MINT_CONV_I4_I8);
945 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
946 ADD_CODE (td, get_data_item_index (td, target_method->klass));
947 ADD_CODE (td, 1 + target_method->klass->rank);
949 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
950 ADD_CODE(td, MINT_JIT_CALL);
951 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
952 mono_error_assert_ok (&error);
955 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
957 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
959 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
962 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
964 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
965 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
969 if (vt_stack_used != 0 || vt_res_size != 0) {
970 ADD_CODE(td, MINT_VTRESULT);
971 ADD_CODE(td, vt_res_size);
972 WRITE32(td, &vt_stack_used);
973 td->vt_sp -= vt_stack_used;
977 static MonoClassField *
978 interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
980 MonoClassField *field = NULL;
981 if (method->wrapper_type != MONO_WRAPPER_NONE) {
982 field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
983 *klass = field->parent;
987 field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
988 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
993 static InterpBasicBlock*
994 get_bb (TransformData *td, InterpBasicBlock *cbb, unsigned char *ip)
996 int offset = ip - td->il_code;
997 InterpBasicBlock *bb = td->offset_to_bb [offset];
1000 bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock));
1002 td->offset_to_bb [offset] = bb;
1004 td->basic_blocks = g_list_append_mempool (td->mempool, td->basic_blocks, bb);
1008 bb->preds = g_slist_prepend_mempool (td->mempool, bb->preds, cbb);
1015 * Compute the set of IL level basic blocks.
1018 get_basic_blocks (TransformData *td)
1020 guint8 *start = (guint8*)td->il_code;
1021 guint8 *end = (guint8*)td->il_code + td->code_size;
1023 unsigned char *target;
1026 const MonoOpcode *opcode;
1027 InterpBasicBlock *cbb;
1029 td->offset_to_bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock*) * (end - start + 1));
1030 td->entry_bb = cbb = get_bb (td, NULL, start);
1033 cli_addr = ip - start;
1034 td->offset_to_bb [cli_addr] = cbb;
1035 i = mono_opcode_value ((const guint8 **)&ip, end);
1036 opcode = &mono_opcodes [i];
1037 switch (opcode->argument) {
1038 case MonoInlineNone:
1041 case MonoInlineString:
1042 case MonoInlineType:
1043 case MonoInlineField:
1044 case MonoInlineMethod:
1047 case MonoShortInlineR:
1054 case MonoShortInlineVar:
1055 case MonoShortInlineI:
1058 case MonoShortInlineBrTarget:
1059 target = start + cli_addr + 2 + (signed char)ip [1];
1060 get_bb (td, cbb, target);
1062 cbb = get_bb (td, cbb, ip);
1064 case MonoInlineBrTarget:
1065 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
1066 get_bb (td, cbb, target);
1068 cbb = get_bb (td, cbb, ip);
1070 case MonoInlineSwitch: {
1071 guint32 n = read32 (ip + 1);
1074 cli_addr += 5 + 4 * n;
1075 target = start + cli_addr;
1076 get_bb (td, cbb, target);
1078 for (j = 0; j < n; ++j) {
1079 target = start + cli_addr + (gint32)read32 (ip);
1080 get_bb (td, cbb, target);
1083 cbb = get_bb (td, cbb, ip);
1091 g_assert_not_reached ();
1097 interp_save_debug_info (InterpMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
1099 MonoDebugMethodJitInfo *dinfo;
1102 if (!mono_debug_enabled ())
1106 * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
1109 dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
1110 dinfo->num_params = rtm->param_count;
1111 dinfo->params = g_new0 (MonoDebugVarInfo, dinfo->num_params);
1112 dinfo->num_locals = header->num_locals;
1113 dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1114 dinfo->code_start = (guint8*)rtm->code;
1115 dinfo->code_size = td->new_ip - td->new_code;
1116 dinfo->epilogue_begin = 0;
1117 dinfo->has_var_info = TRUE;
1118 dinfo->num_line_numbers = line_numbers->len;
1119 dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
1121 for (i = 0; i < dinfo->num_params; i++) {
1122 MonoDebugVarInfo *var = &dinfo->params [i];
1123 var->type = rtm->param_types [i];
1125 for (i = 0; i < dinfo->num_locals; i++) {
1126 MonoDebugVarInfo *var = &dinfo->locals [i];
1127 var->type = header->locals [i];
1130 for (i = 0; i < dinfo->num_line_numbers; i++)
1131 dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
1132 mono_debug_add_method (rtm->method, dinfo, mono_domain_get ());
1134 mono_debug_free_method_jit_info (dinfo);
1137 /* Same as the code in seq-points.c */
1139 insert_pred_seq_point (SeqPoint *last_sp, SeqPoint *sp, GSList **next)
1142 int src_index = last_sp->next_offset;
1143 int dst_index = sp->next_offset;
1145 /* bb->in_bb might contain duplicates */
1146 for (l = next [src_index]; l; l = l->next)
1147 if (GPOINTER_TO_UINT (l->data) == dst_index)
1150 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
1154 recursively_make_pred_seq_points (TransformData *td, InterpBasicBlock *bb)
1156 const gpointer MONO_SEQ_SEEN_LOOP = GINT_TO_POINTER(-1);
1159 GArray *predecessors = g_array_new (FALSE, TRUE, sizeof (gpointer));
1160 GHashTable *seen = g_hash_table_new_full (g_direct_hash, NULL, NULL, NULL);
1162 // Insert/remove sentinel into the memoize table to detect loops containing bb
1163 bb->pred_seq_points = MONO_SEQ_SEEN_LOOP;
1165 for (l = bb->preds; l; l = l->next) {
1166 InterpBasicBlock *in_bb = l->data;
1168 // This bb has the last seq point, append it and continue
1169 if (in_bb->last_seq_point != NULL) {
1170 predecessors = g_array_append_val (predecessors, in_bb->last_seq_point);
1174 // We've looped or handled this before, exit early.
1175 // No last sequence points to find.
1176 if (in_bb->pred_seq_points == MONO_SEQ_SEEN_LOOP)
1179 // Take sequence points from incoming basic blocks
1181 if (in_bb == td->entry_bb)
1184 if (in_bb->pred_seq_points == NULL)
1185 recursively_make_pred_seq_points (td, in_bb);
1187 // Union sequence points with incoming bb's
1188 for (int i=0; i < in_bb->num_pred_seq_points; i++) {
1189 if (!g_hash_table_lookup (seen, in_bb->pred_seq_points [i])) {
1190 g_array_append_val (predecessors, in_bb->pred_seq_points [i]);
1191 g_hash_table_insert (seen, in_bb->pred_seq_points [i], (gpointer)&MONO_SEQ_SEEN_LOOP);
1194 // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
1197 g_hash_table_destroy (seen);
1199 if (predecessors->len != 0) {
1200 bb->pred_seq_points = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint *) * predecessors->len);
1201 bb->num_pred_seq_points = predecessors->len;
1203 for (int newer = 0; newer < bb->num_pred_seq_points; newer++) {
1204 bb->pred_seq_points [newer] = g_array_index (predecessors, gpointer, newer);
1208 g_array_free (predecessors, TRUE);
1212 collect_pred_seq_points (TransformData *td, InterpBasicBlock *bb, SeqPoint *seqp, GSList **next)
1214 // Doesn't have a last sequence point, must find from incoming basic blocks
1215 if (bb->pred_seq_points == NULL && bb != td->entry_bb)
1216 recursively_make_pred_seq_points (td, bb);
1218 for (int i = 0; i < bb->num_pred_seq_points; i++)
1219 insert_pred_seq_point (bb->pred_seq_points [i], seqp, next);
1225 save_seq_points (TransformData *td)
1227 InterpMethod *rtm = td->rtm;
1229 int i, seq_info_size;
1230 MonoSeqPointInfo *info;
1231 MonoDomain *domain = mono_domain_get ();
1232 GSList **next = NULL;
1235 if (!td->gen_sdb_seq_points)
1239 * For each sequence point, compute the list of sequence points immediately
1240 * following it, this is needed to implement 'step over' in the debugger agent.
1241 * Similar to the code in mono_save_seq_point_info ().
1243 for (i = 0; i < td->seq_points->len; ++i) {
1244 SeqPoint *sp = g_ptr_array_index (td->seq_points, i);
1246 /* Store the seq point index here temporarily */
1247 sp->next_offset = i;
1249 next = mono_mempool_alloc0 (td->mempool, sizeof (GList*) * td->seq_points->len);
1250 for (bblist = td->basic_blocks; bblist; bblist = bblist->next) {
1251 InterpBasicBlock *bb = bblist->data;
1253 GSList *bb_seq_points = g_slist_reverse (bb->seq_points);
1254 SeqPoint *last = NULL;
1255 for (GSList *l = bb_seq_points; l; l = l->next) {
1256 SeqPoint *sp = l->data;
1258 if (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET)
1259 /* Used to implement method entry/exit events */
1263 /* Link with the previous seq point in the same bb */
1264 next [last->next_offset] = g_slist_append_mempool (td->mempool, next [last->next_offset], GINT_TO_POINTER (sp->next_offset));
1266 /* Link with the last bb in the previous bblocks */
1267 collect_pred_seq_points (td, bb, sp, next);
1273 /* Serialize the seq points into a byte array */
1274 array = g_byte_array_new ();
1275 SeqPoint zero_seq_point = {0};
1276 SeqPoint* last_seq_point = &zero_seq_point;
1277 for (i = 0; i < td->seq_points->len; ++i) {
1278 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1280 sp->next_offset = 0;
1281 if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next [i], TRUE))
1282 last_seq_point = sp;
1285 if (td->verbose_level) {
1286 g_print ("\nSEQ POINT MAP FOR %s: \n", td->method->name);
1288 for (i = 0; i < td->seq_points->len; ++i) {
1289 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1295 g_print ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
1296 for (l = next [i]; l; l = l->next) {
1297 int next_index = GPOINTER_TO_UINT (l->data);
1298 g_print (" IL0x%x", ((SeqPoint*)g_ptr_array_index (td->seq_points, next_index))->il_offset);
1304 info = mono_seq_point_info_new (array->len, TRUE, array->data, TRUE, &seq_info_size);
1305 mono_jit_stats.allocated_seq_points_size += seq_info_size;
1307 g_byte_array_free (array, TRUE);
1309 mono_domain_lock (domain);
1310 g_hash_table_insert (domain_jit_info (domain)->seq_points, rtm->method, info);
1311 mono_domain_unlock (domain);
1315 emit_seq_point (TransformData *td, int il_offset, InterpBasicBlock *cbb, gboolean nonempty_stack)
1319 seqp = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint));
1320 seqp->il_offset = il_offset;
1321 seqp->native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1323 seqp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
1325 ADD_CODE (td, MINT_SDB_SEQ_POINT);
1326 g_ptr_array_add (td->seq_points, seqp);
1328 cbb->seq_points = g_slist_prepend_mempool (td->mempool, cbb->seq_points, seqp);
1329 cbb->last_seq_point = seqp;
1332 #define BARRIER_IF_VOLATILE(td) \
1335 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER); \
1336 volatile_ = FALSE; \
1341 generate (MonoMethod *method, InterpMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
1343 MonoMethodHeader *header = mono_method_get_header (method);
1344 MonoMethodSignature *signature = mono_method_signature (method);
1345 MonoImage *image = method->klass->image;
1346 MonoDomain *domain = rtm->domain;
1347 MonoClass *constrained_class = NULL;
1349 int offset, mt, i, i32;
1350 gboolean readonly = FALSE;
1351 gboolean volatile_ = FALSE;
1353 MonoClassField *field;
1354 const unsigned char *end;
1355 int new_in_start_offset;
1356 int body_start_offset;
1359 TransformData transform_data;
1361 int generating_code = 1;
1362 GArray *line_numbers;
1363 MonoDebugMethodInfo *minfo;
1364 MonoBitSet *seq_point_locs = NULL;
1365 MonoBitSet *seq_point_set_locs = NULL;
1366 gboolean sym_seq_points = FALSE;
1367 InterpBasicBlock *bb_exit = NULL;
1368 static gboolean verbose_method_inited;
1369 static char* verbose_method_name;
1371 if (!verbose_method_inited) {
1372 verbose_method_name = getenv ("MONO_VERBOSE_METHOD");
1373 verbose_method_inited = TRUE;
1376 memset (&transform_data, 0, sizeof(transform_data));
1377 td = &transform_data;
1379 td->method = method;
1381 td->is_bb_start = is_bb_start;
1382 td->il_code = header->code;
1383 td->code_size = header->code_size;
1384 td->header = header;
1385 td->max_code_size = td->code_size;
1386 td->new_code = (unsigned short *)g_malloc(td->max_code_size * sizeof(gushort));
1387 td->new_code_end = td->new_code + td->max_code_size;
1388 td->mempool = mono_mempool_new ();
1389 td->in_offsets = g_malloc0(header->code_size * sizeof(int));
1390 td->stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
1391 td->stack_height = g_malloc(header->code_size * sizeof(int));
1392 td->vt_stack_size = g_malloc(header->code_size * sizeof(int));
1393 td->n_data_items = 0;
1394 td->max_data_items = 0;
1395 td->data_items = NULL;
1396 td->data_hash = g_hash_table_new (NULL, NULL);
1397 td->clause_indexes = g_malloc (header->code_size * sizeof (int));
1398 td->gen_sdb_seq_points = debug_options.gen_sdb_seq_points;
1399 td->seq_points = g_ptr_array_new ();
1400 td->relocs = g_ptr_array_new ();
1401 td->verbose_level = mono_interp_traceopt;
1402 rtm->data_items = td->data_items;
1403 for (i = 0; i < header->code_size; i++) {
1404 td->stack_height [i] = -1;
1405 td->clause_indexes [i] = -1;
1408 if (verbose_method_name) {
1409 const char *name = verbose_method_name;
1411 if ((strchr (name, '.') > name) || strchr (name, ':')) {
1412 MonoMethodDesc *desc;
1414 desc = mono_method_desc_new (name, TRUE);
1415 if (mono_method_desc_full_match (desc, method)) {
1416 td->verbose_level = 4;
1418 mono_method_desc_free (desc);
1420 if (strcmp (method->name, name) == 0)
1421 td->verbose_level = 4;
1425 if (td->gen_sdb_seq_points) {
1426 get_basic_blocks (td);
1428 minfo = mono_debug_lookup_method (method);
1431 MonoSymSeqPoint *sps;
1432 int i, n_il_offsets;
1434 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
1436 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1437 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1438 sym_seq_points = TRUE;
1440 for (i = 0; i < n_il_offsets; ++i) {
1441 if (sps [i].il_offset < header->code_size)
1442 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
1445 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
1446 /* Methods without line number info like auto-generated property accessors */
1447 seq_point_locs = mono_bitset_new (header->code_size, 0);
1448 seq_point_set_locs = mono_bitset_new (header->code_size, 0);
1449 sym_seq_points = TRUE;
1453 td->new_ip = td->new_code;
1454 td->last_new_ip = NULL;
1456 td->stack = g_malloc0 ((header->max_stack + 1) * sizeof (td->stack [0]));
1458 td->max_stack_height = 0;
1460 line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
1462 for (i = 0; i < header->num_clauses; i++) {
1463 MonoExceptionClause *c = header->clauses + i;
1464 td->stack_height [c->handler_offset] = 0;
1465 td->vt_stack_size [c->handler_offset] = 0;
1466 td->is_bb_start [c->handler_offset] = 1;
1468 td->stack_height [c->handler_offset] = 1;
1469 td->stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
1470 td->stack_state [c->handler_offset][0].type = STACK_TYPE_O;
1471 td->stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
1473 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
1474 td->stack_height [c->data.filter_offset] = 0;
1475 td->vt_stack_size [c->data.filter_offset] = 0;
1476 td->is_bb_start [c->data.filter_offset] = 1;
1478 td->stack_height [c->data.filter_offset] = 1;
1479 td->stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
1480 td->stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
1481 td->stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
1484 for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
1485 if (td->clause_indexes [j] == -1)
1486 td->clause_indexes [j] = i;
1490 td->ip = header->code;
1491 end = td->ip + header->code_size;
1493 if (td->verbose_level) {
1494 char *tmp = mono_disasm_code (NULL, method, td->ip, end);
1495 char *name = mono_method_full_name (method, TRUE);
1496 g_print ("Method %s, original code:\n", name);
1497 g_print ("%s\n", tmp);
1502 if (signature->hasthis)
1503 store_inarg (td, 0);
1504 for (i = 0; i < signature->param_count; i++)
1505 store_inarg (td, i + !!signature->hasthis);
1507 body_start_offset = td->new_ip - td->new_code;
1509 for (i = 0; i < header->num_locals; i++) {
1510 int mt = mint_type(header->locals [i]);
1511 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1512 ADD_CODE(td, MINT_INITLOCALS);
1517 if (rtm->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_ENTER)
1518 ADD_CODE (td, MINT_PROF_ENTER);
1520 if (sym_seq_points) {
1521 InterpBasicBlock *cbb = td->offset_to_bb [0];
1523 emit_seq_point (td, METHOD_ENTRY_IL_OFFSET, cbb, FALSE);
1526 while (td->ip < end) {
1529 g_assert (td->sp >= td->stack);
1530 g_assert (td->vt_sp < 0x10000000);
1531 in_offset = td->ip - header->code;
1532 td->in_offsets [in_offset] = td->new_ip - td->new_code;
1533 new_in_start_offset = td->new_ip - td->new_code;
1534 td->in_start = td->ip;
1536 MonoDebugLineNumberEntry lne;
1537 lne.native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1538 lne.il_offset = in_offset;
1539 g_array_append_val (line_numbers, lne);
1541 if (td->stack_height [in_offset] >= 0) {
1542 g_assert (is_bb_start [in_offset]);
1543 if (td->stack_height [in_offset] > 0)
1544 memcpy (td->stack, td->stack_state [in_offset], td->stack_height [in_offset] * sizeof(td->stack [0]));
1545 td->sp = td->stack + td->stack_height [in_offset];
1546 td->vt_sp = td->vt_stack_size [in_offset];
1548 if (is_bb_start [in_offset]) {
1549 generating_code = 1;
1551 if (!generating_code) {
1552 while (td->ip < end && !is_bb_start [td->ip - td->il_code])
1556 if (td->verbose_level > 1) {
1557 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1558 td->ip - td->il_code,
1559 td->is_bb_start [td->ip - td->il_code] == 3 ? "<>" :
1560 td->is_bb_start [td->ip - td->il_code] == 2 ? "< " :
1561 td->is_bb_start [td->ip - td->il_code] == 1 ? " >" : " ",
1562 mono_opcode_name (*td->ip), td->new_ip - td->new_code, td->sp - td->stack,
1563 td->sp > td->stack ? stack_type_string [td->sp [-1].type] : " ",
1564 (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) : "",
1565 td->vt_sp, td->max_vt_sp);
1568 if (sym_seq_points && mono_bitset_test_fast (seq_point_locs, td->ip - header->code)) {
1569 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1573 * Make methods interruptable at the beginning, and at the targets of
1574 * backward branches.
1576 if (in_offset == 0 || g_slist_length (cbb->preds) > 1)
1577 ADD_CODE (td, MINT_SDB_INTR_LOC);
1579 emit_seq_point (td, in_offset, cbb, FALSE);
1581 mono_bitset_set_fast (seq_point_set_locs, td->ip - header->code);
1585 bb_exit = td->offset_to_bb [td->ip - header->code];
1593 SIMPLE_OP(td, MINT_BREAK);
1599 load_arg (td, *td->ip - CEE_LDARG_0);
1606 load_local (td, *td->ip - CEE_LDLOC_0);
1613 store_local (td, *td->ip - CEE_STLOC_0);
1617 load_arg (td, ((guint8 *)td->ip)[1]);
1620 case CEE_LDARGA_S: {
1621 /* NOTE: n includes this */
1622 int n = ((guint8 *) td->ip) [1];
1623 ADD_CODE (td, MINT_LDARGA);
1624 ADD_CODE (td, td->rtm->arg_offsets [n]);
1625 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1630 store_arg (td, ((guint8 *)td->ip)[1]);
1634 load_local (td, ((guint8 *)td->ip)[1]);
1638 ADD_CODE(td, MINT_LDLOCA_S);
1639 ADD_CODE(td, td->rtm->local_offsets [((guint8 *)td->ip)[1]]);
1640 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1644 store_local (td, ((guint8 *)td->ip)[1]);
1648 SIMPLE_OP(td, MINT_LDNULL);
1649 PUSH_TYPE(td, STACK_TYPE_O, NULL);
1652 SIMPLE_OP(td, MINT_LDC_I4_M1);
1653 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1656 if (!td->is_bb_start[td->ip + 1 - td->il_code] && td->ip [1] == 0xfe && td->ip [2] == CEE_CEQ &&
1657 td->sp > td->stack && td->sp [-1].type == STACK_TYPE_I4) {
1658 SIMPLE_OP(td, MINT_CEQ0_I4);
1661 SIMPLE_OP(td, MINT_LDC_I4_0);
1662 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1666 if (!td->is_bb_start[td->ip + 1 - td->il_code] &&
1667 (td->ip [1] == CEE_ADD || td->ip [1] == CEE_SUB) && td->sp [-1].type == STACK_TYPE_I4) {
1668 ADD_CODE(td, td->ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1671 SIMPLE_OP(td, MINT_LDC_I4_1);
1672 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1682 SIMPLE_OP(td, (*td->ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1683 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1686 ADD_CODE(td, MINT_LDC_I4_S);
1687 ADD_CODE(td, ((gint8 *) td->ip) [1]);
1689 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1692 i32 = read32 (td->ip + 1);
1693 ADD_CODE(td, MINT_LDC_I4);
1696 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1699 gint64 val = read64 (td->ip + 1);
1700 ADD_CODE(td, MINT_LDC_I8);
1703 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I8);
1708 readr4 (td->ip + 1, &val);
1709 ADD_CODE(td, MINT_LDC_R4);
1712 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
1717 readr8 (td->ip + 1, &val);
1718 ADD_CODE(td, MINT_LDC_R8);
1721 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
1725 int type = td->sp [-1].type;
1726 MonoClass *klass = td->sp [-1].klass;
1727 if (td->sp [-1].type == STACK_TYPE_VT) {
1728 gint32 size = mono_class_value_size (klass, NULL);
1730 ADD_CODE(td, MINT_DUP_VT);
1734 SIMPLE_OP(td, MINT_DUP);
1735 PUSH_TYPE(td, type, klass);
1740 SIMPLE_OP(td, MINT_POP);
1742 if (td->sp [-1].type == STACK_TYPE_VT) {
1743 int size = mono_class_value_size (td->sp [-1].klass, NULL);
1744 size = (size + 7) & ~7;
1745 ADD_CODE(td, MINT_VTRESULT);
1754 if (td->sp > td->stack)
1755 g_warning ("CEE_JMP: stack must be empty");
1756 token = read32 (td->ip + 1);
1757 m = mono_get_method_full (image, token, NULL, generic_context);
1758 ADD_CODE (td, MINT_JMP);
1759 ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
1760 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1764 case CEE_CALLVIRT: /* Fall through */
1765 case CEE_CALLI: /* Fall through */
1767 gboolean need_seq_point = FALSE;
1769 if (sym_seq_points && !mono_bitset_test_fast (seq_point_locs, td->ip + 5 - header->code))
1770 need_seq_point = TRUE;
1772 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1774 if (need_seq_point) {
1775 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1778 emit_seq_point (td, td->ip - header->code, cbb, TRUE);
1781 constrained_class = NULL;
1787 if (signature->ret->type != MONO_TYPE_VOID) {
1789 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1790 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1791 vt_size = mono_class_value_size (klass, NULL);
1792 vt_size = (vt_size + 7) & ~7;
1795 if (td->sp > td->stack)
1796 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td->method->klass->name, td->method->name, td->sp - td->stack);
1797 if (td->vt_sp != vt_size)
1798 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td->method->klass->name, td->method->name, td->vt_sp, vt_size);
1800 if (sym_seq_points) {
1801 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1803 emit_seq_point (td, METHOD_EXIT_IL_OFFSET, bb_exit, FALSE);
1807 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1809 ADD_CODE(td, MINT_RET_VT);
1810 WRITE32(td, &vt_size);
1813 generating_code = 0;
1817 handle_branch (td, MINT_BR_S, MINT_BR, 5 + read32 (td->ip + 1));
1819 generating_code = 0;
1822 handle_branch (td, MINT_BR_S, MINT_BR, 2 + (gint8)td->ip [1]);
1824 generating_code = 0;
1827 one_arg_branch (td, MINT_BRFALSE_I4, 5 + read32 (td->ip + 1));
1831 one_arg_branch (td, MINT_BRFALSE_I4, 2 + (gint8)td->ip [1]);
1835 one_arg_branch (td, MINT_BRTRUE_I4, 5 + read32 (td->ip + 1));
1839 one_arg_branch (td, MINT_BRTRUE_I4, 2 + (gint8)td->ip [1]);
1843 two_arg_branch (td, MINT_BEQ_I4, 5 + read32 (td->ip + 1));
1847 two_arg_branch (td, MINT_BEQ_I4, 2 + (gint8) td->ip [1]);
1851 two_arg_branch (td, MINT_BGE_I4, 5 + read32 (td->ip + 1));
1855 two_arg_branch (td, MINT_BGE_I4, 2 + (gint8) td->ip [1]);
1859 two_arg_branch (td, MINT_BGT_I4, 5 + read32 (td->ip + 1));
1863 two_arg_branch (td, MINT_BGT_I4, 2 + (gint8) td->ip [1]);
1867 two_arg_branch (td, MINT_BLT_I4, 5 + read32 (td->ip + 1));
1871 two_arg_branch (td, MINT_BLT_I4, 2 + (gint8) td->ip [1]);
1875 two_arg_branch (td, MINT_BLE_I4, 5 + read32 (td->ip + 1));
1879 two_arg_branch (td, MINT_BLE_I4, 2 + (gint8) td->ip [1]);
1883 two_arg_branch (td, MINT_BNE_UN_I4, 5 + read32 (td->ip + 1));
1887 two_arg_branch (td, MINT_BNE_UN_I4, 2 + (gint8) td->ip [1]);
1891 two_arg_branch (td, MINT_BGE_UN_I4, 5 + read32 (td->ip + 1));
1895 two_arg_branch (td, MINT_BGE_UN_I4, 2 + (gint8) td->ip [1]);
1899 two_arg_branch (td, MINT_BGT_UN_I4, 5 + read32 (td->ip + 1));
1903 two_arg_branch (td, MINT_BGT_UN_I4, 2 + (gint8) td->ip [1]);
1907 two_arg_branch (td, MINT_BLE_UN_I4, 5 + read32 (td->ip + 1));
1911 two_arg_branch (td, MINT_BLE_UN_I4, 2 + (gint8) td->ip [1]);
1915 two_arg_branch (td, MINT_BLT_UN_I4, 5 + read32 (td->ip + 1));
1919 two_arg_branch (td, MINT_BLT_UN_I4, 2 + (gint8) td->ip [1]);
1924 const unsigned char *next_ip;
1926 n = read32 (td->ip);
1927 ADD_CODE (td, MINT_SWITCH);
1930 next_ip = td->ip + n * 4;
1932 int stack_height = td->sp - td->stack;
1933 for (i = 0; i < n; i++) {
1934 offset = read32 (td->ip);
1935 target = next_ip - td->il_code + offset;
1938 if (stack_height > 0 && stack_height != td->stack_height [target])
1939 g_warning ("SWITCH with back branch and non-empty stack");
1941 target = td->in_offsets [target] - (td->new_ip - td->new_code);
1943 td->stack_height [target] = stack_height;
1944 td->vt_stack_size [target] = td->vt_sp;
1945 if (stack_height > 0)
1946 td->stack_state [target] = g_memdup (td->stack, stack_height * sizeof (td->stack [0]));
1948 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
1949 reloc->type = RELOC_SWITCH;
1950 reloc->offset = td->new_ip - td->new_code;
1951 reloc->target = target;
1952 g_ptr_array_add (td->relocs, reloc);
1955 WRITE32 (td, &target);
1961 CHECK_STACK (td, 1);
1962 SIMPLE_OP (td, MINT_LDIND_I1);
1963 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1964 BARRIER_IF_VOLATILE (td);
1967 CHECK_STACK (td, 1);
1968 SIMPLE_OP (td, MINT_LDIND_U1);
1969 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1970 BARRIER_IF_VOLATILE (td);
1973 CHECK_STACK (td, 1);
1974 SIMPLE_OP (td, MINT_LDIND_I2);
1975 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1976 BARRIER_IF_VOLATILE (td);
1979 CHECK_STACK (td, 1);
1980 SIMPLE_OP (td, MINT_LDIND_U2);
1981 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1982 BARRIER_IF_VOLATILE (td);
1985 CHECK_STACK (td, 1);
1986 SIMPLE_OP (td, MINT_LDIND_I4);
1987 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1988 BARRIER_IF_VOLATILE (td);
1991 CHECK_STACK (td, 1);
1992 SIMPLE_OP (td, MINT_LDIND_U4);
1993 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
1994 BARRIER_IF_VOLATILE (td);
1997 CHECK_STACK (td, 1);
1998 SIMPLE_OP (td, MINT_LDIND_I8);
1999 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2000 BARRIER_IF_VOLATILE (td);
2003 CHECK_STACK (td, 1);
2004 SIMPLE_OP (td, MINT_LDIND_I);
2006 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2007 BARRIER_IF_VOLATILE (td);
2010 CHECK_STACK (td, 1);
2011 SIMPLE_OP (td, MINT_LDIND_R4);
2012 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2013 BARRIER_IF_VOLATILE (td);
2016 CHECK_STACK (td, 1);
2017 SIMPLE_OP (td, MINT_LDIND_R8);
2018 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2019 BARRIER_IF_VOLATILE (td);
2022 CHECK_STACK (td, 1);
2023 SIMPLE_OP (td, MINT_LDIND_REF);
2024 BARRIER_IF_VOLATILE (td);
2025 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
2028 CHECK_STACK (td, 2);
2029 BARRIER_IF_VOLATILE (td);
2030 SIMPLE_OP (td, MINT_STIND_REF);
2034 CHECK_STACK (td, 2);
2035 BARRIER_IF_VOLATILE (td);
2036 SIMPLE_OP (td, MINT_STIND_I1);
2040 CHECK_STACK (td, 2);
2041 BARRIER_IF_VOLATILE (td);
2042 SIMPLE_OP (td, MINT_STIND_I2);
2046 CHECK_STACK (td, 2);
2047 BARRIER_IF_VOLATILE (td);
2048 SIMPLE_OP (td, MINT_STIND_I4);
2052 CHECK_STACK (td, 2);
2053 BARRIER_IF_VOLATILE (td);
2054 SIMPLE_OP (td, MINT_STIND_I);
2058 CHECK_STACK (td, 2);
2059 BARRIER_IF_VOLATILE (td);
2060 SIMPLE_OP (td, MINT_STIND_I8);
2064 CHECK_STACK (td, 2);
2065 BARRIER_IF_VOLATILE (td);
2066 SIMPLE_OP (td, MINT_STIND_R4);
2070 CHECK_STACK (td, 2);
2071 BARRIER_IF_VOLATILE (td);
2072 SIMPLE_OP (td, MINT_STIND_R8);
2076 binary_arith_op(td, MINT_ADD_I4);
2080 binary_arith_op(td, MINT_SUB_I4);
2084 binary_arith_op(td, MINT_MUL_I4);
2088 binary_arith_op(td, MINT_DIV_I4);
2092 binary_arith_op(td, MINT_DIV_UN_I4);
2096 binary_arith_op (td, MINT_REM_I4);
2100 binary_arith_op (td, MINT_REM_UN_I4);
2104 binary_arith_op (td, MINT_AND_I4);
2108 binary_arith_op (td, MINT_OR_I4);
2112 binary_arith_op (td, MINT_XOR_I4);
2116 shift_op (td, MINT_SHL_I4);
2120 shift_op (td, MINT_SHR_I4);
2124 shift_op (td, MINT_SHR_UN_I4);
2128 unary_arith_op (td, MINT_NEG_I4);
2132 unary_arith_op (td, MINT_NOT_I4);
2136 CHECK_STACK (td, 1);
2137 switch (td->sp [-1].type) {
2139 ADD_CODE(td, MINT_CONV_U1_R8);
2142 ADD_CODE(td, MINT_CONV_U1_I4);
2145 ADD_CODE(td, MINT_CONV_U1_I8);
2148 g_assert_not_reached ();
2151 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2154 CHECK_STACK (td, 1);
2155 switch (td->sp [-1].type) {
2157 ADD_CODE(td, MINT_CONV_I1_R8);
2160 ADD_CODE(td, MINT_CONV_I1_I4);
2163 ADD_CODE(td, MINT_CONV_I1_I8);
2166 g_assert_not_reached ();
2169 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2172 CHECK_STACK (td, 1);
2173 switch (td->sp [-1].type) {
2175 ADD_CODE(td, MINT_CONV_U2_R8);
2178 ADD_CODE(td, MINT_CONV_U2_I4);
2181 ADD_CODE(td, MINT_CONV_U2_I8);
2184 g_assert_not_reached ();
2187 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2190 CHECK_STACK (td, 1);
2191 switch (td->sp [-1].type) {
2193 ADD_CODE(td, MINT_CONV_I2_R8);
2196 ADD_CODE(td, MINT_CONV_I2_I4);
2199 ADD_CODE(td, MINT_CONV_I2_I8);
2202 g_assert_not_reached ();
2205 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2208 CHECK_STACK (td, 1);
2209 switch (td->sp [-1].type) {
2211 #if SIZEOF_VOID_P == 4
2212 ADD_CODE(td, MINT_CONV_U4_R8);
2214 ADD_CODE(td, MINT_CONV_U8_R8);
2218 #if SIZEOF_VOID_P == 8
2219 ADD_CODE(td, MINT_CONV_U8_I4);
2223 #if SIZEOF_VOID_P == 4
2224 ADD_CODE(td, MINT_CONV_U4_I8);
2230 g_assert_not_reached ();
2233 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2236 CHECK_STACK (td, 1);
2237 switch (td->sp [-1].type) {
2239 #if SIZEOF_VOID_P == 8
2240 ADD_CODE(td, MINT_CONV_I8_R8);
2242 ADD_CODE(td, MINT_CONV_I4_R8);
2246 #if SIZEOF_VOID_P == 8
2247 ADD_CODE(td, MINT_CONV_I8_I4);
2255 #if SIZEOF_VOID_P == 4
2256 ADD_CODE(td, MINT_CONV_I4_I8);
2260 g_assert_not_reached ();
2263 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2266 CHECK_STACK (td, 1);
2267 switch (td->sp [-1].type) {
2269 ADD_CODE(td, MINT_CONV_U4_R8);
2274 ADD_CODE(td, MINT_CONV_U4_I8);
2277 #if SIZEOF_VOID_P == 8
2278 ADD_CODE(td, MINT_CONV_U4_I8);
2282 g_assert_not_reached ();
2285 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2288 CHECK_STACK (td, 1);
2289 switch (td->sp [-1].type) {
2291 ADD_CODE(td, MINT_CONV_I4_R8);
2296 ADD_CODE(td, MINT_CONV_I4_I8);
2299 #if SIZEOF_VOID_P == 8
2300 ADD_CODE(td, MINT_CONV_I4_I8);
2304 g_assert_not_reached ();
2307 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2310 CHECK_STACK (td, 1);
2311 switch (td->sp [-1].type) {
2313 ADD_CODE(td, MINT_CONV_I8_R8);
2316 ADD_CODE(td, MINT_CONV_I8_I4);
2321 #if SIZEOF_VOID_P == 4
2322 ADD_CODE(td, MINT_CONV_I8_I4);
2326 g_assert_not_reached ();
2329 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2332 CHECK_STACK (td, 1);
2333 switch (td->sp [-1].type) {
2335 ADD_CODE(td, MINT_CONV_R4_R8);
2338 ADD_CODE(td, MINT_CONV_R4_I8);
2341 ADD_CODE(td, MINT_CONV_R4_I4);
2344 g_assert_not_reached ();
2347 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2350 CHECK_STACK (td, 1);
2351 switch (td->sp [-1].type) {
2353 ADD_CODE(td, MINT_CONV_R8_I4);
2356 ADD_CODE(td, MINT_CONV_R8_I8);
2361 g_assert_not_reached ();
2364 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2367 CHECK_STACK (td, 1);
2368 switch (td->sp [-1].type) {
2370 ADD_CODE(td, MINT_CONV_U8_I4);
2375 ADD_CODE(td, MINT_CONV_U8_R8);
2378 #if SIZEOF_VOID_P == 4
2379 ADD_CODE(td, MINT_CONV_U8_I4);
2383 g_assert_not_reached ();
2386 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2389 CHECK_STACK (td, 2);
2391 token = read32 (td->ip + 1);
2392 klass = mono_class_get_full (image, token, generic_context);
2394 if (klass->valuetype) {
2395 ADD_CODE (td, MINT_CPOBJ);
2396 ADD_CODE (td, get_data_item_index(td, klass));
2398 ADD_CODE (td, MINT_LDIND_REF);
2399 ADD_CODE (td, MINT_STIND_REF);
2407 CHECK_STACK (td, 1);
2409 token = read32 (td->ip + 1);
2411 if (method->wrapper_type != MONO_WRAPPER_NONE)
2412 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2414 klass = mono_class_get_full (image, token, generic_context);
2416 ADD_CODE(td, MINT_LDOBJ);
2417 ADD_CODE(td, get_data_item_index(td, klass));
2418 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2419 size = mono_class_value_size (klass, NULL);
2423 SET_TYPE(td->sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
2424 BARRIER_IF_VOLATILE (td);
2429 token = mono_metadata_token_index (read32 (td->ip + 1));
2431 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
2432 s = mono_method_get_wrapper_data (method, token);
2433 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
2434 s = mono_string_new_wrapper (mono_method_get_wrapper_data (method, token));
2436 s = mono_ldstr (domain, image, token);
2438 ADD_CODE(td, MINT_LDSTR);
2439 ADD_CODE(td, get_data_item_index (td, s));
2440 PUSH_TYPE(td, STACK_TYPE_O, mono_defaults.string_class);
2445 MonoMethodSignature *csignature;
2446 guint32 vt_stack_used = 0;
2447 guint32 vt_res_size = 0;
2450 token = read32 (td->ip);
2453 if (method->wrapper_type != MONO_WRAPPER_NONE)
2454 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2456 m = mono_get_method_full (image, token, NULL, generic_context);
2458 csignature = mono_method_signature (m);
2460 td->sp -= csignature->param_count;
2461 ADD_CODE(td, MINT_NEWOBJ);
2462 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2463 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2465 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2466 vt_res_size = mono_class_value_size (klass, NULL);
2467 PUSH_VT (td, vt_res_size);
2469 for (i = 0; i < csignature->param_count; ++i) {
2470 int mt = mint_type(csignature->params [i]);
2471 if (mt == MINT_TYPE_VT) {
2472 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
2473 gint32 size = mono_class_value_size (k, NULL);
2474 size = (size + 7) & ~7;
2475 vt_stack_used += size;
2478 if (vt_stack_used != 0 || vt_res_size != 0) {
2479 ADD_CODE(td, MINT_VTRESULT);
2480 ADD_CODE(td, vt_res_size);
2481 WRITE32(td, &vt_stack_used);
2482 td->vt_sp -= vt_stack_used;
2484 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2488 CHECK_STACK (td, 1);
2489 token = read32 (td->ip + 1);
2490 klass = mini_get_class (method, token, generic_context);
2491 ADD_CODE(td, MINT_CASTCLASS);
2492 ADD_CODE(td, get_data_item_index (td, klass));
2493 td->sp [-1].klass = klass;
2497 CHECK_STACK (td, 1);
2498 token = read32 (td->ip + 1);
2499 klass = mini_get_class (method, token, generic_context);
2500 ADD_CODE(td, MINT_ISINST);
2501 ADD_CODE(td, get_data_item_index (td, klass));
2505 switch (td->sp [-1].type) {
2509 ADD_CODE(td, MINT_CONV_R_UN_I8);
2512 ADD_CODE(td, MINT_CONV_R_UN_I4);
2515 g_assert_not_reached ();
2517 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2521 CHECK_STACK (td, 1);
2522 token = read32 (td->ip + 1);
2524 if (method->wrapper_type != MONO_WRAPPER_NONE)
2525 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2527 klass = mono_class_get_full (image, token, generic_context);
2529 if (mono_class_is_nullable (klass)) {
2530 g_error ("cee_unbox: implement Nullable");
2533 ADD_CODE(td, MINT_UNBOX);
2534 ADD_CODE(td, get_data_item_index (td, klass));
2535 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2539 CHECK_STACK (td, 1);
2540 token = read32 (td->ip + 1);
2542 klass = mini_get_class (method, token, generic_context);
2544 if (mini_type_is_reference (&klass->byval_arg)) {
2545 int mt = mint_type (&klass->byval_arg);
2546 ADD_CODE (td, MINT_CASTCLASS);
2547 ADD_CODE (td, get_data_item_index (td, klass));
2548 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2550 } else if (mono_class_is_nullable (klass)) {
2551 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2552 /* td->ip is incremented by interp_transform_call */
2553 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2555 int mt = mint_type (&klass->byval_arg);
2556 ADD_CODE (td, MINT_UNBOX);
2557 ADD_CODE (td, get_data_item_index (td, klass));
2559 ADD_CODE (td, MINT_LDOBJ);
2560 ADD_CODE (td, get_data_item_index(td, klass));
2561 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2563 if (mt == MINT_TYPE_VT) {
2564 int size = mono_class_value_size (klass, NULL);
2572 CHECK_STACK (td, 1);
2573 SIMPLE_OP (td, MINT_THROW);
2575 generating_code = 0;
2578 CHECK_STACK (td, 1);
2579 token = read32 (td->ip + 1);
2580 field = interp_field_from_token (method, token, &klass, generic_context);
2581 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2582 mono_class_init (klass);
2584 ADD_CODE (td, MINT_POP);
2586 ADD_CODE (td, MINT_LDSFLDA);
2587 ADD_CODE (td, get_data_item_index (td, field));
2589 if ((td->sp - 1)->type == STACK_TYPE_O) {
2590 ADD_CODE (td, MINT_LDFLDA);
2592 g_assert ((td->sp -1)->type == STACK_TYPE_MP);
2593 ADD_CODE (td, MINT_LDFLDA_UNSAFE);
2595 ADD_CODE (td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2598 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2601 CHECK_STACK (td, 1);
2602 token = read32 (td->ip + 1);
2603 field = interp_field_from_token (method, token, &klass, generic_context);
2604 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2605 mono_class_init (klass);
2607 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2608 mt = mint_type (&field_klass->byval_arg);
2609 #ifndef DISABLE_REMOTING
2610 if (klass->marshalbyref) {
2611 g_assert (!is_static);
2612 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2613 ADD_CODE(td, get_data_item_index (td, field));
2618 ADD_CODE (td, MINT_POP);
2620 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2621 ADD_CODE (td, get_data_item_index (td, field));
2623 ADD_CODE (td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2624 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2627 if (mt == MINT_TYPE_VT) {
2628 int size = mono_class_value_size (field_klass, NULL);
2632 if (td->sp [-1].type == STACK_TYPE_VT) {
2633 int size = mono_class_value_size (klass, NULL);
2634 size = (size + 7) & ~7;
2636 ADD_CODE (td, MINT_VTRESULT);
2638 WRITE32 (td, &size);
2641 SET_TYPE(td->sp - 1, stack_type [mt], field_klass);
2642 BARRIER_IF_VOLATILE (td);
2646 CHECK_STACK (td, 2);
2647 token = read32 (td->ip + 1);
2648 field = interp_field_from_token (method, token, &klass, generic_context);
2649 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2650 mono_class_init (klass);
2651 mt = mint_type(field->type);
2653 BARRIER_IF_VOLATILE (td);
2655 #ifndef DISABLE_REMOTING
2656 if (klass->marshalbyref) {
2657 g_assert (!is_static);
2658 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2659 ADD_CODE(td, get_data_item_index (td, field));
2664 ADD_CODE (td, MINT_POP);
2666 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2667 ADD_CODE (td, get_data_item_index (td, field));
2669 ADD_CODE (td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2670 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2673 if (mt == MINT_TYPE_VT) {
2674 MonoClass *klass = mono_class_from_mono_type (field->type);
2675 int size = mono_class_value_size (klass, NULL);
2684 token = read32 (td->ip + 1);
2685 field = interp_field_from_token (method, token, &klass, generic_context);
2686 ADD_CODE(td, MINT_LDSFLDA);
2687 ADD_CODE(td, get_data_item_index (td, field));
2689 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
2692 token = read32 (td->ip + 1);
2693 field = interp_field_from_token (method, token, &klass, generic_context);
2694 mt = mint_type(field->type);
2695 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2696 ADD_CODE(td, get_data_item_index (td, field));
2698 if (mt == MINT_TYPE_VT) {
2699 MonoClass *klass = mono_class_from_mono_type (field->type);
2700 int size = mono_class_value_size (klass, NULL);
2703 klass = field->type->data.klass;
2705 if (mt == MINT_TYPE_O)
2706 klass = mono_class_from_mono_type (field->type);
2709 PUSH_TYPE(td, stack_type [mt], klass);
2712 CHECK_STACK (td, 1);
2713 token = read32 (td->ip + 1);
2714 field = interp_field_from_token (method, token, &klass, generic_context);
2715 mt = mint_type(field->type);
2716 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2717 ADD_CODE(td, get_data_item_index (td, field));
2718 if (mt == MINT_TYPE_VT) {
2719 MonoClass *klass = mono_class_from_mono_type (field->type);
2720 int size = mono_class_value_size (klass, NULL);
2722 WRITE32 (td, &size);
2729 token = read32 (td->ip + 1);
2731 if (method->wrapper_type != MONO_WRAPPER_NONE)
2732 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2734 klass = mini_get_class (method, token, generic_context);
2736 BARRIER_IF_VOLATILE (td);
2737 ADD_CODE(td, td->sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2738 ADD_CODE(td, get_data_item_index (td, klass));
2739 if (td->sp [-1].type == STACK_TYPE_VT) {
2740 size = mono_class_value_size (klass, NULL);
2741 size = (size + 7) & ~7;
2748 case CEE_CONV_OVF_I_UN:
2749 case CEE_CONV_OVF_U_UN:
2750 CHECK_STACK (td, 1);
2751 switch (td->sp [-1].type) {
2753 #if SIZEOF_VOID_P == 8
2754 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
2756 ADD_CODE(td, MINT_CONV_OVF_I4_UN_R8);
2760 #if SIZEOF_VOID_P == 4
2761 ADD_CODE (td, MINT_CONV_OVF_I4_UN_I8);
2765 #if SIZEOF_VOID_P == 8
2766 ADD_CODE(td, MINT_CONV_I8_U4);
2767 #elif SIZEOF_VOID_P == 4
2768 if (*td->ip == CEE_CONV_OVF_I_UN)
2769 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
2773 g_assert_not_reached ();
2776 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2779 case CEE_CONV_OVF_I8_UN:
2780 case CEE_CONV_OVF_U8_UN:
2781 CHECK_STACK (td, 1);
2782 switch (td->sp [-1].type) {
2784 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
2787 if (*td->ip == CEE_CONV_OVF_I8_UN)
2788 ADD_CODE (td, MINT_CONV_OVF_I8_U8);
2791 ADD_CODE(td, MINT_CONV_I8_U4);
2794 g_assert_not_reached ();
2797 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2802 CHECK_STACK (td, 1);
2803 token = read32 (td->ip + 1);
2804 if (method->wrapper_type != MONO_WRAPPER_NONE)
2805 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2807 klass = mini_get_class (method, token, generic_context);
2809 if (mono_class_is_nullable (klass)) {
2810 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2811 /* td->ip is incremented by interp_transform_call */
2812 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2813 } else if (!klass->valuetype) {
2814 /* already boxed, do nothing. */
2817 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2818 size = mono_class_value_size (klass, NULL);
2819 size = (size + 7) & ~7;
2822 ADD_CODE(td, MINT_BOX);
2823 ADD_CODE(td, get_data_item_index (td, klass));
2825 SET_TYPE(td->sp - 1, STACK_TYPE_O, klass);
2832 CHECK_STACK (td, 1);
2833 token = read32 (td->ip + 1);
2835 if (method->wrapper_type != MONO_WRAPPER_NONE)
2836 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2838 klass = mini_get_class (method, token, generic_context);
2840 unsigned char lentype = (td->sp - 1)->type;
2841 if (lentype == STACK_TYPE_I8) {
2842 /* mimic mini behaviour */
2843 ADD_CODE (td, MINT_CONV_OVF_U4_I8);
2845 g_assert (lentype == STACK_TYPE_I4);
2846 ADD_CODE (td, MINT_CONV_OVF_U4_I4);
2848 SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
2849 ADD_CODE (td, MINT_NEWARR);
2850 ADD_CODE (td, get_data_item_index (td, klass));
2851 SET_TYPE (td->sp - 1, STACK_TYPE_O, klass);
2856 CHECK_STACK (td, 1);
2857 SIMPLE_OP (td, MINT_LDLEN);
2858 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2861 CHECK_STACK (td, 2);
2863 token = read32 (td->ip + 1);
2865 if (method->wrapper_type != MONO_WRAPPER_NONE)
2866 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2868 klass = mini_get_class (method, token, generic_context);
2870 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2871 ADD_CODE (td, MINT_LDELEMA_TC);
2873 ADD_CODE (td, MINT_LDELEMA);
2875 ADD_CODE (td, get_data_item_index (td, klass));
2876 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2882 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2885 CHECK_STACK (td, 2);
2887 SIMPLE_OP (td, MINT_LDELEM_I1);
2889 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2892 CHECK_STACK (td, 2);
2894 SIMPLE_OP (td, MINT_LDELEM_U1);
2896 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2899 CHECK_STACK (td, 2);
2901 SIMPLE_OP (td, MINT_LDELEM_I2);
2903 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2906 CHECK_STACK (td, 2);
2908 SIMPLE_OP (td, MINT_LDELEM_U2);
2910 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2913 CHECK_STACK (td, 2);
2915 SIMPLE_OP (td, MINT_LDELEM_I4);
2917 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2920 CHECK_STACK (td, 2);
2922 SIMPLE_OP (td, MINT_LDELEM_U4);
2924 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2927 CHECK_STACK (td, 2);
2929 SIMPLE_OP (td, MINT_LDELEM_I8);
2931 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2934 CHECK_STACK (td, 2);
2936 SIMPLE_OP (td, MINT_LDELEM_I);
2938 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2941 CHECK_STACK (td, 2);
2943 SIMPLE_OP (td, MINT_LDELEM_R4);
2945 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2948 CHECK_STACK (td, 2);
2950 SIMPLE_OP (td, MINT_LDELEM_R8);
2952 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2954 case CEE_LDELEM_REF:
2955 CHECK_STACK (td, 2);
2957 SIMPLE_OP (td, MINT_LDELEM_REF);
2959 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
2962 CHECK_STACK (td, 2);
2963 token = read32 (td->ip + 1);
2964 klass = mini_get_class (method, token, generic_context);
2965 switch (mint_type (&klass->byval_arg)) {
2968 SIMPLE_OP (td, MINT_LDELEM_I1);
2970 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2974 SIMPLE_OP (td, MINT_LDELEM_U1);
2976 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2980 SIMPLE_OP (td, MINT_LDELEM_U2);
2982 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2986 SIMPLE_OP (td, MINT_LDELEM_I2);
2988 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2992 SIMPLE_OP (td, MINT_LDELEM_I4);
2994 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2998 SIMPLE_OP (td, MINT_LDELEM_I8);
3000 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3004 SIMPLE_OP (td, MINT_LDELEM_R4);
3006 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3010 SIMPLE_OP (td, MINT_LDELEM_R8);
3012 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3016 SIMPLE_OP (td, MINT_LDELEM_REF);
3018 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3020 case MINT_TYPE_VT: {
3021 int size = mono_class_value_size (klass, NULL);
3023 SIMPLE_OP (td, MINT_LDELEM_VT);
3024 ADD_CODE (td, get_data_item_index (td, klass));
3025 WRITE32 (td, &size);
3027 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_VT);
3032 GString *res = g_string_new ("");
3033 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3034 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3035 g_string_free (res, TRUE);
3043 CHECK_STACK (td, 3);
3045 SIMPLE_OP (td, MINT_STELEM_I);
3049 CHECK_STACK (td, 3);
3051 SIMPLE_OP (td, MINT_STELEM_I1);
3055 CHECK_STACK (td, 3);
3057 SIMPLE_OP (td, MINT_STELEM_I2);
3061 CHECK_STACK (td, 3);
3063 SIMPLE_OP (td, MINT_STELEM_I4);
3067 CHECK_STACK (td, 3);
3069 SIMPLE_OP (td, MINT_STELEM_I8);
3073 CHECK_STACK (td, 3);
3075 SIMPLE_OP (td, MINT_STELEM_R4);
3079 CHECK_STACK (td, 3);
3081 SIMPLE_OP (td, MINT_STELEM_R8);
3084 case CEE_STELEM_REF:
3085 CHECK_STACK (td, 3);
3087 SIMPLE_OP (td, MINT_STELEM_REF);
3091 CHECK_STACK (td, 3);
3093 token = read32 (td->ip + 1);
3094 klass = mini_get_class (method, token, generic_context);
3095 switch (mint_type (&klass->byval_arg)) {
3097 SIMPLE_OP (td, MINT_STELEM_U1);
3100 SIMPLE_OP (td, MINT_STELEM_U2);
3103 SIMPLE_OP (td, MINT_STELEM_I4);
3106 SIMPLE_OP (td, MINT_STELEM_I8);
3109 SIMPLE_OP (td, MINT_STELEM_REF);
3111 case MINT_TYPE_VT: {
3112 int size = mono_class_value_size (klass, NULL);
3113 SIMPLE_OP (td, MINT_STELEM_VT);
3114 ADD_CODE (td, get_data_item_index (td, klass));
3115 WRITE32 (td, &size);
3120 GString *res = g_string_new ("");
3121 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3122 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3123 g_string_free (res, TRUE);
3132 case CEE_CONV_OVF_U1:
3134 case CEE_CONV_OVF_I8:
3136 #if SIZEOF_VOID_P == 8
3137 case CEE_CONV_OVF_U:
3141 CHECK_STACK (td, 1);
3142 SIMPLE_OP (td, MINT_CKFINITE);
3145 CHECK_STACK (td, 1);
3147 token = read32 (td->ip + 1);
3148 klass = mini_get_class (method, token, generic_context);
3150 ADD_CODE (td, MINT_MKREFANY);
3151 ADD_CODE (td, get_data_item_index (td, klass));
3154 PUSH_VT (td, sizeof (MonoTypedRef));
3155 SET_TYPE(td->sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
3157 case CEE_REFANYVAL: {
3158 CHECK_STACK (td, 1);
3160 token = read32 (td->ip + 1);
3162 ADD_CODE (td, MINT_REFANYVAL);
3164 POP_VT (td, sizeof (MonoTypedRef));
3165 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3170 case CEE_CONV_OVF_I1:
3171 case CEE_CONV_OVF_I1_UN:
3172 CHECK_STACK (td, 1);
3173 switch (td->sp [-1].type) {
3175 ADD_CODE(td, MINT_CONV_OVF_I1_R8);
3178 ADD_CODE(td, MINT_CONV_OVF_I1_I4);
3181 ADD_CODE(td, MINT_CONV_OVF_I1_I8);
3184 g_assert_not_reached ();
3187 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3189 case CEE_CONV_OVF_U1:
3190 case CEE_CONV_OVF_U1_UN:
3191 CHECK_STACK (td, 1);
3192 switch (td->sp [-1].type) {
3194 ADD_CODE(td, MINT_CONV_OVF_U1_R8);
3197 ADD_CODE(td, MINT_CONV_OVF_U1_I4);
3200 ADD_CODE(td, MINT_CONV_OVF_U1_I8);
3203 g_assert_not_reached ();
3206 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3208 case CEE_CONV_OVF_I2:
3209 case CEE_CONV_OVF_I2_UN:
3210 CHECK_STACK (td, 1);
3211 switch (td->sp [-1].type) {
3213 ADD_CODE(td, MINT_CONV_OVF_I2_R8);
3216 ADD_CODE(td, MINT_CONV_OVF_I2_I4);
3219 ADD_CODE(td, MINT_CONV_OVF_I2_I8);
3222 g_assert_not_reached ();
3225 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3227 case CEE_CONV_OVF_U2_UN:
3228 case CEE_CONV_OVF_U2:
3229 CHECK_STACK (td, 1);
3230 switch (td->sp [-1].type) {
3232 ADD_CODE(td, MINT_CONV_OVF_U2_R8);
3235 ADD_CODE(td, MINT_CONV_OVF_U2_I4);
3238 ADD_CODE(td, MINT_CONV_OVF_U2_I8);
3241 g_assert_not_reached ();
3244 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3246 #if SIZEOF_VOID_P == 4
3247 case CEE_CONV_OVF_I:
3249 case CEE_CONV_OVF_I4:
3250 case CEE_CONV_OVF_I4_UN:
3251 CHECK_STACK (td, 1);
3252 switch (td->sp [-1].type) {
3254 ADD_CODE(td, MINT_CONV_OVF_I4_R8);
3257 if (*td->ip == CEE_CONV_OVF_I4_UN)
3258 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3261 if (*td->ip == CEE_CONV_OVF_I4_UN)
3262 ADD_CODE (td, MINT_CONV_OVF_I4_U8);
3264 ADD_CODE (td, MINT_CONV_OVF_I4_I8);
3267 g_assert_not_reached ();
3270 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3272 #if SIZEOF_VOID_P == 4
3273 case CEE_CONV_OVF_U:
3275 case CEE_CONV_OVF_U4:
3276 case CEE_CONV_OVF_U4_UN:
3277 CHECK_STACK (td, 1);
3278 switch (td->sp [-1].type) {
3280 ADD_CODE(td, MINT_CONV_OVF_U4_R8);
3283 if (*td->ip != CEE_CONV_OVF_U4_UN)
3284 ADD_CODE(td, MINT_CONV_OVF_U4_I4);
3287 ADD_CODE(td, MINT_CONV_OVF_U4_I8);
3290 g_assert_not_reached ();
3293 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3295 #if SIZEOF_VOID_P == 8
3296 case CEE_CONV_OVF_I:
3298 case CEE_CONV_OVF_I8:
3299 CHECK_STACK (td, 1);
3300 switch (td->sp [-1].type) {
3302 ADD_CODE(td, MINT_CONV_OVF_I8_R8);
3305 ADD_CODE(td, MINT_CONV_I8_I4);
3310 g_assert_not_reached ();
3313 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3315 #if SIZEOF_VOID_P == 8
3316 case CEE_CONV_OVF_U:
3318 case CEE_CONV_OVF_U8:
3319 CHECK_STACK (td, 1);
3320 switch (td->sp [-1].type) {
3322 ADD_CODE(td, MINT_CONV_OVF_U8_R8);
3325 ADD_CODE(td, MINT_CONV_OVF_U8_I4);
3328 ADD_CODE (td, MINT_CONV_OVF_U8_I8);
3331 g_assert_not_reached ();
3334 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3339 token = read32 (td->ip + 1);
3340 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3341 handle = mono_method_get_wrapper_data (method, token);
3342 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
3343 if (klass == mono_defaults.typehandle_class)
3344 handle = &((MonoClass *) handle)->byval_arg;
3346 if (generic_context) {
3347 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
3348 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3351 handle = mono_ldtoken (image, token, &klass, generic_context);
3353 mono_class_init (klass);
3354 mt = mint_type (&klass->byval_arg);
3355 g_assert (mt == MINT_TYPE_VT);
3356 size = mono_class_value_size (klass, NULL);
3357 g_assert (size == sizeof(gpointer));
3358 PUSH_VT (td, sizeof(gpointer));
3359 ADD_CODE (td, MINT_LDTOKEN);
3360 ADD_CODE (td, get_data_item_index (td, handle));
3362 SET_TYPE (td->sp, stack_type [mt], klass);
3368 binary_arith_op(td, MINT_ADD_OVF_I4);
3371 case CEE_ADD_OVF_UN:
3372 binary_arith_op(td, MINT_ADD_OVF_UN_I4);
3376 binary_arith_op(td, MINT_MUL_OVF_I4);
3379 case CEE_MUL_OVF_UN:
3380 binary_arith_op(td, MINT_MUL_OVF_UN_I4);
3384 binary_arith_op(td, MINT_SUB_OVF_I4);
3387 case CEE_SUB_OVF_UN:
3388 binary_arith_op(td, MINT_SUB_OVF_UN_I4);
3391 case CEE_ENDFINALLY:
3392 g_assert (td->clause_indexes [in_offset] != -1);
3394 SIMPLE_OP (td, MINT_ENDFINALLY);
3395 ADD_CODE (td, td->clause_indexes [in_offset]);
3396 generating_code = 0;
3400 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td->ip + 1));
3402 generating_code = 0;
3406 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td->ip [1]);
3408 generating_code = 0;
3413 case CEE_MONO_CALLI_EXTRA_ARG:
3414 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
3415 ADD_CODE (td, MINT_POP);
3418 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3420 case CEE_MONO_JIT_ICALL_ADDR: {
3423 MonoJitICallInfo *info;
3425 token = read32 (td->ip + 1);
3427 func = mono_method_get_wrapper_data (method, token);
3428 info = mono_find_jit_icall_by_addr (func);
3430 ADD_CODE (td, MINT_LDFTN);
3431 ADD_CODE (td, get_data_item_index (td, func));
3432 PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
3435 case CEE_MONO_ICALL: {
3438 MonoJitICallInfo *info;
3440 token = read32 (td->ip + 1);
3442 func = mono_method_get_wrapper_data (method, token);
3443 info = mono_find_jit_icall_by_addr (func);
3446 CHECK_STACK (td, info->sig->param_count);
3447 switch (info->sig->param_count) {
3449 if (MONO_TYPE_IS_VOID (info->sig->ret))
3450 ADD_CODE (td,MINT_ICALL_V_V);
3452 ADD_CODE (td, MINT_ICALL_V_P);
3455 if (MONO_TYPE_IS_VOID (info->sig->ret))
3456 ADD_CODE (td,MINT_ICALL_P_V);
3458 ADD_CODE (td,MINT_ICALL_P_P);
3461 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
3462 if (info->sig->params [1]->type == MONO_TYPE_I4)
3463 ADD_CODE (td,MINT_ICALL_PI_V);
3465 ADD_CODE (td,MINT_ICALL_PP_V);
3467 if (info->sig->params [1]->type == MONO_TYPE_I4)
3468 ADD_CODE (td,MINT_ICALL_PI_P);
3470 ADD_CODE (td,MINT_ICALL_PP_P);
3474 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
3475 if (info->sig->params [2]->type == MONO_TYPE_I4)
3476 ADD_CODE (td,MINT_ICALL_PPI_V);
3478 ADD_CODE (td,MINT_ICALL_PPP_V);
3481 g_assert_not_reached ();
3484 if (func == mono_ftnptr_to_delegate) {
3485 g_error ("TODO: ?");
3487 ADD_CODE(td, get_data_item_index (td, func));
3488 td->sp -= info->sig->param_count;
3490 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3492 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3496 case CEE_MONO_VTADDR: {
3498 CHECK_STACK (td, 1);
3499 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
3500 size = mono_class_native_size(td->sp [-1].klass, NULL);
3502 size = mono_class_value_size(td->sp [-1].klass, NULL);
3503 size = (size + 7) & ~7;
3504 ADD_CODE(td, MINT_VTRESULT);
3509 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3512 case CEE_MONO_LDPTR:
3513 case CEE_MONO_CLASSCONST:
3514 token = read32 (td->ip + 1);
3516 ADD_CODE(td, MINT_MONO_LDPTR);
3517 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3518 td->sp [0].type = STACK_TYPE_I;
3521 case CEE_MONO_OBJADDR:
3522 CHECK_STACK (td, 1);
3524 td->sp[-1].type = STACK_TYPE_MP;
3527 case CEE_MONO_NEWOBJ:
3528 token = read32 (td->ip + 1);
3530 ADD_CODE(td, MINT_MONO_NEWOBJ);
3531 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3532 td->sp [0].type = STACK_TYPE_O;
3535 case CEE_MONO_RETOBJ:
3536 CHECK_STACK (td, 1);
3537 token = read32 (td->ip + 1);
3539 ADD_CODE(td, MINT_MONO_RETOBJ);
3542 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3544 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
3546 if (td->sp > td->stack)
3547 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td->sp-td->stack);
3549 case CEE_MONO_LDNATIVEOBJ:
3550 token = read32 (td->ip + 1);
3552 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3553 g_assert(klass->valuetype);
3554 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3556 case CEE_MONO_TLS: {
3557 gint32 key = read32 (td->ip + 1);
3559 g_assert (key < TLS_KEY_NUM);
3560 ADD_CODE (td, MINT_MONO_TLS);
3562 PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
3565 case CEE_MONO_ATOMIC_STORE_I4:
3566 CHECK_STACK (td, 2);
3567 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3571 case CEE_MONO_SAVE_LMF:
3572 case CEE_MONO_RESTORE_LMF:
3573 case CEE_MONO_NOT_TAKEN:
3576 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3577 ADD_CODE (td, MINT_MONO_LDPTR);
3578 ADD_CODE (td, get_data_item_index (td, mono_thread_interruption_request_flag ()));
3579 PUSH_TYPE (td, STACK_TYPE_MP, NULL);
3582 case CEE_MONO_MEMORY_BARRIER:
3583 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3586 case CEE_MONO_JIT_ATTACH:
3587 ADD_CODE (td, MINT_MONO_JIT_ATTACH);
3590 case CEE_MONO_JIT_DETACH:
3591 ADD_CODE (td, MINT_MONO_JIT_DETACH);
3594 case CEE_MONO_LDDOMAIN:
3595 ADD_CODE (td, MINT_MONO_LDDOMAIN);
3596 td->sp [0].type = STACK_TYPE_I;
3601 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td->ip, td->ip-header->code);
3611 case CEE_PREFIXREF: ves_abort(); break;
3614 * Note: Exceptions thrown when executing a prefixed opcode need
3615 * to take into account the number of prefix bytes (usually the
3616 * throw point is just (ip - n_prefix_bytes).
3622 case CEE_ARGLIST: ves_abort(); break;
3626 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3627 ADD_CODE(td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3629 ADD_CODE(td, MINT_CEQ_I4 + td->sp [-1].type - STACK_TYPE_I4);
3631 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3636 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3637 ADD_CODE(td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3639 ADD_CODE(td, MINT_CGT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3641 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3646 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3647 ADD_CODE(td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3649 ADD_CODE(td, MINT_CGT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3651 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3656 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3657 ADD_CODE(td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3659 ADD_CODE(td, MINT_CLT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3661 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3666 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3667 ADD_CODE(td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3669 ADD_CODE(td, MINT_CLT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3671 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3674 case CEE_LDVIRTFTN: /* fallthrough */
3677 if (*td->ip == CEE_LDVIRTFTN) {
3678 CHECK_STACK (td, 1);
3681 token = read32 (td->ip + 1);
3682 if (method->wrapper_type != MONO_WRAPPER_NONE)
3683 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3685 m = mono_get_method_full (image, token, NULL, generic_context);
3687 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3688 m = mono_marshal_get_synchronized_wrapper (m);
3690 ADD_CODE(td, *td->ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3691 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
3692 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3694 PUSH_SIMPLE_TYPE (td, STACK_TYPE_F);
3698 load_arg (td, read16 (td->ip + 1));
3702 int n = read16 (td->ip + 1);
3703 ADD_CODE (td, MINT_LDARGA);
3704 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offsets */
3705 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3710 store_arg (td, read16 (td->ip + 1));
3714 load_local (td, read16 (td->ip + 1));
3718 ADD_CODE(td, MINT_LDLOCA_S);
3719 ADD_CODE(td, td->rtm->local_offsets [read16 (td->ip + 1)]);
3720 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3724 store_local (td, read16 (td->ip + 1));
3728 CHECK_STACK (td, 1);
3729 #if SIZEOF_VOID_P == 8
3730 if (td->sp [-1].type == STACK_TYPE_I8)
3731 ADD_CODE(td, MINT_CONV_I4_I8);
3733 ADD_CODE(td, MINT_LOCALLOC);
3734 if (td->sp != td->stack + 1)
3735 g_warning("CEE_LOCALLOC: stack not empty");
3737 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3740 case CEE_UNUSED57: ves_abort(); break;
3743 ADD_CODE (td, MINT_ENDFILTER);
3746 case CEE_UNALIGNED_:
3755 /* FIX: should do something? */;
3756 // TODO: This should raise a method_tail_call profiler event.
3760 token = read32 (td->ip + 1);
3761 klass = mini_get_class (method, token, generic_context);
3762 if (klass->valuetype) {
3763 ADD_CODE (td, MINT_INITOBJ);
3764 i32 = mono_class_value_size (klass, NULL);
3767 ADD_CODE (td, MINT_LDNULL);
3768 ADD_CODE (td, MINT_STIND_REF);
3775 /* FIX? convert length to I8? */
3777 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3778 ADD_CODE(td, MINT_CPBLK);
3779 BARRIER_IF_VOLATILE (td);
3787 case CEE_CONSTRAINED_:
3788 token = read32 (td->ip + 1);
3789 constrained_class = mini_get_class (method, token, generic_context);
3790 mono_class_init (constrained_class);
3795 BARRIER_IF_VOLATILE (td);
3796 ADD_CODE(td, MINT_INITBLK);
3801 /* FIXME: implement */
3805 int clause_index = td->clause_indexes [in_offset];
3806 g_assert (clause_index != -1);
3807 SIMPLE_OP (td, MINT_RETHROW);
3808 ADD_CODE (td, rtm->exvar_offsets [clause_index]);
3809 generating_code = 0;
3814 token = read32 (td->ip + 1);
3816 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3818 MonoType *type = mono_type_create_from_typespec (image, token);
3819 size = mono_type_size (type, &align);
3822 MonoClass *szclass = mini_get_class (method, token, generic_context);
3823 mono_class_init (szclass);
3825 if (!szclass->valuetype)
3826 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3828 size = mono_type_size (&szclass->byval_arg, &align);
3830 ADD_CODE(td, MINT_LDC_I4);
3832 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
3835 case CEE_REFANYTYPE:
3836 ADD_CODE (td, MINT_REFANYTYPE);
3838 POP_VT (td, sizeof (MonoTypedRef));
3839 PUSH_VT (td, sizeof (gpointer));
3840 SET_TYPE(td->sp - 1, STACK_TYPE_VT, NULL);
3843 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);
3847 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td->ip, td->ip-header->code);
3850 if (td->new_ip - td->new_code != new_in_start_offset)
3851 td->last_new_ip = td->new_code + new_in_start_offset;
3852 else if (td->is_bb_start [td->in_start - td->il_code])
3853 td->is_bb_start [td->ip - td->il_code] = 1;
3855 td->last_ip = td->in_start;
3858 /* Handle relocations */
3859 for (int i = 0; i < td->relocs->len; ++i) {
3860 Reloc *reloc = g_ptr_array_index (td->relocs, i);
3862 int offset = td->in_offsets [reloc->target] - reloc->offset;
3864 switch (reloc->type) {
3865 case RELOC_SHORT_BRANCH:
3866 g_assert (td->new_code [reloc->offset + 1] == 0xffff);
3867 td->new_code [reloc->offset + 1] = offset;
3869 case RELOC_LONG_BRANCH: {
3870 guint16 *v = (guint16 *) &offset;
3871 g_assert (td->new_code [reloc->offset + 1] == 0xbeef);
3872 g_assert (td->new_code [reloc->offset + 2] == 0xdead);
3873 td->new_code [reloc->offset + 1] = *(guint16 *) v;
3874 td->new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
3877 case RELOC_SWITCH: {
3878 guint16 *v = (guint16*)&offset;
3879 td->new_code [reloc->offset] = *(guint16*)v;
3880 td->new_code [reloc->offset + 1] = *(guint16*)(v + 1);
3884 g_assert_not_reached ();
3889 if (td->verbose_level) {
3890 const guint16 *p = td->new_code;
3891 printf("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td->max_vt_sp);
3892 printf("Calculated stack size: %d, stated size: %d\n", td->max_stack_height, header->max_stack);
3893 while (p < td->new_ip) {
3894 p = mono_interp_dis_mintop(td->new_code, p);
3898 g_assert (td->max_stack_height <= (header->max_stack + 1));
3900 int code_len = td->new_ip - td->new_code;
3902 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3903 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3904 rtm->code = mono_domain_alloc0 (domain, (td->new_ip - td->new_code) * sizeof (gushort));
3905 memcpy (rtm->code, td->new_code, (td->new_ip - td->new_code) * sizeof(gushort));
3906 g_free (td->new_code);
3907 rtm->new_body_start = rtm->code + body_start_offset;
3908 rtm->num_clauses = header->num_clauses;
3909 for (i = 0; i < header->num_clauses; i++) {
3910 MonoExceptionClause *c = rtm->clauses + i;
3911 int end_off = c->try_offset + c->try_len;
3912 c->try_offset = td->in_offsets [c->try_offset];
3913 c->try_len = td->in_offsets [end_off] - c->try_offset;
3914 end_off = c->handler_offset + c->handler_len;
3915 c->handler_offset = td->in_offsets [c->handler_offset];
3916 c->handler_len = td->in_offsets [end_off] - c->handler_offset;
3917 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
3918 c->data.filter_offset = td->in_offsets [c->data.filter_offset];
3920 rtm->vt_stack_size = td->max_vt_sp;
3921 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3922 rtm->data_items = mono_domain_alloc0 (domain, td->n_data_items * sizeof (td->data_items [0]));
3923 memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
3925 /* Save debug info */
3926 interp_save_debug_info (rtm, header, td, line_numbers);
3928 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
3929 int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
3930 MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
3931 jinfo->is_interp = 1;
3933 mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
3934 for (i = 0; i < jinfo->num_clauses; ++i) {
3935 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3936 MonoExceptionClause *c = rtm->clauses + i;
3938 ei->flags = c->flags;
3939 ei->try_start = (guint8*)(rtm->code + c->try_offset);
3940 ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
3941 ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
3942 ei->exvar_offset = rtm->exvar_offsets [i];
3943 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3944 ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
3945 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3947 ei->data.catch_class = c->data.catch_class;
3951 save_seq_points (td);
3953 g_free (td->in_offsets);
3954 for (i = 0; i < header->code_size; ++i)
3955 g_free (td->stack_state [i]);
3956 g_free (td->stack_state);
3957 g_free (td->stack_height);
3958 g_free (td->vt_stack_size);
3959 g_free (td->data_items);
3961 g_hash_table_destroy (td->data_hash);
3962 g_free (td->clause_indexes);
3963 g_ptr_array_free (td->seq_points, TRUE);
3964 g_array_free (line_numbers, TRUE);
3965 g_ptr_array_free (td->relocs, TRUE);
3966 mono_mempool_destroy (td->mempool);
3969 static mono_mutex_t calc_section;
3972 mono_interp_transform_init (void)
3974 mono_os_mutex_init_recursive(&calc_section);
3978 mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context)
3980 int i, align, size, offset;
3981 MonoMethod *method = imethod->method;
3982 MonoImage *image = method->klass->image;
3983 MonoMethodHeader *header = mono_method_get_header (method);
3984 MonoMethodSignature *signature = mono_method_signature (method);
3985 register const unsigned char *ip, *end;
3986 const MonoOpcode *opcode;
3989 unsigned char *is_bb_start;
3991 MonoVTable *method_class_vt;
3993 MonoGenericContext *generic_context = NULL;
3994 MonoDomain *domain = imethod->domain;
3996 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3997 method_class_vt = mono_class_vtable (domain, imethod->method->klass);
3998 if (!method_class_vt->initialized) {
4001 InterpFrame *last_env_frame = context->env_frame;
4002 jmp_buf *old_env = context->current_env;
4003 error_init (&error);
4006 MonoException *failed = context->env_frame->ex;
4007 context->env_frame->ex = NULL;
4008 context->env_frame = last_env_frame;
4009 context->current_env = old_env;
4012 context->env_frame = context->current_frame;
4013 context->current_env = &env;
4014 mono_runtime_class_init_full (method_class_vt, &error);
4015 if (!mono_error_ok (&error)) {
4016 return mono_error_convert_to_exception (&error);
4018 context->env_frame = last_env_frame;
4019 context->current_env = old_env;
4022 MONO_PROFILER_RAISE (jit_begin, (method));
4024 if (mono_method_signature (method)->is_inflated)
4025 generic_context = mono_method_get_context (method);
4027 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
4028 if (generic_container)
4029 generic_context = &generic_container->context;
4032 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4033 MonoMethod *nm = NULL;
4034 mono_os_mutex_lock(&calc_section);
4035 if (imethod->transformed) {
4036 mono_os_mutex_unlock(&calc_section);
4037 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4041 /* assumes all internal calls with an array this are built in... */
4042 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
4043 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
4044 signature = mono_method_signature (nm);
4046 const char *name = method->name;
4047 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4048 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4049 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
4051 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
4052 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
4053 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4054 nm = mono_marshal_get_delegate_invoke (method, NULL);
4055 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4056 nm = mono_marshal_get_delegate_begin_invoke (method);
4057 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4058 nm = mono_marshal_get_delegate_end_invoke (method);
4062 imethod->code = g_malloc(sizeof(short));
4063 imethod->code[0] = MINT_CALLRUN;
4067 imethod->stack_size = sizeof (stackval); /* for tracing */
4068 imethod->alloca_size = imethod->stack_size;
4069 imethod->transformed = TRUE;
4070 mono_os_mutex_unlock(&calc_section);
4071 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4075 header = mono_method_get_header (nm);
4076 mono_os_mutex_unlock(&calc_section);
4077 } else if (method->klass == mono_defaults.array_class) {
4078 if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
4079 mono_os_mutex_lock (&calc_section);
4080 if (!imethod->transformed) {
4081 imethod->code = g_malloc (sizeof (short));
4082 imethod->code[0] = MINT_CALLRUN;
4083 imethod->stack_size = sizeof (stackval); /* for tracing */
4084 imethod->alloca_size = imethod->stack_size;
4085 imethod->transformed = TRUE;
4087 mono_os_mutex_unlock(&calc_section);
4088 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4090 } else if (!strcmp (method->name, "UnsafeStore")) {
4094 g_assert ((signature->param_count + signature->hasthis) < 1000);
4095 g_assert (header->max_stack < 10000);
4096 /* intern the strings in the method. */
4098 end = ip + header->code_size;
4100 is_bb_start = g_malloc0(header->code_size);
4101 is_bb_start [0] = 1;
4108 else if (in == 0xf0) {
4110 in = *ip + MONO_CEE_MONO_ICALL;
4112 opcode = &mono_opcodes [in];
4113 switch (opcode->argument) {
4114 case MonoInlineNone:
4117 case MonoInlineString:
4118 if (method->wrapper_type == MONO_WRAPPER_NONE)
4119 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
4122 case MonoInlineType:
4123 if (method->wrapper_type == MONO_WRAPPER_NONE) {
4124 class = mini_get_class (method, read32 (ip + 1), generic_context);
4125 mono_class_init (class);
4126 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
4128 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
4129 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
4130 mono_class_vtable (domain, class);
4135 case MonoInlineMethod:
4136 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
4137 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
4139 g_free (is_bb_start);
4140 g_error ("FIXME: where to get method and class string?");
4142 // return mono_get_exception_missing_method ();
4144 mono_class_init (m->klass);
4145 if (!mono_class_is_interface (m->klass))
4146 mono_class_vtable (domain, m->klass);
4150 case MonoInlineField:
4154 case MonoShortInlineR:
4157 case MonoInlineBrTarget:
4158 offset = read32 (ip + 1);
4160 backwards = offset < 0;
4161 offset += ip - header->code;
4162 g_assert (offset >= 0 && offset < header->code_size);
4163 is_bb_start [offset] |= backwards ? 2 : 1;
4165 case MonoShortInlineBrTarget:
4166 offset = ((gint8 *)ip) [1];
4168 backwards = offset < 0;
4169 offset += ip - header->code;
4170 g_assert (offset >= 0 && offset < header->code_size);
4171 is_bb_start [offset] |= backwards ? 2 : 1;
4176 case MonoShortInlineVar:
4177 case MonoShortInlineI:
4180 case MonoInlineSwitch: {
4182 const unsigned char *next_ip;
4186 next_ip = ip + 4 * n;
4187 for (i = 0; i < n; i++) {
4188 offset = read32 (ip);
4189 backwards = offset < 0;
4190 offset += next_ip - header->code;
4191 g_assert (offset >= 0 && offset < header->code_size);
4192 is_bb_start [offset] |= backwards ? 2 : 1;
4202 g_assert_not_reached ();
4205 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4207 /* the rest needs to be locked so it is only done once */
4208 mono_os_mutex_lock(&calc_section);
4209 if (imethod->transformed) {
4210 mono_os_mutex_unlock(&calc_section);
4211 g_free (is_bb_start);
4212 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4216 imethod->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
4217 imethod->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
4218 imethod->stack_size = (imethod->stack_size + 7) & ~7;
4220 for (i = 0; i < header->num_locals; ++i) {
4221 size = mono_type_size (header->locals [i], &align);
4222 offset += align - 1;
4223 offset &= ~(align - 1);
4224 imethod->local_offsets [i] = offset;
4227 offset = (offset + 7) & ~7;
4229 imethod->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
4230 for (i = 0; i < header->num_clauses; i++) {
4231 offset += sizeof (MonoObject*);
4232 imethod->exvar_offsets [i] = offset;
4234 offset = (offset + 7) & ~7;
4236 imethod->locals_size = offset;
4237 g_assert (imethod->locals_size < 65536);
4239 imethod->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
4241 if (signature->hasthis) {
4242 g_assert (!signature->pinvoke);
4243 size = mono_type_stack_size (&method->klass->byval_arg, &align);
4244 offset += align - 1;
4245 offset &= ~(align - 1);
4246 imethod->arg_offsets [0] = offset;
4250 for (i = 0; i < signature->param_count; ++i) {
4251 if (signature->pinvoke) {
4253 size = mono_type_native_stack_size (signature->params [i], &dummy);
4257 size = mono_type_stack_size (signature->params [i], &align);
4258 offset += align - 1;
4259 offset &= ~(align - 1);
4260 imethod->arg_offsets [i + !!signature->hasthis] = offset;
4263 offset = (offset + 7) & ~7;
4264 imethod->args_size = offset;
4265 g_assert (imethod->args_size < 10000);
4267 generate (method, imethod, is_bb_start, generic_context);
4269 g_free (is_bb_start);
4271 // FIXME: Add a different callback ?
4272 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4273 imethod->transformed = TRUE;
4274 mono_os_mutex_unlock(&calc_section);