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);
664 target_method = NULL;
666 if (method->wrapper_type == MONO_WRAPPER_NONE)
667 target_method = mono_get_method_full (image, token, NULL, generic_context);
669 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
670 csignature = mono_method_signature (target_method);
671 if (target_method->klass == mono_defaults.string_class) {
672 if (target_method->name [0] == 'g') {
673 if (strcmp (target_method->name, "get_Chars") == 0)
675 else if (strcmp (target_method->name, "get_Length") == 0)
678 } else if (target_method->klass == mono_defaults.array_class) {
679 if (strcmp (target_method->name, "get_Rank") == 0)
680 op = MINT_ARRAY_RANK;
681 else if (strcmp (target_method->name, "get_Length") == 0)
686 csignature = mono_method_signature (target_method);
689 if (constrained_class) {
690 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
691 /* Use the corresponding method from the base type to avoid boxing */
692 MonoType *base_type = mono_class_enum_basetype (constrained_class);
693 g_assert (base_type);
694 constrained_class = mono_class_from_mono_type (base_type);
695 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
696 g_assert (target_method);
700 if (constrained_class) {
701 mono_class_setup_vtable (constrained_class);
703 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);
705 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
707 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
709 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
710 mono_class_setup_vtable (target_method->klass);
712 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)) {
713 ADD_CODE (td, MINT_BOX);
714 ADD_CODE (td, get_data_item_index (td, constrained_class));
715 ADD_CODE (td, csignature->param_count);
716 } else if (!constrained_class->valuetype) {
717 /* managed pointer on the stack, we need to deref that puppy */
718 ADD_CODE (td, MINT_LDIND_I);
719 ADD_CODE (td, csignature->param_count);
721 g_assert (target_method->klass->valuetype);
727 mono_class_init (target_method->klass);
729 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
730 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
731 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
732 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
733 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
734 MonoMethodHeader *mheader = mono_method_get_header (target_method);
736 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
738 if (mono_interp_traceopt)
739 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
740 for (i = csignature->param_count - 1; i >= 0; --i)
741 store_arg (td, i + csignature->hasthis);
743 if (csignature->hasthis) {
744 g_error ("STTHIS removal");
745 // ADD_CODE(td, MINT_STTHIS);
748 ADD_CODE(td, MINT_BR_S);
749 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
750 ADD_CODE(td, offset);
751 if (!is_bb_start [td->ip + 5 - td->il_code])
752 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
756 /* mheader might not exist if this is a delegate invoc, etc */
757 if (mheader && *mheader->code == CEE_RET && called_inited) {
758 if (mono_interp_traceopt)
759 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
760 for (i = 0; i < csignature->param_count; i++)
761 ADD_CODE(td, MINT_POP); /*FIX: vt */
762 if (csignature->hasthis) {
764 ADD_CODE(td, MINT_CKNULL);
765 ADD_CODE(td, MINT_POP);
767 td->sp -= csignature->param_count + csignature->hasthis;
773 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
774 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
775 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
776 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
777 target_method = mono_marshal_get_synchronized_wrapper (target_method);
779 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
780 td->sp -= csignature->param_count + !!csignature->hasthis;
781 for (i = 0; i < csignature->param_count; ++i) {
782 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
784 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
785 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
786 size = mono_class_native_size (klass, NULL);
788 size = mono_class_value_size (klass, NULL);
789 size = (size + 7) & ~7;
790 vt_stack_used += size;
794 /* need to handle typedbyref ... */
795 if (csignature->ret->type != MONO_TYPE_VOID) {
796 int mt = mint_type(csignature->ret);
797 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
798 if (mt == MINT_TYPE_VT) {
799 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
800 vt_res_size = mono_class_native_size (klass, NULL);
802 vt_res_size = mono_class_value_size (klass, NULL);
803 PUSH_VT(td, vt_res_size);
805 PUSH_TYPE(td, stack_type[mt], klass);
811 #if SIZEOF_VOID_P == 8
812 if (op == MINT_LDLEN)
813 ADD_CODE(td, MINT_CONV_I4_I8);
817 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
819 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
821 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
824 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
826 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
827 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
831 if (vt_stack_used != 0 || vt_res_size != 0) {
832 ADD_CODE(td, MINT_VTRESULT);
833 ADD_CODE(td, vt_res_size);
834 WRITE32(td, &vt_stack_used);
835 td->vt_sp -= vt_stack_used;
840 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start)
842 MonoMethodHeader *header = mono_method_get_header (method);
843 MonoMethodSignature *signature = mono_method_signature (method);
844 MonoImage *image = method->klass->image;
845 MonoDomain *domain = mono_domain_get ();
846 MonoGenericContext *generic_context = NULL;
847 MonoClass *constrained_class = NULL;
853 MonoClassField *field;
854 const unsigned char *end;
855 int new_in_start_offset;
856 int body_start_offset;
860 int generating_code = 1;
862 if (mono_method_signature (method)->is_inflated)
863 generic_context = &((MonoMethodInflated *) method)->context;
865 memset(&td, 0, sizeof(td));
868 td.is_bb_start = is_bb_start;
869 td.il_code = header->code;
870 td.code_size = header->code_size;
872 td.max_code_size = td.code_size;
873 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
874 td.new_code_end = td.new_code + td.max_code_size;
875 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
876 td.forward_refs = g_malloc(header->code_size * sizeof(int));
877 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
878 td.stack_height = g_malloc(header->code_size * sizeof(int));
879 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
881 td.max_data_items = 0;
882 td.data_items = NULL;
883 td.data_hash = g_hash_table_new (NULL, NULL);
884 rtm->data_items = td.data_items;
885 for (i = 0; i < header->code_size; i++) {
886 td.forward_refs [i] = -1;
887 td.stack_height [i] = -1;
889 td.new_ip = td.new_code;
890 td.last_new_ip = NULL;
892 td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
894 td.max_stack_height = 0;
896 for (i = 0; i < header->num_clauses; i++) {
897 MonoExceptionClause *c = header->clauses + i;
898 td.stack_height [c->handler_offset] = 0;
899 td.vt_stack_size [c->handler_offset] = 0;
900 td.is_bb_start [c->handler_offset] = 1;
901 if (c->flags == MONO_EXCEPTION_CLAUSE_NONE) {
902 td.stack_height [c->handler_offset] = 1;
903 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
904 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
905 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
909 td.ip = header->code;
910 end = td.ip + header->code_size;
912 if (mono_interp_traceopt) {
913 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
914 char *name = mono_method_full_name (method, TRUE);
915 g_print ("Method %s, original code:\n", name);
916 g_print ("%s\n", tmp);
921 if (signature->hasthis)
922 store_inarg (&td, 0);
923 for (i = 0; i < signature->param_count; i++)
924 store_inarg (&td, i + !!signature->hasthis);
926 body_start_offset = td.new_ip - td.new_code;
928 for (i = 0; i < header->num_locals; i++) {
929 int mt = mint_type(header->locals [i]);
930 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O) {
931 ADD_CODE(&td, MINT_INITLOCALS);
936 while (td.ip < end) {
939 g_assert (td.sp >= td.stack);
940 g_assert (td.vt_sp < 0x10000000);
941 in_offset = td.ip - header->code;
942 td.in_offsets [in_offset] = td.new_ip - td.new_code;
943 new_in_start_offset = td.new_ip - td.new_code;
945 while (td.forward_refs [in_offset] >= 0) {
946 int j = td.forward_refs [in_offset];
948 td.forward_refs [in_offset] = td.forward_refs [j];
949 if (td.in_offsets [j] < 0) {
950 int old_switch_offset = -td.in_offsets [j];
951 int new_switch_offset = td.in_offsets [old_switch_offset];
952 int switch_case = (j - old_switch_offset - 5) / 4;
953 int n_cases = read32 (header->code + old_switch_offset + 1);
954 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
955 slot = new_switch_offset + 3 + 2 * switch_case;
956 td.new_code [slot] = * (unsigned short *)(&offset);
957 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
959 int op = td.new_code [td.in_offsets [j]];
960 if (mono_interp_opargtype [op] == MintOpShortBranch) {
961 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
962 g_assert (offset <= 32767);
963 slot = td.in_offsets [j] + 1;
964 td.new_code [slot] = offset;
966 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
967 slot = td.in_offsets [j] + 1;
968 td.new_code [slot] = * (unsigned short *)(&offset);
969 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
973 if (td.stack_height [in_offset] >= 0) {
974 g_assert (is_bb_start [in_offset]);
975 if (td.stack_height [in_offset] > 0)
976 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
977 td.sp = td.stack + td.stack_height [in_offset];
978 td.vt_sp = td.vt_stack_size [in_offset];
980 if (is_bb_start [in_offset]) {
983 if (!generating_code) {
984 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
988 if (mono_interp_traceopt > 1) {
989 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
991 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
992 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
993 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
994 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
995 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
996 (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) : "",
997 td.vt_sp, td.max_vt_sp);
1005 SIMPLE_OP(td, MINT_BREAK);
1011 load_arg (&td, *td.ip - CEE_LDARG_0);
1018 load_local (&td, *td.ip - CEE_LDLOC_0);
1025 store_local (&td, *td.ip - CEE_STLOC_0);
1029 load_arg (&td, ((guint8 *)td.ip)[1]);
1032 case CEE_LDARGA_S: {
1033 /* NOTE: n includes this */
1034 int n = ((guint8 *)td.ip)[1];
1035 if (n == 0 && signature->hasthis) {
1036 g_error ("LDTHISA: NOPE");
1037 ADD_CODE(&td, MINT_LDTHISA);
1040 ADD_CODE(&td, MINT_LDARGA);
1041 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1043 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1048 store_arg (&td, ((guint8 *)td.ip)[1]);
1052 load_local (&td, ((guint8 *)td.ip)[1]);
1056 ADD_CODE(&td, MINT_LDLOCA_S);
1057 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1058 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1062 store_local (&td, ((guint8 *)td.ip)[1]);
1066 SIMPLE_OP(td, MINT_LDNULL);
1067 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1070 SIMPLE_OP(td, MINT_LDC_I4_M1);
1071 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1074 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1075 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1076 SIMPLE_OP(td, MINT_CEQ0_I4);
1079 SIMPLE_OP(td, MINT_LDC_I4_0);
1080 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1084 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1085 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1086 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1089 SIMPLE_OP(td, MINT_LDC_I4_1);
1090 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1100 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1101 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1104 ADD_CODE(&td, MINT_LDC_I4_S);
1105 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1107 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1110 i32 = read32 (td.ip + 1);
1111 ADD_CODE(&td, MINT_LDC_I4);
1114 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1117 gint64 val = read64 (td.ip + 1);
1118 ADD_CODE(&td, MINT_LDC_I8);
1121 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1126 readr4 (td.ip + 1, &val);
1127 ADD_CODE(&td, MINT_LDC_R4);
1130 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1135 readr8 (td.ip + 1, &val);
1136 ADD_CODE(&td, MINT_LDC_R8);
1139 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1143 int type = td.sp [-1].type;
1144 MonoClass *klass = td.sp [-1].klass;
1145 if (td.sp [-1].type == STACK_TYPE_VT) {
1146 gint32 size = mono_class_value_size (klass, NULL);
1148 ADD_CODE(&td, MINT_DUP_VT);
1149 WRITE32(&td, &size);
1152 SIMPLE_OP(td, MINT_DUP);
1153 PUSH_TYPE(&td, type, klass);
1157 CHECK_STACK(&td, 1);
1158 SIMPLE_OP(td, MINT_POP);
1159 if (td.sp [-1].type == STACK_TYPE_VT) {
1160 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1161 size = (size + 7) & ~7;
1162 ADD_CODE(&td, MINT_VTRESULT);
1164 WRITE32(&td, &size);
1171 if (td.sp > td.stack)
1172 g_warning ("CEE_JMP: stack must be empty");
1173 token = read32 (td.ip + 1);
1174 m = mono_get_method_full (image, token, NULL, generic_context);
1175 ADD_CODE (&td, MINT_JMP);
1176 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1177 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1181 case CEE_CALLVIRT: /* Fall through */
1182 case CEE_CALLI: /* Fall through */
1184 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
1185 constrained_class = NULL;
1190 if (signature->ret->type != MONO_TYPE_VOID) {
1192 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1193 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1194 vt_size = mono_class_value_size (klass, NULL);
1195 vt_size = (vt_size + 7) & ~7;
1198 if (td.sp > td.stack)
1199 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1200 if (td.vt_sp != vt_size)
1201 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1203 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1205 ADD_CODE(&td, MINT_RET_VT);
1206 WRITE32(&td, &vt_size);
1209 generating_code = 0;
1213 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1215 generating_code = 0;
1218 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1220 generating_code = 0;
1223 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1227 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1231 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1235 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1239 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1243 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1247 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1251 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1255 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1259 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1263 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1267 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1271 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1275 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1279 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1283 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1287 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1291 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1295 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1299 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1303 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1307 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1311 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1315 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1320 const unsigned char *next_ip;
1321 const unsigned char *base_ip = td.ip;
1322 unsigned short *next_new_ip;
1325 ADD_CODE(&td, MINT_SWITCH);
1326 ADD_CODE(&td, * (unsigned short *)(&n));
1327 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1329 next_ip = td.ip + n * 4;
1330 next_new_ip = td.new_ip + n * 2;
1331 for (i = 0; i < n; i++) {
1332 offset = read32 (td.ip);
1333 target = next_ip - td.il_code + offset;
1335 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1337 int prev = td.forward_refs [target];
1338 td.forward_refs [td.ip - td.il_code] = prev;
1339 td.forward_refs [target] = td.ip - td.il_code;
1340 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1342 ADD_CODE(&td, * (unsigned short *)(&target));
1343 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1350 CHECK_STACK (&td, 1);
1351 SIMPLE_OP (td, MINT_LDIND_I1);
1352 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1355 CHECK_STACK (&td, 1);
1356 SIMPLE_OP (td, MINT_LDIND_U1);
1357 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1360 CHECK_STACK (&td, 1);
1361 SIMPLE_OP (td, MINT_LDIND_I2);
1362 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1365 CHECK_STACK (&td, 1);
1366 SIMPLE_OP (td, MINT_LDIND_U2);
1367 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1370 CHECK_STACK (&td, 1);
1371 SIMPLE_OP (td, MINT_LDIND_I4);
1372 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1375 CHECK_STACK (&td, 1);
1376 SIMPLE_OP (td, MINT_LDIND_U4);
1377 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1380 CHECK_STACK (&td, 1);
1381 SIMPLE_OP (td, MINT_LDIND_I8);
1382 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1385 CHECK_STACK (&td, 1);
1386 SIMPLE_OP (td, MINT_LDIND_I);
1388 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1391 CHECK_STACK (&td, 1);
1392 SIMPLE_OP (td, MINT_LDIND_R4);
1393 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1396 CHECK_STACK (&td, 1);
1397 SIMPLE_OP (td, MINT_LDIND_R8);
1398 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1401 CHECK_STACK (&td, 1);
1402 SIMPLE_OP (td, MINT_LDIND_REF);
1403 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1406 CHECK_STACK (&td, 2);
1407 SIMPLE_OP (td, MINT_STIND_REF);
1411 CHECK_STACK (&td, 2);
1412 SIMPLE_OP (td, MINT_STIND_I1);
1416 CHECK_STACK (&td, 2);
1417 SIMPLE_OP (td, MINT_STIND_I2);
1421 CHECK_STACK (&td, 2);
1422 SIMPLE_OP (td, MINT_STIND_I4);
1426 CHECK_STACK (&td, 2);
1427 SIMPLE_OP (td, MINT_STIND_I);
1431 CHECK_STACK (&td, 2);
1432 SIMPLE_OP (td, MINT_STIND_I8);
1436 CHECK_STACK (&td, 2);
1437 SIMPLE_OP (td, MINT_STIND_R4);
1441 CHECK_STACK (&td, 2);
1442 SIMPLE_OP (td, MINT_STIND_R8);
1446 binary_arith_op(&td, MINT_ADD_I4);
1450 binary_arith_op(&td, MINT_SUB_I4);
1454 binary_arith_op(&td, MINT_MUL_I4);
1458 binary_arith_op(&td, MINT_DIV_I4);
1462 binary_arith_op(&td, MINT_DIV_UN_I4);
1466 binary_int_op (&td, MINT_REM_I4);
1470 binary_int_op (&td, MINT_REM_UN_I4);
1474 binary_int_op (&td, MINT_AND_I4);
1478 binary_int_op (&td, MINT_OR_I4);
1482 binary_int_op (&td, MINT_XOR_I4);
1486 shift_op (&td, MINT_SHL_I4);
1490 shift_op (&td, MINT_SHR_I4);
1494 shift_op (&td, MINT_SHR_UN_I4);
1498 unary_arith_op (&td, MINT_NEG_I4);
1502 unary_arith_op (&td, MINT_NOT_I4);
1506 CHECK_STACK (&td, 1);
1507 switch (td.sp [-1].type) {
1509 ADD_CODE(&td, MINT_CONV_U1_R8);
1512 ADD_CODE(&td, MINT_CONV_U1_I4);
1515 ADD_CODE(&td, MINT_CONV_U1_I8);
1518 g_assert_not_reached ();
1521 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1524 CHECK_STACK (&td, 1);
1525 switch (td.sp [-1].type) {
1527 ADD_CODE(&td, MINT_CONV_I1_R8);
1530 ADD_CODE(&td, MINT_CONV_I1_I4);
1533 ADD_CODE(&td, MINT_CONV_I1_I8);
1536 g_assert_not_reached ();
1539 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1542 CHECK_STACK (&td, 1);
1543 switch (td.sp [-1].type) {
1545 ADD_CODE(&td, MINT_CONV_U2_R8);
1548 ADD_CODE(&td, MINT_CONV_U2_I4);
1551 ADD_CODE(&td, MINT_CONV_U2_I8);
1554 g_assert_not_reached ();
1557 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1560 CHECK_STACK (&td, 1);
1561 switch (td.sp [-1].type) {
1563 ADD_CODE(&td, MINT_CONV_I2_R8);
1566 ADD_CODE(&td, MINT_CONV_I2_I4);
1569 ADD_CODE(&td, MINT_CONV_I2_I8);
1572 g_assert_not_reached ();
1575 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1578 CHECK_STACK (&td, 1);
1579 switch (td.sp [-1].type) {
1581 #if SIZEOF_VOID_P == 4
1582 ADD_CODE(&td, MINT_CONV_U4_R8);
1584 ADD_CODE(&td, MINT_CONV_U8_R8);
1588 #if SIZEOF_VOID_P == 8
1589 ADD_CODE(&td, MINT_CONV_U8_I4);
1593 #if SIZEOF_VOID_P == 4
1594 ADD_CODE(&td, MINT_CONV_U4_I8);
1600 g_assert_not_reached ();
1603 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1606 CHECK_STACK (&td, 1);
1607 switch (td.sp [-1].type) {
1609 #if SIZEOF_VOID_P == 8
1610 ADD_CODE(&td, MINT_CONV_I8_R8);
1612 ADD_CODE(&td, MINT_CONV_I4_R8);
1616 #if SIZEOF_VOID_P == 8
1617 ADD_CODE(&td, MINT_CONV_I8_I4);
1625 #if SIZEOF_VOID_P == 4
1626 ADD_CODE(&td, MINT_CONV_I4_I8);
1630 g_assert_not_reached ();
1633 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1636 CHECK_STACK (&td, 1);
1637 switch (td.sp [-1].type) {
1639 ADD_CODE(&td, MINT_CONV_U4_R8);
1644 ADD_CODE(&td, MINT_CONV_U4_I8);
1647 #if SIZEOF_VOID_P == 8
1648 ADD_CODE(&td, MINT_CONV_U4_I8);
1652 g_assert_not_reached ();
1655 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1658 CHECK_STACK (&td, 1);
1659 switch (td.sp [-1].type) {
1661 ADD_CODE(&td, MINT_CONV_I4_R8);
1666 ADD_CODE(&td, MINT_CONV_I4_I8);
1669 #if SIZEOF_VOID_P == 8
1670 ADD_CODE(&td, MINT_CONV_I4_I8);
1674 g_assert_not_reached ();
1677 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1680 CHECK_STACK (&td, 1);
1681 switch (td.sp [-1].type) {
1683 ADD_CODE(&td, MINT_CONV_I8_R8);
1686 ADD_CODE(&td, MINT_CONV_I8_I4);
1691 #if SIZEOF_VOID_P == 4
1692 ADD_CODE(&td, MINT_CONV_I8_I4);
1696 g_assert_not_reached ();
1699 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1702 CHECK_STACK (&td, 1);
1703 switch (td.sp [-1].type) {
1705 ADD_CODE(&td, MINT_CONV_R4_R8);
1708 ADD_CODE(&td, MINT_CONV_R4_I8);
1711 ADD_CODE(&td, MINT_CONV_R4_I4);
1714 g_assert_not_reached ();
1717 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1720 CHECK_STACK (&td, 1);
1721 switch (td.sp [-1].type) {
1723 ADD_CODE(&td, MINT_CONV_R8_I4);
1726 ADD_CODE(&td, MINT_CONV_R8_I8);
1731 g_assert_not_reached ();
1734 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1737 CHECK_STACK (&td, 1);
1738 switch (td.sp [-1].type) {
1740 ADD_CODE(&td, MINT_CONV_U8_I4);
1745 ADD_CODE(&td, MINT_CONV_U8_R8);
1748 #if SIZEOF_VOID_P == 4
1749 ADD_CODE(&td, MINT_CONV_U8_I4);
1753 g_assert_not_reached ();
1756 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1762 vtklass = mono_class_get_full (image, read32 (ip), generic_context);
1765 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
1771 CHECK_STACK (&td, 1);
1773 token = read32 (td.ip + 1);
1775 if (method->wrapper_type != MONO_WRAPPER_NONE)
1776 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1778 klass = mono_class_get_full (image, token, generic_context);
1780 ADD_CODE(&td, MINT_LDOBJ);
1781 ADD_CODE(&td, get_data_item_index(&td, klass));
1782 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1783 size = mono_class_value_size (klass, NULL);
1787 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1792 token = mono_metadata_token_index (read32 (td.ip + 1));
1794 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1795 s = mono_string_new_wrapper(
1796 mono_method_get_wrapper_data (method, token));
1799 s = mono_ldstr (domain, image, token);
1800 ADD_CODE(&td, MINT_LDSTR);
1801 ADD_CODE(&td, get_data_item_index (&td, s));
1802 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1807 MonoMethodSignature *csignature;
1808 guint32 vt_stack_used = 0;
1809 guint32 vt_res_size = 0;
1812 token = read32 (td.ip);
1815 if (method->wrapper_type != MONO_WRAPPER_NONE)
1816 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1818 m = mono_get_method_full (image, token, NULL, generic_context);
1820 csignature = mono_method_signature (m);
1822 td.sp -= csignature->param_count;
1823 ADD_CODE(&td, MINT_NEWOBJ);
1824 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1825 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1827 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1828 vt_res_size = mono_class_value_size (klass, NULL);
1829 PUSH_VT (&td, vt_res_size);
1831 for (i = 0; i < csignature->param_count; ++i) {
1832 int mt = mint_type(csignature->params [i]);
1833 if (mt == MINT_TYPE_VT) {
1834 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1835 gint32 size = mono_class_value_size (k, NULL);
1836 size = (size + 7) & ~7;
1837 vt_stack_used += size;
1840 if (vt_stack_used != 0 || vt_res_size != 0) {
1841 ADD_CODE(&td, MINT_VTRESULT);
1842 ADD_CODE(&td, vt_res_size);
1843 WRITE32(&td, &vt_stack_used);
1844 td.vt_sp -= vt_stack_used;
1846 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1850 CHECK_STACK (&td, 1);
1851 token = read32 (td.ip + 1);
1852 klass = mono_class_get_full (image, token, generic_context);
1853 ADD_CODE(&td, MINT_CASTCLASS);
1854 ADD_CODE(&td, get_data_item_index (&td, klass));
1855 td.sp [-1].klass = klass;
1859 CHECK_STACK (&td, 1);
1860 token = read32 (td.ip + 1);
1861 klass = mono_class_get_full (image, token, generic_context);
1862 ADD_CODE(&td, MINT_ISINST);
1863 ADD_CODE(&td, get_data_item_index (&td, klass));
1867 switch (td.sp [-1].type) {
1871 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1874 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1877 g_assert_not_reached ();
1879 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1883 CHECK_STACK (&td, 1);
1884 token = read32 (td.ip + 1);
1886 if (method->wrapper_type != MONO_WRAPPER_NONE)
1887 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1889 klass = mono_class_get_full (image, token, generic_context);
1891 if (mono_class_is_nullable (klass)) {
1892 g_error ("cee_unbox: implement Nullable");
1895 ADD_CODE(&td, MINT_UNBOX);
1896 ADD_CODE(&td, get_data_item_index (&td, klass));
1897 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1901 CHECK_STACK (&td, 1);
1902 token = read32 (td.ip + 1);
1904 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1905 klass = mono_class_get_full (image, token, generic_context);
1907 if (mini_type_is_reference (&klass->byval_arg)) {
1908 g_error ("unbox_any: generic class is reference type");
1909 } else if (mono_class_is_nullable (klass)) {
1910 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1911 /* td.ip is incremented by interp_transform_call */
1912 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
1914 int mt = mint_type (&klass->byval_arg);
1915 ADD_CODE (&td, MINT_UNBOX);
1916 ADD_CODE (&td, get_data_item_index (&td, klass));
1917 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1918 if (mt == MINT_TYPE_VT) {
1919 int size = mono_class_value_size (klass, NULL);
1920 PUSH_VT (&td, size);
1927 CHECK_STACK (&td, 1);
1928 SIMPLE_OP (td, MINT_THROW);
1930 generating_code = 0;
1933 CHECK_STACK (&td, 1);
1934 token = read32 (td.ip + 1);
1935 field = mono_field_from_token (image, token, &klass, generic_context);
1936 mono_class_init (klass);
1937 mt = mint_type(field->type);
1938 ADD_CODE(&td, MINT_LDFLDA);
1939 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1941 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1944 CHECK_STACK (&td, 1);
1945 token = read32 (td.ip + 1);
1946 field = mono_field_from_token (image, token, &klass, generic_context);
1947 mono_class_init (klass);
1949 MonoClass *field_klass = mono_class_from_mono_type (field->type);
1950 mt = mint_type (&field_klass->byval_arg);
1951 if (klass->marshalbyref) {
1952 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
1953 ADD_CODE(&td, get_data_item_index (&td, field));
1955 ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
1956 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1958 if (mt == MINT_TYPE_VT) {
1959 int size = mono_class_value_size (field_klass, NULL);
1961 WRITE32(&td, &size);
1963 if (td.sp [-1].type == STACK_TYPE_VT) {
1964 int size = mono_class_value_size (klass, NULL);
1965 size = (size + 7) & ~7;
1967 ADD_CODE (&td, MINT_VTRESULT);
1969 WRITE32 (&td, &size);
1972 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
1976 CHECK_STACK (&td, 2);
1977 token = read32 (td.ip + 1);
1978 field = mono_field_from_token (image, token, &klass, generic_context);
1979 mono_class_init (klass);
1980 mt = mint_type(field->type);
1981 if (klass->marshalbyref) {
1982 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
1983 ADD_CODE(&td, get_data_item_index (&td, field));
1985 ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
1986 ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
1988 if (mt == MINT_TYPE_VT) {
1989 MonoClass *klass = mono_class_from_mono_type (field->type);
1990 int size = mono_class_value_size (klass, NULL);
1992 WRITE32(&td, &size);
1998 token = read32 (td.ip + 1);
1999 field = mono_field_from_token (image, token, &klass, generic_context);
2000 ADD_CODE(&td, MINT_LDSFLDA);
2001 ADD_CODE(&td, get_data_item_index (&td, field));
2003 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2006 token = read32 (td.ip + 1);
2007 field = mono_field_from_token (image, token, &klass, generic_context);
2008 mt = mint_type(field->type);
2009 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2010 ADD_CODE(&td, get_data_item_index (&td, field));
2012 if (mt == MINT_TYPE_VT) {
2013 MonoClass *klass = mono_class_from_mono_type (field->type);
2014 int size = mono_class_value_size (klass, NULL);
2016 WRITE32(&td, &size);
2017 klass = field->type->data.klass;
2019 if (mt == MINT_TYPE_O)
2020 klass = mono_class_from_mono_type (field->type);
2023 PUSH_TYPE(&td, stack_type [mt], klass);
2026 CHECK_STACK (&td, 1);
2027 token = read32 (td.ip + 1);
2028 field = mono_field_from_token (image, token, &klass, generic_context);
2029 mt = mint_type(field->type);
2030 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2031 ADD_CODE(&td, get_data_item_index (&td, field));
2032 if (mt == MINT_TYPE_VT) {
2033 MonoClass *klass = mono_class_from_mono_type (field->type);
2034 int size = mono_class_value_size (klass, NULL);
2036 WRITE32 (&td, &size);
2043 token = read32 (td.ip + 1);
2045 if (method->wrapper_type != MONO_WRAPPER_NONE)
2046 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2048 klass = mono_class_get_full (image, token, generic_context);
2050 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2051 ADD_CODE(&td, get_data_item_index (&td, klass));
2052 if (td.sp [-1].type == STACK_TYPE_VT) {
2053 size = mono_class_value_size (klass, NULL);
2054 size = (size + 7) & ~7;
2061 case CEE_CONV_OVF_I_UN:
2062 CHECK_STACK (&td, 1);
2063 switch (td.sp [-1].type) {
2065 #if SIZEOF_VOID_P == 8
2066 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2068 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2075 #if SIZEOF_VOID_P == 8
2076 ADD_CODE(&td, MINT_CONV_I8_U4);
2080 g_assert_not_reached ();
2083 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2086 case CEE_CONV_OVF_I8_UN:
2087 CHECK_STACK (&td, 1);
2088 switch (td.sp [-1].type) {
2090 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2095 ADD_CODE(&td, MINT_CONV_I8_U4);
2098 g_assert_not_reached ();
2101 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2106 CHECK_STACK (&td, 1);
2107 token = read32 (td.ip + 1);
2108 if (method->wrapper_type != MONO_WRAPPER_NONE)
2109 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2111 klass = mono_class_get_full (image, token, generic_context);
2113 if (mono_class_is_nullable (klass)) {
2114 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2115 /* td.ip is incremented by interp_transform_call */
2116 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
2117 } else if (!klass->valuetype) {
2118 /* already boxed, do nothing. */
2121 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2122 size = mono_class_value_size (klass, NULL);
2123 size = (size + 7) & ~7;
2126 ADD_CODE(&td, MINT_BOX);
2127 ADD_CODE(&td, get_data_item_index (&td, klass));
2129 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2136 CHECK_STACK (&td, 1);
2137 token = read32 (td.ip + 1);
2139 if (method->wrapper_type != MONO_WRAPPER_NONE)
2140 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2142 klass = mono_class_get_full (image, token, generic_context);
2144 ADD_CODE(&td, MINT_NEWARR);
2145 ADD_CODE(&td, get_data_item_index (&td, klass));
2146 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2150 CHECK_STACK (&td, 1);
2151 SIMPLE_OP (td, MINT_LDLEN);
2152 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2155 CHECK_STACK (&td, 2);
2157 token = read32 (td.ip + 1);
2159 if (method->wrapper_type != MONO_WRAPPER_NONE)
2160 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2162 klass = mono_class_get_full (image, token, generic_context);
2164 ADD_CODE(&td, MINT_LDELEMA);
2165 ADD_CODE(&td, get_data_item_index (&td, klass));
2168 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2171 CHECK_STACK (&td, 2);
2173 SIMPLE_OP (td, MINT_LDELEM_I1);
2175 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2178 CHECK_STACK (&td, 2);
2180 SIMPLE_OP (td, MINT_LDELEM_U1);
2182 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2185 CHECK_STACK (&td, 2);
2187 SIMPLE_OP (td, MINT_LDELEM_I2);
2189 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2192 CHECK_STACK (&td, 2);
2194 SIMPLE_OP (td, MINT_LDELEM_U2);
2196 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2199 CHECK_STACK (&td, 2);
2201 SIMPLE_OP (td, MINT_LDELEM_I4);
2203 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2206 CHECK_STACK (&td, 2);
2208 SIMPLE_OP (td, MINT_LDELEM_U4);
2210 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2213 CHECK_STACK (&td, 2);
2215 SIMPLE_OP (td, MINT_LDELEM_I8);
2217 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2220 CHECK_STACK (&td, 2);
2222 SIMPLE_OP (td, MINT_LDELEM_I);
2224 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2227 CHECK_STACK (&td, 2);
2229 SIMPLE_OP (td, MINT_LDELEM_R4);
2231 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2234 CHECK_STACK (&td, 2);
2236 SIMPLE_OP (td, MINT_LDELEM_R8);
2238 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2240 case CEE_LDELEM_REF:
2241 CHECK_STACK (&td, 2);
2243 SIMPLE_OP (td, MINT_LDELEM_REF);
2245 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2248 CHECK_STACK (&td, 2);
2249 token = read32 (td.ip + 1);
2250 klass = mono_class_get_full (image, token, generic_context);
2251 switch (mint_type (&klass->byval_arg)) {
2254 SIMPLE_OP (td, MINT_LDELEM_I4);
2256 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2258 case MINT_TYPE_VT: {
2259 int size = mono_class_value_size (klass, NULL);
2261 SIMPLE_OP (td, MINT_LDELEM_VT);
2262 ADD_CODE (&td, get_data_item_index (&td, klass));
2263 WRITE32 (&td, &size);
2265 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2266 PUSH_VT (&td, size);
2270 GString *res = g_string_new ("");
2271 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2272 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2273 g_string_free (res, TRUE);
2281 CHECK_STACK (&td, 3);
2283 SIMPLE_OP (td, MINT_STELEM_I);
2287 CHECK_STACK (&td, 3);
2289 SIMPLE_OP (td, MINT_STELEM_I1);
2293 CHECK_STACK (&td, 3);
2295 SIMPLE_OP (td, MINT_STELEM_I2);
2299 CHECK_STACK (&td, 3);
2301 SIMPLE_OP (td, MINT_STELEM_I4);
2305 CHECK_STACK (&td, 3);
2307 SIMPLE_OP (td, MINT_STELEM_I8);
2311 CHECK_STACK (&td, 3);
2313 SIMPLE_OP (td, MINT_STELEM_R4);
2317 CHECK_STACK (&td, 3);
2319 SIMPLE_OP (td, MINT_STELEM_R8);
2322 case CEE_STELEM_REF:
2323 CHECK_STACK (&td, 3);
2325 SIMPLE_OP (td, MINT_STELEM_REF);
2329 CHECK_STACK (&td, 3);
2331 token = read32 (td.ip + 1);
2332 klass = mono_class_get_full (image, token, generic_context);
2333 switch (mint_type (&klass->byval_arg)) {
2335 SIMPLE_OP (td, MINT_STELEM_I4);
2338 SIMPLE_OP (td, MINT_STELEM_REF);
2340 case MINT_TYPE_VT: {
2341 int size = mono_class_value_size (klass, NULL);
2342 SIMPLE_OP (td, MINT_STELEM_VT);
2343 ADD_CODE (&td, get_data_item_index (&td, klass));
2344 WRITE32 (&td, &size);
2349 GString *res = g_string_new ("");
2350 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2351 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2352 g_string_free (res, TRUE);
2361 case CEE_CONV_OVF_U1:
2363 case CEE_CONV_OVF_I8:
2365 #if SIZEOF_VOID_P == 8
2366 case CEE_CONV_OVF_U:
2368 case CEE_REFANYVAL: ves_abort(); break;
2371 CHECK_STACK (&td, 1);
2372 SIMPLE_OP (td, MINT_CKFINITE);
2374 case CEE_CONV_OVF_I1:
2375 case CEE_CONV_OVF_I1_UN:
2376 CHECK_STACK (&td, 1);
2377 switch (td.sp [-1].type) {
2379 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2382 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2385 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2388 g_assert_not_reached ();
2391 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2393 case CEE_CONV_OVF_U1:
2394 case CEE_CONV_OVF_U1_UN:
2395 CHECK_STACK (&td, 1);
2396 switch (td.sp [-1].type) {
2398 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2401 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2404 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2407 g_assert_not_reached ();
2410 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2412 case CEE_CONV_OVF_I2:
2413 case CEE_CONV_OVF_I2_UN:
2414 CHECK_STACK (&td, 1);
2415 switch (td.sp [-1].type) {
2417 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2420 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2423 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2426 g_assert_not_reached ();
2429 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2431 case CEE_CONV_OVF_U2_UN:
2432 case CEE_CONV_OVF_U2:
2433 CHECK_STACK (&td, 1);
2434 switch (td.sp [-1].type) {
2436 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2439 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2442 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2445 g_assert_not_reached ();
2448 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2450 #if SIZEOF_VOID_P == 4
2451 case CEE_CONV_OVF_I:
2453 case CEE_CONV_OVF_I4:
2454 case CEE_CONV_OVF_I4_UN:
2455 CHECK_STACK (&td, 1);
2456 switch (td.sp [-1].type) {
2458 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2461 if (*td.ip == CEE_CONV_OVF_I4_UN)
2462 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2465 ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
2468 g_assert_not_reached ();
2471 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2473 #if SIZEOF_VOID_P == 4
2474 case CEE_CONV_OVF_U:
2476 case CEE_CONV_OVF_U4:
2477 case CEE_CONV_OVF_U4_UN:
2478 CHECK_STACK (&td, 1);
2479 switch (td.sp [-1].type) {
2481 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2484 if (*td.ip != CEE_CONV_OVF_U4_UN)
2485 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2488 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2491 g_assert_not_reached ();
2494 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2496 #if SIZEOF_VOID_P == 8
2497 case CEE_CONV_OVF_I:
2499 case CEE_CONV_OVF_I8:
2500 CHECK_STACK (&td, 1);
2501 switch (td.sp [-1].type) {
2503 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2506 ADD_CODE(&td, MINT_CONV_I8_I4);
2511 g_assert_not_reached ();
2514 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2516 #if SIZEOF_VOID_P == 8
2517 case CEE_CONV_OVF_U:
2519 case CEE_CONV_OVF_U8:
2520 CHECK_STACK (&td, 1);
2521 switch (td.sp [-1].type) {
2523 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2526 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2531 g_assert_not_reached ();
2534 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2539 token = read32 (td.ip + 1);
2540 handle = mono_ldtoken (image, token, &klass, generic_context);
2541 mt = mint_type(&klass->byval_arg);
2542 g_assert (mt == MINT_TYPE_VT);
2543 size = mono_class_value_size (klass, NULL);
2544 g_assert (size == sizeof(gpointer));
2545 PUSH_VT(&td, sizeof(gpointer));
2546 ADD_CODE(&td, MINT_LDTOKEN);
2547 ADD_CODE(&td, get_data_item_index (&td, handle));
2548 PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
2553 binary_arith_op(&td, MINT_ADD_OVF_I4);
2556 case CEE_ADD_OVF_UN:
2557 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2561 binary_arith_op(&td, MINT_MUL_OVF_I4);
2564 case CEE_MUL_OVF_UN:
2565 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2569 binary_arith_op(&td, MINT_SUB_OVF_I4);
2572 case CEE_SUB_OVF_UN:
2573 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2576 case CEE_ENDFINALLY:
2577 SIMPLE_OP (td, MINT_ENDFINALLY);
2578 generating_code = 0;
2582 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2584 generating_code = 0;
2588 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2590 generating_code = 0;
2595 case CEE_MONO_CALLI_EXTRA_ARG:
2596 /* Same as CEE_CALLI, llvm specific */
2597 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
2599 case CEE_MONO_ICALL: {
2602 MonoJitICallInfo *info;
2604 token = read32 (td.ip + 1);
2606 func = mono_method_get_wrapper_data (method, token);
2607 info = mono_find_jit_icall_by_addr (func);
2610 CHECK_STACK (&td, info->sig->param_count);
2611 switch (info->sig->param_count) {
2613 if (MONO_TYPE_IS_VOID (info->sig->ret))
2614 ADD_CODE (&td,MINT_ICALL_V_V);
2616 ADD_CODE (&td, MINT_ICALL_V_P);
2619 if (MONO_TYPE_IS_VOID (info->sig->ret))
2620 ADD_CODE (&td,MINT_ICALL_P_V);
2622 ADD_CODE (&td,MINT_ICALL_P_P);
2625 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2626 if (info->sig->params [1]->type == MONO_TYPE_I4)
2627 ADD_CODE (&td,MINT_ICALL_PI_V);
2629 ADD_CODE (&td,MINT_ICALL_PP_V);
2631 if (info->sig->params [1]->type == MONO_TYPE_I4)
2632 ADD_CODE (&td,MINT_ICALL_PI_P);
2634 ADD_CODE (&td,MINT_ICALL_PP_P);
2638 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2639 if (info->sig->params [2]->type == MONO_TYPE_I4)
2640 ADD_CODE (&td,MINT_ICALL_PPI_V);
2642 ADD_CODE (&td,MINT_ICALL_PPP_V);
2645 g_assert_not_reached ();
2648 if (func == mono_ftnptr_to_delegate) {
2649 g_error ("TODO: ?");
2650 func = mono_interp_ftnptr_to_delegate;
2652 ADD_CODE(&td, get_data_item_index (&td, func));
2653 td.sp -= info->sig->param_count;
2655 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2657 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2661 case CEE_MONO_VTADDR: {
2663 CHECK_STACK (&td, 1);
2664 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2665 size = mono_class_native_size(td.sp [-1].klass, NULL);
2667 size = mono_class_value_size(td.sp [-1].klass, NULL);
2668 size = (size + 7) & ~7;
2669 ADD_CODE(&td, MINT_VTRESULT);
2671 WRITE32(&td, &size);
2674 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2677 case CEE_MONO_LDPTR:
2678 case CEE_MONO_CLASSCONST:
2679 token = read32 (td.ip + 1);
2681 ADD_CODE(&td, MINT_MONO_LDPTR);
2682 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2683 td.sp [0].type = STACK_TYPE_I;
2686 case CEE_MONO_OBJADDR:
2687 CHECK_STACK (&td, 1);
2689 td.sp[-1].type = STACK_TYPE_MP;
2692 case CEE_MONO_NEWOBJ:
2693 token = read32 (td.ip + 1);
2695 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2696 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2697 td.sp [0].type = STACK_TYPE_O;
2700 case CEE_MONO_RETOBJ:
2701 CHECK_STACK (&td, 1);
2702 token = read32 (td.ip + 1);
2704 ADD_CODE(&td, MINT_MONO_RETOBJ);
2707 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2709 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2711 if (td.sp > td.stack)
2712 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2714 case CEE_MONO_LDNATIVEOBJ:
2715 token = read32 (td.ip + 1);
2717 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2718 g_assert(klass->valuetype);
2719 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2721 case CEE_MONO_SAVE_LMF:
2722 case CEE_MONO_RESTORE_LMF:
2723 case CEE_MONO_NOT_TAKEN:
2726 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2727 ADD_CODE (&td, MINT_MONO_LDPTR);
2728 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2729 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2733 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2743 case CEE_PREFIXREF: ves_abort(); break;
2746 * Note: Exceptions thrown when executing a prefixed opcode need
2747 * to take into account the number of prefix bytes (usually the
2748 * throw point is just (ip - n_prefix_bytes).
2754 case CEE_ARGLIST: ves_abort(); break;
2757 CHECK_STACK(&td, 2);
2758 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2759 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2761 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2763 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2767 CHECK_STACK(&td, 2);
2768 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2769 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2771 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2773 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2777 CHECK_STACK(&td, 2);
2778 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2779 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2781 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2783 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2787 CHECK_STACK(&td, 2);
2788 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2789 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2791 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2793 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2797 CHECK_STACK(&td, 2);
2798 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2799 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2801 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2803 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2806 case CEE_LDVIRTFTN: /* fallthrough */
2809 if (*td.ip == CEE_LDVIRTFTN) {
2810 CHECK_STACK (&td, 1);
2813 token = read32 (td.ip + 1);
2814 if (method->wrapper_type != MONO_WRAPPER_NONE)
2815 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2817 m = mono_get_method_full (image, token, NULL, generic_context);
2819 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
2820 m = mono_marshal_get_synchronized_wrapper (m);
2822 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
2823 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
2824 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2826 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
2830 load_arg (&td, read16 (td.ip + 1));
2834 int n = read16 (td.ip + 1);
2835 if (n == 0 && signature->hasthis) {
2836 g_error ("LDTHISA: NOPE");
2837 ADD_CODE(&td, MINT_LDTHISA);
2840 ADD_CODE(&td, MINT_LDARGA);
2841 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
2843 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2848 store_arg (&td, read16 (td.ip + 1));
2852 load_local (&td, read16 (td.ip + 1));
2856 ADD_CODE(&td, MINT_LDLOCA_S);
2857 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
2858 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2862 store_local (&td, read16 (td.ip + 1));
2866 CHECK_STACK (&td, 1);
2867 #if SIZEOF_VOID_P == 8
2868 if (td.sp [-1].type == STACK_TYPE_I8)
2869 ADD_CODE(&td, MINT_CONV_I4_I8);
2871 ADD_CODE(&td, MINT_LOCALLOC);
2872 if (td.sp != td.stack + 1)
2873 g_warning("CEE_LOCALLOC: stack not empty");
2875 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2878 case CEE_UNUSED57: ves_abort(); break;
2879 case CEE_ENDFILTER: ves_abort(); break;
2881 case CEE_UNALIGNED_:
2883 /* FIX: should do something? */;
2887 /* FIX: should do something? */;
2891 /* FIX: should do something? */;
2894 CHECK_STACK(&td, 1);
2895 token = read32 (td.ip + 1);
2896 klass = mono_class_get_full (image, token, generic_context);
2897 ADD_CODE(&td, MINT_INITOBJ);
2898 i32 = mono_class_value_size (klass, NULL);
2904 CHECK_STACK(&td, 3);
2905 /* FIX? convert length to I8? */
2906 ADD_CODE(&td, MINT_CPBLK);
2910 case CEE_CONSTRAINED_:
2911 token = read32 (td.ip + 1);
2912 constrained_class = mono_class_get_full (image, token, generic_context);
2913 mono_class_init (constrained_class);
2917 CHECK_STACK(&td, 3);
2918 ADD_CODE(&td, MINT_INITBLK);
2923 /* FIXME: implement */
2928 SIMPLE_OP (td, MINT_RETHROW);
2929 generating_code = 0;
2933 token = read32 (td.ip + 1);
2935 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
2937 MonoType *type = mono_type_create_from_typespec (image, token);
2938 size = mono_type_size (type, &align);
2941 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
2942 mono_class_init (szclass);
2944 if (!szclass->valuetype)
2945 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
2947 size = mono_class_value_size (szclass, &align);
2949 ADD_CODE(&td, MINT_LDC_I4);
2950 WRITE32(&td, &size);
2951 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
2955 case CEE_REFANYTYPE: ves_abort(); break;
2958 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);
2962 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
2965 if (td.new_ip - td.new_code != new_in_start_offset)
2966 td.last_new_ip = td.new_code + new_in_start_offset;
2967 else if (td.is_bb_start [td.in_start - td.il_code])
2968 td.is_bb_start [td.ip - td.il_code] = 1;
2970 td.last_ip = td.in_start;
2973 if (mono_interp_traceopt) {
2974 const guint16 *p = td.new_code;
2975 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
2976 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
2977 while (p < td.new_ip) {
2978 p = mono_interp_dis_mintop(td.new_code, p);
2983 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
2984 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
2985 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
2986 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
2987 g_free (td.new_code);
2988 rtm->new_body_start = rtm->code + body_start_offset;
2989 rtm->num_clauses = header->num_clauses;
2990 for (i = 0; i < header->num_clauses; i++) {
2991 MonoExceptionClause *c = rtm->clauses + i;
2992 int end_off = c->try_offset + c->try_len;
2993 c->try_offset = td.in_offsets [c->try_offset];
2994 c->try_len = td.in_offsets [end_off] - c->try_offset;
2995 end_off = c->handler_offset + c->handler_len;
2996 c->handler_offset = td.in_offsets [c->handler_offset];
2997 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
2999 rtm->vt_stack_size = td.max_vt_sp;
3000 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3001 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3002 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3003 g_free (td.in_offsets);
3004 g_free (td.forward_refs);
3005 for (i = 0; i < header->code_size; ++i)
3006 g_free (td.stack_state [i]);
3007 g_free (td.stack_state);
3008 g_free (td.stack_height);
3009 g_free (td.vt_stack_size);
3010 g_free (td.data_items);
3011 g_hash_table_destroy (td.data_hash);
3014 static mono_mutex_t calc_section;
3017 mono_interp_transform_init (void)
3019 mono_os_mutex_init_recursive(&calc_section);
3023 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3025 int i, align, size, offset;
3026 MonoMethod *method = runtime_method->method;
3027 MonoImage *image = method->klass->image;
3028 MonoMethodHeader *header = mono_method_get_header (method);
3029 MonoMethodSignature *signature = mono_method_signature (method);
3030 register const unsigned char *ip, *end;
3031 const MonoOpcode *opcode;
3034 MonoDomain *domain = mono_domain_get ();
3035 unsigned char *is_bb_start;
3037 MonoVTable *method_class_vt;
3039 MonoGenericContext *generic_context = NULL;
3041 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3042 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3043 if (!method_class_vt->initialized) {
3045 MonoInvocation *last_env_frame = context->env_frame;
3046 jmp_buf *old_env = context->current_env;
3049 MonoException *failed = context->env_frame->ex;
3050 context->env_frame->ex = NULL;
3051 context->env_frame = last_env_frame;
3052 context->current_env = old_env;
3055 context->env_frame = context->current_frame;
3056 context->current_env = &env;
3057 mono_runtime_class_init (method_class_vt);
3058 context->env_frame = last_env_frame;
3059 context->current_env = old_env;
3062 mono_profiler_method_jit (method); /* sort of... */
3064 if (mono_method_signature (method)->is_inflated)
3065 generic_context = &((MonoMethodInflated *) method)->context;
3067 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3068 MonoMethod *nm = NULL;
3069 mono_os_mutex_lock(&calc_section);
3070 if (runtime_method->transformed) {
3071 mono_os_mutex_unlock(&calc_section);
3072 g_error ("FIXME: no jit info?");
3073 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3077 /* assumes all internal calls with an array this are built in... */
3078 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3079 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3080 signature = mono_method_signature (nm);
3082 const char *name = method->name;
3083 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3084 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3085 nm = mono_marshal_get_delegate_invoke (method, NULL);
3086 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3087 nm = mono_marshal_get_delegate_begin_invoke (method);
3088 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3089 nm = mono_marshal_get_delegate_end_invoke (method);
3093 runtime_method->code = g_malloc(sizeof(short));
3094 runtime_method->code[0] = MINT_CALLRUN;
3098 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3099 runtime_method->alloca_size = runtime_method->stack_size;
3100 runtime_method->transformed = TRUE;
3101 mono_os_mutex_unlock(&calc_section);
3102 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3106 header = mono_method_get_header (nm);
3107 mono_os_mutex_unlock(&calc_section);
3109 g_assert ((signature->param_count + signature->hasthis) < 1000);
3110 g_assert (header->max_stack < 10000);
3111 /* intern the strings in the method. */
3113 end = ip + header->code_size;
3115 is_bb_start = g_malloc0(header->code_size);
3116 is_bb_start [0] = 1;
3123 else if (in == 0xf0) {
3125 in = *ip + MONO_CEE_MONO_ICALL;
3127 opcode = &mono_opcodes [in];
3128 switch (opcode->argument) {
3129 case MonoInlineNone:
3132 case MonoInlineString:
3133 if (method->wrapper_type == MONO_WRAPPER_NONE)
3134 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3137 case MonoInlineType:
3138 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3139 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3140 mono_class_init (class);
3141 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3143 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3144 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3145 mono_class_vtable (domain, class);
3150 case MonoInlineMethod:
3151 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3152 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3154 g_free (is_bb_start);
3155 g_error ("FIXME: where to get method and class string?");
3157 // return mono_get_exception_missing_method ();
3159 mono_class_init (m->klass);
3160 if (!mono_class_is_interface (m->klass))
3161 mono_class_vtable (domain, m->klass);
3165 case MonoInlineField:
3169 case MonoShortInlineR:
3172 case MonoInlineBrTarget:
3173 offset = read32 (ip + 1);
3175 backwards = offset < 0;
3176 offset += ip - header->code;
3177 g_assert (offset >= 0 && offset < header->code_size);
3178 is_bb_start [offset] |= backwards ? 2 : 1;
3180 case MonoShortInlineBrTarget:
3181 offset = ((gint8 *)ip) [1];
3183 backwards = offset < 0;
3184 offset += ip - header->code;
3185 g_assert (offset >= 0 && offset < header->code_size);
3186 is_bb_start [offset] |= backwards ? 2 : 1;
3191 case MonoShortInlineVar:
3192 case MonoShortInlineI:
3195 case MonoInlineSwitch: {
3197 const unsigned char *next_ip;
3201 next_ip = ip + 4 * n;
3202 for (i = 0; i < n; i++) {
3203 offset = read32 (ip);
3204 backwards = offset < 0;
3205 offset += next_ip - header->code;
3206 g_assert (offset >= 0 && offset < header->code_size);
3207 is_bb_start [offset] |= backwards ? 2 : 1;
3217 g_assert_not_reached ();
3220 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3222 /* the rest needs to be locked so it is only done once */
3223 mono_os_mutex_lock(&calc_section);
3224 if (runtime_method->transformed) {
3225 mono_os_mutex_unlock(&calc_section);
3226 g_free (is_bb_start);
3227 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3231 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3232 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3233 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3235 for (i = 0; i < header->num_locals; ++i) {
3236 size = mono_type_size (header->locals [i], &align);
3237 offset += align - 1;
3238 offset &= ~(align - 1);
3239 runtime_method->local_offsets [i] = offset;
3242 offset = (offset + 7) & ~7;
3243 runtime_method->locals_size = offset;
3244 g_assert (runtime_method->locals_size < 65536);
3246 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3248 if (signature->hasthis) {
3249 g_assert (!signature->pinvoke);
3250 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3251 offset += align - 1;
3252 offset &= ~(align - 1);
3253 runtime_method->arg_offsets [0] = offset;
3257 for (i = 0; i < signature->param_count; ++i) {
3258 if (signature->pinvoke) {
3260 size = mono_type_native_stack_size (signature->params [i], &dummy);
3264 size = mono_type_stack_size (signature->params [i], &align);
3265 offset += align - 1;
3266 offset &= ~(align - 1);
3267 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3270 offset = (offset + 7) & ~7;
3271 runtime_method->args_size = offset;
3272 g_assert (runtime_method->args_size < 10000);
3274 generate(method, runtime_method, is_bb_start);
3276 g_free (is_bb_start);
3278 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3279 runtime_method->transformed = TRUE;
3280 mono_os_mutex_unlock(&calc_section);