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);
2509 g_assert_not_reached ();
2512 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2515 CHECK_STACK (td, 1);
2516 switch (td->sp [-1].type) {
2518 #if SIZEOF_VOID_P == 8
2519 ADD_CODE(td, MINT_CONV_I8_R8);
2521 ADD_CODE(td, MINT_CONV_I4_R8);
2525 #if SIZEOF_VOID_P == 8
2526 ADD_CODE(td, MINT_CONV_I8_I4);
2534 #if SIZEOF_VOID_P == 4
2535 ADD_CODE(td, MINT_CONV_I4_I8);
2539 g_assert_not_reached ();
2542 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
2545 CHECK_STACK (td, 1);
2546 switch (td->sp [-1].type) {
2548 ADD_CODE(td, MINT_CONV_U4_R8);
2553 ADD_CODE(td, MINT_CONV_U4_I8);
2556 #if SIZEOF_VOID_P == 8
2557 ADD_CODE(td, MINT_CONV_U4_I8);
2561 g_assert_not_reached ();
2564 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2567 CHECK_STACK (td, 1);
2568 switch (td->sp [-1].type) {
2570 ADD_CODE(td, MINT_CONV_I4_R8);
2575 ADD_CODE(td, MINT_CONV_I4_I8);
2578 #if SIZEOF_VOID_P == 8
2579 ADD_CODE(td, MINT_CONV_I4_I8);
2583 g_assert_not_reached ();
2586 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
2589 CHECK_STACK (td, 1);
2590 switch (td->sp [-1].type) {
2592 ADD_CODE(td, MINT_CONV_I8_R8);
2595 ADD_CODE(td, MINT_CONV_I8_I4);
2600 #if SIZEOF_VOID_P == 4
2601 ADD_CODE(td, MINT_CONV_I8_I4);
2605 g_assert_not_reached ();
2608 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2611 CHECK_STACK (td, 1);
2612 switch (td->sp [-1].type) {
2614 ADD_CODE(td, MINT_CONV_R4_R8);
2617 ADD_CODE(td, MINT_CONV_R4_I8);
2620 ADD_CODE(td, MINT_CONV_R4_I4);
2623 g_assert_not_reached ();
2626 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2629 CHECK_STACK (td, 1);
2630 switch (td->sp [-1].type) {
2632 ADD_CODE(td, MINT_CONV_R8_I4);
2635 ADD_CODE(td, MINT_CONV_R8_I8);
2640 g_assert_not_reached ();
2643 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2646 CHECK_STACK (td, 1);
2647 switch (td->sp [-1].type) {
2649 ADD_CODE(td, MINT_CONV_U8_I4);
2654 ADD_CODE(td, MINT_CONV_U8_R8);
2657 #if SIZEOF_VOID_P == 4
2658 ADD_CODE(td, MINT_CONV_U8_I4);
2662 g_assert_not_reached ();
2665 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
2668 CHECK_STACK (td, 2);
2670 token = read32 (td->ip + 1);
2671 klass = mono_class_get_full (image, token, generic_context);
2673 if (klass->valuetype) {
2674 ADD_CODE (td, MINT_CPOBJ);
2675 ADD_CODE (td, get_data_item_index(td, klass));
2677 ADD_CODE (td, MINT_LDIND_REF);
2678 ADD_CODE (td, MINT_STIND_REF);
2686 CHECK_STACK (td, 1);
2688 token = read32 (td->ip + 1);
2690 if (method->wrapper_type != MONO_WRAPPER_NONE)
2691 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2693 klass = mono_class_get_full (image, token, generic_context);
2695 ADD_CODE(td, MINT_LDOBJ);
2696 ADD_CODE(td, get_data_item_index(td, klass));
2697 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2698 size = mono_class_value_size (klass, NULL);
2702 SET_TYPE(td->sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
2703 BARRIER_IF_VOLATILE (td);
2708 token = mono_metadata_token_index (read32 (td->ip + 1));
2710 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
2711 s = mono_method_get_wrapper_data (method, token);
2712 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
2713 s = mono_string_new_wrapper (mono_method_get_wrapper_data (method, token));
2715 s = mono_ldstr (domain, image, token);
2717 ADD_CODE(td, MINT_LDSTR);
2718 ADD_CODE(td, get_data_item_index (td, s));
2719 PUSH_TYPE(td, STACK_TYPE_O, mono_defaults.string_class);
2724 MonoMethodSignature *csignature;
2725 guint32 vt_stack_used = 0;
2726 guint32 vt_res_size = 0;
2729 token = read32 (td->ip);
2732 if (method->wrapper_type != MONO_WRAPPER_NONE)
2733 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2735 m = mono_get_method_full (image, token, NULL, generic_context);
2737 csignature = mono_method_signature (m);
2740 td->sp -= csignature->param_count;
2741 if (mono_class_is_magic_int (klass) || mono_class_is_magic_float (klass)) {
2742 ADD_CODE (td, MINT_NEWOBJ_MAGIC);
2743 ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2744 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2746 ADD_CODE(td, MINT_NEWOBJ);
2747 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
2748 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2750 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
2751 vt_res_size = mono_class_value_size (klass, NULL);
2752 PUSH_VT (td, vt_res_size);
2754 for (i = 0; i < csignature->param_count; ++i) {
2755 int mt = mint_type(csignature->params [i]);
2756 if (mt == MINT_TYPE_VT) {
2757 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
2758 gint32 size = mono_class_value_size (k, NULL);
2759 size = (size + 7) & ~7;
2760 vt_stack_used += size;
2763 if (vt_stack_used != 0 || vt_res_size != 0) {
2764 ADD_CODE(td, MINT_VTRESULT);
2765 ADD_CODE(td, vt_res_size);
2766 WRITE32(td, &vt_stack_used);
2767 td->vt_sp -= vt_stack_used;
2769 PUSH_TYPE (td, stack_type [mint_type (&klass->byval_arg)], klass);
2774 CHECK_STACK (td, 1);
2775 token = read32 (td->ip + 1);
2776 klass = mini_get_class (method, token, generic_context);
2777 ADD_CODE(td, MINT_CASTCLASS);
2778 ADD_CODE(td, get_data_item_index (td, klass));
2779 td->sp [-1].klass = klass;
2783 CHECK_STACK (td, 1);
2784 token = read32 (td->ip + 1);
2785 klass = mini_get_class (method, token, generic_context);
2786 ADD_CODE(td, MINT_ISINST);
2787 ADD_CODE(td, get_data_item_index (td, klass));
2791 switch (td->sp [-1].type) {
2795 ADD_CODE(td, MINT_CONV_R_UN_I8);
2798 ADD_CODE(td, MINT_CONV_R_UN_I4);
2801 g_assert_not_reached ();
2803 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
2807 CHECK_STACK (td, 1);
2808 token = read32 (td->ip + 1);
2810 if (method->wrapper_type != MONO_WRAPPER_NONE)
2811 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2813 klass = mono_class_get_full (image, token, generic_context);
2815 if (mono_class_is_nullable (klass)) {
2816 g_error ("cee_unbox: implement Nullable");
2819 ADD_CODE(td, MINT_UNBOX);
2820 ADD_CODE(td, get_data_item_index (td, klass));
2821 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2825 CHECK_STACK (td, 1);
2826 token = read32 (td->ip + 1);
2828 klass = mini_get_class (method, token, generic_context);
2830 if (mini_type_is_reference (&klass->byval_arg)) {
2831 int mt = mint_type (&klass->byval_arg);
2832 ADD_CODE (td, MINT_CASTCLASS);
2833 ADD_CODE (td, get_data_item_index (td, klass));
2834 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2836 } else if (mono_class_is_nullable (klass)) {
2837 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2838 /* td->ip is incremented by interp_transform_call */
2839 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2841 int mt = mint_type (&klass->byval_arg);
2842 ADD_CODE (td, MINT_UNBOX);
2843 ADD_CODE (td, get_data_item_index (td, klass));
2845 ADD_CODE (td, MINT_LDOBJ);
2846 ADD_CODE (td, get_data_item_index(td, klass));
2847 SET_TYPE (td->sp - 1, stack_type [mt], klass);
2849 if (mt == MINT_TYPE_VT) {
2850 int size = mono_class_value_size (klass, NULL);
2858 CHECK_STACK (td, 1);
2859 SIMPLE_OP (td, MINT_THROW);
2861 generating_code = 0;
2864 CHECK_STACK (td, 1);
2865 token = read32 (td->ip + 1);
2866 field = interp_field_from_token (method, token, &klass, generic_context);
2867 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2868 mono_class_init (klass);
2870 ADD_CODE (td, MINT_POP);
2872 ADD_CODE (td, MINT_LDSFLDA);
2873 ADD_CODE (td, get_data_item_index (td, field));
2875 if ((td->sp - 1)->type == STACK_TYPE_O) {
2876 ADD_CODE (td, MINT_LDFLDA);
2878 g_assert ((td->sp -1)->type == STACK_TYPE_MP);
2879 ADD_CODE (td, MINT_LDFLDA_UNSAFE);
2881 ADD_CODE (td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2884 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
2887 CHECK_STACK (td, 1);
2888 token = read32 (td->ip + 1);
2889 field = interp_field_from_token (method, token, &klass, generic_context);
2890 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2891 mono_class_init (klass);
2893 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2894 mt = mint_type (&field_klass->byval_arg);
2895 #ifndef DISABLE_REMOTING
2896 if (klass->marshalbyref) {
2897 g_assert (!is_static);
2898 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2899 ADD_CODE(td, get_data_item_index (td, field));
2904 ADD_CODE (td, MINT_POP);
2906 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2907 ADD_CODE (td, get_data_item_index (td, field));
2909 ADD_CODE (td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2910 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2913 if (mt == MINT_TYPE_VT) {
2914 int size = mono_class_value_size (field_klass, NULL);
2918 if (td->sp [-1].type == STACK_TYPE_VT) {
2919 int size = mono_class_value_size (klass, NULL);
2920 size = (size + 7) & ~7;
2922 ADD_CODE (td, MINT_VTRESULT);
2924 WRITE32 (td, &size);
2927 SET_TYPE(td->sp - 1, stack_type [mt], field_klass);
2928 BARRIER_IF_VOLATILE (td);
2932 CHECK_STACK (td, 2);
2933 token = read32 (td->ip + 1);
2934 field = interp_field_from_token (method, token, &klass, generic_context);
2935 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2936 mono_class_init (klass);
2937 mt = mint_type(field->type);
2939 BARRIER_IF_VOLATILE (td);
2941 #ifndef DISABLE_REMOTING
2942 if (klass->marshalbyref) {
2943 g_assert (!is_static);
2944 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2945 ADD_CODE(td, get_data_item_index (td, field));
2950 ADD_CODE (td, MINT_POP);
2952 ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2953 ADD_CODE (td, get_data_item_index (td, field));
2955 ADD_CODE (td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2956 ADD_CODE (td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2959 if (mt == MINT_TYPE_VT) {
2960 MonoClass *klass = mono_class_from_mono_type (field->type);
2961 int size = mono_class_value_size (klass, NULL);
2970 token = read32 (td->ip + 1);
2971 field = interp_field_from_token (method, token, &klass, generic_context);
2972 ADD_CODE(td, MINT_LDSFLDA);
2973 ADD_CODE(td, get_data_item_index (td, field));
2975 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
2978 token = read32 (td->ip + 1);
2979 field = interp_field_from_token (method, token, &klass, generic_context);
2980 mt = mint_type(field->type);
2981 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2982 ADD_CODE(td, get_data_item_index (td, field));
2984 if (mt == MINT_TYPE_VT) {
2985 MonoClass *klass = mono_class_from_mono_type (field->type);
2986 int size = mono_class_value_size (klass, NULL);
2989 klass = field->type->data.klass;
2991 if (mt == MINT_TYPE_O)
2992 klass = mono_class_from_mono_type (field->type);
2995 PUSH_TYPE(td, stack_type [mt], klass);
2998 CHECK_STACK (td, 1);
2999 token = read32 (td->ip + 1);
3000 field = interp_field_from_token (method, token, &klass, generic_context);
3001 mt = mint_type(field->type);
3002 ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
3003 ADD_CODE(td, get_data_item_index (td, field));
3004 if (mt == MINT_TYPE_VT) {
3005 MonoClass *klass = mono_class_from_mono_type (field->type);
3006 int size = mono_class_value_size (klass, NULL);
3008 WRITE32 (td, &size);
3015 token = read32 (td->ip + 1);
3017 if (method->wrapper_type != MONO_WRAPPER_NONE)
3018 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3020 klass = mini_get_class (method, token, generic_context);
3022 BARRIER_IF_VOLATILE (td);
3023 ADD_CODE(td, td->sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
3024 ADD_CODE(td, get_data_item_index (td, klass));
3025 if (td->sp [-1].type == STACK_TYPE_VT) {
3026 size = mono_class_value_size (klass, NULL);
3027 size = (size + 7) & ~7;
3034 case CEE_CONV_OVF_I_UN:
3035 case CEE_CONV_OVF_U_UN:
3036 CHECK_STACK (td, 1);
3037 switch (td->sp [-1].type) {
3039 #if SIZEOF_VOID_P == 8
3040 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
3042 ADD_CODE(td, MINT_CONV_OVF_I4_UN_R8);
3046 #if SIZEOF_VOID_P == 4
3047 ADD_CODE (td, MINT_CONV_OVF_I4_UN_I8);
3051 #if SIZEOF_VOID_P == 8
3052 ADD_CODE(td, MINT_CONV_I8_U4);
3053 #elif SIZEOF_VOID_P == 4
3054 if (*td->ip == CEE_CONV_OVF_I_UN)
3055 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3059 g_assert_not_reached ();
3062 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3065 case CEE_CONV_OVF_I8_UN:
3066 case CEE_CONV_OVF_U8_UN:
3067 CHECK_STACK (td, 1);
3068 switch (td->sp [-1].type) {
3070 ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
3073 if (*td->ip == CEE_CONV_OVF_I8_UN)
3074 ADD_CODE (td, MINT_CONV_OVF_I8_U8);
3077 ADD_CODE(td, MINT_CONV_I8_U4);
3080 g_assert_not_reached ();
3083 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3088 CHECK_STACK (td, 1);
3089 token = read32 (td->ip + 1);
3090 if (method->wrapper_type != MONO_WRAPPER_NONE)
3091 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3093 klass = mini_get_class (method, token, generic_context);
3095 if (mono_class_is_nullable (klass)) {
3096 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
3097 /* td->ip is incremented by interp_transform_call */
3098 interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3099 } else if (!klass->valuetype) {
3100 /* already boxed, do nothing. */
3103 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
3104 size = mono_class_value_size (klass, NULL);
3105 size = (size + 7) & ~7;
3108 ADD_CODE(td, MINT_BOX);
3109 ADD_CODE(td, get_data_item_index (td, klass));
3111 SET_TYPE(td->sp - 1, STACK_TYPE_O, klass);
3118 CHECK_STACK (td, 1);
3119 token = read32 (td->ip + 1);
3121 if (method->wrapper_type != MONO_WRAPPER_NONE)
3122 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3124 klass = mini_get_class (method, token, generic_context);
3126 unsigned char lentype = (td->sp - 1)->type;
3127 if (lentype == STACK_TYPE_I8) {
3128 /* mimic mini behaviour */
3129 ADD_CODE (td, MINT_CONV_OVF_U4_I8);
3131 g_assert (lentype == STACK_TYPE_I4);
3132 ADD_CODE (td, MINT_CONV_OVF_U4_I4);
3134 SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
3135 ADD_CODE (td, MINT_NEWARR);
3136 ADD_CODE (td, get_data_item_index (td, klass));
3137 SET_TYPE (td->sp - 1, STACK_TYPE_O, klass);
3142 CHECK_STACK (td, 1);
3143 SIMPLE_OP (td, MINT_LDLEN);
3144 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3147 CHECK_STACK (td, 2);
3149 token = read32 (td->ip + 1);
3151 if (method->wrapper_type != MONO_WRAPPER_NONE)
3152 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
3154 klass = mini_get_class (method, token, generic_context);
3156 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
3157 ADD_CODE (td, MINT_LDELEMA_TC);
3159 ADD_CODE (td, MINT_LDELEMA);
3161 ADD_CODE (td, get_data_item_index (td, klass));
3162 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
3168 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3171 CHECK_STACK (td, 2);
3173 SIMPLE_OP (td, MINT_LDELEM_I1);
3175 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3178 CHECK_STACK (td, 2);
3180 SIMPLE_OP (td, MINT_LDELEM_U1);
3182 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3185 CHECK_STACK (td, 2);
3187 SIMPLE_OP (td, MINT_LDELEM_I2);
3189 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3192 CHECK_STACK (td, 2);
3194 SIMPLE_OP (td, MINT_LDELEM_U2);
3196 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3199 CHECK_STACK (td, 2);
3201 SIMPLE_OP (td, MINT_LDELEM_I4);
3203 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3206 CHECK_STACK (td, 2);
3208 SIMPLE_OP (td, MINT_LDELEM_U4);
3210 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3213 CHECK_STACK (td, 2);
3215 SIMPLE_OP (td, MINT_LDELEM_I8);
3217 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3220 CHECK_STACK (td, 2);
3222 SIMPLE_OP (td, MINT_LDELEM_I);
3224 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3227 CHECK_STACK (td, 2);
3229 SIMPLE_OP (td, MINT_LDELEM_R4);
3231 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3234 CHECK_STACK (td, 2);
3236 SIMPLE_OP (td, MINT_LDELEM_R8);
3238 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3240 case CEE_LDELEM_REF:
3241 CHECK_STACK (td, 2);
3243 SIMPLE_OP (td, MINT_LDELEM_REF);
3245 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3248 CHECK_STACK (td, 2);
3249 token = read32 (td->ip + 1);
3250 klass = mini_get_class (method, token, generic_context);
3251 switch (mint_type (&klass->byval_arg)) {
3254 SIMPLE_OP (td, MINT_LDELEM_I1);
3256 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3260 SIMPLE_OP (td, MINT_LDELEM_U1);
3262 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3266 SIMPLE_OP (td, MINT_LDELEM_U2);
3268 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3272 SIMPLE_OP (td, MINT_LDELEM_I2);
3274 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3278 SIMPLE_OP (td, MINT_LDELEM_I4);
3280 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3284 SIMPLE_OP (td, MINT_LDELEM_I8);
3286 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3290 SIMPLE_OP (td, MINT_LDELEM_R4);
3292 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3296 SIMPLE_OP (td, MINT_LDELEM_R8);
3298 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
3302 SIMPLE_OP (td, MINT_LDELEM_REF);
3304 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
3306 case MINT_TYPE_VT: {
3307 int size = mono_class_value_size (klass, NULL);
3309 SIMPLE_OP (td, MINT_LDELEM_VT);
3310 ADD_CODE (td, get_data_item_index (td, klass));
3311 WRITE32 (td, &size);
3313 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_VT);
3318 GString *res = g_string_new ("");
3319 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3320 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3321 g_string_free (res, TRUE);
3329 CHECK_STACK (td, 3);
3331 SIMPLE_OP (td, MINT_STELEM_I);
3335 CHECK_STACK (td, 3);
3337 SIMPLE_OP (td, MINT_STELEM_I1);
3341 CHECK_STACK (td, 3);
3343 SIMPLE_OP (td, MINT_STELEM_I2);
3347 CHECK_STACK (td, 3);
3349 SIMPLE_OP (td, MINT_STELEM_I4);
3353 CHECK_STACK (td, 3);
3355 SIMPLE_OP (td, MINT_STELEM_I8);
3359 CHECK_STACK (td, 3);
3361 SIMPLE_OP (td, MINT_STELEM_R4);
3365 CHECK_STACK (td, 3);
3367 SIMPLE_OP (td, MINT_STELEM_R8);
3370 case CEE_STELEM_REF:
3371 CHECK_STACK (td, 3);
3373 SIMPLE_OP (td, MINT_STELEM_REF);
3377 CHECK_STACK (td, 3);
3379 token = read32 (td->ip + 1);
3380 klass = mini_get_class (method, token, generic_context);
3381 switch (mint_type (&klass->byval_arg)) {
3383 SIMPLE_OP (td, MINT_STELEM_U1);
3386 SIMPLE_OP (td, MINT_STELEM_U2);
3389 SIMPLE_OP (td, MINT_STELEM_I4);
3392 SIMPLE_OP (td, MINT_STELEM_I8);
3395 SIMPLE_OP (td, MINT_STELEM_REF);
3397 case MINT_TYPE_VT: {
3398 int size = mono_class_value_size (klass, NULL);
3399 SIMPLE_OP (td, MINT_STELEM_VT);
3400 ADD_CODE (td, get_data_item_index (td, klass));
3401 WRITE32 (td, &size);
3406 GString *res = g_string_new ("");
3407 mono_type_get_desc (res, &klass->byval_arg, TRUE);
3408 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
3409 g_string_free (res, TRUE);
3418 case CEE_CONV_OVF_U1:
3420 case CEE_CONV_OVF_I8:
3422 #if SIZEOF_VOID_P == 8
3423 case CEE_CONV_OVF_U:
3427 CHECK_STACK (td, 1);
3428 SIMPLE_OP (td, MINT_CKFINITE);
3431 CHECK_STACK (td, 1);
3433 token = read32 (td->ip + 1);
3434 klass = mini_get_class (method, token, generic_context);
3436 ADD_CODE (td, MINT_MKREFANY);
3437 ADD_CODE (td, get_data_item_index (td, klass));
3440 PUSH_VT (td, sizeof (MonoTypedRef));
3441 SET_TYPE(td->sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
3443 case CEE_REFANYVAL: {
3444 CHECK_STACK (td, 1);
3446 token = read32 (td->ip + 1);
3448 ADD_CODE (td, MINT_REFANYVAL);
3450 POP_VT (td, sizeof (MonoTypedRef));
3451 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3456 case CEE_CONV_OVF_I1:
3457 case CEE_CONV_OVF_I1_UN:
3458 CHECK_STACK (td, 1);
3459 switch (td->sp [-1].type) {
3461 ADD_CODE(td, MINT_CONV_OVF_I1_R8);
3464 ADD_CODE(td, MINT_CONV_OVF_I1_I4);
3467 ADD_CODE(td, MINT_CONV_OVF_I1_I8);
3470 g_assert_not_reached ();
3473 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3475 case CEE_CONV_OVF_U1:
3476 case CEE_CONV_OVF_U1_UN:
3477 CHECK_STACK (td, 1);
3478 switch (td->sp [-1].type) {
3480 ADD_CODE(td, MINT_CONV_OVF_U1_R8);
3483 ADD_CODE(td, MINT_CONV_OVF_U1_I4);
3486 ADD_CODE(td, MINT_CONV_OVF_U1_I8);
3489 g_assert_not_reached ();
3492 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3494 case CEE_CONV_OVF_I2:
3495 case CEE_CONV_OVF_I2_UN:
3496 CHECK_STACK (td, 1);
3497 switch (td->sp [-1].type) {
3499 ADD_CODE(td, MINT_CONV_OVF_I2_R8);
3502 ADD_CODE(td, MINT_CONV_OVF_I2_I4);
3505 ADD_CODE(td, MINT_CONV_OVF_I2_I8);
3508 g_assert_not_reached ();
3511 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3513 case CEE_CONV_OVF_U2_UN:
3514 case CEE_CONV_OVF_U2:
3515 CHECK_STACK (td, 1);
3516 switch (td->sp [-1].type) {
3518 ADD_CODE(td, MINT_CONV_OVF_U2_R8);
3521 ADD_CODE(td, MINT_CONV_OVF_U2_I4);
3524 ADD_CODE(td, MINT_CONV_OVF_U2_I8);
3527 g_assert_not_reached ();
3530 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3532 #if SIZEOF_VOID_P == 4
3533 case CEE_CONV_OVF_I:
3535 case CEE_CONV_OVF_I4:
3536 case CEE_CONV_OVF_I4_UN:
3537 CHECK_STACK (td, 1);
3538 switch (td->sp [-1].type) {
3540 ADD_CODE(td, MINT_CONV_OVF_I4_R8);
3543 if (*td->ip == CEE_CONV_OVF_I4_UN)
3544 ADD_CODE(td, MINT_CONV_OVF_I4_U4);
3547 if (*td->ip == CEE_CONV_OVF_I4_UN)
3548 ADD_CODE (td, MINT_CONV_OVF_I4_U8);
3550 ADD_CODE (td, MINT_CONV_OVF_I4_I8);
3553 g_assert_not_reached ();
3556 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3558 #if SIZEOF_VOID_P == 4
3559 case CEE_CONV_OVF_U:
3561 case CEE_CONV_OVF_U4:
3562 case CEE_CONV_OVF_U4_UN:
3563 CHECK_STACK (td, 1);
3564 switch (td->sp [-1].type) {
3566 ADD_CODE(td, MINT_CONV_OVF_U4_R8);
3569 if (*td->ip != CEE_CONV_OVF_U4_UN)
3570 ADD_CODE(td, MINT_CONV_OVF_U4_I4);
3573 ADD_CODE(td, MINT_CONV_OVF_U4_I8);
3576 g_assert_not_reached ();
3579 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3581 #if SIZEOF_VOID_P == 8
3582 case CEE_CONV_OVF_I:
3584 case CEE_CONV_OVF_I8:
3585 CHECK_STACK (td, 1);
3586 switch (td->sp [-1].type) {
3588 ADD_CODE(td, MINT_CONV_OVF_I8_R8);
3591 ADD_CODE(td, MINT_CONV_I8_I4);
3596 g_assert_not_reached ();
3599 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3601 #if SIZEOF_VOID_P == 8
3602 case CEE_CONV_OVF_U:
3604 case CEE_CONV_OVF_U8:
3605 CHECK_STACK (td, 1);
3606 switch (td->sp [-1].type) {
3608 ADD_CODE(td, MINT_CONV_OVF_U8_R8);
3611 ADD_CODE(td, MINT_CONV_OVF_U8_I4);
3614 ADD_CODE (td, MINT_CONV_OVF_U8_I8);
3617 g_assert_not_reached ();
3620 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
3625 token = read32 (td->ip + 1);
3626 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3627 handle = mono_method_get_wrapper_data (method, token);
3628 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
3629 if (klass == mono_defaults.typehandle_class)
3630 handle = &((MonoClass *) handle)->byval_arg;
3632 if (generic_context) {
3633 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
3634 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3637 handle = mono_ldtoken (image, token, &klass, generic_context);
3639 mono_class_init (klass);
3640 mt = mint_type (&klass->byval_arg);
3641 g_assert (mt == MINT_TYPE_VT);
3642 size = mono_class_value_size (klass, NULL);
3643 g_assert (size == sizeof(gpointer));
3644 PUSH_VT (td, sizeof(gpointer));
3645 ADD_CODE (td, MINT_LDTOKEN);
3646 ADD_CODE (td, get_data_item_index (td, handle));
3648 SET_TYPE (td->sp, stack_type [mt], klass);
3654 binary_arith_op(td, MINT_ADD_OVF_I4);
3657 case CEE_ADD_OVF_UN:
3658 binary_arith_op(td, MINT_ADD_OVF_UN_I4);
3662 binary_arith_op(td, MINT_MUL_OVF_I4);
3665 case CEE_MUL_OVF_UN:
3666 binary_arith_op(td, MINT_MUL_OVF_UN_I4);
3670 binary_arith_op(td, MINT_SUB_OVF_I4);
3673 case CEE_SUB_OVF_UN:
3674 binary_arith_op(td, MINT_SUB_OVF_UN_I4);
3677 case CEE_ENDFINALLY:
3678 g_assert (td->clause_indexes [in_offset] != -1);
3680 SIMPLE_OP (td, MINT_ENDFINALLY);
3681 ADD_CODE (td, td->clause_indexes [in_offset]);
3682 generating_code = 0;
3686 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td->ip + 1));
3688 generating_code = 0;
3692 handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td->ip [1]);
3694 generating_code = 0;
3699 case CEE_MONO_CALLI_EXTRA_ARG:
3700 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
3701 ADD_CODE (td, MINT_POP);
3704 interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
3706 case CEE_MONO_JIT_ICALL_ADDR: {
3709 MonoJitICallInfo *info;
3711 token = read32 (td->ip + 1);
3713 func = mono_method_get_wrapper_data (method, token);
3714 info = mono_find_jit_icall_by_addr (func);
3716 ADD_CODE (td, MINT_LDFTN);
3717 ADD_CODE (td, get_data_item_index (td, func));
3718 PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
3721 case CEE_MONO_ICALL: {
3724 MonoJitICallInfo *info;
3726 token = read32 (td->ip + 1);
3728 func = mono_method_get_wrapper_data (method, token);
3729 info = mono_find_jit_icall_by_addr (func);
3732 CHECK_STACK (td, info->sig->param_count);
3733 switch (info->sig->param_count) {
3735 if (MONO_TYPE_IS_VOID (info->sig->ret))
3736 ADD_CODE (td,MINT_ICALL_V_V);
3738 ADD_CODE (td, MINT_ICALL_V_P);
3741 if (MONO_TYPE_IS_VOID (info->sig->ret))
3742 ADD_CODE (td,MINT_ICALL_P_V);
3744 ADD_CODE (td,MINT_ICALL_P_P);
3747 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
3748 if (info->sig->params [1]->type == MONO_TYPE_I4)
3749 ADD_CODE (td,MINT_ICALL_PI_V);
3751 ADD_CODE (td,MINT_ICALL_PP_V);
3753 if (info->sig->params [1]->type == MONO_TYPE_I4)
3754 ADD_CODE (td,MINT_ICALL_PI_P);
3756 ADD_CODE (td,MINT_ICALL_PP_P);
3760 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
3761 if (info->sig->params [2]->type == MONO_TYPE_I4)
3762 ADD_CODE (td,MINT_ICALL_PPI_V);
3764 ADD_CODE (td,MINT_ICALL_PPP_V);
3767 g_assert_not_reached ();
3770 if (func == mono_ftnptr_to_delegate) {
3771 g_error ("TODO: ?");
3773 ADD_CODE(td, get_data_item_index (td, func));
3774 td->sp -= info->sig->param_count;
3776 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3778 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
3782 case CEE_MONO_VTADDR: {
3784 CHECK_STACK (td, 1);
3785 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
3786 size = mono_class_native_size(td->sp [-1].klass, NULL);
3788 size = mono_class_value_size(td->sp [-1].klass, NULL);
3789 size = (size + 7) & ~7;
3790 ADD_CODE(td, MINT_VTRESULT);
3795 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3798 case CEE_MONO_LDPTR:
3799 case CEE_MONO_CLASSCONST:
3800 token = read32 (td->ip + 1);
3802 ADD_CODE(td, MINT_MONO_LDPTR);
3803 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3804 td->sp [0].type = STACK_TYPE_I;
3807 case CEE_MONO_OBJADDR:
3808 CHECK_STACK (td, 1);
3810 td->sp[-1].type = STACK_TYPE_MP;
3813 case CEE_MONO_NEWOBJ:
3814 token = read32 (td->ip + 1);
3816 ADD_CODE(td, MINT_MONO_NEWOBJ);
3817 ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
3818 td->sp [0].type = STACK_TYPE_O;
3821 case CEE_MONO_RETOBJ:
3822 CHECK_STACK (td, 1);
3823 token = read32 (td->ip + 1);
3825 ADD_CODE(td, MINT_MONO_RETOBJ);
3828 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3830 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
3832 if (td->sp > td->stack)
3833 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td->sp-td->stack);
3835 case CEE_MONO_LDNATIVEOBJ:
3836 token = read32 (td->ip + 1);
3838 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3839 g_assert(klass->valuetype);
3840 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
3842 case CEE_MONO_TLS: {
3843 gint32 key = read32 (td->ip + 1);
3845 g_assert (key < TLS_KEY_NUM);
3846 ADD_CODE (td, MINT_MONO_TLS);
3848 PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
3851 case CEE_MONO_ATOMIC_STORE_I4:
3852 CHECK_STACK (td, 2);
3853 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3857 case CEE_MONO_SAVE_LMF:
3858 case CEE_MONO_RESTORE_LMF:
3859 case CEE_MONO_NOT_TAKEN:
3862 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3863 ADD_CODE (td, MINT_MONO_LDPTR);
3864 ADD_CODE (td, get_data_item_index (td, mono_thread_interruption_request_flag ()));
3865 PUSH_TYPE (td, STACK_TYPE_MP, NULL);
3868 case CEE_MONO_MEMORY_BARRIER:
3869 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
3872 case CEE_MONO_JIT_ATTACH:
3873 ADD_CODE (td, MINT_MONO_JIT_ATTACH);
3876 case CEE_MONO_JIT_DETACH:
3877 ADD_CODE (td, MINT_MONO_JIT_DETACH);
3880 case CEE_MONO_LDDOMAIN:
3881 ADD_CODE (td, MINT_MONO_LDDOMAIN);
3882 td->sp [0].type = STACK_TYPE_I;
3887 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td->ip, td->ip-header->code);
3897 case CEE_PREFIXREF: ves_abort(); break;
3900 * Note: Exceptions thrown when executing a prefixed opcode need
3901 * to take into account the number of prefix bytes (usually the
3902 * throw point is just (ip - n_prefix_bytes).
3908 case CEE_ARGLIST: ves_abort(); break;
3912 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3913 ADD_CODE(td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3915 ADD_CODE(td, MINT_CEQ_I4 + td->sp [-1].type - STACK_TYPE_I4);
3917 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3922 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3923 ADD_CODE(td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3925 ADD_CODE(td, MINT_CGT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3927 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3932 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3933 ADD_CODE(td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3935 ADD_CODE(td, MINT_CGT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3937 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3942 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3943 ADD_CODE(td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3945 ADD_CODE(td, MINT_CLT_I4 + td->sp [-1].type - STACK_TYPE_I4);
3947 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3952 if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
3953 ADD_CODE(td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3955 ADD_CODE(td, MINT_CLT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
3957 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
3960 case CEE_LDVIRTFTN: /* fallthrough */
3963 if (*td->ip == CEE_LDVIRTFTN) {
3964 CHECK_STACK (td, 1);
3967 token = read32 (td->ip + 1);
3968 if (method->wrapper_type != MONO_WRAPPER_NONE)
3969 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3971 m = mono_get_method_full (image, token, NULL, generic_context);
3973 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3974 m = mono_marshal_get_synchronized_wrapper (m);
3976 ADD_CODE(td, *td->ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3977 ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, &error)));
3978 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3980 PUSH_SIMPLE_TYPE (td, STACK_TYPE_F);
3984 load_arg (td, read16 (td->ip + 1));
3988 int n = read16 (td->ip + 1);
3989 ADD_CODE (td, MINT_LDARGA);
3990 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offsets */
3991 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
3996 store_arg (td, read16 (td->ip + 1));
4000 load_local (td, read16 (td->ip + 1));
4004 ADD_CODE(td, MINT_LDLOCA_S);
4005 ADD_CODE(td, td->rtm->local_offsets [read16 (td->ip + 1)]);
4006 PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
4010 store_local (td, read16 (td->ip + 1));
4014 CHECK_STACK (td, 1);
4015 #if SIZEOF_VOID_P == 8
4016 if (td->sp [-1].type == STACK_TYPE_I8)
4017 ADD_CODE(td, MINT_CONV_I4_I8);
4019 ADD_CODE(td, MINT_LOCALLOC);
4020 if (td->sp != td->stack + 1)
4021 g_warning("CEE_LOCALLOC: stack not empty");
4023 SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
4026 case CEE_UNUSED57: ves_abort(); break;
4029 ADD_CODE (td, MINT_ENDFILTER);
4032 case CEE_UNALIGNED_:
4041 /* FIX: should do something? */;
4042 // TODO: This should raise a method_tail_call profiler event.
4046 token = read32 (td->ip + 1);
4047 klass = mini_get_class (method, token, generic_context);
4048 if (klass->valuetype) {
4049 ADD_CODE (td, MINT_INITOBJ);
4050 i32 = mono_class_value_size (klass, NULL);
4053 ADD_CODE (td, MINT_LDNULL);
4054 ADD_CODE (td, MINT_STIND_REF);
4061 /* FIX? convert length to I8? */
4063 ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
4064 ADD_CODE(td, MINT_CPBLK);
4065 BARRIER_IF_VOLATILE (td);
4073 case CEE_CONSTRAINED_:
4074 token = read32 (td->ip + 1);
4075 constrained_class = mini_get_class (method, token, generic_context);
4076 mono_class_init (constrained_class);
4081 BARRIER_IF_VOLATILE (td);
4082 ADD_CODE(td, MINT_INITBLK);
4087 /* FIXME: implement */
4091 int clause_index = td->clause_indexes [in_offset];
4092 g_assert (clause_index != -1);
4093 SIMPLE_OP (td, MINT_RETHROW);
4094 ADD_CODE (td, rtm->exvar_offsets [clause_index]);
4095 generating_code = 0;
4100 token = read32 (td->ip + 1);
4102 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
4104 MonoType *type = mono_type_create_from_typespec (image, token);
4105 size = mono_type_size (type, &align);
4108 MonoClass *szclass = mini_get_class (method, token, generic_context);
4109 mono_class_init (szclass);
4111 if (!szclass->valuetype)
4112 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4114 size = mono_type_size (&szclass->byval_arg, &align);
4116 ADD_CODE(td, MINT_LDC_I4);
4118 PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
4121 case CEE_REFANYTYPE:
4122 ADD_CODE (td, MINT_REFANYTYPE);
4124 POP_VT (td, sizeof (MonoTypedRef));
4125 PUSH_VT (td, sizeof (gpointer));
4126 SET_TYPE(td->sp - 1, STACK_TYPE_VT, NULL);
4129 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);
4133 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td->ip, td->ip-header->code);
4136 if (td->new_ip - td->new_code != new_in_start_offset)
4137 td->last_new_ip = td->new_code + new_in_start_offset;
4138 else if (td->is_bb_start [td->in_start - td->il_code])
4139 td->is_bb_start [td->ip - td->il_code] = 1;
4141 td->last_ip = td->in_start;
4144 /* Handle relocations */
4145 for (int i = 0; i < td->relocs->len; ++i) {
4146 Reloc *reloc = g_ptr_array_index (td->relocs, i);
4148 int offset = td->in_offsets [reloc->target] - reloc->offset;
4150 switch (reloc->type) {
4151 case RELOC_SHORT_BRANCH:
4152 g_assert (td->new_code [reloc->offset + 1] == 0xffff);
4153 td->new_code [reloc->offset + 1] = offset;
4155 case RELOC_LONG_BRANCH: {
4156 guint16 *v = (guint16 *) &offset;
4157 g_assert (td->new_code [reloc->offset + 1] == 0xbeef);
4158 g_assert (td->new_code [reloc->offset + 2] == 0xdead);
4159 td->new_code [reloc->offset + 1] = *(guint16 *) v;
4160 td->new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
4163 case RELOC_SWITCH: {
4164 guint16 *v = (guint16*)&offset;
4165 td->new_code [reloc->offset] = *(guint16*)v;
4166 td->new_code [reloc->offset + 1] = *(guint16*)(v + 1);
4170 g_assert_not_reached ();
4175 if (td->verbose_level) {
4176 const guint16 *p = td->new_code;
4177 printf("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td->max_vt_sp);
4178 printf("Calculated stack size: %d, stated size: %d\n", td->max_stack_height, header->max_stack);
4179 while (p < td->new_ip) {
4180 p = mono_interp_dis_mintop(td->new_code, p);
4184 g_assert (td->max_stack_height <= (header->max_stack + 1));
4186 int code_len = td->new_ip - td->new_code;
4188 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
4189 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
4190 rtm->code = mono_domain_alloc0 (domain, (td->new_ip - td->new_code) * sizeof (gushort));
4191 memcpy (rtm->code, td->new_code, (td->new_ip - td->new_code) * sizeof(gushort));
4192 g_free (td->new_code);
4193 rtm->new_body_start = rtm->code + body_start_offset;
4194 rtm->num_clauses = header->num_clauses;
4195 for (i = 0; i < header->num_clauses; i++) {
4196 MonoExceptionClause *c = rtm->clauses + i;
4197 int end_off = c->try_offset + c->try_len;
4198 c->try_offset = td->in_offsets [c->try_offset];
4199 c->try_len = td->in_offsets [end_off] - c->try_offset;
4200 end_off = c->handler_offset + c->handler_len;
4201 c->handler_offset = td->in_offsets [c->handler_offset];
4202 c->handler_len = td->in_offsets [end_off] - c->handler_offset;
4203 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
4204 c->data.filter_offset = td->in_offsets [c->data.filter_offset];
4206 rtm->vt_stack_size = td->max_vt_sp;
4207 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
4208 rtm->data_items = mono_domain_alloc0 (domain, td->n_data_items * sizeof (td->data_items [0]));
4209 memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
4211 /* Save debug info */
4212 interp_save_debug_info (rtm, header, td, line_numbers);
4214 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
4215 int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
4216 MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
4217 jinfo->is_interp = 1;
4219 mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
4220 for (i = 0; i < jinfo->num_clauses; ++i) {
4221 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
4222 MonoExceptionClause *c = rtm->clauses + i;
4224 ei->flags = c->flags;
4225 ei->try_start = (guint8*)(rtm->code + c->try_offset);
4226 ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
4227 ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
4228 ei->exvar_offset = rtm->exvar_offsets [i];
4229 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4230 ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
4231 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4233 ei->data.catch_class = c->data.catch_class;
4237 save_seq_points (td);
4239 g_free (td->in_offsets);
4240 for (i = 0; i < header->code_size; ++i)
4241 g_free (td->stack_state [i]);
4242 g_free (td->stack_state);
4243 g_free (td->stack_height);
4244 g_free (td->vt_stack_size);
4245 g_free (td->data_items);
4247 g_hash_table_destroy (td->data_hash);
4248 g_free (td->clause_indexes);
4249 g_ptr_array_free (td->seq_points, TRUE);
4250 g_array_free (line_numbers, TRUE);
4251 g_ptr_array_free (td->relocs, TRUE);
4252 mono_mempool_destroy (td->mempool);
4255 static mono_mutex_t calc_section;
4258 mono_interp_transform_init (void)
4260 mono_os_mutex_init_recursive(&calc_section);
4264 mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context)
4267 int i, align, size, offset;
4268 MonoMethod *method = imethod->method;
4269 MonoImage *image = method->klass->image;
4270 MonoMethodHeader *header = mono_method_get_header (method);
4271 MonoMethodSignature *signature = mono_method_signature (method);
4272 register const unsigned char *ip, *end;
4273 const MonoOpcode *opcode;
4276 unsigned char *is_bb_start;
4278 MonoVTable *method_class_vt;
4280 MonoGenericContext *generic_context = NULL;
4281 MonoDomain *domain = imethod->domain;
4283 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4284 method_class_vt = mono_class_vtable_full (domain, imethod->method->klass, &error);
4285 if (!is_ok (&error))
4286 return mono_error_convert_to_exception (&error);
4288 if (!method_class_vt->initialized) {
4290 InterpFrame *last_env_frame = context->env_frame;
4291 jmp_buf *old_env = context->current_env;
4292 error_init (&error);
4295 MonoException *failed = context->env_frame->ex;
4296 context->env_frame->ex = NULL;
4297 context->env_frame = last_env_frame;
4298 context->current_env = old_env;
4301 context->env_frame = context->current_frame;
4302 context->current_env = &env;
4303 mono_runtime_class_init_full (method_class_vt, &error);
4304 if (!mono_error_ok (&error)) {
4305 return mono_error_convert_to_exception (&error);
4307 context->env_frame = last_env_frame;
4308 context->current_env = old_env;
4311 MONO_PROFILER_RAISE (jit_begin, (method));
4313 if (mono_method_signature (method)->is_inflated)
4314 generic_context = mono_method_get_context (method);
4316 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
4317 if (generic_container)
4318 generic_context = &generic_container->context;
4321 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4322 MonoMethod *nm = NULL;
4323 mono_os_mutex_lock(&calc_section);
4324 if (imethod->transformed) {
4325 mono_os_mutex_unlock(&calc_section);
4326 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4330 /* assumes all internal calls with an array this are built in... */
4331 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
4332 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
4333 signature = mono_method_signature (nm);
4335 const char *name = method->name;
4336 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4337 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4338 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
4340 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
4341 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
4342 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4343 nm = mono_marshal_get_delegate_invoke (method, NULL);
4344 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4345 nm = mono_marshal_get_delegate_begin_invoke (method);
4346 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4347 nm = mono_marshal_get_delegate_end_invoke (method);
4351 imethod->code = g_malloc(sizeof(short));
4352 imethod->code[0] = MINT_CALLRUN;
4356 imethod->stack_size = sizeof (stackval); /* for tracing */
4357 imethod->alloca_size = imethod->stack_size;
4358 imethod->transformed = TRUE;
4359 mono_os_mutex_unlock(&calc_section);
4360 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4364 header = mono_method_get_header (nm);
4365 mono_os_mutex_unlock(&calc_section);
4366 } else if (method->klass == mono_defaults.array_class) {
4367 if (!strcmp (method->name, "UnsafeMov") || !strcmp (method->name, "UnsafeLoad")) {
4368 mono_os_mutex_lock (&calc_section);
4369 if (!imethod->transformed) {
4370 imethod->code = g_malloc (sizeof (short));
4371 imethod->code[0] = MINT_CALLRUN;
4372 imethod->stack_size = sizeof (stackval); /* for tracing */
4373 imethod->alloca_size = imethod->stack_size;
4374 imethod->transformed = TRUE;
4376 mono_os_mutex_unlock(&calc_section);
4377 MONO_PROFILER_RAISE (jit_done, (method, NULL));
4379 } else if (!strcmp (method->name, "UnsafeStore")) {
4383 g_assert ((signature->param_count + signature->hasthis) < 1000);
4384 g_assert (header->max_stack < 10000);
4385 /* intern the strings in the method. */
4387 end = ip + header->code_size;
4389 is_bb_start = g_malloc0(header->code_size);
4390 is_bb_start [0] = 1;
4397 else if (in == 0xf0) {
4399 in = *ip + MONO_CEE_MONO_ICALL;
4401 opcode = &mono_opcodes [in];
4402 switch (opcode->argument) {
4403 case MonoInlineNone:
4406 case MonoInlineString:
4407 if (method->wrapper_type == MONO_WRAPPER_NONE)
4408 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
4411 case MonoInlineType:
4412 if (method->wrapper_type == MONO_WRAPPER_NONE) {
4413 class = mini_get_class (method, read32 (ip + 1), generic_context);
4414 mono_class_init (class);
4415 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
4417 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
4418 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
4419 mono_class_vtable (domain, class);
4424 case MonoInlineMethod:
4425 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
4426 m = mono_get_method_checked (image, read32 (ip + 1), NULL, generic_context, &error);
4427 if (!is_ok (&error)) {
4428 g_free (is_bb_start);
4429 return mono_error_convert_to_exception (&error);
4431 mono_class_init (m->klass);
4432 if (!mono_class_is_interface (m->klass))
4433 mono_class_vtable (domain, m->klass);
4437 case MonoInlineField:
4441 case MonoShortInlineR:
4444 case MonoInlineBrTarget:
4445 offset = read32 (ip + 1);
4447 backwards = offset < 0;
4448 offset += ip - header->code;
4449 g_assert (offset >= 0 && offset < header->code_size);
4450 is_bb_start [offset] |= backwards ? 2 : 1;
4452 case MonoShortInlineBrTarget:
4453 offset = ((gint8 *)ip) [1];
4455 backwards = offset < 0;
4456 offset += ip - header->code;
4457 g_assert (offset >= 0 && offset < header->code_size);
4458 is_bb_start [offset] |= backwards ? 2 : 1;
4463 case MonoShortInlineVar:
4464 case MonoShortInlineI:
4467 case MonoInlineSwitch: {
4469 const unsigned char *next_ip;
4473 next_ip = ip + 4 * n;
4474 for (i = 0; i < n; i++) {
4475 offset = read32 (ip);
4476 backwards = offset < 0;
4477 offset += next_ip - header->code;
4478 g_assert (offset >= 0 && offset < header->code_size);
4479 is_bb_start [offset] |= backwards ? 2 : 1;
4489 g_assert_not_reached ();
4492 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
4494 /* the rest needs to be locked so it is only done once */
4495 mono_os_mutex_lock(&calc_section);
4496 if (imethod->transformed) {
4497 mono_os_mutex_unlock(&calc_section);
4498 g_free (is_bb_start);
4499 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4503 imethod->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
4504 imethod->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
4505 imethod->stack_size = (imethod->stack_size + 7) & ~7;
4507 for (i = 0; i < header->num_locals; ++i) {
4508 size = mono_type_size (header->locals [i], &align);
4509 offset += align - 1;
4510 offset &= ~(align - 1);
4511 imethod->local_offsets [i] = offset;
4514 offset = (offset + 7) & ~7;
4516 imethod->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
4517 for (i = 0; i < header->num_clauses; i++) {
4518 offset += sizeof (MonoObject*);
4519 imethod->exvar_offsets [i] = offset;
4521 offset = (offset + 7) & ~7;
4523 imethod->locals_size = offset;
4524 g_assert (imethod->locals_size < 65536);
4526 imethod->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
4528 if (signature->hasthis) {
4529 g_assert (!signature->pinvoke);
4530 size = mono_type_stack_size (&method->klass->byval_arg, &align);
4531 offset += align - 1;
4532 offset &= ~(align - 1);
4533 imethod->arg_offsets [0] = offset;
4537 for (i = 0; i < signature->param_count; ++i) {
4538 if (signature->pinvoke) {
4540 size = mono_type_native_stack_size (signature->params [i], &dummy);
4544 size = mono_type_stack_size (signature->params [i], &align);
4545 offset += align - 1;
4546 offset &= ~(align - 1);
4547 imethod->arg_offsets [i + !!signature->hasthis] = offset;
4550 offset = (offset + 7) & ~7;
4551 imethod->args_size = offset;
4552 g_assert (imethod->args_size < 10000);
4554 generate (method, imethod, is_bb_start, generic_context);
4556 g_free (is_bb_start);
4558 // FIXME: Add a different callback ?
4559 MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
4560 imethod->transformed = TRUE;
4561 mono_os_mutex_unlock(&calc_section);