3 * transform CIL into different opcodes for more
4 * efficient interpretation
6 * Written by Bernie Solomon (bernard@ugsolutions.com)
12 #include <mono/metadata/appdomain.h>
13 #include <mono/metadata/class-internals.h>
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/metadata/exception.h>
16 #include <mono/metadata/exception-internals.h>
17 #include <mono/metadata/mono-endian.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/seq-points-data.h>
22 #include <mono/metadata/mono-basic-block.h>
23 #include <mono/metadata/abi-details.h>
24 #include <mono/metadata/reflection-internals.h>
25 #include <mono/utils/unlocked.h>
27 #include <mono/mini/mini.h>
28 #include <mono/mini/mini-runtime.h>
31 #include "interp-internals.h"
34 #define INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK 1
35 #define INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY 2
36 #define INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT 4
37 #define INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL 8
39 MonoInterpStats mono_interp_stats
;
43 typedef struct InterpInst InterpInst
;
54 InterpInst
*next
, *prev
;
55 // If this is -1, this instruction is not logically associated with an IL offset, it is
56 // part of the IL instruction associated with the previous interp instruction.
59 guint16 data
[MONO_ZERO_LEN_ARRAY
];
66 SeqPoint
*last_seq_point
;
68 // This will hold a list of last sequence points of incoming basic blocks
69 SeqPoint
**pred_seq_points
;
70 guint num_pred_seq_points
;
81 /* In the interpreter IR */
90 MonoMethod
*inlined_method
;
91 MonoMethodHeader
*header
;
93 const unsigned char *il_code
;
94 const unsigned char *ip
;
95 const unsigned char *in_start
;
96 InterpInst
*last_ins
, *first_ins
;
99 StackInfo
**stack_state
;
102 unsigned char *is_bb_start
;
103 unsigned short *new_code
;
104 unsigned short *new_code_end
;
105 unsigned int max_code_size
;
108 unsigned int max_stack_height
;
109 unsigned int stack_capacity
;
111 unsigned int max_vt_sp
;
112 unsigned int total_locals_size
;
116 GHashTable
*data_hash
;
118 gboolean gen_sdb_seq_points
;
119 GPtrArray
*seq_points
;
120 InterpBasicBlock
**offset_to_bb
;
121 InterpBasicBlock
*entry_bb
;
122 MonoMemPool
*mempool
;
125 gboolean verbose_level
;
126 GArray
*line_numbers
;
129 #define STACK_TYPE_I4 0
130 #define STACK_TYPE_I8 1
131 #define STACK_TYPE_R4 2
132 #define STACK_TYPE_R8 3
133 #define STACK_TYPE_O 4
134 #define STACK_TYPE_VT 5
135 #define STACK_TYPE_MP 6
136 #define STACK_TYPE_F 7
138 static const char *stack_type_string
[] = { "I4", "I8", "R4", "R8", "O ", "VT", "MP", "F " };
140 #if SIZEOF_VOID_P == 8
141 #define STACK_TYPE_I STACK_TYPE_I8
143 #define STACK_TYPE_I STACK_TYPE_I4
146 static int stack_type
[] = {
147 STACK_TYPE_I4
, /*I1*/
148 STACK_TYPE_I4
, /*U1*/
149 STACK_TYPE_I4
, /*I2*/
150 STACK_TYPE_I4
, /*U2*/
151 STACK_TYPE_I4
, /*I4*/
152 STACK_TYPE_I8
, /*I8*/
153 STACK_TYPE_R4
, /*R4*/
154 STACK_TYPE_R8
, /*R8*/
160 #if SIZEOF_VOID_P == 8
161 #define MINT_NEG_P MINT_NEG_I8
162 #define MINT_NOT_P MINT_NOT_I8
164 #define MINT_NEG_FP MINT_NEG_R8
166 #define MINT_ADD_P MINT_ADD_I8
167 #define MINT_SUB_P MINT_SUB_I8
168 #define MINT_MUL_P MINT_MUL_I8
169 #define MINT_DIV_P MINT_DIV_I8
170 #define MINT_DIV_UN_P MINT_DIV_UN_I8
171 #define MINT_REM_P MINT_REM_I8
172 #define MINT_REM_UN_P MINT_REM_UN_I8
173 #define MINT_AND_P MINT_AND_I8
174 #define MINT_OR_P MINT_OR_I8
175 #define MINT_XOR_P MINT_XOR_I8
176 #define MINT_SHL_P MINT_SHL_I8
177 #define MINT_SHR_P MINT_SHR_I8
178 #define MINT_SHR_UN_P MINT_SHR_UN_I8
180 #define MINT_CEQ_P MINT_CEQ_I8
181 #define MINT_CNE_P MINT_CNE_I8
182 #define MINT_CLT_P MINT_CLT_I8
183 #define MINT_CLT_UN_P MINT_CLT_UN_I8
184 #define MINT_CGT_P MINT_CGT_I8
185 #define MINT_CGT_UN_P MINT_CGT_UN_I8
186 #define MINT_CLE_P MINT_CLE_I8
187 #define MINT_CLE_UN_P MINT_CLE_UN_I8
188 #define MINT_CGE_P MINT_CGE_I8
189 #define MINT_CGE_UN_P MINT_CGE_UN_I8
191 #define MINT_ADD_FP MINT_ADD_R8
192 #define MINT_SUB_FP MINT_SUB_R8
193 #define MINT_MUL_FP MINT_MUL_R8
194 #define MINT_DIV_FP MINT_DIV_R8
195 #define MINT_REM_FP MINT_REM_R8
197 #define MINT_CNE_FP MINT_CNE_R8
198 #define MINT_CEQ_FP MINT_CEQ_R8
199 #define MINT_CGT_FP MINT_CGT_R8
200 #define MINT_CGE_FP MINT_CGE_R8
201 #define MINT_CLT_FP MINT_CLT_R8
202 #define MINT_CLE_FP MINT_CLE_R8
204 #define MINT_CONV_OVF_U4_P MINT_CONV_OVF_U4_I8
207 #define MINT_NEG_P MINT_NEG_I4
208 #define MINT_NOT_P MINT_NOT_I4
210 #define MINT_NEG_FP MINT_NEG_R4
212 #define MINT_ADD_P MINT_ADD_I4
213 #define MINT_SUB_P MINT_SUB_I4
214 #define MINT_MUL_P MINT_MUL_I4
215 #define MINT_DIV_P MINT_DIV_I4
216 #define MINT_DIV_UN_P MINT_DIV_UN_I4
217 #define MINT_REM_P MINT_REM_I4
218 #define MINT_REM_UN_P MINT_REM_UN_I4
219 #define MINT_AND_P MINT_AND_I4
220 #define MINT_OR_P MINT_OR_I4
221 #define MINT_XOR_P MINT_XOR_I4
222 #define MINT_SHL_P MINT_SHL_I4
223 #define MINT_SHR_P MINT_SHR_I4
224 #define MINT_SHR_UN_P MINT_SHR_UN_I4
226 #define MINT_CEQ_P MINT_CEQ_I4
227 #define MINT_CNE_P MINT_CNE_I4
228 #define MINT_CLT_P MINT_CLT_I4
229 #define MINT_CLT_UN_P MINT_CLT_UN_I4
230 #define MINT_CGT_P MINT_CGT_I4
231 #define MINT_CGT_UN_P MINT_CGT_UN_I4
232 #define MINT_CLE_P MINT_CLE_I4
233 #define MINT_CLE_UN_P MINT_CLE_UN_I4
234 #define MINT_CGE_P MINT_CGE_I4
235 #define MINT_CGE_UN_P MINT_CGE_UN_I4
237 #define MINT_ADD_FP MINT_ADD_R4
238 #define MINT_SUB_FP MINT_SUB_R4
239 #define MINT_MUL_FP MINT_MUL_R4
240 #define MINT_DIV_FP MINT_DIV_R4
241 #define MINT_REM_FP MINT_REM_R4
243 #define MINT_CNE_FP MINT_CNE_R4
244 #define MINT_CEQ_FP MINT_CEQ_R4
245 #define MINT_CGT_FP MINT_CGT_R4
246 #define MINT_CGE_FP MINT_CGE_R4
247 #define MINT_CLT_FP MINT_CLT_R4
248 #define MINT_CLE_FP MINT_CLE_R4
250 #define MINT_CONV_OVF_U4_P MINT_CONV_OVF_U4_I4
254 const gchar
*op_name
;
258 // static const MagicIntrinsic int_binop[] = {
260 static const MagicIntrinsic int_unnop
[] = {
261 { "op_UnaryPlus", {MINT_NOP
, MINT_NOP
, MINT_NOP
}},
262 { "op_UnaryNegation", {MINT_NEG_P
, MINT_NEG_P
, MINT_NEG_FP
}},
263 { "op_OnesComplement", {MINT_NOT_P
, MINT_NOT_P
, MINT_NIY
}}
266 static const MagicIntrinsic int_binop
[] = {
267 { "op_Addition", {MINT_ADD_P
, MINT_ADD_P
, MINT_ADD_FP
}},
268 { "op_Subtraction", {MINT_SUB_P
, MINT_SUB_P
, MINT_SUB_FP
}},
269 { "op_Multiply", {MINT_MUL_P
, MINT_MUL_P
, MINT_MUL_FP
}},
270 { "op_Division", {MINT_DIV_P
, MINT_DIV_UN_P
, MINT_DIV_FP
}},
271 { "op_Modulus", {MINT_REM_P
, MINT_REM_UN_P
, MINT_REM_FP
}},
272 { "op_BitwiseAnd", {MINT_AND_P
, MINT_AND_P
, MINT_NIY
}},
273 { "op_BitwiseOr", {MINT_OR_P
, MINT_OR_P
, MINT_NIY
}},
274 { "op_ExclusiveOr", {MINT_XOR_P
, MINT_XOR_P
, MINT_NIY
}},
275 { "op_LeftShift", {MINT_SHL_P
, MINT_SHL_P
, MINT_NIY
}},
276 { "op_RightShift", {MINT_SHR_P
, MINT_SHR_UN_P
, MINT_NIY
}},
279 static const MagicIntrinsic int_cmpop
[] = {
280 { "op_Inequality", {MINT_CNE_P
, MINT_CNE_P
, MINT_CNE_FP
}},
281 { "op_Equality", {MINT_CEQ_P
, MINT_CEQ_P
, MINT_CEQ_FP
}},
282 { "op_GreaterThan", {MINT_CGT_P
, MINT_CGT_UN_P
, MINT_CGT_FP
}},
283 { "op_GreaterThanOrEqual", {MINT_CGE_P
, MINT_CGE_UN_P
, MINT_CGE_FP
}},
284 { "op_LessThan", {MINT_CLT_P
, MINT_CLT_UN_P
, MINT_CLT_FP
}},
285 { "op_LessThanOrEqual", {MINT_CLE_P
, MINT_CLE_UN_P
, MINT_CLE_FP
}}
288 static gboolean
generate_code (TransformData
*td
, MonoMethod
*method
, MonoMethodHeader
*header
, MonoGenericContext
*generic_context
, MonoError
*error
);
291 interp_new_ins (TransformData
*td
, guint16 opcode
, int len
)
293 InterpInst
*new_inst
;
295 // Size of data region of instruction is length of instruction minus 1 (the opcode slot)
296 new_inst
= mono_mempool_alloc0 (td
->mempool
, sizeof (InterpInst
) + sizeof (guint16
) * (len
- 1));
297 new_inst
->opcode
= opcode
;
298 // opcodes from inlined methods don't have il offset associated with them since the offset
299 // stored here is relevant only for the original method
300 if (!td
->inlined_method
)
301 new_inst
->il_offset
= td
->in_start
- td
->il_code
;
303 new_inst
->il_offset
= -1;
307 // This version need to be used with switch opcode, which doesn't have constant length
309 interp_add_ins_explicit (TransformData
*td
, guint16 opcode
, int len
)
311 InterpInst
*new_inst
= interp_new_ins (td
, opcode
, len
);
312 new_inst
->prev
= td
->last_ins
;
314 td
->last_ins
->next
= new_inst
;
316 td
->first_ins
= new_inst
;
317 td
->last_ins
= new_inst
;
322 interp_add_ins (TransformData
*td
, guint16 opcode
)
324 return interp_add_ins_explicit (td
, opcode
, mono_interp_oplen
[opcode
]);
327 // Inserts a new instruction inside the instruction stream
329 interp_insert_ins (TransformData
*td
, InterpInst
*prev_ins
, guint16 opcode
)
331 InterpInst
*new_inst
= interp_new_ins (td
, opcode
, mono_interp_oplen
[opcode
]);
332 g_assert (prev_ins
&& prev_ins
->next
);
334 new_inst
->prev
= prev_ins
;
335 new_inst
->next
= prev_ins
->next
;
336 prev_ins
->next
= new_inst
;
337 new_inst
->next
->prev
= new_inst
;
343 interp_remove_ins (TransformData
*td
, InterpInst
*ins
)
346 ins
->prev
->next
= ins
->next
;
348 td
->first_ins
= ins
->next
;
351 ins
->next
->prev
= ins
->prev
;
353 td
->last_ins
= ins
->prev
;
357 #define CHECK_STACK(td, n) \
359 int stack_size = (td)->sp - (td)->stack; \
360 if (stack_size < (n)) \
361 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
362 m_class_get_name ((td)->method->klass), (td)->method->name, \
363 stack_size, n, (td)->ip - (td)->il_code); \
366 #define ENSURE_I4(td, sp_off) \
368 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
369 interp_add_ins (td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
372 #define CHECK_TYPELOAD(klass) \
374 if (!(klass) || mono_class_has_failure (klass)) { \
375 mono_error_set_for_class_failure (error, klass); \
380 #if NO_UNALIGNED_ACCESS
381 #define WRITE32(ip, v) \
383 * (ip) = * (guint16 *)(v); \
384 * ((ip) + 1) = * ((guint16 *)(v) + 1); \
388 #define WRITE32_INS(ins, index, v) \
390 (ins)->data [index] = * (guint16 *)(v); \
391 (ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
394 #define WRITE64_INS(ins, index, v) \
396 (ins)->data [index] = * (guint16 *)(v); \
397 (ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
398 (ins)->data [index + 2] = * ((guint16 *)(v) + 2); \
399 (ins)->data [index + 3] = * ((guint16 *)(v) + 3); \
402 #define WRITE32(ip, v) \
404 * (guint32*)(ip) = * (guint32 *)(v); \
407 #define WRITE32_INS(ins, index, v) \
409 * (guint32 *)(&(ins)->data [index]) = * (guint32 *)(v); \
412 #define WRITE64_INS(ins, index, v) \
414 * (guint64 *)(&(ins)->data [index]) = * (guint64 *)(v); \
421 handle_branch (TransformData
*td
, int short_op
, int long_op
, int offset
)
423 int shorten_branch
= 0;
424 int target
= td
->ip
+ offset
- td
->il_code
;
425 if (target
< 0 || target
>= td
->code_size
)
426 g_assert_not_reached ();
427 /* Add exception checkpoint or safepoint for backward branches */
429 if (mono_threads_are_safepoints_enabled ())
430 interp_add_ins (td
, MINT_SAFEPOINT
);
432 interp_add_ins (td
, MINT_CHECKPOINT
);
434 if (offset
> 0 && td
->stack_height
[target
] < 0) {
435 td
->stack_height
[target
] = td
->sp
- td
->stack
;
436 if (td
->stack_height
[target
] > 0)
437 td
->stack_state
[target
] = (StackInfo
*)g_memdup (td
->stack
, td
->stack_height
[target
] * sizeof (td
->stack
[0]));
438 td
->vt_stack_size
[target
] = td
->vt_sp
;
441 if (td
->header
->code_size
<= 25000) /* FIX to be precise somehow? */
444 if (shorten_branch
) {
445 interp_add_ins (td
, short_op
);
446 td
->last_ins
->data
[0] = (guint16
) target
;
448 interp_add_ins (td
, long_op
);
449 WRITE32_INS (td
->last_ins
, 0, &target
);
454 one_arg_branch(TransformData
*td
, int mint_op
, int offset
)
456 int type
= td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-1].type
;
457 int long_op
= mint_op
+ type
- STACK_TYPE_I4
;
458 int short_op
= long_op
+ MINT_BRFALSE_I4_S
- MINT_BRFALSE_I4
;
461 handle_branch (td
, short_op
, long_op
, offset
);
465 two_arg_branch(TransformData
*td
, int mint_op
, int offset
)
467 int type1
= td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-1].type
;
468 int type2
= td
->sp
[-2].type
== STACK_TYPE_O
|| td
->sp
[-2].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-2].type
;
469 int long_op
= mint_op
+ type1
- STACK_TYPE_I4
;
470 int short_op
= long_op
+ MINT_BEQ_I4_S
- MINT_BEQ_I4
;
472 if (type1
== STACK_TYPE_I4
&& type2
== STACK_TYPE_I8
) {
473 interp_add_ins (td
, MINT_CONV_I8_I4
);
474 // The il instruction starts with the actual branch, and not with the conversion opcodes
475 td
->last_ins
->il_offset
= -1;
476 } else if (type1
== STACK_TYPE_I8
&& type2
== STACK_TYPE_I4
) {
477 interp_add_ins (td
, MINT_CONV_I8_I4_SP
);
478 td
->last_ins
->il_offset
= -1;
479 } else if (type1
== STACK_TYPE_R4
&& type2
== STACK_TYPE_R8
) {
480 interp_add_ins (td
, MINT_CONV_R8_R4
);
481 td
->last_ins
->il_offset
= -1;
482 } else if (type1
== STACK_TYPE_R8
&& type2
== STACK_TYPE_R4
) {
483 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
484 td
->last_ins
->il_offset
= -1;
485 } else if (type1
!= type2
) {
486 g_warning("%s.%s: branch type mismatch %d %d",
487 m_class_get_name (td
->method
->klass
), td
->method
->name
,
488 td
->sp
[-1].type
, td
->sp
[-2].type
);
491 handle_branch (td
, short_op
, long_op
, offset
);
495 unary_arith_op(TransformData
*td
, int mint_op
)
497 int op
= mint_op
+ td
->sp
[-1].type
- STACK_TYPE_I4
;
499 interp_add_ins (td
, op
);
503 binary_arith_op(TransformData
*td
, int mint_op
)
505 int type1
= td
->sp
[-2].type
;
506 int type2
= td
->sp
[-1].type
;
508 #if SIZEOF_VOID_P == 8
509 if ((type1
== STACK_TYPE_MP
|| type1
== STACK_TYPE_I8
) && type2
== STACK_TYPE_I4
) {
510 interp_add_ins (td
, MINT_CONV_I8_I4
);
511 type2
= STACK_TYPE_I8
;
513 if (type1
== STACK_TYPE_I4
&& (type2
== STACK_TYPE_MP
|| type2
== STACK_TYPE_I8
)) {
514 interp_add_ins (td
, MINT_CONV_I8_I4_SP
);
515 type1
= STACK_TYPE_I8
;
516 td
->sp
[-2].type
= STACK_TYPE_I8
;
519 if (type1
== STACK_TYPE_R8
&& type2
== STACK_TYPE_R4
) {
520 interp_add_ins (td
, MINT_CONV_R8_R4
);
521 type2
= STACK_TYPE_R8
;
523 if (type1
== STACK_TYPE_R4
&& type2
== STACK_TYPE_R8
) {
524 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
525 type1
= STACK_TYPE_R8
;
526 td
->sp
[-2].type
= STACK_TYPE_R8
;
528 if (type1
== STACK_TYPE_MP
)
529 type1
= STACK_TYPE_I
;
530 if (type2
== STACK_TYPE_MP
)
531 type2
= STACK_TYPE_I
;
532 if (type1
!= type2
) {
533 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
534 m_class_get_name (td
->method
->klass
), td
->method
->name
,
535 td
->ip
- td
->il_code
, mono_interp_opname
[mint_op
], type1
, type2
);
537 op
= mint_op
+ type1
- STACK_TYPE_I4
;
539 interp_add_ins (td
, op
);
544 shift_op(TransformData
*td
, int mint_op
)
546 int op
= mint_op
+ td
->sp
[-2].type
- STACK_TYPE_I4
;
548 if (td
->sp
[-1].type
!= STACK_TYPE_I4
) {
549 g_warning("%s.%s: shift type mismatch %d",
550 m_class_get_name (td
->method
->klass
), td
->method
->name
,
553 interp_add_ins (td
, op
);
558 can_store (int st_value
, int vt_value
)
560 if (st_value
== STACK_TYPE_O
|| st_value
== STACK_TYPE_MP
)
561 st_value
= STACK_TYPE_I
;
562 if (vt_value
== STACK_TYPE_O
|| vt_value
== STACK_TYPE_MP
)
563 vt_value
= STACK_TYPE_I
;
564 return st_value
== vt_value
;
567 #define SET_SIMPLE_TYPE(s, ty) \
574 #define SET_TYPE(s, ty, k) \
581 #define REALLOC_STACK(td, sppos) \
583 (td)->stack_capacity *= 2; \
584 (td)->stack = (StackInfo*)realloc ((td)->stack, (td)->stack_capacity * sizeof (td->stack [0])); \
585 (td)->sp = (td)->stack + (sppos); \
588 #define PUSH_SIMPLE_TYPE(td, ty) \
592 sp_height = (td)->sp - (td)->stack; \
593 if (sp_height > (td)->max_stack_height) \
594 (td)->max_stack_height = sp_height; \
595 if (sp_height > (td)->stack_capacity) \
596 REALLOC_STACK(td, sp_height); \
597 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
600 #define PUSH_TYPE(td, ty, k) \
604 sp_height = (td)->sp - (td)->stack; \
605 if (sp_height > (td)->max_stack_height) \
606 (td)->max_stack_height = sp_height; \
607 if (sp_height > (td)->stack_capacity) \
608 REALLOC_STACK(td, sp_height); \
609 SET_TYPE((td)->sp - 1, ty, k); \
613 move_stack (TransformData
*td
, int start
, int amount
)
615 int sp_height
= td
->sp
- td
->stack
;
616 int to_move
= sp_height
- start
;
621 if (sp_height
> td
->max_stack_height
)
622 td
->max_stack_height
= sp_height
;
623 if (sp_height
> td
->stack_capacity
)
624 REALLOC_STACK (td
, sp_height
);
626 g_assert (td
->sp
>= td
->stack
);
630 memmove (td
->stack
+ start
+ amount
, td
->stack
+ start
, to_move
* sizeof (StackInfo
));
633 #define PUSH_VT(td, size) \
635 (td)->vt_sp += ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
636 if ((td)->vt_sp > (td)->max_vt_sp) \
637 (td)->max_vt_sp = (td)->vt_sp; \
640 #define POP_VT(td, size) \
642 (td)->vt_sp -= ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
646 get_arg_type_exact (TransformData
*td
, int n
, int *mt
)
649 gboolean hasthis
= mono_method_signature_internal (td
->method
)->hasthis
;
651 if (hasthis
&& n
== 0)
652 type
= m_class_get_byval_arg (td
->method
->klass
);
654 type
= mono_method_signature_internal (td
->method
)->params
[n
- !!hasthis
];
657 *mt
= mint_type (type
);
663 load_arg(TransformData
*td
, int n
)
666 MonoClass
*klass
= NULL
;
668 gboolean hasthis
= mono_method_signature_internal (td
->method
)->hasthis
;
670 type
= get_arg_type_exact (td
, n
, &mt
);
672 if (mt
== MINT_TYPE_VT
) {
674 klass
= mono_class_from_mono_type_internal (type
);
675 if (mono_method_signature_internal (td
->method
)->pinvoke
)
676 size
= mono_class_native_size (klass
, NULL
);
678 size
= mono_class_value_size (klass
, NULL
);
680 if (hasthis
&& n
== 0) {
682 interp_add_ins (td
, MINT_LDARG_P
);
683 td
->last_ins
->data
[0] = 0;
687 interp_add_ins (td
, MINT_LDARG_VT
);
688 td
->last_ins
->data
[0] = n
;
689 WRITE32_INS (td
->last_ins
, 1, &size
);
692 if (hasthis
&& n
== 0) {
694 interp_add_ins (td
, MINT_LDARG_P
);
695 td
->last_ins
->data
[0] = n
;
698 interp_add_ins (td
, MINT_LDARG_I1
+ (mt
- MINT_TYPE_I1
));
699 td
->last_ins
->data
[0] = n
;
700 if (mt
== MINT_TYPE_O
)
701 klass
= mono_class_from_mono_type_internal (type
);
704 PUSH_TYPE(td
, stack_type
[mt
], klass
);
708 store_arg(TransformData
*td
, int n
)
714 type
= get_arg_type_exact (td
, n
, &mt
);
716 if (mt
== MINT_TYPE_VT
) {
718 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
719 if (mono_method_signature_internal (td
->method
)->pinvoke
)
720 size
= mono_class_native_size (klass
, NULL
);
722 size
= mono_class_value_size (klass
, NULL
);
723 interp_add_ins (td
, MINT_STARG_VT
);
724 td
->last_ins
->data
[0] = n
;
725 WRITE32_INS (td
->last_ins
, 1, &size
);
726 if (td
->sp
[-1].type
== STACK_TYPE_VT
)
729 interp_add_ins (td
, MINT_STARG_I1
+ (mt
- MINT_TYPE_I1
));
730 td
->last_ins
->data
[0] = n
;
736 load_local_general (TransformData
*td
, int offset
, MonoType
*type
)
738 int mt
= mint_type (type
);
739 MonoClass
*klass
= NULL
;
740 if (mt
== MINT_TYPE_VT
) {
741 klass
= mono_class_from_mono_type_internal (type
);
742 gint32 size
= mono_class_value_size (klass
, NULL
);
744 interp_add_ins (td
, MINT_LDLOC_VT
);
745 td
->last_ins
->data
[0] = offset
;
746 WRITE32_INS (td
->last_ins
, 1, &size
);
748 g_assert (mt
< MINT_TYPE_VT
);
749 if (!td
->gen_sdb_seq_points
&&
750 mt
== MINT_TYPE_I4
&& !td
->is_bb_start
[td
->in_start
- td
->il_code
] && td
->last_ins
!= NULL
&&
751 td
->last_ins
->opcode
== MINT_STLOC_I4
&& td
->last_ins
->data
[0] == offset
) {
752 td
->last_ins
->opcode
= MINT_STLOC_NP_I4
;
753 } else if (!td
->gen_sdb_seq_points
&&
754 mt
== MINT_TYPE_O
&& !td
->is_bb_start
[td
->in_start
- td
->il_code
] && td
->last_ins
!= NULL
&&
755 td
->last_ins
->opcode
== MINT_STLOC_O
&& td
->last_ins
->data
[0] == offset
) {
756 td
->last_ins
->opcode
= MINT_STLOC_NP_O
;
758 interp_add_ins (td
, MINT_LDLOC_I1
+ (mt
- MINT_TYPE_I1
));
759 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
761 if (mt
== MINT_TYPE_O
)
762 klass
= mono_class_from_mono_type_internal (type
);
764 PUSH_TYPE(td
, stack_type
[mt
], klass
);
768 load_local (TransformData
*td
, int n
)
770 MonoType
*type
= td
->header
->locals
[n
];
771 int offset
= td
->rtm
->local_offsets
[n
];
772 load_local_general (td
, offset
, type
);
776 store_local_general (TransformData
*td
, int offset
, MonoType
*type
)
778 int mt
= mint_type (type
);
780 #if SIZEOF_VOID_P == 8
781 if (td
->sp
[-1].type
== STACK_TYPE_I4
&& stack_type
[mt
] == STACK_TYPE_I8
) {
782 interp_add_ins (td
, MINT_CONV_I8_I4
);
783 td
->sp
[-1].type
= STACK_TYPE_I8
;
786 if (!can_store(td
->sp
[-1].type
, stack_type
[mt
])) {
787 g_warning("%s.%s: Store local stack type mismatch %d %d",
788 m_class_get_name (td
->method
->klass
), td
->method
->name
,
789 stack_type
[mt
], td
->sp
[-1].type
);
791 if (mt
== MINT_TYPE_VT
) {
792 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
793 gint32 size
= mono_class_value_size (klass
, NULL
);
794 interp_add_ins (td
, MINT_STLOC_VT
);
795 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
796 WRITE32_INS (td
->last_ins
, 1, &size
);
797 if (td
->sp
[-1].type
== STACK_TYPE_VT
)
800 g_assert (mt
< MINT_TYPE_VT
);
801 interp_add_ins (td
, MINT_STLOC_I1
+ (mt
- MINT_TYPE_I1
));
802 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
808 store_local (TransformData
*td
, int n
)
810 MonoType
*type
= td
->header
->locals
[n
];
811 int offset
= td
->rtm
->local_offsets
[n
];
812 store_local_general (td
, offset
, type
);
815 #define SIMPLE_OP(td, op) \
817 interp_add_ins (td, op); \
822 get_data_item_index (TransformData
*td
, void *ptr
)
824 gpointer p
= g_hash_table_lookup (td
->data_hash
, ptr
);
827 return GPOINTER_TO_UINT (p
) - 1;
828 if (td
->max_data_items
== td
->n_data_items
) {
829 td
->max_data_items
= td
->n_data_items
== 0 ? 16 : 2 * td
->max_data_items
;
830 td
->data_items
= (gpointer
*)g_realloc (td
->data_items
, td
->max_data_items
* sizeof(td
->data_items
[0]));
832 index
= td
->n_data_items
;
833 td
->data_items
[index
] = ptr
;
835 g_hash_table_insert (td
->data_hash
, ptr
, GUINT_TO_POINTER (index
+ 1));
840 jit_call_supported (MonoMethod
*method
, MonoMethodSignature
*sig
)
844 if (sig
->param_count
> 6)
848 if (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)
850 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
852 if (method
->is_inflated
)
854 if (method
->string_ctor
)
857 if (mono_aot_only
&& m_class_get_image (method
->klass
)->aot_module
&& !(method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)) {
859 gpointer addr
= mono_jit_compile_method_jit_only (method
, error
);
860 if (addr
&& mono_error_ok (error
))
864 for (l
= mono_interp_jit_classes
; l
; l
= l
->next
) {
865 const char *class_name
= (const char*)l
->data
;
867 if (!strcmp (m_class_get_name (method
->klass
), class_name
))
875 static int mono_class_get_magic_index (MonoClass
*k
)
877 if (mono_class_is_magic_int (k
))
878 return !strcmp ("nint", m_class_get_name (k
)) ? 0 : 1;
880 if (mono_class_is_magic_float (k
))
887 interp_generate_mae_throw (TransformData
*td
, MonoMethod
*method
, MonoMethod
*target_method
)
889 MonoJitICallInfo
*info
= &mono_get_jit_icall_info ()->mono_throw_method_access
;
891 /* Inject code throwing MethodAccessException */
892 interp_add_ins (td
, MINT_MONO_LDPTR
);
893 td
->last_ins
->data
[0] = get_data_item_index (td
, method
);
894 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
896 interp_add_ins (td
, MINT_MONO_LDPTR
);
897 td
->last_ins
->data
[0] = get_data_item_index (td
, target_method
);
898 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
900 interp_add_ins (td
, MINT_ICALL_PP_V
);
901 td
->last_ins
->data
[0] = get_data_item_index (td
, (gpointer
)info
->func
);
907 interp_generate_bie_throw (TransformData
*td
)
909 MonoJitICallInfo
*info
= &mono_get_jit_icall_info ()->mono_throw_bad_image
;
911 interp_add_ins (td
, MINT_ICALL_PP_V
);
912 td
->last_ins
->data
[0] = get_data_item_index (td
, (gpointer
)info
->func
);
916 * These are additional locals that can be allocated as we transform the code.
917 * They are allocated past the method locals so they are accessed in the same
918 * way, with an offset relative to the frame->locals.
921 create_interp_local (TransformData
*td
, MonoType
*type
)
924 int offset
= td
->total_locals_size
;
926 size
= mono_type_size (type
, &align
);
927 offset
= ALIGN_TO (offset
, align
);
929 td
->total_locals_size
= offset
+ size
;
935 dump_mint_code (const guint16
*start
, const guint16
* end
)
937 const guint16
*p
= start
;
939 char *ins
= mono_interp_dis_mintop (start
, p
);
940 g_print ("%s\n", ins
);
942 p
= mono_interp_dis_mintop_len (p
);
948 mono_interp_print_code (InterpMethod
*imethod
)
950 MonoJitInfo
*jinfo
= imethod
->jinfo
;
951 const guint16
*start
;
956 char *name
= mono_method_full_name (imethod
->method
, 1);
957 g_print ("Method : %s\n", name
);
960 start
= (guint16
*) jinfo
->code_start
;
961 dump_mint_code (start
, start
+ jinfo
->code_size
);
965 static MonoMethodHeader
*
966 interp_method_get_header (MonoMethod
* method
, MonoError
*error
)
968 /* An explanation: mono_method_get_header_internal returns an error if
969 * called on a method with no body (e.g. an abstract method, or an
970 * icall). We don't want that.
972 if (mono_method_has_no_body (method
))
975 return mono_method_get_header_internal (method
, error
);
978 /* stores top of stack as local and pushes address of it on stack */
980 emit_store_value_as_local (TransformData
*td
, MonoType
*src
)
982 int size
= mini_magic_type_size (NULL
, src
);
983 int local_offset
= create_interp_local (td
, mini_native_type_replace_type (src
));
985 store_local_general (td
, local_offset
, src
);
987 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
988 interp_add_ins (td
, MINT_LDLOC_VT
);
989 td
->last_ins
->data
[0] = local_offset
;
990 WRITE32_INS (td
->last_ins
, 1, &size
);
993 PUSH_TYPE (td
, STACK_TYPE_VT
, NULL
);
996 // Returns whether we can optimize away the instructions starting at start.
997 // If any instructions are part of a new basic block, we can't remove them.
999 interp_is_bb_start (TransformData
*td
, InterpInst
*start
, InterpInst
*end
)
1001 InterpInst
*ins
= start
;
1002 while (ins
!= end
) {
1003 if (ins
->il_offset
!= -1) {
1004 if (td
->is_bb_start
[ins
->il_offset
])
1013 interp_ins_is_ldc (InterpInst
*ins
)
1015 return ins
->opcode
>= MINT_LDC_I4_M1
&& ins
->opcode
<= MINT_LDC_I8
;
1019 interp_ldc_i4_get_const (InterpInst
*ins
)
1021 switch (ins
->opcode
) {
1022 case MINT_LDC_I4_M1
: return -1;
1023 case MINT_LDC_I4_0
: return 0;
1024 case MINT_LDC_I4_1
: return 1;
1025 case MINT_LDC_I4_2
: return 2;
1026 case MINT_LDC_I4_3
: return 3;
1027 case MINT_LDC_I4_4
: return 4;
1028 case MINT_LDC_I4_5
: return 5;
1029 case MINT_LDC_I4_6
: return 6;
1030 case MINT_LDC_I4_7
: return 7;
1031 case MINT_LDC_I4_8
: return 8;
1032 case MINT_LDC_I4_S
: return (gint32
)(gint8
)ins
->data
[0];
1033 case MINT_LDC_I4
: return READ32 (&ins
->data
[0]);
1035 g_assert_not_reached ();
1040 interp_get_ldind_for_mt (int mt
)
1043 case MINT_TYPE_I1
: return MINT_LDIND_I1_CHECK
;
1044 case MINT_TYPE_U1
: return MINT_LDIND_U1_CHECK
;
1045 case MINT_TYPE_I2
: return MINT_LDIND_I2_CHECK
;
1046 case MINT_TYPE_U2
: return MINT_LDIND_U2_CHECK
;
1047 case MINT_TYPE_I4
: return MINT_LDIND_I4_CHECK
;
1048 case MINT_TYPE_I8
: return MINT_LDIND_I8_CHECK
;
1049 case MINT_TYPE_R4
: return MINT_LDIND_R4_CHECK
;
1050 case MINT_TYPE_R8
: return MINT_LDIND_R8_CHECK
;
1051 case MINT_TYPE_O
: return MINT_LDIND_REF
;
1053 g_assert_not_reached ();
1058 /* Return TRUE if call transformation is finished */
1060 interp_handle_intrinsics (TransformData
*td
, MonoMethod
*target_method
, MonoClass
*constrained_class
, MonoMethodSignature
*csignature
, gboolean readonly
, int *op
)
1062 const char *tm
= target_method
->name
;
1064 int type_index
= mono_class_get_magic_index (target_method
->klass
);
1065 gboolean in_corlib
= m_class_get_image (target_method
->klass
) == mono_defaults
.corlib
;
1066 const char *klass_name_space
= m_class_get_name_space (target_method
->klass
);
1067 const char *klass_name
= m_class_get_name (target_method
->klass
);
1069 if (target_method
->klass
== mono_defaults
.string_class
) {
1070 if (tm
[0] == 'g') {
1071 if (strcmp (tm
, "get_Chars") == 0)
1073 else if (strcmp (tm
, "get_Length") == 0)
1076 } else if (type_index
>= 0) {
1077 MonoClass
*magic_class
= target_method
->klass
;
1079 const int mt
= mint_type (m_class_get_byval_arg (magic_class
));
1080 if (!strcmp (".ctor", tm
)) {
1081 MonoType
*arg
= csignature
->params
[0];
1082 /* depending on SIZEOF_VOID_P and the type of the value passed to the .ctor we either have to CONV it, or do nothing */
1083 int arg_size
= mini_magic_type_size (NULL
, arg
);
1085 if (arg_size
> SIZEOF_VOID_P
) { // 8 -> 4
1086 switch (type_index
) {
1088 interp_add_ins (td
, MINT_CONV_I4_I8
);
1091 interp_add_ins (td
, MINT_CONV_R4_R8
);
1096 if (arg_size
< SIZEOF_VOID_P
) { // 4 -> 8
1097 switch (type_index
) {
1099 interp_add_ins (td
, MINT_CONV_I8_I4
);
1102 interp_add_ins (td
, MINT_CONV_I8_U4
);
1105 interp_add_ins (td
, MINT_CONV_R8_R4
);
1110 switch (type_index
) {
1112 #if SIZEOF_VOID_P == 4
1113 interp_add_ins (td
, MINT_STIND_I4
);
1115 interp_add_ins (td
, MINT_STIND_I8
);
1119 #if SIZEOF_VOID_P == 4
1120 interp_add_ins (td
, MINT_STIND_R4
);
1122 interp_add_ins (td
, MINT_STIND_R8
);
1130 } else if (!strcmp ("op_Implicit", tm
) || !strcmp ("op_Explicit", tm
)) {
1131 MonoType
*src
= csignature
->params
[0];
1132 MonoType
*dst
= csignature
->ret
;
1133 MonoClass
*src_klass
= mono_class_from_mono_type_internal (src
);
1134 int src_size
= mini_magic_type_size (NULL
, src
);
1135 int dst_size
= mini_magic_type_size (NULL
, dst
);
1137 gboolean store_value_as_local
= FALSE
;
1139 switch (type_index
) {
1141 if (!mini_magic_is_int_type (src
) || !mini_magic_is_int_type (dst
)) {
1142 if (mini_magic_is_int_type (src
))
1143 store_value_as_local
= TRUE
;
1144 else if (mono_class_is_magic_float (src_klass
))
1145 store_value_as_local
= TRUE
;
1151 if (!mini_magic_is_float_type (src
) || !mini_magic_is_float_type (dst
)) {
1152 if (mini_magic_is_float_type (src
))
1153 store_value_as_local
= TRUE
;
1154 else if (mono_class_is_magic_int (src_klass
))
1155 store_value_as_local
= TRUE
;
1162 if (store_value_as_local
) {
1163 emit_store_value_as_local (td
, src
);
1165 /* emit call to managed conversion method */
1169 if (src_size
> dst_size
) { // 8 -> 4
1170 switch (type_index
) {
1172 interp_add_ins (td
, MINT_CONV_I4_I8
);
1175 interp_add_ins (td
, MINT_CONV_R4_R8
);
1180 if (src_size
< dst_size
) { // 4 -> 8
1181 switch (type_index
) {
1183 interp_add_ins (td
, MINT_CONV_I8_I4
);
1186 interp_add_ins (td
, MINT_CONV_I8_U4
);
1189 interp_add_ins (td
, MINT_CONV_R8_R4
);
1194 SET_TYPE (td
->sp
- 1, stack_type
[mint_type (dst
)], mono_class_from_mono_type_internal (dst
));
1197 } else if (!strcmp ("op_Increment", tm
)) {
1198 g_assert (type_index
!= 2); // no nfloat
1199 #if SIZEOF_VOID_P == 8
1200 interp_add_ins (td
, MINT_ADD1_I8
);
1202 interp_add_ins (td
, MINT_ADD1_I4
);
1204 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1207 } else if (!strcmp ("op_Decrement", tm
)) {
1208 g_assert (type_index
!= 2); // no nfloat
1209 #if SIZEOF_VOID_P == 8
1210 interp_add_ins (td
, MINT_SUB1_I8
);
1212 interp_add_ins (td
, MINT_SUB1_I4
);
1214 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1217 } else if (!strcmp ("CompareTo", tm
) || !strcmp ("Equals", tm
)) {
1218 MonoType
*arg
= csignature
->params
[0];
1220 /* on 'System.n*::{CompareTo,Equals} (System.n*)' variant we need to push managed
1221 * pointer instead of value */
1222 if (arg
->type
== MONO_TYPE_VALUETYPE
)
1223 emit_store_value_as_local (td
, arg
);
1225 /* emit call to managed conversion method */
1227 } else if (!strcmp (".cctor", tm
)) {
1230 } else if (!strcmp ("Parse", tm
)) {
1233 } else if (!strcmp ("ToString", tm
)) {
1236 } else if (!strcmp ("GetHashCode", tm
)) {
1239 } else if (!strcmp ("IsNaN", tm
) || !strcmp ("IsInfinity", tm
) || !strcmp ("IsNegativeInfinity", tm
) || !strcmp ("IsPositiveInfinity", tm
)) {
1240 g_assert (type_index
== 2); // nfloat only
1245 for (i
= 0; i
< sizeof (int_unnop
) / sizeof (MagicIntrinsic
); ++i
) {
1246 if (!strcmp (int_unnop
[i
].op_name
, tm
)) {
1247 interp_add_ins (td
, int_unnop
[i
].insn
[type_index
]);
1248 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1254 for (i
= 0; i
< sizeof (int_binop
) / sizeof (MagicIntrinsic
); ++i
) {
1255 if (!strcmp (int_binop
[i
].op_name
, tm
)) {
1256 interp_add_ins (td
, int_binop
[i
].insn
[type_index
]);
1258 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1264 for (i
= 0; i
< sizeof (int_cmpop
) / sizeof (MagicIntrinsic
); ++i
) {
1265 if (!strcmp (int_cmpop
[i
].op_name
, tm
)) {
1266 MonoClass
*k
= mono_defaults
.boolean_class
;
1267 interp_add_ins (td
, int_cmpop
[i
].insn
[type_index
]);
1269 SET_TYPE (td
->sp
- 1, stack_type
[mint_type (m_class_get_byval_arg (k
))], k
);
1275 g_error ("TODO: interp_transform_call %s:%s", m_class_get_name (target_method
->klass
), tm
);
1276 } else if (mono_class_is_subclass_of_internal (target_method
->klass
, mono_defaults
.array_class
, FALSE
)) {
1277 if (!strcmp (tm
, "get_Rank")) {
1278 *op
= MINT_ARRAY_RANK
;
1279 } else if (!strcmp (tm
, "get_Length")) {
1281 } else if (!strcmp (tm
, "Address")) {
1282 *op
= readonly
? MINT_LDELEMA
: MINT_LDELEMA_TC
;
1283 } else if (!strcmp (tm
, "UnsafeMov") || !strcmp (tm
, "UnsafeLoad") || !strcmp (tm
, "Set") || !strcmp (tm
, "Get")) {
1285 } else if (!strcmp (tm
, "UnsafeStore")) {
1286 g_error ("TODO ArrayClass::UnsafeStore");
1288 } else if (in_corlib
&&
1289 !strcmp (klass_name_space
, "System.Diagnostics") &&
1290 !strcmp (klass_name
, "Debugger")) {
1291 if (!strcmp (tm
, "Break") && csignature
->param_count
== 0) {
1292 if (mini_should_insert_breakpoint (td
->method
))
1295 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "ByReference`1")) {
1296 g_assert (!strcmp (tm
, "get_Value"));
1297 *op
= MINT_INTRINS_BYREFERENCE_GET_VALUE
;
1298 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "Math") && csignature
->param_count
== 1 && csignature
->params
[0]->type
== MONO_TYPE_R8
) {
1299 if (tm
[0] == 'A') {
1300 if (strcmp (tm
, "Abs") == 0 && csignature
->params
[0]->type
== MONO_TYPE_R8
) {
1302 } else if (strcmp (tm
, "Asin") == 0){
1304 } else if (strcmp (tm
, "Asinh") == 0){
1306 } else if (strcmp (tm
, "Acos") == 0){
1308 } else if (strcmp (tm
, "Acosh") == 0){
1310 } else if (strcmp (tm
, "Atan") == 0){
1312 } else if (strcmp (tm
, "Atanh") == 0){
1315 } else if (tm
[0] == 'C') {
1316 if (strcmp (tm
, "Cos") == 0) {
1318 } else if (strcmp (tm
, "Cbrt") == 0){
1320 } else if (strcmp (tm
, "Cosh") == 0){
1323 } else if (tm
[0] == 'S') {
1324 if (strcmp (tm
, "Sin") == 0) {
1326 } else if (strcmp (tm
, "Sqrt") == 0) {
1328 } else if (strcmp (tm
, "Sinh") == 0){
1331 } else if (tm
[0] == 'T') {
1332 if (strcmp (tm
, "Tan") == 0) {
1334 } else if (strcmp (tm
, "Tanh") == 0){
1338 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && (!strcmp (klass_name
, "Span`1") || !strcmp (klass_name
, "ReadOnlySpan`1"))) {
1339 if (!strcmp (tm
, "get_Item")) {
1340 MonoGenericClass
*gclass
= mono_class_get_generic_class (target_method
->klass
);
1341 MonoClass
*param_class
= mono_class_from_mono_type_internal (gclass
->context
.class_inst
->type_argv
[0]);
1343 if (!mini_is_gsharedvt_variable_klass (param_class
)) {
1344 MonoClassField
*length_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_length", NULL
);
1345 g_assert (length_field
);
1346 int offset_length
= length_field
->offset
- sizeof (MonoObject
);
1348 MonoClassField
*ptr_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_pointer", NULL
);
1349 g_assert (ptr_field
);
1350 int offset_pointer
= ptr_field
->offset
- sizeof (MonoObject
);
1352 int size
= mono_class_array_element_size (param_class
);
1353 interp_add_ins (td
, MINT_GETITEM_SPAN
);
1354 td
->last_ins
->data
[0] = size
;
1355 td
->last_ins
->data
[1] = offset_length
;
1356 td
->last_ins
->data
[2] = offset_pointer
;
1358 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
1363 } else if (!strcmp (tm
, "get_Length")) {
1364 MonoClassField
*length_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_length", NULL
);
1365 g_assert (length_field
);
1366 int offset_length
= length_field
->offset
- sizeof (MonoObject
);
1367 interp_add_ins (td
, MINT_LDLEN_SPAN
);
1368 td
->last_ins
->data
[0] = offset_length
;
1369 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
1373 } else if (in_corlib
&& !strcmp (klass_name_space
, "Internal.Runtime.CompilerServices") && !strcmp (klass_name
, "Unsafe")) {
1374 #ifdef ENABLE_NETCORE
1375 if (!strcmp (tm
, "AddByteOffset"))
1376 *op
= MINT_INTRINS_UNSAFE_ADD_BYTE_OFFSET
;
1377 else if (!strcmp (tm
, "ByteOffset"))
1378 *op
= MINT_INTRINS_UNSAFE_BYTE_OFFSET
;
1379 else if (!strcmp (tm
, "As") || !strcmp (tm
, "AsRef"))
1381 else if (!strcmp (tm
, "AsPointer")) {
1383 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
1386 } else if (!strcmp (tm
, "IsAddressLessThan")) {
1387 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1389 g_assert (ctx
->method_inst
);
1390 g_assert (ctx
->method_inst
->type_argc
== 1);
1392 MonoClass
*k
= mono_defaults
.boolean_class
;
1393 interp_add_ins (td
, MINT_CLT_UN_P
);
1395 SET_TYPE (td
->sp
- 1, stack_type
[mint_type (m_class_get_byval_arg (k
))], k
);
1398 } else if (!strcmp (tm
, "SizeOf")) {
1399 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1401 g_assert (ctx
->method_inst
);
1402 g_assert (ctx
->method_inst
->type_argc
== 1);
1403 MonoType
*t
= ctx
->method_inst
->type_argv
[0];
1405 int esize
= mono_type_size (t
, &align
);
1406 interp_add_ins (td
, MINT_LDC_I4
);
1407 WRITE32_INS (td
->last_ins
, 0, &esize
);
1408 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
1411 } else if (!strcmp (tm
, "AreSame")) {
1415 } else if (in_corlib
&& !strcmp (klass_name_space
, "System.Runtime.CompilerServices") && !strcmp (klass_name
, "RuntimeHelpers")) {
1416 #ifdef ENABLE_NETCORE
1417 if (!strcmp (tm
, "IsBitwiseEquatable")) {
1418 g_assert (csignature
->param_count
== 0);
1419 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1421 g_assert (ctx
->method_inst
);
1422 g_assert (ctx
->method_inst
->type_argc
== 1);
1423 MonoType
*t
= mini_get_underlying_type (ctx
->method_inst
->type_argv
[0]);
1425 if (MONO_TYPE_IS_PRIMITIVE (t
) && t
->type
!= MONO_TYPE_R4
&& t
->type
!= MONO_TYPE_R8
)
1426 *op
= MINT_LDC_I4_1
;
1428 *op
= MINT_LDC_I4_0
;
1429 } else if (!strcmp (tm
, "ObjectHasComponentSize")) {
1430 *op
= MINT_INTRINS_RUNTIMEHELPERS_OBJECT_HAS_COMPONENT_SIZE
;
1433 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "RuntimeMethodHandle") && !strcmp (tm
, "GetFunctionPointer") && csignature
->param_count
== 1) {
1434 // We must intrinsify this method on interp so we don't return a pointer to native code entering interpreter
1435 *op
= MINT_LDFTN_DYNAMIC
;
1436 } else if (in_corlib
&& target_method
->klass
== mono_defaults
.object_class
) {
1437 if (!strcmp (tm
, "InternalGetHashCode"))
1438 *op
= MINT_INTRINS_GET_HASHCODE
;
1439 #ifdef DISABLE_REMOTING
1440 else if (!strcmp (tm
, "GetType"))
1441 *op
= MINT_INTRINS_GET_TYPE
;
1443 #ifdef ENABLE_NETCORE
1444 else if (!strcmp (tm
, "GetRawData")) {
1445 #if SIZEOF_VOID_P == 8
1446 interp_add_ins (td
, MINT_LDC_I8_S
);
1448 interp_add_ins (td
, MINT_LDC_I4_S
);
1450 td
->last_ins
->data
[0] = (gint16
) MONO_ABI_SIZEOF (MonoObject
);
1452 interp_add_ins (td
, MINT_ADD_P
);
1453 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
1459 } else if (in_corlib
&& target_method
->klass
== mono_defaults
.enum_class
&& !strcmp (tm
, "HasFlag")) {
1460 gboolean intrinsify
= FALSE
;
1461 MonoClass
*base_klass
= NULL
;
1462 if (td
->last_ins
&& td
->last_ins
->opcode
== MINT_BOX
&&
1463 td
->last_ins
->prev
&& interp_ins_is_ldc (td
->last_ins
->prev
) &&
1464 td
->last_ins
->prev
->prev
&& td
->last_ins
->prev
->prev
->opcode
== MINT_BOX
&&
1465 td
->sp
[-2].klass
== td
->sp
[-1].klass
&&
1466 !interp_is_bb_start (td
, td
->last_ins
->prev
->prev
, NULL
) &&
1467 !td
->is_bb_start
[td
->in_start
- td
->il_code
]) {
1468 // csc pattern : box, ldc, box, call HasFlag
1469 g_assert (m_class_is_enumtype (td
->sp
[-2].klass
));
1470 MonoType
*base_type
= mono_type_get_underlying_type (m_class_get_byval_arg (td
->sp
[-2].klass
));
1471 base_klass
= mono_class_from_mono_type_internal (base_type
);
1473 // Remove the boxing of valuetypes
1474 interp_remove_ins (td
, td
->last_ins
->prev
->prev
);
1475 interp_remove_ins (td
, td
->last_ins
);
1478 } else if (td
->last_ins
&& td
->last_ins
->opcode
== MINT_BOX
&&
1479 td
->last_ins
->prev
&& interp_ins_is_ldc (td
->last_ins
->prev
) &&
1480 constrained_class
&& td
->sp
[-1].klass
== constrained_class
&&
1481 !interp_is_bb_start (td
, td
->last_ins
->prev
, NULL
) &&
1482 !td
->is_bb_start
[td
->in_start
- td
->il_code
]) {
1483 // mcs pattern : ldc, box, constrained Enum, call HasFlag
1484 g_assert (m_class_is_enumtype (constrained_class
));
1485 MonoType
*base_type
= mono_type_get_underlying_type (m_class_get_byval_arg (constrained_class
));
1486 base_klass
= mono_class_from_mono_type_internal (base_type
);
1487 int mt
= mint_type (m_class_get_byval_arg (base_klass
));
1489 // Remove boxing and load the value of this
1490 interp_remove_ins (td
, td
->last_ins
);
1491 interp_insert_ins (td
, td
->last_ins
->prev
, interp_get_ldind_for_mt (mt
));
1496 interp_add_ins (td
, MINT_INTRINS_ENUM_HASFLAG
);
1497 td
->last_ins
->data
[0] = get_data_item_index (td
, base_klass
);
1499 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
1503 } else if (in_corlib
&& !strcmp (klass_name_space
, "System.Threading") && !strcmp (klass_name
, "Interlocked")) {
1505 if (!strcmp (tm
, "MemoryBarrier") && csignature
->param_count
== 0)
1506 *op
= MINT_MONO_MEMORY_BARRIER
;
1508 } else if (in_corlib
&&
1509 !strcmp (klass_name_space
, "System.Runtime.CompilerServices") &&
1510 !strcmp (klass_name
, "JitHelpers") &&
1511 (!strcmp (tm
, "EnumEquals") || !strcmp (tm
, "EnumCompareTo"))) {
1512 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1514 g_assert (ctx
->method_inst
);
1515 g_assert (ctx
->method_inst
->type_argc
== 1);
1516 g_assert (csignature
->param_count
== 2);
1518 MonoType
*t
= ctx
->method_inst
->type_argv
[0];
1519 t
= mini_get_underlying_type (t
);
1521 gboolean is_i8
= (t
->type
== MONO_TYPE_I8
|| t
->type
== MONO_TYPE_U8
);
1522 gboolean is_unsigned
= (t
->type
== MONO_TYPE_U1
|| t
->type
== MONO_TYPE_U2
|| t
->type
== MONO_TYPE_U4
|| t
->type
== MONO_TYPE_U8
|| t
->type
== MONO_TYPE_U
);
1524 gboolean is_compareto
= strcmp (tm
, "EnumCompareTo") == 0;
1526 int offseta
, offsetb
;
1527 offseta
= create_interp_local (td
, t
);
1528 offsetb
= create_interp_local (td
, t
);
1531 store_local_general (td
, offsetb
, t
);
1532 store_local_general (td
, offseta
, t
);
1534 load_local_general (td
, offseta
, t
);
1535 load_local_general (td
, offsetb
, t
);
1537 interp_add_ins (td
, is_i8
? MINT_CGT_UN_I8
: MINT_CGT_UN_I4
);
1539 interp_add_ins (td
, is_i8
? MINT_CGT_I8
: MINT_CGT_I4
);
1541 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
1543 load_local_general (td
, offseta
, t
);
1544 load_local_general (td
, offsetb
, t
);
1546 interp_add_ins (td
, is_i8
? MINT_CLT_UN_I8
: MINT_CLT_UN_I4
);
1548 interp_add_ins (td
, is_i8
? MINT_CLT_I8
: MINT_CLT_I4
);
1550 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
1551 // (a > b) - (a < b)
1552 interp_add_ins (td
, MINT_SUB_I4
);
1569 interp_transform_internal_calls (MonoMethod
*method
, MonoMethod
*target_method
, MonoMethodSignature
*csignature
, gboolean is_virtual
)
1571 if (method
->wrapper_type
== MONO_WRAPPER_NONE
&& target_method
!= NULL
) {
1572 if (target_method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)
1573 target_method
= mono_marshal_get_native_wrapper (target_method
, FALSE
, FALSE
);
1574 if (!is_virtual
&& target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)
1575 target_method
= mono_marshal_get_synchronized_wrapper (target_method
);
1577 if (target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
&& !is_virtual
&& !mono_class_is_marshalbyref (target_method
->klass
) && m_class_get_rank (target_method
->klass
) == 0)
1578 target_method
= mono_marshal_get_native_wrapper (target_method
, FALSE
, FALSE
);
1580 return target_method
;
1584 interp_type_as_ptr (MonoType
*tp
)
1586 if (MONO_TYPE_IS_POINTER (tp
))
1588 if (MONO_TYPE_IS_REFERENCE (tp
))
1590 if ((tp
)->type
== MONO_TYPE_I4
)
1592 #if SIZEOF_VOID_P == 8
1593 if ((tp
)->type
== MONO_TYPE_I8
)
1596 if ((tp
)->type
== MONO_TYPE_BOOLEAN
)
1598 if ((tp
)->type
== MONO_TYPE_CHAR
)
1600 if ((tp
)->type
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (tp
->data
.klass
))
1605 #define INTERP_TYPE_AS_PTR(tp) interp_type_as_ptr (tp)
1608 interp_icall_op_for_sig (MonoMethodSignature
*sig
)
1611 switch (sig
->param_count
) {
1613 if (MONO_TYPE_IS_VOID (sig
->ret
))
1614 op
= MINT_ICALL_V_V
;
1615 else if (INTERP_TYPE_AS_PTR (sig
->ret
))
1616 op
= MINT_ICALL_V_P
;
1619 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1620 if (INTERP_TYPE_AS_PTR (sig
->params
[0]))
1621 op
= MINT_ICALL_P_V
;
1622 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1623 if (INTERP_TYPE_AS_PTR (sig
->params
[0]))
1624 op
= MINT_ICALL_P_P
;
1628 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1629 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1630 INTERP_TYPE_AS_PTR (sig
->params
[1]))
1631 op
= MINT_ICALL_PP_V
;
1632 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1633 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1634 INTERP_TYPE_AS_PTR (sig
->params
[1]))
1635 op
= MINT_ICALL_PP_P
;
1639 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1640 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1641 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1642 INTERP_TYPE_AS_PTR (sig
->params
[2]))
1643 op
= MINT_ICALL_PPP_V
;
1644 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1645 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1646 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1647 INTERP_TYPE_AS_PTR (sig
->params
[2]))
1648 op
= MINT_ICALL_PPP_P
;
1652 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1653 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1654 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1655 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1656 INTERP_TYPE_AS_PTR (sig
->params
[3]))
1657 op
= MINT_ICALL_PPPP_V
;
1658 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1659 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1660 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1661 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1662 INTERP_TYPE_AS_PTR (sig
->params
[3]))
1663 op
= MINT_ICALL_PPPP_P
;
1667 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1668 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1669 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1670 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1671 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1672 INTERP_TYPE_AS_PTR (sig
->params
[4]))
1673 op
= MINT_ICALL_PPPPP_V
;
1674 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1675 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1676 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1677 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1678 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1679 INTERP_TYPE_AS_PTR (sig
->params
[4]))
1680 op
= MINT_ICALL_PPPPP_P
;
1684 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1685 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1686 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1687 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1688 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1689 INTERP_TYPE_AS_PTR (sig
->params
[4]) &&
1690 INTERP_TYPE_AS_PTR (sig
->params
[5]))
1691 op
= MINT_ICALL_PPPPPP_V
;
1692 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1693 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1694 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1695 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1696 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1697 INTERP_TYPE_AS_PTR (sig
->params
[4]) &&
1698 INTERP_TYPE_AS_PTR (sig
->params
[5]))
1699 op
= MINT_ICALL_PPPPPP_P
;
1706 #define INLINE_LENGTH_LIMIT 20
1709 interp_method_check_inlining (TransformData
*td
, MonoMethod
*method
)
1711 MonoMethodHeaderSummary header
;
1713 if (td
->method
== method
)
1716 if (!mono_method_get_header_summary (method
, &header
))
1719 /*runtime, icall and pinvoke are checked by summary call*/
1720 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_NOINLINING
) ||
1721 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) ||
1722 (mono_class_is_marshalbyref (method
->klass
)) ||
1726 if (header
.code_size
>= INLINE_LENGTH_LIMIT
)
1729 if (mono_class_needs_cctor_run (method
->klass
, NULL
)) {
1732 if (!m_class_get_runtime_info (method
->klass
))
1733 /* No vtable created yet */
1735 vtable
= mono_class_vtable_checked (td
->rtm
->domain
, method
->klass
, error
);
1736 if (!is_ok (error
)) {
1737 mono_error_cleanup (error
);
1740 if (!vtable
->initialized
)
1744 /* We currently access at runtime the wrapper data */
1745 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1748 /* Our usage of `emit_store_value_as_local ()` for nint, nuint and nfloat
1749 * is kinda hacky, and doesn't work with the inliner */
1750 if (mono_class_get_magic_index (method
->klass
) >= 0)
1757 interp_inline_method (TransformData
*td
, MonoMethod
*target_method
, MonoMethodHeader
*header
, MonoError
*error
)
1759 const unsigned char *prev_ip
, *prev_il_code
, *prev_in_start
;
1760 StackInfo
**prev_stack_state
;
1761 int *prev_stack_height
, *prev_vt_stack_size
, *prev_clause_indexes
, *prev_in_offsets
;
1762 guint8
*prev_is_bb_start
;
1764 unsigned int prev_max_stack_height
, prev_max_vt_sp
, prev_total_locals_size
;
1765 int prev_n_data_items
;
1768 MonoGenericContext
*generic_context
= NULL
;
1769 StackInfo
*prev_param_area
;
1770 MonoMethod
*prev_inlined_method
;
1771 MonoMethodSignature
*csignature
= mono_method_signature_internal (target_method
);
1772 int nargs
= csignature
->param_count
+ !!csignature
->hasthis
;
1773 InterpInst
*prev_last_ins
;
1775 if (csignature
->is_inflated
)
1776 generic_context
= mono_method_get_context (target_method
);
1778 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (target_method
);
1779 if (generic_container
)
1780 generic_context
= &generic_container
->context
;
1784 prev_il_code
= td
->il_code
;
1785 prev_in_start
= td
->in_start
;
1786 prev_sp_offset
= td
->sp
- td
->stack
;
1787 prev_vt_sp
= td
->vt_sp
;
1788 prev_stack_state
= td
->stack_state
;
1789 prev_stack_height
= td
->stack_height
;
1790 prev_vt_stack_size
= td
->vt_stack_size
;
1791 prev_clause_indexes
= td
->clause_indexes
;
1792 prev_inlined_method
= td
->inlined_method
;
1793 prev_last_ins
= td
->last_ins
;
1794 td
->inlined_method
= target_method
;
1796 prev_max_stack_height
= td
->max_stack_height
;
1797 prev_max_vt_sp
= td
->max_vt_sp
;
1798 prev_total_locals_size
= td
->total_locals_size
;
1800 prev_n_data_items
= td
->n_data_items
;
1801 prev_in_offsets
= td
->in_offsets
;
1802 td
->in_offsets
= (int*)g_malloc0((header
->code_size
+ 1) * sizeof(int));
1804 prev_is_bb_start
= td
->is_bb_start
;
1806 /* Inlining pops the arguments, restore the stack */
1807 prev_param_area
= (StackInfo
*)g_malloc (nargs
* sizeof (StackInfo
));
1808 memcpy (prev_param_area
, &td
->sp
[-nargs
], nargs
* sizeof (StackInfo
));
1810 if (td
->verbose_level
)
1811 g_print ("Inline start method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1812 ret
= generate_code (td
, target_method
, header
, generic_context
, error
);
1815 if (td
->verbose_level
)
1816 g_print ("Inline aborted method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1817 td
->max_stack_height
= prev_max_stack_height
;
1818 td
->max_vt_sp
= prev_max_vt_sp
;
1819 td
->total_locals_size
= prev_total_locals_size
;
1822 /* Remove any newly added items */
1823 for (i
= prev_n_data_items
; i
< td
->n_data_items
; i
++) {
1824 g_hash_table_remove (td
->data_hash
, td
->data_items
[i
]);
1826 td
->n_data_items
= prev_n_data_items
;
1827 td
->sp
= td
->stack
+ prev_sp_offset
;
1828 memcpy (&td
->sp
[-nargs
], prev_param_area
, nargs
* sizeof (StackInfo
));
1829 td
->vt_sp
= prev_vt_sp
;
1830 td
->last_ins
= prev_last_ins
;
1832 td
->last_ins
->next
= NULL
;
1833 UnlockedIncrement (&mono_interp_stats
.inline_failures
);
1835 if (td
->verbose_level
)
1836 g_print ("Inline end method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1837 UnlockedIncrement (&mono_interp_stats
.inlined_methods
);
1838 // Make sure we have an IR instruction associated with the now removed IL CALL
1839 // FIXME This could be prettier. We might be able to make inlining saner now that
1840 // that we can easily tweak the instruction list.
1841 if (!prev_inlined_method
) {
1842 if (prev_last_ins
) {
1843 if (prev_last_ins
->next
)
1844 prev_last_ins
->next
->il_offset
= prev_in_start
- prev_il_code
;
1845 } else if (td
->first_ins
) {
1846 td
->first_ins
->il_offset
= prev_in_start
- prev_il_code
;
1852 td
->in_start
= prev_in_start
;
1853 td
->il_code
= prev_il_code
;
1854 td
->stack_state
= prev_stack_state
;
1855 td
->stack_height
= prev_stack_height
;
1856 td
->vt_stack_size
= prev_vt_stack_size
;
1857 td
->clause_indexes
= prev_clause_indexes
;
1858 td
->is_bb_start
= prev_is_bb_start
;
1859 td
->inlined_method
= prev_inlined_method
;
1861 g_free (td
->in_offsets
);
1862 td
->in_offsets
= prev_in_offsets
;
1864 g_free (prev_param_area
);
1869 interp_constrained_box (TransformData
*td
, MonoDomain
*domain
, MonoClass
*constrained_class
, MonoMethodSignature
*csignature
, MonoError
*error
)
1871 int mt
= mint_type (m_class_get_byval_arg (constrained_class
));
1872 if (mono_class_is_nullable (constrained_class
)) {
1873 g_assert (mt
== MINT_TYPE_VT
);
1874 interp_add_ins (td
, MINT_BOX_NULLABLE
);
1875 td
->last_ins
->data
[0] = get_data_item_index (td
, constrained_class
);
1876 td
->last_ins
->data
[1] = csignature
->param_count
| ((td
->sp
- 1 - csignature
->param_count
)->type
!= STACK_TYPE_MP
? 0 : BOX_NOT_CLEAR_VT_SP
);
1878 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, constrained_class
, error
);
1879 return_if_nok (error
);
1881 if (mt
== MINT_TYPE_VT
) {
1882 interp_add_ins (td
, MINT_BOX_VT
);
1883 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
1884 td
->last_ins
->data
[1] = csignature
->param_count
| ((td
->sp
- 1 - csignature
->param_count
)->type
!= STACK_TYPE_MP
? 0 : BOX_NOT_CLEAR_VT_SP
);
1886 interp_add_ins (td
, MINT_BOX
);
1887 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
1888 td
->last_ins
->data
[1] = csignature
->param_count
;
1893 /* Return FALSE if error, including inline failure */
1895 interp_transform_call (TransformData
*td
, MonoMethod
*method
, MonoMethod
*target_method
, MonoDomain
*domain
, MonoGenericContext
*generic_context
, unsigned char *is_bb_start
, MonoClass
*constrained_class
, gboolean readonly
, MonoError
*error
, gboolean check_visibility
, gboolean save_last_error
)
1897 MonoImage
*image
= m_class_get_image (method
->klass
);
1898 MonoMethodSignature
*csignature
;
1899 int is_virtual
= *td
->ip
== CEE_CALLVIRT
;
1900 int calli
= *td
->ip
== CEE_CALLI
|| *td
->ip
== CEE_MONO_CALLI_EXTRA_ARG
;
1902 guint32 vt_stack_used
= 0;
1903 guint32 vt_res_size
= 0;
1907 int need_null_check
= is_virtual
;
1909 guint32 token
= read32 (td
->ip
+ 1);
1911 if (target_method
== NULL
) {
1914 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1915 csignature
= (MonoMethodSignature
*)mono_method_get_wrapper_data (method
, token
);
1917 csignature
= mono_metadata_parse_signature_checked (image
, token
, error
);
1918 return_val_if_nok (error
, FALSE
);
1921 if (generic_context
) {
1922 csignature
= mono_inflate_generic_signature (csignature
, generic_context
, error
);
1923 return_val_if_nok (error
, FALSE
);
1927 * The compiled interp entry wrapper is passed to runtime_invoke instead of
1928 * the InterpMethod pointer. FIXME
1930 native
= csignature
->pinvoke
|| method
->wrapper_type
== MONO_WRAPPER_RUNTIME_INVOKE
;
1933 target_method
= NULL
;
1935 if (method
->wrapper_type
== MONO_WRAPPER_NONE
) {
1936 target_method
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
1937 return_val_if_nok (error
, FALSE
);
1939 target_method
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
1940 csignature
= mono_method_signature_internal (target_method
);
1942 if (generic_context
) {
1943 csignature
= mono_inflate_generic_signature (csignature
, generic_context
, error
);
1944 return_val_if_nok (error
, FALSE
);
1945 target_method
= mono_class_inflate_generic_method_checked (target_method
, generic_context
, error
);
1946 return_val_if_nok (error
, FALSE
);
1950 csignature
= mono_method_signature_internal (target_method
);
1953 if (check_visibility
&& target_method
&& !mono_method_can_access_method (method
, target_method
))
1954 interp_generate_mae_throw (td
, method
, target_method
);
1956 if (target_method
&& target_method
->string_ctor
) {
1957 /* Create the real signature */
1958 MonoMethodSignature
*ctor_sig
= mono_metadata_signature_dup_mempool (td
->mempool
, csignature
);
1959 ctor_sig
->ret
= m_class_get_byval_arg (mono_defaults
.string_class
);
1961 csignature
= ctor_sig
;
1965 if (target_method
&& interp_handle_intrinsics (td
, target_method
, constrained_class
, csignature
, readonly
, &op
))
1968 if (constrained_class
) {
1969 if (m_class_is_enumtype (constrained_class
) && !strcmp (target_method
->name
, "GetHashCode")) {
1970 /* Use the corresponding method from the base type to avoid boxing */
1971 MonoType
*base_type
= mono_class_enum_basetype_internal (constrained_class
);
1972 g_assert (base_type
);
1973 constrained_class
= mono_class_from_mono_type_internal (base_type
);
1974 target_method
= mono_class_get_method_from_name_checked (constrained_class
, target_method
->name
, 0, 0, error
);
1975 mono_error_assert_ok (error
);
1976 g_assert (target_method
);
1980 if (constrained_class
) {
1981 mono_class_setup_vtable (constrained_class
);
1982 if (mono_class_has_failure (constrained_class
)) {
1983 mono_error_set_for_class_failure (error
, constrained_class
);
1987 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
);
1989 target_method
= mono_get_method_constrained_with_method (image
, target_method
, constrained_class
, generic_context
, error
);
1991 g_print (" : %s::%s. %s (%p)\n", target_method
->klass
->name
, target_method
->name
, mono_signature_full_name (target_method
->signature
), target_method
);
1993 /* Intrinsics: Try again, it could be that `mono_get_method_constrained_with_method` resolves to a method that we can substitute */
1994 if (target_method
&& interp_handle_intrinsics (td
, target_method
, constrained_class
, csignature
, readonly
, &op
))
1997 return_val_if_nok (error
, FALSE
);
1998 mono_class_setup_vtable (target_method
->klass
);
2000 if (!m_class_is_valuetype (constrained_class
)) {
2001 /* managed pointer on the stack, we need to deref that puppy */
2002 interp_add_ins (td
, MINT_LDIND_I
);
2003 td
->last_ins
->data
[0] = csignature
->param_count
;
2004 } else if (target_method
->klass
== mono_defaults
.object_class
|| target_method
->klass
== m_class_get_parent (mono_defaults
.enum_class
) || target_method
->klass
== mono_defaults
.enum_class
) {
2005 if (target_method
->klass
== mono_defaults
.enum_class
&& (td
->sp
- csignature
->param_count
- 1)->type
== STACK_TYPE_MP
) {
2006 /* managed pointer on the stack, we need to deref that puppy */
2007 /* Always load the entire stackval, to handle also the case where the enum has long storage */
2008 interp_add_ins (td
, MINT_LDIND_I8
);
2009 td
->last_ins
->data
[0] = csignature
->param_count
;
2012 interp_constrained_box (td
, domain
, constrained_class
, csignature
, error
);
2013 return_val_if_nok (error
, FALSE
);
2015 if (target_method
->klass
!= constrained_class
) {
2017 * The type parameter is instantiated as a valuetype,
2018 * but that type doesn't override the method we're
2019 * calling, so we need to box `this'.
2021 if (target_method
->klass
== mono_defaults
.enum_class
&& (td
->sp
- csignature
->param_count
- 1)->type
== STACK_TYPE_MP
) {
2022 /* managed pointer on the stack, we need to deref that puppy */
2023 /* Always load the entire stackval, to handle also the case where the enum has long storage */
2024 interp_add_ins (td
, MINT_LDIND_I8
);
2025 td
->last_ins
->data
[0] = csignature
->param_count
;
2028 interp_constrained_box (td
, domain
, constrained_class
, csignature
, error
);
2029 return_val_if_nok (error
, FALSE
);
2036 mono_class_init_internal (target_method
->klass
);
2038 if (!is_virtual
&& target_method
&& (target_method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)) {
2039 if (!mono_class_is_interface (method
->klass
))
2040 interp_generate_bie_throw (td
);
2045 if (is_virtual
&& target_method
&& (!(target_method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) ||
2046 (MONO_METHOD_IS_FINAL (target_method
) &&
2047 target_method
->wrapper_type
!= MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)) &&
2048 !(mono_class_is_marshalbyref (target_method
->klass
))) {
2049 /* Not really virtual, just needs a null check */
2051 need_null_check
= TRUE
;
2054 CHECK_STACK (td
, csignature
->param_count
+ csignature
->hasthis
);
2055 if (!td
->gen_sdb_seq_points
&& !calli
&& op
== -1 && (!is_virtual
|| (target_method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) == 0) &&
2056 (target_method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) == 0 &&
2057 (target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) == 0 &&
2058 !(target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_NOINLINING
)) {
2059 (void)mono_class_vtable_checked (domain
, target_method
->klass
, error
);
2060 return_val_if_nok (error
, FALSE
);
2062 if (method
== target_method
&& *(td
->ip
+ 5) == CEE_RET
&& !(csignature
->hasthis
&& m_class_is_valuetype (target_method
->klass
))) {
2063 if (td
->inlined_method
)
2066 if (td
->verbose_level
)
2067 g_print ("Optimize tail call of %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
2069 for (i
= csignature
->param_count
- 1 + !!csignature
->hasthis
; i
>= 0; --i
)
2072 interp_add_ins (td
, MINT_BR_S
);
2073 // We are branching to the beginning of the method
2074 td
->last_ins
->data
[0] = 0;
2075 if (!is_bb_start
[td
->ip
+ 5 - td
->il_code
])
2076 ++td
->ip
; /* gobble the CEE_RET if it isn't branched to */
2082 target_method
= interp_transform_internal_calls (method
, target_method
, csignature
, is_virtual
);
2084 if (csignature
->call_convention
== MONO_CALL_VARARG
) {
2085 csignature
= mono_method_get_signature_checked (target_method
, image
, token
, generic_context
, error
);
2086 int vararg_stack
= 0;
2088 * For vararg calls, ArgIterator expects the signature and the varargs to be
2089 * stored in a linear memory. We allocate the necessary vt_stack space for
2090 * this. All varargs will be pushed to the vt_stack at call site.
2092 vararg_stack
+= sizeof (gpointer
);
2093 for (i
= csignature
->sentinelpos
; i
< csignature
->param_count
; ++i
) {
2094 int align
, arg_size
;
2095 arg_size
= mono_type_stack_size (csignature
->params
[i
], &align
);
2096 vararg_stack
+= arg_size
;
2098 vt_stack_used
+= ALIGN_TO (vararg_stack
, MINT_VT_ALIGNMENT
);
2099 PUSH_VT (td
, vararg_stack
);
2102 if (need_null_check
) {
2103 interp_add_ins (td
, MINT_CKNULL_N
);
2104 td
->last_ins
->data
[0] = csignature
->param_count
+ 1;
2107 g_assert (csignature
->call_convention
!= MONO_CALL_FASTCALL
);
2108 if ((mono_interp_opt
& INTERP_OPT_INLINE
) && op
== -1 && !is_virtual
&& target_method
&& interp_method_check_inlining (td
, target_method
)) {
2109 MonoMethodHeader
*mheader
= interp_method_get_header (target_method
, error
);
2110 return_val_if_nok (error
, FALSE
);
2112 if (interp_inline_method (td
, target_method
, mheader
, error
)) {
2118 /* Don't inline methods that do calls */
2119 if (op
== -1 && td
->inlined_method
)
2122 td
->sp
-= csignature
->param_count
+ !!csignature
->hasthis
;
2123 for (i
= 0; i
< csignature
->param_count
; ++i
) {
2124 if (td
->sp
[i
+ !!csignature
->hasthis
].type
== STACK_TYPE_VT
) {
2126 MonoClass
*klass
= mono_class_from_mono_type_internal (csignature
->params
[i
]);
2127 if (csignature
->pinvoke
&& method
->wrapper_type
!= MONO_WRAPPER_NONE
)
2128 size
= mono_class_native_size (klass
, NULL
);
2130 size
= mono_class_value_size (klass
, NULL
);
2131 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
2132 vt_stack_used
+= size
;
2136 /* need to handle typedbyref ... */
2137 if (csignature
->ret
->type
!= MONO_TYPE_VOID
) {
2138 int mt
= mint_type(csignature
->ret
);
2139 MonoClass
*klass
= mono_class_from_mono_type_internal (csignature
->ret
);
2140 if (mt
== MINT_TYPE_VT
) {
2141 if (csignature
->pinvoke
&& method
->wrapper_type
!= MONO_WRAPPER_NONE
)
2142 vt_res_size
= mono_class_native_size (klass
, NULL
);
2144 vt_res_size
= mono_class_value_size (klass
, NULL
);
2145 if (mono_class_has_failure (klass
)) {
2146 mono_error_set_for_class_failure (error
, klass
);
2149 PUSH_VT(td
, vt_res_size
);
2151 PUSH_TYPE(td
, stack_type
[mt
], klass
);
2155 /* We need to convert delegate invoke to a indirect call on the interp_invoke_impl field */
2156 if (target_method
&& m_class_get_parent (target_method
->klass
) == mono_defaults
.multicastdelegate_class
) {
2157 const char *name
= target_method
->name
;
2158 if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
2160 interp_add_ins (td
, MINT_LD_DELEGATE_INVOKE_IMPL
);
2161 td
->last_ins
->data
[0] = csignature
->param_count
+ 1;
2166 interp_add_ins (td
, op
);
2168 if (op
== MINT_LDLEN
) {
2169 #ifdef MONO_BIG_ARRAYS
2170 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I8
);
2172 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
2175 if (op
== MINT_LDELEMA
|| op
== MINT_LDELEMA_TC
) {
2176 td
->last_ins
->data
[0] = get_data_item_index (td
, m_class_get_element_class (target_method
->klass
));
2177 td
->last_ins
->data
[1] = 1 + m_class_get_rank (target_method
->klass
);
2180 if (op
== MINT_CALLRUN
) {
2181 td
->last_ins
->data
[0] = get_data_item_index (td
, target_method
);
2182 td
->last_ins
->data
[1] = get_data_item_index (td
, mono_method_signature_internal (target_method
));
2184 } else if (!calli
&& !is_virtual
&& jit_call_supported (target_method
, csignature
)) {
2185 interp_add_ins (td
, MINT_JIT_CALL
);
2186 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)mono_interp_get_imethod (domain
, target_method
, error
));
2187 mono_error_assert_ok (error
);
2189 #ifndef MONO_ARCH_HAS_NO_PROPER_MONOCTX
2190 /* Try using fast icall path for simple signatures */
2191 if (native
&& !method
->dynamic
)
2192 op
= interp_icall_op_for_sig (csignature
);
2194 if (csignature
->call_convention
== MONO_CALL_VARARG
)
2195 interp_add_ins (td
, MINT_CALL_VARARG
);
2197 interp_add_ins (td
, native
? ((op
!= -1) ? MINT_CALLI_NAT_FAST
: MINT_CALLI_NAT
) : MINT_CALLI
);
2198 else if (is_virtual
&& !mono_class_is_marshalbyref (target_method
->klass
))
2199 interp_add_ins (td
, is_void
? MINT_VCALLVIRT_FAST
: MINT_CALLVIRT_FAST
);
2200 else if (is_virtual
)
2201 interp_add_ins (td
, is_void
? MINT_VCALLVIRT
: MINT_CALLVIRT
);
2203 interp_add_ins (td
, is_void
? MINT_VCALL
: MINT_CALL
);
2206 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)csignature
);
2208 td
->last_ins
->data
[1] = op
;
2209 if (td
->last_ins
->opcode
== MINT_CALLI_NAT_FAST
)
2210 td
->last_ins
->data
[2] = save_last_error
;
2211 } else if (op
== -1 && td
->last_ins
->opcode
== MINT_CALLI_NAT
) {
2212 td
->last_ins
->data
[1] = save_last_error
;
2215 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)mono_interp_get_imethod (domain
, target_method
, error
));
2216 return_val_if_nok (error
, FALSE
);
2217 if (csignature
->call_convention
== MONO_CALL_VARARG
)
2218 td
->last_ins
->data
[1] = get_data_item_index (td
, (void *)csignature
);
2219 else if (is_virtual
&& !mono_class_is_marshalbyref (target_method
->klass
)) {
2220 /* FIXME Use fastpath also for MBRO. Asserts in mono_method_get_vtable_slot */
2221 if (mono_class_is_interface (target_method
->klass
))
2222 td
->last_ins
->data
[1] = -2 * MONO_IMT_SIZE
+ mono_method_get_imt_slot (target_method
);
2224 td
->last_ins
->data
[1] = mono_method_get_vtable_slot (target_method
);
2229 if (vt_stack_used
!= 0 || vt_res_size
!= 0) {
2230 interp_add_ins (td
, MINT_VTRESULT
);
2231 td
->last_ins
->data
[0] = vt_res_size
;
2232 WRITE32_INS (td
->last_ins
, 1, &vt_stack_used
);
2233 td
->vt_sp
-= vt_stack_used
;
2239 static MonoClassField
*
2240 interp_field_from_token (MonoMethod
*method
, guint32 token
, MonoClass
**klass
, MonoGenericContext
*generic_context
, MonoError
*error
)
2242 MonoClassField
*field
= NULL
;
2243 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
2244 field
= (MonoClassField
*) mono_method_get_wrapper_data (method
, token
);
2245 *klass
= field
->parent
;
2247 mono_class_setup_fields (field
->parent
);
2249 field
= mono_field_from_token_checked (m_class_get_image (method
->klass
), token
, klass
, generic_context
, error
);
2250 return_val_if_nok (error
, NULL
);
2253 if (!method
->skip_visibility
&& !mono_method_can_access_field (method
, field
)) {
2254 char *method_fname
= mono_method_full_name (method
, TRUE
);
2255 char *field_fname
= mono_field_full_name (field
);
2256 mono_error_set_generic_error (error
, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname
, method_fname
);
2257 g_free (method_fname
);
2258 g_free (field_fname
);
2265 static InterpBasicBlock
*
2266 get_bb (TransformData
*td
, InterpBasicBlock
*cbb
, unsigned char *ip
)
2268 int offset
= ip
- td
->il_code
;
2269 InterpBasicBlock
*bb
= td
->offset_to_bb
[offset
];
2272 bb
= (InterpBasicBlock
*)mono_mempool_alloc0 (td
->mempool
, sizeof (InterpBasicBlock
));
2274 td
->offset_to_bb
[offset
] = bb
;
2276 td
->basic_blocks
= g_list_append_mempool (td
->mempool
, td
->basic_blocks
, bb
);
2280 bb
->preds
= g_slist_prepend_mempool (td
->mempool
, bb
->preds
, cbb
);
2287 * Compute the set of IL level basic blocks.
2290 get_basic_blocks (TransformData
*td
)
2292 guint8
*start
= (guint8
*)td
->il_code
;
2293 guint8
*end
= (guint8
*)td
->il_code
+ td
->code_size
;
2295 unsigned char *target
;
2298 const MonoOpcode
*opcode
;
2299 InterpBasicBlock
*cbb
;
2301 td
->offset_to_bb
= (InterpBasicBlock
**)mono_mempool_alloc0 (td
->mempool
, sizeof (InterpBasicBlock
*) * (end
- start
+ 1));
2302 td
->entry_bb
= cbb
= get_bb (td
, NULL
, start
);
2305 cli_addr
= ip
- start
;
2306 td
->offset_to_bb
[cli_addr
] = cbb
;
2307 i
= mono_opcode_value ((const guint8
**)&ip
, end
);
2308 opcode
= &mono_opcodes
[i
];
2309 switch (opcode
->argument
) {
2310 case MonoInlineNone
:
2313 case MonoInlineString
:
2314 case MonoInlineType
:
2315 case MonoInlineField
:
2316 case MonoInlineMethod
:
2319 case MonoShortInlineR
:
2326 case MonoShortInlineVar
:
2327 case MonoShortInlineI
:
2330 case MonoShortInlineBrTarget
:
2331 target
= start
+ cli_addr
+ 2 + (signed char)ip
[1];
2332 get_bb (td
, cbb
, target
);
2334 cbb
= get_bb (td
, cbb
, ip
);
2336 case MonoInlineBrTarget
:
2337 target
= start
+ cli_addr
+ 5 + (gint32
)read32 (ip
+ 1);
2338 get_bb (td
, cbb
, target
);
2340 cbb
= get_bb (td
, cbb
, ip
);
2342 case MonoInlineSwitch
: {
2343 guint32 n
= read32 (ip
+ 1);
2346 cli_addr
+= 5 + 4 * n
;
2347 target
= start
+ cli_addr
;
2348 get_bb (td
, cbb
, target
);
2350 for (j
= 0; j
< n
; ++j
) {
2351 target
= start
+ cli_addr
+ (gint32
)read32 (ip
);
2352 get_bb (td
, cbb
, target
);
2355 cbb
= get_bb (td
, cbb
, ip
);
2363 g_assert_not_reached ();
2367 cbb
= get_bb (td
, NULL
, ip
);
2372 interp_save_debug_info (InterpMethod
*rtm
, MonoMethodHeader
*header
, TransformData
*td
, GArray
*line_numbers
)
2374 MonoDebugMethodJitInfo
*dinfo
;
2377 if (!mono_debug_enabled ())
2381 * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
2384 dinfo
= g_new0 (MonoDebugMethodJitInfo
, 1);
2385 dinfo
->num_params
= rtm
->param_count
;
2386 dinfo
->params
= g_new0 (MonoDebugVarInfo
, dinfo
->num_params
);
2387 dinfo
->num_locals
= header
->num_locals
;
2388 dinfo
->locals
= g_new0 (MonoDebugVarInfo
, header
->num_locals
);
2389 dinfo
->code_start
= (guint8
*)rtm
->code
;
2390 dinfo
->code_size
= td
->new_code_end
- td
->new_code
;
2391 dinfo
->epilogue_begin
= 0;
2392 dinfo
->has_var_info
= TRUE
;
2393 dinfo
->num_line_numbers
= line_numbers
->len
;
2394 dinfo
->line_numbers
= g_new0 (MonoDebugLineNumberEntry
, dinfo
->num_line_numbers
);
2396 for (i
= 0; i
< dinfo
->num_params
; i
++) {
2397 MonoDebugVarInfo
*var
= &dinfo
->params
[i
];
2398 var
->type
= rtm
->param_types
[i
];
2400 for (i
= 0; i
< dinfo
->num_locals
; i
++) {
2401 MonoDebugVarInfo
*var
= &dinfo
->locals
[i
];
2402 var
->type
= mono_metadata_type_dup (NULL
, header
->locals
[i
]);
2405 for (i
= 0; i
< dinfo
->num_line_numbers
; i
++)
2406 dinfo
->line_numbers
[i
] = g_array_index (line_numbers
, MonoDebugLineNumberEntry
, i
);
2407 mono_debug_add_method (rtm
->method
, dinfo
, rtm
->domain
);
2409 mono_debug_free_method_jit_info (dinfo
);
2412 /* Same as the code in seq-points.c */
2414 insert_pred_seq_point (SeqPoint
*last_sp
, SeqPoint
*sp
, GSList
**next
)
2417 int src_index
= last_sp
->next_offset
;
2418 int dst_index
= sp
->next_offset
;
2420 /* bb->in_bb might contain duplicates */
2421 for (l
= next
[src_index
]; l
; l
= l
->next
)
2422 if (GPOINTER_TO_UINT (l
->data
) == dst_index
)
2425 next
[src_index
] = g_slist_append (next
[src_index
], GUINT_TO_POINTER (dst_index
));
2429 recursively_make_pred_seq_points (TransformData
*td
, InterpBasicBlock
*bb
)
2431 SeqPoint
** const MONO_SEQ_SEEN_LOOP
= (SeqPoint
**)GINT_TO_POINTER(-1);
2434 GArray
*predecessors
= g_array_new (FALSE
, TRUE
, sizeof (gpointer
));
2435 GHashTable
*seen
= g_hash_table_new_full (g_direct_hash
, NULL
, NULL
, NULL
);
2437 // Insert/remove sentinel into the memoize table to detect loops containing bb
2438 bb
->pred_seq_points
= MONO_SEQ_SEEN_LOOP
;
2440 for (l
= bb
->preds
; l
; l
= l
->next
) {
2441 InterpBasicBlock
*in_bb
= (InterpBasicBlock
*)l
->data
;
2443 // This bb has the last seq point, append it and continue
2444 if (in_bb
->last_seq_point
!= NULL
) {
2445 predecessors
= g_array_append_val (predecessors
, in_bb
->last_seq_point
);
2449 // We've looped or handled this before, exit early.
2450 // No last sequence points to find.
2451 if (in_bb
->pred_seq_points
== MONO_SEQ_SEEN_LOOP
)
2454 // Take sequence points from incoming basic blocks
2456 if (in_bb
== td
->entry_bb
)
2459 if (in_bb
->pred_seq_points
== NULL
)
2460 recursively_make_pred_seq_points (td
, in_bb
);
2462 // Union sequence points with incoming bb's
2463 for (int i
=0; i
< in_bb
->num_pred_seq_points
; i
++) {
2464 if (!g_hash_table_lookup (seen
, in_bb
->pred_seq_points
[i
])) {
2465 g_array_append_val (predecessors
, in_bb
->pred_seq_points
[i
]);
2466 g_hash_table_insert (seen
, in_bb
->pred_seq_points
[i
], (gpointer
)&MONO_SEQ_SEEN_LOOP
);
2469 // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
2472 g_hash_table_destroy (seen
);
2474 if (predecessors
->len
!= 0) {
2475 bb
->pred_seq_points
= (SeqPoint
**)mono_mempool_alloc0 (td
->mempool
, sizeof (SeqPoint
*) * predecessors
->len
);
2476 bb
->num_pred_seq_points
= predecessors
->len
;
2478 for (int newer
= 0; newer
< bb
->num_pred_seq_points
; newer
++) {
2479 bb
->pred_seq_points
[newer
] = (SeqPoint
*)g_array_index (predecessors
, gpointer
, newer
);
2483 g_array_free (predecessors
, TRUE
);
2487 collect_pred_seq_points (TransformData
*td
, InterpBasicBlock
*bb
, SeqPoint
*seqp
, GSList
**next
)
2489 // Doesn't have a last sequence point, must find from incoming basic blocks
2490 if (bb
->pred_seq_points
== NULL
&& bb
!= td
->entry_bb
)
2491 recursively_make_pred_seq_points (td
, bb
);
2493 for (int i
= 0; i
< bb
->num_pred_seq_points
; i
++)
2494 insert_pred_seq_point (bb
->pred_seq_points
[i
], seqp
, next
);
2500 save_seq_points (TransformData
*td
, MonoJitInfo
*jinfo
)
2503 int i
, seq_info_size
;
2504 MonoSeqPointInfo
*info
;
2505 GSList
**next
= NULL
;
2508 if (!td
->gen_sdb_seq_points
)
2512 * For each sequence point, compute the list of sequence points immediately
2513 * following it, this is needed to implement 'step over' in the debugger agent.
2514 * Similar to the code in mono_save_seq_point_info ().
2516 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2517 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2519 /* Store the seq point index here temporarily */
2520 sp
->next_offset
= i
;
2522 next
= (GSList
**)mono_mempool_alloc0 (td
->mempool
, sizeof (GList
*) * td
->seq_points
->len
);
2523 for (bblist
= td
->basic_blocks
; bblist
; bblist
= bblist
->next
) {
2524 InterpBasicBlock
*bb
= (InterpBasicBlock
*)bblist
->data
;
2526 GSList
*bb_seq_points
= g_slist_reverse (bb
->seq_points
);
2527 SeqPoint
*last
= NULL
;
2528 for (GSList
*l
= bb_seq_points
; l
; l
= l
->next
) {
2529 SeqPoint
*sp
= (SeqPoint
*)l
->data
;
2531 if (sp
->il_offset
== METHOD_ENTRY_IL_OFFSET
|| sp
->il_offset
== METHOD_EXIT_IL_OFFSET
)
2532 /* Used to implement method entry/exit events */
2536 /* Link with the previous seq point in the same bb */
2537 next
[last
->next_offset
] = g_slist_append_mempool (td
->mempool
, next
[last
->next_offset
], GINT_TO_POINTER (sp
->next_offset
));
2539 /* Link with the last bb in the previous bblocks */
2540 collect_pred_seq_points (td
, bb
, sp
, next
);
2546 /* Serialize the seq points into a byte array */
2547 array
= g_byte_array_new ();
2548 SeqPoint zero_seq_point
= {0};
2549 SeqPoint
* last_seq_point
= &zero_seq_point
;
2550 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2551 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2553 sp
->next_offset
= 0;
2554 if (mono_seq_point_info_add_seq_point (array
, sp
, last_seq_point
, next
[i
], TRUE
))
2555 last_seq_point
= sp
;
2558 if (td
->verbose_level
) {
2559 g_print ("\nSEQ POINT MAP FOR %s: \n", td
->method
->name
);
2561 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2562 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2568 g_print ("\tIL0x%x[0x%0x] ->", sp
->il_offset
, sp
->native_offset
);
2569 for (l
= next
[i
]; l
; l
= l
->next
) {
2570 int next_index
= GPOINTER_TO_UINT (l
->data
);
2571 g_print (" IL0x%x", ((SeqPoint
*)g_ptr_array_index (td
->seq_points
, next_index
))->il_offset
);
2577 info
= mono_seq_point_info_new (array
->len
, TRUE
, array
->data
, TRUE
, &seq_info_size
);
2578 mono_atomic_fetch_add_i32 (&mono_jit_stats
.allocated_seq_points_size
, seq_info_size
);
2580 g_byte_array_free (array
, TRUE
);
2582 jinfo
->seq_points
= info
;
2585 #define BARRIER_IF_VOLATILE(td) \
2588 interp_add_ins (td, MINT_MONO_MEMORY_BARRIER); \
2589 volatile_ = FALSE; \
2593 #define INLINE_FAILURE \
2595 if (td->method != method) \
2600 interp_method_compute_offsets (InterpMethod
*imethod
, MonoMethodSignature
*signature
, MonoMethodHeader
*header
)
2602 int i
, offset
, size
, align
;
2604 imethod
->local_offsets
= (guint32
*)g_malloc (header
->num_locals
* sizeof(guint32
));
2606 for (i
= 0; i
< header
->num_locals
; ++i
) {
2607 size
= mono_type_size (header
->locals
[i
], &align
);
2608 offset
+= align
- 1;
2609 offset
&= ~(align
- 1);
2610 imethod
->local_offsets
[i
] = offset
;
2613 offset
= (offset
+ 7) & ~7;
2615 imethod
->exvar_offsets
= (guint32
*)g_malloc (header
->num_clauses
* sizeof (guint32
));
2616 for (i
= 0; i
< header
->num_clauses
; i
++) {
2617 imethod
->exvar_offsets
[i
] = offset
;
2618 offset
+= sizeof (MonoObject
*);
2620 offset
= (offset
+ 7) & ~7;
2622 imethod
->locals_size
= offset
;
2623 g_assert (imethod
->locals_size
< 65536);
2627 get_arg_type (MonoMethodSignature
*signature
, int arg_n
)
2629 if (signature
->hasthis
&& arg_n
== 0)
2630 return mono_get_object_type ();
2631 return signature
->params
[arg_n
- !!signature
->hasthis
];
2635 init_bb_start (TransformData
*td
, MonoMethodHeader
*header
)
2637 const unsigned char *ip
, *end
;
2638 const MonoOpcode
*opcode
;
2639 int offset
, i
, in
, backwards
;
2641 /* intern the strings in the method. */
2643 end
= ip
+ header
->code_size
;
2645 td
->is_bb_start
[0] = 1;
2652 else if (in
== 0xf0) {
2654 in
= *ip
+ MONO_CEE_MONO_ICALL
;
2656 opcode
= &mono_opcodes
[in
];
2657 switch (opcode
->argument
) {
2658 case MonoInlineNone
:
2661 case MonoInlineString
:
2664 case MonoInlineType
:
2667 case MonoInlineMethod
:
2670 case MonoInlineField
:
2674 case MonoShortInlineR
:
2677 case MonoInlineBrTarget
:
2678 offset
= read32 (ip
+ 1);
2680 backwards
= offset
< 0;
2681 offset
+= ip
- header
->code
;
2682 g_assert (offset
>= 0 && offset
< header
->code_size
);
2683 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2685 case MonoShortInlineBrTarget
:
2686 offset
= ((gint8
*)ip
) [1];
2688 backwards
= offset
< 0;
2689 offset
+= ip
- header
->code
;
2690 g_assert (offset
>= 0 && offset
< header
->code_size
);
2691 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2696 case MonoShortInlineVar
:
2697 case MonoShortInlineI
:
2700 case MonoInlineSwitch
: {
2702 const unsigned char *next_ip
;
2706 next_ip
= ip
+ 4 * n
;
2707 for (i
= 0; i
< n
; i
++) {
2708 offset
= read32 (ip
);
2709 backwards
= offset
< 0;
2710 offset
+= next_ip
- header
->code
;
2711 g_assert (offset
>= 0 && offset
< header
->code_size
);
2712 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2722 g_assert_not_reached ();
2727 #ifdef NO_UNALIGNED_ACCESS
2729 get_unaligned_opcode (int opcode
)
2733 return MINT_LDFLD_I8_UNALIGNED
;
2735 return MINT_LDFLD_R8_UNALIGNED
;
2737 return MINT_STFLD_I8_UNALIGNED
;
2739 return MINT_STFLD_R8_UNALIGNED
;
2741 g_assert_not_reached ();
2748 interp_handle_isinst (TransformData
*td
, MonoClass
*klass
, gboolean isinst_instr
)
2750 /* Follow the logic from jit's handle_isinst */
2751 if (!mono_class_has_variant_generic_params (klass
)) {
2752 if (mono_class_is_interface (klass
))
2753 interp_add_ins (td
, isinst_instr
? MINT_ISINST_INTERFACE
: MINT_CASTCLASS_INTERFACE
);
2754 else if (!mono_class_is_marshalbyref (klass
) && m_class_get_rank (klass
) == 0 && !mono_class_is_nullable (klass
))
2755 interp_add_ins (td
, isinst_instr
? MINT_ISINST_COMMON
: MINT_CASTCLASS_COMMON
);
2757 interp_add_ins (td
, isinst_instr
? MINT_ISINST
: MINT_CASTCLASS
);
2759 interp_add_ins (td
, isinst_instr
? MINT_ISINST
: MINT_CASTCLASS
);
2761 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
2766 interp_emit_ldobj (TransformData
*td
, MonoClass
*klass
)
2768 int mt
= mint_type (m_class_get_byval_arg (klass
));
2771 if (mt
== MINT_TYPE_VT
) {
2772 interp_add_ins (td
, MINT_LDOBJ_VT
);
2773 size
= mono_class_value_size (klass
, NULL
);
2774 WRITE32_INS (td
->last_ins
, 0, &size
);
2777 int opcode
= interp_get_ldind_for_mt (mt
);
2778 interp_add_ins (td
, opcode
);
2781 SET_TYPE (td
->sp
- 1, stack_type
[mt
], klass
);
2785 interp_emit_stobj (TransformData
*td
, MonoClass
*klass
)
2787 int mt
= mint_type (m_class_get_byval_arg (klass
));
2789 if (mt
== MINT_TYPE_VT
) {
2791 interp_add_ins (td
, MINT_STOBJ_VT
);
2792 td
->last_ins
->data
[0] = get_data_item_index(td
, klass
);
2793 size
= mono_class_value_size (klass
, NULL
);
2800 opcode
= MINT_STIND_I1
;
2804 opcode
= MINT_STIND_I2
;
2807 opcode
= MINT_STIND_I4
;
2810 opcode
= MINT_STIND_I8
;
2813 opcode
= MINT_STIND_R4
;
2816 opcode
= MINT_STIND_R8
;
2819 opcode
= MINT_STIND_REF
;
2821 default: g_assert_not_reached (); break;
2823 interp_add_ins (td
, opcode
);
2829 interp_emit_ldsflda (TransformData
*td
, MonoClassField
*field
, MonoError
*error
)
2831 MonoDomain
*domain
= td
->rtm
->domain
;
2832 // Initialize the offset for the field
2833 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, field
->parent
, error
);
2834 return_if_nok (error
);
2836 if (mono_class_field_is_special_static (field
)) {
2839 mono_domain_lock (domain
);
2840 g_assert (domain
->special_static_fields
);
2841 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (domain
->special_static_fields
, field
));
2842 mono_domain_unlock (domain
);
2845 interp_add_ins (td
, MINT_LDSSFLDA
);
2846 WRITE32_INS(td
->last_ins
, 0, &offset
);
2848 interp_add_ins (td
, MINT_LDSFLDA
);
2849 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
2850 td
->last_ins
->data
[1] = get_data_item_index (td
, (char*)mono_vtable_get_static_field_data (vtable
) + field
->offset
);
2855 interp_emit_sfld_access (TransformData
*td
, MonoClassField
*field
, MonoClass
*field_class
, int mt
, gboolean is_load
, MonoError
*error
)
2857 MonoDomain
*domain
= td
->rtm
->domain
;
2858 // Initialize the offset for the field
2859 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, field
->parent
, error
);
2860 return_if_nok (error
);
2862 if (mono_class_field_is_special_static (field
)) {
2865 mono_domain_lock (domain
);
2866 g_assert (domain
->special_static_fields
);
2867 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (domain
->special_static_fields
, field
));
2868 mono_domain_unlock (domain
);
2871 // Offset is SpecialStaticOffset
2872 if ((offset
& 0x80000000) == 0 && mt
!= MINT_TYPE_VT
) {
2873 // This field is thread static
2874 interp_add_ins (td
, (is_load
? MINT_LDTSFLD_I1
: MINT_STTSFLD_I1
) + mt
);
2875 WRITE32_INS(td
->last_ins
, 0, &offset
);
2877 if (mt
== MINT_TYPE_VT
) {
2878 interp_add_ins (td
, is_load
? MINT_LDSSFLD_VT
: MINT_STSSFLD_VT
);
2879 WRITE32_INS(td
->last_ins
, 0, &offset
);
2881 int size
= mono_class_value_size (field_class
, NULL
);
2882 WRITE32_INS(td
->last_ins
, 2, &size
);
2884 interp_add_ins (td
, is_load
? MINT_LDSSFLD
: MINT_STSSFLD
);
2885 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
2886 WRITE32_INS(td
->last_ins
, 1, &offset
);
2891 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_LDSFLD_VT
: (MINT_LDSFLD_I1
+ mt
- MINT_TYPE_I1
));
2893 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_STSFLD_VT
: (MINT_STSFLD_I1
+ mt
- MINT_TYPE_I1
));
2895 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
2896 td
->last_ins
->data
[1] = get_data_item_index (td
, (char*)mono_vtable_get_static_field_data (vtable
) + field
->offset
);
2898 if (mt
== MINT_TYPE_VT
) {
2899 int size
= mono_class_value_size (field_class
, NULL
);
2900 WRITE32_INS(td
->last_ins
, 2, &size
);
2906 generate_code (TransformData
*td
, MonoMethod
*method
, MonoMethodHeader
*header
, MonoGenericContext
*generic_context
, MonoError
*error
)
2909 int offset
, mt
, i
, i32
;
2912 const unsigned char *end
;
2913 MonoSimpleBasicBlock
*bb
= NULL
, *original_bb
= NULL
;
2914 gboolean sym_seq_points
= FALSE
;
2915 MonoBitSet
*seq_point_locs
= NULL
;
2916 gboolean readonly
= FALSE
;
2917 gboolean volatile_
= FALSE
;
2918 MonoClass
*constrained_class
= NULL
;
2920 MonoClassField
*field
;
2921 MonoImage
*image
= m_class_get_image (method
->klass
);
2922 InterpMethod
*rtm
= td
->rtm
;
2923 MonoDomain
*domain
= rtm
->domain
;
2924 MonoMethodSignature
*signature
= mono_method_signature_internal (method
);
2925 gboolean ret
= TRUE
;
2926 gboolean emitted_funccall_seq_point
= FALSE
;
2927 guint32
*arg_offsets
= NULL
;
2928 guint32
*local_offsets
= NULL
;
2929 InterpInst
*last_seq_point
= NULL
;
2930 gboolean save_last_error
= FALSE
;
2932 original_bb
= bb
= mono_basic_block_split (method
, error
, header
);
2933 goto_if_nok (error
, exit
);
2936 td
->il_code
= header
->code
;
2937 td
->in_start
= td
->ip
= header
->code
;
2938 end
= td
->ip
+ header
->code_size
;
2939 td
->stack_state
= (StackInfo
**)g_malloc0(header
->code_size
* sizeof(StackInfo
*));
2940 td
->stack_height
= (int*)g_malloc(header
->code_size
* sizeof(int));
2941 td
->vt_stack_size
= (int*)g_malloc(header
->code_size
* sizeof(int));
2942 td
->clause_indexes
= (int*)g_malloc (header
->code_size
* sizeof (int));
2943 td
->is_bb_start
= (guint8
*)g_malloc0(header
->code_size
);
2945 init_bb_start (td
, header
);
2947 for (i
= 0; i
< header
->code_size
; i
++) {
2948 td
->stack_height
[i
] = -1;
2949 td
->clause_indexes
[i
] = -1;
2952 for (i
= 0; i
< header
->num_clauses
; i
++) {
2953 MonoExceptionClause
*c
= header
->clauses
+ i
;
2954 td
->stack_height
[c
->handler_offset
] = 0;
2955 td
->vt_stack_size
[c
->handler_offset
] = 0;
2956 td
->is_bb_start
[c
->handler_offset
] = 1;
2958 td
->stack_height
[c
->handler_offset
] = 1;
2959 td
->stack_state
[c
->handler_offset
] = (StackInfo
*)g_malloc0(sizeof(StackInfo
));
2960 td
->stack_state
[c
->handler_offset
][0].type
= STACK_TYPE_O
;
2961 td
->stack_state
[c
->handler_offset
][0].klass
= NULL
; /*FIX*/
2963 if (c
->flags
& MONO_EXCEPTION_CLAUSE_FILTER
) {
2964 td
->stack_height
[c
->data
.filter_offset
] = 0;
2965 td
->vt_stack_size
[c
->data
.filter_offset
] = 0;
2966 td
->is_bb_start
[c
->data
.filter_offset
] = 1;
2968 td
->stack_height
[c
->data
.filter_offset
] = 1;
2969 td
->stack_state
[c
->data
.filter_offset
] = (StackInfo
*)g_malloc0(sizeof(StackInfo
));
2970 td
->stack_state
[c
->data
.filter_offset
][0].type
= STACK_TYPE_O
;
2971 td
->stack_state
[c
->data
.filter_offset
][0].klass
= NULL
; /*FIX*/
2974 for (int j
= c
->handler_offset
; j
< c
->handler_offset
+ c
->handler_len
; ++j
) {
2975 if (td
->clause_indexes
[j
] == -1)
2976 td
->clause_indexes
[j
] = i
;
2980 if (td
->gen_sdb_seq_points
&& td
->method
== method
) {
2981 MonoDebugMethodInfo
*minfo
;
2982 get_basic_blocks (td
);
2984 minfo
= mono_debug_lookup_method (method
);
2987 MonoSymSeqPoint
*sps
;
2988 int i
, n_il_offsets
;
2990 mono_debug_get_seq_points (minfo
, NULL
, NULL
, NULL
, &sps
, &n_il_offsets
);
2992 seq_point_locs
= mono_bitset_mem_new (mono_mempool_alloc0 (td
->mempool
, mono_bitset_alloc_size (header
->code_size
, 0)), header
->code_size
, 0);
2993 sym_seq_points
= TRUE
;
2995 for (i
= 0; i
< n_il_offsets
; ++i
) {
2996 if (sps
[i
].il_offset
< header
->code_size
)
2997 mono_bitset_set_fast (seq_point_locs
, sps
[i
].il_offset
);
3001 MonoDebugMethodAsyncInfo
* asyncMethod
= mono_debug_lookup_method_async_debug_info (method
);
3003 for (i
= 0; asyncMethod
!= NULL
&& i
< asyncMethod
->num_awaits
; i
++) {
3004 mono_bitset_set_fast (seq_point_locs
, asyncMethod
->resume_offsets
[i
]);
3005 mono_bitset_set_fast (seq_point_locs
, asyncMethod
->yield_offsets
[i
]);
3007 mono_debug_free_method_async_debug_info (asyncMethod
);
3009 } else if (!method
->wrapper_type
&& !method
->dynamic
&& mono_debug_image_has_debug_info (m_class_get_image (method
->klass
))) {
3010 /* Methods without line number info like auto-generated property accessors */
3011 seq_point_locs
= mono_bitset_new (header
->code_size
, 0);
3012 sym_seq_points
= TRUE
;
3016 if (sym_seq_points
) {
3017 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3018 last_seq_point
->flags
= INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY
;
3021 if (mono_debugger_method_has_breakpoint (method
))
3022 interp_add_ins (td
, MINT_BREAKPOINT
);
3024 if (method
== td
->method
) {
3025 if (td
->verbose_level
) {
3026 char *tmp
= mono_disasm_code (NULL
, method
, td
->ip
, end
);
3027 char *name
= mono_method_full_name (method
, TRUE
);
3028 g_print ("Method %s, original code:\n", name
);
3029 g_print ("%s\n", tmp
);
3034 if (header
->num_locals
&& header
->init_locals
)
3035 interp_add_ins (td
, MINT_INITLOCALS
);
3037 if (mono_jit_trace_calls
!= NULL
&& mono_trace_eval (method
))
3038 interp_add_ins (td
, MINT_TRACE_ENTER
);
3039 else if (rtm
->prof_flags
& MONO_PROFILER_CALL_INSTRUMENTATION_ENTER
)
3040 interp_add_ins (td
, MINT_PROF_ENTER
);
3042 /* safepoint is required on method entry */
3043 if (mono_threads_are_safepoints_enabled ())
3044 interp_add_ins (td
, MINT_SAFEPOINT
);
3047 arg_offsets
= (guint32
*) g_malloc ((!!signature
->hasthis
+ signature
->param_count
) * sizeof (guint32
));
3048 /* Allocate locals to store inlined method args from stack */
3049 for (i
= signature
->param_count
- 1; i
>= 0; i
--) {
3050 offset
= create_interp_local (td
, signature
->params
[i
]);
3051 arg_offsets
[i
+ !!signature
->hasthis
] = offset
;
3052 store_local_general (td
, offset
, signature
->params
[i
]);
3055 if (signature
->hasthis
) {
3057 * If this is value type, it is passed by address and not by value.
3058 * FIXME We should use MINT_TYPE_P instead of MINT_TYPE_O
3060 MonoType
*type
= mono_get_object_type ();
3061 offset
= create_interp_local (td
, type
);
3062 arg_offsets
[0] = offset
;
3063 store_local_general (td
, offset
, type
);
3066 local_offsets
= (guint32
*) g_malloc (header
->num_locals
* sizeof (guint32
));
3067 /* Allocate locals to store inlined method args from stack */
3068 for (i
= 0; i
< header
->num_locals
; i
++)
3069 local_offsets
[i
] = create_interp_local (td
, header
->locals
[i
]);
3072 while (td
->ip
< end
) {
3073 g_assert (td
->sp
>= td
->stack
);
3074 g_assert (td
->vt_sp
< 0x10000000);
3075 in_offset
= td
->ip
- header
->code
;
3076 td
->in_start
= td
->ip
;
3077 InterpInst
*prev_last_ins
= td
->last_ins
;
3079 if (td
->stack_height
[in_offset
] >= 0) {
3080 g_assert (td
->is_bb_start
[in_offset
]);
3081 if (td
->stack_height
[in_offset
] > 0)
3082 memcpy (td
->stack
, td
->stack_state
[in_offset
], td
->stack_height
[in_offset
] * sizeof(td
->stack
[0]));
3083 td
->sp
= td
->stack
+ td
->stack_height
[in_offset
];
3084 td
->vt_sp
= td
->vt_stack_size
[in_offset
];
3087 if (in_offset
== bb
->end
)
3091 int op_size
= mono_opcode_size (td
->ip
, end
);
3092 g_assert (op_size
> 0); /* The BB formation pass must catch all bad ops */
3094 if (td
->verbose_level
> 1)
3095 g_print ("SKIPPING DEAD OP at %x\n", in_offset
);
3101 if (td
->verbose_level
> 1) {
3102 g_print ("IL_%04lx %s %-10s, sp %ld, %s %-12s vt_sp %u (max %u)\n",
3103 td
->ip
- td
->il_code
,
3104 td
->is_bb_start
[td
->ip
- td
->il_code
] == 3 ? "<>" :
3105 td
->is_bb_start
[td
->ip
- td
->il_code
] == 2 ? "< " :
3106 td
->is_bb_start
[td
->ip
- td
->il_code
] == 1 ? " >" : " ",
3107 mono_opcode_name (*td
->ip
), td
->sp
- td
->stack
,
3108 td
->sp
> td
->stack
? stack_type_string
[td
->sp
[-1].type
] : " ",
3109 (td
->sp
> td
->stack
&& (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_VT
)) ? (td
->sp
[-1].klass
== NULL
? "?" : m_class_get_name (td
->sp
[-1].klass
)) : "",
3110 td
->vt_sp
, td
->max_vt_sp
);
3113 if (sym_seq_points
&& mono_bitset_test_fast (seq_point_locs
, td
->ip
- header
->code
)) {
3114 InterpBasicBlock
*cbb
= td
->offset_to_bb
[td
->ip
- header
->code
];
3118 * Make methods interruptable at the beginning, and at the targets of
3119 * backward branches.
3121 if (in_offset
== 0 || g_slist_length (cbb
->preds
) > 1)
3122 interp_add_ins (td
, MINT_SDB_INTR_LOC
);
3124 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3127 if (td
->is_bb_start
[in_offset
]) {
3128 int index
= td
->clause_indexes
[in_offset
];
3130 MonoExceptionClause
*clause
= &header
->clauses
[index
];
3131 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
||
3132 clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
3133 in_offset
== clause
->handler_offset
)
3134 interp_add_ins (td
, MINT_START_ABORT_PROT
);
3141 emitted_funccall_seq_point
= FALSE
;
3145 SIMPLE_OP(td
, MINT_BREAK
);
3151 int arg_n
= *td
->ip
- CEE_LDARG_0
;
3152 if (td
->method
== method
)
3153 load_arg (td
, arg_n
);
3155 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
3163 int loc_n
= *td
->ip
- CEE_LDLOC_0
;
3164 if (td
->method
== method
)
3165 load_local (td
, loc_n
);
3167 load_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
3175 int loc_n
= *td
->ip
- CEE_STLOC_0
;
3176 if (td
->method
== method
)
3177 store_local (td
, loc_n
);
3179 store_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
3184 int arg_n
= ((guint8
*)td
->ip
)[1];
3185 if (td
->method
== method
)
3186 load_arg (td
, arg_n
);
3188 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
3192 case CEE_LDARGA_S
: {
3193 /* NOTE: n includes this */
3194 int n
= ((guint8
*) td
->ip
) [1];
3196 if (td
->method
== method
) {
3197 get_arg_type_exact (td
, n
, &mt
);
3198 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDARGA_VT
: MINT_LDARGA
);
3199 td
->last_ins
->data
[0] = n
;
3201 interp_add_ins (td
, MINT_LDLOCA_S
);
3202 td
->last_ins
->data
[0] = arg_offsets
[n
];
3204 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
3209 int arg_n
= ((guint8
*)td
->ip
)[1];
3210 if (td
->method
== method
)
3211 store_arg (td
, arg_n
);
3213 store_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
3218 int loc_n
= ((guint8
*)td
->ip
)[1];
3219 if (td
->method
== method
)
3220 load_local (td
, loc_n
);
3222 load_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
3226 case CEE_LDLOCA_S
: {
3227 int loc_n
= ((guint8
*)td
->ip
)[1];
3228 interp_add_ins (td
, MINT_LDLOCA_S
);
3229 if (td
->method
== method
)
3230 td
->last_ins
->data
[0] = td
->rtm
->local_offsets
[loc_n
];
3232 td
->last_ins
->data
[0] = local_offsets
[loc_n
];
3233 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
3238 int loc_n
= ((guint8
*)td
->ip
)[1];
3239 if (td
->method
== method
)
3240 store_local (td
, loc_n
);
3242 store_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
3247 SIMPLE_OP(td
, MINT_LDNULL
);
3248 PUSH_TYPE(td
, STACK_TYPE_O
, NULL
);
3251 SIMPLE_OP(td
, MINT_LDC_I4_M1
);
3252 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3255 if (!td
->is_bb_start
[td
->ip
+ 1 - td
->il_code
] && td
->ip
[1] == 0xfe && td
->ip
[2] == CEE_CEQ
&&
3256 td
->sp
> td
->stack
&& td
->sp
[-1].type
== STACK_TYPE_I4
) {
3257 SIMPLE_OP(td
, MINT_CEQ0_I4
);
3260 SIMPLE_OP(td
, MINT_LDC_I4_0
);
3261 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3265 if (!td
->is_bb_start
[td
->ip
+ 1 - td
->il_code
] &&
3266 (td
->ip
[1] == CEE_ADD
|| td
->ip
[1] == CEE_SUB
) && td
->sp
[-1].type
== STACK_TYPE_I4
) {
3267 interp_add_ins (td
, td
->ip
[1] == CEE_ADD
? MINT_ADD1_I4
: MINT_SUB1_I4
);
3270 SIMPLE_OP(td
, MINT_LDC_I4_1
);
3271 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3281 SIMPLE_OP(td
, (*td
->ip
- CEE_LDC_I4_0
) + MINT_LDC_I4_0
);
3282 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3285 interp_add_ins (td
, MINT_LDC_I4_S
);
3286 td
->last_ins
->data
[0] = ((gint8
*) td
->ip
) [1];
3288 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3291 i32
= read32 (td
->ip
+ 1);
3292 interp_add_ins (td
, MINT_LDC_I4
);
3293 WRITE32_INS (td
->last_ins
, 0, &i32
);
3295 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3298 gint64 val
= read64 (td
->ip
+ 1);
3299 interp_add_ins (td
, MINT_LDC_I8
);
3300 WRITE64_INS (td
->last_ins
, 0, &val
);
3302 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I8
);
3307 readr4 (td
->ip
+ 1, &val
);
3308 interp_add_ins (td
, MINT_LDC_R4
);
3309 WRITE32_INS (td
->last_ins
, 0, &val
);
3311 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_R4
);
3316 readr8 (td
->ip
+ 1, &val
);
3317 interp_add_ins (td
, MINT_LDC_R8
);
3318 WRITE64_INS (td
->last_ins
, 0, &val
);
3320 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_R8
);
3324 int type
= td
->sp
[-1].type
;
3325 MonoClass
*klass
= td
->sp
[-1].klass
;
3326 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
3327 gint32 size
= mono_class_value_size (klass
, NULL
);
3329 interp_add_ins (td
, MINT_DUP_VT
);
3330 WRITE32_INS (td
->last_ins
, 0, &size
);
3333 SIMPLE_OP(td
, MINT_DUP
);
3334 PUSH_TYPE(td
, type
, klass
);
3339 SIMPLE_OP(td
, MINT_POP
);
3340 td
->last_ins
->data
[0] = 0;
3341 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
3342 int size
= mono_class_value_size (td
->sp
[-1].klass
, NULL
);
3343 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
3344 interp_add_ins (td
, MINT_VTRESULT
);
3345 td
->last_ins
->data
[0] = 0;
3346 WRITE32_INS (td
->last_ins
, 1, &size
);
3354 if (td
->sp
> td
->stack
)
3355 g_warning ("CEE_JMP: stack must be empty");
3356 token
= read32 (td
->ip
+ 1);
3357 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
3358 goto_if_nok (error
, exit
);
3359 interp_add_ins (td
, MINT_JMP
);
3360 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3361 goto_if_nok (error
, exit
);
3365 case CEE_CALLVIRT
: /* Fall through */
3366 case CEE_CALLI
: /* Fall through */
3368 gboolean need_seq_point
= FALSE
;
3370 if (sym_seq_points
&& !mono_bitset_test_fast (seq_point_locs
, td
->ip
+ 5 - header
->code
))
3371 need_seq_point
= TRUE
;
3373 if (!interp_transform_call (td
, method
, NULL
, domain
, generic_context
, td
->is_bb_start
, constrained_class
, readonly
, error
, TRUE
, save_last_error
))
3376 if (need_seq_point
) {
3377 //check is is a nested call and remove the MONO_INST_NONEMPTY_STACK of the last breakpoint, only for non native methods
3378 if (!(method
->flags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
3379 if (emitted_funccall_seq_point
) {
3381 last_seq_point
->flags
|= INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL
;
3384 emitted_funccall_seq_point
= TRUE
;
3386 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3387 // This seq point is actually associated with the instruction following the call
3388 last_seq_point
->il_offset
= td
->ip
- header
->code
;
3389 last_seq_point
->flags
= INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK
;
3392 constrained_class
= NULL
;
3394 save_last_error
= FALSE
;
3398 /* Return from inlined method, return value is on top of stack */
3399 if (td
->method
!= method
) {
3405 MonoType
*ult
= mini_type_get_underlying_type (signature
->ret
);
3406 if (ult
->type
!= MONO_TYPE_VOID
) {
3407 CHECK_STACK (td
, 1);
3409 if (mint_type (ult
) == MINT_TYPE_VT
) {
3410 MonoClass
*klass
= mono_class_from_mono_type_internal (ult
);
3411 vt_size
= mono_class_value_size (klass
, NULL
);
3414 if (td
->sp
> td
->stack
) {
3415 mono_error_set_generic_error (error
, "System", "InvalidProgramException", "");
3418 if (td
->vt_sp
!= ALIGN_TO (vt_size
, MINT_VT_ALIGNMENT
))
3419 g_error ("%s: CEE_RET: value type stack: %d vs. %d", mono_method_full_name (td
->method
, TRUE
), td
->vt_sp
, vt_size
);
3421 if (sym_seq_points
) {
3422 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3423 td
->last_ins
->flags
= INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT
;
3426 if (mono_jit_trace_calls
!= NULL
&& mono_trace_eval (method
)) {
3427 /* This does the return as well */
3428 interp_add_ins (td
, MINT_TRACE_EXIT
);
3429 if (ult
->type
== MONO_TYPE_VOID
)
3431 WRITE32_INS (td
->last_ins
, 0, &vt_size
);
3435 SIMPLE_OP(td
, ult
->type
== MONO_TYPE_VOID
? MINT_RET_VOID
: MINT_RET
);
3437 interp_add_ins (td
, MINT_RET_VT
);
3438 WRITE32_INS (td
->last_ins
, 0, &vt_size
);
3445 int offset
= read32 (td
->ip
+ 1);
3448 handle_branch (td
, MINT_BR_S
, MINT_BR
, 5 + offset
);
3454 int offset
= (gint8
)td
->ip
[1];
3457 handle_branch (td
, MINT_BR_S
, MINT_BR
, 2 + (gint8
)td
->ip
[1]);
3464 one_arg_branch (td
, MINT_BRFALSE_I4
, 5 + read32 (td
->ip
+ 1));
3469 one_arg_branch (td
, MINT_BRFALSE_I4
, 2 + (gint8
)td
->ip
[1]);
3474 one_arg_branch (td
, MINT_BRTRUE_I4
, 5 + read32 (td
->ip
+ 1));
3479 one_arg_branch (td
, MINT_BRTRUE_I4
, 2 + (gint8
)td
->ip
[1]);
3484 two_arg_branch (td
, MINT_BEQ_I4
, 5 + read32 (td
->ip
+ 1));
3489 two_arg_branch (td
, MINT_BEQ_I4
, 2 + (gint8
) td
->ip
[1]);
3494 two_arg_branch (td
, MINT_BGE_I4
, 5 + read32 (td
->ip
+ 1));
3499 two_arg_branch (td
, MINT_BGE_I4
, 2 + (gint8
) td
->ip
[1]);
3504 two_arg_branch (td
, MINT_BGT_I4
, 5 + read32 (td
->ip
+ 1));
3509 two_arg_branch (td
, MINT_BGT_I4
, 2 + (gint8
) td
->ip
[1]);
3514 two_arg_branch (td
, MINT_BLT_I4
, 5 + read32 (td
->ip
+ 1));
3519 two_arg_branch (td
, MINT_BLT_I4
, 2 + (gint8
) td
->ip
[1]);
3524 two_arg_branch (td
, MINT_BLE_I4
, 5 + read32 (td
->ip
+ 1));
3529 two_arg_branch (td
, MINT_BLE_I4
, 2 + (gint8
) td
->ip
[1]);
3534 two_arg_branch (td
, MINT_BNE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3539 two_arg_branch (td
, MINT_BNE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3544 two_arg_branch (td
, MINT_BGE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3549 two_arg_branch (td
, MINT_BGE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3554 two_arg_branch (td
, MINT_BGT_UN_I4
, 5 + read32 (td
->ip
+ 1));
3559 two_arg_branch (td
, MINT_BGT_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3564 two_arg_branch (td
, MINT_BLE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3569 two_arg_branch (td
, MINT_BLE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3574 two_arg_branch (td
, MINT_BLT_UN_I4
, 5 + read32 (td
->ip
+ 1));
3579 two_arg_branch (td
, MINT_BLT_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3585 const unsigned char *next_ip
;
3587 n
= read32 (td
->ip
);
3588 interp_add_ins_explicit (td
, MINT_SWITCH
, MINT_SWITCH_LEN (n
));
3589 WRITE32_INS (td
->last_ins
, 0, &n
);
3591 next_ip
= td
->ip
+ n
* 4;
3593 int stack_height
= td
->sp
- td
->stack
;
3594 for (i
= 0; i
< n
; i
++) {
3595 offset
= read32 (td
->ip
);
3596 target
= next_ip
- td
->il_code
+ offset
;
3599 if (stack_height
> 0 && stack_height
!= td
->stack_height
[target
])
3600 g_warning ("SWITCH with back branch and non-empty stack");
3603 td
->stack_height
[target
] = stack_height
;
3604 td
->vt_stack_size
[target
] = td
->vt_sp
;
3605 if (stack_height
> 0)
3606 td
->stack_state
[target
] = (StackInfo
*)g_memdup (td
->stack
, stack_height
* sizeof (td
->stack
[0]));
3608 WRITE32_INS (td
->last_ins
, 2 + i
* 2, &target
);
3614 CHECK_STACK (td
, 1);
3615 SIMPLE_OP (td
, MINT_LDIND_I1_CHECK
);
3616 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3617 BARRIER_IF_VOLATILE (td
);
3620 CHECK_STACK (td
, 1);
3621 SIMPLE_OP (td
, MINT_LDIND_U1_CHECK
);
3622 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3623 BARRIER_IF_VOLATILE (td
);
3626 CHECK_STACK (td
, 1);
3627 SIMPLE_OP (td
, MINT_LDIND_I2_CHECK
);
3628 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3629 BARRIER_IF_VOLATILE (td
);
3632 CHECK_STACK (td
, 1);
3633 SIMPLE_OP (td
, MINT_LDIND_U2_CHECK
);
3634 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3635 BARRIER_IF_VOLATILE (td
);
3638 CHECK_STACK (td
, 1);
3639 SIMPLE_OP (td
, MINT_LDIND_I4_CHECK
);
3640 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3641 BARRIER_IF_VOLATILE (td
);
3644 CHECK_STACK (td
, 1);
3645 SIMPLE_OP (td
, MINT_LDIND_U4_CHECK
);
3646 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3647 BARRIER_IF_VOLATILE (td
);
3650 CHECK_STACK (td
, 1);
3651 SIMPLE_OP (td
, MINT_LDIND_I8_CHECK
);
3652 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
3653 BARRIER_IF_VOLATILE (td
);
3656 CHECK_STACK (td
, 1);
3657 SIMPLE_OP (td
, MINT_LDIND_REF_CHECK
);
3658 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3659 BARRIER_IF_VOLATILE (td
);
3662 CHECK_STACK (td
, 1);
3663 SIMPLE_OP (td
, MINT_LDIND_R4_CHECK
);
3664 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
3665 BARRIER_IF_VOLATILE (td
);
3668 CHECK_STACK (td
, 1);
3669 SIMPLE_OP (td
, MINT_LDIND_R8_CHECK
);
3670 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
3671 BARRIER_IF_VOLATILE (td
);
3674 CHECK_STACK (td
, 1);
3675 SIMPLE_OP (td
, MINT_LDIND_REF_CHECK
);
3676 BARRIER_IF_VOLATILE (td
);
3677 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
3680 CHECK_STACK (td
, 2);
3681 BARRIER_IF_VOLATILE (td
);
3682 SIMPLE_OP (td
, MINT_STIND_REF
);
3686 CHECK_STACK (td
, 2);
3687 BARRIER_IF_VOLATILE (td
);
3688 SIMPLE_OP (td
, MINT_STIND_I1
);
3692 CHECK_STACK (td
, 2);
3693 BARRIER_IF_VOLATILE (td
);
3694 SIMPLE_OP (td
, MINT_STIND_I2
);
3698 CHECK_STACK (td
, 2);
3699 BARRIER_IF_VOLATILE (td
);
3700 SIMPLE_OP (td
, MINT_STIND_I4
);
3704 CHECK_STACK (td
, 2);
3705 BARRIER_IF_VOLATILE (td
);
3706 SIMPLE_OP (td
, MINT_STIND_I
);
3710 CHECK_STACK (td
, 2);
3711 BARRIER_IF_VOLATILE (td
);
3712 SIMPLE_OP (td
, MINT_STIND_I8
);
3716 CHECK_STACK (td
, 2);
3717 BARRIER_IF_VOLATILE (td
);
3718 SIMPLE_OP (td
, MINT_STIND_R4
);
3722 CHECK_STACK (td
, 2);
3723 BARRIER_IF_VOLATILE (td
);
3724 SIMPLE_OP (td
, MINT_STIND_R8
);
3728 binary_arith_op(td
, MINT_ADD_I4
);
3732 binary_arith_op(td
, MINT_SUB_I4
);
3736 binary_arith_op(td
, MINT_MUL_I4
);
3740 binary_arith_op(td
, MINT_DIV_I4
);
3744 binary_arith_op(td
, MINT_DIV_UN_I4
);
3748 binary_arith_op (td
, MINT_REM_I4
);
3752 binary_arith_op (td
, MINT_REM_UN_I4
);
3756 binary_arith_op (td
, MINT_AND_I4
);
3760 binary_arith_op (td
, MINT_OR_I4
);
3764 binary_arith_op (td
, MINT_XOR_I4
);
3768 shift_op (td
, MINT_SHL_I4
);
3772 shift_op (td
, MINT_SHR_I4
);
3776 shift_op (td
, MINT_SHR_UN_I4
);
3780 unary_arith_op (td
, MINT_NEG_I4
);
3784 unary_arith_op (td
, MINT_NOT_I4
);
3788 CHECK_STACK (td
, 1);
3789 switch (td
->sp
[-1].type
) {
3791 interp_add_ins (td
, MINT_CONV_U1_R4
);
3794 interp_add_ins (td
, MINT_CONV_U1_R8
);
3797 interp_add_ins (td
, MINT_CONV_U1_I4
);
3800 interp_add_ins (td
, MINT_CONV_U1_I8
);
3803 g_assert_not_reached ();
3806 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3809 CHECK_STACK (td
, 1);
3810 switch (td
->sp
[-1].type
) {
3812 interp_add_ins (td
, MINT_CONV_I1_R4
);
3815 interp_add_ins (td
, MINT_CONV_I1_R8
);
3818 interp_add_ins (td
, MINT_CONV_I1_I4
);
3821 interp_add_ins (td
, MINT_CONV_I1_I8
);
3824 g_assert_not_reached ();
3827 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3830 CHECK_STACK (td
, 1);
3831 switch (td
->sp
[-1].type
) {
3833 interp_add_ins (td
, MINT_CONV_U2_R4
);
3836 interp_add_ins (td
, MINT_CONV_U2_R8
);
3839 interp_add_ins (td
, MINT_CONV_U2_I4
);
3842 interp_add_ins (td
, MINT_CONV_U2_I8
);
3845 g_assert_not_reached ();
3848 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3851 CHECK_STACK (td
, 1);
3852 switch (td
->sp
[-1].type
) {
3854 interp_add_ins (td
, MINT_CONV_I2_R4
);
3857 interp_add_ins (td
, MINT_CONV_I2_R8
);
3860 interp_add_ins (td
, MINT_CONV_I2_I4
);
3863 interp_add_ins (td
, MINT_CONV_I2_I8
);
3866 g_assert_not_reached ();
3869 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3872 CHECK_STACK (td
, 1);
3873 switch (td
->sp
[-1].type
) {
3875 #if SIZEOF_VOID_P == 4
3876 interp_add_ins (td
, MINT_CONV_U4_R8
);
3878 interp_add_ins (td
, MINT_CONV_U8_R8
);
3882 #if SIZEOF_VOID_P == 8
3883 interp_add_ins (td
, MINT_CONV_U8_I4
);
3887 #if SIZEOF_VOID_P == 4
3888 interp_add_ins (td
, MINT_CONV_U4_I8
);
3895 g_assert_not_reached ();
3898 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3901 CHECK_STACK (td
, 1);
3902 switch (td
->sp
[-1].type
) {
3904 #if SIZEOF_VOID_P == 8
3905 interp_add_ins (td
, MINT_CONV_I8_R8
);
3907 interp_add_ins (td
, MINT_CONV_I4_R8
);
3911 #if SIZEOF_VOID_P == 8
3912 interp_add_ins (td
, MINT_CONV_I8_I4
);
3920 #if SIZEOF_VOID_P == 4
3921 interp_add_ins (td
, MINT_CONV_I4_I8
);
3925 g_assert_not_reached ();
3928 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3931 CHECK_STACK (td
, 1);
3932 switch (td
->sp
[-1].type
) {
3934 interp_add_ins (td
, MINT_CONV_U4_R4
);
3937 interp_add_ins (td
, MINT_CONV_U4_R8
);
3942 interp_add_ins (td
, MINT_CONV_U4_I8
);
3945 #if SIZEOF_VOID_P == 8
3946 interp_add_ins (td
, MINT_CONV_U4_I8
);
3950 g_assert_not_reached ();
3953 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3956 CHECK_STACK (td
, 1);
3957 switch (td
->sp
[-1].type
) {
3959 interp_add_ins (td
, MINT_CONV_I4_R4
);
3962 interp_add_ins (td
, MINT_CONV_I4_R8
);
3967 interp_add_ins (td
, MINT_CONV_I4_I8
);
3970 #if SIZEOF_VOID_P == 8
3971 interp_add_ins (td
, MINT_CONV_I4_I8
);
3975 g_assert_not_reached ();
3978 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3981 CHECK_STACK (td
, 1);
3982 switch (td
->sp
[-1].type
) {
3984 interp_add_ins (td
, MINT_CONV_I8_R4
);
3987 interp_add_ins (td
, MINT_CONV_I8_R8
);
3989 case STACK_TYPE_I4
: {
3990 if (interp_ins_is_ldc (td
->last_ins
) && !td
->is_bb_start
[in_offset
]) {
3991 gint64 ct
= interp_ldc_i4_get_const (td
->last_ins
);
3992 interp_remove_ins (td
, td
->last_ins
);
3994 interp_add_ins (td
, MINT_LDC_I8
);
3995 WRITE64_INS (td
->last_ins
, 0, &ct
);
3997 interp_add_ins (td
, MINT_CONV_I8_I4
);
4004 #if SIZEOF_VOID_P == 4
4005 interp_add_ins (td
, MINT_CONV_I8_I4
);
4009 g_assert_not_reached ();
4012 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4015 CHECK_STACK (td
, 1);
4016 switch (td
->sp
[-1].type
) {
4018 interp_add_ins (td
, MINT_CONV_R4_R8
);
4021 interp_add_ins (td
, MINT_CONV_R4_I8
);
4024 interp_add_ins (td
, MINT_CONV_R4_I4
);
4030 g_assert_not_reached ();
4033 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
4036 CHECK_STACK (td
, 1);
4037 switch (td
->sp
[-1].type
) {
4039 interp_add_ins (td
, MINT_CONV_R8_I4
);
4042 interp_add_ins (td
, MINT_CONV_R8_I8
);
4045 interp_add_ins (td
, MINT_CONV_R8_R4
);
4050 g_assert_not_reached ();
4053 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4056 CHECK_STACK (td
, 1);
4057 switch (td
->sp
[-1].type
) {
4059 if (interp_ins_is_ldc (td
->last_ins
) && !td
->is_bb_start
[in_offset
]) {
4060 gint64 ct
= (guint32
)interp_ldc_i4_get_const (td
->last_ins
);
4061 interp_remove_ins (td
, td
->last_ins
);
4063 interp_add_ins (td
, MINT_LDC_I8
);
4064 WRITE64_INS (td
->last_ins
, 0, &ct
);
4066 interp_add_ins (td
, MINT_CONV_U8_I4
);
4072 interp_add_ins (td
, MINT_CONV_U8_R4
);
4075 interp_add_ins (td
, MINT_CONV_U8_R8
);
4078 #if SIZEOF_VOID_P == 4
4079 interp_add_ins (td
, MINT_CONV_U8_I4
);
4083 g_assert_not_reached ();
4086 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4089 CHECK_STACK (td
, 2);
4091 token
= read32 (td
->ip
+ 1);
4092 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
4093 goto_if_nok (error
, exit
);
4095 if (m_class_is_valuetype (klass
)) {
4096 int mt
= mint_type (m_class_get_byval_arg (klass
));
4097 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_CPOBJ_VT
: MINT_CPOBJ
);
4098 td
->last_ins
->data
[0] = get_data_item_index(td
, klass
);
4100 interp_add_ins (td
, MINT_LDIND_REF
);
4101 interp_add_ins (td
, MINT_STIND_REF
);
4108 CHECK_STACK (td
, 1);
4110 token
= read32 (td
->ip
+ 1);
4112 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4113 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4115 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
4116 goto_if_nok (error
, exit
);
4119 interp_emit_ldobj (td
, klass
);
4122 BARRIER_IF_VOLATILE (td
);
4126 token
= mono_metadata_token_index (read32 (td
->ip
+ 1));
4128 if (method
->wrapper_type
== MONO_WRAPPER_NONE
) {
4129 MonoString
*s
= mono_ldstr_checked (domain
, image
, token
, error
);
4130 goto_if_nok (error
, exit
);
4131 /* GC won't scan code stream, but reference is held by metadata
4132 * machinery so we are good here */
4133 interp_add_ins (td
, MINT_LDSTR
);
4134 td
->last_ins
->data
[0] = get_data_item_index (td
, s
);
4136 /* defer allocation to execution-time */
4137 interp_add_ins (td
, MINT_LDSTR_TOKEN
);
4138 td
->last_ins
->data
[0] = get_data_item_index (td
, GUINT_TO_POINTER (token
));
4140 PUSH_TYPE(td
, STACK_TYPE_O
, mono_defaults
.string_class
);
4145 MonoMethodSignature
*csignature
;
4146 guint32 vt_stack_used
= 0;
4147 guint32 vt_res_size
= 0;
4150 token
= read32 (td
->ip
);
4153 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4154 m
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
4156 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
4157 goto_if_nok (error
, exit
);
4160 csignature
= mono_method_signature_internal (m
);
4163 if (!mono_class_init_internal (klass
)) {
4164 mono_error_set_for_class_failure (error
, klass
);
4165 goto_if_nok (error
, exit
);
4168 if (mono_class_get_flags (klass
) & TYPE_ATTRIBUTE_ABSTRACT
) {
4169 char* full_name
= mono_type_get_full_name (klass
);
4170 mono_error_set_member_access (error
, "Cannot create an abstract class: %s", full_name
);
4172 goto_if_nok (error
, exit
);
4175 if (mono_class_is_magic_int (klass
) || mono_class_is_magic_float (klass
)) {
4176 td
->sp
-= csignature
->param_count
;
4177 #if SIZEOF_VOID_P == 8
4178 if (mono_class_is_magic_int (klass
) && td
->sp
[0].type
== STACK_TYPE_I4
)
4179 interp_add_ins (td
, MINT_CONV_I8_I4
);
4180 else if (mono_class_is_magic_float (klass
) && td
->sp
[0].type
== STACK_TYPE_R4
)
4181 interp_add_ins (td
, MINT_CONV_R8_R4
);
4183 interp_add_ins (td
, MINT_NEWOBJ_MAGIC
);
4184 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4185 goto_if_nok (error
, exit
);
4187 PUSH_TYPE (td
, stack_type
[mint_type (m_class_get_byval_arg (klass
))], klass
);
4189 if (m_class_get_parent (klass
) == mono_defaults
.array_class
) {
4190 interp_add_ins (td
, MINT_NEWOBJ_ARRAY
);
4191 td
->last_ins
->data
[0] = get_data_item_index (td
, m
->klass
);
4192 td
->last_ins
->data
[1] = csignature
->param_count
;
4193 } else if (m_class_get_image (klass
) == mono_defaults
.corlib
&&
4194 !strcmp (m_class_get_name (m
->klass
), "ByReference`1") &&
4195 !strcmp (m
->name
, ".ctor")) {
4196 /* public ByReference(ref T value) */
4197 g_assert (csignature
->hasthis
&& csignature
->param_count
== 1);
4198 interp_add_ins (td
, MINT_INTRINS_BYREFERENCE_CTOR
);
4199 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4200 } else if (klass
!= mono_defaults
.string_class
&&
4201 !mono_class_is_marshalbyref (klass
) &&
4202 !mono_class_has_finalizer (klass
) &&
4203 !m_class_has_weak_fields (klass
)) {
4204 if (!m_class_is_valuetype (klass
)) {
4205 InterpInst
*newobj_fast
= interp_add_ins (td
, MINT_NEWOBJ_FAST
);
4207 newobj_fast
->data
[1] = csignature
->param_count
;
4209 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
4210 goto_if_nok (error
, exit
);
4211 newobj_fast
->data
[2] = get_data_item_index (td
, vtable
);
4213 move_stack (td
, (td
->sp
- td
->stack
) - csignature
->param_count
, 2);
4215 StackInfo
*tmp_sp
= td
->sp
- csignature
->param_count
- 2;
4216 SET_TYPE (tmp_sp
, STACK_TYPE_O
, klass
);
4217 SET_TYPE (tmp_sp
+ 1, STACK_TYPE_O
, klass
);
4219 if ((mono_interp_opt
& INTERP_OPT_INLINE
) && interp_method_check_inlining (td
, m
)) {
4220 MonoMethodHeader
*mheader
= interp_method_get_header (m
, error
);
4221 goto_if_nok (error
, exit
);
4223 if (interp_inline_method (td
, m
, mheader
, error
)) {
4224 newobj_fast
->data
[0] = 0xffff;
4228 // If inlining failed we need to restore the stack
4229 move_stack (td
, (td
->sp
- td
->stack
) - csignature
->param_count
, -2);
4230 // Set the method to be executed as part of newobj instruction
4231 newobj_fast
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4233 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
)
4234 interp_add_ins (td
, MINT_NEWOBJ_VTST_FAST
);
4236 interp_add_ins (td
, MINT_NEWOBJ_VT_FAST
);
4238 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4239 td
->last_ins
->data
[1] = csignature
->param_count
;
4241 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4242 td
->last_ins
->data
[2] = mono_class_value_size (klass
, NULL
);
4246 interp_add_ins (td
, MINT_NEWOBJ
);
4247 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4249 goto_if_nok (error
, exit
);
4250 /* The constructor was not inlined, abort inlining of current method */
4253 td
->sp
-= csignature
->param_count
;
4254 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4255 vt_res_size
= mono_class_value_size (klass
, NULL
);
4256 PUSH_VT (td
, vt_res_size
);
4258 for (i
= 0; i
< csignature
->param_count
; ++i
) {
4259 int mt
= mint_type(csignature
->params
[i
]);
4260 if (mt
== MINT_TYPE_VT
) {
4261 MonoClass
*k
= mono_class_from_mono_type_internal (csignature
->params
[i
]);
4262 gint32 size
= mono_class_value_size (k
, NULL
);
4263 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4264 vt_stack_used
+= size
;
4267 if (vt_stack_used
!= 0 || vt_res_size
!= 0) {
4268 interp_add_ins (td
, MINT_VTRESULT
);
4269 td
->last_ins
->data
[0] = vt_res_size
;
4270 WRITE32_INS (td
->last_ins
, 1, &vt_stack_used
);
4271 td
->vt_sp
-= vt_stack_used
;
4273 PUSH_TYPE (td
, stack_type
[mint_type (m_class_get_byval_arg (klass
))], klass
);
4279 gboolean isinst_instr
= *td
->ip
== CEE_ISINST
;
4280 CHECK_STACK (td
, 1);
4281 token
= read32 (td
->ip
+ 1);
4282 klass
= mini_get_class (method
, token
, generic_context
);
4283 CHECK_TYPELOAD (klass
);
4284 interp_handle_isinst (td
, klass
, isinst_instr
);
4286 td
->sp
[-1].klass
= klass
;
4290 switch (td
->sp
[-1].type
) {
4294 interp_add_ins (td
, MINT_CONV_R_UN_I8
);
4297 interp_add_ins (td
, MINT_CONV_R_UN_I4
);
4300 g_assert_not_reached ();
4302 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4306 CHECK_STACK (td
, 1);
4307 token
= read32 (td
->ip
+ 1);
4309 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4310 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4312 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
4313 goto_if_nok (error
, exit
);
4316 if (mono_class_is_nullable (klass
)) {
4317 MonoMethod
*target_method
;
4318 if (m_class_is_enumtype (mono_class_get_nullable_param_internal (klass
)))
4319 target_method
= mono_class_get_method_from_name_checked (klass
, "UnboxExact", 1, 0, error
);
4321 target_method
= mono_class_get_method_from_name_checked (klass
, "Unbox", 1, 0, error
);
4322 goto_if_nok (error
, exit
);
4323 /* td->ip is incremented by interp_transform_call */
4324 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
, FALSE
))
4327 * CEE_UNBOX needs to push address of vtype while Nullable.Unbox returns the value type
4328 * We create a local variable in the frame so that we can fetch its address.
4330 int local_offset
= create_interp_local (td
, m_class_get_byval_arg (klass
));
4331 store_local_general (td
, local_offset
, m_class_get_byval_arg (klass
));
4332 interp_add_ins (td
, MINT_LDLOCA_S
);
4333 td
->last_ins
->data
[0] = local_offset
;
4334 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
4336 interp_add_ins (td
, MINT_UNBOX
);
4337 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4338 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
4343 CHECK_STACK (td
, 1);
4344 token
= read32 (td
->ip
+ 1);
4346 klass
= mini_get_class (method
, token
, generic_context
);
4347 CHECK_TYPELOAD (klass
);
4349 if (mini_type_is_reference (m_class_get_byval_arg (klass
))) {
4350 int mt
= mint_type (m_class_get_byval_arg (klass
));
4351 interp_handle_isinst (td
, klass
, FALSE
);
4352 SET_TYPE (td
->sp
- 1, stack_type
[mt
], klass
);
4353 } else if (mono_class_is_nullable (klass
)) {
4354 MonoMethod
*target_method
;
4355 if (m_class_is_enumtype (mono_class_get_nullable_param_internal (klass
)))
4356 target_method
= mono_class_get_method_from_name_checked (klass
, "UnboxExact", 1, 0, error
);
4358 target_method
= mono_class_get_method_from_name_checked (klass
, "Unbox", 1, 0, error
);
4359 goto_if_nok (error
, exit
);
4360 /* td->ip is incremented by interp_transform_call */
4361 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
, FALSE
))
4364 interp_add_ins (td
, MINT_UNBOX
);
4365 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4367 interp_emit_ldobj (td
, klass
);
4375 CHECK_STACK (td
, 1);
4376 SIMPLE_OP (td
, MINT_THROW
);
4380 CHECK_STACK (td
, 1);
4381 token
= read32 (td
->ip
+ 1);
4382 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4383 goto_if_nok (error
, exit
);
4384 MonoType
*ftype
= mono_field_get_type_internal (field
);
4385 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4386 mono_class_init_internal (klass
);
4387 #ifndef DISABLE_REMOTING
4388 if (m_class_get_marshalbyref (klass
) || mono_class_is_contextbound (klass
) || klass
== mono_defaults
.marshalbyrefobject_class
) {
4389 g_assert (!is_static
);
4390 int offset
= m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4392 interp_add_ins (td
, MINT_MONO_LDPTR
);
4393 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4394 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
4395 interp_add_ins (td
, MINT_MONO_LDPTR
);
4396 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4397 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
4398 interp_add_ins (td
, MINT_LDC_I4
);
4399 WRITE32_INS (td
->last_ins
, 0, &offset
);
4400 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
4401 #if SIZEOF_VOID_P == 8
4402 interp_add_ins (td
, MINT_CONV_I8_I4
);
4405 MonoMethod
*wrapper
= mono_marshal_get_ldflda_wrapper (field
->type
);
4406 /* td->ip is incremented by interp_transform_call */
4407 if (!interp_transform_call (td
, method
, wrapper
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
, FALSE
))
4413 interp_add_ins (td
, MINT_POP
);
4414 td
->last_ins
->data
[0] = 0;
4415 interp_emit_ldsflda (td
, field
, error
);
4416 goto_if_nok (error
, exit
);
4418 if ((td
->sp
- 1)->type
== STACK_TYPE_O
) {
4419 interp_add_ins (td
, MINT_LDFLDA
);
4421 g_assert ((td
->sp
-1)->type
== STACK_TYPE_MP
);
4422 interp_add_ins (td
, MINT_LDFLDA_UNSAFE
);
4424 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4428 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
4432 CHECK_STACK (td
, 1);
4433 token
= read32 (td
->ip
+ 1);
4434 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4435 goto_if_nok (error
, exit
);
4436 MonoType
*ftype
= mono_field_get_type_internal (field
);
4437 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4438 mono_class_init_internal (klass
);
4440 MonoClass
*field_klass
= mono_class_from_mono_type_internal (ftype
);
4441 mt
= mint_type (m_class_get_byval_arg (field_klass
));
4442 #ifndef DISABLE_REMOTING
4443 if (m_class_get_marshalbyref (klass
)) {
4444 g_assert (!is_static
);
4445 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDRMFLD_VT
: MINT_LDRMFLD
);
4446 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4451 interp_add_ins (td
, MINT_POP
);
4452 td
->last_ins
->data
[0] = 0;
4453 interp_emit_sfld_access (td
, field
, field_klass
, mt
, TRUE
, error
);
4454 goto_if_nok (error
, exit
);
4456 int opcode
= MINT_LDFLD_I1
+ mt
- MINT_TYPE_I1
;
4457 #ifdef NO_UNALIGNED_ACCESS
4458 if ((mt
== MINT_TYPE_I8
|| mt
== MINT_TYPE_R8
) && field
->offset
% SIZEOF_VOID_P
!= 0)
4459 opcode
= get_unaligned_opcode (opcode
);
4461 interp_add_ins (td
, opcode
);
4462 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4463 if (mt
== MINT_TYPE_VT
) {
4464 int size
= mono_class_value_size (field_klass
, NULL
);
4465 WRITE32_INS (td
->last_ins
, 1, &size
);
4469 if (mt
== MINT_TYPE_VT
) {
4470 int size
= mono_class_value_size (field_klass
, NULL
);
4473 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
4474 int size
= mono_class_value_size (klass
, NULL
);
4475 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4476 int field_vt_size
= 0;
4477 if (mt
== MINT_TYPE_VT
) {
4479 * Pop the loaded field from the vtstack (it will still be present
4480 * at the same vtstack address) and we will load it in place of the
4481 * containing valuetype with the second MINT_VTRESULT.
4483 field_vt_size
= mono_class_value_size (field_klass
, NULL
);
4484 field_vt_size
= ALIGN_TO (field_vt_size
, MINT_VT_ALIGNMENT
);
4485 interp_add_ins (td
, MINT_VTRESULT
);
4486 td
->last_ins
->data
[0] = 0;
4487 WRITE32_INS (td
->last_ins
, 1, &field_vt_size
);
4490 interp_add_ins (td
, MINT_VTRESULT
);
4491 td
->last_ins
->data
[0] = field_vt_size
;
4492 WRITE32_INS (td
->last_ins
, 1, &size
);
4495 SET_TYPE (td
->sp
- 1, stack_type
[mt
], field_klass
);
4496 BARRIER_IF_VOLATILE (td
);
4500 CHECK_STACK (td
, 2);
4501 token
= read32 (td
->ip
+ 1);
4502 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4503 goto_if_nok (error
, exit
);
4504 MonoType
*ftype
= mono_field_get_type_internal (field
);
4505 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4506 MonoClass
*field_klass
= mono_class_from_mono_type_internal (ftype
);
4507 mono_class_init_internal (klass
);
4508 mt
= mint_type (ftype
);
4510 BARRIER_IF_VOLATILE (td
);
4512 #ifndef DISABLE_REMOTING
4513 if (m_class_get_marshalbyref (klass
)) {
4514 g_assert (!is_static
);
4515 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_STRMFLD_VT
: MINT_STRMFLD
);
4516 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4521 interp_add_ins (td
, MINT_POP
);
4522 td
->last_ins
->data
[0] = 1;
4523 interp_emit_sfld_access (td
, field
, field_klass
, mt
, FALSE
, error
);
4524 goto_if_nok (error
, exit
);
4526 /* the vtable of the field might not be initialized at this point */
4527 mono_class_vtable_checked (domain
, field_klass
, error
);
4528 goto_if_nok (error
, exit
);
4530 int opcode
= MINT_STFLD_I1
+ mt
- MINT_TYPE_I1
;
4531 #ifdef NO_UNALIGNED_ACCESS
4532 if ((mt
== MINT_TYPE_I8
|| mt
== MINT_TYPE_R8
) && field
->offset
% SIZEOF_VOID_P
!= 0)
4533 opcode
= get_unaligned_opcode (opcode
);
4535 interp_add_ins (td
, opcode
);
4536 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4537 if (mt
== MINT_TYPE_VT
) {
4538 /* the vtable of the field might not be initialized at this point */
4539 mono_class_vtable_checked (domain
, field_klass
, error
);
4540 goto_if_nok (error
, exit
);
4542 td
->last_ins
->data
[1] = get_data_item_index (td
, field_klass
);
4546 if (mt
== MINT_TYPE_VT
) {
4547 int size
= mono_class_value_size (field_klass
, NULL
);
4555 token
= read32 (td
->ip
+ 1);
4556 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4557 goto_if_nok (error
, exit
);
4558 interp_emit_ldsflda (td
, field
, error
);
4559 goto_if_nok (error
, exit
);
4561 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
4565 token
= read32 (td
->ip
+ 1);
4566 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4567 goto_if_nok (error
, exit
);
4568 MonoType
*ftype
= mono_field_get_type_internal (field
);
4569 mt
= mint_type (ftype
);
4570 klass
= mono_class_from_mono_type_internal (ftype
);
4572 interp_emit_sfld_access (td
, field
, klass
, mt
, TRUE
, error
);
4573 goto_if_nok (error
, exit
);
4575 if (mt
== MINT_TYPE_VT
) {
4576 int size
= mono_class_value_size (klass
, NULL
);
4580 PUSH_TYPE(td
, stack_type
[mt
], klass
);
4584 CHECK_STACK (td
, 1);
4585 token
= read32 (td
->ip
+ 1);
4586 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4587 goto_if_nok (error
, exit
);
4588 MonoType
*ftype
= mono_field_get_type_internal (field
);
4589 mt
= mint_type (ftype
);
4591 /* the vtable of the field might not be initialized at this point */
4592 MonoClass
*fld_klass
= mono_class_from_mono_type_internal (ftype
);
4593 mono_class_vtable_checked (domain
, fld_klass
, error
);
4594 goto_if_nok (error
, exit
);
4596 interp_emit_sfld_access (td
, field
, fld_klass
, mt
, FALSE
, error
);
4597 goto_if_nok (error
, exit
);
4599 if (mt
== MINT_TYPE_VT
) {
4600 int size
= mono_class_value_size (fld_klass
, NULL
);
4608 token
= read32 (td
->ip
+ 1);
4610 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4611 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4613 klass
= mini_get_class (method
, token
, generic_context
);
4614 CHECK_TYPELOAD (klass
);
4616 BARRIER_IF_VOLATILE (td
);
4618 interp_emit_stobj (td
, klass
);
4623 case CEE_CONV_OVF_I_UN
:
4624 case CEE_CONV_OVF_U_UN
:
4625 CHECK_STACK (td
, 1);
4626 switch (td
->sp
[-1].type
) {
4628 #if SIZEOF_VOID_P == 8
4629 interp_add_ins (td
, MINT_CONV_OVF_I8_UN_R8
);
4631 interp_add_ins (td
, MINT_CONV_OVF_I4_UN_R8
);
4635 #if SIZEOF_VOID_P == 4
4636 interp_add_ins (td
, MINT_CONV_OVF_I4_UN_I8
);
4640 #if SIZEOF_VOID_P == 8
4641 interp_add_ins (td
, MINT_CONV_I8_U4
);
4642 #elif SIZEOF_VOID_P == 4
4643 if (*td
->ip
== CEE_CONV_OVF_I_UN
)
4644 interp_add_ins (td
, MINT_CONV_OVF_I4_U4
);
4648 g_assert_not_reached ();
4651 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4654 case CEE_CONV_OVF_I8_UN
:
4655 case CEE_CONV_OVF_U8_UN
:
4656 CHECK_STACK (td
, 1);
4657 switch (td
->sp
[-1].type
) {
4659 interp_add_ins (td
, MINT_CONV_OVF_I8_UN_R8
);
4662 if (*td
->ip
== CEE_CONV_OVF_I8_UN
)
4663 interp_add_ins (td
, MINT_CONV_OVF_I8_U8
);
4666 interp_add_ins (td
, MINT_CONV_I8_U4
);
4669 interp_add_ins (td
, MINT_CONV_OVF_I8_UN_R4
);
4672 g_assert_not_reached ();
4675 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4680 CHECK_STACK (td
, 1);
4681 token
= read32 (td
->ip
+ 1);
4682 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4683 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4685 klass
= mini_get_class (method
, token
, generic_context
);
4686 CHECK_TYPELOAD (klass
);
4688 if (mono_class_is_nullable (klass
)) {
4689 MonoMethod
*target_method
= mono_class_get_method_from_name_checked (klass
, "Box", 1, 0, error
);
4690 goto_if_nok (error
, exit
);
4691 /* td->ip is incremented by interp_transform_call */
4692 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
, FALSE
))
4694 } else if (!m_class_is_valuetype (klass
)) {
4695 /* already boxed, do nothing. */
4698 if (G_UNLIKELY (m_class_is_byreflike (klass
))) {
4699 mono_error_set_bad_image (error
, image
, "Cannot box IsByRefLike type '%s.%s'", m_class_get_name_space (klass
), m_class_get_name (klass
));
4702 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4703 size
= mono_class_value_size (klass
, NULL
);
4704 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4706 } else if (td
->sp
[-1].type
== STACK_TYPE_R8
&& m_class_get_byval_arg (klass
)->type
== MONO_TYPE_R4
) {
4707 interp_add_ins (td
, MINT_CONV_R4_R8
);
4709 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
4710 goto_if_nok (error
, exit
);
4712 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
)
4713 interp_add_ins (td
, MINT_BOX_VT
);
4715 interp_add_ins (td
, MINT_BOX
);
4716 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
4717 td
->last_ins
->data
[1] = 0;
4718 SET_TYPE(td
->sp
- 1, STACK_TYPE_O
, klass
);
4725 CHECK_STACK (td
, 1);
4726 token
= read32 (td
->ip
+ 1);
4728 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4729 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4731 klass
= mini_get_class (method
, token
, generic_context
);
4732 CHECK_TYPELOAD (klass
);
4734 MonoClass
*array_class
= mono_class_create_array (klass
, 1);
4735 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, array_class
, error
);
4736 goto_if_nok (error
, exit
);
4738 unsigned char lentype
= (td
->sp
- 1)->type
;
4739 if (lentype
== STACK_TYPE_I8
) {
4740 /* mimic mini behaviour */
4741 interp_add_ins (td
, MINT_CONV_OVF_U4_I8
);
4743 g_assert (lentype
== STACK_TYPE_I4
);
4744 interp_add_ins (td
, MINT_CONV_OVF_U4_I4
);
4746 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
4747 interp_add_ins (td
, MINT_NEWARR
);
4748 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
4749 SET_TYPE (td
->sp
- 1, STACK_TYPE_O
, klass
);
4754 CHECK_STACK (td
, 1);
4755 SIMPLE_OP (td
, MINT_LDLEN
);
4756 #ifdef MONO_BIG_ARRAYS
4757 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I8
);
4759 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
4764 CHECK_STACK (td
, 2);
4766 token
= read32 (td
->ip
+ 1);
4768 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4769 klass
= (MonoClass
*) mono_method_get_wrapper_data (method
, token
);
4771 klass
= mini_get_class (method
, token
, generic_context
);
4773 CHECK_TYPELOAD (klass
);
4775 if (!m_class_is_valuetype (klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
&& !readonly
) {
4777 * Check the class for failures before the type check, which can
4778 * throw other exceptions.
4780 mono_class_setup_vtable (klass
);
4781 CHECK_TYPELOAD (klass
);
4782 interp_add_ins (td
, MINT_LDELEMA_TC
);
4783 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4784 td
->last_ins
->data
[1] = 2;
4786 interp_add_ins (td
, MINT_LDELEMA_FAST
);
4787 mono_class_init_internal (klass
);
4788 size
= mono_class_array_element_size (klass
);
4789 WRITE32_INS (td
->last_ins
, 0, &size
);
4796 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
4800 CHECK_STACK (td
, 2);
4802 SIMPLE_OP (td
, MINT_LDELEM_I1
);
4804 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4807 CHECK_STACK (td
, 2);
4809 SIMPLE_OP (td
, MINT_LDELEM_U1
);
4811 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4814 CHECK_STACK (td
, 2);
4816 SIMPLE_OP (td
, MINT_LDELEM_I2
);
4818 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4821 CHECK_STACK (td
, 2);
4823 SIMPLE_OP (td
, MINT_LDELEM_U2
);
4825 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4828 CHECK_STACK (td
, 2);
4830 SIMPLE_OP (td
, MINT_LDELEM_I4
);
4832 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4835 CHECK_STACK (td
, 2);
4837 SIMPLE_OP (td
, MINT_LDELEM_U4
);
4839 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4842 CHECK_STACK (td
, 2);
4844 SIMPLE_OP (td
, MINT_LDELEM_I8
);
4846 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4849 CHECK_STACK (td
, 2);
4851 SIMPLE_OP (td
, MINT_LDELEM_I
);
4853 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
4856 CHECK_STACK (td
, 2);
4858 SIMPLE_OP (td
, MINT_LDELEM_R4
);
4860 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
4863 CHECK_STACK (td
, 2);
4865 SIMPLE_OP (td
, MINT_LDELEM_R8
);
4867 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4869 case CEE_LDELEM_REF
:
4870 CHECK_STACK (td
, 2);
4872 SIMPLE_OP (td
, MINT_LDELEM_REF
);
4874 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
4877 CHECK_STACK (td
, 2);
4878 token
= read32 (td
->ip
+ 1);
4879 klass
= mini_get_class (method
, token
, generic_context
);
4880 CHECK_TYPELOAD (klass
);
4881 switch (mint_type (m_class_get_byval_arg (klass
))) {
4884 SIMPLE_OP (td
, MINT_LDELEM_I1
);
4886 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4890 SIMPLE_OP (td
, MINT_LDELEM_U1
);
4892 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4896 SIMPLE_OP (td
, MINT_LDELEM_U2
);
4898 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4902 SIMPLE_OP (td
, MINT_LDELEM_I2
);
4904 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4908 SIMPLE_OP (td
, MINT_LDELEM_I4
);
4910 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4914 SIMPLE_OP (td
, MINT_LDELEM_I8
);
4916 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4920 SIMPLE_OP (td
, MINT_LDELEM_R4
);
4922 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
4926 SIMPLE_OP (td
, MINT_LDELEM_R8
);
4928 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4932 SIMPLE_OP (td
, MINT_LDELEM_REF
);
4934 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
4936 case MINT_TYPE_VT
: {
4937 int size
= mono_class_value_size (klass
, NULL
);
4939 SIMPLE_OP (td
, MINT_LDELEM_VT
);
4940 WRITE32_INS (td
->last_ins
, 0, &size
);
4942 SET_TYPE (td
->sp
- 1, STACK_TYPE_VT
, klass
);
4947 GString
*res
= g_string_new ("");
4948 mono_type_get_desc (res
, m_class_get_byval_arg (klass
), TRUE
);
4949 g_print ("LDELEM: %s -> %d (%s)\n", m_class_get_name (klass
), mint_type (m_class_get_byval_arg (klass
)), res
->str
);
4950 g_string_free (res
, TRUE
);
4958 CHECK_STACK (td
, 3);
4960 SIMPLE_OP (td
, MINT_STELEM_I
);
4964 CHECK_STACK (td
, 3);
4966 SIMPLE_OP (td
, MINT_STELEM_I1
);
4970 CHECK_STACK (td
, 3);
4972 SIMPLE_OP (td
, MINT_STELEM_I2
);
4976 CHECK_STACK (td
, 3);
4978 SIMPLE_OP (td
, MINT_STELEM_I4
);
4982 CHECK_STACK (td
, 3);
4984 SIMPLE_OP (td
, MINT_STELEM_I8
);
4988 CHECK_STACK (td
, 3);
4990 SIMPLE_OP (td
, MINT_STELEM_R4
);
4994 CHECK_STACK (td
, 3);
4996 SIMPLE_OP (td
, MINT_STELEM_R8
);
4999 case CEE_STELEM_REF
:
5000 CHECK_STACK (td
, 3);
5002 SIMPLE_OP (td
, MINT_STELEM_REF
);
5006 CHECK_STACK (td
, 3);
5008 token
= read32 (td
->ip
+ 1);
5009 klass
= mini_get_class (method
, token
, generic_context
);
5010 CHECK_TYPELOAD (klass
);
5011 switch (mint_type (m_class_get_byval_arg (klass
))) {
5013 SIMPLE_OP (td
, MINT_STELEM_I1
);
5016 SIMPLE_OP (td
, MINT_STELEM_U1
);
5019 SIMPLE_OP (td
, MINT_STELEM_I2
);
5022 SIMPLE_OP (td
, MINT_STELEM_U2
);
5025 SIMPLE_OP (td
, MINT_STELEM_I4
);
5028 SIMPLE_OP (td
, MINT_STELEM_I8
);
5031 SIMPLE_OP (td
, MINT_STELEM_R4
);
5034 SIMPLE_OP (td
, MINT_STELEM_R8
);
5037 SIMPLE_OP (td
, MINT_STELEM_REF
);
5039 case MINT_TYPE_VT
: {
5040 int size
= mono_class_value_size (klass
, NULL
);
5041 SIMPLE_OP (td
, MINT_STELEM_VT
);
5042 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
5043 WRITE32_INS (td
->last_ins
, 1, &size
);
5048 GString
*res
= g_string_new ("");
5049 mono_type_get_desc (res
, m_class_get_byval_arg (klass
), TRUE
);
5050 g_print ("STELEM: %s -> %d (%s)\n", m_class_get_name (klass
), mint_type (m_class_get_byval_arg (klass
)), res
->str
);
5051 g_string_free (res
, TRUE
);
5060 case CEE_CONV_OVF_U1
:
5062 case CEE_CONV_OVF_I8
:
5064 #if SIZEOF_VOID_P == 8
5065 case CEE_CONV_OVF_U
:
5069 CHECK_STACK (td
, 1);
5070 SIMPLE_OP (td
, MINT_CKFINITE
);
5073 CHECK_STACK (td
, 1);
5075 token
= read32 (td
->ip
+ 1);
5076 klass
= mini_get_class (method
, token
, generic_context
);
5077 CHECK_TYPELOAD (klass
);
5079 interp_add_ins (td
, MINT_MKREFANY
);
5080 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
5083 PUSH_VT (td
, sizeof (MonoTypedRef
));
5084 SET_TYPE(td
->sp
- 1, STACK_TYPE_VT
, mono_defaults
.typed_reference_class
);
5086 case CEE_REFANYVAL
: {
5087 CHECK_STACK (td
, 1);
5089 token
= read32 (td
->ip
+ 1);
5090 klass
= mini_get_class (method
, token
, generic_context
);
5091 CHECK_TYPELOAD (klass
);
5093 interp_add_ins (td
, MINT_REFANYVAL
);
5094 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
5096 POP_VT (td
, sizeof (MonoTypedRef
));
5097 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5102 case CEE_CONV_OVF_I1
:
5103 case CEE_CONV_OVF_I1_UN
: {
5104 gboolean is_un
= *td
->ip
== CEE_CONV_OVF_I1_UN
;
5105 CHECK_STACK (td
, 1);
5106 switch (td
->sp
[-1].type
) {
5108 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_UN_R8
: MINT_CONV_OVF_I1_R8
);
5111 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_U4
: MINT_CONV_OVF_I1_I4
);
5114 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_U8
: MINT_CONV_OVF_I1_I8
);
5117 g_assert_not_reached ();
5120 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5123 case CEE_CONV_OVF_U1
:
5124 case CEE_CONV_OVF_U1_UN
:
5125 CHECK_STACK (td
, 1);
5126 switch (td
->sp
[-1].type
) {
5128 interp_add_ins (td
, MINT_CONV_OVF_U1_R8
);
5131 interp_add_ins (td
, MINT_CONV_OVF_U1_I4
);
5134 interp_add_ins (td
, MINT_CONV_OVF_U1_I8
);
5137 g_assert_not_reached ();
5140 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5142 case CEE_CONV_OVF_I2
:
5143 case CEE_CONV_OVF_I2_UN
: {
5144 gboolean is_un
= *td
->ip
== CEE_CONV_OVF_I2_UN
;
5145 CHECK_STACK (td
, 1);
5146 switch (td
->sp
[-1].type
) {
5148 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_UN_R8
: MINT_CONV_OVF_I2_R8
);
5151 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_U4
: MINT_CONV_OVF_I2_I4
);
5154 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_U8
: MINT_CONV_OVF_I2_I8
);
5157 g_assert_not_reached ();
5160 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5163 case CEE_CONV_OVF_U2_UN
:
5164 case CEE_CONV_OVF_U2
:
5165 CHECK_STACK (td
, 1);
5166 switch (td
->sp
[-1].type
) {
5168 interp_add_ins (td
, MINT_CONV_OVF_U2_R8
);
5171 interp_add_ins (td
, MINT_CONV_OVF_U2_I4
);
5174 interp_add_ins (td
, MINT_CONV_OVF_U2_I8
);
5177 g_assert_not_reached ();
5180 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5182 #if SIZEOF_VOID_P == 4
5183 case CEE_CONV_OVF_I
:
5185 case CEE_CONV_OVF_I4
:
5186 case CEE_CONV_OVF_I4_UN
:
5187 CHECK_STACK (td
, 1);
5188 switch (td
->sp
[-1].type
) {
5190 interp_add_ins (td
, MINT_CONV_OVF_I4_R4
);
5193 interp_add_ins (td
, MINT_CONV_OVF_I4_R8
);
5196 if (*td
->ip
== CEE_CONV_OVF_I4_UN
)
5197 interp_add_ins (td
, MINT_CONV_OVF_I4_U4
);
5200 if (*td
->ip
== CEE_CONV_OVF_I4_UN
)
5201 interp_add_ins (td
, MINT_CONV_OVF_I4_U8
);
5203 interp_add_ins (td
, MINT_CONV_OVF_I4_I8
);
5206 g_assert_not_reached ();
5209 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5211 #if SIZEOF_VOID_P == 4
5212 case CEE_CONV_OVF_U
:
5214 case CEE_CONV_OVF_U4
:
5215 case CEE_CONV_OVF_U4_UN
:
5216 CHECK_STACK (td
, 1);
5217 switch (td
->sp
[-1].type
) {
5219 interp_add_ins (td
, MINT_CONV_OVF_U4_R4
);
5222 interp_add_ins (td
, MINT_CONV_OVF_U4_R8
);
5225 if (*td
->ip
!= CEE_CONV_OVF_U4_UN
)
5226 interp_add_ins (td
, MINT_CONV_OVF_U4_I4
);
5229 interp_add_ins (td
, MINT_CONV_OVF_U4_I8
);
5232 interp_add_ins (td
, MINT_CONV_OVF_U4_P
);
5235 g_assert_not_reached ();
5238 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5240 #if SIZEOF_VOID_P == 8
5241 case CEE_CONV_OVF_I
:
5243 case CEE_CONV_OVF_I8
:
5244 CHECK_STACK (td
, 1);
5245 switch (td
->sp
[-1].type
) {
5247 interp_add_ins (td
, MINT_CONV_OVF_I8_R4
);
5250 interp_add_ins (td
, MINT_CONV_OVF_I8_R8
);
5253 interp_add_ins (td
, MINT_CONV_I8_I4
);
5258 g_assert_not_reached ();
5261 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
5263 #if SIZEOF_VOID_P == 8
5264 case CEE_CONV_OVF_U
:
5266 case CEE_CONV_OVF_U8
:
5267 CHECK_STACK (td
, 1);
5268 switch (td
->sp
[-1].type
) {
5270 interp_add_ins (td
, MINT_CONV_OVF_U8_R4
);
5273 interp_add_ins (td
, MINT_CONV_OVF_U8_R8
);
5276 interp_add_ins (td
, MINT_CONV_OVF_U8_I4
);
5279 interp_add_ins (td
, MINT_CONV_OVF_U8_I8
);
5282 g_assert_not_reached ();
5285 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
5290 token
= read32 (td
->ip
+ 1);
5291 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
|| method
->wrapper_type
== MONO_WRAPPER_SYNCHRONIZED
) {
5292 handle
= mono_method_get_wrapper_data (method
, token
);
5293 klass
= (MonoClass
*) mono_method_get_wrapper_data (method
, token
+ 1);
5294 if (klass
== mono_defaults
.typehandle_class
)
5295 handle
= m_class_get_byval_arg ((MonoClass
*) handle
);
5297 if (generic_context
) {
5298 handle
= mono_class_inflate_generic_type_checked ((MonoType
*)handle
, generic_context
, error
);
5299 goto_if_nok (error
, exit
);
5302 handle
= mono_ldtoken_checked (image
, token
, &klass
, generic_context
, error
);
5303 goto_if_nok (error
, exit
);
5305 mono_class_init_internal (klass
);
5306 mt
= mint_type (m_class_get_byval_arg (klass
));
5307 g_assert (mt
== MINT_TYPE_VT
);
5308 size
= mono_class_value_size (klass
, NULL
);
5309 g_assert (size
== sizeof(gpointer
));
5311 const unsigned char *next_ip
= td
->ip
+ 5;
5312 MonoMethod
*cmethod
;
5313 if (next_ip
< end
&&
5314 !td
->is_bb_start
[next_ip
- td
->il_code
] &&
5315 (*next_ip
== CEE_CALL
|| *next_ip
== CEE_CALLVIRT
) &&
5316 (cmethod
= mono_get_method_checked (image
, read32 (next_ip
+ 1), NULL
, generic_context
, error
)) &&
5317 (cmethod
->klass
== mono_defaults
.systemtype_class
) &&
5318 (strcmp (cmethod
->name
, "GetTypeFromHandle") == 0)) {
5319 interp_add_ins (td
, MINT_MONO_LDPTR
);
5320 gpointer systype
= mono_type_get_object_checked (domain
, (MonoType
*)handle
, error
);
5321 goto_if_nok (error
, exit
);
5322 td
->last_ins
->data
[0] = get_data_item_index (td
, systype
);
5323 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
5324 td
->ip
= next_ip
+ 5;
5326 PUSH_VT (td
, sizeof(gpointer
));
5327 interp_add_ins (td
, MINT_LDTOKEN
);
5328 td
->last_ins
->data
[0] = get_data_item_index (td
, handle
);
5329 PUSH_TYPE (td
, stack_type
[mt
], klass
);
5336 binary_arith_op(td
, MINT_ADD_OVF_I4
);
5339 case CEE_ADD_OVF_UN
:
5340 binary_arith_op(td
, MINT_ADD_OVF_UN_I4
);
5344 binary_arith_op(td
, MINT_MUL_OVF_I4
);
5347 case CEE_MUL_OVF_UN
:
5348 binary_arith_op(td
, MINT_MUL_OVF_UN_I4
);
5352 binary_arith_op(td
, MINT_SUB_OVF_I4
);
5355 case CEE_SUB_OVF_UN
:
5356 binary_arith_op(td
, MINT_SUB_OVF_UN_I4
);
5359 case CEE_ENDFINALLY
: {
5360 g_assert (td
->clause_indexes
[in_offset
] != -1);
5362 SIMPLE_OP (td
, MINT_ENDFINALLY
);
5363 td
->last_ins
->data
[0] = td
->clause_indexes
[in_offset
];
5370 if (*td
->ip
== CEE_LEAVE
)
5371 offset
= 5 + read32 (td
->ip
+ 1);
5373 offset
= 2 + (gint8
)td
->ip
[1];
5376 if (td
->clause_indexes
[in_offset
] != -1) {
5377 /* LEAVE instructions in catch clauses need to check for abort exceptions */
5378 handle_branch (td
, MINT_LEAVE_S_CHECK
, MINT_LEAVE_CHECK
, offset
);
5380 handle_branch (td
, MINT_LEAVE_S
, MINT_LEAVE
, offset
);
5383 if (*td
->ip
== CEE_LEAVE
)
5389 case MONO_CUSTOM_PREFIX
:
5392 case CEE_MONO_RETHROW
:
5393 CHECK_STACK (td
, 1);
5394 SIMPLE_OP (td
, MINT_MONO_RETHROW
);
5398 case CEE_MONO_LD_DELEGATE_METHOD_PTR
:
5401 interp_add_ins (td
, MINT_LD_DELEGATE_METHOD_PTR
);
5402 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
5404 case CEE_MONO_CALLI_EXTRA_ARG
:
5405 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
5406 interp_add_ins (td
, MINT_POP
);
5407 td
->last_ins
->data
[0] = 1;
5409 if (!interp_transform_call (td
, method
, NULL
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
, FALSE
))
5412 case CEE_MONO_JIT_ICALL_ADDR
: {
5413 const guint32 token
= read32 (td
->ip
+ 1);
5415 const gconstpointer func
= mono_find_jit_icall_info ((MonoJitICallId
)token
)->func
;
5417 interp_add_ins (td
, MINT_LDFTN
);
5418 td
->last_ins
->data
[0] = get_data_item_index (td
, (gpointer
)func
);
5419 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
5422 case CEE_MONO_ICALL
: {
5423 MonoJitICallId
const jit_icall_id
= (MonoJitICallId
)read32 (td
->ip
+ 1);
5424 MonoJitICallInfo
const * const info
= mono_find_jit_icall_info (jit_icall_id
);
5427 CHECK_STACK (td
, info
->sig
->param_count
);
5428 if (jit_icall_id
== MONO_JIT_ICALL_mono_threads_attach_coop
) {
5429 rtm
->needs_thread_attach
= 1;
5431 /* attach needs two arguments, and has one return value: leave one element on the stack */
5432 interp_add_ins (td
, MINT_POP
);
5433 td
->last_ins
->data
[0] = 0;
5434 } else if (jit_icall_id
== MONO_JIT_ICALL_mono_threads_detach_coop
) {
5435 g_assert (rtm
->needs_thread_attach
);
5437 /* detach consumes two arguments, and no return value: drop both of them */
5438 interp_add_ins (td
, MINT_POP
);
5439 td
->last_ins
->data
[0] = 0;
5440 interp_add_ins (td
, MINT_POP
);
5441 td
->last_ins
->data
[0] = 0;
5443 int const icall_op
= interp_icall_op_for_sig (info
->sig
);
5444 g_assert (icall_op
!= -1);
5446 interp_add_ins (td
, icall_op
);
5447 // hash here is overkill
5448 td
->last_ins
->data
[0] = get_data_item_index (td
, (gpointer
)info
->func
);
5450 td
->sp
-= info
->sig
->param_count
;
5452 if (!MONO_TYPE_IS_VOID (info
->sig
->ret
)) {
5453 int mt
= mint_type (info
->sig
->ret
);
5454 PUSH_SIMPLE_TYPE(td
, stack_type
[mt
]);
5458 case CEE_MONO_VTADDR
: {
5460 CHECK_STACK (td
, 1);
5461 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
5462 size
= mono_class_native_size(td
->sp
[-1].klass
, NULL
);
5464 size
= mono_class_value_size(td
->sp
[-1].klass
, NULL
);
5465 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
5466 interp_add_ins (td
, MINT_VTRESULT
);
5467 td
->last_ins
->data
[0] = 0;
5468 WRITE32_INS (td
->last_ins
, 1, &size
);
5471 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5474 case CEE_MONO_LDPTR
:
5475 case CEE_MONO_CLASSCONST
:
5476 token
= read32 (td
->ip
+ 1);
5478 interp_add_ins (td
, MINT_MONO_LDPTR
);
5479 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_method_get_wrapper_data (method
, token
));
5480 td
->sp
[0].type
= STACK_TYPE_I
;
5483 case CEE_MONO_OBJADDR
:
5484 CHECK_STACK (td
, 1);
5486 td
->sp
[-1].type
= STACK_TYPE_MP
;
5489 case CEE_MONO_NEWOBJ
:
5490 token
= read32 (td
->ip
+ 1);
5492 interp_add_ins (td
, MINT_MONO_NEWOBJ
);
5493 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_method_get_wrapper_data (method
, token
));
5494 td
->sp
[0].type
= STACK_TYPE_O
;
5497 case CEE_MONO_RETOBJ
:
5498 CHECK_STACK (td
, 1);
5499 token
= read32 (td
->ip
+ 1);
5501 interp_add_ins (td
, MINT_MONO_RETOBJ
);
5504 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
5506 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
5508 if (td
->sp
> td
->stack
)
5509 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td
->sp
-td
->stack
);
5511 case CEE_MONO_LDNATIVEOBJ
:
5512 token
= read32 (td
->ip
+ 1);
5514 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
5515 g_assert(m_class_is_valuetype (klass
));
5516 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5518 case CEE_MONO_TLS
: {
5519 gint32 key
= read32 (td
->ip
+ 1);
5521 g_assertf (key
== TLS_KEY_SGEN_THREAD_INFO
, "%d", key
);
5522 interp_add_ins (td
, MINT_MONO_SGEN_THREAD_INFO
);
5523 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
5526 case CEE_MONO_ATOMIC_STORE_I4
:
5527 CHECK_STACK (td
, 2);
5528 SIMPLE_OP (td
, MINT_MONO_ATOMIC_STORE_I4
);
5532 case CEE_MONO_SAVE_LMF
:
5533 case CEE_MONO_RESTORE_LMF
:
5534 case CEE_MONO_NOT_TAKEN
:
5537 case CEE_MONO_LDPTR_INT_REQ_FLAG
:
5538 interp_add_ins (td
, MINT_MONO_LDPTR
);
5539 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_thread_interruption_request_flag ());
5540 PUSH_TYPE (td
, STACK_TYPE_MP
, NULL
);
5543 case CEE_MONO_MEMORY_BARRIER
:
5544 interp_add_ins (td
, MINT_MONO_MEMORY_BARRIER
);
5547 case CEE_MONO_LDDOMAIN
:
5548 interp_add_ins (td
, MINT_MONO_LDDOMAIN
);
5549 td
->sp
[0].type
= STACK_TYPE_I
;
5553 case CEE_MONO_SAVE_LAST_ERROR
:
5554 save_last_error
= TRUE
;
5558 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td
->ip
, td
->ip
-header
->code
);
5568 case CEE_PREFIXREF
: ves_abort(); break;
5571 * Note: Exceptions thrown when executing a prefixed opcode need
5572 * to take into account the number of prefix bytes (usually the
5573 * throw point is just (ip - n_prefix_bytes).
5579 interp_add_ins (td
, MINT_ARGLIST
);
5580 PUSH_VT (td
, SIZEOF_VOID_P
);
5581 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_VT
);
5586 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
) {
5587 interp_add_ins (td
, MINT_CEQ_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5589 if (td
->sp
[-1].type
== STACK_TYPE_R4
&& td
->sp
[-2].type
== STACK_TYPE_R8
)
5590 interp_add_ins (td
, MINT_CONV_R8_R4
);
5591 if (td
->sp
[-1].type
== STACK_TYPE_R8
&& td
->sp
[-2].type
== STACK_TYPE_R4
)
5592 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
5593 interp_add_ins (td
, MINT_CEQ_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5596 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5601 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5602 interp_add_ins (td
, MINT_CGT_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5604 interp_add_ins (td
, MINT_CGT_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5606 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5611 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5612 interp_add_ins (td
, MINT_CGT_UN_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5614 interp_add_ins (td
, MINT_CGT_UN_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5616 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5621 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5622 interp_add_ins (td
, MINT_CLT_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5624 interp_add_ins (td
, MINT_CLT_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5626 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5631 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5632 interp_add_ins (td
, MINT_CLT_UN_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5634 interp_add_ins (td
, MINT_CLT_UN_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5636 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5639 case CEE_LDVIRTFTN
: /* fallthrough */
5642 if (*td
->ip
== CEE_LDVIRTFTN
) {
5643 CHECK_STACK (td
, 1);
5646 token
= read32 (td
->ip
+ 1);
5647 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
5648 m
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
5650 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
5651 goto_if_nok (error
, exit
);
5654 if (!mono_method_can_access_method (method
, m
))
5655 interp_generate_mae_throw (td
, method
, m
);
5657 if (method
->wrapper_type
== MONO_WRAPPER_NONE
&& m
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)
5658 m
= mono_marshal_get_synchronized_wrapper (m
);
5660 interp_add_ins (td
, *td
->ip
== CEE_LDFTN
? MINT_LDFTN
: MINT_LDVIRTFTN
);
5661 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
5662 goto_if_nok (error
, exit
);
5664 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_F
);
5668 int arg_n
= read16 (td
->ip
+ 1);
5669 if (td
->method
== method
)
5670 load_arg (td
, arg_n
);
5672 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
5677 int n
= read16 (td
->ip
+ 1);
5679 if (td
->method
== method
) {
5680 get_arg_type_exact (td
, n
, &mt
);
5681 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDARGA_VT
: MINT_LDARGA
);
5682 td
->last_ins
->data
[0] = n
;
5684 interp_add_ins (td
, MINT_LDLOCA_S
);
5685 td
->last_ins
->data
[0] = arg_offsets
[n
];
5687 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
5692 int arg_n
= read16 (td
->ip
+ 1);
5693 if (td
->method
== method
)
5694 store_arg (td
, arg_n
);
5696 store_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
5701 int loc_n
= read16 (td
->ip
+ 1);
5702 if (td
->method
== method
)
5703 load_local (td
, loc_n
);
5705 load_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
5710 int loc_n
= read16 (td
->ip
+ 1);
5711 interp_add_ins (td
, MINT_LDLOCA_S
);
5712 if (td
->method
== method
)
5713 td
->last_ins
->data
[0] = td
->rtm
->local_offsets
[loc_n
];
5715 td
->last_ins
->data
[0] = local_offsets
[loc_n
];
5716 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
5721 int loc_n
= read16 (td
->ip
+ 1);
5722 if (td
->method
== method
)
5723 store_local (td
, loc_n
);
5725 store_local_general (td
, local_offsets
[loc_n
], header
->locals
[loc_n
]);
5731 CHECK_STACK (td
, 1);
5732 #if SIZEOF_VOID_P == 8
5733 if (td
->sp
[-1].type
== STACK_TYPE_I8
)
5734 interp_add_ins (td
, MINT_CONV_I4_I8
);
5736 interp_add_ins (td
, MINT_LOCALLOC
);
5737 if (td
->sp
!= td
->stack
+ 1)
5738 g_warning("CEE_LOCALLOC: stack not empty");
5740 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5743 case CEE_UNUSED57
: ves_abort(); break;
5746 interp_add_ins (td
, MINT_ENDFILTER
);
5749 case CEE_UNALIGNED_
:
5758 /* FIX: should do something? */;
5759 // TODO: This should raise a method_tail_call profiler event.
5763 token
= read32 (td
->ip
+ 1);
5764 klass
= mini_get_class (method
, token
, generic_context
);
5765 CHECK_TYPELOAD (klass
);
5766 if (m_class_is_valuetype (klass
)) {
5767 interp_add_ins (td
, MINT_INITOBJ
);
5768 i32
= mono_class_value_size (klass
, NULL
);
5769 WRITE32_INS (td
->last_ins
, 0, &i32
);
5771 interp_add_ins (td
, MINT_LDNULL
);
5772 interp_add_ins (td
, MINT_STIND_REF
);
5779 /* FIX? convert length to I8? */
5781 interp_add_ins (td
, MINT_MONO_MEMORY_BARRIER
);
5782 interp_add_ins (td
, MINT_CPBLK
);
5783 BARRIER_IF_VOLATILE (td
);
5791 case CEE_CONSTRAINED_
:
5792 token
= read32 (td
->ip
+ 1);
5793 constrained_class
= mini_get_class (method
, token
, generic_context
);
5794 CHECK_TYPELOAD (constrained_class
);
5799 BARRIER_IF_VOLATILE (td
);
5800 interp_add_ins (td
, MINT_INITBLK
);
5805 /* FIXME: implement */
5809 int clause_index
= td
->clause_indexes
[in_offset
];
5810 g_assert (clause_index
!= -1);
5811 SIMPLE_OP (td
, MINT_RETHROW
);
5812 td
->last_ins
->data
[0] = rtm
->exvar_offsets
[clause_index
];
5818 token
= read32 (td
->ip
+ 1);
5820 if (mono_metadata_token_table (token
) == MONO_TABLE_TYPESPEC
&& !image_is_dynamic (m_class_get_image (method
->klass
)) && !generic_context
) {
5822 MonoType
*type
= mono_type_create_from_typespec_checked (image
, token
, error
);
5823 goto_if_nok (error
, exit
);
5824 size
= mono_type_size (type
, &align
);
5827 MonoClass
*szclass
= mini_get_class (method
, token
, generic_context
);
5828 CHECK_TYPELOAD (szclass
);
5830 if (!szclass
->valuetype
)
5831 THROW_EX (mono_exception_from_name (mono_defaults
.corlib
, "System", "InvalidProgramException"), ip
- 5);
5833 size
= mono_type_size (m_class_get_byval_arg (szclass
), &align
);
5835 interp_add_ins (td
, MINT_LDC_I4
);
5836 WRITE32_INS (td
->last_ins
, 0, &size
);
5837 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
5840 case CEE_REFANYTYPE
:
5841 interp_add_ins (td
, MINT_REFANYTYPE
);
5843 POP_VT (td
, sizeof (MonoTypedRef
));
5844 PUSH_VT (td
, sizeof (gpointer
));
5845 SET_TYPE(td
->sp
- 1, STACK_TYPE_VT
, NULL
);
5848 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
);
5852 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td
->ip
, td
->ip
-header
->code
);
5855 // No IR instructions were added as part of the IL instruction. Extend bb_start
5856 if (prev_last_ins
== td
->last_ins
&& td
->is_bb_start
[in_offset
] && td
->ip
< end
)
5857 td
->is_bb_start
[td
->ip
- td
->il_code
] = 1;
5860 g_assert (td
->ip
== end
);
5863 g_free (arg_offsets
);
5864 g_free (local_offsets
);
5865 mono_basic_block_free (original_bb
);
5866 for (i
= 0; i
< header
->code_size
; ++i
)
5867 g_free (td
->stack_state
[i
]);
5868 g_free (td
->stack_state
);
5869 g_free (td
->stack_height
);
5870 g_free (td
->vt_stack_size
);
5871 g_free (td
->clause_indexes
);
5872 g_free (td
->is_bb_start
);
5880 // We are trying to branch to an il offset that has no associated ir instruction with it.
5881 // We will branch instead to the next instruction that we find
5883 resolve_in_offset (TransformData
*td
, int il_offset
)
5886 g_assert (!td
->in_offsets
[il_offset
]);
5887 while (!td
->in_offsets
[i
])
5889 td
->in_offsets
[il_offset
] = td
->in_offsets
[i
];
5890 return td
->in_offsets
[il_offset
];
5893 // We store in the in_offset array the native_offset + 1, so 0 can mean only that the il
5894 // offset is uninitialized. Otherwise 0 is valid value for first interp instruction.
5896 get_in_offset (TransformData
*td
, int il_offset
)
5898 int target_offset
= td
->in_offsets
[il_offset
];
5900 return target_offset
- 1;
5901 return resolve_in_offset (td
, il_offset
) - 1;
5905 handle_relocations (TransformData
*td
)
5907 // Handle relocations
5908 for (int i
= 0; i
< td
->relocs
->len
; ++i
) {
5909 Reloc
*reloc
= (Reloc
*)g_ptr_array_index (td
->relocs
, i
);
5910 int offset
= get_in_offset (td
, reloc
->target
) - reloc
->offset
;
5912 switch (reloc
->type
) {
5913 case RELOC_SHORT_BRANCH
:
5914 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xdead);
5915 td
->new_code
[reloc
->offset
+ 1] = offset
;
5917 case RELOC_LONG_BRANCH
: {
5918 guint16
*v
= (guint16
*) &offset
;
5919 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xdead);
5920 g_assert (td
->new_code
[reloc
->offset
+ 2] == 0xbeef);
5921 td
->new_code
[reloc
->offset
+ 1] = *(guint16
*) v
;
5922 td
->new_code
[reloc
->offset
+ 2] = *(guint16
*) (v
+ 1);
5925 case RELOC_SWITCH
: {
5926 guint16
*v
= (guint16
*)&offset
;
5927 g_assert (td
->new_code
[reloc
->offset
] == 0xdead);
5928 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xbeef);
5929 td
->new_code
[reloc
->offset
] = *(guint16
*)v
;
5930 td
->new_code
[reloc
->offset
+ 1] = *(guint16
*)(v
+ 1);
5934 g_assert_not_reached ();
5942 get_inst_length (InterpInst
*ins
)
5944 if (ins
->opcode
== MINT_SWITCH
)
5945 return MINT_SWITCH_LEN (READ32 (&ins
->data
[0]));
5947 return mono_interp_oplen
[ins
->opcode
];
5951 emit_compacted_instruction (TransformData
*td
, guint16
* start_ip
, InterpInst
*ins
)
5953 guint16 opcode
= ins
->opcode
;
5954 guint16
*ip
= start_ip
;
5956 // We know what IL offset this instruction was created for. We can now map the IL offset
5957 // to the IR offset. We use this array to resolve the relocations, which reference the IL.
5958 if (ins
->il_offset
!= -1 && !td
->in_offsets
[ins
->il_offset
]) {
5959 g_assert (ins
->il_offset
>= 0 && ins
->il_offset
< td
->header
->code_size
);
5960 td
->in_offsets
[ins
->il_offset
] = start_ip
- td
->new_code
+ 1;
5962 MonoDebugLineNumberEntry lne
;
5963 lne
.native_offset
= (guint8
*)start_ip
- (guint8
*)td
->new_code
;
5964 lne
.il_offset
= ins
->il_offset
;
5965 g_array_append_val (td
->line_numbers
, lne
);
5969 if (opcode
== MINT_SWITCH
) {
5970 int labels
= READ32 (&ins
->data
[0]);
5971 // Write number of switch labels
5972 *ip
++ = ins
->data
[0];
5973 *ip
++ = ins
->data
[1];
5974 // Add relocation for each label
5975 for (int i
= 0; i
< labels
; i
++) {
5976 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
5977 reloc
->type
= RELOC_SWITCH
;
5978 reloc
->offset
= ip
- td
->new_code
;
5979 reloc
->target
= READ32 (&ins
->data
[2 + i
* 2]);
5980 g_ptr_array_add (td
->relocs
, reloc
);
5984 } else if ((opcode
>= MINT_BRFALSE_I4_S
&& opcode
<= MINT_BRTRUE_R8_S
) ||
5985 (opcode
>= MINT_BEQ_I4_S
&& opcode
<= MINT_BLT_UN_R8_S
) ||
5986 opcode
== MINT_BR_S
|| opcode
== MINT_LEAVE_S
|| opcode
== MINT_LEAVE_S_CHECK
) {
5987 const int br_offset
= start_ip
- td
->new_code
;
5988 if (ins
->data
[0] < ins
->il_offset
) {
5989 // Backwards branch. We can already patch it.
5990 *ip
++ = get_in_offset (td
, ins
->data
[0]) - br_offset
;
5992 // We don't know the in_offset of the target, add a reloc
5993 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
5994 reloc
->type
= RELOC_SHORT_BRANCH
;
5995 reloc
->offset
= br_offset
;
5996 reloc
->target
= ins
->data
[0];
5997 g_ptr_array_add (td
->relocs
, reloc
);
6000 } else if ((opcode
>= MINT_BRFALSE_I4
&& opcode
<= MINT_BRTRUE_R8
) ||
6001 (opcode
>= MINT_BEQ_I4
&& opcode
<= MINT_BLT_UN_R8
) ||
6002 opcode
== MINT_BR
|| opcode
== MINT_LEAVE
|| opcode
== MINT_LEAVE_CHECK
) {
6003 const int br_offset
= start_ip
- td
->new_code
;
6004 int target_il
= READ32 (&ins
->data
[0]);
6005 if (target_il
< ins
->il_offset
) {
6006 // Backwards branch. We can already patch it
6007 const int br_offset
= start_ip
- td
->new_code
;
6008 int target_offset
= get_in_offset (td
, target_il
) - br_offset
;
6009 WRITE32 (ip
, &target_offset
);
6011 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
6012 reloc
->type
= RELOC_LONG_BRANCH
;
6013 reloc
->offset
= br_offset
;
6014 reloc
->target
= target_il
;
6015 g_ptr_array_add (td
->relocs
, reloc
);
6019 } else if (opcode
== MINT_SDB_SEQ_POINT
) {
6020 SeqPoint
*seqp
= (SeqPoint
*)mono_mempool_alloc0 (td
->mempool
, sizeof (SeqPoint
));
6021 InterpBasicBlock
*cbb
;
6023 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY
) {
6024 seqp
->il_offset
= METHOD_ENTRY_IL_OFFSET
;
6025 cbb
= td
->offset_to_bb
[0];
6027 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT
)
6028 seqp
->il_offset
= METHOD_EXIT_IL_OFFSET
;
6030 seqp
->il_offset
= ins
->il_offset
;
6031 cbb
= td
->offset_to_bb
[ins
->il_offset
];
6033 seqp
->native_offset
= (guint8
*)start_ip
- (guint8
*)td
->new_code
;
6034 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK
)
6035 seqp
->flags
|= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK
;
6036 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL
)
6037 seqp
->flags
|= MONO_SEQ_POINT_FLAG_NESTED_CALL
;
6038 g_ptr_array_add (td
->seq_points
, seqp
);
6040 cbb
->seq_points
= g_slist_prepend_mempool (td
->mempool
, cbb
->seq_points
, seqp
);
6041 cbb
->last_seq_point
= seqp
;
6043 int size
= get_inst_length (ins
) - 1;
6044 // Emit the rest of the data
6045 for (int i
= 0; i
< size
; i
++)
6046 *ip
++ = ins
->data
[i
];
6051 // Generates the final code, after we are done with all the passes
6053 generate_compacted_code (TransformData
*td
)
6057 td
->relocs
= g_ptr_array_new ();
6059 // Iterate once to compute the exact size of the compacted code
6060 InterpInst
*ins
= td
->first_ins
;
6062 size
+= get_inst_length (ins
);
6066 // Generate the compacted stream of instructions
6067 td
->new_code
= ip
= (guint16
*)mono_domain_alloc0 (td
->rtm
->domain
, size
* sizeof (guint16
));
6068 ins
= td
->first_ins
;
6070 ip
= emit_compacted_instruction (td
, ip
, ins
);
6073 td
->new_code_end
= ip
;
6074 td
->in_offsets
[td
->header
->code_size
] = td
->new_code_end
- td
->new_code
;
6076 // Patch all branches
6077 handle_relocations (td
);
6079 g_ptr_array_free (td
->relocs
, TRUE
);
6083 generate (MonoMethod
*method
, MonoMethodHeader
*header
, InterpMethod
*rtm
, MonoGenericContext
*generic_context
, MonoError
*error
)
6085 MonoDomain
*domain
= rtm
->domain
;
6087 TransformData transform_data
;
6089 static gboolean verbose_method_inited
;
6090 static char* verbose_method_name
;
6092 if (!verbose_method_inited
) {
6093 verbose_method_name
= g_getenv ("MONO_VERBOSE_METHOD");
6094 verbose_method_inited
= TRUE
;
6097 memset (&transform_data
, 0, sizeof(transform_data
));
6098 td
= &transform_data
;
6100 td
->method
= method
;
6102 td
->code_size
= header
->code_size
;
6103 td
->header
= header
;
6104 td
->max_code_size
= td
->code_size
;
6105 td
->in_offsets
= (int*)g_malloc0((header
->code_size
+ 1) * sizeof(int));
6106 td
->mempool
= mono_mempool_new ();
6107 td
->n_data_items
= 0;
6108 td
->max_data_items
= 0;
6109 td
->data_items
= NULL
;
6110 td
->data_hash
= g_hash_table_new (NULL
, NULL
);
6111 td
->gen_sdb_seq_points
= mini_debug_options
.gen_sdb_seq_points
;
6112 td
->seq_points
= g_ptr_array_new ();
6113 td
->verbose_level
= mono_interp_traceopt
;
6114 td
->total_locals_size
= rtm
->locals_size
;
6115 rtm
->data_items
= td
->data_items
;
6117 if (verbose_method_name
) {
6118 const char *name
= verbose_method_name
;
6120 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
6121 MonoMethodDesc
*desc
;
6123 desc
= mono_method_desc_new (name
, TRUE
);
6124 if (mono_method_desc_full_match (desc
, method
)) {
6125 td
->verbose_level
= 4;
6127 mono_method_desc_free (desc
);
6129 if (strcmp (method
->name
, name
) == 0)
6130 td
->verbose_level
= 4;
6134 td
->stack
= (StackInfo
*)g_malloc0 ((header
->max_stack
+ 1) * sizeof (td
->stack
[0]));
6135 td
->stack_capacity
= header
->max_stack
+ 1;
6137 td
->max_stack_height
= 0;
6138 td
->line_numbers
= g_array_new (FALSE
, TRUE
, sizeof (MonoDebugLineNumberEntry
));
6140 generate_code (td
, method
, header
, generic_context
, error
);
6141 goto_if_nok (error
, exit
);
6143 generate_compacted_code (td
);
6145 if (td
->verbose_level
) {
6146 g_print ("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method
, TRUE
), rtm
, td
->max_vt_sp
);
6147 g_print ("Calculated stack size: %d, stated size: %d\n", td
->max_stack_height
, header
->max_stack
);
6148 dump_mint_code (td
->new_code
, td
->new_code_end
);
6151 /* Check if we use excessive stack space */
6152 if (td
->max_stack_height
> header
->max_stack
* 3 && header
->max_stack
> 16)
6153 g_warning ("Excessive stack space usage for method %s, %d/%d", method
->name
, td
->max_stack_height
, header
->max_stack
);
6156 code_len
= td
->new_code_end
- td
->new_code
;
6158 rtm
->clauses
= (MonoExceptionClause
*)mono_domain_alloc0 (domain
, header
->num_clauses
* sizeof (MonoExceptionClause
));
6159 memcpy (rtm
->clauses
, header
->clauses
, header
->num_clauses
* sizeof(MonoExceptionClause
));
6160 rtm
->code
= (gushort
*)td
->new_code
;
6161 rtm
->init_locals
= header
->init_locals
;
6162 rtm
->num_clauses
= header
->num_clauses
;
6163 for (i
= 0; i
< header
->num_clauses
; i
++) {
6164 MonoExceptionClause
*c
= rtm
->clauses
+ i
;
6165 int end_off
= c
->try_offset
+ c
->try_len
;
6166 c
->try_offset
= get_in_offset (td
, c
->try_offset
);
6167 c
->try_len
= get_in_offset (td
, end_off
) - c
->try_offset
;
6168 g_assert ((c
->try_offset
+ c
->try_len
) < code_len
);
6169 end_off
= c
->handler_offset
+ c
->handler_len
;
6170 c
->handler_offset
= get_in_offset (td
, c
->handler_offset
);
6171 c
->handler_len
= get_in_offset (td
, end_off
) - c
->handler_offset
;
6172 g_assert (c
->handler_len
>= 0 && (c
->handler_offset
+ c
->handler_len
) <= code_len
);
6173 if (c
->flags
& MONO_EXCEPTION_CLAUSE_FILTER
)
6174 c
->data
.filter_offset
= get_in_offset (td
, c
->data
.filter_offset
);
6176 rtm
->stack_size
= (sizeof (stackval
)) * (td
->max_stack_height
+ 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
6177 rtm
->stack_size
= ALIGN_TO (rtm
->stack_size
, MINT_VT_ALIGNMENT
);
6178 rtm
->vt_stack_size
= td
->max_vt_sp
;
6179 rtm
->total_locals_size
= td
->total_locals_size
;
6180 rtm
->alloca_size
= rtm
->total_locals_size
+ rtm
->vt_stack_size
+ rtm
->stack_size
;
6181 rtm
->data_items
= (gpointer
*)mono_domain_alloc0 (domain
, td
->n_data_items
* sizeof (td
->data_items
[0]));
6182 memcpy (rtm
->data_items
, td
->data_items
, td
->n_data_items
* sizeof (td
->data_items
[0]));
6184 /* Save debug info */
6185 interp_save_debug_info (rtm
, header
, td
, td
->line_numbers
);
6187 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
6189 jinfo_len
= mono_jit_info_size ((MonoJitInfoFlags
)0, header
->num_clauses
, 0);
6191 jinfo
= (MonoJitInfo
*)mono_domain_alloc0 (domain
, jinfo_len
);
6192 jinfo
->is_interp
= 1;
6194 mono_jit_info_init (jinfo
, method
, (guint8
*)rtm
->code
, code_len
, (MonoJitInfoFlags
)0, header
->num_clauses
, 0);
6195 for (i
= 0; i
< jinfo
->num_clauses
; ++i
) {
6196 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
6197 MonoExceptionClause
*c
= rtm
->clauses
+ i
;
6199 ei
->flags
= c
->flags
;
6200 ei
->try_start
= (guint8
*)(rtm
->code
+ c
->try_offset
);
6201 ei
->try_end
= (guint8
*)(rtm
->code
+ c
->try_offset
+ c
->try_len
);
6202 ei
->handler_start
= (guint8
*)(rtm
->code
+ c
->handler_offset
);
6203 ei
->exvar_offset
= rtm
->exvar_offsets
[i
];
6204 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
6205 ei
->data
.filter
= (guint8
*)(rtm
->code
+ c
->data
.filter_offset
);
6206 } else if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
6207 ei
->data
.handler_end
= (guint8
*)(rtm
->code
+ c
->handler_offset
+ c
->handler_len
);
6209 ei
->data
.catch_class
= c
->data
.catch_class
;
6213 save_seq_points (td
, jinfo
);
6216 g_free (td
->in_offsets
);
6217 g_free (td
->data_items
);
6219 g_hash_table_destroy (td
->data_hash
);
6220 g_ptr_array_free (td
->seq_points
, TRUE
);
6221 g_array_free (td
->line_numbers
, TRUE
);
6222 mono_mempool_destroy (td
->mempool
);
6225 static mono_mutex_t calc_section
;
6228 mono_interp_transform_init (void)
6230 mono_os_mutex_init_recursive(&calc_section
);
6234 mono_interp_transform_method (InterpMethod
*imethod
, ThreadContext
*context
, MonoError
*error
)
6236 MonoMethod
*method
= imethod
->method
;
6237 MonoMethodHeader
*header
= NULL
;
6238 MonoMethodSignature
*signature
= mono_method_signature_internal (method
);
6239 MonoVTable
*method_class_vt
;
6240 MonoGenericContext
*generic_context
= NULL
;
6241 MonoDomain
*domain
= imethod
->domain
;
6242 InterpMethod tmp_imethod
;
6243 InterpMethod
*real_imethod
;
6247 if (mono_class_is_open_constructed_type (m_class_get_byval_arg (method
->klass
))) {
6248 mono_error_set_invalid_operation (error
, "%s", "Could not execute the method because the containing type is not fully instantiated.");
6252 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
6253 method_class_vt
= mono_class_vtable_checked (domain
, imethod
->method
->klass
, error
);
6254 return_if_nok (error
);
6256 if (!method_class_vt
->initialized
) {
6257 mono_runtime_class_init_full (method_class_vt
, error
);
6258 return_if_nok (error
);
6261 MONO_PROFILER_RAISE (jit_begin
, (method
));
6263 if (mono_method_signature_internal (method
)->is_inflated
)
6264 generic_context
= mono_method_get_context (method
);
6266 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (method
);
6267 if (generic_container
)
6268 generic_context
= &generic_container
->context
;
6271 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
6272 MonoMethod
*nm
= NULL
;
6273 if (imethod
->transformed
) {
6274 MONO_PROFILER_RAISE (jit_done
, (method
, imethod
->jinfo
));
6278 /* assumes all internal calls with an array this are built in... */
6279 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
&& (! mono_method_signature_internal (method
)->hasthis
|| m_class_get_rank (method
->klass
) == 0)) {
6280 nm
= mono_marshal_get_native_wrapper (method
, FALSE
, FALSE
);
6281 signature
= mono_method_signature_internal (nm
);
6283 const char *name
= method
->name
;
6284 if (m_class_get_parent (method
->klass
) == mono_defaults
.multicastdelegate_class
) {
6285 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
6286 MonoJitICallInfo
*mi
= &mono_get_jit_icall_info ()->ves_icall_mono_delegate_ctor_interp
;
6287 nm
= mono_marshal_get_icall_wrapper (mi
, TRUE
);
6288 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
6290 * Usually handled during transformation of the caller, but
6291 * when the caller is handled by another execution engine
6292 * (for example fullAOT) we need to handle it here. That's
6293 * known to be wrong in cases where the reference to
6294 * `MonoDelegate` would be needed (FIXME).
6296 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
6297 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
6298 nm
= mono_marshal_get_delegate_begin_invoke (method
);
6299 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
6300 nm
= mono_marshal_get_delegate_end_invoke (method
);
6304 g_assert_not_reached ();
6307 mono_os_mutex_lock (&calc_section
);
6308 imethod
->stack_size
= sizeof (stackval
); /* for tracing */
6309 imethod
->alloca_size
= imethod
->stack_size
;
6310 mono_memory_barrier ();
6311 imethod
->transformed
= TRUE
;
6312 mono_os_mutex_unlock (&calc_section
);
6313 MONO_PROFILER_RAISE (jit_done
, (method
, NULL
));
6317 header
= interp_method_get_header (nm
, error
);
6318 return_if_nok (error
);
6322 header
= mono_method_get_header_checked (method
, error
);
6323 return_if_nok (error
);
6326 g_assert ((signature
->param_count
+ signature
->hasthis
) < 1000);
6327 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
6329 /* Make modifications to a copy of imethod, copy them back inside the lock */
6330 real_imethod
= imethod
;
6331 memcpy (&tmp_imethod
, imethod
, sizeof (InterpMethod
));
6332 imethod
= &tmp_imethod
;
6334 interp_method_compute_offsets (imethod
, signature
, header
);
6336 MONO_TIME_TRACK (mono_interp_stats
.transform_time
, generate (method
, header
, imethod
, generic_context
, error
));
6338 mono_metadata_free_mh (header
);
6340 return_if_nok (error
);
6342 /* Copy changes back */
6343 imethod
= real_imethod
;
6344 mono_os_mutex_lock (&calc_section
);
6345 if (!imethod
->transformed
) {
6346 // Ignore the first two fields which are unchanged. next_jit_code_hash shouldn't
6347 // be modified because it is racy with internal hash table insert.
6348 const int start_offset
= 2 * sizeof (gpointer
);
6349 memcpy ((char*)imethod
+ start_offset
, (char*)&tmp_imethod
+ start_offset
, sizeof (InterpMethod
) - start_offset
);
6350 mono_memory_barrier ();
6351 imethod
->transformed
= TRUE
;
6352 mono_atomic_fetch_add_i32 (&mono_jit_stats
.methods_with_interp
, 1);
6355 mono_os_mutex_unlock (&calc_section
);
6357 mono_domain_lock (domain
);
6358 if (!g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, imethod
->method
))
6359 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, imethod
->method
, imethod
->jinfo
->seq_points
);
6360 mono_domain_unlock (domain
);
6362 // FIXME: Add a different callback ?
6363 MONO_PROFILER_RAISE (jit_done
, (method
, imethod
->jinfo
));