3 * transform CIL into different opcodes for more
4 * efficient interpretation
6 * Written by Bernie Solomon (bernard@ugsolutions.com)
11 #include <mono/metadata/appdomain.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/mono-endian.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/seq-points-data.h>
20 #include <mono/mini/mini.h>
23 #include "interp-internals.h"
26 // TODO: export from marshal.c
27 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
42 SeqPoint *last_seq_point;
44 // This will hold a list of last sequence points of incoming basic blocks
45 SeqPoint **pred_seq_points;
46 guint num_pred_seq_points;
57 /* In the interpreter IR */
66 MonoMethodHeader *header;
68 const unsigned char *il_code;
69 const unsigned char *ip;
70 const unsigned char *last_ip;
71 const unsigned char *in_start;
74 StackInfo **stack_state;
77 unsigned char *is_bb_start;
78 unsigned short *new_code;
79 unsigned short *new_code_end;
80 unsigned short *new_ip;
81 unsigned short *last_new_ip;
82 unsigned int max_code_size;
85 unsigned int max_stack_height;
87 unsigned int max_vt_sp;
91 GHashTable *data_hash;
93 gboolean gen_sdb_seq_points;
94 GPtrArray *seq_points;
95 InterpBasicBlock **offset_to_bb;
96 InterpBasicBlock *entry_bb;
100 gboolean verbose_level;
103 #define MINT_TYPE_I1 0
104 #define MINT_TYPE_U1 1
105 #define MINT_TYPE_I2 2
106 #define MINT_TYPE_U2 3
107 #define MINT_TYPE_I4 4
108 #define MINT_TYPE_I8 5
109 #define MINT_TYPE_R4 6
110 #define MINT_TYPE_R8 7
111 #define MINT_TYPE_O 8
112 #define MINT_TYPE_P 9
113 #define MINT_TYPE_VT 10
115 #define STACK_TYPE_I4 0
116 #define STACK_TYPE_I8 1
117 #define STACK_TYPE_R8 2
118 #define STACK_TYPE_O 3
119 #define STACK_TYPE_VT 4
120 #define STACK_TYPE_MP 5
121 #define STACK_TYPE_F 6
123 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
125 #if SIZEOF_VOID_P == 8
126 #define STACK_TYPE_I STACK_TYPE_I8
128 #define STACK_TYPE_I STACK_TYPE_I4
131 static int stack_type [] = {
132 STACK_TYPE_I4, /*I1*/
133 STACK_TYPE_I4, /*U1*/
134 STACK_TYPE_I4, /*I2*/
135 STACK_TYPE_I4, /*U2*/
136 STACK_TYPE_I4, /*I4*/
137 STACK_TYPE_I8, /*I8*/
138 STACK_TYPE_R8, /*R4*/
139 STACK_TYPE_R8, /*R8*/
145 #if SIZEOF_VOID_P == 8
146 #define MINT_NEG_P MINT_NEG_I8
147 #define MINT_NOT_P MINT_NOT_I8
149 #define MINT_NEG_FP MINT_NEG_R8
151 #define MINT_ADD_P MINT_ADD_I8
152 #define MINT_SUB_P MINT_SUB_I8
153 #define MINT_MUL_P MINT_MUL_I8
154 #define MINT_DIV_P MINT_DIV_I8
155 #define MINT_DIV_UN_P MINT_DIV_UN_I8
156 #define MINT_REM_P MINT_REM_I8
157 #define MINT_REM_UN_P MINT_REM_UN_I8
158 #define MINT_AND_P MINT_AND_I8
159 #define MINT_OR_P MINT_OR_I8
160 #define MINT_XOR_P MINT_XOR_I8
161 #define MINT_SHL_P MINT_SHL_I8
162 #define MINT_SHR_P MINT_SHR_I8
163 #define MINT_SHR_UN_P MINT_SHR_UN_I8
165 #define MINT_CEQ_P MINT_CEQ_I8
166 #define MINT_CNE_P MINT_CNE_I8
167 #define MINT_CLT_P MINT_CLT_I8
168 #define MINT_CLT_UN_P MINT_CLT_UN_I8
169 #define MINT_CGT_P MINT_CGT_I8
170 #define MINT_CGT_UN_P MINT_CGT_UN_I8
171 #define MINT_CLE_P MINT_CLE_I8
172 #define MINT_CLE_UN_P MINT_CLE_UN_I8
173 #define MINT_CGE_P MINT_CGE_I8
174 #define MINT_CGE_UN_P MINT_CGE_UN_I8
176 #define MINT_ADD_FP MINT_ADD_R8
177 #define MINT_SUB_FP MINT_SUB_R8
178 #define MINT_MUL_FP MINT_MUL_R8
179 #define MINT_DIV_FP MINT_DIV_R8
180 #define MINT_REM_FP MINT_REM_R8
182 #define MINT_CNE_FP MINT_CNE_R8
183 #define MINT_CEQ_FP MINT_CEQ_R8
184 #define MINT_CGT_FP MINT_CGT_R8
185 #define MINT_CGE_FP MINT_CGE_R8
186 #define MINT_CLT_FP MINT_CLT_R8
187 #define MINT_CLE_FP MINT_CLE_R8
191 #define MINT_NEG_P MINT_NEG_I4
192 #define MINT_NOT_P MINT_NOT_I4
194 #define MINT_NEG_FP MINT_NEG_R8
196 #define MINT_ADD_P MINT_ADD_I4
197 #define MINT_SUB_P MINT_SUB_I4
198 #define MINT_MUL_P MINT_MUL_I4
199 #define MINT_DIV_P MINT_DIV_I4
200 #define MINT_DIV_UN_P MINT_DIV_UN_I4
201 #define MINT_REM_P MINT_REM_I4
202 #define MINT_REM_UN_P MINT_REM_UN_I4
203 #define MINT_AND_P MINT_AND_I4
204 #define MINT_OR_P MINT_OR_I4
205 #define MINT_XOR_P MINT_XOR_I4
206 #define MINT_SHL_P MINT_SHL_I4
207 #define MINT_SHR_P MINT_SHR_I4
208 #define MINT_SHR_UN_P MINT_SHR_UN_I4
210 #define MINT_CEQ_P MINT_CEQ_I4
211 #define MINT_CNE_P MINT_CNE_I4
212 #define MINT_CLT_P MINT_CLT_I4
213 #define MINT_CLT_UN_P MINT_CLT_UN_I4
214 #define MINT_CGT_P MINT_CGT_I4
215 #define MINT_CGT_UN_P MINT_CGT_UN_I4
216 #define MINT_CLE_P MINT_CLE_I4
217 #define MINT_CLE_UN_P MINT_CLE_UN_I4
218 #define MINT_CGE_P MINT_CGE_I4
219 #define MINT_CGE_UN_P MINT_CGE_UN_I4
221 #define MINT_ADD_FP MINT_ADD_R8
222 #define MINT_SUB_FP MINT_SUB_R8
223 #define MINT_MUL_FP MINT_MUL_R8
224 #define MINT_DIV_FP MINT_DIV_R8
225 #define MINT_REM_FP MINT_REM_R8
227 #define MINT_CNE_FP MINT_CNE_R8
228 #define MINT_CEQ_FP MINT_CEQ_R8
229 #define MINT_CGT_FP MINT_CGT_R8
230 #define MINT_CGE_FP MINT_CGE_R8
231 #define MINT_CLT_FP MINT_CLT_R8
232 #define MINT_CLE_FP MINT_CLE_R8
237 const gchar *op_name;
241 // static const MagicIntrinsic int_binop[] = {
243 static const MagicIntrinsic int_unnop[] = {
244 { "op_UnaryPlus", {MINT_NOP, MINT_NOP, MINT_NOP}},
245 { "op_UnaryNegation", {MINT_NEG_P, MINT_NEG_P, MINT_NEG_FP}},
246 { "op_OnesComplement", {MINT_NOT_P, MINT_NOT_P, MINT_NIY}}
249 static const MagicIntrinsic int_binop[] = {
250 { "op_Addition", {MINT_ADD_P, MINT_ADD_P, MINT_ADD_FP}},
251 { "op_Subtraction", {MINT_SUB_P, MINT_SUB_P, MINT_SUB_FP}},
252 { "op_Multiply", {MINT_MUL_P, MINT_MUL_P, MINT_MUL_FP}},
253 { "op_Division", {MINT_DIV_P, MINT_DIV_UN_P, MINT_DIV_FP}},
254 { "op_Modulus", {MINT_REM_P, MINT_REM_UN_P, MINT_REM_FP}},
255 { "op_BitwiseAnd", {MINT_AND_P, MINT_AND_P, MINT_NIY}},
256 { "op_BitwiseOr", {MINT_OR_P, MINT_OR_P, MINT_NIY}},
257 { "op_ExclusiveOr", {MINT_XOR_P, MINT_XOR_P, MINT_NIY}},
258 { "op_LeftShift", {MINT_SHL_P, MINT_SHL_P, MINT_NIY}},
259 { "op_RightShift", {MINT_SHR_P, MINT_SHR_UN_P, MINT_NIY}},
262 static const MagicIntrinsic int_cmpop[] = {
263 { "op_Inequality", {MINT_CNE_P, MINT_CNE_P, MINT_CNE_FP}},
264 { "op_Equality", {MINT_CEQ_P, MINT_CEQ_P, MINT_CEQ_FP}},
265 { "op_GreaterThan", {MINT_CGT_P, MINT_CGT_UN_P, MINT_CGT_FP}},
266 { "op_GreaterThanOrEqual", {MINT_CGE_P, MINT_CGE_UN_P, MINT_CGE_FP}},
267 { "op_LessThan", {MINT_CLT_P, MINT_CLT_UN_P, MINT_CLT_FP}},
268 { "op_LessThanOrEqual", {MINT_CLE_P, MINT_CLE_UN_P, MINT_CLE_FP}}
272 grow_code (TransformData *td)
274 unsigned int old_ip_offset = td->new_ip - td->new_code;
275 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
276 g_assert (old_ip_offset <= td->max_code_size);
277 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
278 td->new_code_end = td->new_code + td->max_code_size;
279 td->new_ip = td->new_code + old_ip_offset;
280 td->last_new_ip = td->new_code + old_last_ip_offset;
283 #define ENSURE_CODE(td, n) \
285 if ((td)->new_ip + (n) > (td)->new_code_end) \
289 #define ADD_CODE(td, n) \
291 if ((td)->new_ip == (td)->new_code_end) \
293 *(td)->new_ip++ = (n); \
296 #define CHECK_STACK(td, n) \
298 int stack_size = (td)->sp - (td)->stack; \
299 if (stack_size < (n)) \
300 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
301 (td)->method->klass->name, (td)->method->name, \
302 stack_size, n, (td)->ip - (td)->il_code); \
305 #define ENSURE_I4(td, sp_off) \
307 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
308 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
312 handle_branch (TransformData *td, int short_op, int long_op, int offset)
314 int shorten_branch = 0;
315 int target = td->ip + offset - td->il_code;
316 if (target < 0 || target >= td->code_size)
317 g_assert_not_reached ();
318 if (offset > 0 && td->stack_height [target] < 0) {
319 td->stack_height [target] = td->sp - td->stack;
320 if (td->stack_height [target] > 0)
321 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
322 td->vt_stack_size [target] = td->vt_sp;
325 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
326 if (offset >= -32768) {
330 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
333 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
334 if (shorten_branch) {
336 reloc->type = RELOC_SHORT_BRANCH;
339 reloc->type = RELOC_LONG_BRANCH;
341 reloc->offset = td->new_ip - td->new_code;
342 reloc->target = target;
343 g_ptr_array_add (td->relocs, reloc);
345 if (shorten_branch) {
346 ADD_CODE(td, short_op);
347 ADD_CODE(td, offset);
349 ADD_CODE(td, long_op);
350 ADD_CODE(td, * (unsigned short *)(&offset));
351 ADD_CODE(td, * ((unsigned short *)&offset + 1));
356 one_arg_branch(TransformData *td, int mint_op, int offset)
358 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
359 int long_op = mint_op + type - STACK_TYPE_I4;
360 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
363 handle_branch (td, short_op, long_op, offset);
367 two_arg_branch(TransformData *td, int mint_op, int offset)
369 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
370 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
371 int long_op = mint_op + type1 - STACK_TYPE_I4;
372 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
374 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
375 ADD_CODE(td, MINT_CONV_I8_I4);
376 td->in_offsets [td->ip - td->il_code]++;
377 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
378 ADD_CODE(td, MINT_CONV_I8_I4_SP);
379 td->in_offsets [td->ip - td->il_code]++;
380 } else if (type1 != type2) {
381 g_warning("%s.%s: branch type mismatch %d %d",
382 td->method->klass->name, td->method->name,
383 td->sp [-1].type, td->sp [-2].type);
386 handle_branch (td, short_op, long_op, offset);
390 unary_arith_op(TransformData *td, int mint_op)
392 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
398 binary_arith_op(TransformData *td, int mint_op)
400 int type1 = td->sp [-2].type;
401 int type2 = td->sp [-1].type;
403 #if SIZEOF_VOID_P == 8
404 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
405 ADD_CODE(td, MINT_CONV_I8_I4);
406 type2 = STACK_TYPE_I8;
408 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
409 ADD_CODE(td, MINT_CONV_I8_I4_SP);
410 type1 = STACK_TYPE_I8;
411 td->sp [-2].type = STACK_TYPE_I8;
414 if (type1 == STACK_TYPE_MP)
415 type1 = STACK_TYPE_I;
416 if (type2 == STACK_TYPE_MP)
417 type2 = STACK_TYPE_I;
418 if (type1 != type2) {
419 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
420 td->method->klass->name, td->method->name,
421 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
423 op = mint_op + type1 - STACK_TYPE_I4;
430 shift_op(TransformData *td, int mint_op)
432 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
434 if (td->sp [-1].type != STACK_TYPE_I4) {
435 g_warning("%s.%s: shift type mismatch %d",
436 td->method->klass->name, td->method->name,
444 mint_type(MonoType *type_)
446 MonoType *type = mini_native_type_replace_type (type_);
450 switch (type->type) {
454 case MONO_TYPE_BOOLEAN:
466 #if SIZEOF_VOID_P == 4
480 case MONO_TYPE_STRING:
481 case MONO_TYPE_SZARRAY:
482 case MONO_TYPE_CLASS:
483 case MONO_TYPE_OBJECT:
484 case MONO_TYPE_ARRAY:
486 case MONO_TYPE_VALUETYPE:
487 if (type->data.klass->enumtype) {
488 type = mono_class_enum_basetype (type->data.klass);
492 case MONO_TYPE_TYPEDBYREF:
494 case MONO_TYPE_GENERICINST:
495 type = &type->data.generic_class->container_class->byval_arg;
498 g_warning ("got type 0x%02x", type->type);
499 g_assert_not_reached ();
505 can_store (int stack_type, int var_type)
507 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
508 stack_type = STACK_TYPE_I;
509 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
510 var_type = STACK_TYPE_I;
511 return stack_type == var_type;
514 #define SET_SIMPLE_TYPE(s, ty) \
521 #define SET_TYPE(s, ty, k) \
528 #define PUSH_SIMPLE_TYPE(td, ty) \
532 sp_height = (td)->sp - (td)->stack; \
533 if (sp_height > (td)->max_stack_height) \
534 (td)->max_stack_height = sp_height; \
535 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
538 #define PUSH_TYPE(td, ty, k) \
542 sp_height = (td)->sp - (td)->stack; \
543 if (sp_height > (td)->max_stack_height) \
544 (td)->max_stack_height = sp_height; \
545 SET_TYPE((td)->sp - 1, ty, k); \
548 #define PUSH_VT(td, size) \
550 (td)->vt_sp += ((size) + 7) & ~7; \
551 if ((td)->vt_sp > (td)->max_vt_sp) \
552 (td)->max_vt_sp = (td)->vt_sp; \
555 #define POP_VT(td, size) \
557 (td)->vt_sp -= ((size) + 7) & ~7; \
560 #if NO_UNALIGNED_ACCESS
561 #define WRITE32(td, v) \
563 ENSURE_CODE(td, 2); \
564 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
565 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
569 #define WRITE64(td, v) \
571 ENSURE_CODE(td, 4); \
572 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
573 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
574 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
575 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
579 #define WRITE32(td, v) \
581 ENSURE_CODE(td, 2); \
582 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
586 #define WRITE64(td, v) \
588 ENSURE_CODE(td, 4); \
589 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
596 load_arg(TransformData *td, int n)
599 MonoClass *klass = NULL;
602 gboolean hasthis = mono_method_signature (td->method)->hasthis;
603 if (hasthis && n == 0)
604 type = &td->method->klass->byval_arg;
606 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
608 mt = mint_type (type);
609 if (mt == MINT_TYPE_VT) {
611 klass = mono_class_from_mono_type (type);
612 if (mono_method_signature (td->method)->pinvoke)
613 size = mono_class_native_size (klass, NULL);
615 size = mono_class_value_size (klass, NULL);
617 if (hasthis && n == 0) {
619 ADD_CODE (td, MINT_LDARG_P);
620 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
624 ADD_CODE (td, MINT_LDARG_VT);
625 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
629 if (hasthis && n == 0) {
631 ADD_CODE (td, MINT_LDARG_P);
632 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
635 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
636 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
637 if (mt == MINT_TYPE_O)
638 klass = mono_class_from_mono_type (type);
641 PUSH_TYPE(td, stack_type[mt], klass);
645 store_arg(TransformData *td, int n)
651 gboolean hasthis = mono_method_signature (td->method)->hasthis;
652 if (hasthis && n == 0)
653 type = &td->method->klass->byval_arg;
655 type = mono_method_signature (td->method)->params [n - !!hasthis];
657 mt = mint_type (type);
658 if (mt == MINT_TYPE_VT) {
660 MonoClass *klass = mono_class_from_mono_type (type);
661 if (mono_method_signature (td->method)->pinvoke)
662 size = mono_class_native_size (klass, NULL);
664 size = mono_class_value_size (klass, NULL);
665 ADD_CODE(td, MINT_STARG_VT);
666 ADD_CODE(td, td->rtm->arg_offsets [n]);
668 if (td->sp [-1].type == STACK_TYPE_VT)
671 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
672 ADD_CODE(td, td->rtm->arg_offsets [n]);
678 store_inarg(TransformData *td, int n)
681 gboolean hasthis = mono_method_signature (td->method)->hasthis;
682 if (hasthis && n == 0)
683 type = &td->method->klass->byval_arg;
685 type = mono_method_signature (td->method)->params [n - !!hasthis];
687 int mt = mint_type (type);
688 if (hasthis && n == 0) {
689 ADD_CODE (td, MINT_STINARG_P);
693 if (mt == MINT_TYPE_VT) {
694 MonoClass *klass = mono_class_from_mono_type (type);
696 if (mono_method_signature (td->method)->pinvoke)
697 size = mono_class_native_size (klass, NULL);
699 size = mono_class_value_size (klass, NULL);
700 ADD_CODE(td, MINT_STINARG_VT);
704 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
710 load_local(TransformData *td, int n)
712 MonoType *type = td->header->locals [n];
713 int mt = mint_type (type);
714 int offset = td->rtm->local_offsets [n];
715 MonoClass *klass = NULL;
716 if (mt == MINT_TYPE_VT) {
717 klass = mono_class_from_mono_type (type);
718 gint32 size = mono_class_value_size (klass, NULL);
720 ADD_CODE(td, MINT_LDLOC_VT);
721 ADD_CODE(td, offset); /*FIX for large offset */
724 g_assert (mt < MINT_TYPE_VT);
725 if (!td->gen_sdb_seq_points &&
726 mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
727 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
728 td->last_new_ip [0] = MINT_STLOC_NP_I4;
729 } else if (!td->gen_sdb_seq_points &&
730 mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
731 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
732 td->last_new_ip [0] = MINT_STLOC_NP_O;
734 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
735 ADD_CODE(td, offset); /*FIX for large offset */
737 if (mt == MINT_TYPE_O)
738 klass = mono_class_from_mono_type (type);
740 PUSH_TYPE(td, stack_type[mt], klass);
744 store_local(TransformData *td, int n)
746 MonoType *type = td->header->locals [n];
747 int mt = mint_type (type);
748 int offset = td->rtm->local_offsets [n];
750 #if SIZEOF_VOID_P == 8
751 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
752 ADD_CODE(td, MINT_CONV_I8_I4);
753 td->sp [-1].type = STACK_TYPE_I8;
756 if (!can_store(td->sp [-1].type, stack_type [mt])) {
757 g_warning("%s.%s: Store local stack type mismatch %d %d",
758 td->method->klass->name, td->method->name,
759 stack_type [mt], td->sp [-1].type);
761 if (mt == MINT_TYPE_VT) {
762 MonoClass *klass = mono_class_from_mono_type (type);
763 gint32 size = mono_class_value_size (klass, NULL);
764 ADD_CODE(td, MINT_STLOC_VT);
765 ADD_CODE(td, offset); /*FIX for large offset */
767 if (td->sp [-1].type == STACK_TYPE_VT)
770 g_assert (mt < MINT_TYPE_VT);
771 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
772 ADD_CODE(td, offset); /*FIX for large offset */
777 #define SIMPLE_OP(td, op) \
784 get_data_item_index (TransformData *td, void *ptr)
786 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
789 return GPOINTER_TO_UINT (p) - 1;
790 if (td->max_data_items == td->n_data_items) {
791 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
792 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
794 index = td->n_data_items;
795 td->data_items [index] = ptr;
797 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
802 jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
806 if (sig->param_count > 6)
810 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
812 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
814 if (method->is_inflated)
816 if (method->string_ctor)
819 for (l = jit_classes; l; l = l->next) {
820 char *class_name = l->data;
822 if (!strcmp (method->klass->name, class_name))
830 static inline gboolean
831 type_size (MonoType *type)
833 if (type->type == MONO_TYPE_I4 || type->type == MONO_TYPE_U4)
835 else if (type->type == MONO_TYPE_I8 || type->type == MONO_TYPE_U8)
837 else if (type->type == MONO_TYPE_R4 && !type->byref)
839 else if (type->type == MONO_TYPE_R8 && !type->byref)
841 return SIZEOF_VOID_P;
844 static int mono_class_get_magic_index (MonoClass *k)
846 if (mono_class_is_magic_int (k))
847 return !strcmp ("nint", k->name) ? 0 : 1;
849 if (mono_class_is_magic_float (k))
857 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
859 MonoImage *image = method->klass->image;
860 MonoMethodSignature *csignature;
862 int virtual = *td->ip == CEE_CALLVIRT;
863 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
865 guint32 vt_stack_used = 0;
866 guint32 vt_res_size = 0;
871 guint32 token = read32 (td->ip + 1);
873 if (target_method == NULL) {
876 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
878 if (method->wrapper_type != MONO_WRAPPER_NONE)
879 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
881 csignature = mono_metadata_parse_signature (image, token);
883 if (generic_context) {
884 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
885 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
888 target_method = NULL;
890 if (method->wrapper_type == MONO_WRAPPER_NONE)
891 target_method = mono_get_method_full (image, token, NULL, generic_context);
893 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
894 csignature = mono_method_signature (target_method);
896 if (generic_context) {
897 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
898 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
899 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
900 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
904 csignature = mono_method_signature (target_method);
907 if (target_method && target_method->string_ctor) {
908 /* Create the real signature */
909 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (td->mempool, csignature);
910 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
912 csignature = ctor_sig;
917 const char *tm = target_method->name;
918 int type_index = mono_class_get_magic_index (target_method->klass);
920 if (target_method->klass == mono_defaults.string_class) {
922 if (strcmp (tm, "get_Chars") == 0)
924 else if (strcmp (tm, "get_Length") == 0)
927 } else if (type_index >= 0) {
928 MonoClass *magic_class = target_method->klass;
930 const int mt = mint_type (&magic_class->byval_arg);
931 if (!strcmp (".ctor", tm)) {
932 MonoType *arg = csignature->params [0];
933 /* depending on SIZEOF_VOID_P and the type of the value passed to the .ctor we either have to CONV it, or do nothing */
934 int arg_size = type_size (arg);
936 if (arg_size > SIZEOF_VOID_P) { // 8 -> 4
937 switch (type_index) {
939 ADD_CODE (td, MINT_CONV_I8_I4);
942 // ADD_CODE (td, MINT_CONV_R8_R4);
947 if (arg_size < SIZEOF_VOID_P) { // 4 -> 8
948 switch (type_index) {
950 ADD_CODE (td, MINT_CONV_I4_I8);
953 ADD_CODE (td, MINT_CONV_R4_R8);
958 switch (type_index) {
960 #if SIZEOF_VOID_P == 4
961 ADD_CODE (td, MINT_STIND_I4);
963 ADD_CODE (td, MINT_STIND_I8);
967 #if SIZEOF_VOID_P == 4
968 ADD_CODE (td, MINT_STIND_R4);
970 ADD_CODE (td, MINT_STIND_R8);
978 } else if (!strcmp ("op_Implicit", tm ) || !strcmp ("op_Explicit", tm)) {
979 int arg_size = type_size (csignature->params [0]);
980 if (arg_size > SIZEOF_VOID_P) { // 8 -> 4
981 switch (type_index) {
983 ADD_CODE (td, MINT_CONV_I8_I4);
986 // ADD_CODE (td, MINT_CONV_R4_R8);
991 if (arg_size < SIZEOF_VOID_P) { // 4 -> 8
992 switch (type_index) {
994 ADD_CODE (td, MINT_CONV_I4_I8);
997 ADD_CODE (td, MINT_CONV_R4_R8);
1002 SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
1005 } else if (!strcmp (".cctor", tm)) {
1008 } else if (!strcmp ("Parse", tm)) {
1013 for (i = 0; i < sizeof (int_unnop) / sizeof (MagicIntrinsic); ++i) {
1014 if (!strcmp (int_unnop [i].op_name, tm)) {
1015 ADD_CODE (td, int_unnop [i].insn [type_index]);
1016 SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
1022 for (i = 0; i < sizeof (int_binop) / sizeof (MagicIntrinsic); ++i) {
1023 if (!strcmp (int_binop [i].op_name, tm)) {
1024 ADD_CODE (td, int_binop [i].insn [type_index]);
1026 SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
1032 for (i = 0; i < sizeof (int_cmpop) / sizeof (MagicIntrinsic); ++i) {
1033 if (!strcmp (int_cmpop [i].op_name, tm)) {
1034 MonoClass *k = mono_defaults.boolean_class;
1035 ADD_CODE (td, int_cmpop [i].insn [type_index]);
1037 SET_TYPE (td->sp - 1, stack_type [mint_type (&k->byval_arg)], k);
1043 g_error ("TODO: %s", tm);
1044 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
1045 if (!strcmp (tm, "get_Rank")) {
1046 op = MINT_ARRAY_RANK;
1047 } else if (!strcmp (tm, "get_Length")) {
1049 } else if (!strcmp (tm, "Address")) {
1050 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
1052 } else if (target_method->klass->image == mono_defaults.corlib &&
1053 (strcmp (target_method->klass->name_space, "System.Diagnostics") == 0) &&
1054 (strcmp (target_method->klass->name, "Debugger") == 0)) {
1055 if (!strcmp (tm, "Break") && csignature->param_count == 0) {
1056 if (mini_should_insert_breakpoint (method))
1063 if (constrained_class) {
1064 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
1065 /* Use the corresponding method from the base type to avoid boxing */
1066 MonoType *base_type = mono_class_enum_basetype (constrained_class);
1067 g_assert (base_type);
1068 constrained_class = mono_class_from_mono_type (base_type);
1069 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
1070 g_assert (target_method);
1074 if (constrained_class) {
1075 mono_class_setup_vtable (constrained_class);
1077 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);
1079 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
1081 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
1083 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1084 mono_class_setup_vtable (target_method->klass);
1086 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)) {
1087 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
1088 /* managed pointer on the stack, we need to deref that puppy */
1089 ADD_CODE (td, MINT_LDIND_I);
1090 ADD_CODE (td, csignature->param_count);
1092 ADD_CODE (td, MINT_BOX);
1093 ADD_CODE (td, get_data_item_index (td, constrained_class));
1094 ADD_CODE (td, csignature->param_count);
1095 } else if (!constrained_class->valuetype) {
1096 /* managed pointer on the stack, we need to deref that puppy */
1097 ADD_CODE (td, MINT_LDIND_I);
1098 ADD_CODE (td, csignature->param_count);
1100 if (target_method->klass->valuetype) {
1103 /* Interface method */
1106 mono_class_setup_vtable (constrained_class);
1107 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
1109 g_error ("type load error: constrained_class");
1110 slot = mono_method_get_vtable_slot (target_method);
1112 g_error ("type load error: target_method->klass");
1113 target_method = constrained_class->vtable [ioffset + slot];
1115 if (target_method->klass == mono_defaults.enum_class) {
1116 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
1117 /* managed pointer on the stack, we need to deref that puppy */
1118 ADD_CODE (td, MINT_LDIND_I);
1119 ADD_CODE (td, csignature->param_count);
1121 ADD_CODE (td, MINT_BOX);
1122 ADD_CODE (td, get_data_item_index (td, constrained_class));
1123 ADD_CODE (td, csignature->param_count);
1131 mono_class_init (target_method->klass);
1133 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
1134 if (!calli && op == -1 && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
1135 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1136 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0 &&
1137 !(target_method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING)) {
1138 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
1139 MonoMethodHeader *mheader = mono_method_get_header (target_method);
1141 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
1143 if (td->verbose_level)
1144 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
1146 for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
1149 ADD_CODE(td, MINT_BR_S);
1150 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
1151 ADD_CODE(td, offset);
1152 if (!is_bb_start [td->ip + 5 - td->il_code])
1153 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
1157 /* mheader might not exist if this is a delegate invoc, etc */
1158 gboolean has_vt_arg = FALSE;
1159 for (i = 0; i < csignature->param_count; i++)
1160 has_vt_arg |= !mini_type_is_reference (csignature->params [i]);
1162 if (mheader && *mheader->code == CEE_RET && called_inited && !has_vt_arg) {
1163 if (td->verbose_level)
1164 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
1165 for (i = 0; i < csignature->param_count; i++) {
1166 ADD_CODE (td, MINT_POP); /*FIX: vt */
1169 if (csignature->hasthis) {
1171 ADD_CODE(td, MINT_CKNULL);
1172 ADD_CODE (td, MINT_POP);
1175 td->sp -= csignature->param_count + csignature->hasthis;
1181 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
1182 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1183 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
1184 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1185 target_method = mono_marshal_get_synchronized_wrapper (target_method);
1187 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
1188 td->sp -= csignature->param_count + !!csignature->hasthis;
1189 for (i = 0; i < csignature->param_count; ++i) {
1190 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
1192 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
1193 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
1194 size = mono_class_native_size (klass, NULL);
1196 size = mono_class_value_size (klass, NULL);
1197 size = (size + 7) & ~7;
1198 vt_stack_used += size;
1202 /* need to handle typedbyref ... */
1203 if (csignature->ret->type != MONO_TYPE_VOID) {
1204 int mt = mint_type(csignature->ret);
1205 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
1206 if (mt == MINT_TYPE_VT) {
1207 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
1208 vt_res_size = mono_class_native_size (klass, NULL);
1210 vt_res_size = mono_class_value_size (klass, NULL);
1211 PUSH_VT(td, vt_res_size);
1213 PUSH_TYPE(td, stack_type[mt], klass);
1219 #if SIZEOF_VOID_P == 8
1220 if (op == MINT_LDLEN)
1221 ADD_CODE (td, MINT_CONV_I4_I8);
1223 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
1224 ADD_CODE (td, get_data_item_index (td, target_method->klass));
1225 ADD_CODE (td, 1 + target_method->klass->rank);
1227 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
1228 ADD_CODE(td, MINT_JIT_CALL);
1229 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
1230 mono_error_assert_ok (&error);
1233 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
1235 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
1237 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
1240 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
1242 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, &error)));
1243 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1247 if (vt_stack_used != 0 || vt_res_size != 0) {
1248 ADD_CODE(td, MINT_VTRESULT);
1249 ADD_CODE(td, vt_res_size);
1250 WRITE32(td, &vt_stack_used);
1251 td->vt_sp -= vt_stack_used;
1255 static MonoClassField *
1256 interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
1258 MonoClassField *field = NULL;
1259 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1260 field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
1261 *klass = field->parent;
1264 error_init (&error);
1265 field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
1266 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1271 static InterpBasicBlock*
1272 get_bb (TransformData *td, InterpBasicBlock *cbb, unsigned char *ip)
1274 int offset = ip - td->il_code;
1275 InterpBasicBlock *bb = td->offset_to_bb [offset];
1278 bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock));
1280 td->offset_to_bb [offset] = bb;
1282 td->basic_blocks = g_list_append_mempool (td->mempool, td->basic_blocks, bb);
1286 bb->preds = g_slist_prepend_mempool (td->mempool, bb->preds, cbb);
1293 * Compute the set of IL level basic blocks.
1296 get_basic_blocks (TransformData *td)
1298 guint8 *start = (guint8*)td->il_code;
1299 guint8 *end = (guint8*)td->il_code + td->code_size;
1301 unsigned char *target;
1304 const MonoOpcode *opcode;
1305 InterpBasicBlock *cbb;
1307 td->offset_to_bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock*) * (end - start + 1));
1308 td->entry_bb = cbb = get_bb (td, NULL, start);
1311 cli_addr = ip - start;
1312 td->offset_to_bb [cli_addr] = cbb;
1313 i = mono_opcode_value ((const guint8 **)&ip, end);
1314 opcode = &mono_opcodes [i];
1315 switch (opcode->argument) {
1316 case MonoInlineNone:
1319 case MonoInlineString:
1320 case MonoInlineType:
1321 case MonoInlineField:
1322 case MonoInlineMethod:
1325 case MonoShortInlineR:
1332 case MonoShortInlineVar:
1333 case MonoShortInlineI:
1336 case MonoShortInlineBrTarget:
1337 target = start + cli_addr + 2 + (signed char)ip [1];
1338 get_bb (td, cbb, target);
1340 cbb = get_bb (td, cbb, ip);
1342 case MonoInlineBrTarget:
1343 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
1344 get_bb (td, cbb, target);
1346 cbb = get_bb (td, cbb, ip);
1348 case MonoInlineSwitch: {
1349 guint32 n = read32 (ip + 1);
1352 cli_addr += 5 + 4 * n;
1353 target = start + cli_addr;
1354 get_bb (td, cbb, target);
1356 for (j = 0; j < n; ++j) {
1357 target = start + cli_addr + (gint32)read32 (ip);
1358 get_bb (td, cbb, target);
1361 cbb = get_bb (td, cbb, ip);
1369 g_assert_not_reached ();
1375 interp_save_debug_info (InterpMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
1377 MonoDebugMethodJitInfo *dinfo;
1380 if (!mono_debug_enabled ())
1384 * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
1387 dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
1388 dinfo->num_params = rtm->param_count;
1389 dinfo->params = g_new0 (MonoDebugVarInfo, dinfo->num_params);
1390 dinfo->num_locals = header->num_locals;
1391 dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1392 dinfo->code_start = (guint8*)rtm->code;
1393 dinfo->code_size = td->new_ip - td->new_code;
1394 dinfo->epilogue_begin = 0;
1395 dinfo->has_var_info = TRUE;
1396 dinfo->num_line_numbers = line_numbers->len;
1397 dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
1399 for (i = 0; i < dinfo->num_params; i++) {
1400 MonoDebugVarInfo *var = &dinfo->params [i];
1401 var->type = rtm->param_types [i];
1403 for (i = 0; i < dinfo->num_locals; i++) {
1404 MonoDebugVarInfo *var = &dinfo->locals [i];
1405 var->type = header->locals [i];
1408 for (i = 0; i < dinfo->num_line_numbers; i++)
1409 dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
1410 mono_debug_add_method (rtm->method, dinfo, mono_domain_get ());
1412 mono_debug_free_method_jit_info (dinfo);
1415 /* Same as the code in seq-points.c */
1417 insert_pred_seq_point (SeqPoint *last_sp, SeqPoint *sp, GSList **next)
1420 int src_index = last_sp->next_offset;
1421 int dst_index = sp->next_offset;
1423 /* bb->in_bb might contain duplicates */
1424 for (l = next [src_index]; l; l = l->next)
1425 if (GPOINTER_TO_UINT (l->data) == dst_index)
1428 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
1432 recursively_make_pred_seq_points (TransformData *td, InterpBasicBlock *bb)
1434 const gpointer MONO_SEQ_SEEN_LOOP = GINT_TO_POINTER(-1);
1437 GArray *predecessors = g_array_new (FALSE, TRUE, sizeof (gpointer));
1438 GHashTable *seen = g_hash_table_new_full (g_direct_hash, NULL, NULL, NULL);
1440 // Insert/remove sentinel into the memoize table to detect loops containing bb
1441 bb->pred_seq_points = MONO_SEQ_SEEN_LOOP;
1443 for (l = bb->preds; l; l = l->next) {
1444 InterpBasicBlock *in_bb = l->data;
1446 // This bb has the last seq point, append it and continue
1447 if (in_bb->last_seq_point != NULL) {
1448 predecessors = g_array_append_val (predecessors, in_bb->last_seq_point);
1452 // We've looped or handled this before, exit early.
1453 // No last sequence points to find.
1454 if (in_bb->pred_seq_points == MONO_SEQ_SEEN_LOOP)
1457 // Take sequence points from incoming basic blocks
1459 if (in_bb == td->entry_bb)
1462 if (in_bb->pred_seq_points == NULL)
1463 recursively_make_pred_seq_points (td, in_bb);
1465 // Union sequence points with incoming bb's
1466 for (int i=0; i < in_bb->num_pred_seq_points; i++) {
1467 if (!g_hash_table_lookup (seen, in_bb->pred_seq_points [i])) {
1468 g_array_append_val (predecessors, in_bb->pred_seq_points [i]);
1469 g_hash_table_insert (seen, in_bb->pred_seq_points [i], (gpointer)&MONO_SEQ_SEEN_LOOP);
1472 // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
1475 g_hash_table_destroy (seen);
1477 if (predecessors->len != 0) {
1478 bb->pred_seq_points = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint *) * predecessors->len);
1479 bb->num_pred_seq_points = predecessors->len;
1481 for (int newer = 0; newer < bb->num_pred_seq_points; newer++) {
1482 bb->pred_seq_points [newer] = g_array_index (predecessors, gpointer, newer);
1486 g_array_free (predecessors, TRUE);
1490 collect_pred_seq_points (TransformData *td, InterpBasicBlock *bb, SeqPoint *seqp, GSList **next)
1492 // Doesn't have a last sequence point, must find from incoming basic blocks
1493 if (bb->pred_seq_points == NULL && bb != td->entry_bb)
1494 recursively_make_pred_seq_points (td, bb);
1496 for (int i = 0; i < bb->num_pred_seq_points; i++)
1497 insert_pred_seq_point (bb->pred_seq_points [i], seqp, next);
1503 save_seq_points (TransformData *td)
1505 InterpMethod *rtm = td->rtm;
1507 int i, seq_info_size;
1508 MonoSeqPointInfo *info;
1509 MonoDomain *domain = mono_domain_get ();
1510 GSList **next = NULL;
1513 if (!td->gen_sdb_seq_points)
1517 * For each sequence point, compute the list of sequence points immediately
1518 * following it, this is needed to implement 'step over' in the debugger agent.
1519 * Similar to the code in mono_save_seq_point_info ().
1521 for (i = 0; i < td->seq_points->len; ++i) {
1522 SeqPoint *sp = g_ptr_array_index (td->seq_points, i);
1524 /* Store the seq point index here temporarily */
1525 sp->next_offset = i;
1527 next = mono_mempool_alloc0 (td->mempool, sizeof (GList*) * td->seq_points->len);
1528 for (bblist = td->basic_blocks; bblist; bblist = bblist->next) {
1529 InterpBasicBlock *bb = bblist->data;
1531 GSList *bb_seq_points = g_slist_reverse (bb->seq_points);
1532 SeqPoint *last = NULL;
1533 for (GSList *l = bb_seq_points; l; l = l->next) {
1534 SeqPoint *sp = l->data;
1536 if (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET)
1537 /* Used to implement method entry/exit events */
1541 /* Link with the previous seq point in the same bb */
1542 next [last->next_offset] = g_slist_append_mempool (td->mempool, next [last->next_offset], GINT_TO_POINTER (sp->next_offset));
1544 /* Link with the last bb in the previous bblocks */
1545 collect_pred_seq_points (td, bb, sp, next);
1551 /* Serialize the seq points into a byte array */
1552 array = g_byte_array_new ();
1553 SeqPoint zero_seq_point = {0};
1554 SeqPoint* last_seq_point = &zero_seq_point;
1555 for (i = 0; i < td->seq_points->len; ++i) {
1556 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1558 sp->next_offset = 0;
1559 if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next [i], TRUE))
1560 last_seq_point = sp;
1563 if (td->verbose_level) {
1564 g_print ("\nSEQ POINT MAP FOR %s: \n", td->method->name);
1566 for (i = 0; i < td->seq_points->len; ++i) {
1567 SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
1573 g_print ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
1574 for (l = next [i]; l; l = l->next) {
1575 int next_index = GPOINTER_TO_UINT (l->data);
1576 g_print (" IL0x%x", ((SeqPoint*)g_ptr_array_index (td->seq_points, next_index))->il_offset);
1582 info = mono_seq_point_info_new (array->len, TRUE, array->data, TRUE, &seq_info_size);
1583 InterlockedAdd (&mono_jit_stats.allocated_seq_points_size, seq_info_size);
1585 g_byte_array_free (array, TRUE);
1587 mono_domain_lock (domain);
1588 g_hash_table_insert (domain_jit_info (domain)->seq_points, rtm->method, info);
1589 mono_domain_unlock (domain);
1593 emit_seq_point (TransformData *td, int il_offset, InterpBasicBlock *cbb, gboolean nonempty_stack)
1597 seqp = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint));
1598 seqp->il_offset = il_offset;
1599 seqp->native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1601 seqp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
1603 ADD_CODE (td, MINT_SDB_SEQ_POINT);
1604 g_ptr_array_add (td->seq_points, seqp);
1606 cbb->seq_points = g_slist_prepend_mempool (td->mempool, cbb->seq_points, seqp);
1607 cbb->last_seq_point = seqp;
1610 #define BARRIER_IF_VOLATILE(td) \
1613 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER); \
1614 volatile_ = FALSE; \
1619 generate (MonoMethod *method, InterpMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
1621 MonoMethodHeader *header = mono_method_get_header (method);
1622 MonoMethodSignature *signature = mono_method_signature (method);
1623 MonoImage *image = method->klass->image;
1624 MonoDomain *domain = rtm->domain;
1625 MonoClass *constrained_class = NULL;
1627 int offset, mt, i, i32;
1628 gboolean readonly = FALSE;
1629 gboolean volatile_ = FALSE;
1631 MonoClassField *field;
1632 const unsigned char *end;
1633 int new_in_start_offset;
1634 int body_start_offset;
1637 TransformData transform_data;
1639 int generating_code = 1;
1640 GArray *line_numbers;
1641 MonoDebugMethodInfo *minfo;
1642 MonoBitSet *seq_point_locs = NULL;
1643 MonoBitSet *seq_point_set_locs = NULL;
1644 gboolean sym_seq_points = FALSE;
1645 InterpBasicBlock *bb_exit = NULL;
1646 static gboolean verbose_method_inited;
1647 static char* verbose_method_name;
1649 if (!verbose_method_inited) {
1650 verbose_method_name = getenv ("MONO_VERBOSE_METHOD");
1651 verbose_method_inited = TRUE;
1654 memset (&transform_data, 0, sizeof(transform_data));
1655 td = &transform_data;
1657 td->method = method;
1659 td->is_bb_start = is_bb_start;
1660 td->il_code = header->code;
1661 td->code_size = header->code_size;
1662 td->header = header;
1663 td->max_code_size = td->code_size;
1664 td->new_code = (unsigned short *)g_malloc(td->max_code_size * sizeof(gushort));
1665 td->new_code_end = td->new_code + td->max_code_size;
1666 td->mempool = mono_mempool_new ();
1667 td->in_offsets = g_malloc0(header->code_size * sizeof(int));
1668 td->stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
1669 td->stack_height = g_malloc(header->code_size * sizeof(int));
1670 td->vt_stack_size = g_malloc(header->code_size * sizeof(int));
1671 td->n_data_items = 0;
1672 td->max_data_items = 0;
1673 td->data_items = NULL;
1674 td->data_hash = g_hash_table_new (NULL, NULL);
1675 td->clause_indexes = g_malloc (header->code_size * sizeof (int));
1676 td->gen_sdb_seq_points = debug_options.gen_sdb_seq_points;
1677 td->seq_points = g_ptr_array_new ();
1678 td->relocs = g_ptr_array_new ();
1679 td->verbose_level = mono_interp_traceopt;
1680 rtm->data_items = td->data_items;
1681 for (i = 0; i < header->code_size; i++) {
1682 td->stack_height [i] = -1;
1683 td->clause_indexes [i] = -1;
1686 if (verbose_method_name) {
1687 const char *name = verbose_method_name;
1689 if ((strchr (name, '.') > name) || strchr (name, ':')) {
1690 MonoMethodDesc *desc;
1692 desc = mono_method_desc_new (name, TRUE);
1693 if (mono_method_desc_full_match (desc, method)) {
1694 td->verbose_level = 4;
1696 mono_method_desc_free (desc);
1698 if (strcmp (method->name, name) == 0)
1699 td->verbose_level = 4;
1703 if (td->gen_sdb_seq_points) {
1704 get_basic_blocks (td);
1706 minfo = mono_debug_lookup_method (method);
1709 MonoSymSeqPoint *sps;
1710 int i, n_il_offsets;
1712 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
1714 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1715 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
1716 sym_seq_points = TRUE;
1718 for (i = 0; i < n_il_offsets; ++i) {
1719 if (sps [i].il_offset < header->code_size)
1720 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
1723 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
1724 /* Methods without line number info like auto-generated property accessors */
1725 seq_point_locs = mono_bitset_new (header->code_size, 0);
1726 seq_point_set_locs = mono_bitset_new (header->code_size, 0);
1727 sym_seq_points = TRUE;
1731 td->new_ip = td->new_code;
1732 td->last_new_ip = NULL;
1734 td->stack = g_malloc0 ((header->max_stack + 1) * sizeof (td->stack [0]));
1736 td->max_stack_height = 0;
1738 line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
1740 for (i = 0; i < header->num_clauses; i++) {
1741 MonoExceptionClause *c = header->clauses + i;
1742 td->stack_height [c->handler_offset] = 0;
1743 td->vt_stack_size [c->handler_offset] = 0;
1744 td->is_bb_start [c->handler_offset] = 1;
1746 td->stack_height [c->handler_offset] = 1;
1747 td->stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
1748 td->stack_state [c->handler_offset][0].type = STACK_TYPE_O;
1749 td->stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
1751 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
1752 td->stack_height [c->data.filter_offset] = 0;
1753 td->vt_stack_size [c->data.filter_offset] = 0;
1754 td->is_bb_start [c->data.filter_offset] = 1;
1756 td->stack_height [c->data.filter_offset] = 1;
1757 td->stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
1758 td->stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
1759 td->stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
1762 for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
1763 if (td->clause_indexes [j] == -1)
1764 td->clause_indexes [j] = i;
1768 td->ip = header->code;
1769 end = td->ip + header->code_size;
1771 if (td->verbose_level) {
1772 char *tmp = mono_disasm_code (NULL, method, td->ip, end);
1773 char *name = mono_method_full_name (method, TRUE);
1774 g_print ("Method %s, original code:\n", name);
1775 g_print ("%s\n", tmp);
1780 if (signature->hasthis)
1781 store_inarg (td, 0);
1782 for (i = 0; i < signature->param_count; i++)
1783 store_inarg (td, i + !!signature->hasthis);
1785 body_start_offset = td->new_ip - td->new_code;
1787 for (i = 0; i < header->num_locals; i++) {
1788 int mt = mint_type(header->locals [i]);
1789 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1790 ADD_CODE(td, MINT_INITLOCALS);
1795 if (rtm->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_ENTER)
1796 ADD_CODE (td, MINT_PROF_ENTER);
1798 if (sym_seq_points) {
1799 InterpBasicBlock *cbb = td->offset_to_bb [0];
1801 emit_seq_point (td, METHOD_ENTRY_IL_OFFSET, cbb, FALSE);
1804 while (td->ip < end) {
1807 g_assert (td->sp >= td->stack);
1808 g_assert (td->vt_sp < 0x10000000);
1809 in_offset = td->ip - header->code;
1810 td->in_offsets [in_offset] = td->new_ip - td->new_code;
1811 new_in_start_offset = td->new_ip - td->new_code;
1812 td->in_start = td->ip;
1814 MonoDebugLineNumberEntry lne;
1815 lne.native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
1816 lne.il_offset = in_offset;
1817 g_array_append_val (line_numbers, lne);
1819 if (td->stack_height [in_offset] >= 0) {
1820 g_assert (is_bb_start [in_offset]);
1821 if (td->stack_height [in_offset] > 0)
1822 memcpy (td->stack, td->stack_state [in_offset], td->stack_height [in_offset] * sizeof(td->stack [0]));
1823 td->sp = td->stack + td->stack_height [in_offset];
1824 td->vt_sp = td->vt_stack_size [in_offset];
1826 if (is_bb_start [in_offset]) {
1827 generating_code = 1;
1829 if (!generating_code) {
1830 while (td->ip < end && !is_bb_start [td->ip - td->il_code])
1834 if (td->verbose_level > 1) {
1835 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1836 td->ip - td->il_code,
1837 td->is_bb_start [td->ip - td->il_code] == 3 ? "<>" :
1838 td->is_bb_start [td->ip - td->il_code] == 2 ? "< " :
1839 td->is_bb_start [td->ip - td->il_code] == 1 ? " >" : " ",
1840 mono_opcode_name (*td->ip), td->new_ip - td->new_code, td->sp - td->stack,
1841 td->sp > td->stack ? stack_type_string [td->sp [-1].type] : " ",
1842 (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) : "",
1843 td->vt_sp, td->max_vt_sp);
1846 if (sym_seq_points && mono_bitset_test_fast (seq_point_locs, td->ip - header->code)) {
1847 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
1851 * Make methods interruptable at the beginning, and at the targets of
1852 * backward branches.
1854 if (in_offset == 0 || g_slist_length (cbb->preds) > 1)
1855 ADD_CODE (td, MINT_SDB_INTR_LOC);
1857 emit_seq_point (td, in_offset, cbb, FALSE);
1859 mono_bitset_set_fast (seq_point_set_locs, td->ip - header->code);
1863 bb_exit = td->offset_to_bb [td->ip - header->code];
1871 SIMPLE_OP(td, MINT_BREAK);
1877 load_arg (td, *td->ip - CEE_LDARG_0);
1884 load_local (td, *td->ip - CEE_LDLOC_0);
1891 store_local (td, *td->ip - CEE_STLOC_0);
1895 load_arg (td, ((guint8 *)td->ip)[1]);
1898 case CEE_LDARGA_S: {
1899 /* NOTE: n includes this */
1900 int n = ((guint8 *) td->ip) [1];
1901 ADD_CODE (td, MINT_LDARGA);
1902 ADD_CODE (td, td->rtm->arg_offsets [n]);
1903 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1908 store_arg (td, ((guint8 *)td->ip)[1]);
1912 load_local (td, ((guint8 *)td->ip)[1]);
1916 ADD_CODE(td, MINT_LDLOCA_S);
1917 ADD_CODE(td, td->rtm->local_offsets [((guint8 *)td->ip)[1]]);
1918 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
1922 store_local (td, ((guint8 *)td->ip)[1]);
1926 SIMPLE_OP(td, MINT_LDNULL);
1927 PUSH_TYPE(td, STACK_TYPE_O, NULL);
1930 SIMPLE_OP(td, MINT_LDC_I4_M1);
1931 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1934 if (!td->is_bb_start[td->ip + 1 - td->il_code] && td->ip [1] == 0xfe && td->ip [2] == CEE_CEQ &&
1935 td->sp > td->stack && td->sp [-1].type == STACK_TYPE_I4) {
1936 SIMPLE_OP(td, MINT_CEQ0_I4);
1939 SIMPLE_OP(td, MINT_LDC_I4_0);
1940 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1944 if (!td->is_bb_start[td->ip + 1 - td->il_code] &&
1945 (td->ip [1] == CEE_ADD || td->ip [1] == CEE_SUB) && td->sp [-1].type == STACK_TYPE_I4) {
1946 ADD_CODE(td, td->ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1949 SIMPLE_OP(td, MINT_LDC_I4_1);
1950 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1960 SIMPLE_OP(td, (*td->ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1961 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1964 ADD_CODE(td, MINT_LDC_I4_S);
1965 ADD_CODE(td, ((gint8 *) td->ip) [1]);
1967 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1970 i32 = read32 (td->ip + 1);
1971 ADD_CODE(td, MINT_LDC_I4);
1974 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
1977 gint64 val = read64 (td->ip + 1);
1978 ADD_CODE(td, MINT_LDC_I8);
1981 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I8);
1986 readr4 (td->ip + 1, &val);
1987 ADD_CODE(td, MINT_LDC_R4);
1990 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
1995 readr8 (td->ip + 1, &val);
1996 ADD_CODE(td, MINT_LDC_R8);
1999 PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
2003 int type = td->sp [-1].type;
2004 MonoClass *klass = td->sp [-1].klass;
2005 if (td->sp [-1].type == STACK_TYPE_VT) {
2006 gint32 size = mono_class_value_size (klass, NULL);
2008 ADD_CODE(td, MINT_DUP_VT);
2012 SIMPLE_OP(td, MINT_DUP);
2013 PUSH_TYPE(td, type, klass);
2018 SIMPLE_OP(td, MINT_POP);
2020 if (td->sp [-1].type == STACK_TYPE_VT) {
2021 int size = mono_class_value_size (td->sp [-1].klass, NULL);
2022 size = (size + 7) & ~7;
2023 ADD_CODE(td, MINT_VTRESULT);
2032 if (td->sp > td->stack)
2033 g_warning ("CEE_JMP: stack must be empty");
2034 token = read32 (td->ip + 1);
2035 m = mono_get_method_full (image, token, NULL, generic_context);
2036 ADD_CODE (td, MINT_JMP);
2037 ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2038 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2042 case CEE_CALLVIRT: /* Fall through */
2043 case CEE_CALLI: /* Fall through */
2045 gboolean need_seq_point = FALSE;
2047 if (sym_seq_points && !mono_bitset_test_fast (seq_point_locs, td->ip + 5 - header->code))
2048 need_seq_point = TRUE;
2050 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
2052 if (need_seq_point) {
2053 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
2056 emit_seq_point (td, td->ip - header->code, cbb, TRUE);
2059 constrained_class = NULL;
2065 if (signature->ret->type != MONO_TYPE_VOID) {
2067 MonoClass *klass = mono_class_from_mono_type (signature->ret);
2068 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2069 vt_size = mono_class_value_size (klass, NULL);
2070 vt_size = (vt_size + 7) & ~7;
2073 if (td->sp > td->stack)
2074 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td->method->klass->name, td->method->name, td->sp - td->stack);
2075 if (td->vt_sp != vt_size)
2076 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td->method->klass->name, td->method->name, td->vt_sp, vt_size);
2078 if (sym_seq_points) {
2079 InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
2081 emit_seq_point (td, METHOD_EXIT_IL_OFFSET, bb_exit, FALSE);
2085 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
2087 ADD_CODE(td, MINT_RET_VT);
2088 WRITE32(td, &vt_size);
2091 generating_code = 0;
2095 handle_branch (td, MINT_BR_S, MINT_BR, 5 + read32 (td->ip + 1));
2097 generating_code = 0;
2100 handle_branch (td, MINT_BR_S, MINT_BR, 2 + (gint8)td->ip [1]);
2102 generating_code = 0;
2105 one_arg_branch (td, MINT_BRFALSE_I4, 5 + read32 (td->ip + 1));
2109 one_arg_branch (td, MINT_BRFALSE_I4, 2 + (gint8)td->ip [1]);
2113 one_arg_branch (td, MINT_BRTRUE_I4, 5 + read32 (td->ip + 1));
2117 one_arg_branch (td, MINT_BRTRUE_I4, 2 + (gint8)td->ip [1]);
2121 two_arg_branch (td, MINT_BEQ_I4, 5 + read32 (td->ip + 1));
2125 two_arg_branch (td, MINT_BEQ_I4, 2 + (gint8) td->ip [1]);
2129 two_arg_branch (td, MINT_BGE_I4, 5 + read32 (td->ip + 1));
2133 two_arg_branch (td, MINT_BGE_I4, 2 + (gint8) td->ip [1]);
2137 two_arg_branch (td, MINT_BGT_I4, 5 + read32 (td->ip + 1));
2141 two_arg_branch (td, MINT_BGT_I4, 2 + (gint8) td->ip [1]);
2145 two_arg_branch (td, MINT_BLT_I4, 5 + read32 (td->ip + 1));
2149 two_arg_branch (td, MINT_BLT_I4, 2 + (gint8) td->ip [1]);
2153 two_arg_branch (td, MINT_BLE_I4, 5 + read32 (td->ip + 1));
2157 two_arg_branch (td, MINT_BLE_I4, 2 + (gint8) td->ip [1]);
2161 two_arg_branch (td, MINT_BNE_UN_I4, 5 + read32 (td->ip + 1));
2165 two_arg_branch (td, MINT_BNE_UN_I4, 2 + (gint8) td->ip [1]);
2169 two_arg_branch (td, MINT_BGE_UN_I4, 5 + read32 (td->ip + 1));
2173 two_arg_branch (td, MINT_BGE_UN_I4, 2 + (gint8) td->ip [1]);
2177 two_arg_branch (td, MINT_BGT_UN_I4, 5 + read32 (td->ip + 1));
2181 two_arg_branch (td, MINT_BGT_UN_I4, 2 + (gint8) td->ip [1]);
2185 two_arg_branch (td, MINT_BLE_UN_I4, 5 + read32 (td->ip + 1));
2189 two_arg_branch (td, MINT_BLE_UN_I4, 2 + (gint8) td->ip [1]);
2193 two_arg_branch (td, MINT_BLT_UN_I4, 5 + read32 (td->ip + 1));
2197 two_arg_branch (td, MINT_BLT_UN_I4, 2 + (gint8) td->ip [1]);
2202 const unsigned char *next_ip;
2204 n = read32 (td->ip);
2205 ADD_CODE (td, MINT_SWITCH);
2208 next_ip = td->ip + n * 4;
2210 int stack_height = td->sp - td->stack;
2211 for (i = 0; i < n; i++) {
2212 offset = read32 (td->ip);
2213 target = next_ip - td->il_code + offset;
2216 if (stack_height > 0 && stack_height != td->stack_height [target])
2217 g_warning ("SWITCH with back branch and non-empty stack");
2219 target = td->in_offsets [target] - (td->new_ip - td->new_code);
2221 td->stack_height [target] = stack_height;
2222 td->vt_stack_size [target] = td->vt_sp;
2223 if (stack_height > 0)
2224 td->stack_state [target] = g_memdup (td->stack, stack_height * sizeof (td->stack [0]));
2226 Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
2227 reloc->type = RELOC_SWITCH;
2228 reloc->offset = td->new_ip - td->new_code;
2229 reloc->target = target;
2230 g_ptr_array_add (td->relocs, reloc);
2233 WRITE32 (td, &target);
2239 CHECK_STACK (td, 1);
2240 SIMPLE_OP (td, MINT_LDIND_I1);
2241 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2242 BARRIER_IF_VOLATILE (td);
2245 CHECK_STACK (td, 1);
2246 SIMPLE_OP (td, MINT_LDIND_U1);
2247 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2248 BARRIER_IF_VOLATILE (td);
2251 CHECK_STACK (td, 1);
2252 SIMPLE_OP (td, MINT_LDIND_I2);
2253 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2254 BARRIER_IF_VOLATILE (td);
2257 CHECK_STACK (td, 1);
2258 SIMPLE_OP (td, MINT_LDIND_U2);
2259 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2260 BARRIER_IF_VOLATILE (td);
2263 CHECK_STACK (td, 1);
2264 SIMPLE_OP (td, MINT_LDIND_I4);
2265 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2266 BARRIER_IF_VOLATILE (td);
2269 CHECK_STACK (td, 1);
2270 SIMPLE_OP (td, MINT_LDIND_U4);
2271 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2272 BARRIER_IF_VOLATILE (td);
2275 CHECK_STACK (td, 1);
2276 SIMPLE_OP (td, MINT_LDIND_I8);
2277 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2278 BARRIER_IF_VOLATILE (td);
2281 CHECK_STACK (td, 1);
2282 SIMPLE_OP (td, MINT_LDIND_I);
2284 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2285 BARRIER_IF_VOLATILE (td);
2288 CHECK_STACK (td, 1);
2289 SIMPLE_OP (td, MINT_LDIND_R4);
2290 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2291 BARRIER_IF_VOLATILE (td);
2294 CHECK_STACK (td, 1);
2295 SIMPLE_OP (td, MINT_LDIND_R8);
2296 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2297 BARRIER_IF_VOLATILE (td);
2300 CHECK_STACK (td, 1);
2301 SIMPLE_OP (td, MINT_LDIND_REF);
2302 BARRIER_IF_VOLATILE (td);
2303 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
2306 CHECK_STACK (td, 2);
2307 BARRIER_IF_VOLATILE (td);
2308 SIMPLE_OP (td, MINT_STIND_REF);
2312 CHECK_STACK (td, 2);
2313 BARRIER_IF_VOLATILE (td);
2314 SIMPLE_OP (td, MINT_STIND_I1);
2318 CHECK_STACK (td, 2);
2319 BARRIER_IF_VOLATILE (td);
2320 SIMPLE_OP (td, MINT_STIND_I2);
2324 CHECK_STACK (td, 2);
2325 BARRIER_IF_VOLATILE (td);
2326 SIMPLE_OP (td, MINT_STIND_I4);
2330 CHECK_STACK (td, 2);
2331 BARRIER_IF_VOLATILE (td);
2332 SIMPLE_OP (td, MINT_STIND_I);
2336 CHECK_STACK (td, 2);
2337 BARRIER_IF_VOLATILE (td);
2338 SIMPLE_OP (td, MINT_STIND_I8);
2342 CHECK_STACK (td, 2);
2343 BARRIER_IF_VOLATILE (td);
2344 SIMPLE_OP (td, MINT_STIND_R4);
2348 CHECK_STACK (td, 2);
2349 BARRIER_IF_VOLATILE (td);
2350 SIMPLE_OP (td, MINT_STIND_R8);
2354 binary_arith_op(td, MINT_ADD_I4);
2358 binary_arith_op(td, MINT_SUB_I4);
2362 binary_arith_op(td, MINT_MUL_I4);
2366 binary_arith_op(td, MINT_DIV_I4);
2370 binary_arith_op(td, MINT_DIV_UN_I4);
2374 binary_arith_op (td, MINT_REM_I4);
2378 binary_arith_op (td, MINT_REM_UN_I4);
2382 binary_arith_op (td, MINT_AND_I4);
2386 binary_arith_op (td, MINT_OR_I4);
2390 binary_arith_op (td, MINT_XOR_I4);
2394 shift_op (td, MINT_SHL_I4);
2398 shift_op (td, MINT_SHR_I4);
2402 shift_op (td, MINT_SHR_UN_I4);
2406 unary_arith_op (td, MINT_NEG_I4);
2410 unary_arith_op (td, MINT_NOT_I4);
2414 CHECK_STACK (td, 1);
2415 switch (td->sp [-1].type) {
2417 ADD_CODE(td, MINT_CONV_U1_R8);
2420 ADD_CODE(td, MINT_CONV_U1_I4);
2423 ADD_CODE(td, MINT_CONV_U1_I8);
2426 g_assert_not_reached ();
2429 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2432 CHECK_STACK (td, 1);
2433 switch (td->sp [-1].type) {
2435 ADD_CODE(td, MINT_CONV_I1_R8);
2438 ADD_CODE(td, MINT_CONV_I1_I4);
2441 ADD_CODE(td, MINT_CONV_I1_I8);
2444 g_assert_not_reached ();
2447 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2450 CHECK_STACK (td, 1);
2451 switch (td->sp [-1].type) {
2453 ADD_CODE(td, MINT_CONV_U2_R8);
2456 ADD_CODE(td, MINT_CONV_U2_I4);
2459 ADD_CODE(td, MINT_CONV_U2_I8);
2462 g_assert_not_reached ();
2465 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2468 CHECK_STACK (td, 1);
2469 switch (td->sp [-1].type) {
2471 ADD_CODE(td, MINT_CONV_I2_R8);
2474 ADD_CODE(td, MINT_CONV_I2_I4);
2477 ADD_CODE(td, MINT_CONV_I2_I8);
2480 g_assert_not_reached ();
2483 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2486 CHECK_STACK (td, 1);
2487 switch (td->sp [-1].type) {
2489 #if SIZEOF_VOID_P == 4
2490 ADD_CODE(td, MINT_CONV_U4_R8);
2492 ADD_CODE(td, MINT_CONV_U8_R8);
2496 #if SIZEOF_VOID_P == 8
2497 ADD_CODE(td, MINT_CONV_U8_I4);
2501 #if SIZEOF_VOID_P == 4
2502 ADD_CODE(td, MINT_CONV_U4_I8);
2508 g_assert_not_reached ();
2511 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2514 CHECK_STACK (td, 1);
2515 switch (td->sp [-1].type) {
2517 #if SIZEOF_VOID_P == 8
2518 ADD_CODE(td, MINT_CONV_I8_R8);
2520 ADD_CODE(td, MINT_CONV_I4_R8);
2524 #if SIZEOF_VOID_P == 8
2525 ADD_CODE(td, MINT_CONV_I8_I4);
2533 #if SIZEOF_VOID_P == 4
2534 ADD_CODE(td, MINT_CONV_I4_I8);
2538 g_assert_not_reached ();
2541 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2544 CHECK_STACK (td, 1);
2545 switch (td->sp [-1].type) {
2547 ADD_CODE(td, MINT_CONV_U4_R8);
2552 ADD_CODE(td, MINT_CONV_U4_I8);
2555 #if SIZEOF_VOID_P == 8
2556 ADD_CODE(td, MINT_CONV_U4_I8);
2560 g_assert_not_reached ();
2563 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2566 CHECK_STACK (td, 1);
2567 switch (td->sp [-1].type) {
2569 ADD_CODE(td, MINT_CONV_I4_R8);
2574 ADD_CODE(td, MINT_CONV_I4_I8);
2577 #if SIZEOF_VOID_P == 8
2578 ADD_CODE(td, MINT_CONV_I4_I8);
2582 g_assert_not_reached ();
2585 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2588 CHECK_STACK (td, 1);
2589 switch (td->sp [-1].type) {
2591 ADD_CODE(td, MINT_CONV_I8_R8);
2594 ADD_CODE(td, MINT_CONV_I8_I4);
2599 #if SIZEOF_VOID_P == 4
2600 ADD_CODE(td, MINT_CONV_I8_I4);
2604 g_assert_not_reached ();
2607 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2610 CHECK_STACK (td, 1);
2611 switch (td->sp [-1].type) {
2613 ADD_CODE(td, MINT_CONV_R4_R8);
2616 ADD_CODE(td, MINT_CONV_R4_I8);
2619 ADD_CODE(td, MINT_CONV_R4_I4);
2622 g_assert_not_reached ();
2625 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2628 CHECK_STACK (td, 1);
2629 switch (td->sp [-1].type) {
2631 ADD_CODE(td, MINT_CONV_R8_I4);
2634 ADD_CODE(td, MINT_CONV_R8_I8);
2639 g_assert_not_reached ();
2642 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2645 CHECK_STACK (td, 1);
2646 switch (td->sp [-1].type) {
2648 ADD_CODE(td, MINT_CONV_U8_I4);
2653 ADD_CODE(td, MINT_CONV_U8_R8);
2656 #if SIZEOF_VOID_P == 4
2657 ADD_CODE(td, MINT_CONV_U8_I4);
2661 g_assert_not_reached ();
2664 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2667 CHECK_STACK (td, 2);
2669 token = read32 (td->ip + 1);
2670 klass = mono_class_get_full (image, token, generic_context);
2672 if (klass->valuetype) {
2673 ADD_CODE (td, MINT_CPOBJ);
2674 ADD_CODE (td, get_data_item_index(td, klass));
2676 ADD_CODE (td, MINT_LDIND_REF);
2677 ADD_CODE (td, MINT_STIND_REF);
2685 CHECK_STACK (td, 1);
2687 token = read32 (td->ip + 1);
2689 if (method->wrapper_type != MONO_WRAPPER_NONE)
2690 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2692 klass = mono_class_get_full (image, token, generic_context);
2694 ADD_CODE(td, MINT_LDOBJ);
2695 ADD_CODE(td, get_data_item_index(td, klass));
2696 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2697 size = mono_class_value_size (klass, NULL);
2701 SET_TYPE(td->sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
2702 BARRIER_IF_VOLATILE (td);
2707 token = mono_metadata_token_index (read32 (td->ip + 1));
2709 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
2710 s = mono_method_get_wrapper_data (method, token);
2711 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
2712 s = mono_string_new_wrapper (mono_method_get_wrapper_data (method, token));
2714 s = mono_ldstr (domain, image, token);
2716 ADD_CODE(td, MINT_LDSTR);
2717 ADD_CODE(td, get_data_item_index (td, s));
2718 PUSH_TYPE(td, STACK_TYPE_O, mono_defaults.string_class);
2723 MonoMethodSignature *csignature;
2724 guint32 vt_stack_used = 0;
2725 guint32 vt_res_size = 0;
2728 token = read32 (td->ip);
2731 if (method->wrapper_type != MONO_WRAPPER_NONE)
2732 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2734 m = mono_get_method_full (image, token, NULL, generic_context);
2736 csignature = mono_method_signature (m);
2739 td->sp -= csignature->param_count;
2740 if (mono_class_is_magic_int (klass) || mono_class_is_magic_float (klass)) {
2741 ADD_CODE (td, MINT_NEWOBJ_MAGIC);
2742 ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2743 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2745 ADD_CODE(td, MINT_NEWOBJ);
2746 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2747 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2749 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2750 vt_res_size = mono_class_value_size (klass, NULL);
2751 PUSH_VT (td, vt_res_size);
2753 for (i = 0; i < csignature->param_count; ++i) {
2754 int mt = mint_type(csignature->params [i]);
2755 if (mt == MINT_TYPE_VT) {
2756 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
2757 gint32 size = mono_class_value_size (k, NULL);
2758 size = (size + 7) & ~7;
2759 vt_stack_used += size;
2762 if (vt_stack_used != 0 || vt_res_size != 0) {
2763 ADD_CODE(td, MINT_VTRESULT);
2764 ADD_CODE(td, vt_res_size);
2765 WRITE32(td, &vt_stack_used);
2766 td->vt_sp -= vt_stack_used;
2768 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2773 CHECK_STACK (td, 1);
2774 token = read32 (td->ip + 1);
2775 klass = mini_get_class (method, token, generic_context);
2776 ADD_CODE(td, MINT_CASTCLASS);
2777 ADD_CODE(td, get_data_item_index (td, klass));
2778 td->sp [-1].klass = klass;
2782 CHECK_STACK (td, 1);
2783 token = read32 (td->ip + 1);
2784 klass = mini_get_class (method, token, generic_context);
2785 ADD_CODE(td, MINT_ISINST);
2786 ADD_CODE(td, get_data_item_index (td, klass));
2790 switch (td->sp [-1].type) {
2794 ADD_CODE(td, MINT_CONV_R_UN_I8);
2797 ADD_CODE(td, MINT_CONV_R_UN_I4);
2800 g_assert_not_reached ();
2802 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2806 CHECK_STACK (td, 1);
2807 token = read32 (td->ip + 1);
2809 if (method->wrapper_type != MONO_WRAPPER_NONE)
2810 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2812 klass = mono_class_get_full (image, token, generic_context);
2814 if (mono_class_is_nullable (klass)) {
2815 g_error ("cee_unbox: implement Nullable");
2818 ADD_CODE(td, MINT_UNBOX);
2819 ADD_CODE(td, get_data_item_index (td, klass));
2820 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2824 CHECK_STACK (td, 1);
2825 token = read32 (td->ip + 1);
2827 klass = mini_get_class (method, token, generic_context);
2829 if (mini_type_is_reference (&klass->byval_arg)) {
2830 int mt = mint_type (&klass->byval_arg);
2831 ADD_CODE (td, MINT_CASTCLASS);
2832 ADD_CODE (td, get_data_item_index (td, klass));
2833 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2835 } else if (mono_class_is_nullable (klass)) {
2836 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2837 /* td->ip is incremented by interp_transform_call */
2838 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2840 int mt = mint_type (&klass->byval_arg);
2841 ADD_CODE (td, MINT_UNBOX);
2842 ADD_CODE (td, get_data_item_index (td, klass));
2844 ADD_CODE (td, MINT_LDOBJ);
2845 ADD_CODE (td, get_data_item_index(td, klass));
2846 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2848 if (mt == MINT_TYPE_VT) {
2849 int size = mono_class_value_size (klass, NULL);
2857 CHECK_STACK (td, 1);
2858 SIMPLE_OP (td, MINT_THROW);
2860 generating_code = 0;
2863 CHECK_STACK (td, 1);
2864 token = read32 (td->ip + 1);
2865 field = interp_field_from_token (method, token, &klass, generic_context);
2866 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2867 mono_class_init (klass);
2869 ADD_CODE (td, MINT_POP);
2871 ADD_CODE (td, MINT_LDSFLDA);
2872 ADD_CODE (td, get_data_item_index (td, field));
2874 if ((td->sp - 1)->type == STACK_TYPE_O) {
2875 ADD_CODE (td, MINT_LDFLDA);
2877 g_assert ((td->sp -1)->type == STACK_TYPE_MP);
2878 ADD_CODE (td, MINT_LDFLDA_UNSAFE);
2880 ADD_CODE (td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2883 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2886 CHECK_STACK (td, 1);
2887 token = read32 (td->ip + 1);
2888 field = interp_field_from_token (method, token, &klass, generic_context);
2889 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2890 mono_class_init (klass);
2892 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2893 mt = mint_type (&field_klass->byval_arg);
2894 #ifndef DISABLE_REMOTING
2895 if (klass->marshalbyref) {
2896 g_assert (!is_static);
2897 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2898 ADD_CODE(td, get_data_item_index (td, field));
2903 ADD_CODE (td, MINT_POP);
2905 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2906 ADD_CODE (td, get_data_item_index (td, field));
2908 ADD_CODE (td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2909 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2912 if (mt == MINT_TYPE_VT) {
2913 int size = mono_class_value_size (field_klass, NULL);
2917 if (td->sp [-1].type == STACK_TYPE_VT) {
2918 int size = mono_class_value_size (klass, NULL);
2919 size = (size + 7) & ~7;
2921 ADD_CODE (td, MINT_VTRESULT);
2923 WRITE32 (td, &size);
2926 SET_TYPE(td->sp - 1, stack_type [mt], field_klass);
2927 BARRIER_IF_VOLATILE (td);
2931 CHECK_STACK (td, 2);
2932 token = read32 (td->ip + 1);
2933 field = interp_field_from_token (method, token, &klass, generic_context);
2934 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2935 mono_class_init (klass);
2936 mt = mint_type(field->type);
2938 BARRIER_IF_VOLATILE (td);
2940 #ifndef DISABLE_REMOTING
2941 if (klass->marshalbyref) {
2942 g_assert (!is_static);
2943 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2944 ADD_CODE(td, get_data_item_index (td, field));
2949 ADD_CODE (td, MINT_POP);
2951 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2952 ADD_CODE (td, get_data_item_index (td, field));
2954 ADD_CODE (td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2955 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2958 if (mt == MINT_TYPE_VT) {
2959 MonoClass *klass = mono_class_from_mono_type (field->type);
2960 int size = mono_class_value_size (klass, NULL);
2969 token = read32 (td->ip + 1);
2970 field = interp_field_from_token (method, token, &klass, generic_context);
2971 ADD_CODE(td, MINT_LDSFLDA);
2972 ADD_CODE(td, get_data_item_index (td, field));
2974 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
2977 token = read32 (td->ip + 1);
2978 field = interp_field_from_token (method, token, &klass, generic_context);
2979 mt = mint_type(field->type);
2980 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2981 ADD_CODE(td, get_data_item_index (td, field));
2983 if (mt == MINT_TYPE_VT) {
2984 MonoClass *klass = mono_class_from_mono_type (field->type);
2985 int size = mono_class_value_size (klass, NULL);
2988 klass = field->type->data.klass;
2990 if (mt == MINT_TYPE_O)
2991 klass = mono_class_from_mono_type (field->type);
2994 PUSH_TYPE(td, stack_type [mt], klass);
2997 CHECK_STACK (td, 1);
2998 token = read32 (td->ip + 1);
2999 field = interp_field_from_token (method, token, &klass, generic_context);
3000 mt = mint_type(field->type);
3001 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
3002 ADD_CODE(td, get_data_item_index (td, field));
3003 if (mt == MINT_TYPE_VT) {
3004 MonoClass *klass = mono_class_from_mono_type (field->type);
3005 int size = mono_class_value_size (klass, NULL);
3007 WRITE32 (td, &size);
3014 token = read32 (td->ip + 1);
3016 if (method->wrapper_type != MONO_WRAPPER_NONE)
3017 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3019 klass = mini_get_class (method, token, generic_context);
3021 BARRIER_IF_VOLATILE (td);
3022 ADD_CODE(td, td->sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
3023 ADD_CODE(td, get_data_item_index (td, klass));
3024 if (td->sp [-1].type == STACK_TYPE_VT) {
3025 size = mono_class_value_size (klass, NULL);
3026 size = (size + 7) & ~7;
3033 case CEE_CONV_OVF_I_UN:
3034 case CEE_CONV_OVF_U_UN:
3035 CHECK_STACK (td, 1);
3036 switch (td->sp [-1].type) {
3038 #if SIZEOF_VOID_P == 8
3039 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
3041 ADD_CODE(td, MINT_CONV_OVF_I4_UN_R8);
3045 #if SIZEOF_VOID_P == 4
3046 ADD_CODE (td, MINT_CONV_OVF_I4_UN_I8);
3050 #if SIZEOF_VOID_P == 8
3051 ADD_CODE(td, MINT_CONV_I8_U4);
3052 #elif SIZEOF_VOID_P == 4
3053 if (*td->ip == CEE_CONV_OVF_I_UN)
3054 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3058 g_assert_not_reached ();
3061 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3064 case CEE_CONV_OVF_I8_UN:
3065 case CEE_CONV_OVF_U8_UN:
3066 CHECK_STACK (td, 1);
3067 switch (td->sp [-1].type) {
3069 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
3072 if (*td->ip == CEE_CONV_OVF_I8_UN)
3073 ADD_CODE (td, MINT_CONV_OVF_I8_U8);
3076 ADD_CODE(td, MINT_CONV_I8_U4);
3079 g_assert_not_reached ();
3082 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3087 CHECK_STACK (td, 1);
3088 token = read32 (td->ip + 1);
3089 if (method->wrapper_type != MONO_WRAPPER_NONE)
3090 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3092 klass = mini_get_class (method, token, generic_context);
3094 if (mono_class_is_nullable (klass)) {
3095 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
3096 /* td->ip is incremented by interp_transform_call */
3097 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3098 } else if (!klass->valuetype) {
3099 /* already boxed, do nothing. */
3102 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
3103 size = mono_class_value_size (klass, NULL);
3104 size = (size + 7) & ~7;
3107 ADD_CODE(td, MINT_BOX);
3108 ADD_CODE(td, get_data_item_index (td, klass));
3110 SET_TYPE(td->sp - 1, STACK_TYPE_O, klass);
3117 CHECK_STACK (td, 1);
3118 token = read32 (td->ip + 1);
3120 if (method->wrapper_type != MONO_WRAPPER_NONE)
3121 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3123 klass = mini_get_class (method, token, generic_context);
3125 unsigned char lentype = (td->sp - 1)->type;
3126 if (lentype == STACK_TYPE_I8) {
3127 /* mimic mini behaviour */
3128 ADD_CODE (td, MINT_CONV_OVF_U4_I8);
3130 g_assert (lentype == STACK_TYPE_I4);
3131 ADD_CODE (td, MINT_CONV_OVF_U4_I4);
3133 SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
3134 ADD_CODE (td, MINT_NEWARR);
3135 ADD_CODE (td, get_data_item_index (td, klass));
3136 SET_TYPE (td->sp - 1, STACK_TYPE_O, klass);
3141 CHECK_STACK (td, 1);
3142 SIMPLE_OP (td, MINT_LDLEN);
3143 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3146 CHECK_STACK (td, 2);
3148 token = read32 (td->ip + 1);
3150 if (method->wrapper_type != MONO_WRAPPER_NONE)
3151 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
3153 klass = mini_get_class (method, token, generic_context);
3155 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
3156 ADD_CODE (td, MINT_LDELEMA_TC);
3158 ADD_CODE (td, MINT_LDELEMA);
3160 ADD_CODE (td, get_data_item_index (td, klass));
3161 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
3167 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3170 CHECK_STACK (td, 2);
3172 SIMPLE_OP (td, MINT_LDELEM_I1);
3174 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3177 CHECK_STACK (td, 2);
3179 SIMPLE_OP (td, MINT_LDELEM_U1);
3181 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3184 CHECK_STACK (td, 2);
3186 SIMPLE_OP (td, MINT_LDELEM_I2);
3188 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3191 CHECK_STACK (td, 2);
3193 SIMPLE_OP (td, MINT_LDELEM_U2);
3195 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3198 CHECK_STACK (td, 2);
3200 SIMPLE_OP (td, MINT_LDELEM_I4);
3202 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3205 CHECK_STACK (td, 2);
3207 SIMPLE_OP (td, MINT_LDELEM_U4);
3209 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3212 CHECK_STACK (td, 2);
3214 SIMPLE_OP (td, MINT_LDELEM_I8);
3216 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3219 CHECK_STACK (td, 2);
3221 SIMPLE_OP (td, MINT_LDELEM_I);
3223 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3226 CHECK_STACK (td, 2);
3228 SIMPLE_OP (td, MINT_LDELEM_R4);
3230 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3233 CHECK_STACK (td, 2);
3235 SIMPLE_OP (td, MINT_LDELEM_R8);
3237 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3239 case CEE_LDELEM_REF:
3240 CHECK_STACK (td, 2);
3242 SIMPLE_OP (td, MINT_LDELEM_REF);
3244 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3247 CHECK_STACK (td, 2);
3248 token = read32 (td->ip + 1);
3249 klass = mini_get_class (method, token, generic_context);
3250 switch (mint_type (&klass->byval_arg)) {
3253 SIMPLE_OP (td, MINT_LDELEM_I1);
3255 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3259 SIMPLE_OP (td, MINT_LDELEM_U1);
3261 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3265 SIMPLE_OP (td, MINT_LDELEM_U2);
3267 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3271 SIMPLE_OP (td, MINT_LDELEM_I2);
3273 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3277 SIMPLE_OP (td, MINT_LDELEM_I4);
3279 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3283 SIMPLE_OP (td, MINT_LDELEM_I8);
3285 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3289 SIMPLE_OP (td, MINT_LDELEM_R4);
3291 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3295 SIMPLE_OP (td, MINT_LDELEM_R8);
3297 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3301 SIMPLE_OP (td, MINT_LDELEM_REF);
3303 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3305 case MINT_TYPE_VT: {
3306 int size = mono_class_value_size (klass, NULL);
3308 SIMPLE_OP (td, MINT_LDELEM_VT);
3309 ADD_CODE (td, get_data_item_index (td, klass));
3310 WRITE32 (td, &size);
3312 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_VT);
3317 GString *res = g_string_new ("");
3318 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3319 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3320 g_string_free (res, TRUE);
3328 CHECK_STACK (td, 3);
3330 SIMPLE_OP (td, MINT_STELEM_I);
3334 CHECK_STACK (td, 3);
3336 SIMPLE_OP (td, MINT_STELEM_I1);
3340 CHECK_STACK (td, 3);
3342 SIMPLE_OP (td, MINT_STELEM_I2);
3346 CHECK_STACK (td, 3);
3348 SIMPLE_OP (td, MINT_STELEM_I4);
3352 CHECK_STACK (td, 3);
3354 SIMPLE_OP (td, MINT_STELEM_I8);
3358 CHECK_STACK (td, 3);
3360 SIMPLE_OP (td, MINT_STELEM_R4);
3364 CHECK_STACK (td, 3);
3366 SIMPLE_OP (td, MINT_STELEM_R8);
3369 case CEE_STELEM_REF:
3370 CHECK_STACK (td, 3);
3372 SIMPLE_OP (td, MINT_STELEM_REF);
3376 CHECK_STACK (td, 3);
3378 token = read32 (td->ip + 1);
3379 klass = mini_get_class (method, token, generic_context);
3380 switch (mint_type (&klass->byval_arg)) {
3382 SIMPLE_OP (td, MINT_STELEM_U1);
3385 SIMPLE_OP (td, MINT_STELEM_U2);
3388 SIMPLE_OP (td, MINT_STELEM_I4);
3391 SIMPLE_OP (td, MINT_STELEM_I8);
3394 SIMPLE_OP (td, MINT_STELEM_REF);
3396 case MINT_TYPE_VT: {
3397 int size = mono_class_value_size (klass, NULL);
3398 SIMPLE_OP (td, MINT_STELEM_VT);
3399 ADD_CODE (td, get_data_item_index (td, klass));
3400 WRITE32 (td, &size);
3405 GString *res = g_string_new ("");
3406 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3407 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3408 g_string_free (res, TRUE);
3417 case CEE_CONV_OVF_U1:
3419 case CEE_CONV_OVF_I8:
3421 #if SIZEOF_VOID_P == 8
3422 case CEE_CONV_OVF_U:
3426 CHECK_STACK (td, 1);
3427 SIMPLE_OP (td, MINT_CKFINITE);
3430 CHECK_STACK (td, 1);
3432 token = read32 (td->ip + 1);
3433 klass = mini_get_class (method, token, generic_context);
3435 ADD_CODE (td, MINT_MKREFANY);
3436 ADD_CODE (td, get_data_item_index (td, klass));
3439 PUSH_VT (td, sizeof (MonoTypedRef));
3440 SET_TYPE(td->sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
3442 case CEE_REFANYVAL: {
3443 CHECK_STACK (td, 1);
3445 token = read32 (td->ip + 1);
3447 ADD_CODE (td, MINT_REFANYVAL);
3449 POP_VT (td, sizeof (MonoTypedRef));
3450 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3455 case CEE_CONV_OVF_I1:
3456 case CEE_CONV_OVF_I1_UN:
3457 CHECK_STACK (td, 1);
3458 switch (td->sp [-1].type) {
3460 ADD_CODE(td, MINT_CONV_OVF_I1_R8);
3463 ADD_CODE(td, MINT_CONV_OVF_I1_I4);
3466 ADD_CODE(td, MINT_CONV_OVF_I1_I8);
3469 g_assert_not_reached ();
3472 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3474 case CEE_CONV_OVF_U1:
3475 case CEE_CONV_OVF_U1_UN:
3476 CHECK_STACK (td, 1);
3477 switch (td->sp [-1].type) {
3479 ADD_CODE(td, MINT_CONV_OVF_U1_R8);
3482 ADD_CODE(td, MINT_CONV_OVF_U1_I4);
3485 ADD_CODE(td, MINT_CONV_OVF_U1_I8);
3488 g_assert_not_reached ();
3491 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3493 case CEE_CONV_OVF_I2:
3494 case CEE_CONV_OVF_I2_UN:
3495 CHECK_STACK (td, 1);
3496 switch (td->sp [-1].type) {
3498 ADD_CODE(td, MINT_CONV_OVF_I2_R8);
3501 ADD_CODE(td, MINT_CONV_OVF_I2_I4);
3504 ADD_CODE(td, MINT_CONV_OVF_I2_I8);
3507 g_assert_not_reached ();
3510 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3512 case CEE_CONV_OVF_U2_UN:
3513 case CEE_CONV_OVF_U2:
3514 CHECK_STACK (td, 1);
3515 switch (td->sp [-1].type) {
3517 ADD_CODE(td, MINT_CONV_OVF_U2_R8);
3520 ADD_CODE(td, MINT_CONV_OVF_U2_I4);
3523 ADD_CODE(td, MINT_CONV_OVF_U2_I8);
3526 g_assert_not_reached ();
3529 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3531 #if SIZEOF_VOID_P == 4
3532 case CEE_CONV_OVF_I:
3534 case CEE_CONV_OVF_I4:
3535 case CEE_CONV_OVF_I4_UN:
3536 CHECK_STACK (td, 1);
3537 switch (td->sp [-1].type) {
3539 ADD_CODE(td, MINT_CONV_OVF_I4_R8);
3542 if (*td->ip == CEE_CONV_OVF_I4_UN)
3543 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3546 if (*td->ip == CEE_CONV_OVF_I4_UN)
3547 ADD_CODE (td, MINT_CONV_OVF_I4_U8);
3549 ADD_CODE (td, MINT_CONV_OVF_I4_I8);
3552 g_assert_not_reached ();
3555 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3557 #if SIZEOF_VOID_P == 4
3558 case CEE_CONV_OVF_U:
3560 case CEE_CONV_OVF_U4:
3561 case CEE_CONV_OVF_U4_UN:
3562 CHECK_STACK (td, 1);
3563 switch (td->sp [-1].type) {
3565 ADD_CODE(td, MINT_CONV_OVF_U4_R8);
3568 if (*td->ip != CEE_CONV_OVF_U4_UN)
3569 ADD_CODE(td, MINT_CONV_OVF_U4_I4);
3572 ADD_CODE(td, MINT_CONV_OVF_U4_I8);
3575 g_assert_not_reached ();
3578 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3580 #if SIZEOF_VOID_P == 8
3581 case CEE_CONV_OVF_I:
3583 case CEE_CONV_OVF_I8:
3584 CHECK_STACK (td, 1);
3585 switch (td->sp [-1].type) {
3587 ADD_CODE(td, MINT_CONV_OVF_I8_R8);
3590 ADD_CODE(td, MINT_CONV_I8_I4);
3595 g_assert_not_reached ();
3598 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3600 #if SIZEOF_VOID_P == 8
3601 case CEE_CONV_OVF_U:
3603 case CEE_CONV_OVF_U8:
3604 CHECK_STACK (td, 1);
3605 switch (td->sp [-1].type) {
3607 ADD_CODE(td, MINT_CONV_OVF_U8_R8);
3610 ADD_CODE(td, MINT_CONV_OVF_U8_I4);
3613 ADD_CODE (td, MINT_CONV_OVF_U8_I8);
3616 g_assert_not_reached ();
3619 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3624 token = read32 (td->ip + 1);
3625 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3626 handle = mono_method_get_wrapper_data (method, token);
3627 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
3628 if (klass == mono_defaults.typehandle_class)
3629 handle = &((MonoClass *) handle)->byval_arg;
3631 if (generic_context) {
3632 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
3633 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3636 handle = mono_ldtoken (image, token, &klass, generic_context);
3638 mono_class_init (klass);
3639 mt = mint_type (&klass->byval_arg);
3640 g_assert (mt == MINT_TYPE_VT);
3641 size = mono_class_value_size (klass, NULL);
3642 g_assert (size == sizeof(gpointer));
3643 PUSH_VT (td, sizeof(gpointer));
3644 ADD_CODE (td, MINT_LDTOKEN);
3645 ADD_CODE (td, get_data_item_index (td, handle));
3647 SET_TYPE (td->sp, stack_type [mt], klass);
3653 binary_arith_op(td, MINT_ADD_OVF_I4);
3656 case CEE_ADD_OVF_UN:
3657 binary_arith_op(td, MINT_ADD_OVF_UN_I4);
3661 binary_arith_op(td, MINT_MUL_OVF_I4);
3664 case CEE_MUL_OVF_UN:
3665 binary_arith_op(td, MINT_MUL_OVF_UN_I4);
3669 binary_arith_op(td, MINT_SUB_OVF_I4);
3672 case CEE_SUB_OVF_UN:
3673 binary_arith_op(td, MINT_SUB_OVF_UN_I4);
3676 case CEE_ENDFINALLY:
3677 g_assert (td->clause_indexes [in_offset] != -1);
3679 SIMPLE_OP (td, MINT_ENDFINALLY);
3680 ADD_CODE (td, td->clause_indexes [in_offset]);
3681 generating_code = 0;
3685 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td->ip + 1));
3687 generating_code = 0;
3691 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td->ip [1]);
3693 generating_code = 0;
3698 case CEE_MONO_CALLI_EXTRA_ARG:
3699 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
3700 ADD_CODE (td, MINT_POP);
3703 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3705 case CEE_MONO_JIT_ICALL_ADDR: {
3708 MonoJitICallInfo *info;
3710 token = read32 (td->ip + 1);
3712 func = mono_method_get_wrapper_data (method, token);
3713 info = mono_find_jit_icall_by_addr (func);
3715 ADD_CODE (td, MINT_LDFTN);
3716 ADD_CODE (td, get_data_item_index (td, func));
3717 PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
3720 case CEE_MONO_ICALL: {
3723 MonoJitICallInfo *info;
3725 token = read32 (td->ip + 1);
3727 func = mono_method_get_wrapper_data (method, token);
3728 info = mono_find_jit_icall_by_addr (func);
3731 CHECK_STACK (td, info->sig->param_count);
3732 switch (info->sig->param_count) {
3734 if (MONO_TYPE_IS_VOID (info->sig->ret))
3735 ADD_CODE (td,MINT_ICALL_V_V);
3737 ADD_CODE (td, MINT_ICALL_V_P);
3740 if (MONO_TYPE_IS_VOID (info->sig->ret))
3741 ADD_CODE (td,MINT_ICALL_P_V);
3743 ADD_CODE (td,MINT_ICALL_P_P);
3746 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
3747 if (info->sig->params [1]->type == MONO_TYPE_I4)
3748 ADD_CODE (td,MINT_ICALL_PI_V);
3750 ADD_CODE (td,MINT_ICALL_PP_V);
3752 if (info->sig->params [1]->type == MONO_TYPE_I4)
3753 ADD_CODE (td,MINT_ICALL_PI_P);
3755 ADD_CODE (td,MINT_ICALL_PP_P);
3759 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
3760 if (info->sig->params [2]->type == MONO_TYPE_I4)
3761 ADD_CODE (td,MINT_ICALL_PPI_V);
3763 ADD_CODE (td,MINT_ICALL_PPP_V);
3766 g_assert_not_reached ();
3769 if (func == mono_ftnptr_to_delegate) {
3770 g_error ("TODO: ?");
3772 ADD_CODE(td, get_data_item_index (td, func));
3773 td->sp -= info->sig->param_count;
3775 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3777 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3781 case CEE_MONO_VTADDR: {
3783 CHECK_STACK (td, 1);
3784 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
3785 size = mono_class_native_size(td->sp [-1].klass, NULL);
3787 size = mono_class_value_size(td->sp [-1].klass, NULL);
3788 size = (size + 7) & ~7;
3789 ADD_CODE(td, MINT_VTRESULT);
3794 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3797 case CEE_MONO_LDPTR:
3798 case CEE_MONO_CLASSCONST:
3799 token = read32 (td->ip + 1);
3801 ADD_CODE(td, MINT_MONO_LDPTR);
3802 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3803 td->sp [0].type = STACK_TYPE_I;
3806 case CEE_MONO_OBJADDR:
3807 CHECK_STACK (td, 1);
3809 td->sp[-1].type = STACK_TYPE_MP;
3812 case CEE_MONO_NEWOBJ:
3813 token = read32 (td->ip + 1);
3815 ADD_CODE(td, MINT_MONO_NEWOBJ);
3816 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3817 td->sp [0].type = STACK_TYPE_O;
3820 case CEE_MONO_RETOBJ:
3821 CHECK_STACK (td, 1);
3822 token = read32 (td->ip + 1);
3824 ADD_CODE(td, MINT_MONO_RETOBJ);
3827 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3829 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
3831 if (td->sp > td->stack)
3832 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td->sp-td->stack);
3834 case CEE_MONO_LDNATIVEOBJ:
3835 token = read32 (td->ip + 1);
3837 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3838 g_assert(klass->valuetype);
3839 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3841 case CEE_MONO_TLS: {
3842 gint32 key = read32 (td->ip + 1);
3844 g_assert (key < TLS_KEY_NUM);
3845 ADD_CODE (td, MINT_MONO_TLS);
3847 PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
3850 case CEE_MONO_ATOMIC_STORE_I4:
3851 CHECK_STACK (td, 2);
3852 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3856 case CEE_MONO_SAVE_LMF:
3857 case CEE_MONO_RESTORE_LMF:
3858 case CEE_MONO_NOT_TAKEN:
3861 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3862 ADD_CODE (td, MINT_MONO_LDPTR);
3863 ADD_CODE (td, get_data_item_index (td, mono_thread_interruption_request_flag ()));
3864 PUSH_TYPE (td, STACK_TYPE_MP, NULL);
3867 case CEE_MONO_MEMORY_BARRIER:
3868 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3871 case CEE_MONO_JIT_ATTACH:
3872 ADD_CODE (td, MINT_MONO_JIT_ATTACH);
3875 case CEE_MONO_JIT_DETACH:
3876 ADD_CODE (td, MINT_MONO_JIT_DETACH);
3879 case CEE_MONO_LDDOMAIN:
3880 ADD_CODE (td, MINT_MONO_LDDOMAIN);
3881 td->sp [0].type = STACK_TYPE_I;
3886 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td->ip, td->ip-header->code);
3896 case CEE_PREFIXREF: ves_abort(); break;
3899 * Note: Exceptions thrown when executing a prefixed opcode need
3900 * to take into account the number of prefix bytes (usually the
3901 * throw point is just (ip - n_prefix_bytes).
3907 case CEE_ARGLIST: ves_abort(); break;
3911 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3912 ADD_CODE(td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3914 ADD_CODE(td, MINT_CEQ_I4 + td->sp [-1].type - STACK_TYPE_I4);
3916 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3921 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3922 ADD_CODE(td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3924 ADD_CODE(td, MINT_CGT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3926 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3931 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3932 ADD_CODE(td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3934 ADD_CODE(td, MINT_CGT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3936 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3941 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3942 ADD_CODE(td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3944 ADD_CODE(td, MINT_CLT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3946 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3951 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3952 ADD_CODE(td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3954 ADD_CODE(td, MINT_CLT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3956 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3959 case CEE_LDVIRTFTN: /* fallthrough */
3962 if (*td->ip == CEE_LDVIRTFTN) {
3963 CHECK_STACK (td, 1);
3966 token = read32 (td->ip + 1);
3967 if (method->wrapper_type != MONO_WRAPPER_NONE)
3968 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3970 m = mono_get_method_full (image, token, NULL, generic_context);
3972 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3973 m = mono_marshal_get_synchronized_wrapper (m);
3975 ADD_CODE(td, *td->ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3976 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
3977 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3979 PUSH_SIMPLE_TYPE (td, STACK_TYPE_F);
3983 load_arg (td, read16 (td->ip + 1));
3987 int n = read16 (td->ip + 1);
3988 ADD_CODE (td, MINT_LDARGA);
3989 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offsets */
3990 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3995 store_arg (td, read16 (td->ip + 1));
3999 load_local (td, read16 (td->ip + 1));
4003 ADD_CODE(td, MINT_LDLOCA_S);
4004 ADD_CODE(td, td->rtm->local_offsets [read16 (td->ip + 1)]);
4005 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
4009 store_local (td, read16 (td->ip + 1));
4013 CHECK_STACK (td, 1);
4014 #if SIZEOF_VOID_P == 8
4015 if (td->sp [-1].type == STACK_TYPE_I8)
4016 ADD_CODE(td, MINT_CONV_I4_I8);
4018 ADD_CODE(td, MINT_LOCALLOC);
4019 if (td->sp != td->stack + 1)
4020 g_warning("CEE_LOCALLOC: stack not empty");
4022 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
4025 case CEE_UNUSED57: ves_abort(); break;
4028 ADD_CODE (td, MINT_ENDFILTER);
4031 case CEE_UNALIGNED_:
4040 /* FIX: should do something? */;
4041 // TODO: This should raise a method_tail_call profiler event.
4045 token = read32 (td->ip + 1);
4046 klass = mini_get_class (method, token, generic_context);
4047 if (klass->valuetype) {
4048 ADD_CODE (td, MINT_INITOBJ);
4049 i32 = mono_class_value_size (klass, NULL);
4052 ADD_CODE (td, MINT_LDNULL);
4053 ADD_CODE (td, MINT_STIND_REF);
4060 /* FIX? convert length to I8? */
4062 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
4063 ADD_CODE(td, MINT_CPBLK);
4064 BARRIER_IF_VOLATILE (td);
4072 case CEE_CONSTRAINED_:
4073 token = read32 (td->ip + 1);
4074 constrained_class = mini_get_class (method, token, generic_context);
4075 mono_class_init (constrained_class);
4080 BARRIER_IF_VOLATILE (td);
4081 ADD_CODE(td, MINT_INITBLK);
4086 /* FIXME: implement */
4090 int clause_index = td->clause_indexes [in_offset];
4091 g_assert (clause_index != -1);
4092 SIMPLE_OP (td, MINT_RETHROW);
4093 ADD_CODE (td, rtm->exvar_offsets [clause_index]);
4094 generating_code = 0;
4099 token = read32 (td->ip + 1);
4101 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
4103 MonoType *type = mono_type_create_from_typespec (image, token);
4104 size = mono_type_size (type, &align);
4107 MonoClass *szclass = mini_get_class (method, token, generic_context);
4108 mono_class_init (szclass);
4110 if (!szclass->valuetype)
4111 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4113 size = mono_type_size (&szclass->byval_arg, &align);
4115 ADD_CODE(td, MINT_LDC_I4);
4117 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
4120 case CEE_REFANYTYPE:
4121 ADD_CODE (td, MINT_REFANYTYPE);
4123 POP_VT (td, sizeof (MonoTypedRef));
4124 PUSH_VT (td, sizeof (gpointer));
4125 SET_TYPE(td->sp - 1, STACK_TYPE_VT, NULL);
4128 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);
4132 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td->ip, td->ip-header->code);
4135 if (td->new_ip - td->new_code != new_in_start_offset)
4136 td->last_new_ip = td->new_code + new_in_start_offset;
4137 else if (td->is_bb_start [td->in_start - td->il_code])
4138 td->is_bb_start [td->ip - td->il_code] = 1;
4140 td->last_ip = td->in_start;
4143 /* Handle relocations */
4144 for (int i = 0; i < td->relocs->len; ++i) {
4145 Reloc *reloc = g_ptr_array_index (td->relocs, i);
4147 int offset = td->in_offsets [reloc->target] - reloc->offset;
4149 switch (reloc->type) {
4150 case RELOC_SHORT_BRANCH:
4151 g_assert (td->new_code [reloc->offset + 1] == 0xffff);
4152 td->new_code [reloc->offset + 1] = offset;
4154 case RELOC_LONG_BRANCH: {
4155 guint16 *v = (guint16 *) &offset;
4156 g_assert (td->new_code [reloc->offset + 1] == 0xbeef);
4157 g_assert (td->new_code [reloc->offset + 2] == 0xdead);
4158 td->new_code [reloc->offset + 1] = *(guint16 *) v;
4159 td->new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
4162 case RELOC_SWITCH: {
4163 guint16 *v = (guint16*)&offset;
4164 td->new_code [reloc->offset] = *(guint16*)v;
4165 td->new_code [reloc->offset + 1] = *(guint16*)(v + 1);
4169 g_assert_not_reached ();
4174 if (td->verbose_level) {
4175 const guint16 *p = td->new_code;
4176 printf("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td->max_vt_sp);
4177 printf("Calculated stack size: %d, stated size: %d\n", td->max_stack_height, header->max_stack);
4178 while (p < td->new_ip) {
4179 p = mono_interp_dis_mintop(td->new_code, p);
4183 g_assert (td->max_stack_height <= (header->max_stack + 1));
4185 int code_len = td->new_ip - td->new_code;
4187 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
4188 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
4189 rtm->code = mono_domain_alloc0 (domain, (td->new_ip - td->new_code) * sizeof (gushort));
4190 memcpy (rtm->code, td->new_code, (td->new_ip - td->new_code) * sizeof(gushort));
4191 g_free (td->new_code);
4192 rtm->new_body_start = rtm->code + body_start_offset;
4193 rtm->num_clauses = header->num_clauses;
4194 for (i = 0; i < header->num_clauses; i++) {
4195 MonoExceptionClause *c = rtm->clauses + i;
4196 int end_off = c->try_offset + c->try_len;
4197 c->try_offset = td->in_offsets [c->try_offset];
4198 c->try_len = td->in_offsets [end_off] - c->try_offset;
4199 end_off = c->handler_offset + c->handler_len;
4200 c->handler_offset = td->in_offsets [c->handler_offset];
4201 c->handler_len = td->in_offsets [end_off] - c->handler_offset;
4202 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
4203 c->data.filter_offset = td->in_offsets [c->data.filter_offset];
4205 rtm->vt_stack_size = td->max_vt_sp;
4206 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
4207 rtm->data_items = mono_domain_alloc0 (domain, td->n_data_items * sizeof (td->data_items [0]));
4208 memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
4210 /* Save debug info */
4211 interp_save_debug_info (rtm, header, td, line_numbers);
4213 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
4214 int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
4215 MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
4216 jinfo->is_interp = 1;
4218 mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
4219 for (i = 0; i < jinfo->num_clauses; ++i) {
4220 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
4221 MonoExceptionClause *c = rtm->clauses + i;
4223 ei->flags = c->flags;
4224 ei->try_start = (guint8*)(rtm->code + c->try_offset);
4225 ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
4226 ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
4227 ei->exvar_offset = rtm->exvar_offsets [i];
4228 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4229 ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
4230 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4232 ei->data.catch_class = c->data.catch_class;
4236 save_seq_points (td);
4238 g_free (td->in_offsets);
4239 for (i = 0; i < header->code_size; ++i)
4240 g_free (td->stack_state [i]);
4241 g_free (td->stack_state);
4242 g_free (td->stack_height);
4243 g_free (td->vt_stack_size);
4244 g_free (td->data_items);
4246 g_hash_table_destroy (td->data_hash);
4247 g_free (td->clause_indexes);
4248 g_ptr_array_free (td->seq_points, TRUE);
4249 g_array_free (line_numbers, TRUE);
4250 g_ptr_array_free (td->relocs, TRUE);
4251 mono_mempool_destroy (td->mempool);
4254 static mono_mutex_t calc_section;
4257 mono_interp_transform_init (void)
4259 mono_os_mutex_init_recursive(&calc_section);
4263 mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context)
4266 int i, align, size, offset;
4267 MonoMethod *method = imethod->method;
4268 MonoImage *image = method->klass->image;
4269 MonoMethodHeader *header = mono_method_get_header (method);
4270 MonoMethodSignature *signature = mono_method_signature (method);
4271 register const unsigned char *ip, *end;
4272 const MonoOpcode *opcode;
4275 unsigned char *is_bb_start;
4277 MonoVTable *method_class_vt;
4279 MonoGenericContext *generic_context = NULL;
4280 MonoDomain *domain = imethod->domain;
4282 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4283 method_class_vt = mono_class_vtable_full (domain, imethod->method->klass, &error);
4284 if (!is_ok (&error))
4285 return mono_error_convert_to_exception (&error);
4287 if (!method_class_vt->initialized) {
4289 InterpFrame *last_env_frame = context->env_frame;
4290 jmp_buf *old_env = context->current_env;
4291 error_init (&error);
4294 MonoException *failed = context->env_frame->ex;
4295 context->env_frame->ex = NULL;
4296 context->env_frame = last_env_frame;
4297 context->current_env = old_env;
4300 context->env_frame = context->current_frame;
4301 context->current_env = &env;
4302 mono_runtime_class_init_full (method_class_vt, &error);
4303 if (!mono_error_ok (&error)) {
4304 return mono_error_convert_to_exception (&error);
4306 context->env_frame = last_env_frame;
4307 context->current_env = old_env;
4310 MONO_PROFILER_RAISE (jit_begin, (method));
4312 if (mono_method_signature (method)->is_inflated)
4313 generic_context = mono_method_get_context (method);
4315 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
4316 if (generic_container)
4317 generic_context = &generic_container->context;
4320 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4321 MonoMethod *nm = NULL;
4322 mono_os_mutex_lock(&calc_section);
4323 if (imethod->transformed) {
4324 mono_os_mutex_unlock(&calc_section);
4325 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4329 /* assumes all internal calls with an array this are built in... */
4330 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
4331 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
4332 signature = mono_method_signature (nm);
4334 const char *name = method->name;
4335 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4336 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4337 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
4339 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
4340 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
4341 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4342 nm = mono_marshal_get_delegate_invoke (method, NULL);
4343 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4344 nm = mono_marshal_get_delegate_begin_invoke (method);
4345 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4346 nm = mono_marshal_get_delegate_end_invoke (method);
4350 imethod->code = g_malloc(sizeof(short));
4351 imethod->code[0] = MINT_CALLRUN;
4355 imethod->stack_size = sizeof (stackval); /* for tracing */
4356 imethod->alloca_size = imethod->stack_size;
4357 imethod->transformed = TRUE;
4358 mono_os_mutex_unlock(&calc_section);
4359 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4363 header = mono_method_get_header (nm);
4364 mono_os_mutex_unlock(&calc_section);
4365 } else if (method->klass == mono_defaults.array_class) {
4366 if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
4367 mono_os_mutex_lock (&calc_section);
4368 if (!imethod->transformed) {
4369 imethod->code = g_malloc (sizeof (short));
4370 imethod->code[0] = MINT_CALLRUN;
4371 imethod->stack_size = sizeof (stackval); /* for tracing */
4372 imethod->alloca_size = imethod->stack_size;
4373 imethod->transformed = TRUE;
4375 mono_os_mutex_unlock(&calc_section);
4376 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4378 } else if (!strcmp (method->name, "UnsafeStore")) {
4382 g_assert ((signature->param_count + signature->hasthis) < 1000);
4383 g_assert (header->max_stack < 10000);
4384 /* intern the strings in the method. */
4386 end = ip + header->code_size;
4388 is_bb_start = g_malloc0(header->code_size);
4389 is_bb_start [0] = 1;
4396 else if (in == 0xf0) {
4398 in = *ip + MONO_CEE_MONO_ICALL;
4400 opcode = &mono_opcodes [in];
4401 switch (opcode->argument) {
4402 case MonoInlineNone:
4405 case MonoInlineString:
4406 if (method->wrapper_type == MONO_WRAPPER_NONE)
4407 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
4410 case MonoInlineType:
4411 if (method->wrapper_type == MONO_WRAPPER_NONE) {
4412 class = mini_get_class (method, read32 (ip + 1), generic_context);
4413 mono_class_init (class);
4414 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
4416 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
4417 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
4418 mono_class_vtable (domain, class);
4423 case MonoInlineMethod:
4424 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
4425 m = mono_get_method_checked (image, read32 (ip + 1), NULL, generic_context, &error);
4426 if (!is_ok (&error)) {
4427 g_free (is_bb_start);
4428 return mono_error_convert_to_exception (&error);
4430 mono_class_init (m->klass);
4431 if (!mono_class_is_interface (m->klass))
4432 mono_class_vtable (domain, m->klass);
4436 case MonoInlineField:
4440 case MonoShortInlineR:
4443 case MonoInlineBrTarget:
4444 offset = read32 (ip + 1);
4446 backwards = offset < 0;
4447 offset += ip - header->code;
4448 g_assert (offset >= 0 && offset < header->code_size);
4449 is_bb_start [offset] |= backwards ? 2 : 1;
4451 case MonoShortInlineBrTarget:
4452 offset = ((gint8 *)ip) [1];
4454 backwards = offset < 0;
4455 offset += ip - header->code;
4456 g_assert (offset >= 0 && offset < header->code_size);
4457 is_bb_start [offset] |= backwards ? 2 : 1;
4462 case MonoShortInlineVar:
4463 case MonoShortInlineI:
4466 case MonoInlineSwitch: {
4468 const unsigned char *next_ip;
4472 next_ip = ip + 4 * n;
4473 for (i = 0; i < n; i++) {
4474 offset = read32 (ip);
4475 backwards = offset < 0;
4476 offset += next_ip - header->code;
4477 g_assert (offset >= 0 && offset < header->code_size);
4478 is_bb_start [offset] |= backwards ? 2 : 1;
4488 g_assert_not_reached ();
4491 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4493 /* the rest needs to be locked so it is only done once */
4494 mono_os_mutex_lock(&calc_section);
4495 if (imethod->transformed) {
4496 mono_os_mutex_unlock(&calc_section);
4497 g_free (is_bb_start);
4498 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4502 imethod->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
4503 imethod->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
4504 imethod->stack_size = (imethod->stack_size + 7) & ~7;
4506 for (i = 0; i < header->num_locals; ++i) {
4507 size = mono_type_size (header->locals [i], &align);
4508 offset += align - 1;
4509 offset &= ~(align - 1);
4510 imethod->local_offsets [i] = offset;
4513 offset = (offset + 7) & ~7;
4515 imethod->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
4516 for (i = 0; i < header->num_clauses; i++) {
4517 offset += sizeof (MonoObject*);
4518 imethod->exvar_offsets [i] = offset;
4520 offset = (offset + 7) & ~7;
4522 imethod->locals_size = offset;
4523 g_assert (imethod->locals_size < 65536);
4525 imethod->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
4527 if (signature->hasthis) {
4528 g_assert (!signature->pinvoke);
4529 size = mono_type_stack_size (&method->klass->byval_arg, &align);
4530 offset += align - 1;
4531 offset &= ~(align - 1);
4532 imethod->arg_offsets [0] = offset;
4536 for (i = 0; i < signature->param_count; ++i) {
4537 if (signature->pinvoke) {
4539 size = mono_type_native_stack_size (signature->params [i], &dummy);
4543 size = mono_type_stack_size (signature->params [i], &align);
4544 offset += align - 1;
4545 offset &= ~(align - 1);
4546 imethod->arg_offsets [i + !!signature->hasthis] = offset;
4549 offset = (offset + 7) & ~7;
4550 imethod->args_size = offset;
4551 g_assert (imethod->args_size < 10000);
4553 generate (method, imethod, is_bb_start, generic_context);
4555 g_free (is_bb_start);
4557 // FIXME: Add a different callback ?
4558 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4559 imethod->transformed = TRUE;
4560 mono_os_mutex_unlock(&calc_section);