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_runtime_method (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_runtime_method (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 (RuntimeMethod *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 RuntimeMethod *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, RuntimeMethod *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;
1360 int generating_code = 1;
1361 GArray *line_numbers;
1362 MonoDebugMethodInfo *minfo;
1363 MonoBitSet *seq_point_locs = NULL;
1364 MonoBitSet *seq_point_set_locs = NULL;
1365 gboolean sym_seq_points = FALSE;
1366 InterpBasicBlock *bb_exit = NULL;
1367 static gboolean verbose_method_inited;
1368 static char* verbose_method_name;
1370 if (!verbose_method_inited) {
1371 verbose_method_name = getenv ("MONO_VERBOSE_METHOD");
1372 verbose_method_inited = TRUE;
1375 memset (&td, 0, sizeof(td));
1378 td.is_bb_start = is_bb_start;
1379 td.il_code = header->code;
1380 td.code_size = header->code_size;
1382 td.max_code_size = td.code_size;
1383 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
1384 td.new_code_end = td.new_code + td.max_code_size;
1385 td.mempool = mono_mempool_new ();
1386 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
1387 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
1388 td.stack_height = g_malloc(header->code_size * sizeof(int));
1389 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
1390 td.n_data_items = 0;
1391 td.max_data_items = 0;
1392 td.data_items = NULL;
1393 td.data_hash = g_hash_table_new (NULL, NULL);
1394 td.clause_indexes = g_malloc (header->code_size * sizeof (int));
1395 td.gen_sdb_seq_points = debug_options.gen_sdb_seq_points;
1396 td.seq_points = g_ptr_array_new ();
1397 td.relocs = g_ptr_array_new ();
1398 td.verbose_level = mono_interp_traceopt;
1399 rtm->data_items = td.data_items;
1400 for (i = 0; i < header->code_size; i++) {
1401 td.stack_height [i] = -1;
1402 td.clause_indexes [i] = -1;
1405 if (verbose_method_name) {
1406 const char *name = verbose_method_name;
1408 if ((strchr (name, '.') > name) || strchr (name, ':')) {
1409 MonoMethodDesc *desc;
1411 desc = mono_method_desc_new (name, TRUE);
1412 if (mono_method_desc_full_match (desc, method)) {
1413 td.verbose_level = 4;
1415 mono_method_desc_free (desc);
1417 if (strcmp (method->name, name) == 0)
1418 td.verbose_level = 4;
1422 if (td.gen_sdb_seq_points) {
1423 get_basic_blocks (&td);
1425 minfo = mono_debug_lookup_method (method);
1428 MonoSymSeqPoint *sps;
1429 int i, n_il_offsets;
1431 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
1433 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td.mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1434 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);
1435 sym_seq_points = TRUE;
1437 for (i = 0; i < n_il_offsets; ++i) {
1438 if (sps [i].il_offset < header->code_size)
1439 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
1442 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
1443 /* Methods without line number info like auto-generated property accessors */
1444 seq_point_locs = mono_bitset_new (header->code_size, 0);
1445 seq_point_set_locs = mono_bitset_new (header->code_size, 0);
1446 sym_seq_points = TRUE;
1450 td.new_ip = td.new_code;
1451 td.last_new_ip = NULL;
1453 td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
1455 td.max_stack_height = 0;
1457 line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
1459 for (i = 0; i < header->num_clauses; i++) {
1460 MonoExceptionClause *c = header->clauses + i;
1461 td.stack_height [c->handler_offset] = 0;
1462 td.vt_stack_size [c->handler_offset] = 0;
1463 td.is_bb_start [c->handler_offset] = 1;
1465 td.stack_height [c->handler_offset] = 1;
1466 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
1467 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
1468 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
1470 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
1471 td.stack_height [c->data.filter_offset] = 0;
1472 td.vt_stack_size [c->data.filter_offset] = 0;
1473 td.is_bb_start [c->data.filter_offset] = 1;
1475 td.stack_height [c->data.filter_offset] = 1;
1476 td.stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
1477 td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
1478 td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
1481 for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
1482 if (td.clause_indexes [j] == -1)
1483 td.clause_indexes [j] = i;
1487 td.ip = header->code;
1488 end = td.ip + header->code_size;
1490 if (td.verbose_level) {
1491 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
1492 char *name = mono_method_full_name (method, TRUE);
1493 g_print ("Method %s, original code:\n", name);
1494 g_print ("%s\n", tmp);
1499 if (signature->hasthis)
1500 store_inarg (&td, 0);
1501 for (i = 0; i < signature->param_count; i++)
1502 store_inarg (&td, i + !!signature->hasthis);
1504 body_start_offset = td.new_ip - td.new_code;
1506 for (i = 0; i < header->num_locals; i++) {
1507 int mt = mint_type(header->locals [i]);
1508 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1509 ADD_CODE(&td, MINT_INITLOCALS);
1514 if (sym_seq_points) {
1515 InterpBasicBlock *cbb = td.offset_to_bb [0];
1517 emit_seq_point (&td, METHOD_ENTRY_IL_OFFSET, cbb, FALSE);
1520 while (td.ip < end) {
1523 g_assert (td.sp >= td.stack);
1524 g_assert (td.vt_sp < 0x10000000);
1525 in_offset = td.ip - header->code;
1526 td.in_offsets [in_offset] = td.new_ip - td.new_code;
1527 new_in_start_offset = td.new_ip - td.new_code;
1528 td.in_start = td.ip;
1530 MonoDebugLineNumberEntry lne;
1531 lne.native_offset = (guint8*)td.new_ip - (guint8*)td.new_code;
1532 lne.il_offset = in_offset;
1533 g_array_append_val (line_numbers, lne);
1535 if (td.stack_height [in_offset] >= 0) {
1536 g_assert (is_bb_start [in_offset]);
1537 if (td.stack_height [in_offset] > 0)
1538 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1539 td.sp = td.stack + td.stack_height [in_offset];
1540 td.vt_sp = td.vt_stack_size [in_offset];
1542 if (is_bb_start [in_offset]) {
1543 generating_code = 1;
1545 if (!generating_code) {
1546 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1550 if (td.verbose_level > 1) {
1551 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1553 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1554 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1555 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1556 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1557 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1558 (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) : "",
1559 td.vt_sp, td.max_vt_sp);
1562 if (sym_seq_points && mono_bitset_test_fast (seq_point_locs, td.ip - header->code)) {
1563 InterpBasicBlock *cbb = td.offset_to_bb [td.ip - header->code];
1567 * Make methods interruptable at the beginning, and at the targets of
1568 * backward branches.
1570 if (in_offset == 0 || g_slist_length (cbb->preds) > 1)
1571 ADD_CODE (&td, MINT_SDB_INTR_LOC);
1573 emit_seq_point (&td, in_offset, cbb, FALSE);
1575 mono_bitset_set_fast (seq_point_set_locs, td.ip - header->code);
1579 bb_exit = td.offset_to_bb [td.ip - header->code];
1587 SIMPLE_OP(td, MINT_BREAK);
1593 load_arg (&td, *td.ip - CEE_LDARG_0);
1600 load_local (&td, *td.ip - CEE_LDLOC_0);
1607 store_local (&td, *td.ip - CEE_STLOC_0);
1611 load_arg (&td, ((guint8 *)td.ip)[1]);
1614 case CEE_LDARGA_S: {
1615 /* NOTE: n includes this */
1616 int n = ((guint8 *) td.ip) [1];
1617 ADD_CODE (&td, MINT_LDARGA);
1618 ADD_CODE (&td, td.rtm->arg_offsets [n]);
1619 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1624 store_arg (&td, ((guint8 *)td.ip)[1]);
1628 load_local (&td, ((guint8 *)td.ip)[1]);
1632 ADD_CODE(&td, MINT_LDLOCA_S);
1633 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1634 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1638 store_local (&td, ((guint8 *)td.ip)[1]);
1642 SIMPLE_OP(td, MINT_LDNULL);
1643 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1646 SIMPLE_OP(td, MINT_LDC_I4_M1);
1647 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1650 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1651 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1652 SIMPLE_OP(td, MINT_CEQ0_I4);
1655 SIMPLE_OP(td, MINT_LDC_I4_0);
1656 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1660 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1661 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1662 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1665 SIMPLE_OP(td, MINT_LDC_I4_1);
1666 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1676 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1677 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1680 ADD_CODE(&td, MINT_LDC_I4_S);
1681 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1683 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1686 i32 = read32 (td.ip + 1);
1687 ADD_CODE(&td, MINT_LDC_I4);
1690 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1693 gint64 val = read64 (td.ip + 1);
1694 ADD_CODE(&td, MINT_LDC_I8);
1697 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1702 readr4 (td.ip + 1, &val);
1703 ADD_CODE(&td, MINT_LDC_R4);
1706 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1711 readr8 (td.ip + 1, &val);
1712 ADD_CODE(&td, MINT_LDC_R8);
1715 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1719 int type = td.sp [-1].type;
1720 MonoClass *klass = td.sp [-1].klass;
1721 if (td.sp [-1].type == STACK_TYPE_VT) {
1722 gint32 size = mono_class_value_size (klass, NULL);
1724 ADD_CODE(&td, MINT_DUP_VT);
1725 WRITE32(&td, &size);
1728 SIMPLE_OP(td, MINT_DUP);
1729 PUSH_TYPE(&td, type, klass);
1733 CHECK_STACK(&td, 1);
1734 SIMPLE_OP(td, MINT_POP);
1736 if (td.sp [-1].type == STACK_TYPE_VT) {
1737 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1738 size = (size + 7) & ~7;
1739 ADD_CODE(&td, MINT_VTRESULT);
1741 WRITE32(&td, &size);
1748 if (td.sp > td.stack)
1749 g_warning ("CEE_JMP: stack must be empty");
1750 token = read32 (td.ip + 1);
1751 m = mono_get_method_full (image, token, NULL, generic_context);
1752 ADD_CODE (&td, MINT_JMP);
1753 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1754 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1758 case CEE_CALLVIRT: /* Fall through */
1759 case CEE_CALLI: /* Fall through */
1761 gboolean need_seq_point = FALSE;
1763 if (sym_seq_points && !mono_bitset_test_fast (seq_point_locs, td.ip + 5 - header->code))
1764 need_seq_point = TRUE;
1766 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1768 if (need_seq_point) {
1769 InterpBasicBlock *cbb = td.offset_to_bb [td.ip - header->code];
1772 emit_seq_point (&td, td.ip - header->code, cbb, TRUE);
1775 constrained_class = NULL;
1781 if (signature->ret->type != MONO_TYPE_VOID) {
1783 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1784 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1785 vt_size = mono_class_value_size (klass, NULL);
1786 vt_size = (vt_size + 7) & ~7;
1789 if (td.sp > td.stack)
1790 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1791 if (td.vt_sp != vt_size)
1792 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1794 if (sym_seq_points) {
1795 InterpBasicBlock *cbb = td.offset_to_bb [td.ip - header->code];
1797 emit_seq_point (&td, METHOD_EXIT_IL_OFFSET, bb_exit, FALSE);
1801 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1803 ADD_CODE(&td, MINT_RET_VT);
1804 WRITE32(&td, &vt_size);
1807 generating_code = 0;
1811 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1813 generating_code = 0;
1816 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1818 generating_code = 0;
1821 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1825 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1829 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1833 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1837 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1841 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1845 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1849 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1853 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1857 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1861 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1865 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1869 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1873 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1877 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1881 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1885 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1889 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1893 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1897 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1901 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1905 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1909 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1913 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1918 const unsigned char *next_ip;
1921 ADD_CODE (&td, MINT_SWITCH);
1924 next_ip = td.ip + n * 4;
1926 int stack_height = td.sp - td.stack;
1927 for (i = 0; i < n; i++) {
1928 offset = read32 (td.ip);
1929 target = next_ip - td.il_code + offset;
1932 if (stack_height > 0 && stack_height != td.stack_height [target])
1933 g_warning ("SWITCH with back branch and non-empty stack");
1935 target = td.in_offsets [target] - (td.new_ip - td.new_code);
1937 td.stack_height [target] = stack_height;
1938 td.vt_stack_size [target] = td.vt_sp;
1939 if (stack_height > 0)
1940 td.stack_state [target] = g_memdup (td.stack, stack_height * sizeof (td.stack [0]));
1942 Reloc *reloc = mono_mempool_alloc0 (td.mempool, sizeof (Reloc));
1943 reloc->type = RELOC_SWITCH;
1944 reloc->offset = td.new_ip - td.new_code;
1945 reloc->target = target;
1946 g_ptr_array_add (td.relocs, reloc);
1949 WRITE32 (&td, &target);
1955 CHECK_STACK (&td, 1);
1956 SIMPLE_OP (td, MINT_LDIND_I1);
1957 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1958 BARRIER_IF_VOLATILE (td);
1961 CHECK_STACK (&td, 1);
1962 SIMPLE_OP (td, MINT_LDIND_U1);
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_I2);
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_U2);
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_I4);
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_U4);
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_I8);
1993 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1994 BARRIER_IF_VOLATILE (td);
1997 CHECK_STACK (&td, 1);
1998 SIMPLE_OP (td, MINT_LDIND_I);
2000 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2001 BARRIER_IF_VOLATILE (td);
2004 CHECK_STACK (&td, 1);
2005 SIMPLE_OP (td, MINT_LDIND_R4);
2006 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2007 BARRIER_IF_VOLATILE (td);
2010 CHECK_STACK (&td, 1);
2011 SIMPLE_OP (td, MINT_LDIND_R8);
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_REF);
2018 BARRIER_IF_VOLATILE (td);
2019 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2022 CHECK_STACK (&td, 2);
2023 BARRIER_IF_VOLATILE (td);
2024 SIMPLE_OP (td, MINT_STIND_REF);
2028 CHECK_STACK (&td, 2);
2029 BARRIER_IF_VOLATILE (td);
2030 SIMPLE_OP (td, MINT_STIND_I1);
2034 CHECK_STACK (&td, 2);
2035 BARRIER_IF_VOLATILE (td);
2036 SIMPLE_OP (td, MINT_STIND_I2);
2040 CHECK_STACK (&td, 2);
2041 BARRIER_IF_VOLATILE (td);
2042 SIMPLE_OP (td, MINT_STIND_I4);
2046 CHECK_STACK (&td, 2);
2047 BARRIER_IF_VOLATILE (td);
2048 SIMPLE_OP (td, MINT_STIND_I);
2052 CHECK_STACK (&td, 2);
2053 BARRIER_IF_VOLATILE (td);
2054 SIMPLE_OP (td, MINT_STIND_I8);
2058 CHECK_STACK (&td, 2);
2059 BARRIER_IF_VOLATILE (td);
2060 SIMPLE_OP (td, MINT_STIND_R4);
2064 CHECK_STACK (&td, 2);
2065 BARRIER_IF_VOLATILE (td);
2066 SIMPLE_OP (td, MINT_STIND_R8);
2070 binary_arith_op(&td, MINT_ADD_I4);
2074 binary_arith_op(&td, MINT_SUB_I4);
2078 binary_arith_op(&td, MINT_MUL_I4);
2082 binary_arith_op(&td, MINT_DIV_I4);
2086 binary_arith_op(&td, MINT_DIV_UN_I4);
2090 binary_arith_op (&td, MINT_REM_I4);
2094 binary_arith_op (&td, MINT_REM_UN_I4);
2098 binary_arith_op (&td, MINT_AND_I4);
2102 binary_arith_op (&td, MINT_OR_I4);
2106 binary_arith_op (&td, MINT_XOR_I4);
2110 shift_op (&td, MINT_SHL_I4);
2114 shift_op (&td, MINT_SHR_I4);
2118 shift_op (&td, MINT_SHR_UN_I4);
2122 unary_arith_op (&td, MINT_NEG_I4);
2126 unary_arith_op (&td, MINT_NOT_I4);
2130 CHECK_STACK (&td, 1);
2131 switch (td.sp [-1].type) {
2133 ADD_CODE(&td, MINT_CONV_U1_R8);
2136 ADD_CODE(&td, MINT_CONV_U1_I4);
2139 ADD_CODE(&td, MINT_CONV_U1_I8);
2142 g_assert_not_reached ();
2145 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2148 CHECK_STACK (&td, 1);
2149 switch (td.sp [-1].type) {
2151 ADD_CODE(&td, MINT_CONV_I1_R8);
2154 ADD_CODE(&td, MINT_CONV_I1_I4);
2157 ADD_CODE(&td, MINT_CONV_I1_I8);
2160 g_assert_not_reached ();
2163 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2166 CHECK_STACK (&td, 1);
2167 switch (td.sp [-1].type) {
2169 ADD_CODE(&td, MINT_CONV_U2_R8);
2172 ADD_CODE(&td, MINT_CONV_U2_I4);
2175 ADD_CODE(&td, MINT_CONV_U2_I8);
2178 g_assert_not_reached ();
2181 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2184 CHECK_STACK (&td, 1);
2185 switch (td.sp [-1].type) {
2187 ADD_CODE(&td, MINT_CONV_I2_R8);
2190 ADD_CODE(&td, MINT_CONV_I2_I4);
2193 ADD_CODE(&td, MINT_CONV_I2_I8);
2196 g_assert_not_reached ();
2199 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2202 CHECK_STACK (&td, 1);
2203 switch (td.sp [-1].type) {
2205 #if SIZEOF_VOID_P == 4
2206 ADD_CODE(&td, MINT_CONV_U4_R8);
2208 ADD_CODE(&td, MINT_CONV_U8_R8);
2212 #if SIZEOF_VOID_P == 8
2213 ADD_CODE(&td, MINT_CONV_U8_I4);
2217 #if SIZEOF_VOID_P == 4
2218 ADD_CODE(&td, MINT_CONV_U4_I8);
2224 g_assert_not_reached ();
2227 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2230 CHECK_STACK (&td, 1);
2231 switch (td.sp [-1].type) {
2233 #if SIZEOF_VOID_P == 8
2234 ADD_CODE(&td, MINT_CONV_I8_R8);
2236 ADD_CODE(&td, MINT_CONV_I4_R8);
2240 #if SIZEOF_VOID_P == 8
2241 ADD_CODE(&td, MINT_CONV_I8_I4);
2249 #if SIZEOF_VOID_P == 4
2250 ADD_CODE(&td, MINT_CONV_I4_I8);
2254 g_assert_not_reached ();
2257 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2260 CHECK_STACK (&td, 1);
2261 switch (td.sp [-1].type) {
2263 ADD_CODE(&td, MINT_CONV_U4_R8);
2268 ADD_CODE(&td, MINT_CONV_U4_I8);
2271 #if SIZEOF_VOID_P == 8
2272 ADD_CODE(&td, MINT_CONV_U4_I8);
2276 g_assert_not_reached ();
2279 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2282 CHECK_STACK (&td, 1);
2283 switch (td.sp [-1].type) {
2285 ADD_CODE(&td, MINT_CONV_I4_R8);
2290 ADD_CODE(&td, MINT_CONV_I4_I8);
2293 #if SIZEOF_VOID_P == 8
2294 ADD_CODE(&td, MINT_CONV_I4_I8);
2298 g_assert_not_reached ();
2301 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2304 CHECK_STACK (&td, 1);
2305 switch (td.sp [-1].type) {
2307 ADD_CODE(&td, MINT_CONV_I8_R8);
2310 ADD_CODE(&td, MINT_CONV_I8_I4);
2315 #if SIZEOF_VOID_P == 4
2316 ADD_CODE(&td, MINT_CONV_I8_I4);
2320 g_assert_not_reached ();
2323 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2326 CHECK_STACK (&td, 1);
2327 switch (td.sp [-1].type) {
2329 ADD_CODE(&td, MINT_CONV_R4_R8);
2332 ADD_CODE(&td, MINT_CONV_R4_I8);
2335 ADD_CODE(&td, MINT_CONV_R4_I4);
2338 g_assert_not_reached ();
2341 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2344 CHECK_STACK (&td, 1);
2345 switch (td.sp [-1].type) {
2347 ADD_CODE(&td, MINT_CONV_R8_I4);
2350 ADD_CODE(&td, MINT_CONV_R8_I8);
2355 g_assert_not_reached ();
2358 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2361 CHECK_STACK (&td, 1);
2362 switch (td.sp [-1].type) {
2364 ADD_CODE(&td, MINT_CONV_U8_I4);
2369 ADD_CODE(&td, MINT_CONV_U8_R8);
2372 #if SIZEOF_VOID_P == 4
2373 ADD_CODE(&td, MINT_CONV_U8_I4);
2377 g_assert_not_reached ();
2380 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2383 CHECK_STACK (&td, 2);
2385 token = read32 (td.ip + 1);
2386 klass = mono_class_get_full (image, token, generic_context);
2388 if (klass->valuetype) {
2389 ADD_CODE (&td, MINT_CPOBJ);
2390 ADD_CODE (&td, get_data_item_index(&td, klass));
2392 ADD_CODE (&td, MINT_LDIND_REF);
2393 ADD_CODE (&td, MINT_STIND_REF);
2401 CHECK_STACK (&td, 1);
2403 token = read32 (td.ip + 1);
2405 if (method->wrapper_type != MONO_WRAPPER_NONE)
2406 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2408 klass = mono_class_get_full (image, token, generic_context);
2410 ADD_CODE(&td, MINT_LDOBJ);
2411 ADD_CODE(&td, get_data_item_index(&td, klass));
2412 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2413 size = mono_class_value_size (klass, NULL);
2417 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
2418 BARRIER_IF_VOLATILE (td);
2423 token = mono_metadata_token_index (read32 (td.ip + 1));
2425 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
2426 s = mono_method_get_wrapper_data (method, token);
2427 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
2428 s = mono_string_new_wrapper (mono_method_get_wrapper_data (method, token));
2430 s = mono_ldstr (domain, image, token);
2432 ADD_CODE(&td, MINT_LDSTR);
2433 ADD_CODE(&td, get_data_item_index (&td, s));
2434 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
2439 MonoMethodSignature *csignature;
2440 guint32 vt_stack_used = 0;
2441 guint32 vt_res_size = 0;
2444 token = read32 (td.ip);
2447 if (method->wrapper_type != MONO_WRAPPER_NONE)
2448 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2450 m = mono_get_method_full (image, token, NULL, generic_context);
2452 csignature = mono_method_signature (m);
2454 td.sp -= csignature->param_count;
2455 ADD_CODE(&td, MINT_NEWOBJ);
2456 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2457 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2459 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2460 vt_res_size = mono_class_value_size (klass, NULL);
2461 PUSH_VT (&td, vt_res_size);
2463 for (i = 0; i < csignature->param_count; ++i) {
2464 int mt = mint_type(csignature->params [i]);
2465 if (mt == MINT_TYPE_VT) {
2466 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
2467 gint32 size = mono_class_value_size (k, NULL);
2468 size = (size + 7) & ~7;
2469 vt_stack_used += size;
2472 if (vt_stack_used != 0 || vt_res_size != 0) {
2473 ADD_CODE(&td, MINT_VTRESULT);
2474 ADD_CODE(&td, vt_res_size);
2475 WRITE32(&td, &vt_stack_used);
2476 td.vt_sp -= vt_stack_used;
2478 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
2482 CHECK_STACK (&td, 1);
2483 token = read32 (td.ip + 1);
2484 klass = mini_get_class (method, token, generic_context);
2485 ADD_CODE(&td, MINT_CASTCLASS);
2486 ADD_CODE(&td, get_data_item_index (&td, klass));
2487 td.sp [-1].klass = klass;
2491 CHECK_STACK (&td, 1);
2492 token = read32 (td.ip + 1);
2493 klass = mini_get_class (method, token, generic_context);
2494 ADD_CODE(&td, MINT_ISINST);
2495 ADD_CODE(&td, get_data_item_index (&td, klass));
2499 switch (td.sp [-1].type) {
2503 ADD_CODE(&td, MINT_CONV_R_UN_I8);
2506 ADD_CODE(&td, MINT_CONV_R_UN_I4);
2509 g_assert_not_reached ();
2511 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2515 CHECK_STACK (&td, 1);
2516 token = read32 (td.ip + 1);
2518 if (method->wrapper_type != MONO_WRAPPER_NONE)
2519 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2521 klass = mono_class_get_full (image, token, generic_context);
2523 if (mono_class_is_nullable (klass)) {
2524 g_error ("cee_unbox: implement Nullable");
2527 ADD_CODE(&td, MINT_UNBOX);
2528 ADD_CODE(&td, get_data_item_index (&td, klass));
2529 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2533 CHECK_STACK (&td, 1);
2534 token = read32 (td.ip + 1);
2536 klass = mini_get_class (method, token, generic_context);
2538 if (mini_type_is_reference (&klass->byval_arg)) {
2539 int mt = mint_type (&klass->byval_arg);
2540 ADD_CODE (&td, MINT_CASTCLASS);
2541 ADD_CODE (&td, get_data_item_index (&td, klass));
2542 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2544 } else if (mono_class_is_nullable (klass)) {
2545 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2546 /* td.ip is incremented by interp_transform_call */
2547 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2549 int mt = mint_type (&klass->byval_arg);
2550 ADD_CODE (&td, MINT_UNBOX);
2551 ADD_CODE (&td, get_data_item_index (&td, klass));
2553 ADD_CODE (&td, MINT_LDOBJ);
2554 ADD_CODE (&td, get_data_item_index(&td, klass));
2555 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2557 if (mt == MINT_TYPE_VT) {
2558 int size = mono_class_value_size (klass, NULL);
2559 PUSH_VT (&td, size);
2566 CHECK_STACK (&td, 1);
2567 SIMPLE_OP (td, MINT_THROW);
2569 generating_code = 0;
2572 CHECK_STACK (&td, 1);
2573 token = read32 (td.ip + 1);
2574 field = interp_field_from_token (method, token, &klass, generic_context);
2575 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2576 mono_class_init (klass);
2578 ADD_CODE (&td, MINT_POP);
2580 ADD_CODE (&td, MINT_LDSFLDA);
2581 ADD_CODE (&td, get_data_item_index (&td, field));
2583 if ((td.sp - 1)->type == STACK_TYPE_O) {
2584 ADD_CODE (&td, MINT_LDFLDA);
2586 g_assert ((td.sp -1)->type == STACK_TYPE_MP);
2587 ADD_CODE (&td, MINT_LDFLDA_UNSAFE);
2589 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2592 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2595 CHECK_STACK (&td, 1);
2596 token = read32 (td.ip + 1);
2597 field = interp_field_from_token (method, token, &klass, generic_context);
2598 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2599 mono_class_init (klass);
2601 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2602 mt = mint_type (&field_klass->byval_arg);
2603 #ifndef DISABLE_REMOTING
2604 if (klass->marshalbyref) {
2605 g_assert (!is_static);
2606 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2607 ADD_CODE(&td, get_data_item_index (&td, field));
2612 ADD_CODE (&td, MINT_POP);
2614 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2615 ADD_CODE (&td, get_data_item_index (&td, field));
2617 ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2618 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2621 if (mt == MINT_TYPE_VT) {
2622 int size = mono_class_value_size (field_klass, NULL);
2624 WRITE32(&td, &size);
2626 if (td.sp [-1].type == STACK_TYPE_VT) {
2627 int size = mono_class_value_size (klass, NULL);
2628 size = (size + 7) & ~7;
2630 ADD_CODE (&td, MINT_VTRESULT);
2632 WRITE32 (&td, &size);
2635 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2636 BARRIER_IF_VOLATILE (td);
2640 CHECK_STACK (&td, 2);
2641 token = read32 (td.ip + 1);
2642 field = interp_field_from_token (method, token, &klass, generic_context);
2643 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2644 mono_class_init (klass);
2645 mt = mint_type(field->type);
2647 BARRIER_IF_VOLATILE (td);
2649 #ifndef DISABLE_REMOTING
2650 if (klass->marshalbyref) {
2651 g_assert (!is_static);
2652 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2653 ADD_CODE(&td, get_data_item_index (&td, field));
2658 ADD_CODE (&td, MINT_POP);
2660 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2661 ADD_CODE (&td, get_data_item_index (&td, field));
2663 ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2664 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2667 if (mt == MINT_TYPE_VT) {
2668 MonoClass *klass = mono_class_from_mono_type (field->type);
2669 int size = mono_class_value_size (klass, NULL);
2671 WRITE32(&td, &size);
2678 token = read32 (td.ip + 1);
2679 field = interp_field_from_token (method, token, &klass, generic_context);
2680 ADD_CODE(&td, MINT_LDSFLDA);
2681 ADD_CODE(&td, get_data_item_index (&td, field));
2683 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2686 token = read32 (td.ip + 1);
2687 field = interp_field_from_token (method, token, &klass, generic_context);
2688 mt = mint_type(field->type);
2689 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2690 ADD_CODE(&td, get_data_item_index (&td, field));
2692 if (mt == MINT_TYPE_VT) {
2693 MonoClass *klass = mono_class_from_mono_type (field->type);
2694 int size = mono_class_value_size (klass, NULL);
2696 WRITE32(&td, &size);
2697 klass = field->type->data.klass;
2699 if (mt == MINT_TYPE_O)
2700 klass = mono_class_from_mono_type (field->type);
2703 PUSH_TYPE(&td, stack_type [mt], klass);
2706 CHECK_STACK (&td, 1);
2707 token = read32 (td.ip + 1);
2708 field = interp_field_from_token (method, token, &klass, generic_context);
2709 mt = mint_type(field->type);
2710 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2711 ADD_CODE(&td, get_data_item_index (&td, field));
2712 if (mt == MINT_TYPE_VT) {
2713 MonoClass *klass = mono_class_from_mono_type (field->type);
2714 int size = mono_class_value_size (klass, NULL);
2716 WRITE32 (&td, &size);
2723 token = read32 (td.ip + 1);
2725 if (method->wrapper_type != MONO_WRAPPER_NONE)
2726 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2728 klass = mini_get_class (method, token, generic_context);
2730 BARRIER_IF_VOLATILE (td);
2731 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2732 ADD_CODE(&td, get_data_item_index (&td, klass));
2733 if (td.sp [-1].type == STACK_TYPE_VT) {
2734 size = mono_class_value_size (klass, NULL);
2735 size = (size + 7) & ~7;
2742 case CEE_CONV_OVF_I_UN:
2743 case CEE_CONV_OVF_U_UN:
2744 CHECK_STACK (&td, 1);
2745 switch (td.sp [-1].type) {
2747 #if SIZEOF_VOID_P == 8
2748 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2750 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2754 #if SIZEOF_VOID_P == 4
2755 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2759 #if SIZEOF_VOID_P == 8
2760 ADD_CODE(&td, MINT_CONV_I8_U4);
2761 #elif SIZEOF_VOID_P == 4
2762 if (*td.ip == CEE_CONV_OVF_I_UN)
2763 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2767 g_assert_not_reached ();
2770 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2773 case CEE_CONV_OVF_I8_UN:
2774 case CEE_CONV_OVF_U8_UN:
2775 CHECK_STACK (&td, 1);
2776 switch (td.sp [-1].type) {
2778 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2781 if (*td.ip == CEE_CONV_OVF_I8_UN)
2782 ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
2785 ADD_CODE(&td, MINT_CONV_I8_U4);
2788 g_assert_not_reached ();
2791 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2796 CHECK_STACK (&td, 1);
2797 token = read32 (td.ip + 1);
2798 if (method->wrapper_type != MONO_WRAPPER_NONE)
2799 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2801 klass = mini_get_class (method, token, generic_context);
2803 if (mono_class_is_nullable (klass)) {
2804 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2805 /* td.ip is incremented by interp_transform_call */
2806 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2807 } else if (!klass->valuetype) {
2808 /* already boxed, do nothing. */
2811 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2812 size = mono_class_value_size (klass, NULL);
2813 size = (size + 7) & ~7;
2816 ADD_CODE(&td, MINT_BOX);
2817 ADD_CODE(&td, get_data_item_index (&td, klass));
2819 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2826 CHECK_STACK (&td, 1);
2827 token = read32 (td.ip + 1);
2829 if (method->wrapper_type != MONO_WRAPPER_NONE)
2830 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2832 klass = mini_get_class (method, token, generic_context);
2834 unsigned char lentype = (td.sp - 1)->type;
2835 if (lentype == STACK_TYPE_I8) {
2836 /* mimic mini behaviour */
2837 ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
2839 g_assert (lentype == STACK_TYPE_I4);
2840 ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
2842 SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
2843 ADD_CODE (&td, MINT_NEWARR);
2844 ADD_CODE (&td, get_data_item_index (&td, klass));
2845 SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
2850 CHECK_STACK (&td, 1);
2851 SIMPLE_OP (td, MINT_LDLEN);
2852 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2855 CHECK_STACK (&td, 2);
2857 token = read32 (td.ip + 1);
2859 if (method->wrapper_type != MONO_WRAPPER_NONE)
2860 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2862 klass = mini_get_class (method, token, generic_context);
2864 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2865 ADD_CODE (&td, MINT_LDELEMA_TC);
2867 ADD_CODE (&td, MINT_LDELEMA);
2869 ADD_CODE (&td, get_data_item_index (&td, klass));
2870 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2876 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2879 CHECK_STACK (&td, 2);
2881 SIMPLE_OP (td, MINT_LDELEM_I1);
2883 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2886 CHECK_STACK (&td, 2);
2888 SIMPLE_OP (td, MINT_LDELEM_U1);
2890 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2893 CHECK_STACK (&td, 2);
2895 SIMPLE_OP (td, MINT_LDELEM_I2);
2897 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2900 CHECK_STACK (&td, 2);
2902 SIMPLE_OP (td, MINT_LDELEM_U2);
2904 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2907 CHECK_STACK (&td, 2);
2909 SIMPLE_OP (td, MINT_LDELEM_I4);
2911 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2914 CHECK_STACK (&td, 2);
2916 SIMPLE_OP (td, MINT_LDELEM_U4);
2918 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2921 CHECK_STACK (&td, 2);
2923 SIMPLE_OP (td, MINT_LDELEM_I8);
2925 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2928 CHECK_STACK (&td, 2);
2930 SIMPLE_OP (td, MINT_LDELEM_I);
2932 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2935 CHECK_STACK (&td, 2);
2937 SIMPLE_OP (td, MINT_LDELEM_R4);
2939 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2942 CHECK_STACK (&td, 2);
2944 SIMPLE_OP (td, MINT_LDELEM_R8);
2946 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2948 case CEE_LDELEM_REF:
2949 CHECK_STACK (&td, 2);
2951 SIMPLE_OP (td, MINT_LDELEM_REF);
2953 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2956 CHECK_STACK (&td, 2);
2957 token = read32 (td.ip + 1);
2958 klass = mini_get_class (method, token, generic_context);
2959 switch (mint_type (&klass->byval_arg)) {
2962 SIMPLE_OP (td, MINT_LDELEM_I1);
2964 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2968 SIMPLE_OP (td, MINT_LDELEM_U1);
2970 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2974 SIMPLE_OP (td, MINT_LDELEM_U2);
2976 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2980 SIMPLE_OP (td, MINT_LDELEM_I2);
2982 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2986 SIMPLE_OP (td, MINT_LDELEM_I4);
2988 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2992 SIMPLE_OP (td, MINT_LDELEM_I8);
2994 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2998 SIMPLE_OP (td, MINT_LDELEM_R4);
3000 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
3004 SIMPLE_OP (td, MINT_LDELEM_R8);
3006 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
3010 SIMPLE_OP (td, MINT_LDELEM_REF);
3012 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
3014 case MINT_TYPE_VT: {
3015 int size = mono_class_value_size (klass, NULL);
3017 SIMPLE_OP (td, MINT_LDELEM_VT);
3018 ADD_CODE (&td, get_data_item_index (&td, klass));
3019 WRITE32 (&td, &size);
3021 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
3022 PUSH_VT (&td, size);
3026 GString *res = g_string_new ("");
3027 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3028 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3029 g_string_free (res, TRUE);
3037 CHECK_STACK (&td, 3);
3039 SIMPLE_OP (td, MINT_STELEM_I);
3043 CHECK_STACK (&td, 3);
3045 SIMPLE_OP (td, MINT_STELEM_I1);
3049 CHECK_STACK (&td, 3);
3051 SIMPLE_OP (td, MINT_STELEM_I2);
3055 CHECK_STACK (&td, 3);
3057 SIMPLE_OP (td, MINT_STELEM_I4);
3061 CHECK_STACK (&td, 3);
3063 SIMPLE_OP (td, MINT_STELEM_I8);
3067 CHECK_STACK (&td, 3);
3069 SIMPLE_OP (td, MINT_STELEM_R4);
3073 CHECK_STACK (&td, 3);
3075 SIMPLE_OP (td, MINT_STELEM_R8);
3078 case CEE_STELEM_REF:
3079 CHECK_STACK (&td, 3);
3081 SIMPLE_OP (td, MINT_STELEM_REF);
3085 CHECK_STACK (&td, 3);
3087 token = read32 (td.ip + 1);
3088 klass = mini_get_class (method, token, generic_context);
3089 switch (mint_type (&klass->byval_arg)) {
3091 SIMPLE_OP (td, MINT_STELEM_U1);
3094 SIMPLE_OP (td, MINT_STELEM_U2);
3097 SIMPLE_OP (td, MINT_STELEM_I4);
3100 SIMPLE_OP (td, MINT_STELEM_I8);
3103 SIMPLE_OP (td, MINT_STELEM_REF);
3105 case MINT_TYPE_VT: {
3106 int size = mono_class_value_size (klass, NULL);
3107 SIMPLE_OP (td, MINT_STELEM_VT);
3108 ADD_CODE (&td, get_data_item_index (&td, klass));
3109 WRITE32 (&td, &size);
3114 GString *res = g_string_new ("");
3115 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3116 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3117 g_string_free (res, TRUE);
3126 case CEE_CONV_OVF_U1:
3128 case CEE_CONV_OVF_I8:
3130 #if SIZEOF_VOID_P == 8
3131 case CEE_CONV_OVF_U:
3135 CHECK_STACK (&td, 1);
3136 SIMPLE_OP (td, MINT_CKFINITE);
3139 CHECK_STACK (&td, 1);
3141 token = read32 (td.ip + 1);
3142 klass = mini_get_class (method, token, generic_context);
3144 ADD_CODE (&td, MINT_MKREFANY);
3145 ADD_CODE (&td, get_data_item_index (&td, klass));
3148 PUSH_VT (&td, sizeof (MonoTypedRef));
3149 SET_TYPE(td.sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
3151 case CEE_REFANYVAL: {
3152 CHECK_STACK (&td, 1);
3154 token = read32 (td.ip + 1);
3156 ADD_CODE (&td, MINT_REFANYVAL);
3158 POP_VT (&td, sizeof (MonoTypedRef));
3159 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3164 case CEE_CONV_OVF_I1:
3165 case CEE_CONV_OVF_I1_UN:
3166 CHECK_STACK (&td, 1);
3167 switch (td.sp [-1].type) {
3169 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
3172 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
3175 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
3178 g_assert_not_reached ();
3181 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3183 case CEE_CONV_OVF_U1:
3184 case CEE_CONV_OVF_U1_UN:
3185 CHECK_STACK (&td, 1);
3186 switch (td.sp [-1].type) {
3188 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
3191 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
3194 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
3197 g_assert_not_reached ();
3200 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3202 case CEE_CONV_OVF_I2:
3203 case CEE_CONV_OVF_I2_UN:
3204 CHECK_STACK (&td, 1);
3205 switch (td.sp [-1].type) {
3207 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
3210 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
3213 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
3216 g_assert_not_reached ();
3219 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3221 case CEE_CONV_OVF_U2_UN:
3222 case CEE_CONV_OVF_U2:
3223 CHECK_STACK (&td, 1);
3224 switch (td.sp [-1].type) {
3226 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
3229 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
3232 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
3235 g_assert_not_reached ();
3238 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3240 #if SIZEOF_VOID_P == 4
3241 case CEE_CONV_OVF_I:
3243 case CEE_CONV_OVF_I4:
3244 case CEE_CONV_OVF_I4_UN:
3245 CHECK_STACK (&td, 1);
3246 switch (td.sp [-1].type) {
3248 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
3251 if (*td.ip == CEE_CONV_OVF_I4_UN)
3252 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
3255 if (*td.ip == CEE_CONV_OVF_I4_UN)
3256 ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
3258 ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
3261 g_assert_not_reached ();
3264 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3266 #if SIZEOF_VOID_P == 4
3267 case CEE_CONV_OVF_U:
3269 case CEE_CONV_OVF_U4:
3270 case CEE_CONV_OVF_U4_UN:
3271 CHECK_STACK (&td, 1);
3272 switch (td.sp [-1].type) {
3274 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
3277 if (*td.ip != CEE_CONV_OVF_U4_UN)
3278 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
3281 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
3284 g_assert_not_reached ();
3287 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3289 #if SIZEOF_VOID_P == 8
3290 case CEE_CONV_OVF_I:
3292 case CEE_CONV_OVF_I8:
3293 CHECK_STACK (&td, 1);
3294 switch (td.sp [-1].type) {
3296 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
3299 ADD_CODE(&td, MINT_CONV_I8_I4);
3304 g_assert_not_reached ();
3307 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
3309 #if SIZEOF_VOID_P == 8
3310 case CEE_CONV_OVF_U:
3312 case CEE_CONV_OVF_U8:
3313 CHECK_STACK (&td, 1);
3314 switch (td.sp [-1].type) {
3316 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
3319 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
3322 ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
3325 g_assert_not_reached ();
3328 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
3333 token = read32 (td.ip + 1);
3334 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3335 handle = mono_method_get_wrapper_data (method, token);
3336 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
3337 if (klass == mono_defaults.typehandle_class)
3338 handle = &((MonoClass *) handle)->byval_arg;
3340 if (generic_context) {
3341 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
3342 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3345 handle = mono_ldtoken (image, token, &klass, generic_context);
3347 mono_class_init (klass);
3348 mt = mint_type (&klass->byval_arg);
3349 g_assert (mt == MINT_TYPE_VT);
3350 size = mono_class_value_size (klass, NULL);
3351 g_assert (size == sizeof(gpointer));
3352 PUSH_VT (&td, sizeof(gpointer));
3353 ADD_CODE (&td, MINT_LDTOKEN);
3354 ADD_CODE (&td, get_data_item_index (&td, handle));
3356 SET_TYPE (td.sp, stack_type [mt], klass);
3362 binary_arith_op(&td, MINT_ADD_OVF_I4);
3365 case CEE_ADD_OVF_UN:
3366 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
3370 binary_arith_op(&td, MINT_MUL_OVF_I4);
3373 case CEE_MUL_OVF_UN:
3374 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
3378 binary_arith_op(&td, MINT_SUB_OVF_I4);
3381 case CEE_SUB_OVF_UN:
3382 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
3385 case CEE_ENDFINALLY:
3386 g_assert (td.clause_indexes [in_offset] != -1);
3388 SIMPLE_OP (td, MINT_ENDFINALLY);
3389 ADD_CODE (&td, td.clause_indexes [in_offset]);
3390 generating_code = 0;
3394 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
3396 generating_code = 0;
3400 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
3402 generating_code = 0;
3407 case CEE_MONO_CALLI_EXTRA_ARG:
3408 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
3409 ADD_CODE (&td, MINT_POP);
3412 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3414 case CEE_MONO_JIT_ICALL_ADDR: {
3417 MonoJitICallInfo *info;
3419 token = read32 (td.ip + 1);
3421 func = mono_method_get_wrapper_data (method, token);
3422 info = mono_find_jit_icall_by_addr (func);
3424 ADD_CODE (&td, MINT_LDFTN);
3425 ADD_CODE (&td, get_data_item_index (&td, func));
3426 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
3429 case CEE_MONO_ICALL: {
3432 MonoJitICallInfo *info;
3434 token = read32 (td.ip + 1);
3436 func = mono_method_get_wrapper_data (method, token);
3437 info = mono_find_jit_icall_by_addr (func);
3440 CHECK_STACK (&td, info->sig->param_count);
3441 switch (info->sig->param_count) {
3443 if (MONO_TYPE_IS_VOID (info->sig->ret))
3444 ADD_CODE (&td,MINT_ICALL_V_V);
3446 ADD_CODE (&td, MINT_ICALL_V_P);
3449 if (MONO_TYPE_IS_VOID (info->sig->ret))
3450 ADD_CODE (&td,MINT_ICALL_P_V);
3452 ADD_CODE (&td,MINT_ICALL_P_P);
3455 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
3456 if (info->sig->params [1]->type == MONO_TYPE_I4)
3457 ADD_CODE (&td,MINT_ICALL_PI_V);
3459 ADD_CODE (&td,MINT_ICALL_PP_V);
3461 if (info->sig->params [1]->type == MONO_TYPE_I4)
3462 ADD_CODE (&td,MINT_ICALL_PI_P);
3464 ADD_CODE (&td,MINT_ICALL_PP_P);
3468 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
3469 if (info->sig->params [2]->type == MONO_TYPE_I4)
3470 ADD_CODE (&td,MINT_ICALL_PPI_V);
3472 ADD_CODE (&td,MINT_ICALL_PPP_V);
3475 g_assert_not_reached ();
3478 if (func == mono_ftnptr_to_delegate) {
3479 g_error ("TODO: ?");
3481 ADD_CODE(&td, get_data_item_index (&td, func));
3482 td.sp -= info->sig->param_count;
3484 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3486 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
3490 case CEE_MONO_VTADDR: {
3492 CHECK_STACK (&td, 1);
3493 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
3494 size = mono_class_native_size(td.sp [-1].klass, NULL);
3496 size = mono_class_value_size(td.sp [-1].klass, NULL);
3497 size = (size + 7) & ~7;
3498 ADD_CODE(&td, MINT_VTRESULT);
3500 WRITE32(&td, &size);
3503 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3506 case CEE_MONO_LDPTR:
3507 case CEE_MONO_CLASSCONST:
3508 token = read32 (td.ip + 1);
3510 ADD_CODE(&td, MINT_MONO_LDPTR);
3511 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
3512 td.sp [0].type = STACK_TYPE_I;
3515 case CEE_MONO_OBJADDR:
3516 CHECK_STACK (&td, 1);
3518 td.sp[-1].type = STACK_TYPE_MP;
3521 case CEE_MONO_NEWOBJ:
3522 token = read32 (td.ip + 1);
3524 ADD_CODE(&td, MINT_MONO_NEWOBJ);
3525 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
3526 td.sp [0].type = STACK_TYPE_O;
3529 case CEE_MONO_RETOBJ:
3530 CHECK_STACK (&td, 1);
3531 token = read32 (td.ip + 1);
3533 ADD_CODE(&td, MINT_MONO_RETOBJ);
3536 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3538 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
3540 if (td.sp > td.stack)
3541 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
3543 case CEE_MONO_LDNATIVEOBJ:
3544 token = read32 (td.ip + 1);
3546 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3547 g_assert(klass->valuetype);
3548 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3550 case CEE_MONO_TLS: {
3551 gint32 key = read32 (td.ip + 1);
3553 g_assert (key < TLS_KEY_NUM);
3554 ADD_CODE (&td, MINT_MONO_TLS);
3555 WRITE32 (&td, &key);
3556 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_MP);
3559 case CEE_MONO_ATOMIC_STORE_I4:
3560 CHECK_STACK (&td, 2);
3561 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3565 case CEE_MONO_SAVE_LMF:
3566 case CEE_MONO_RESTORE_LMF:
3567 case CEE_MONO_NOT_TAKEN:
3570 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3571 ADD_CODE (&td, MINT_MONO_LDPTR);
3572 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
3573 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
3576 case CEE_MONO_MEMORY_BARRIER:
3577 ADD_CODE (&td, MINT_MONO_MEMORY_BARRIER);
3580 case CEE_MONO_JIT_ATTACH:
3581 ADD_CODE (&td, MINT_MONO_JIT_ATTACH);
3584 case CEE_MONO_JIT_DETACH:
3585 ADD_CODE (&td, MINT_MONO_JIT_DETACH);
3588 case CEE_MONO_LDDOMAIN:
3589 ADD_CODE (&td, MINT_MONO_LDDOMAIN);
3590 td.sp [0].type = STACK_TYPE_I;
3595 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
3605 case CEE_PREFIXREF: ves_abort(); break;
3608 * Note: Exceptions thrown when executing a prefixed opcode need
3609 * to take into account the number of prefix bytes (usually the
3610 * throw point is just (ip - n_prefix_bytes).
3616 case CEE_ARGLIST: ves_abort(); break;
3619 CHECK_STACK(&td, 2);
3620 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3621 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3623 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
3625 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3629 CHECK_STACK(&td, 2);
3630 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3631 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3633 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3635 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3639 CHECK_STACK(&td, 2);
3640 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3641 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3643 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3645 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3649 CHECK_STACK(&td, 2);
3650 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3651 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3653 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3655 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3659 CHECK_STACK(&td, 2);
3660 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3661 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3663 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3665 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3668 case CEE_LDVIRTFTN: /* fallthrough */
3671 if (*td.ip == CEE_LDVIRTFTN) {
3672 CHECK_STACK (&td, 1);
3675 token = read32 (td.ip + 1);
3676 if (method->wrapper_type != MONO_WRAPPER_NONE)
3677 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3679 m = mono_get_method_full (image, token, NULL, generic_context);
3681 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3682 m = mono_marshal_get_synchronized_wrapper (m);
3684 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3685 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
3686 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3688 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
3692 load_arg (&td, read16 (td.ip + 1));
3696 int n = read16 (td.ip + 1);
3697 ADD_CODE (&td, MINT_LDARGA);
3698 ADD_CODE (&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
3699 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3704 store_arg (&td, read16 (td.ip + 1));
3708 load_local (&td, read16 (td.ip + 1));
3712 ADD_CODE(&td, MINT_LDLOCA_S);
3713 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
3714 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3718 store_local (&td, read16 (td.ip + 1));
3722 CHECK_STACK (&td, 1);
3723 #if SIZEOF_VOID_P == 8
3724 if (td.sp [-1].type == STACK_TYPE_I8)
3725 ADD_CODE(&td, MINT_CONV_I4_I8);
3727 ADD_CODE(&td, MINT_LOCALLOC);
3728 if (td.sp != td.stack + 1)
3729 g_warning("CEE_LOCALLOC: stack not empty");
3731 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3734 case CEE_UNUSED57: ves_abort(); break;
3737 ADD_CODE (&td, MINT_ENDFILTER);
3740 case CEE_UNALIGNED_:
3749 /* FIX: should do something? */;
3752 CHECK_STACK(&td, 1);
3753 token = read32 (td.ip + 1);
3754 klass = mini_get_class (method, token, generic_context);
3755 if (klass->valuetype) {
3756 ADD_CODE (&td, MINT_INITOBJ);
3757 i32 = mono_class_value_size (klass, NULL);
3758 WRITE32 (&td, &i32);
3760 ADD_CODE (&td, MINT_LDNULL);
3761 ADD_CODE (&td, MINT_STIND_REF);
3767 CHECK_STACK(&td, 3);
3768 /* FIX? convert length to I8? */
3770 ADD_CODE (&td, MINT_MONO_MEMORY_BARRIER);
3771 ADD_CODE(&td, MINT_CPBLK);
3772 BARRIER_IF_VOLATILE (td);
3780 case CEE_CONSTRAINED_:
3781 token = read32 (td.ip + 1);
3782 constrained_class = mini_get_class (method, token, generic_context);
3783 mono_class_init (constrained_class);
3787 CHECK_STACK(&td, 3);
3788 BARRIER_IF_VOLATILE (td);
3789 ADD_CODE(&td, MINT_INITBLK);
3794 /* FIXME: implement */
3798 int clause_index = td.clause_indexes [in_offset];
3799 g_assert (clause_index != -1);
3800 SIMPLE_OP (td, MINT_RETHROW);
3801 ADD_CODE (&td, rtm->exvar_offsets [clause_index]);
3802 generating_code = 0;
3807 token = read32 (td.ip + 1);
3809 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3811 MonoType *type = mono_type_create_from_typespec (image, token);
3812 size = mono_type_size (type, &align);
3815 MonoClass *szclass = mini_get_class (method, token, generic_context);
3816 mono_class_init (szclass);
3818 if (!szclass->valuetype)
3819 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3821 size = mono_type_size (&szclass->byval_arg, &align);
3823 ADD_CODE(&td, MINT_LDC_I4);
3824 WRITE32(&td, &size);
3825 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3828 case CEE_REFANYTYPE:
3829 ADD_CODE (&td, MINT_REFANYTYPE);
3831 POP_VT (&td, sizeof (MonoTypedRef));
3832 PUSH_VT (&td, sizeof (gpointer));
3833 SET_TYPE(td.sp - 1, STACK_TYPE_VT, NULL);
3836 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);
3840 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3843 if (td.new_ip - td.new_code != new_in_start_offset)
3844 td.last_new_ip = td.new_code + new_in_start_offset;
3845 else if (td.is_bb_start [td.in_start - td.il_code])
3846 td.is_bb_start [td.ip - td.il_code] = 1;
3848 td.last_ip = td.in_start;
3851 /* Handle relocations */
3852 for (int i = 0; i < td.relocs->len; ++i) {
3853 Reloc *reloc = g_ptr_array_index (td.relocs, i);
3855 int offset = td.in_offsets [reloc->target] - reloc->offset;
3857 switch (reloc->type) {
3858 case RELOC_SHORT_BRANCH:
3859 g_assert (td.new_code [reloc->offset + 1] == 0xffff);
3860 td.new_code [reloc->offset + 1] = offset;
3862 case RELOC_LONG_BRANCH: {
3863 guint16 *v = (guint16 *) &offset;
3864 g_assert (td.new_code [reloc->offset + 1] == 0xbeef);
3865 g_assert (td.new_code [reloc->offset + 2] == 0xdead);
3866 td.new_code [reloc->offset + 1] = *(guint16 *) v;
3867 td.new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
3870 case RELOC_SWITCH: {
3871 guint16 *v = (guint16*)&offset;
3872 td.new_code [reloc->offset] = *(guint16*)v;
3873 td.new_code [reloc->offset + 1] = *(guint16*)(v + 1);
3877 g_assert_not_reached ();
3882 if (td.verbose_level) {
3883 const guint16 *p = td.new_code;
3884 printf("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td.max_vt_sp);
3885 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3886 while (p < td.new_ip) {
3887 p = mono_interp_dis_mintop(td.new_code, p);
3891 g_assert (td.max_stack_height <= (header->max_stack + 1));
3893 int code_len = td.new_ip - td.new_code;
3895 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3896 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3897 rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
3898 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3899 g_free (td.new_code);
3900 rtm->new_body_start = rtm->code + body_start_offset;
3901 rtm->num_clauses = header->num_clauses;
3902 for (i = 0; i < header->num_clauses; i++) {
3903 MonoExceptionClause *c = rtm->clauses + i;
3904 int end_off = c->try_offset + c->try_len;
3905 c->try_offset = td.in_offsets [c->try_offset];
3906 c->try_len = td.in_offsets [end_off] - c->try_offset;
3907 end_off = c->handler_offset + c->handler_len;
3908 c->handler_offset = td.in_offsets [c->handler_offset];
3909 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3910 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
3911 c->data.filter_offset = td.in_offsets [c->data.filter_offset];
3913 rtm->vt_stack_size = td.max_vt_sp;
3914 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3915 rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
3916 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3918 /* Save debug info */
3919 interp_save_debug_info (rtm, header, &td, line_numbers);
3921 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
3922 int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
3923 MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
3924 jinfo->is_interp = 1;
3926 mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
3927 for (i = 0; i < jinfo->num_clauses; ++i) {
3928 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3929 MonoExceptionClause *c = rtm->clauses + i;
3931 ei->flags = c->flags;
3932 ei->try_start = (guint8*)(rtm->code + c->try_offset);
3933 ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
3934 ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
3935 ei->exvar_offset = rtm->exvar_offsets [i];
3936 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3937 ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
3938 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3940 ei->data.catch_class = c->data.catch_class;
3944 save_seq_points (&td);
3946 g_free (td.in_offsets);
3947 for (i = 0; i < header->code_size; ++i)
3948 g_free (td.stack_state [i]);
3949 g_free (td.stack_state);
3950 g_free (td.stack_height);
3951 g_free (td.vt_stack_size);
3952 g_free (td.data_items);
3954 g_hash_table_destroy (td.data_hash);
3955 g_free (td.clause_indexes);
3956 g_ptr_array_free (td.seq_points, TRUE);
3957 g_array_free (line_numbers, TRUE);
3958 g_ptr_array_free (td.relocs, TRUE);
3959 mono_mempool_destroy (td.mempool);
3962 static mono_mutex_t calc_section;
3965 mono_interp_transform_init (void)
3967 mono_os_mutex_init_recursive(&calc_section);
3971 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3973 int i, align, size, offset;
3974 MonoMethod *method = runtime_method->method;
3975 MonoImage *image = method->klass->image;
3976 MonoMethodHeader *header = mono_method_get_header (method);
3977 MonoMethodSignature *signature = mono_method_signature (method);
3978 register const unsigned char *ip, *end;
3979 const MonoOpcode *opcode;
3982 unsigned char *is_bb_start;
3984 MonoVTable *method_class_vt;
3986 MonoGenericContext *generic_context = NULL;
3987 MonoDomain *domain = runtime_method->domain;
3989 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3990 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3991 if (!method_class_vt->initialized) {
3994 MonoInvocation *last_env_frame = context->env_frame;
3995 jmp_buf *old_env = context->current_env;
3996 error_init (&error);
3999 MonoException *failed = context->env_frame->ex;
4000 context->env_frame->ex = NULL;
4001 context->env_frame = last_env_frame;
4002 context->current_env = old_env;
4005 context->env_frame = context->current_frame;
4006 context->current_env = &env;
4007 mono_runtime_class_init_full (method_class_vt, &error);
4008 if (!mono_error_ok (&error)) {
4009 return mono_error_convert_to_exception (&error);
4011 context->env_frame = last_env_frame;
4012 context->current_env = old_env;
4015 mono_profiler_method_jit (method); /* sort of... */
4017 if (mono_method_signature (method)->is_inflated)
4018 generic_context = mono_method_get_context (method);
4020 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
4021 if (generic_container)
4022 generic_context = &generic_container->context;
4025 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4026 MonoMethod *nm = NULL;
4027 mono_os_mutex_lock(&calc_section);
4028 if (runtime_method->transformed) {
4029 mono_os_mutex_unlock(&calc_section);
4030 mono_profiler_method_end_jit (method, runtime_method->jinfo, MONO_PROFILE_OK);
4034 /* assumes all internal calls with an array this are built in... */
4035 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
4036 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
4037 signature = mono_method_signature (nm);
4039 const char *name = method->name;
4040 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4041 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4042 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
4044 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
4045 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
4046 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4047 nm = mono_marshal_get_delegate_invoke (method, NULL);
4048 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4049 nm = mono_marshal_get_delegate_begin_invoke (method);
4050 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4051 nm = mono_marshal_get_delegate_end_invoke (method);
4055 runtime_method->code = g_malloc(sizeof(short));
4056 runtime_method->code[0] = MINT_CALLRUN;
4060 runtime_method->stack_size = sizeof (stackval); /* for tracing */
4061 runtime_method->alloca_size = runtime_method->stack_size;
4062 runtime_method->transformed = TRUE;
4063 mono_os_mutex_unlock(&calc_section);
4064 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
4068 header = mono_method_get_header (nm);
4069 mono_os_mutex_unlock(&calc_section);
4070 } else if (method->klass == mono_defaults.array_class) {
4071 if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
4072 mono_os_mutex_lock (&calc_section);
4073 if (!runtime_method->transformed) {
4074 runtime_method->code = g_malloc (sizeof (short));
4075 runtime_method->code[0] = MINT_CALLRUN;
4076 runtime_method->stack_size = sizeof (stackval); /* for tracing */
4077 runtime_method->alloca_size = runtime_method->stack_size;
4078 runtime_method->transformed = TRUE;
4080 mono_os_mutex_unlock(&calc_section);
4081 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
4083 } else if (!strcmp (method->name, "UnsafeStore")) {
4087 g_assert ((signature->param_count + signature->hasthis) < 1000);
4088 g_assert (header->max_stack < 10000);
4089 /* intern the strings in the method. */
4091 end = ip + header->code_size;
4093 is_bb_start = g_malloc0(header->code_size);
4094 is_bb_start [0] = 1;
4101 else if (in == 0xf0) {
4103 in = *ip + MONO_CEE_MONO_ICALL;
4105 opcode = &mono_opcodes [in];
4106 switch (opcode->argument) {
4107 case MonoInlineNone:
4110 case MonoInlineString:
4111 if (method->wrapper_type == MONO_WRAPPER_NONE)
4112 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
4115 case MonoInlineType:
4116 if (method->wrapper_type == MONO_WRAPPER_NONE) {
4117 class = mini_get_class (method, read32 (ip + 1), generic_context);
4118 mono_class_init (class);
4119 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
4121 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
4122 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
4123 mono_class_vtable (domain, class);
4128 case MonoInlineMethod:
4129 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
4130 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
4132 g_free (is_bb_start);
4133 g_error ("FIXME: where to get method and class string?");
4135 // return mono_get_exception_missing_method ();
4137 mono_class_init (m->klass);
4138 if (!mono_class_is_interface (m->klass))
4139 mono_class_vtable (domain, m->klass);
4143 case MonoInlineField:
4147 case MonoShortInlineR:
4150 case MonoInlineBrTarget:
4151 offset = read32 (ip + 1);
4153 backwards = offset < 0;
4154 offset += ip - header->code;
4155 g_assert (offset >= 0 && offset < header->code_size);
4156 is_bb_start [offset] |= backwards ? 2 : 1;
4158 case MonoShortInlineBrTarget:
4159 offset = ((gint8 *)ip) [1];
4161 backwards = offset < 0;
4162 offset += ip - header->code;
4163 g_assert (offset >= 0 && offset < header->code_size);
4164 is_bb_start [offset] |= backwards ? 2 : 1;
4169 case MonoShortInlineVar:
4170 case MonoShortInlineI:
4173 case MonoInlineSwitch: {
4175 const unsigned char *next_ip;
4179 next_ip = ip + 4 * n;
4180 for (i = 0; i < n; i++) {
4181 offset = read32 (ip);
4182 backwards = offset < 0;
4183 offset += next_ip - header->code;
4184 g_assert (offset >= 0 && offset < header->code_size);
4185 is_bb_start [offset] |= backwards ? 2 : 1;
4195 g_assert_not_reached ();
4198 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4200 /* the rest needs to be locked so it is only done once */
4201 mono_os_mutex_lock(&calc_section);
4202 if (runtime_method->transformed) {
4203 mono_os_mutex_unlock(&calc_section);
4204 g_free (is_bb_start);
4205 mono_profiler_method_end_jit (method, runtime_method->jinfo, MONO_PROFILE_OK);
4209 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
4210 runtime_method->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
4211 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
4213 for (i = 0; i < header->num_locals; ++i) {
4214 size = mono_type_size (header->locals [i], &align);
4215 offset += align - 1;
4216 offset &= ~(align - 1);
4217 runtime_method->local_offsets [i] = offset;
4220 offset = (offset + 7) & ~7;
4222 runtime_method->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
4223 for (i = 0; i < header->num_clauses; i++) {
4224 offset += sizeof (MonoObject*);
4225 runtime_method->exvar_offsets [i] = offset;
4227 offset = (offset + 7) & ~7;
4229 runtime_method->locals_size = offset;
4230 g_assert (runtime_method->locals_size < 65536);
4232 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
4234 if (signature->hasthis) {
4235 g_assert (!signature->pinvoke);
4236 size = mono_type_stack_size (&method->klass->byval_arg, &align);
4237 offset += align - 1;
4238 offset &= ~(align - 1);
4239 runtime_method->arg_offsets [0] = offset;
4243 for (i = 0; i < signature->param_count; ++i) {
4244 if (signature->pinvoke) {
4246 size = mono_type_native_stack_size (signature->params [i], &dummy);
4250 size = mono_type_stack_size (signature->params [i], &align);
4251 offset += align - 1;
4252 offset &= ~(align - 1);
4253 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
4256 offset = (offset + 7) & ~7;
4257 runtime_method->args_size = offset;
4258 g_assert (runtime_method->args_size < 10000);
4260 generate (method, runtime_method, is_bb_start, generic_context);
4262 g_free (is_bb_start);
4264 // FIXME: Add a different callback ?
4265 mono_profiler_method_end_jit (method, runtime_method->jinfo, MONO_PROFILE_OK);
4266 runtime_method->transformed = TRUE;
4267 mono_os_mutex_unlock(&calc_section);