2 * transform CIL into different opcodes for more
3 * efficient interpretation
5 * Written by Bernie Solomon (bernard@ugsolutions.com)
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/exception.h>
13 #include <mono/metadata/mono-endian.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/profiler-private.h>
16 #include <mono/metadata/tabledefs.h>
18 #include <mono/mini/mini.h>
21 #include "interp-internals.h"
24 // TODO: export from marshal.c
25 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
39 MonoMethodHeader *header;
41 const unsigned char *il_code;
42 const unsigned char *ip;
43 const unsigned char *last_ip;
44 const unsigned char *in_start;
48 StackInfo **stack_state;
51 unsigned char *is_bb_start;
52 unsigned short *new_code;
53 unsigned short *new_code_end;
54 unsigned short *new_ip;
55 unsigned short *last_new_ip;
56 unsigned int max_code_size;
59 unsigned int max_stack_height;
61 unsigned int max_vt_sp;
65 GHashTable *data_hash;
68 #define MINT_TYPE_I1 0
69 #define MINT_TYPE_U1 1
70 #define MINT_TYPE_I2 2
71 #define MINT_TYPE_U2 3
72 #define MINT_TYPE_I4 4
73 #define MINT_TYPE_I8 5
74 #define MINT_TYPE_R4 6
75 #define MINT_TYPE_R8 7
78 #define MINT_TYPE_VT 10
80 #define STACK_TYPE_I4 0
81 #define STACK_TYPE_I8 1
82 #define STACK_TYPE_R8 2
83 #define STACK_TYPE_O 3
84 #define STACK_TYPE_VT 4
85 #define STACK_TYPE_MP 5
86 #define STACK_TYPE_F 6
88 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
90 #if SIZEOF_VOID_P == 8
91 #define STACK_TYPE_I STACK_TYPE_I8
93 #define STACK_TYPE_I STACK_TYPE_I4
96 static int stack_type [] = {
100 STACK_TYPE_I4, /*U2*/
101 STACK_TYPE_I4, /*I4*/
102 STACK_TYPE_I8, /*I8*/
103 STACK_TYPE_R8, /*R4*/
104 STACK_TYPE_R8, /*R8*/
111 grow_code (TransformData *td)
113 unsigned int old_ip_offset = td->new_ip - td->new_code;
114 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
115 g_assert (old_ip_offset <= td->max_code_size);
116 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
117 td->new_code_end = td->new_code + td->max_code_size;
118 td->new_ip = td->new_code + old_ip_offset;
119 td->last_new_ip = td->new_code + old_last_ip_offset;
122 #define ENSURE_CODE(td, n) \
124 if ((td)->new_ip + (n) > (td)->new_code_end) \
128 #define ADD_CODE(td, n) \
130 if ((td)->new_ip == (td)->new_code_end) \
132 *(td)->new_ip++ = (n); \
135 #define CHECK_STACK(td, n) \
137 int stack_size = (td)->sp - (td)->stack; \
138 if (stack_size < (n)) \
139 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
140 (td)->method->klass->name, (td)->method->name, \
141 stack_size, n, (td)->ip - (td)->il_code); \
144 #define ENSURE_I4(td, sp_off) \
146 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
147 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
151 handle_branch(TransformData *td, int short_op, int long_op, int offset)
153 int shorten_branch = 0;
154 int target = td->ip + offset - td->il_code;
155 if (target < 0 || target >= td->code_size)
156 g_assert_not_reached ();
157 if (offset > 0 && td->stack_height [target] < 0) {
158 td->stack_height [target] = td->sp - td->stack;
159 if (td->stack_height [target] > 0)
160 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
161 td->vt_stack_size [target] = td->vt_sp;
164 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
165 if (offset >= -32768) {
169 int prev = td->forward_refs [target];
170 td->forward_refs [td->ip - td->il_code] = prev;
171 td->forward_refs [target] = td->ip - td->il_code;
173 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
176 if (shorten_branch) {
177 ADD_CODE(td, short_op);
178 ADD_CODE(td, offset);
180 ADD_CODE(td, long_op);
181 ADD_CODE(td, * (unsigned short *)(&offset));
182 ADD_CODE(td, * ((unsigned short *)&offset + 1));
187 one_arg_branch(TransformData *td, int mint_op, int offset)
189 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
190 int long_op = mint_op + type - STACK_TYPE_I4;
191 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
194 handle_branch (td, short_op, long_op, offset);
198 two_arg_branch(TransformData *td, int mint_op, int offset)
200 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
201 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
202 int long_op = mint_op + type1 - STACK_TYPE_I4;
203 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
205 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
206 ADD_CODE(td, MINT_CONV_I8_I4);
207 td->in_offsets [td->ip - td->il_code]++;
208 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
209 ADD_CODE(td, MINT_CONV_I8_I4_SP);
210 td->in_offsets [td->ip - td->il_code]++;
211 } else if (type1 != type2) {
212 g_warning("%s.%s: branch type mismatch %d %d",
213 td->method->klass->name, td->method->name,
214 td->sp [-1].type, td->sp [-2].type);
217 handle_branch (td, short_op, long_op, offset);
221 unary_arith_op(TransformData *td, int mint_op)
223 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
229 binary_arith_op(TransformData *td, int mint_op)
231 int type1 = td->sp [-2].type;
232 int type2 = td->sp [-1].type;
234 #if SIZEOF_VOID_P == 8
235 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
236 ADD_CODE(td, MINT_CONV_I8_I4);
237 type2 = STACK_TYPE_I8;
239 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
240 ADD_CODE(td, MINT_CONV_I8_I4_SP);
241 type1 = STACK_TYPE_I8;
242 td->sp [-2].type = STACK_TYPE_I8;
245 if (type1 == STACK_TYPE_MP)
246 type1 = STACK_TYPE_I;
247 if (type2 == STACK_TYPE_MP)
248 type2 = STACK_TYPE_I;
249 if (type1 != type2) {
250 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
251 td->method->klass->name, td->method->name,
252 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
254 op = mint_op + type1 - STACK_TYPE_I4;
261 binary_int_op(TransformData *td, int mint_op)
263 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
265 if (td->sp [-1].type != td->sp [-2].type)
266 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
272 shift_op(TransformData *td, int mint_op)
274 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
276 if (td->sp [-1].type != STACK_TYPE_I4) {
277 g_warning("%s.%s: shift type mismatch %d",
278 td->method->klass->name, td->method->name,
286 mint_type(MonoType *type)
291 switch (type->type) {
295 case MONO_TYPE_BOOLEAN:
307 #if SIZEOF_VOID_P == 4
321 case MONO_TYPE_STRING:
322 case MONO_TYPE_SZARRAY:
323 case MONO_TYPE_CLASS:
324 case MONO_TYPE_OBJECT:
325 case MONO_TYPE_ARRAY:
327 case MONO_TYPE_VALUETYPE:
328 if (type->data.klass->enumtype) {
329 type = mono_class_enum_basetype (type->data.klass);
333 case MONO_TYPE_GENERICINST:
334 type = &type->data.generic_class->container_class->byval_arg;
337 g_warning ("got type 0x%02x", type->type);
338 g_assert_not_reached ();
344 can_store (int stack_type, int var_type)
346 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
347 stack_type = STACK_TYPE_I;
348 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
349 var_type = STACK_TYPE_I;
350 return stack_type == var_type;
353 #define SET_SIMPLE_TYPE(s, ty) \
360 #define SET_TYPE(s, ty, k) \
367 #define PUSH_SIMPLE_TYPE(td, ty) \
371 sp_height = (td)->sp - (td)->stack; \
372 if (sp_height > (td)->max_stack_height) \
373 (td)->max_stack_height = sp_height; \
374 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
377 #define PUSH_TYPE(td, ty, k) \
381 sp_height = (td)->sp - (td)->stack; \
382 if (sp_height > (td)->max_stack_height) \
383 (td)->max_stack_height = sp_height; \
384 SET_TYPE((td)->sp - 1, ty, k); \
387 #define PUSH_VT(td, size) \
389 (td)->vt_sp += ((size) + 7) & ~7; \
390 if ((td)->vt_sp > (td)->max_vt_sp) \
391 (td)->max_vt_sp = (td)->vt_sp; \
394 #define POP_VT(td, size) \
396 (td)->vt_sp -= ((size) + 7) & ~7; \
399 #if NO_UNALIGNED_ACCESS
400 #define WRITE32(td, v) \
402 ENSURE_CODE(td, 2); \
403 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
404 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
408 #define WRITE64(td, v) \
410 ENSURE_CODE(td, 4); \
411 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
412 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
413 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
414 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
418 #define WRITE32(td, v) \
420 ENSURE_CODE(td, 2); \
421 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
425 #define WRITE64(td, v) \
427 ENSURE_CODE(td, 4); \
428 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
435 load_arg(TransformData *td, int n)
438 MonoClass *klass = NULL;
441 gboolean hasthis = mono_method_signature (td->method)->hasthis;
442 if (hasthis && n == 0)
443 type = &td->method->klass->byval_arg;
445 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
447 mt = mint_type (type);
448 if (mt == MINT_TYPE_VT) {
450 klass = mono_class_from_mono_type (type);
451 if (mono_method_signature (td->method)->pinvoke)
452 size = mono_class_native_size (klass, NULL);
454 size = mono_class_value_size (klass, NULL);
456 if (hasthis && n == 0) {
458 ADD_CODE (td, MINT_LDARG_P);
459 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
463 ADD_CODE (td, MINT_LDARG_VT);
464 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
468 if (hasthis && n == 0) {
470 ADD_CODE (td, MINT_LDARG_P);
471 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
474 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
475 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
476 if (mt == MINT_TYPE_O)
477 klass = mono_class_from_mono_type (type);
480 PUSH_TYPE(td, stack_type[mt], klass);
484 store_arg(TransformData *td, int n)
490 gboolean hasthis = mono_method_signature (td->method)->hasthis;
491 if (hasthis && n == 0)
492 type = &td->method->klass->byval_arg;
494 type = mono_method_signature (td->method)->params [n - !!hasthis];
496 mt = mint_type (type);
497 if (mt == MINT_TYPE_VT) {
499 g_error ("data.klass");
500 if (mono_method_signature (td->method)->pinvoke)
501 size = mono_class_native_size (type->data.klass, NULL);
503 size = mono_class_value_size (type->data.klass, NULL);
504 ADD_CODE(td, MINT_STARG_VT);
507 if (td->sp [-1].type == STACK_TYPE_VT)
510 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
511 ADD_CODE(td, td->rtm->arg_offsets [n]);
517 store_inarg(TransformData *td, int n)
520 gboolean hasthis = mono_method_signature (td->method)->hasthis;
521 if (hasthis && n == 0)
522 type = &td->method->klass->byval_arg;
524 type = mono_method_signature (td->method)->params [n - !!hasthis];
526 int mt = mint_type (type);
527 if (hasthis && n == 0) {
528 ADD_CODE (td, MINT_STINARG_P);
532 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
535 if (mono_method_signature (td->method)->pinvoke)
536 size = mono_class_native_size (klass, NULL);
538 size = mono_class_value_size (klass, NULL);
539 ADD_CODE(td, MINT_STINARG_VT);
543 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
549 load_local(TransformData *td, int n)
551 MonoType *type = td->header->locals [n];
552 int mt = mint_type (type);
553 int offset = td->rtm->local_offsets [n];
554 MonoClass *klass = NULL;
555 if (mt == MINT_TYPE_VT) {
556 klass = mono_class_from_mono_type (type);
557 gint32 size = mono_class_value_size (klass, NULL);
559 ADD_CODE(td, MINT_LDLOC_VT);
560 ADD_CODE(td, offset); /*FIX for large offset */
563 g_assert (mt < MINT_TYPE_VT);
564 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
565 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
566 td->last_new_ip [0] = MINT_STLOC_NP_I4;
567 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
568 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
569 td->last_new_ip [0] = MINT_STLOC_NP_O;
571 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
572 ADD_CODE(td, offset); /*FIX for large offset */
574 if (mt == MINT_TYPE_O)
575 klass = mono_class_from_mono_type (type);
577 PUSH_TYPE(td, stack_type[mt], klass);
581 store_local(TransformData *td, int n)
583 MonoType *type = td->header->locals [n];
584 int mt = mint_type (type);
585 int offset = td->rtm->local_offsets [n];
587 #if SIZEOF_VOID_P == 8
588 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
589 ADD_CODE(td, MINT_CONV_I8_I4);
590 td->sp [-1].type = STACK_TYPE_I8;
593 if (!can_store(td->sp [-1].type, stack_type [mt])) {
594 g_warning("%s.%s: Store local stack type mismatch %d %d",
595 td->method->klass->name, td->method->name,
596 stack_type [mt], td->sp [-1].type);
598 if (mt == MINT_TYPE_VT) {
599 MonoClass *klass = mono_class_from_mono_type (type);
600 gint32 size = mono_class_value_size (klass, NULL);
601 ADD_CODE(td, MINT_STLOC_VT);
602 ADD_CODE(td, offset); /*FIX for large offset */
604 if (td->sp [-1].type == STACK_TYPE_VT)
607 g_assert (mt < MINT_TYPE_VT);
608 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
609 ADD_CODE(td, offset); /*FIX for large offset */
614 #define SIMPLE_OP(td, op) \
621 get_data_item_index (TransformData *td, void *ptr)
623 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
626 return GPOINTER_TO_UINT (p) - 1;
627 if (td->max_data_items == td->n_data_items) {
628 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
629 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
631 index = td->n_data_items;
632 td->data_items [index] = ptr;
634 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
639 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)
641 MonoImage *image = method->klass->image;
642 MonoMethodSignature *csignature;
644 int virtual = *td->ip == CEE_CALLVIRT;
645 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
647 guint32 vt_stack_used = 0;
648 guint32 vt_res_size = 0;
653 guint32 token = read32 (td->ip + 1);
655 if (target_method == NULL) {
658 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
660 if (method->wrapper_type != MONO_WRAPPER_NONE)
661 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
663 csignature = mono_metadata_parse_signature (image, token);
665 if (generic_context) {
666 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
667 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
670 target_method = NULL;
672 if (method->wrapper_type == MONO_WRAPPER_NONE)
673 target_method = mono_get_method_full (image, token, NULL, generic_context);
675 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
676 csignature = mono_method_signature (target_method);
677 if (target_method->klass == mono_defaults.string_class) {
678 if (target_method->name [0] == 'g') {
679 if (strcmp (target_method->name, "get_Chars") == 0)
681 else if (strcmp (target_method->name, "get_Length") == 0)
684 } else if (target_method->klass == mono_defaults.array_class) {
685 if (strcmp (target_method->name, "get_Rank") == 0)
686 op = MINT_ARRAY_RANK;
687 else if (strcmp (target_method->name, "get_Length") == 0)
689 } else if (target_method && generic_context) {
690 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
691 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
692 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
693 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
697 csignature = mono_method_signature (target_method);
700 if (constrained_class) {
701 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
702 /* Use the corresponding method from the base type to avoid boxing */
703 MonoType *base_type = mono_class_enum_basetype (constrained_class);
704 g_assert (base_type);
705 constrained_class = mono_class_from_mono_type (base_type);
706 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
707 g_assert (target_method);
711 if (constrained_class) {
712 mono_class_setup_vtable (constrained_class);
714 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);
716 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
718 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
720 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
721 mono_class_setup_vtable (target_method->klass);
723 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)) {
724 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
725 /* managed pointer on the stack, we need to deref that puppy */
726 ADD_CODE (td, MINT_LDIND_I);
727 ADD_CODE (td, csignature->param_count);
729 ADD_CODE (td, MINT_BOX);
730 ADD_CODE (td, get_data_item_index (td, constrained_class));
731 ADD_CODE (td, csignature->param_count);
732 } else if (!constrained_class->valuetype) {
733 /* managed pointer on the stack, we need to deref that puppy */
734 ADD_CODE (td, MINT_LDIND_I);
735 ADD_CODE (td, csignature->param_count);
737 g_assert (target_method->klass->valuetype);
743 mono_class_init (target_method->klass);
745 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
746 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
747 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
748 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
749 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
750 MonoMethodHeader *mheader = mono_method_get_header (target_method);
752 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
754 if (mono_interp_traceopt)
755 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
756 for (i = csignature->param_count - 1; i >= 0; --i)
757 store_arg (td, i + csignature->hasthis);
759 if (csignature->hasthis) {
760 g_error ("STTHIS removal");
761 // ADD_CODE(td, MINT_STTHIS);
764 ADD_CODE(td, MINT_BR_S);
765 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
766 ADD_CODE(td, offset);
767 if (!is_bb_start [td->ip + 5 - td->il_code])
768 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
772 /* mheader might not exist if this is a delegate invoc, etc */
773 if (mheader && *mheader->code == CEE_RET && called_inited) {
774 if (mono_interp_traceopt)
775 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
776 for (i = 0; i < csignature->param_count; i++)
777 ADD_CODE (td, MINT_POP); /*FIX: vt */
779 if (csignature->hasthis) {
781 ADD_CODE(td, MINT_CKNULL);
782 ADD_CODE (td, MINT_POP);
785 td->sp -= csignature->param_count + csignature->hasthis;
791 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
792 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
793 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
794 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
795 target_method = mono_marshal_get_synchronized_wrapper (target_method);
797 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
798 td->sp -= csignature->param_count + !!csignature->hasthis;
799 for (i = 0; i < csignature->param_count; ++i) {
800 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
802 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
803 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
804 size = mono_class_native_size (klass, NULL);
806 size = mono_class_value_size (klass, NULL);
807 size = (size + 7) & ~7;
808 vt_stack_used += size;
812 /* need to handle typedbyref ... */
813 if (csignature->ret->type != MONO_TYPE_VOID) {
814 int mt = mint_type(csignature->ret);
815 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
816 if (mt == MINT_TYPE_VT) {
817 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
818 vt_res_size = mono_class_native_size (klass, NULL);
820 vt_res_size = mono_class_value_size (klass, NULL);
821 PUSH_VT(td, vt_res_size);
823 PUSH_TYPE(td, stack_type[mt], klass);
829 #if SIZEOF_VOID_P == 8
830 if (op == MINT_LDLEN)
831 ADD_CODE(td, MINT_CONV_I4_I8);
835 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
837 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
839 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
842 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
844 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
845 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
849 if (vt_stack_used != 0 || vt_res_size != 0) {
850 ADD_CODE(td, MINT_VTRESULT);
851 ADD_CODE(td, vt_res_size);
852 WRITE32(td, &vt_stack_used);
853 td->vt_sp -= vt_stack_used;
858 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
860 MonoMethodHeader *header = mono_method_get_header (method);
861 MonoMethodSignature *signature = mono_method_signature (method);
862 MonoImage *image = method->klass->image;
863 MonoDomain *domain = mono_domain_get ();
864 MonoClass *constrained_class = NULL;
870 MonoClassField *field;
871 const unsigned char *end;
872 int new_in_start_offset;
873 int body_start_offset;
877 int generating_code = 1;
879 memset(&td, 0, sizeof(td));
882 td.is_bb_start = is_bb_start;
883 td.il_code = header->code;
884 td.code_size = header->code_size;
886 td.max_code_size = td.code_size;
887 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
888 td.new_code_end = td.new_code + td.max_code_size;
889 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
890 td.forward_refs = g_malloc(header->code_size * sizeof(int));
891 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
892 td.stack_height = g_malloc(header->code_size * sizeof(int));
893 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
895 td.max_data_items = 0;
896 td.data_items = NULL;
897 td.data_hash = g_hash_table_new (NULL, NULL);
898 rtm->data_items = td.data_items;
899 for (i = 0; i < header->code_size; i++) {
900 td.forward_refs [i] = -1;
901 td.stack_height [i] = -1;
903 td.new_ip = td.new_code;
904 td.last_new_ip = NULL;
906 td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
908 td.max_stack_height = 0;
910 for (i = 0; i < header->num_clauses; i++) {
911 MonoExceptionClause *c = header->clauses + i;
912 td.stack_height [c->handler_offset] = 0;
913 td.vt_stack_size [c->handler_offset] = 0;
914 td.is_bb_start [c->handler_offset] = 1;
915 if (c->flags == MONO_EXCEPTION_CLAUSE_NONE) {
916 td.stack_height [c->handler_offset] = 1;
917 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
918 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
919 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
923 td.ip = header->code;
924 end = td.ip + header->code_size;
926 if (mono_interp_traceopt) {
927 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
928 char *name = mono_method_full_name (method, TRUE);
929 g_print ("Method %s, original code:\n", name);
930 g_print ("%s\n", tmp);
935 if (signature->hasthis)
936 store_inarg (&td, 0);
937 for (i = 0; i < signature->param_count; i++)
938 store_inarg (&td, i + !!signature->hasthis);
940 body_start_offset = td.new_ip - td.new_code;
942 for (i = 0; i < header->num_locals; i++) {
943 int mt = mint_type(header->locals [i]);
944 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O) {
945 ADD_CODE(&td, MINT_INITLOCALS);
950 while (td.ip < end) {
953 g_assert (td.sp >= td.stack);
954 g_assert (td.vt_sp < 0x10000000);
955 in_offset = td.ip - header->code;
956 td.in_offsets [in_offset] = td.new_ip - td.new_code;
957 new_in_start_offset = td.new_ip - td.new_code;
959 while (td.forward_refs [in_offset] >= 0) {
960 int j = td.forward_refs [in_offset];
962 td.forward_refs [in_offset] = td.forward_refs [j];
963 if (td.in_offsets [j] < 0) {
964 int old_switch_offset = -td.in_offsets [j];
965 int new_switch_offset = td.in_offsets [old_switch_offset];
966 int switch_case = (j - old_switch_offset - 5) / 4;
967 int n_cases = read32 (header->code + old_switch_offset + 1);
968 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
969 slot = new_switch_offset + 3 + 2 * switch_case;
970 td.new_code [slot] = * (unsigned short *)(&offset);
971 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
973 int op = td.new_code [td.in_offsets [j]];
974 if (mono_interp_opargtype [op] == MintOpShortBranch) {
975 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
976 g_assert (offset <= 32767);
977 slot = td.in_offsets [j] + 1;
978 td.new_code [slot] = offset;
980 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
981 slot = td.in_offsets [j] + 1;
982 td.new_code [slot] = * (unsigned short *)(&offset);
983 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
987 if (td.stack_height [in_offset] >= 0) {
988 g_assert (is_bb_start [in_offset]);
989 if (td.stack_height [in_offset] > 0)
990 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
991 td.sp = td.stack + td.stack_height [in_offset];
992 td.vt_sp = td.vt_stack_size [in_offset];
994 if (is_bb_start [in_offset]) {
997 if (!generating_code) {
998 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1002 if (mono_interp_traceopt > 1) {
1003 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1005 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1006 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1007 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1008 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1009 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1010 (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) : "",
1011 td.vt_sp, td.max_vt_sp);
1019 SIMPLE_OP(td, MINT_BREAK);
1025 load_arg (&td, *td.ip - CEE_LDARG_0);
1032 load_local (&td, *td.ip - CEE_LDLOC_0);
1039 store_local (&td, *td.ip - CEE_STLOC_0);
1043 load_arg (&td, ((guint8 *)td.ip)[1]);
1046 case CEE_LDARGA_S: {
1047 /* NOTE: n includes this */
1048 int n = ((guint8 *)td.ip)[1];
1049 if (n == 0 && signature->hasthis) {
1050 g_error ("LDTHISA: NOPE");
1051 ADD_CODE(&td, MINT_LDTHISA);
1054 ADD_CODE(&td, MINT_LDARGA);
1055 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1057 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1062 store_arg (&td, ((guint8 *)td.ip)[1]);
1066 load_local (&td, ((guint8 *)td.ip)[1]);
1070 ADD_CODE(&td, MINT_LDLOCA_S);
1071 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1072 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1076 store_local (&td, ((guint8 *)td.ip)[1]);
1080 SIMPLE_OP(td, MINT_LDNULL);
1081 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1084 SIMPLE_OP(td, MINT_LDC_I4_M1);
1085 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1088 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1089 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1090 SIMPLE_OP(td, MINT_CEQ0_I4);
1093 SIMPLE_OP(td, MINT_LDC_I4_0);
1094 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1098 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1099 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1100 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1103 SIMPLE_OP(td, MINT_LDC_I4_1);
1104 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1114 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1115 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1118 ADD_CODE(&td, MINT_LDC_I4_S);
1119 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1121 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1124 i32 = read32 (td.ip + 1);
1125 ADD_CODE(&td, MINT_LDC_I4);
1128 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1131 gint64 val = read64 (td.ip + 1);
1132 ADD_CODE(&td, MINT_LDC_I8);
1135 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1140 readr4 (td.ip + 1, &val);
1141 ADD_CODE(&td, MINT_LDC_R4);
1144 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1149 readr8 (td.ip + 1, &val);
1150 ADD_CODE(&td, MINT_LDC_R8);
1153 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1157 int type = td.sp [-1].type;
1158 MonoClass *klass = td.sp [-1].klass;
1159 if (td.sp [-1].type == STACK_TYPE_VT) {
1160 gint32 size = mono_class_value_size (klass, NULL);
1162 ADD_CODE(&td, MINT_DUP_VT);
1163 WRITE32(&td, &size);
1166 SIMPLE_OP(td, MINT_DUP);
1167 PUSH_TYPE(&td, type, klass);
1171 CHECK_STACK(&td, 1);
1172 SIMPLE_OP(td, MINT_POP);
1174 if (td.sp [-1].type == STACK_TYPE_VT) {
1175 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1176 size = (size + 7) & ~7;
1177 ADD_CODE(&td, MINT_VTRESULT);
1179 WRITE32(&td, &size);
1186 if (td.sp > td.stack)
1187 g_warning ("CEE_JMP: stack must be empty");
1188 token = read32 (td.ip + 1);
1189 m = mono_get_method_full (image, token, NULL, generic_context);
1190 ADD_CODE (&td, MINT_JMP);
1191 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1192 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1196 case CEE_CALLVIRT: /* Fall through */
1197 case CEE_CALLI: /* Fall through */
1199 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
1200 constrained_class = NULL;
1205 if (signature->ret->type != MONO_TYPE_VOID) {
1207 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1208 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1209 vt_size = mono_class_value_size (klass, NULL);
1210 vt_size = (vt_size + 7) & ~7;
1213 if (td.sp > td.stack)
1214 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1215 if (td.vt_sp != vt_size)
1216 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1218 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1220 ADD_CODE(&td, MINT_RET_VT);
1221 WRITE32(&td, &vt_size);
1224 generating_code = 0;
1228 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1230 generating_code = 0;
1233 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1235 generating_code = 0;
1238 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1242 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1246 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1250 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1254 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1258 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1262 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1266 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1270 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1274 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1278 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1282 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1286 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1290 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1294 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1298 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1302 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1306 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1310 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1314 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1318 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1322 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1326 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1330 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1335 const unsigned char *next_ip;
1336 const unsigned char *base_ip = td.ip;
1337 unsigned short *next_new_ip;
1340 ADD_CODE(&td, MINT_SWITCH);
1341 ADD_CODE(&td, * (unsigned short *)(&n));
1342 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1344 next_ip = td.ip + n * 4;
1345 next_new_ip = td.new_ip + n * 2;
1346 for (i = 0; i < n; i++) {
1347 offset = read32 (td.ip);
1348 target = next_ip - td.il_code + offset;
1350 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1352 int prev = td.forward_refs [target];
1353 td.forward_refs [td.ip - td.il_code] = prev;
1354 td.forward_refs [target] = td.ip - td.il_code;
1355 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1357 ADD_CODE(&td, * (unsigned short *)(&target));
1358 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1365 CHECK_STACK (&td, 1);
1366 SIMPLE_OP (td, MINT_LDIND_I1);
1367 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1370 CHECK_STACK (&td, 1);
1371 SIMPLE_OP (td, MINT_LDIND_U1);
1372 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1375 CHECK_STACK (&td, 1);
1376 SIMPLE_OP (td, MINT_LDIND_I2);
1377 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1380 CHECK_STACK (&td, 1);
1381 SIMPLE_OP (td, MINT_LDIND_U2);
1382 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1385 CHECK_STACK (&td, 1);
1386 SIMPLE_OP (td, MINT_LDIND_I4);
1387 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1390 CHECK_STACK (&td, 1);
1391 SIMPLE_OP (td, MINT_LDIND_U4);
1392 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1395 CHECK_STACK (&td, 1);
1396 SIMPLE_OP (td, MINT_LDIND_I8);
1397 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1400 CHECK_STACK (&td, 1);
1401 SIMPLE_OP (td, MINT_LDIND_I);
1403 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1406 CHECK_STACK (&td, 1);
1407 SIMPLE_OP (td, MINT_LDIND_R4);
1408 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1411 CHECK_STACK (&td, 1);
1412 SIMPLE_OP (td, MINT_LDIND_R8);
1413 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1416 CHECK_STACK (&td, 1);
1417 SIMPLE_OP (td, MINT_LDIND_REF);
1418 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1421 CHECK_STACK (&td, 2);
1422 SIMPLE_OP (td, MINT_STIND_REF);
1426 CHECK_STACK (&td, 2);
1427 SIMPLE_OP (td, MINT_STIND_I1);
1431 CHECK_STACK (&td, 2);
1432 SIMPLE_OP (td, MINT_STIND_I2);
1436 CHECK_STACK (&td, 2);
1437 SIMPLE_OP (td, MINT_STIND_I4);
1441 CHECK_STACK (&td, 2);
1442 SIMPLE_OP (td, MINT_STIND_I);
1446 CHECK_STACK (&td, 2);
1447 SIMPLE_OP (td, MINT_STIND_I8);
1451 CHECK_STACK (&td, 2);
1452 SIMPLE_OP (td, MINT_STIND_R4);
1456 CHECK_STACK (&td, 2);
1457 SIMPLE_OP (td, MINT_STIND_R8);
1461 binary_arith_op(&td, MINT_ADD_I4);
1465 binary_arith_op(&td, MINT_SUB_I4);
1469 binary_arith_op(&td, MINT_MUL_I4);
1473 binary_arith_op(&td, MINT_DIV_I4);
1477 binary_arith_op(&td, MINT_DIV_UN_I4);
1481 binary_int_op (&td, MINT_REM_I4);
1485 binary_int_op (&td, MINT_REM_UN_I4);
1489 binary_int_op (&td, MINT_AND_I4);
1493 binary_int_op (&td, MINT_OR_I4);
1497 binary_int_op (&td, MINT_XOR_I4);
1501 shift_op (&td, MINT_SHL_I4);
1505 shift_op (&td, MINT_SHR_I4);
1509 shift_op (&td, MINT_SHR_UN_I4);
1513 unary_arith_op (&td, MINT_NEG_I4);
1517 unary_arith_op (&td, MINT_NOT_I4);
1521 CHECK_STACK (&td, 1);
1522 switch (td.sp [-1].type) {
1524 ADD_CODE(&td, MINT_CONV_U1_R8);
1527 ADD_CODE(&td, MINT_CONV_U1_I4);
1530 ADD_CODE(&td, MINT_CONV_U1_I8);
1533 g_assert_not_reached ();
1536 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1539 CHECK_STACK (&td, 1);
1540 switch (td.sp [-1].type) {
1542 ADD_CODE(&td, MINT_CONV_I1_R8);
1545 ADD_CODE(&td, MINT_CONV_I1_I4);
1548 ADD_CODE(&td, MINT_CONV_I1_I8);
1551 g_assert_not_reached ();
1554 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1557 CHECK_STACK (&td, 1);
1558 switch (td.sp [-1].type) {
1560 ADD_CODE(&td, MINT_CONV_U2_R8);
1563 ADD_CODE(&td, MINT_CONV_U2_I4);
1566 ADD_CODE(&td, MINT_CONV_U2_I8);
1569 g_assert_not_reached ();
1572 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1575 CHECK_STACK (&td, 1);
1576 switch (td.sp [-1].type) {
1578 ADD_CODE(&td, MINT_CONV_I2_R8);
1581 ADD_CODE(&td, MINT_CONV_I2_I4);
1584 ADD_CODE(&td, MINT_CONV_I2_I8);
1587 g_assert_not_reached ();
1590 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1593 CHECK_STACK (&td, 1);
1594 switch (td.sp [-1].type) {
1596 #if SIZEOF_VOID_P == 4
1597 ADD_CODE(&td, MINT_CONV_U4_R8);
1599 ADD_CODE(&td, MINT_CONV_U8_R8);
1603 #if SIZEOF_VOID_P == 8
1604 ADD_CODE(&td, MINT_CONV_U8_I4);
1608 #if SIZEOF_VOID_P == 4
1609 ADD_CODE(&td, MINT_CONV_U4_I8);
1615 g_assert_not_reached ();
1618 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1621 CHECK_STACK (&td, 1);
1622 switch (td.sp [-1].type) {
1624 #if SIZEOF_VOID_P == 8
1625 ADD_CODE(&td, MINT_CONV_I8_R8);
1627 ADD_CODE(&td, MINT_CONV_I4_R8);
1631 #if SIZEOF_VOID_P == 8
1632 ADD_CODE(&td, MINT_CONV_I8_I4);
1640 #if SIZEOF_VOID_P == 4
1641 ADD_CODE(&td, MINT_CONV_I4_I8);
1645 g_assert_not_reached ();
1648 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1651 CHECK_STACK (&td, 1);
1652 switch (td.sp [-1].type) {
1654 ADD_CODE(&td, MINT_CONV_U4_R8);
1659 ADD_CODE(&td, MINT_CONV_U4_I8);
1662 #if SIZEOF_VOID_P == 8
1663 ADD_CODE(&td, MINT_CONV_U4_I8);
1667 g_assert_not_reached ();
1670 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1673 CHECK_STACK (&td, 1);
1674 switch (td.sp [-1].type) {
1676 ADD_CODE(&td, MINT_CONV_I4_R8);
1681 ADD_CODE(&td, MINT_CONV_I4_I8);
1684 #if SIZEOF_VOID_P == 8
1685 ADD_CODE(&td, MINT_CONV_I4_I8);
1689 g_assert_not_reached ();
1692 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1695 CHECK_STACK (&td, 1);
1696 switch (td.sp [-1].type) {
1698 ADD_CODE(&td, MINT_CONV_I8_R8);
1701 ADD_CODE(&td, MINT_CONV_I8_I4);
1706 #if SIZEOF_VOID_P == 4
1707 ADD_CODE(&td, MINT_CONV_I8_I4);
1711 g_assert_not_reached ();
1714 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1717 CHECK_STACK (&td, 1);
1718 switch (td.sp [-1].type) {
1720 ADD_CODE(&td, MINT_CONV_R4_R8);
1723 ADD_CODE(&td, MINT_CONV_R4_I8);
1726 ADD_CODE(&td, MINT_CONV_R4_I4);
1729 g_assert_not_reached ();
1732 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1735 CHECK_STACK (&td, 1);
1736 switch (td.sp [-1].type) {
1738 ADD_CODE(&td, MINT_CONV_R8_I4);
1741 ADD_CODE(&td, MINT_CONV_R8_I8);
1746 g_assert_not_reached ();
1749 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1752 CHECK_STACK (&td, 1);
1753 switch (td.sp [-1].type) {
1755 ADD_CODE(&td, MINT_CONV_U8_I4);
1760 ADD_CODE(&td, MINT_CONV_U8_R8);
1763 #if SIZEOF_VOID_P == 4
1764 ADD_CODE(&td, MINT_CONV_U8_I4);
1768 g_assert_not_reached ();
1771 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1777 vtklass = mono_class_get_full (image, read32 (ip), generic_context);
1780 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
1786 CHECK_STACK (&td, 1);
1788 token = read32 (td.ip + 1);
1790 if (method->wrapper_type != MONO_WRAPPER_NONE)
1791 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1793 klass = mono_class_get_full (image, token, generic_context);
1795 ADD_CODE(&td, MINT_LDOBJ);
1796 ADD_CODE(&td, get_data_item_index(&td, klass));
1797 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1798 size = mono_class_value_size (klass, NULL);
1802 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1807 token = mono_metadata_token_index (read32 (td.ip + 1));
1809 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1810 s = mono_string_new_wrapper(
1811 mono_method_get_wrapper_data (method, token));
1814 s = mono_ldstr (domain, image, token);
1815 ADD_CODE(&td, MINT_LDSTR);
1816 ADD_CODE(&td, get_data_item_index (&td, s));
1817 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1822 MonoMethodSignature *csignature;
1823 guint32 vt_stack_used = 0;
1824 guint32 vt_res_size = 0;
1827 token = read32 (td.ip);
1830 if (method->wrapper_type != MONO_WRAPPER_NONE)
1831 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1833 m = mono_get_method_full (image, token, NULL, generic_context);
1835 csignature = mono_method_signature (m);
1837 td.sp -= csignature->param_count;
1838 ADD_CODE(&td, MINT_NEWOBJ);
1839 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1840 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1842 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1843 vt_res_size = mono_class_value_size (klass, NULL);
1844 PUSH_VT (&td, vt_res_size);
1846 for (i = 0; i < csignature->param_count; ++i) {
1847 int mt = mint_type(csignature->params [i]);
1848 if (mt == MINT_TYPE_VT) {
1849 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1850 gint32 size = mono_class_value_size (k, NULL);
1851 size = (size + 7) & ~7;
1852 vt_stack_used += size;
1855 if (vt_stack_used != 0 || vt_res_size != 0) {
1856 ADD_CODE(&td, MINT_VTRESULT);
1857 ADD_CODE(&td, vt_res_size);
1858 WRITE32(&td, &vt_stack_used);
1859 td.vt_sp -= vt_stack_used;
1861 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1865 CHECK_STACK (&td, 1);
1866 token = read32 (td.ip + 1);
1867 klass = mono_class_get_full (image, token, generic_context);
1868 ADD_CODE(&td, MINT_CASTCLASS);
1869 ADD_CODE(&td, get_data_item_index (&td, klass));
1870 td.sp [-1].klass = klass;
1874 CHECK_STACK (&td, 1);
1875 token = read32 (td.ip + 1);
1876 klass = mono_class_get_full (image, token, generic_context);
1877 ADD_CODE(&td, MINT_ISINST);
1878 ADD_CODE(&td, get_data_item_index (&td, klass));
1882 switch (td.sp [-1].type) {
1886 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1889 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1892 g_assert_not_reached ();
1894 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1898 CHECK_STACK (&td, 1);
1899 token = read32 (td.ip + 1);
1901 if (method->wrapper_type != MONO_WRAPPER_NONE)
1902 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1904 klass = mono_class_get_full (image, token, generic_context);
1906 if (mono_class_is_nullable (klass)) {
1907 g_error ("cee_unbox: implement Nullable");
1910 ADD_CODE(&td, MINT_UNBOX);
1911 ADD_CODE(&td, get_data_item_index (&td, klass));
1912 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1916 CHECK_STACK (&td, 1);
1917 token = read32 (td.ip + 1);
1919 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1920 klass = mono_class_get_full (image, token, generic_context);
1922 if (mini_type_is_reference (&klass->byval_arg)) {
1923 g_error ("unbox_any: generic class is reference type");
1924 } else if (mono_class_is_nullable (klass)) {
1925 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1926 /* td.ip is incremented by interp_transform_call */
1927 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
1929 int mt = mint_type (&klass->byval_arg);
1930 ADD_CODE (&td, MINT_UNBOX);
1931 ADD_CODE (&td, get_data_item_index (&td, klass));
1932 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1933 if (mt == MINT_TYPE_VT) {
1934 int size = mono_class_value_size (klass, NULL);
1935 PUSH_VT (&td, size);
1942 CHECK_STACK (&td, 1);
1943 SIMPLE_OP (td, MINT_THROW);
1945 generating_code = 0;
1948 CHECK_STACK (&td, 1);
1949 token = read32 (td.ip + 1);
1950 field = mono_field_from_token (image, token, &klass, generic_context);
1951 mono_class_init (klass);
1952 mt = mint_type(field->type);
1953 ADD_CODE(&td, MINT_LDFLDA);
1954 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1956 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1959 CHECK_STACK (&td, 1);
1960 token = read32 (td.ip + 1);
1961 field = mono_field_from_token (image, token, &klass, generic_context);
1962 mono_class_init (klass);
1964 MonoClass *field_klass = mono_class_from_mono_type (field->type);
1965 mt = mint_type (&field_klass->byval_arg);
1966 if (klass->marshalbyref) {
1967 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
1968 ADD_CODE(&td, get_data_item_index (&td, field));
1970 ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
1971 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1973 if (mt == MINT_TYPE_VT) {
1974 int size = mono_class_value_size (field_klass, NULL);
1976 WRITE32(&td, &size);
1978 if (td.sp [-1].type == STACK_TYPE_VT) {
1979 int size = mono_class_value_size (klass, NULL);
1980 size = (size + 7) & ~7;
1982 ADD_CODE (&td, MINT_VTRESULT);
1984 WRITE32 (&td, &size);
1987 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
1991 CHECK_STACK (&td, 2);
1992 token = read32 (td.ip + 1);
1993 field = mono_field_from_token (image, token, &klass, generic_context);
1994 mono_class_init (klass);
1995 mt = mint_type(field->type);
1996 if (klass->marshalbyref) {
1997 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
1998 ADD_CODE(&td, get_data_item_index (&td, field));
2000 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2001 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2003 if (mt == MINT_TYPE_VT) {
2004 MonoClass *klass = mono_class_from_mono_type (field->type);
2005 int size = mono_class_value_size (klass, NULL);
2007 WRITE32(&td, &size);
2013 token = read32 (td.ip + 1);
2014 field = mono_field_from_token (image, token, &klass, generic_context);
2015 ADD_CODE(&td, MINT_LDSFLDA);
2016 ADD_CODE(&td, get_data_item_index (&td, field));
2018 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2021 token = read32 (td.ip + 1);
2022 field = mono_field_from_token (image, token, &klass, generic_context);
2023 mt = mint_type(field->type);
2024 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2025 ADD_CODE(&td, get_data_item_index (&td, field));
2027 if (mt == MINT_TYPE_VT) {
2028 MonoClass *klass = mono_class_from_mono_type (field->type);
2029 int size = mono_class_value_size (klass, NULL);
2031 WRITE32(&td, &size);
2032 klass = field->type->data.klass;
2034 if (mt == MINT_TYPE_O)
2035 klass = mono_class_from_mono_type (field->type);
2038 PUSH_TYPE(&td, stack_type [mt], klass);
2041 CHECK_STACK (&td, 1);
2042 token = read32 (td.ip + 1);
2043 field = mono_field_from_token (image, token, &klass, generic_context);
2044 mt = mint_type(field->type);
2045 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2046 ADD_CODE(&td, get_data_item_index (&td, field));
2047 if (mt == MINT_TYPE_VT) {
2048 MonoClass *klass = mono_class_from_mono_type (field->type);
2049 int size = mono_class_value_size (klass, NULL);
2051 WRITE32 (&td, &size);
2058 token = read32 (td.ip + 1);
2060 if (method->wrapper_type != MONO_WRAPPER_NONE)
2061 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2063 klass = mono_class_get_full (image, token, generic_context);
2065 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2066 ADD_CODE(&td, get_data_item_index (&td, klass));
2067 if (td.sp [-1].type == STACK_TYPE_VT) {
2068 size = mono_class_value_size (klass, NULL);
2069 size = (size + 7) & ~7;
2076 case CEE_CONV_OVF_I_UN:
2077 CHECK_STACK (&td, 1);
2078 switch (td.sp [-1].type) {
2080 #if SIZEOF_VOID_P == 8
2081 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2083 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2090 #if SIZEOF_VOID_P == 8
2091 ADD_CODE(&td, MINT_CONV_I8_U4);
2095 g_assert_not_reached ();
2098 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2101 case CEE_CONV_OVF_I8_UN:
2102 CHECK_STACK (&td, 1);
2103 switch (td.sp [-1].type) {
2105 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2110 ADD_CODE(&td, MINT_CONV_I8_U4);
2113 g_assert_not_reached ();
2116 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2121 CHECK_STACK (&td, 1);
2122 token = read32 (td.ip + 1);
2123 if (method->wrapper_type != MONO_WRAPPER_NONE)
2124 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2126 klass = mono_class_get_full (image, token, generic_context);
2128 if (mono_class_is_nullable (klass)) {
2129 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2130 /* td.ip is incremented by interp_transform_call */
2131 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2132 } else if (!klass->valuetype) {
2133 /* already boxed, do nothing. */
2136 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2137 size = mono_class_value_size (klass, NULL);
2138 size = (size + 7) & ~7;
2141 ADD_CODE(&td, MINT_BOX);
2142 ADD_CODE(&td, get_data_item_index (&td, klass));
2144 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2151 CHECK_STACK (&td, 1);
2152 token = read32 (td.ip + 1);
2154 if (method->wrapper_type != MONO_WRAPPER_NONE)
2155 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2157 klass = mono_class_get_full (image, token, generic_context);
2159 ADD_CODE(&td, MINT_NEWARR);
2160 ADD_CODE(&td, get_data_item_index (&td, klass));
2161 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2165 CHECK_STACK (&td, 1);
2166 SIMPLE_OP (td, MINT_LDLEN);
2167 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2170 CHECK_STACK (&td, 2);
2172 token = read32 (td.ip + 1);
2174 if (method->wrapper_type != MONO_WRAPPER_NONE)
2175 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2177 klass = mono_class_get_full (image, token, generic_context);
2179 ADD_CODE(&td, MINT_LDELEMA);
2180 ADD_CODE(&td, get_data_item_index (&td, klass));
2183 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2186 CHECK_STACK (&td, 2);
2188 SIMPLE_OP (td, MINT_LDELEM_I1);
2190 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2193 CHECK_STACK (&td, 2);
2195 SIMPLE_OP (td, MINT_LDELEM_U1);
2197 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2200 CHECK_STACK (&td, 2);
2202 SIMPLE_OP (td, MINT_LDELEM_I2);
2204 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2207 CHECK_STACK (&td, 2);
2209 SIMPLE_OP (td, MINT_LDELEM_U2);
2211 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2214 CHECK_STACK (&td, 2);
2216 SIMPLE_OP (td, MINT_LDELEM_I4);
2218 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2221 CHECK_STACK (&td, 2);
2223 SIMPLE_OP (td, MINT_LDELEM_U4);
2225 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2228 CHECK_STACK (&td, 2);
2230 SIMPLE_OP (td, MINT_LDELEM_I8);
2232 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2235 CHECK_STACK (&td, 2);
2237 SIMPLE_OP (td, MINT_LDELEM_I);
2239 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2242 CHECK_STACK (&td, 2);
2244 SIMPLE_OP (td, MINT_LDELEM_R4);
2246 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2249 CHECK_STACK (&td, 2);
2251 SIMPLE_OP (td, MINT_LDELEM_R8);
2253 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2255 case CEE_LDELEM_REF:
2256 CHECK_STACK (&td, 2);
2258 SIMPLE_OP (td, MINT_LDELEM_REF);
2260 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2263 CHECK_STACK (&td, 2);
2264 token = read32 (td.ip + 1);
2265 klass = mono_class_get_full (image, token, generic_context);
2266 switch (mint_type (&klass->byval_arg)) {
2269 SIMPLE_OP (td, MINT_LDELEM_I1);
2271 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2275 SIMPLE_OP (td, MINT_LDELEM_U1);
2277 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2281 SIMPLE_OP (td, MINT_LDELEM_U2);
2283 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2287 SIMPLE_OP (td, MINT_LDELEM_I2);
2289 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2293 SIMPLE_OP (td, MINT_LDELEM_I4);
2295 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2299 SIMPLE_OP (td, MINT_LDELEM_I8);
2301 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2305 SIMPLE_OP (td, MINT_LDELEM_R4);
2307 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2311 SIMPLE_OP (td, MINT_LDELEM_R8);
2313 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2317 SIMPLE_OP (td, MINT_LDELEM_REF);
2319 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2321 case MINT_TYPE_VT: {
2322 int size = mono_class_value_size (klass, NULL);
2324 SIMPLE_OP (td, MINT_LDELEM_VT);
2325 ADD_CODE (&td, get_data_item_index (&td, klass));
2326 WRITE32 (&td, &size);
2328 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2329 PUSH_VT (&td, size);
2333 GString *res = g_string_new ("");
2334 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2335 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2336 g_string_free (res, TRUE);
2344 CHECK_STACK (&td, 3);
2346 SIMPLE_OP (td, MINT_STELEM_I);
2350 CHECK_STACK (&td, 3);
2352 SIMPLE_OP (td, MINT_STELEM_I1);
2356 CHECK_STACK (&td, 3);
2358 SIMPLE_OP (td, MINT_STELEM_I2);
2362 CHECK_STACK (&td, 3);
2364 SIMPLE_OP (td, MINT_STELEM_I4);
2368 CHECK_STACK (&td, 3);
2370 SIMPLE_OP (td, MINT_STELEM_I8);
2374 CHECK_STACK (&td, 3);
2376 SIMPLE_OP (td, MINT_STELEM_R4);
2380 CHECK_STACK (&td, 3);
2382 SIMPLE_OP (td, MINT_STELEM_R8);
2385 case CEE_STELEM_REF:
2386 CHECK_STACK (&td, 3);
2388 SIMPLE_OP (td, MINT_STELEM_REF);
2392 CHECK_STACK (&td, 3);
2394 token = read32 (td.ip + 1);
2395 klass = mono_class_get_full (image, token, generic_context);
2396 switch (mint_type (&klass->byval_arg)) {
2398 SIMPLE_OP (td, MINT_STELEM_I4);
2401 SIMPLE_OP (td, MINT_STELEM_REF);
2403 case MINT_TYPE_VT: {
2404 int size = mono_class_value_size (klass, NULL);
2405 SIMPLE_OP (td, MINT_STELEM_VT);
2406 ADD_CODE (&td, get_data_item_index (&td, klass));
2407 WRITE32 (&td, &size);
2412 GString *res = g_string_new ("");
2413 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2414 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2415 g_string_free (res, TRUE);
2424 case CEE_CONV_OVF_U1:
2426 case CEE_CONV_OVF_I8:
2428 #if SIZEOF_VOID_P == 8
2429 case CEE_CONV_OVF_U:
2431 case CEE_REFANYVAL: ves_abort(); break;
2434 CHECK_STACK (&td, 1);
2435 SIMPLE_OP (td, MINT_CKFINITE);
2437 case CEE_CONV_OVF_I1:
2438 case CEE_CONV_OVF_I1_UN:
2439 CHECK_STACK (&td, 1);
2440 switch (td.sp [-1].type) {
2442 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2445 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2448 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2451 g_assert_not_reached ();
2454 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2456 case CEE_CONV_OVF_U1:
2457 case CEE_CONV_OVF_U1_UN:
2458 CHECK_STACK (&td, 1);
2459 switch (td.sp [-1].type) {
2461 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2464 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2467 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2470 g_assert_not_reached ();
2473 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2475 case CEE_CONV_OVF_I2:
2476 case CEE_CONV_OVF_I2_UN:
2477 CHECK_STACK (&td, 1);
2478 switch (td.sp [-1].type) {
2480 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2483 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2486 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2489 g_assert_not_reached ();
2492 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2494 case CEE_CONV_OVF_U2_UN:
2495 case CEE_CONV_OVF_U2:
2496 CHECK_STACK (&td, 1);
2497 switch (td.sp [-1].type) {
2499 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2502 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2505 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2508 g_assert_not_reached ();
2511 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2513 #if SIZEOF_VOID_P == 4
2514 case CEE_CONV_OVF_I:
2516 case CEE_CONV_OVF_I4:
2517 case CEE_CONV_OVF_I4_UN:
2518 CHECK_STACK (&td, 1);
2519 switch (td.sp [-1].type) {
2521 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2524 if (*td.ip == CEE_CONV_OVF_I4_UN)
2525 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2528 ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
2531 g_assert_not_reached ();
2534 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2536 #if SIZEOF_VOID_P == 4
2537 case CEE_CONV_OVF_U:
2539 case CEE_CONV_OVF_U4:
2540 case CEE_CONV_OVF_U4_UN:
2541 CHECK_STACK (&td, 1);
2542 switch (td.sp [-1].type) {
2544 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2547 if (*td.ip != CEE_CONV_OVF_U4_UN)
2548 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2551 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2554 g_assert_not_reached ();
2557 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2559 #if SIZEOF_VOID_P == 8
2560 case CEE_CONV_OVF_I:
2562 case CEE_CONV_OVF_I8:
2563 CHECK_STACK (&td, 1);
2564 switch (td.sp [-1].type) {
2566 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2569 ADD_CODE(&td, MINT_CONV_I8_I4);
2574 g_assert_not_reached ();
2577 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2579 #if SIZEOF_VOID_P == 8
2580 case CEE_CONV_OVF_U:
2582 case CEE_CONV_OVF_U8:
2583 CHECK_STACK (&td, 1);
2584 switch (td.sp [-1].type) {
2586 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2589 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2594 g_assert_not_reached ();
2597 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2602 token = read32 (td.ip + 1);
2603 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2604 handle = mono_method_get_wrapper_data (method, token);
2605 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2606 if (klass == mono_defaults.typehandle_class)
2607 handle = &((MonoClass *) handle)->byval_arg;
2609 handle = mono_ldtoken (image, token, &klass, generic_context);
2611 mt = mint_type(&klass->byval_arg);
2612 g_assert (mt == MINT_TYPE_VT);
2613 size = mono_class_value_size (klass, NULL);
2614 g_assert (size == sizeof(gpointer));
2615 PUSH_VT(&td, sizeof(gpointer));
2616 ADD_CODE(&td, MINT_LDTOKEN);
2617 ADD_CODE(&td, get_data_item_index (&td, handle));
2618 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2623 binary_arith_op(&td, MINT_ADD_OVF_I4);
2626 case CEE_ADD_OVF_UN:
2627 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2631 binary_arith_op(&td, MINT_MUL_OVF_I4);
2634 case CEE_MUL_OVF_UN:
2635 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2639 binary_arith_op(&td, MINT_SUB_OVF_I4);
2642 case CEE_SUB_OVF_UN:
2643 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2646 case CEE_ENDFINALLY:
2647 SIMPLE_OP (td, MINT_ENDFINALLY);
2648 generating_code = 0;
2652 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2654 generating_code = 0;
2658 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2660 generating_code = 0;
2665 case CEE_MONO_CALLI_EXTRA_ARG:
2666 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2667 ADD_CODE (&td, MINT_POP);
2670 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2672 case CEE_MONO_JIT_ICALL_ADDR: {
2675 MonoJitICallInfo *info;
2677 token = read32 (td.ip + 1);
2679 func = mono_method_get_wrapper_data (method, token);
2680 info = mono_find_jit_icall_by_addr (func);
2682 ADD_CODE (&td, MINT_LDFTN);
2683 ADD_CODE (&td, get_data_item_index (&td, func));
2684 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2687 case CEE_MONO_ICALL: {
2690 MonoJitICallInfo *info;
2692 token = read32 (td.ip + 1);
2694 func = mono_method_get_wrapper_data (method, token);
2695 info = mono_find_jit_icall_by_addr (func);
2698 CHECK_STACK (&td, info->sig->param_count);
2699 switch (info->sig->param_count) {
2701 if (MONO_TYPE_IS_VOID (info->sig->ret))
2702 ADD_CODE (&td,MINT_ICALL_V_V);
2704 ADD_CODE (&td, MINT_ICALL_V_P);
2707 if (MONO_TYPE_IS_VOID (info->sig->ret))
2708 ADD_CODE (&td,MINT_ICALL_P_V);
2710 ADD_CODE (&td,MINT_ICALL_P_P);
2713 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2714 if (info->sig->params [1]->type == MONO_TYPE_I4)
2715 ADD_CODE (&td,MINT_ICALL_PI_V);
2717 ADD_CODE (&td,MINT_ICALL_PP_V);
2719 if (info->sig->params [1]->type == MONO_TYPE_I4)
2720 ADD_CODE (&td,MINT_ICALL_PI_P);
2722 ADD_CODE (&td,MINT_ICALL_PP_P);
2726 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2727 if (info->sig->params [2]->type == MONO_TYPE_I4)
2728 ADD_CODE (&td,MINT_ICALL_PPI_V);
2730 ADD_CODE (&td,MINT_ICALL_PPP_V);
2733 g_assert_not_reached ();
2736 if (func == mono_ftnptr_to_delegate) {
2737 g_error ("TODO: ?");
2739 ADD_CODE(&td, get_data_item_index (&td, func));
2740 td.sp -= info->sig->param_count;
2742 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2744 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2748 case CEE_MONO_VTADDR: {
2750 CHECK_STACK (&td, 1);
2751 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2752 size = mono_class_native_size(td.sp [-1].klass, NULL);
2754 size = mono_class_value_size(td.sp [-1].klass, NULL);
2755 size = (size + 7) & ~7;
2756 ADD_CODE(&td, MINT_VTRESULT);
2758 WRITE32(&td, &size);
2761 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2764 case CEE_MONO_LDPTR:
2765 case CEE_MONO_CLASSCONST:
2766 token = read32 (td.ip + 1);
2768 ADD_CODE(&td, MINT_MONO_LDPTR);
2769 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2770 td.sp [0].type = STACK_TYPE_I;
2773 case CEE_MONO_OBJADDR:
2774 CHECK_STACK (&td, 1);
2776 td.sp[-1].type = STACK_TYPE_MP;
2779 case CEE_MONO_NEWOBJ:
2780 token = read32 (td.ip + 1);
2782 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2783 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2784 td.sp [0].type = STACK_TYPE_O;
2787 case CEE_MONO_RETOBJ:
2788 CHECK_STACK (&td, 1);
2789 token = read32 (td.ip + 1);
2791 ADD_CODE(&td, MINT_MONO_RETOBJ);
2794 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2796 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2798 if (td.sp > td.stack)
2799 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2801 case CEE_MONO_LDNATIVEOBJ:
2802 token = read32 (td.ip + 1);
2804 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2805 g_assert(klass->valuetype);
2806 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2808 case CEE_MONO_SAVE_LMF:
2809 case CEE_MONO_RESTORE_LMF:
2810 case CEE_MONO_NOT_TAKEN:
2813 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2814 ADD_CODE (&td, MINT_MONO_LDPTR);
2815 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2816 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2820 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2830 case CEE_PREFIXREF: ves_abort(); break;
2833 * Note: Exceptions thrown when executing a prefixed opcode need
2834 * to take into account the number of prefix bytes (usually the
2835 * throw point is just (ip - n_prefix_bytes).
2841 case CEE_ARGLIST: ves_abort(); break;
2844 CHECK_STACK(&td, 2);
2845 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2846 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2848 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2850 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2854 CHECK_STACK(&td, 2);
2855 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2856 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2858 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2860 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2864 CHECK_STACK(&td, 2);
2865 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2866 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2868 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2870 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2874 CHECK_STACK(&td, 2);
2875 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2876 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2878 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2880 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2884 CHECK_STACK(&td, 2);
2885 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2886 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2888 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2890 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2893 case CEE_LDVIRTFTN: /* fallthrough */
2896 if (*td.ip == CEE_LDVIRTFTN) {
2897 CHECK_STACK (&td, 1);
2900 token = read32 (td.ip + 1);
2901 if (method->wrapper_type != MONO_WRAPPER_NONE)
2902 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2904 m = mono_get_method_full (image, token, NULL, generic_context);
2906 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2907 m = mono_marshal_get_synchronized_wrapper (m);
2909 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2910 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2911 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2913 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2917 load_arg (&td, read16 (td.ip + 1));
2921 int n = read16 (td.ip + 1);
2922 if (n == 0 && signature->hasthis) {
2923 g_error ("LDTHISA: NOPE");
2924 ADD_CODE(&td, MINT_LDTHISA);
2927 ADD_CODE(&td, MINT_LDARGA);
2928 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2930 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2935 store_arg (&td, read16 (td.ip + 1));
2939 load_local (&td, read16 (td.ip + 1));
2943 ADD_CODE(&td, MINT_LDLOCA_S);
2944 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2945 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2949 store_local (&td, read16 (td.ip + 1));
2953 CHECK_STACK (&td, 1);
2954 #if SIZEOF_VOID_P == 8
2955 if (td.sp [-1].type == STACK_TYPE_I8)
2956 ADD_CODE(&td, MINT_CONV_I4_I8);
2958 ADD_CODE(&td, MINT_LOCALLOC);
2959 if (td.sp != td.stack + 1)
2960 g_warning("CEE_LOCALLOC: stack not empty");
2962 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2965 case CEE_UNUSED57: ves_abort(); break;
2966 case CEE_ENDFILTER: ves_abort(); break;
2968 case CEE_UNALIGNED_:
2970 /* FIX: should do something? */;
2974 /* FIX: should do something? */;
2978 /* FIX: should do something? */;
2981 CHECK_STACK(&td, 1);
2982 token = read32 (td.ip + 1);
2983 klass = mono_class_get_full (image, token, generic_context);
2984 ADD_CODE(&td, MINT_INITOBJ);
2985 i32 = mono_class_value_size (klass, NULL);
2991 CHECK_STACK(&td, 3);
2992 /* FIX? convert length to I8? */
2993 ADD_CODE(&td, MINT_CPBLK);
2997 case CEE_CONSTRAINED_:
2998 token = read32 (td.ip + 1);
2999 constrained_class = mono_class_get_full (image, token, generic_context);
3000 mono_class_init (constrained_class);
3004 CHECK_STACK(&td, 3);
3005 ADD_CODE(&td, MINT_INITBLK);
3010 /* FIXME: implement */
3015 SIMPLE_OP (td, MINT_RETHROW);
3016 generating_code = 0;
3020 token = read32 (td.ip + 1);
3022 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
3024 MonoType *type = mono_type_create_from_typespec (image, token);
3025 size = mono_type_size (type, &align);
3028 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
3029 mono_class_init (szclass);
3031 if (!szclass->valuetype)
3032 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3034 size = mono_class_value_size (szclass, &align);
3036 ADD_CODE(&td, MINT_LDC_I4);
3037 WRITE32(&td, &size);
3038 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3042 case CEE_REFANYTYPE: ves_abort(); break;
3045 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);
3049 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3052 if (td.new_ip - td.new_code != new_in_start_offset)
3053 td.last_new_ip = td.new_code + new_in_start_offset;
3054 else if (td.is_bb_start [td.in_start - td.il_code])
3055 td.is_bb_start [td.ip - td.il_code] = 1;
3057 td.last_ip = td.in_start;
3060 if (mono_interp_traceopt) {
3061 const guint16 *p = td.new_code;
3062 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3063 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3064 while (p < td.new_ip) {
3065 p = mono_interp_dis_mintop(td.new_code, p);
3070 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
3071 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3072 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
3073 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3074 g_free (td.new_code);
3075 rtm->new_body_start = rtm->code + body_start_offset;
3076 rtm->num_clauses = header->num_clauses;
3077 for (i = 0; i < header->num_clauses; i++) {
3078 MonoExceptionClause *c = rtm->clauses + i;
3079 int end_off = c->try_offset + c->try_len;
3080 c->try_offset = td.in_offsets [c->try_offset];
3081 c->try_len = td.in_offsets [end_off] - c->try_offset;
3082 end_off = c->handler_offset + c->handler_len;
3083 c->handler_offset = td.in_offsets [c->handler_offset];
3084 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3086 rtm->vt_stack_size = td.max_vt_sp;
3087 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3088 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3089 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3090 g_free (td.in_offsets);
3091 g_free (td.forward_refs);
3092 for (i = 0; i < header->code_size; ++i)
3093 g_free (td.stack_state [i]);
3094 g_free (td.stack_state);
3095 g_free (td.stack_height);
3096 g_free (td.vt_stack_size);
3097 g_free (td.data_items);
3098 g_hash_table_destroy (td.data_hash);
3101 static mono_mutex_t calc_section;
3104 mono_interp_transform_init (void)
3106 mono_os_mutex_init_recursive(&calc_section);
3110 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3112 int i, align, size, offset;
3113 MonoMethod *method = runtime_method->method;
3114 MonoImage *image = method->klass->image;
3115 MonoMethodHeader *header = mono_method_get_header (method);
3116 MonoMethodSignature *signature = mono_method_signature (method);
3117 register const unsigned char *ip, *end;
3118 const MonoOpcode *opcode;
3121 MonoDomain *domain = mono_domain_get ();
3122 unsigned char *is_bb_start;
3124 MonoVTable *method_class_vt;
3126 MonoGenericContext *generic_context = NULL;
3128 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3129 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3130 if (!method_class_vt->initialized) {
3132 MonoInvocation *last_env_frame = context->env_frame;
3133 jmp_buf *old_env = context->current_env;
3136 MonoException *failed = context->env_frame->ex;
3137 context->env_frame->ex = NULL;
3138 context->env_frame = last_env_frame;
3139 context->current_env = old_env;
3142 context->env_frame = context->current_frame;
3143 context->current_env = &env;
3144 mono_runtime_class_init (method_class_vt);
3145 context->env_frame = last_env_frame;
3146 context->current_env = old_env;
3149 mono_profiler_method_jit (method); /* sort of... */
3151 if (mono_method_signature (method)->is_inflated)
3152 generic_context = mono_method_get_context (method);
3154 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3155 if (generic_container)
3156 generic_context = &generic_container->context;
3159 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3160 MonoMethod *nm = NULL;
3161 mono_os_mutex_lock(&calc_section);
3162 if (runtime_method->transformed) {
3163 mono_os_mutex_unlock(&calc_section);
3164 g_error ("FIXME: no jit info?");
3165 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3169 /* assumes all internal calls with an array this are built in... */
3170 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3171 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3172 signature = mono_method_signature (nm);
3174 const char *name = method->name;
3175 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3176 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3177 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3179 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3180 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3181 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3182 nm = mono_marshal_get_delegate_invoke (method, NULL);
3183 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3184 nm = mono_marshal_get_delegate_begin_invoke (method);
3185 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3186 nm = mono_marshal_get_delegate_end_invoke (method);
3190 runtime_method->code = g_malloc(sizeof(short));
3191 runtime_method->code[0] = MINT_CALLRUN;
3195 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3196 runtime_method->alloca_size = runtime_method->stack_size;
3197 runtime_method->transformed = TRUE;
3198 mono_os_mutex_unlock(&calc_section);
3199 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3203 header = mono_method_get_header (nm);
3204 mono_os_mutex_unlock(&calc_section);
3206 g_assert ((signature->param_count + signature->hasthis) < 1000);
3207 g_assert (header->max_stack < 10000);
3208 /* intern the strings in the method. */
3210 end = ip + header->code_size;
3212 is_bb_start = g_malloc0(header->code_size);
3213 is_bb_start [0] = 1;
3220 else if (in == 0xf0) {
3222 in = *ip + MONO_CEE_MONO_ICALL;
3224 opcode = &mono_opcodes [in];
3225 switch (opcode->argument) {
3226 case MonoInlineNone:
3229 case MonoInlineString:
3230 if (method->wrapper_type == MONO_WRAPPER_NONE)
3231 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3234 case MonoInlineType:
3235 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3236 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3237 mono_class_init (class);
3238 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3240 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3241 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3242 mono_class_vtable (domain, class);
3247 case MonoInlineMethod:
3248 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3249 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3251 g_free (is_bb_start);
3252 g_error ("FIXME: where to get method and class string?");
3254 // return mono_get_exception_missing_method ();
3256 mono_class_init (m->klass);
3257 if (!mono_class_is_interface (m->klass))
3258 mono_class_vtable (domain, m->klass);
3262 case MonoInlineField:
3266 case MonoShortInlineR:
3269 case MonoInlineBrTarget:
3270 offset = read32 (ip + 1);
3272 backwards = offset < 0;
3273 offset += ip - header->code;
3274 g_assert (offset >= 0 && offset < header->code_size);
3275 is_bb_start [offset] |= backwards ? 2 : 1;
3277 case MonoShortInlineBrTarget:
3278 offset = ((gint8 *)ip) [1];
3280 backwards = offset < 0;
3281 offset += ip - header->code;
3282 g_assert (offset >= 0 && offset < header->code_size);
3283 is_bb_start [offset] |= backwards ? 2 : 1;
3288 case MonoShortInlineVar:
3289 case MonoShortInlineI:
3292 case MonoInlineSwitch: {
3294 const unsigned char *next_ip;
3298 next_ip = ip + 4 * n;
3299 for (i = 0; i < n; i++) {
3300 offset = read32 (ip);
3301 backwards = offset < 0;
3302 offset += next_ip - header->code;
3303 g_assert (offset >= 0 && offset < header->code_size);
3304 is_bb_start [offset] |= backwards ? 2 : 1;
3314 g_assert_not_reached ();
3317 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3319 /* the rest needs to be locked so it is only done once */
3320 mono_os_mutex_lock(&calc_section);
3321 if (runtime_method->transformed) {
3322 mono_os_mutex_unlock(&calc_section);
3323 g_free (is_bb_start);
3324 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3328 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3329 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3330 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3332 for (i = 0; i < header->num_locals; ++i) {
3333 size = mono_type_size (header->locals [i], &align);
3334 offset += align - 1;
3335 offset &= ~(align - 1);
3336 runtime_method->local_offsets [i] = offset;
3339 offset = (offset + 7) & ~7;
3340 runtime_method->locals_size = offset;
3341 g_assert (runtime_method->locals_size < 65536);
3343 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3345 if (signature->hasthis) {
3346 g_assert (!signature->pinvoke);
3347 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3348 offset += align - 1;
3349 offset &= ~(align - 1);
3350 runtime_method->arg_offsets [0] = offset;
3354 for (i = 0; i < signature->param_count; ++i) {
3355 if (signature->pinvoke) {
3357 size = mono_type_native_stack_size (signature->params [i], &dummy);
3361 size = mono_type_stack_size (signature->params [i], &align);
3362 offset += align - 1;
3363 offset &= ~(align - 1);
3364 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3367 offset = (offset + 7) & ~7;
3368 runtime_method->args_size = offset;
3369 g_assert (runtime_method->args_size < 10000);
3371 generate (method, runtime_method, is_bb_start, generic_context);
3373 g_free (is_bb_start);
3375 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3376 runtime_method->transformed = TRUE;
3377 mono_os_mutex_unlock(&calc_section);