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/debug-helpers.h>
14 #include <mono/metadata/exception.h>
15 #include <mono/metadata/exception-internals.h>
16 #include <mono/metadata/mono-endian.h>
17 #include <mono/metadata/marshal.h>
18 #include <mono/metadata/profiler-private.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/seq-points-data.h>
21 #include <mono/metadata/mono-basic-block.h>
22 #include <mono/metadata/abi-details.h>
23 #include <mono/metadata/reflection-internals.h>
24 #include <mono/utils/unlocked.h>
26 #include <mono/mini/mini.h>
27 #include <mono/mini/mini-runtime.h>
30 #include "interp-internals.h"
33 #define INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK 1
34 #define INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY 2
35 #define INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT 4
36 #define INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL 8
38 MonoInterpStats mono_interp_stats
;
42 typedef struct InterpInst InterpInst
;
53 InterpInst
*next
, *prev
;
54 // If this is -1, this instruction is not logically associated with an IL offset, it is
55 // part of the IL instruction associated with the previous interp instruction.
58 guint16 data
[MONO_ZERO_LEN_ARRAY
];
65 SeqPoint
*last_seq_point
;
67 // This will hold a list of last sequence points of incoming basic blocks
68 SeqPoint
**pred_seq_points
;
69 guint num_pred_seq_points
;
80 /* In the interpreter IR */
89 MonoMethod
*inlined_method
;
90 MonoMethodHeader
*header
;
92 const unsigned char *il_code
;
93 const unsigned char *ip
;
94 const unsigned char *in_start
;
95 InterpInst
*last_ins
, *first_ins
;
98 StackInfo
**stack_state
;
101 unsigned char *is_bb_start
;
102 unsigned short *new_code
;
103 unsigned short *new_code_end
;
104 unsigned int max_code_size
;
107 unsigned int max_stack_height
;
108 unsigned int stack_capacity
;
110 unsigned int max_vt_sp
;
111 unsigned int total_locals_size
;
115 GHashTable
*data_hash
;
117 gboolean gen_sdb_seq_points
;
118 GPtrArray
*seq_points
;
119 InterpBasicBlock
**offset_to_bb
;
120 InterpBasicBlock
*entry_bb
;
121 MonoMemPool
*mempool
;
124 gboolean verbose_level
;
125 GArray
*line_numbers
;
128 #define STACK_TYPE_I4 0
129 #define STACK_TYPE_I8 1
130 #define STACK_TYPE_R4 2
131 #define STACK_TYPE_R8 3
132 #define STACK_TYPE_O 4
133 #define STACK_TYPE_VT 5
134 #define STACK_TYPE_MP 6
135 #define STACK_TYPE_F 7
137 static const char *stack_type_string
[] = { "I4", "I8", "R4", "R8", "O ", "VT", "MP", "F " };
139 #if SIZEOF_VOID_P == 8
140 #define STACK_TYPE_I STACK_TYPE_I8
142 #define STACK_TYPE_I STACK_TYPE_I4
145 static int stack_type
[] = {
146 STACK_TYPE_I4
, /*I1*/
147 STACK_TYPE_I4
, /*U1*/
148 STACK_TYPE_I4
, /*I2*/
149 STACK_TYPE_I4
, /*U2*/
150 STACK_TYPE_I4
, /*I4*/
151 STACK_TYPE_I8
, /*I8*/
152 STACK_TYPE_R4
, /*R4*/
153 STACK_TYPE_R8
, /*R8*/
159 #if SIZEOF_VOID_P == 8
160 #define MINT_NEG_P MINT_NEG_I8
161 #define MINT_NOT_P MINT_NOT_I8
163 #define MINT_NEG_FP MINT_NEG_R8
165 #define MINT_ADD_P MINT_ADD_I8
166 #define MINT_SUB_P MINT_SUB_I8
167 #define MINT_MUL_P MINT_MUL_I8
168 #define MINT_DIV_P MINT_DIV_I8
169 #define MINT_DIV_UN_P MINT_DIV_UN_I8
170 #define MINT_REM_P MINT_REM_I8
171 #define MINT_REM_UN_P MINT_REM_UN_I8
172 #define MINT_AND_P MINT_AND_I8
173 #define MINT_OR_P MINT_OR_I8
174 #define MINT_XOR_P MINT_XOR_I8
175 #define MINT_SHL_P MINT_SHL_I8
176 #define MINT_SHR_P MINT_SHR_I8
177 #define MINT_SHR_UN_P MINT_SHR_UN_I8
179 #define MINT_CEQ_P MINT_CEQ_I8
180 #define MINT_CNE_P MINT_CNE_I8
181 #define MINT_CLT_P MINT_CLT_I8
182 #define MINT_CLT_UN_P MINT_CLT_UN_I8
183 #define MINT_CGT_P MINT_CGT_I8
184 #define MINT_CGT_UN_P MINT_CGT_UN_I8
185 #define MINT_CLE_P MINT_CLE_I8
186 #define MINT_CLE_UN_P MINT_CLE_UN_I8
187 #define MINT_CGE_P MINT_CGE_I8
188 #define MINT_CGE_UN_P MINT_CGE_UN_I8
190 #define MINT_ADD_FP MINT_ADD_R8
191 #define MINT_SUB_FP MINT_SUB_R8
192 #define MINT_MUL_FP MINT_MUL_R8
193 #define MINT_DIV_FP MINT_DIV_R8
194 #define MINT_REM_FP MINT_REM_R8
196 #define MINT_CNE_FP MINT_CNE_R8
197 #define MINT_CEQ_FP MINT_CEQ_R8
198 #define MINT_CGT_FP MINT_CGT_R8
199 #define MINT_CGE_FP MINT_CGE_R8
200 #define MINT_CLT_FP MINT_CLT_R8
201 #define MINT_CLE_FP MINT_CLE_R8
205 #define MINT_NEG_P MINT_NEG_I4
206 #define MINT_NOT_P MINT_NOT_I4
208 #define MINT_NEG_FP MINT_NEG_R4
210 #define MINT_ADD_P MINT_ADD_I4
211 #define MINT_SUB_P MINT_SUB_I4
212 #define MINT_MUL_P MINT_MUL_I4
213 #define MINT_DIV_P MINT_DIV_I4
214 #define MINT_DIV_UN_P MINT_DIV_UN_I4
215 #define MINT_REM_P MINT_REM_I4
216 #define MINT_REM_UN_P MINT_REM_UN_I4
217 #define MINT_AND_P MINT_AND_I4
218 #define MINT_OR_P MINT_OR_I4
219 #define MINT_XOR_P MINT_XOR_I4
220 #define MINT_SHL_P MINT_SHL_I4
221 #define MINT_SHR_P MINT_SHR_I4
222 #define MINT_SHR_UN_P MINT_SHR_UN_I4
224 #define MINT_CEQ_P MINT_CEQ_I4
225 #define MINT_CNE_P MINT_CNE_I4
226 #define MINT_CLT_P MINT_CLT_I4
227 #define MINT_CLT_UN_P MINT_CLT_UN_I4
228 #define MINT_CGT_P MINT_CGT_I4
229 #define MINT_CGT_UN_P MINT_CGT_UN_I4
230 #define MINT_CLE_P MINT_CLE_I4
231 #define MINT_CLE_UN_P MINT_CLE_UN_I4
232 #define MINT_CGE_P MINT_CGE_I4
233 #define MINT_CGE_UN_P MINT_CGE_UN_I4
235 #define MINT_ADD_FP MINT_ADD_R4
236 #define MINT_SUB_FP MINT_SUB_R4
237 #define MINT_MUL_FP MINT_MUL_R4
238 #define MINT_DIV_FP MINT_DIV_R4
239 #define MINT_REM_FP MINT_REM_R4
241 #define MINT_CNE_FP MINT_CNE_R4
242 #define MINT_CEQ_FP MINT_CEQ_R4
243 #define MINT_CGT_FP MINT_CGT_R4
244 #define MINT_CGE_FP MINT_CGE_R4
245 #define MINT_CLT_FP MINT_CLT_R4
246 #define MINT_CLE_FP MINT_CLE_R4
251 const gchar
*op_name
;
255 // static const MagicIntrinsic int_binop[] = {
257 static const MagicIntrinsic int_unnop
[] = {
258 { "op_UnaryPlus", {MINT_NOP
, MINT_NOP
, MINT_NOP
}},
259 { "op_UnaryNegation", {MINT_NEG_P
, MINT_NEG_P
, MINT_NEG_FP
}},
260 { "op_OnesComplement", {MINT_NOT_P
, MINT_NOT_P
, MINT_NIY
}}
263 static const MagicIntrinsic int_binop
[] = {
264 { "op_Addition", {MINT_ADD_P
, MINT_ADD_P
, MINT_ADD_FP
}},
265 { "op_Subtraction", {MINT_SUB_P
, MINT_SUB_P
, MINT_SUB_FP
}},
266 { "op_Multiply", {MINT_MUL_P
, MINT_MUL_P
, MINT_MUL_FP
}},
267 { "op_Division", {MINT_DIV_P
, MINT_DIV_UN_P
, MINT_DIV_FP
}},
268 { "op_Modulus", {MINT_REM_P
, MINT_REM_UN_P
, MINT_REM_FP
}},
269 { "op_BitwiseAnd", {MINT_AND_P
, MINT_AND_P
, MINT_NIY
}},
270 { "op_BitwiseOr", {MINT_OR_P
, MINT_OR_P
, MINT_NIY
}},
271 { "op_ExclusiveOr", {MINT_XOR_P
, MINT_XOR_P
, MINT_NIY
}},
272 { "op_LeftShift", {MINT_SHL_P
, MINT_SHL_P
, MINT_NIY
}},
273 { "op_RightShift", {MINT_SHR_P
, MINT_SHR_UN_P
, MINT_NIY
}},
276 static const MagicIntrinsic int_cmpop
[] = {
277 { "op_Inequality", {MINT_CNE_P
, MINT_CNE_P
, MINT_CNE_FP
}},
278 { "op_Equality", {MINT_CEQ_P
, MINT_CEQ_P
, MINT_CEQ_FP
}},
279 { "op_GreaterThan", {MINT_CGT_P
, MINT_CGT_UN_P
, MINT_CGT_FP
}},
280 { "op_GreaterThanOrEqual", {MINT_CGE_P
, MINT_CGE_UN_P
, MINT_CGE_FP
}},
281 { "op_LessThan", {MINT_CLT_P
, MINT_CLT_UN_P
, MINT_CLT_FP
}},
282 { "op_LessThanOrEqual", {MINT_CLE_P
, MINT_CLE_UN_P
, MINT_CLE_FP
}}
285 static gboolean
generate_code (TransformData
*td
, MonoMethod
*method
, MonoMethodHeader
*header
, MonoGenericContext
*generic_context
, MonoError
*error
);
288 interp_new_ins (TransformData
*td
, guint16 opcode
, int len
)
290 InterpInst
*new_inst
;
292 // Size of data region of instruction is length of instruction minus 1 (the opcode slot)
293 new_inst
= mono_mempool_alloc0 (td
->mempool
, sizeof (InterpInst
) + sizeof (guint16
) * (len
- 1));
294 new_inst
->opcode
= opcode
;
295 // opcodes from inlined methods don't have il offset associated with them since the offset
296 // stored here is relevant only for the original method
297 if (!td
->inlined_method
)
298 new_inst
->il_offset
= td
->in_start
- td
->il_code
;
300 new_inst
->il_offset
= -1;
304 // This version need to be used with switch opcode, which doesn't have constant length
306 interp_add_ins_explicit (TransformData
*td
, guint16 opcode
, int len
)
308 InterpInst
*new_inst
= interp_new_ins (td
, opcode
, len
);
309 new_inst
->prev
= td
->last_ins
;
311 td
->last_ins
->next
= new_inst
;
313 td
->first_ins
= new_inst
;
314 td
->last_ins
= new_inst
;
319 interp_add_ins (TransformData
*td
, guint16 opcode
)
321 return interp_add_ins_explicit (td
, opcode
, mono_interp_oplen
[opcode
]);
324 // Inserts a new instruction inside the instruction stream
326 interp_insert_ins (TransformData
*td
, InterpInst
*prev_ins
, guint16 opcode
)
328 InterpInst
*new_inst
= interp_new_ins (td
, opcode
, mono_interp_oplen
[opcode
]);
329 g_assert (prev_ins
&& prev_ins
->next
);
331 new_inst
->prev
= prev_ins
;
332 new_inst
->next
= prev_ins
->next
;
333 prev_ins
->next
= new_inst
;
334 new_inst
->next
->prev
= new_inst
;
340 interp_remove_ins (TransformData
*td
, InterpInst
*ins
)
343 ins
->prev
->next
= ins
->next
;
345 td
->first_ins
= ins
->next
;
348 ins
->next
->prev
= ins
->prev
;
350 td
->last_ins
= ins
->prev
;
354 #define CHECK_STACK(td, n) \
356 int stack_size = (td)->sp - (td)->stack; \
357 if (stack_size < (n)) \
358 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
359 m_class_get_name ((td)->method->klass), (td)->method->name, \
360 stack_size, n, (td)->ip - (td)->il_code); \
363 #define ENSURE_I4(td, sp_off) \
365 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
366 interp_add_ins (td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
369 #define CHECK_TYPELOAD(klass) \
371 if (!(klass) || mono_class_has_failure (klass)) { \
372 mono_error_set_for_class_failure (error, klass); \
377 #if NO_UNALIGNED_ACCESS
378 #define WRITE32(ip, v) \
380 * (ip) = * (guint16 *)(v); \
381 * ((ip) + 1) = * ((guint16 *)(v) + 1); \
385 #define WRITE32_INS(ins, index, v) \
387 (ins)->data [index] = * (guint16 *)(v); \
388 (ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
391 #define WRITE64_INS(ins, index, v) \
393 (ins)->data [index] = * (guint16 *)(v); \
394 (ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
395 (ins)->data [index + 2] = * ((guint16 *)(v) + 2); \
396 (ins)->data [index + 3] = * ((guint16 *)(v) + 3); \
399 #define WRITE32(ip, v) \
401 * (guint32*)(ip) = * (guint32 *)(v); \
404 #define WRITE32_INS(ins, index, v) \
406 * (guint32 *)(&(ins)->data [index]) = * (guint32 *)(v); \
409 #define WRITE64_INS(ins, index, v) \
411 * (guint64 *)(&(ins)->data [index]) = * (guint64 *)(v); \
418 handle_branch (TransformData
*td
, int short_op
, int long_op
, int offset
)
420 int shorten_branch
= 0;
421 int target
= td
->ip
+ offset
- td
->il_code
;
422 if (target
< 0 || target
>= td
->code_size
)
423 g_assert_not_reached ();
424 /* Add exception checkpoint or safepoint for backward branches */
426 if (mono_threads_are_safepoints_enabled ())
427 interp_add_ins (td
, MINT_SAFEPOINT
);
429 interp_add_ins (td
, MINT_CHECKPOINT
);
431 if (offset
> 0 && td
->stack_height
[target
] < 0) {
432 td
->stack_height
[target
] = td
->sp
- td
->stack
;
433 if (td
->stack_height
[target
] > 0)
434 td
->stack_state
[target
] = (StackInfo
*)g_memdup (td
->stack
, td
->stack_height
[target
] * sizeof (td
->stack
[0]));
435 td
->vt_stack_size
[target
] = td
->vt_sp
;
438 if (td
->header
->code_size
<= 25000) /* FIX to be precise somehow? */
441 if (shorten_branch
) {
442 interp_add_ins (td
, short_op
);
443 td
->last_ins
->data
[0] = (guint16
) target
;
445 interp_add_ins (td
, long_op
);
446 WRITE32_INS (td
->last_ins
, 0, &target
);
451 one_arg_branch(TransformData
*td
, int mint_op
, int offset
)
453 int type
= td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-1].type
;
454 int long_op
= mint_op
+ type
- STACK_TYPE_I4
;
455 int short_op
= long_op
+ MINT_BRFALSE_I4_S
- MINT_BRFALSE_I4
;
458 handle_branch (td
, short_op
, long_op
, offset
);
462 two_arg_branch(TransformData
*td
, int mint_op
, int offset
)
464 int type1
= td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-1].type
;
465 int type2
= td
->sp
[-2].type
== STACK_TYPE_O
|| td
->sp
[-2].type
== STACK_TYPE_MP
? STACK_TYPE_I
: td
->sp
[-2].type
;
466 int long_op
= mint_op
+ type1
- STACK_TYPE_I4
;
467 int short_op
= long_op
+ MINT_BEQ_I4_S
- MINT_BEQ_I4
;
469 if (type1
== STACK_TYPE_I4
&& type2
== STACK_TYPE_I8
) {
470 interp_add_ins (td
, MINT_CONV_I8_I4
);
471 // The il instruction starts with the actual branch, and not with the conversion opcodes
472 td
->last_ins
->il_offset
= -1;
473 } else if (type1
== STACK_TYPE_I8
&& type2
== STACK_TYPE_I4
) {
474 interp_add_ins (td
, MINT_CONV_I8_I4_SP
);
475 td
->last_ins
->il_offset
= -1;
476 } else if (type1
== STACK_TYPE_R4
&& type2
== STACK_TYPE_R8
) {
477 interp_add_ins (td
, MINT_CONV_R8_R4
);
478 td
->last_ins
->il_offset
= -1;
479 } else if (type1
== STACK_TYPE_R8
&& type2
== STACK_TYPE_R4
) {
480 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
481 td
->last_ins
->il_offset
= -1;
482 } else if (type1
!= type2
) {
483 g_warning("%s.%s: branch type mismatch %d %d",
484 m_class_get_name (td
->method
->klass
), td
->method
->name
,
485 td
->sp
[-1].type
, td
->sp
[-2].type
);
488 handle_branch (td
, short_op
, long_op
, offset
);
492 unary_arith_op(TransformData
*td
, int mint_op
)
494 int op
= mint_op
+ td
->sp
[-1].type
- STACK_TYPE_I4
;
496 interp_add_ins (td
, op
);
500 binary_arith_op(TransformData
*td
, int mint_op
)
502 int type1
= td
->sp
[-2].type
;
503 int type2
= td
->sp
[-1].type
;
505 #if SIZEOF_VOID_P == 8
506 if ((type1
== STACK_TYPE_MP
|| type1
== STACK_TYPE_I8
) && type2
== STACK_TYPE_I4
) {
507 interp_add_ins (td
, MINT_CONV_I8_I4
);
508 type2
= STACK_TYPE_I8
;
510 if (type1
== STACK_TYPE_I4
&& (type2
== STACK_TYPE_MP
|| type2
== STACK_TYPE_I8
)) {
511 interp_add_ins (td
, MINT_CONV_I8_I4_SP
);
512 type1
= STACK_TYPE_I8
;
513 td
->sp
[-2].type
= STACK_TYPE_I8
;
516 if (type1
== STACK_TYPE_R8
&& type2
== STACK_TYPE_R4
) {
517 interp_add_ins (td
, MINT_CONV_R8_R4
);
518 type2
= STACK_TYPE_R8
;
520 if (type1
== STACK_TYPE_R4
&& type2
== STACK_TYPE_R8
) {
521 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
522 type1
= STACK_TYPE_R8
;
523 td
->sp
[-2].type
= STACK_TYPE_R8
;
525 if (type1
== STACK_TYPE_MP
)
526 type1
= STACK_TYPE_I
;
527 if (type2
== STACK_TYPE_MP
)
528 type2
= STACK_TYPE_I
;
529 if (type1
!= type2
) {
530 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
531 m_class_get_name (td
->method
->klass
), td
->method
->name
,
532 td
->ip
- td
->il_code
, mono_interp_opname
[mint_op
], type1
, type2
);
534 op
= mint_op
+ type1
- STACK_TYPE_I4
;
536 interp_add_ins (td
, op
);
541 shift_op(TransformData
*td
, int mint_op
)
543 int op
= mint_op
+ td
->sp
[-2].type
- STACK_TYPE_I4
;
545 if (td
->sp
[-1].type
!= STACK_TYPE_I4
) {
546 g_warning("%s.%s: shift type mismatch %d",
547 m_class_get_name (td
->method
->klass
), td
->method
->name
,
550 interp_add_ins (td
, op
);
555 can_store (int st_value
, int vt_value
)
557 if (st_value
== STACK_TYPE_O
|| st_value
== STACK_TYPE_MP
)
558 st_value
= STACK_TYPE_I
;
559 if (vt_value
== STACK_TYPE_O
|| vt_value
== STACK_TYPE_MP
)
560 vt_value
= STACK_TYPE_I
;
561 return st_value
== vt_value
;
564 #define SET_SIMPLE_TYPE(s, ty) \
571 #define SET_TYPE(s, ty, k) \
578 #define REALLOC_STACK(td, sppos) \
580 (td)->stack_capacity *= 2; \
581 (td)->stack = (StackInfo*)realloc ((td)->stack, (td)->stack_capacity * sizeof (td->stack [0])); \
582 (td)->sp = (td)->stack + sppos; \
585 #define PUSH_SIMPLE_TYPE(td, ty) \
589 sp_height = (td)->sp - (td)->stack; \
590 if (sp_height > (td)->max_stack_height) \
591 (td)->max_stack_height = sp_height; \
592 if (sp_height > (td)->stack_capacity) \
593 REALLOC_STACK(td, sp_height); \
594 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
597 #define PUSH_TYPE(td, ty, k) \
601 sp_height = (td)->sp - (td)->stack; \
602 if (sp_height > (td)->max_stack_height) \
603 (td)->max_stack_height = sp_height; \
604 if (sp_height > (td)->stack_capacity) \
605 REALLOC_STACK(td, sp_height); \
606 SET_TYPE((td)->sp - 1, ty, k); \
609 #define PUSH_VT(td, size) \
611 (td)->vt_sp += ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
612 if ((td)->vt_sp > (td)->max_vt_sp) \
613 (td)->max_vt_sp = (td)->vt_sp; \
616 #define POP_VT(td, size) \
618 (td)->vt_sp -= ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
622 get_arg_type_exact (TransformData
*td
, int n
, int *mt
)
625 gboolean hasthis
= mono_method_signature_internal (td
->method
)->hasthis
;
627 if (hasthis
&& n
== 0)
628 type
= m_class_get_byval_arg (td
->method
->klass
);
630 type
= mono_method_signature_internal (td
->method
)->params
[n
- !!hasthis
];
633 *mt
= mint_type (type
);
639 load_arg(TransformData
*td
, int n
)
642 MonoClass
*klass
= NULL
;
644 gboolean hasthis
= mono_method_signature_internal (td
->method
)->hasthis
;
646 type
= get_arg_type_exact (td
, n
, &mt
);
648 if (mt
== MINT_TYPE_VT
) {
650 klass
= mono_class_from_mono_type_internal (type
);
651 if (mono_method_signature_internal (td
->method
)->pinvoke
)
652 size
= mono_class_native_size (klass
, NULL
);
654 size
= mono_class_value_size (klass
, NULL
);
656 if (hasthis
&& n
== 0) {
658 interp_add_ins (td
, MINT_LDARG_P
);
659 td
->last_ins
->data
[0] = 0;
663 interp_add_ins (td
, MINT_LDARG_VT
);
664 td
->last_ins
->data
[0] = n
;
665 WRITE32_INS (td
->last_ins
, 1, &size
);
668 if (hasthis
&& n
== 0) {
670 interp_add_ins (td
, MINT_LDARG_P
);
671 td
->last_ins
->data
[0] = n
;
674 interp_add_ins (td
, MINT_LDARG_I1
+ (mt
- MINT_TYPE_I1
));
675 td
->last_ins
->data
[0] = n
;
676 if (mt
== MINT_TYPE_O
)
677 klass
= mono_class_from_mono_type_internal (type
);
680 PUSH_TYPE(td
, stack_type
[mt
], klass
);
684 store_arg(TransformData
*td
, int n
)
690 type
= get_arg_type_exact (td
, n
, &mt
);
692 if (mt
== MINT_TYPE_VT
) {
694 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
695 if (mono_method_signature_internal (td
->method
)->pinvoke
)
696 size
= mono_class_native_size (klass
, NULL
);
698 size
= mono_class_value_size (klass
, NULL
);
699 interp_add_ins (td
, MINT_STARG_VT
);
700 td
->last_ins
->data
[0] = n
;
701 WRITE32_INS (td
->last_ins
, 1, &size
);
702 if (td
->sp
[-1].type
== STACK_TYPE_VT
)
705 interp_add_ins (td
, MINT_STARG_I1
+ (mt
- MINT_TYPE_I1
));
706 td
->last_ins
->data
[0] = n
;
712 load_local_general (TransformData
*td
, int offset
, MonoType
*type
)
714 int mt
= mint_type (type
);
715 MonoClass
*klass
= NULL
;
716 if (mt
== MINT_TYPE_VT
) {
717 klass
= mono_class_from_mono_type_internal (type
);
718 gint32 size
= mono_class_value_size (klass
, NULL
);
720 interp_add_ins (td
, MINT_LDLOC_VT
);
721 td
->last_ins
->data
[0] = offset
;
722 WRITE32_INS (td
->last_ins
, 1, &size
);
724 g_assert (mt
< MINT_TYPE_VT
);
725 if (!td
->gen_sdb_seq_points
&&
726 mt
== MINT_TYPE_I4
&& !td
->is_bb_start
[td
->in_start
- td
->il_code
] && td
->last_ins
!= NULL
&&
727 td
->last_ins
->opcode
== MINT_STLOC_I4
&& td
->last_ins
->data
[0] == offset
) {
728 td
->last_ins
->opcode
= MINT_STLOC_NP_I4
;
729 } else if (!td
->gen_sdb_seq_points
&&
730 mt
== MINT_TYPE_O
&& !td
->is_bb_start
[td
->in_start
- td
->il_code
] && td
->last_ins
!= NULL
&&
731 td
->last_ins
->opcode
== MINT_STLOC_O
&& td
->last_ins
->data
[0] == offset
) {
732 td
->last_ins
->opcode
= MINT_STLOC_NP_O
;
734 interp_add_ins (td
, MINT_LDLOC_I1
+ (mt
- MINT_TYPE_I1
));
735 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
737 if (mt
== MINT_TYPE_O
)
738 klass
= mono_class_from_mono_type_internal (type
);
740 PUSH_TYPE(td
, stack_type
[mt
], klass
);
744 load_local (TransformData
*td
, int n
)
746 MonoType
*type
= td
->header
->locals
[n
];
747 int offset
= td
->rtm
->local_offsets
[n
];
748 load_local_general (td
, offset
, type
);
752 store_local_general (TransformData
*td
, int offset
, MonoType
*type
)
754 int mt
= mint_type (type
);
756 #if SIZEOF_VOID_P == 8
757 if (td
->sp
[-1].type
== STACK_TYPE_I4
&& stack_type
[mt
] == STACK_TYPE_I8
) {
758 interp_add_ins (td
, MINT_CONV_I8_I4
);
759 td
->sp
[-1].type
= STACK_TYPE_I8
;
762 if (!can_store(td
->sp
[-1].type
, stack_type
[mt
])) {
763 g_warning("%s.%s: Store local stack type mismatch %d %d",
764 m_class_get_name (td
->method
->klass
), td
->method
->name
,
765 stack_type
[mt
], td
->sp
[-1].type
);
767 if (mt
== MINT_TYPE_VT
) {
768 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
769 gint32 size
= mono_class_value_size (klass
, NULL
);
770 interp_add_ins (td
, MINT_STLOC_VT
);
771 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
772 WRITE32_INS (td
->last_ins
, 1, &size
);
773 if (td
->sp
[-1].type
== STACK_TYPE_VT
)
776 g_assert (mt
< MINT_TYPE_VT
);
777 interp_add_ins (td
, MINT_STLOC_I1
+ (mt
- MINT_TYPE_I1
));
778 td
->last_ins
->data
[0] = offset
; /*FIX for large offset */
784 store_local (TransformData
*td
, int n
)
786 MonoType
*type
= td
->header
->locals
[n
];
787 int offset
= td
->rtm
->local_offsets
[n
];
788 store_local_general (td
, offset
, type
);
791 #define SIMPLE_OP(td, op) \
793 interp_add_ins (td, op); \
798 get_data_item_index (TransformData
*td
, void *ptr
)
800 gpointer p
= g_hash_table_lookup (td
->data_hash
, ptr
);
803 return GPOINTER_TO_UINT (p
) - 1;
804 if (td
->max_data_items
== td
->n_data_items
) {
805 td
->max_data_items
= td
->n_data_items
== 0 ? 16 : 2 * td
->max_data_items
;
806 td
->data_items
= (gpointer
*)g_realloc (td
->data_items
, td
->max_data_items
* sizeof(td
->data_items
[0]));
808 index
= td
->n_data_items
;
809 td
->data_items
[index
] = ptr
;
811 g_hash_table_insert (td
->data_hash
, ptr
, GUINT_TO_POINTER (index
+ 1));
816 jit_call_supported (MonoMethod
*method
, MonoMethodSignature
*sig
)
820 if (sig
->param_count
> 6)
824 if (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)
826 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
828 if (method
->is_inflated
)
830 if (method
->string_ctor
)
833 if (mono_aot_only
&& m_class_get_image (method
->klass
)->aot_module
&& !(method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)) {
835 gpointer addr
= mono_jit_compile_method_jit_only (method
, error
);
836 if (addr
&& mono_error_ok (error
))
840 for (l
= mono_interp_jit_classes
; l
; l
= l
->next
) {
841 const char *class_name
= (const char*)l
->data
;
843 if (!strcmp (m_class_get_name (method
->klass
), class_name
))
851 static int mono_class_get_magic_index (MonoClass
*k
)
853 if (mono_class_is_magic_int (k
))
854 return !strcmp ("nint", m_class_get_name (k
)) ? 0 : 1;
856 if (mono_class_is_magic_float (k
))
863 interp_generate_mae_throw (TransformData
*td
, MonoMethod
*method
, MonoMethod
*target_method
)
865 MonoJitICallInfo
*info
= mono_find_jit_icall_by_name ("mono_throw_method_access");
867 /* Inject code throwing MethodAccessException */
868 interp_add_ins (td
, MINT_MONO_LDPTR
);
869 td
->last_ins
->data
[0] = get_data_item_index (td
, method
);
870 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
872 interp_add_ins (td
, MINT_MONO_LDPTR
);
873 td
->last_ins
->data
[0] = get_data_item_index (td
, target_method
);
874 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
876 interp_add_ins (td
, MINT_ICALL_PP_V
);
877 td
->last_ins
->data
[0] = get_data_item_index (td
, (gpointer
)info
->func
);
883 * These are additional locals that can be allocated as we transform the code.
884 * They are allocated past the method locals so they are accessed in the same
885 * way, with an offset relative to the frame->locals.
888 create_interp_local (TransformData
*td
, MonoType
*type
)
891 int offset
= td
->total_locals_size
;
893 size
= mono_type_size (type
, &align
);
894 offset
= ALIGN_TO (offset
, align
);
896 td
->total_locals_size
= offset
+ size
;
902 dump_mint_code (const guint16
*start
, const guint16
* end
)
904 const guint16
*p
= start
;
906 char *ins
= mono_interp_dis_mintop (start
, p
);
907 g_print ("%s\n", ins
);
909 p
= mono_interp_dis_mintop_len (p
);
915 mono_interp_print_code (InterpMethod
*imethod
)
917 MonoJitInfo
*jinfo
= imethod
->jinfo
;
918 const guint16
*start
;
923 char *name
= mono_method_full_name (imethod
->method
, 1);
924 g_print ("Method : %s\n", name
);
927 start
= (guint16
*) jinfo
->code_start
;
928 dump_mint_code (start
, start
+ jinfo
->code_size
);
932 static MonoMethodHeader
*
933 interp_method_get_header (MonoMethod
* method
, MonoError
*error
)
935 /* An explanation: mono_method_get_header_internal returns an error if
936 * called on a method with no body (e.g. an abstract method, or an
937 * icall). We don't want that.
939 if (mono_method_has_no_body (method
))
942 return mono_method_get_header_internal (method
, error
);
945 /* stores top of stack as local and pushes address of it on stack */
947 emit_store_value_as_local (TransformData
*td
, MonoType
*src
)
949 int size
= mini_magic_type_size (NULL
, src
);
950 int local_offset
= create_interp_local (td
, mini_native_type_replace_type (src
));
952 store_local_general (td
, local_offset
, src
);
954 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
955 interp_add_ins (td
, MINT_LDLOC_VT
);
956 td
->last_ins
->data
[0] = local_offset
;
957 WRITE32_INS (td
->last_ins
, 1, &size
);
960 PUSH_TYPE (td
, STACK_TYPE_VT
, NULL
);
963 // Returns whether we can optimize away the instructions starting at start.
964 // If any instructions are part of a new basic block, we can't remove them.
966 interp_is_bb_start (TransformData
*td
, InterpInst
*start
, InterpInst
*end
)
968 InterpInst
*ins
= start
;
970 if (ins
->il_offset
!= -1) {
971 if (td
->is_bb_start
[ins
->il_offset
])
980 interp_ins_is_ldc (InterpInst
*ins
)
982 return ins
->opcode
>= MINT_LDC_I4_M1
&& ins
->opcode
<= MINT_LDC_I8
;
986 interp_ldc_i4_get_const (InterpInst
*ins
)
988 switch (ins
->opcode
) {
989 case MINT_LDC_I4_M1
: return -1;
990 case MINT_LDC_I4_0
: return 0;
991 case MINT_LDC_I4_1
: return 1;
992 case MINT_LDC_I4_2
: return 2;
993 case MINT_LDC_I4_3
: return 3;
994 case MINT_LDC_I4_4
: return 4;
995 case MINT_LDC_I4_5
: return 5;
996 case MINT_LDC_I4_6
: return 6;
997 case MINT_LDC_I4_7
: return 7;
998 case MINT_LDC_I4_8
: return 8;
999 case MINT_LDC_I4_S
: return (gint32
)(gint8
)ins
->data
[0];
1000 case MINT_LDC_I4
: return READ32 (&ins
->data
[0]);
1002 g_assert_not_reached ();
1007 interp_get_ldind_for_mt (int mt
)
1010 case MINT_TYPE_I1
: return MINT_LDIND_I1_CHECK
;
1011 case MINT_TYPE_U1
: return MINT_LDIND_U1_CHECK
;
1012 case MINT_TYPE_I2
: return MINT_LDIND_I2_CHECK
;
1013 case MINT_TYPE_U2
: return MINT_LDIND_U2_CHECK
;
1014 case MINT_TYPE_I4
: return MINT_LDIND_I4_CHECK
;
1015 case MINT_TYPE_I8
: return MINT_LDIND_I8_CHECK
;
1016 case MINT_TYPE_R4
: return MINT_LDIND_R4_CHECK
;
1017 case MINT_TYPE_R8
: return MINT_LDIND_R8_CHECK
;
1018 case MINT_TYPE_O
: return MINT_LDIND_REF
;
1020 g_assert_not_reached ();
1025 /* Return TRUE if call transformation is finished */
1027 interp_handle_intrinsics (TransformData
*td
, MonoMethod
*target_method
, MonoClass
*constrained_class
, MonoMethodSignature
*csignature
, gboolean readonly
, int *op
)
1029 const char *tm
= target_method
->name
;
1031 int type_index
= mono_class_get_magic_index (target_method
->klass
);
1032 gboolean in_corlib
= m_class_get_image (target_method
->klass
) == mono_defaults
.corlib
;
1033 const char *klass_name_space
= m_class_get_name_space (target_method
->klass
);
1034 const char *klass_name
= m_class_get_name (target_method
->klass
);
1036 if (target_method
->klass
== mono_defaults
.string_class
) {
1037 if (tm
[0] == 'g') {
1038 if (strcmp (tm
, "get_Chars") == 0)
1040 else if (strcmp (tm
, "get_Length") == 0)
1043 } else if (type_index
>= 0) {
1044 MonoClass
*magic_class
= target_method
->klass
;
1046 const int mt
= mint_type (m_class_get_byval_arg (magic_class
));
1047 if (!strcmp (".ctor", tm
)) {
1048 MonoType
*arg
= csignature
->params
[0];
1049 /* depending on SIZEOF_VOID_P and the type of the value passed to the .ctor we either have to CONV it, or do nothing */
1050 int arg_size
= mini_magic_type_size (NULL
, arg
);
1052 if (arg_size
> SIZEOF_VOID_P
) { // 8 -> 4
1053 switch (type_index
) {
1055 interp_add_ins (td
, MINT_CONV_I4_I8
);
1058 interp_add_ins (td
, MINT_CONV_R4_R8
);
1063 if (arg_size
< SIZEOF_VOID_P
) { // 4 -> 8
1064 switch (type_index
) {
1066 interp_add_ins (td
, MINT_CONV_I8_I4
);
1069 interp_add_ins (td
, MINT_CONV_I8_U4
);
1072 interp_add_ins (td
, MINT_CONV_R8_R4
);
1077 switch (type_index
) {
1079 #if SIZEOF_VOID_P == 4
1080 interp_add_ins (td
, MINT_STIND_I4
);
1082 interp_add_ins (td
, MINT_STIND_I8
);
1086 #if SIZEOF_VOID_P == 4
1087 interp_add_ins (td
, MINT_STIND_R4
);
1089 interp_add_ins (td
, MINT_STIND_R8
);
1097 } else if (!strcmp ("op_Implicit", tm
) || !strcmp ("op_Explicit", tm
)) {
1098 MonoType
*src
= csignature
->params
[0];
1099 MonoType
*dst
= csignature
->ret
;
1100 MonoClass
*src_klass
= mono_class_from_mono_type_internal (src
);
1101 int src_size
= mini_magic_type_size (NULL
, src
);
1102 int dst_size
= mini_magic_type_size (NULL
, dst
);
1104 gboolean store_value_as_local
= FALSE
;
1106 switch (type_index
) {
1108 if (!mini_magic_is_int_type (src
) || !mini_magic_is_int_type (dst
)) {
1109 if (mini_magic_is_int_type (src
))
1110 store_value_as_local
= TRUE
;
1111 else if (mono_class_is_magic_float (src_klass
))
1112 store_value_as_local
= TRUE
;
1118 if (!mini_magic_is_float_type (src
) || !mini_magic_is_float_type (dst
)) {
1119 if (mini_magic_is_float_type (src
))
1120 store_value_as_local
= TRUE
;
1121 else if (mono_class_is_magic_int (src_klass
))
1122 store_value_as_local
= TRUE
;
1129 if (store_value_as_local
) {
1130 emit_store_value_as_local (td
, src
);
1132 /* emit call to managed conversion method */
1136 #if SIZEOF_VOID_P == 4
1137 if (src_size
> dst_size
) { // 8 -> 4
1138 switch (type_index
) {
1140 interp_add_ins (td
, MINT_CONV_I4_I8
);
1143 interp_add_ins (td
, MINT_CONV_R4_R8
);
1149 #if SIZEOF_VOID_P == 8
1150 if (src_size
< dst_size
) { // 4 -> 8
1151 switch (type_index
) {
1153 interp_add_ins (td
, MINT_CONV_I8_I4
);
1156 interp_add_ins (td
, MINT_CONV_I8_U4
);
1159 interp_add_ins (td
, MINT_CONV_R8_R4
);
1165 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1168 } else if (!strcmp ("op_Increment", tm
)) {
1169 g_assert (type_index
!= 2); // no nfloat
1170 #if SIZEOF_VOID_P == 8
1171 interp_add_ins (td
, MINT_ADD1_I8
);
1173 interp_add_ins (td
, MINT_ADD1_I4
);
1175 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1178 } else if (!strcmp ("op_Decrement", tm
)) {
1179 g_assert (type_index
!= 2); // no nfloat
1180 #if SIZEOF_VOID_P == 8
1181 interp_add_ins (td
, MINT_SUB1_I8
);
1183 interp_add_ins (td
, MINT_SUB1_I4
);
1185 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1188 } else if (!strcmp ("CompareTo", tm
) || !strcmp ("Equals", tm
)) {
1189 MonoType
*arg
= csignature
->params
[0];
1191 /* on 'System.n*::{CompareTo,Equals} (System.n*)' variant we need to push managed
1192 * pointer instead of value */
1193 if (arg
->type
== MONO_TYPE_VALUETYPE
)
1194 emit_store_value_as_local (td
, arg
);
1196 /* emit call to managed conversion method */
1198 } else if (!strcmp (".cctor", tm
)) {
1201 } else if (!strcmp ("Parse", tm
)) {
1204 } else if (!strcmp ("ToString", tm
)) {
1207 } else if (!strcmp ("GetHashCode", tm
)) {
1210 } else if (!strcmp ("IsNaN", tm
) || !strcmp ("IsInfinity", tm
) || !strcmp ("IsNegativeInfinity", tm
) || !strcmp ("IsPositiveInfinity", tm
)) {
1211 g_assert (type_index
== 2); // nfloat only
1216 for (i
= 0; i
< sizeof (int_unnop
) / sizeof (MagicIntrinsic
); ++i
) {
1217 if (!strcmp (int_unnop
[i
].op_name
, tm
)) {
1218 interp_add_ins (td
, int_unnop
[i
].insn
[type_index
]);
1219 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1225 for (i
= 0; i
< sizeof (int_binop
) / sizeof (MagicIntrinsic
); ++i
) {
1226 if (!strcmp (int_binop
[i
].op_name
, tm
)) {
1227 interp_add_ins (td
, int_binop
[i
].insn
[type_index
]);
1229 SET_TYPE (td
->sp
- 1, stack_type
[mt
], magic_class
);
1235 for (i
= 0; i
< sizeof (int_cmpop
) / sizeof (MagicIntrinsic
); ++i
) {
1236 if (!strcmp (int_cmpop
[i
].op_name
, tm
)) {
1237 MonoClass
*k
= mono_defaults
.boolean_class
;
1238 interp_add_ins (td
, int_cmpop
[i
].insn
[type_index
]);
1240 SET_TYPE (td
->sp
- 1, stack_type
[mint_type (m_class_get_byval_arg (k
))], k
);
1246 g_error ("TODO: interp_transform_call %s:%s", m_class_get_name (target_method
->klass
), tm
);
1247 } else if (mono_class_is_subclass_of (target_method
->klass
, mono_defaults
.array_class
, FALSE
)) {
1248 if (!strcmp (tm
, "get_Rank")) {
1249 *op
= MINT_ARRAY_RANK
;
1250 } else if (!strcmp (tm
, "get_Length")) {
1252 } else if (!strcmp (tm
, "Address")) {
1253 *op
= readonly
? MINT_LDELEMA
: MINT_LDELEMA_TC
;
1254 } else if (!strcmp (tm
, "UnsafeMov") || !strcmp (tm
, "UnsafeLoad") || !strcmp (tm
, "Set") || !strcmp (tm
, "Get")) {
1256 } else if (!strcmp (tm
, "UnsafeStore")) {
1257 g_error ("TODO ArrayClass::UnsafeStore");
1259 } else if (in_corlib
&&
1260 !strcmp (klass_name_space
, "System.Diagnostics") &&
1261 !strcmp (klass_name
, "Debugger")) {
1262 if (!strcmp (tm
, "Break") && csignature
->param_count
== 0) {
1263 if (mini_should_insert_breakpoint (td
->method
))
1266 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "ByReference`1")) {
1267 *op
= MINT_INTRINS_BYREFERENCE_GET_VALUE
;
1268 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "Math") && csignature
->param_count
== 1 && csignature
->params
[0]->type
== MONO_TYPE_R8
) {
1269 if (tm
[0] == 'A') {
1270 if (strcmp (tm
, "Abs") == 0 && csignature
->params
[0]->type
== MONO_TYPE_R8
) {
1272 } else if (strcmp (tm
, "Asin") == 0){
1274 } else if (strcmp (tm
, "Asinh") == 0){
1276 } else if (strcmp (tm
, "Acos") == 0){
1278 } else if (strcmp (tm
, "Acosh") == 0){
1280 } else if (strcmp (tm
, "Atan") == 0){
1282 } else if (strcmp (tm
, "Atanh") == 0){
1285 } else if (tm
[0] == 'C') {
1286 if (strcmp (tm
, "Cos") == 0) {
1288 } else if (strcmp (tm
, "Cbrt") == 0){
1290 } else if (strcmp (tm
, "Cosh") == 0){
1293 } else if (tm
[0] == 'S') {
1294 if (strcmp (tm
, "Sin") == 0) {
1296 } else if (strcmp (tm
, "Sqrt") == 0) {
1298 } else if (strcmp (tm
, "Sinh") == 0){
1301 } else if (tm
[0] == 'T') {
1302 if (strcmp (tm
, "Tan") == 0) {
1304 } else if (strcmp (tm
, "Tanh") == 0){
1308 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && (!strcmp (klass_name
, "Span`1") || !strcmp (klass_name
, "ReadOnlySpan`1"))) {
1309 if (!strcmp (tm
, "get_Item") && csignature
->params
[0]->type
!= MONO_TYPE_VALUETYPE
) {
1310 MonoGenericClass
*gclass
= mono_class_get_generic_class (target_method
->klass
);
1311 MonoClass
*param_class
= mono_class_from_mono_type_internal (gclass
->context
.class_inst
->type_argv
[0]);
1313 if (!mini_is_gsharedvt_variable_klass (param_class
)) {
1314 MonoClassField
*length_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_length", NULL
);
1315 g_assert (length_field
);
1316 int offset_length
= length_field
->offset
- sizeof (MonoObject
);
1318 MonoClassField
*ptr_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_pointer", NULL
);
1319 g_assert (ptr_field
);
1320 int offset_pointer
= ptr_field
->offset
- sizeof (MonoObject
);
1322 int size
= mono_class_array_element_size (param_class
);
1323 interp_add_ins (td
, MINT_GETITEM_SPAN
);
1324 td
->last_ins
->data
[0] = size
;
1325 td
->last_ins
->data
[1] = offset_length
;
1326 td
->last_ins
->data
[2] = offset_pointer
;
1328 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
1333 } else if (!strcmp (tm
, "get_Length")) {
1334 MonoClassField
*length_field
= mono_class_get_field_from_name_full (target_method
->klass
, "_length", NULL
);
1335 g_assert (length_field
);
1336 int offset_length
= length_field
->offset
- sizeof (MonoObject
);
1337 interp_add_ins (td
, MINT_LDLEN_SPAN
);
1338 td
->last_ins
->data
[0] = offset_length
;
1339 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
1343 } else if (in_corlib
&& !strcmp (klass_name_space
, "Internal.Runtime.CompilerServices") && !strcmp (klass_name
, "Unsafe")) {
1344 #ifdef ENABLE_NETCORE
1345 if (!strcmp (tm
, "AddByteOffset"))
1346 *op
= MINT_INTRINS_UNSAFE_ADD_BYTE_OFFSET
;
1347 else if (!strcmp (tm
, "ByteOffset"))
1348 *op
= MINT_INTRINS_UNSAFE_BYTE_OFFSET
;
1349 else if (!strcmp (tm
, "As") || !strcmp (tm
, "AsRef"))
1351 else if (!strcmp (tm
, "SizeOf")) {
1352 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1354 g_assert (ctx
->method_inst
);
1355 g_assert (ctx
->method_inst
->type_argc
== 1);
1356 MonoType
*t
= ctx
->method_inst
->type_argv
[0];
1358 int esize
= mono_type_size (t
, &align
);
1359 interp_add_ins (td
, MINT_LDC_I4
);
1360 WRITE32_INS (td
->last_ins
, 0, &esize
);
1361 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
1364 } else if (!strcmp (tm
, "AreSame")) {
1368 } else if (in_corlib
&& !strcmp (klass_name_space
, "System.Runtime.CompilerServices") && !strcmp (klass_name
, "RuntimeHelpers")) {
1369 #ifdef ENABLE_NETCORE
1370 if (!strcmp (tm
, "IsBitwiseEquatable")) {
1371 g_assert (csignature
->param_count
== 0);
1372 MonoGenericContext
*ctx
= mono_method_get_context (target_method
);
1374 g_assert (ctx
->method_inst
);
1375 g_assert (ctx
->method_inst
->type_argc
== 1);
1376 MonoType
*t
= mini_get_underlying_type (ctx
->method_inst
->type_argv
[0]);
1378 if (MONO_TYPE_IS_PRIMITIVE (t
) && t
->type
!= MONO_TYPE_R4
&& t
->type
!= MONO_TYPE_R8
)
1379 *op
= MINT_LDC_I4_1
;
1381 *op
= MINT_LDC_I4_0
;
1382 } else if (!strcmp (tm
, "ObjectHasComponentSize")) {
1383 *op
= MINT_INTRINS_RUNTIMEHELPERS_OBJECT_HAS_COMPONENT_SIZE
;
1386 } else if (in_corlib
&& !strcmp (klass_name_space
, "System") && !strcmp (klass_name
, "RuntimeMethodHandle") && !strcmp (tm
, "GetFunctionPointer") && csignature
->param_count
== 1) {
1387 // We must intrinsify this method on interp so we don't return a pointer to native code entering interpreter
1388 *op
= MINT_LDFTN_DYNAMIC
;
1389 } else if (target_method
->klass
== mono_defaults
.object_class
) {
1390 if (!strcmp (tm
, "InternalGetHashCode"))
1391 *op
= MINT_INTRINS_GET_HASHCODE
;
1392 #ifdef DISABLE_REMOTING
1393 else if (!strcmp (tm
, "GetType"))
1394 *op
= MINT_INTRINS_GET_TYPE
;
1396 } else if (in_corlib
&& target_method
->klass
== mono_defaults
.enum_class
&& !strcmp (tm
, "HasFlag")) {
1397 gboolean intrinsify
= FALSE
;
1398 MonoClass
*base_klass
= NULL
;
1399 if (td
->last_ins
&& td
->last_ins
->opcode
== MINT_BOX
&&
1400 td
->last_ins
->prev
&& interp_ins_is_ldc (td
->last_ins
->prev
) &&
1401 td
->last_ins
->prev
->prev
&& td
->last_ins
->prev
->prev
->opcode
== MINT_BOX
&&
1402 td
->sp
[-2].klass
== td
->sp
[-1].klass
&&
1403 !interp_is_bb_start (td
, td
->last_ins
->prev
->prev
, NULL
) &&
1404 !td
->is_bb_start
[td
->in_start
- td
->il_code
]) {
1405 // csc pattern : box, ldc, box, call HasFlag
1406 g_assert (m_class_is_enumtype (td
->sp
[-2].klass
));
1407 MonoType
*base_type
= mono_type_get_underlying_type (m_class_get_byval_arg (td
->sp
[-2].klass
));
1408 base_klass
= mono_class_from_mono_type_internal (base_type
);
1410 // Remove the boxing of valuetypes
1411 interp_remove_ins (td
, td
->last_ins
->prev
->prev
);
1412 interp_remove_ins (td
, td
->last_ins
);
1415 } else if (td
->last_ins
&& td
->last_ins
->opcode
== MINT_BOX
&&
1416 td
->last_ins
->prev
&& interp_ins_is_ldc (td
->last_ins
->prev
) &&
1417 constrained_class
&& td
->sp
[-1].klass
== constrained_class
&&
1418 !interp_is_bb_start (td
, td
->last_ins
->prev
, NULL
) &&
1419 !td
->is_bb_start
[td
->in_start
- td
->il_code
]) {
1420 // mcs pattern : ldc, box, constrained Enum, call HasFlag
1421 g_assert (m_class_is_enumtype (constrained_class
));
1422 MonoType
*base_type
= mono_type_get_underlying_type (m_class_get_byval_arg (constrained_class
));
1423 base_klass
= mono_class_from_mono_type_internal (base_type
);
1424 int mt
= mint_type (m_class_get_byval_arg (base_klass
));
1426 // Remove boxing and load the value of this
1427 interp_remove_ins (td
, td
->last_ins
);
1428 interp_insert_ins (td
, td
->last_ins
->prev
, interp_get_ldind_for_mt (mt
));
1433 interp_add_ins (td
, MINT_INTRINS_ENUM_HASFLAG
);
1434 td
->last_ins
->data
[0] = get_data_item_index (td
, base_klass
);
1436 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
1446 interp_transform_internal_calls (MonoMethod
*method
, MonoMethod
*target_method
, MonoMethodSignature
*csignature
, gboolean is_virtual
)
1448 if (method
->wrapper_type
== MONO_WRAPPER_NONE
&& target_method
!= NULL
) {
1449 if (target_method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)
1450 target_method
= mono_marshal_get_native_wrapper (target_method
, FALSE
, FALSE
);
1451 if (!is_virtual
&& target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)
1452 target_method
= mono_marshal_get_synchronized_wrapper (target_method
);
1454 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)
1455 target_method
= mono_marshal_get_native_wrapper (target_method
, FALSE
, FALSE
);
1457 return target_method
;
1461 interp_type_as_ptr (MonoType
*tp
)
1463 if (MONO_TYPE_IS_POINTER (tp
))
1465 if (MONO_TYPE_IS_REFERENCE (tp
))
1467 if ((tp
)->type
== MONO_TYPE_I4
)
1469 #if SIZEOF_VOID_P == 8
1470 if ((tp
)->type
== MONO_TYPE_I8
)
1473 if ((tp
)->type
== MONO_TYPE_BOOLEAN
)
1475 if ((tp
)->type
== MONO_TYPE_CHAR
)
1477 if ((tp
)->type
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (tp
->data
.klass
))
1482 #define INTERP_TYPE_AS_PTR(tp) interp_type_as_ptr (tp)
1485 interp_icall_op_for_sig (MonoMethodSignature
*sig
)
1488 switch (sig
->param_count
) {
1490 if (MONO_TYPE_IS_VOID (sig
->ret
))
1491 op
= MINT_ICALL_V_V
;
1492 else if (INTERP_TYPE_AS_PTR (sig
->ret
))
1493 op
= MINT_ICALL_V_P
;
1496 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1497 if (INTERP_TYPE_AS_PTR (sig
->params
[0]))
1498 op
= MINT_ICALL_P_V
;
1499 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1500 if (INTERP_TYPE_AS_PTR (sig
->params
[0]))
1501 op
= MINT_ICALL_P_P
;
1505 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1506 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1507 INTERP_TYPE_AS_PTR (sig
->params
[1]))
1508 op
= MINT_ICALL_PP_V
;
1509 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1510 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1511 INTERP_TYPE_AS_PTR (sig
->params
[1]))
1512 op
= MINT_ICALL_PP_P
;
1516 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1517 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1518 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1519 INTERP_TYPE_AS_PTR (sig
->params
[2]))
1520 op
= MINT_ICALL_PPP_V
;
1521 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1522 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1523 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1524 INTERP_TYPE_AS_PTR (sig
->params
[2]))
1525 op
= MINT_ICALL_PPP_P
;
1529 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1530 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1531 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1532 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1533 INTERP_TYPE_AS_PTR (sig
->params
[3]))
1534 op
= MINT_ICALL_PPPP_V
;
1535 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1536 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1537 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1538 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1539 INTERP_TYPE_AS_PTR (sig
->params
[3]))
1540 op
= MINT_ICALL_PPPP_P
;
1544 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1545 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1546 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1547 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1548 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1549 INTERP_TYPE_AS_PTR (sig
->params
[4]))
1550 op
= MINT_ICALL_PPPPP_V
;
1551 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1552 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1553 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1554 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1555 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1556 INTERP_TYPE_AS_PTR (sig
->params
[4]))
1557 op
= MINT_ICALL_PPPPP_P
;
1561 if (MONO_TYPE_IS_VOID (sig
->ret
)) {
1562 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1563 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1564 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1565 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1566 INTERP_TYPE_AS_PTR (sig
->params
[4]) &&
1567 INTERP_TYPE_AS_PTR (sig
->params
[5]))
1568 op
= MINT_ICALL_PPPPPP_V
;
1569 } else if (INTERP_TYPE_AS_PTR (sig
->ret
)) {
1570 if (INTERP_TYPE_AS_PTR (sig
->params
[0]) &&
1571 INTERP_TYPE_AS_PTR (sig
->params
[1]) &&
1572 INTERP_TYPE_AS_PTR (sig
->params
[2]) &&
1573 INTERP_TYPE_AS_PTR (sig
->params
[3]) &&
1574 INTERP_TYPE_AS_PTR (sig
->params
[4]) &&
1575 INTERP_TYPE_AS_PTR (sig
->params
[5]))
1576 op
= MINT_ICALL_PPPPPP_P
;
1583 #define INLINE_LENGTH_LIMIT 20
1586 interp_method_check_inlining (TransformData
*td
, MonoMethod
*method
)
1588 MonoMethodHeaderSummary header
;
1590 if (td
->method
== method
)
1593 if (!mono_method_get_header_summary (method
, &header
))
1596 /*runtime, icall and pinvoke are checked by summary call*/
1597 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_NOINLINING
) ||
1598 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) ||
1599 (mono_class_is_marshalbyref (method
->klass
)) ||
1600 header
.has_clauses
||
1604 if (header
.code_size
>= INLINE_LENGTH_LIMIT
)
1607 if (mono_class_needs_cctor_run (method
->klass
, NULL
)) {
1610 if (!m_class_get_runtime_info (method
->klass
))
1611 /* No vtable created yet */
1613 vtable
= mono_class_vtable_checked (td
->rtm
->domain
, method
->klass
, error
);
1614 if (!is_ok (error
)) {
1615 mono_error_cleanup (error
);
1618 if (!vtable
->initialized
)
1622 /* We currently access at runtime the wrapper data */
1623 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1630 interp_inline_method (TransformData
*td
, MonoMethod
*target_method
, MonoMethodHeader
*header
, MonoError
*error
)
1632 const unsigned char *prev_ip
, *prev_il_code
, *prev_in_start
;
1633 StackInfo
**prev_stack_state
;
1634 int *prev_stack_height
, *prev_vt_stack_size
, *prev_clause_indexes
, *prev_in_offsets
;
1635 guint8
*prev_is_bb_start
;
1637 unsigned int prev_max_stack_height
, prev_max_vt_sp
, prev_total_locals_size
;
1638 int prev_n_data_items
;
1641 MonoGenericContext
*generic_context
= NULL
;
1642 StackInfo
*prev_param_area
;
1643 MonoMethod
*prev_inlined_method
;
1644 MonoMethodSignature
*csignature
= mono_method_signature_internal (target_method
);
1645 int nargs
= csignature
->param_count
+ !!csignature
->hasthis
;
1646 InterpInst
*prev_last_ins
;
1648 if (csignature
->is_inflated
)
1649 generic_context
= mono_method_get_context (target_method
);
1651 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (target_method
);
1652 if (generic_container
)
1653 generic_context
= &generic_container
->context
;
1657 prev_il_code
= td
->il_code
;
1658 prev_in_start
= td
->in_start
;
1659 prev_sp_offset
= td
->sp
- td
->stack
;
1660 prev_vt_sp
= td
->vt_sp
;
1661 prev_stack_state
= td
->stack_state
;
1662 prev_stack_height
= td
->stack_height
;
1663 prev_vt_stack_size
= td
->vt_stack_size
;
1664 prev_clause_indexes
= td
->clause_indexes
;
1665 prev_inlined_method
= td
->inlined_method
;
1666 prev_last_ins
= td
->last_ins
;
1667 td
->inlined_method
= target_method
;
1669 prev_max_stack_height
= td
->max_stack_height
;
1670 prev_max_vt_sp
= td
->max_vt_sp
;
1671 prev_total_locals_size
= td
->total_locals_size
;
1673 prev_n_data_items
= td
->n_data_items
;
1674 prev_in_offsets
= td
->in_offsets
;
1675 td
->in_offsets
= (int*)g_malloc0((header
->code_size
+ 1) * sizeof(int));
1677 prev_is_bb_start
= td
->is_bb_start
;
1679 /* Inlining pops the arguments, restore the stack */
1680 prev_param_area
= (StackInfo
*)g_malloc (nargs
* sizeof (StackInfo
));
1681 memcpy (prev_param_area
, &td
->sp
[-nargs
], nargs
* sizeof (StackInfo
));
1683 if (td
->verbose_level
)
1684 g_print ("Inline start method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1685 ret
= generate_code (td
, target_method
, header
, generic_context
, error
);
1688 if (td
->verbose_level
)
1689 g_print ("Inline aborted method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1690 td
->max_stack_height
= prev_max_stack_height
;
1691 td
->max_vt_sp
= prev_max_vt_sp
;
1692 td
->total_locals_size
= prev_total_locals_size
;
1695 /* Remove any newly added items */
1696 for (i
= prev_n_data_items
; i
< td
->n_data_items
; i
++) {
1697 g_hash_table_remove (td
->data_hash
, td
->data_items
[i
]);
1699 td
->n_data_items
= prev_n_data_items
;
1700 td
->sp
= td
->stack
+ prev_sp_offset
;
1701 memcpy (&td
->sp
[-nargs
], prev_param_area
, nargs
* sizeof (StackInfo
));
1702 td
->vt_sp
= prev_vt_sp
;
1703 td
->last_ins
= prev_last_ins
;
1705 td
->last_ins
->next
= NULL
;
1706 UnlockedIncrement (&mono_interp_stats
.inline_failures
);
1708 if (td
->verbose_level
)
1709 g_print ("Inline end method %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1710 UnlockedIncrement (&mono_interp_stats
.inlined_methods
);
1711 // Make sure we have an IR instruction associated with the now removed IL CALL
1712 // FIXME This could be prettier. We might be able to make inlining saner now that
1713 // that we can easily tweak the instruction list.
1714 if (!prev_inlined_method
) {
1715 if (prev_last_ins
) {
1716 if (prev_last_ins
->next
)
1717 prev_last_ins
->next
->il_offset
= prev_in_start
- prev_il_code
;
1718 } else if (td
->first_ins
) {
1719 td
->first_ins
->il_offset
= prev_in_start
- prev_il_code
;
1725 td
->in_start
= prev_in_start
;
1726 td
->il_code
= prev_il_code
;
1727 td
->stack_state
= prev_stack_state
;
1728 td
->stack_height
= prev_stack_height
;
1729 td
->vt_stack_size
= prev_vt_stack_size
;
1730 td
->clause_indexes
= prev_clause_indexes
;
1731 td
->is_bb_start
= prev_is_bb_start
;
1732 td
->inlined_method
= prev_inlined_method
;
1734 g_free (td
->in_offsets
);
1735 td
->in_offsets
= prev_in_offsets
;
1737 g_free (prev_param_area
);
1742 interp_constrained_box (TransformData
*td
, MonoDomain
*domain
, MonoClass
*constrained_class
, MonoMethodSignature
*csignature
, MonoError
*error
)
1744 int mt
= mint_type (m_class_get_byval_arg (constrained_class
));
1745 if (mono_class_is_nullable (constrained_class
)) {
1746 g_assert (mt
== MINT_TYPE_VT
);
1747 interp_add_ins (td
, MINT_BOX_NULLABLE
);
1748 td
->last_ins
->data
[0] = get_data_item_index (td
, constrained_class
);
1749 td
->last_ins
->data
[1] = csignature
->param_count
| ((td
->sp
- 1 - csignature
->param_count
)->type
!= STACK_TYPE_MP
? 0 : BOX_NOT_CLEAR_VT_SP
);
1751 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, constrained_class
, error
);
1752 return_if_nok (error
);
1754 if (mt
== MINT_TYPE_VT
) {
1755 interp_add_ins (td
, MINT_BOX_VT
);
1756 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
1757 td
->last_ins
->data
[1] = csignature
->param_count
| ((td
->sp
- 1 - csignature
->param_count
)->type
!= STACK_TYPE_MP
? 0 : BOX_NOT_CLEAR_VT_SP
);
1759 interp_add_ins (td
, MINT_BOX
);
1760 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
1761 td
->last_ins
->data
[1] = csignature
->param_count
;
1766 /* Return FALSE if error, including inline failure */
1768 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
)
1770 MonoImage
*image
= m_class_get_image (method
->klass
);
1771 MonoMethodSignature
*csignature
;
1772 int is_virtual
= *td
->ip
== CEE_CALLVIRT
;
1773 int calli
= *td
->ip
== CEE_CALLI
|| *td
->ip
== CEE_MONO_CALLI_EXTRA_ARG
;
1775 guint32 vt_stack_used
= 0;
1776 guint32 vt_res_size
= 0;
1780 int need_null_check
= is_virtual
;
1782 guint32 token
= read32 (td
->ip
+ 1);
1784 if (target_method
== NULL
) {
1787 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1788 csignature
= (MonoMethodSignature
*)mono_method_get_wrapper_data (method
, token
);
1790 csignature
= mono_metadata_parse_signature_checked (image
, token
, error
);
1791 return_val_if_nok (error
, FALSE
);
1794 if (generic_context
) {
1795 csignature
= mono_inflate_generic_signature (csignature
, generic_context
, error
);
1796 return_val_if_nok (error
, FALSE
);
1800 * The compiled interp entry wrapper is passed to runtime_invoke instead of
1801 * the InterpMethod pointer. FIXME
1803 native
= csignature
->pinvoke
|| method
->wrapper_type
== MONO_WRAPPER_RUNTIME_INVOKE
;
1806 target_method
= NULL
;
1808 if (method
->wrapper_type
== MONO_WRAPPER_NONE
) {
1809 target_method
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
1810 return_val_if_nok (error
, FALSE
);
1812 target_method
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
1813 csignature
= mono_method_signature_internal (target_method
);
1815 if (generic_context
) {
1816 csignature
= mono_inflate_generic_signature (csignature
, generic_context
, error
);
1817 return_val_if_nok (error
, FALSE
);
1818 target_method
= mono_class_inflate_generic_method_checked (target_method
, generic_context
, error
);
1819 return_val_if_nok (error
, FALSE
);
1823 csignature
= mono_method_signature_internal (target_method
);
1826 if (check_visibility
&& target_method
&& !mono_method_can_access_method (method
, target_method
))
1827 interp_generate_mae_throw (td
, method
, target_method
);
1829 if (target_method
&& target_method
->string_ctor
) {
1830 /* Create the real signature */
1831 MonoMethodSignature
*ctor_sig
= mono_metadata_signature_dup_mempool (td
->mempool
, csignature
);
1832 ctor_sig
->ret
= m_class_get_byval_arg (mono_defaults
.string_class
);
1834 csignature
= ctor_sig
;
1838 if (target_method
&& interp_handle_intrinsics (td
, target_method
, constrained_class
, csignature
, readonly
, &op
))
1841 if (constrained_class
) {
1842 if (m_class_is_enumtype (constrained_class
) && !strcmp (target_method
->name
, "GetHashCode")) {
1843 /* Use the corresponding method from the base type to avoid boxing */
1844 MonoType
*base_type
= mono_class_enum_basetype_internal (constrained_class
);
1845 g_assert (base_type
);
1846 constrained_class
= mono_class_from_mono_type_internal (base_type
);
1847 target_method
= mono_class_get_method_from_name_checked (constrained_class
, target_method
->name
, 0, 0, error
);
1848 mono_error_assert_ok (error
);
1849 g_assert (target_method
);
1853 if (constrained_class
) {
1854 mono_class_setup_vtable (constrained_class
);
1855 if (mono_class_has_failure (constrained_class
)) {
1856 mono_error_set_for_class_failure (error
, constrained_class
);
1860 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
);
1862 target_method
= mono_get_method_constrained_with_method (image
, target_method
, constrained_class
, generic_context
, error
);
1864 g_print (" : %s::%s. %s (%p)\n", target_method
->klass
->name
, target_method
->name
, mono_signature_full_name (target_method
->signature
), target_method
);
1866 /* Intrinsics: Try again, it could be that `mono_get_method_constrained_with_method` resolves to a method that we can substitute */
1867 if (target_method
&& interp_handle_intrinsics (td
, target_method
, constrained_class
, csignature
, readonly
, &op
))
1870 return_val_if_nok (error
, FALSE
);
1871 mono_class_setup_vtable (target_method
->klass
);
1873 if (!m_class_is_valuetype (constrained_class
)) {
1874 /* managed pointer on the stack, we need to deref that puppy */
1875 interp_add_ins (td
, MINT_LDIND_I
);
1876 td
->last_ins
->data
[0] = csignature
->param_count
;
1877 } 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
) {
1878 if (target_method
->klass
== mono_defaults
.enum_class
&& (td
->sp
- csignature
->param_count
- 1)->type
== STACK_TYPE_MP
) {
1879 /* managed pointer on the stack, we need to deref that puppy */
1880 /* Always load the entire stackval, to handle also the case where the enum has long storage */
1881 interp_add_ins (td
, MINT_LDIND_I8
);
1882 td
->last_ins
->data
[0] = csignature
->param_count
;
1885 interp_constrained_box (td
, domain
, constrained_class
, csignature
, error
);
1886 return_val_if_nok (error
, FALSE
);
1888 if (target_method
->klass
!= constrained_class
) {
1890 * The type parameter is instantiated as a valuetype,
1891 * but that type doesn't override the method we're
1892 * calling, so we need to box `this'.
1894 if ((td
->sp
- csignature
->param_count
- 1)->type
== STACK_TYPE_MP
) {
1895 /* managed pointer on the stack, we need to deref that puppy */
1896 /* Always load the entire stackval, to handle also the case where the enum has long storage */
1897 interp_add_ins (td
, MINT_LDIND_I8
);
1898 td
->last_ins
->data
[0] = csignature
->param_count
;
1901 interp_constrained_box (td
, domain
, constrained_class
, csignature
, error
);
1902 return_val_if_nok (error
, FALSE
);
1909 mono_class_init_internal (target_method
->klass
);
1911 if (!is_virtual
&& target_method
&& (target_method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
1912 /* MS.NET seems to silently convert this to a callvirt */
1915 if (is_virtual
&& target_method
&& (!(target_method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) ||
1916 (MONO_METHOD_IS_FINAL (target_method
) &&
1917 target_method
->wrapper_type
!= MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)) &&
1918 !(mono_class_is_marshalbyref (target_method
->klass
))) {
1919 /* Not really virtual, just needs a null check */
1921 need_null_check
= TRUE
;
1924 CHECK_STACK (td
, csignature
->param_count
+ csignature
->hasthis
);
1925 if (!td
->gen_sdb_seq_points
&& !calli
&& op
== -1 && (!is_virtual
|| (target_method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) == 0) &&
1926 (target_method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) == 0 &&
1927 (target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) == 0 &&
1928 !(target_method
->iflags
& METHOD_IMPL_ATTRIBUTE_NOINLINING
)) {
1929 (void)mono_class_vtable_checked (domain
, target_method
->klass
, error
);
1930 return_val_if_nok (error
, FALSE
);
1932 if (method
== target_method
&& *(td
->ip
+ 5) == CEE_RET
&& !(csignature
->hasthis
&& m_class_is_valuetype (target_method
->klass
))) {
1933 if (td
->inlined_method
)
1936 if (td
->verbose_level
)
1937 g_print ("Optimize tail call of %s.%s\n", m_class_get_name (target_method
->klass
), target_method
->name
);
1939 for (i
= csignature
->param_count
- 1 + !!csignature
->hasthis
; i
>= 0; --i
)
1942 interp_add_ins (td
, MINT_BR_S
);
1943 // We are branching to the beginning of the method
1944 td
->last_ins
->data
[0] = 0;
1945 if (!is_bb_start
[td
->ip
+ 5 - td
->il_code
])
1946 ++td
->ip
; /* gobble the CEE_RET if it isn't branched to */
1952 target_method
= interp_transform_internal_calls (method
, target_method
, csignature
, is_virtual
);
1954 if (csignature
->call_convention
== MONO_CALL_VARARG
) {
1955 csignature
= mono_method_get_signature_checked (target_method
, image
, token
, generic_context
, error
);
1956 int vararg_stack
= 0;
1958 * For vararg calls, ArgIterator expects the signature and the varargs to be
1959 * stored in a linear memory. We allocate the necessary vt_stack space for
1960 * this. All varargs will be pushed to the vt_stack at call site.
1962 vararg_stack
+= sizeof (gpointer
);
1963 for (i
= csignature
->sentinelpos
; i
< csignature
->param_count
; ++i
) {
1964 int align
, arg_size
;
1965 arg_size
= mono_type_stack_size (csignature
->params
[i
], &align
);
1966 vararg_stack
+= arg_size
;
1968 vt_stack_used
+= ALIGN_TO (vararg_stack
, MINT_VT_ALIGNMENT
);
1969 PUSH_VT (td
, vararg_stack
);
1972 if (need_null_check
) {
1973 interp_add_ins (td
, MINT_CKNULL_N
);
1974 td
->last_ins
->data
[0] = csignature
->param_count
+ 1;
1977 g_assert (csignature
->call_convention
!= MONO_CALL_FASTCALL
);
1978 if ((mono_interp_opt
& INTERP_OPT_INLINE
) && op
== -1 && !is_virtual
&& target_method
&& interp_method_check_inlining (td
, target_method
)) {
1979 MonoMethodHeader
*mheader
= interp_method_get_header (target_method
, error
);
1980 return_val_if_nok (error
, FALSE
);
1982 if (interp_inline_method (td
, target_method
, mheader
, error
)) {
1988 /* Don't inline methods that do calls */
1989 if (op
== -1 && td
->inlined_method
)
1992 td
->sp
-= csignature
->param_count
+ !!csignature
->hasthis
;
1993 for (i
= 0; i
< csignature
->param_count
; ++i
) {
1994 if (td
->sp
[i
+ !!csignature
->hasthis
].type
== STACK_TYPE_VT
) {
1996 MonoClass
*klass
= mono_class_from_mono_type_internal (csignature
->params
[i
]);
1997 if (csignature
->pinvoke
&& method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1998 size
= mono_class_native_size (klass
, NULL
);
2000 size
= mono_class_value_size (klass
, NULL
);
2001 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
2002 vt_stack_used
+= size
;
2006 /* need to handle typedbyref ... */
2007 if (csignature
->ret
->type
!= MONO_TYPE_VOID
) {
2008 int mt
= mint_type(csignature
->ret
);
2009 MonoClass
*klass
= mono_class_from_mono_type_internal (csignature
->ret
);
2010 if (mt
== MINT_TYPE_VT
) {
2011 if (csignature
->pinvoke
&& method
->wrapper_type
!= MONO_WRAPPER_NONE
)
2012 vt_res_size
= mono_class_native_size (klass
, NULL
);
2014 vt_res_size
= mono_class_value_size (klass
, NULL
);
2015 if (mono_class_has_failure (klass
)) {
2016 mono_error_set_for_class_failure (error
, klass
);
2019 PUSH_VT(td
, vt_res_size
);
2021 PUSH_TYPE(td
, stack_type
[mt
], klass
);
2025 /* We need to convert delegate invoke to a indirect call on the interp_invoke_impl field */
2026 if (target_method
&& m_class_get_parent (target_method
->klass
) == mono_defaults
.multicastdelegate_class
) {
2027 const char *name
= target_method
->name
;
2028 if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
2030 interp_add_ins (td
, MINT_LD_DELEGATE_INVOKE_IMPL
);
2031 td
->last_ins
->data
[0] = csignature
->param_count
+ 1;
2036 interp_add_ins (td
, op
);
2038 if (op
== MINT_LDLEN
) {
2039 #ifdef MONO_BIG_ARRAYS
2040 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I8
);
2042 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
2045 if (op
== MINT_LDELEMA
|| op
== MINT_LDELEMA_TC
) {
2046 td
->last_ins
->data
[0] = get_data_item_index (td
, m_class_get_element_class (target_method
->klass
));
2047 td
->last_ins
->data
[1] = 1 + m_class_get_rank (target_method
->klass
);
2050 if (op
== MINT_CALLRUN
) {
2051 td
->last_ins
->data
[0] = get_data_item_index (td
, target_method
);
2052 td
->last_ins
->data
[1] = get_data_item_index (td
, mono_method_signature_internal (target_method
));
2054 } else if (!calli
&& !is_virtual
&& jit_call_supported (target_method
, csignature
)) {
2055 interp_add_ins (td
, MINT_JIT_CALL
);
2056 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)mono_interp_get_imethod (domain
, target_method
, error
));
2057 mono_error_assert_ok (error
);
2059 #ifndef MONO_ARCH_HAS_NO_PROPER_MONOCTX
2060 /* Try using fast icall path for simple signatures */
2061 if (native
&& !method
->dynamic
)
2062 op
= interp_icall_op_for_sig (csignature
);
2064 if (csignature
->call_convention
== MONO_CALL_VARARG
)
2065 interp_add_ins (td
, MINT_CALL_VARARG
);
2067 interp_add_ins (td
, native
? ((op
!= -1) ? MINT_CALLI_NAT_FAST
: MINT_CALLI_NAT
) : MINT_CALLI
);
2068 else if (is_virtual
&& !mono_class_is_marshalbyref (target_method
->klass
))
2069 interp_add_ins (td
, is_void
? MINT_VCALLVIRT_FAST
: MINT_CALLVIRT_FAST
);
2070 else if (is_virtual
)
2071 interp_add_ins (td
, is_void
? MINT_VCALLVIRT
: MINT_CALLVIRT
);
2073 interp_add_ins (td
, is_void
? MINT_VCALL
: MINT_CALL
);
2076 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)csignature
);
2078 td
->last_ins
->data
[1] = op
;
2080 td
->last_ins
->data
[0] = get_data_item_index (td
, (void *)mono_interp_get_imethod (domain
, target_method
, error
));
2081 return_val_if_nok (error
, FALSE
);
2082 if (csignature
->call_convention
== MONO_CALL_VARARG
)
2083 td
->last_ins
->data
[1] = get_data_item_index (td
, (void *)csignature
);
2084 else if (is_virtual
&& !mono_class_is_marshalbyref (target_method
->klass
)) {
2085 /* FIXME Use fastpath also for MBRO. Asserts in mono_method_get_vtable_slot */
2086 if (mono_class_is_interface (target_method
->klass
))
2087 td
->last_ins
->data
[1] = -2 * MONO_IMT_SIZE
+ mono_method_get_imt_slot (target_method
);
2089 td
->last_ins
->data
[1] = mono_method_get_vtable_slot (target_method
);
2094 if (vt_stack_used
!= 0 || vt_res_size
!= 0) {
2095 interp_add_ins (td
, MINT_VTRESULT
);
2096 td
->last_ins
->data
[0] = vt_res_size
;
2097 WRITE32_INS (td
->last_ins
, 1, &vt_stack_used
);
2098 td
->vt_sp
-= vt_stack_used
;
2104 static MonoClassField
*
2105 interp_field_from_token (MonoMethod
*method
, guint32 token
, MonoClass
**klass
, MonoGenericContext
*generic_context
, MonoError
*error
)
2107 MonoClassField
*field
= NULL
;
2108 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
2109 field
= (MonoClassField
*) mono_method_get_wrapper_data (method
, token
);
2110 *klass
= field
->parent
;
2112 mono_class_setup_fields (field
->parent
);
2114 field
= mono_field_from_token_checked (m_class_get_image (method
->klass
), token
, klass
, generic_context
, error
);
2115 return_val_if_nok (error
, NULL
);
2118 if (!method
->skip_visibility
&& !mono_method_can_access_field (method
, field
)) {
2119 char *method_fname
= mono_method_full_name (method
, TRUE
);
2120 char *field_fname
= mono_field_full_name (field
);
2121 mono_error_set_generic_error (error
, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname
, method_fname
);
2122 g_free (method_fname
);
2123 g_free (field_fname
);
2130 static InterpBasicBlock
*
2131 get_bb (TransformData
*td
, InterpBasicBlock
*cbb
, unsigned char *ip
)
2133 int offset
= ip
- td
->il_code
;
2134 InterpBasicBlock
*bb
= td
->offset_to_bb
[offset
];
2137 bb
= (InterpBasicBlock
*)mono_mempool_alloc0 (td
->mempool
, sizeof (InterpBasicBlock
));
2139 td
->offset_to_bb
[offset
] = bb
;
2141 td
->basic_blocks
= g_list_append_mempool (td
->mempool
, td
->basic_blocks
, bb
);
2145 bb
->preds
= g_slist_prepend_mempool (td
->mempool
, bb
->preds
, cbb
);
2152 * Compute the set of IL level basic blocks.
2155 get_basic_blocks (TransformData
*td
)
2157 guint8
*start
= (guint8
*)td
->il_code
;
2158 guint8
*end
= (guint8
*)td
->il_code
+ td
->code_size
;
2160 unsigned char *target
;
2163 const MonoOpcode
*opcode
;
2164 InterpBasicBlock
*cbb
;
2166 td
->offset_to_bb
= (InterpBasicBlock
**)mono_mempool_alloc0 (td
->mempool
, sizeof (InterpBasicBlock
*) * (end
- start
+ 1));
2167 td
->entry_bb
= cbb
= get_bb (td
, NULL
, start
);
2170 cli_addr
= ip
- start
;
2171 td
->offset_to_bb
[cli_addr
] = cbb
;
2172 i
= mono_opcode_value ((const guint8
**)&ip
, end
);
2173 opcode
= &mono_opcodes
[i
];
2174 switch (opcode
->argument
) {
2175 case MonoInlineNone
:
2178 case MonoInlineString
:
2179 case MonoInlineType
:
2180 case MonoInlineField
:
2181 case MonoInlineMethod
:
2184 case MonoShortInlineR
:
2191 case MonoShortInlineVar
:
2192 case MonoShortInlineI
:
2195 case MonoShortInlineBrTarget
:
2196 target
= start
+ cli_addr
+ 2 + (signed char)ip
[1];
2197 get_bb (td
, cbb
, target
);
2199 cbb
= get_bb (td
, cbb
, ip
);
2201 case MonoInlineBrTarget
:
2202 target
= start
+ cli_addr
+ 5 + (gint32
)read32 (ip
+ 1);
2203 get_bb (td
, cbb
, target
);
2205 cbb
= get_bb (td
, cbb
, ip
);
2207 case MonoInlineSwitch
: {
2208 guint32 n
= read32 (ip
+ 1);
2211 cli_addr
+= 5 + 4 * n
;
2212 target
= start
+ cli_addr
;
2213 get_bb (td
, cbb
, target
);
2215 for (j
= 0; j
< n
; ++j
) {
2216 target
= start
+ cli_addr
+ (gint32
)read32 (ip
);
2217 get_bb (td
, cbb
, target
);
2220 cbb
= get_bb (td
, cbb
, ip
);
2228 g_assert_not_reached ();
2232 cbb
= get_bb (td
, NULL
, ip
);
2237 interp_save_debug_info (InterpMethod
*rtm
, MonoMethodHeader
*header
, TransformData
*td
, GArray
*line_numbers
)
2239 MonoDebugMethodJitInfo
*dinfo
;
2242 if (!mono_debug_enabled ())
2246 * We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
2249 dinfo
= g_new0 (MonoDebugMethodJitInfo
, 1);
2250 dinfo
->num_params
= rtm
->param_count
;
2251 dinfo
->params
= g_new0 (MonoDebugVarInfo
, dinfo
->num_params
);
2252 dinfo
->num_locals
= header
->num_locals
;
2253 dinfo
->locals
= g_new0 (MonoDebugVarInfo
, header
->num_locals
);
2254 dinfo
->code_start
= (guint8
*)rtm
->code
;
2255 dinfo
->code_size
= td
->new_code_end
- td
->new_code
;
2256 dinfo
->epilogue_begin
= 0;
2257 dinfo
->has_var_info
= TRUE
;
2258 dinfo
->num_line_numbers
= line_numbers
->len
;
2259 dinfo
->line_numbers
= g_new0 (MonoDebugLineNumberEntry
, dinfo
->num_line_numbers
);
2261 for (i
= 0; i
< dinfo
->num_params
; i
++) {
2262 MonoDebugVarInfo
*var
= &dinfo
->params
[i
];
2263 var
->type
= rtm
->param_types
[i
];
2265 for (i
= 0; i
< dinfo
->num_locals
; i
++) {
2266 MonoDebugVarInfo
*var
= &dinfo
->locals
[i
];
2267 var
->type
= mono_metadata_type_dup (NULL
, header
->locals
[i
]);
2270 for (i
= 0; i
< dinfo
->num_line_numbers
; i
++)
2271 dinfo
->line_numbers
[i
] = g_array_index (line_numbers
, MonoDebugLineNumberEntry
, i
);
2272 mono_debug_add_method (rtm
->method
, dinfo
, rtm
->domain
);
2274 mono_debug_free_method_jit_info (dinfo
);
2277 /* Same as the code in seq-points.c */
2279 insert_pred_seq_point (SeqPoint
*last_sp
, SeqPoint
*sp
, GSList
**next
)
2282 int src_index
= last_sp
->next_offset
;
2283 int dst_index
= sp
->next_offset
;
2285 /* bb->in_bb might contain duplicates */
2286 for (l
= next
[src_index
]; l
; l
= l
->next
)
2287 if (GPOINTER_TO_UINT (l
->data
) == dst_index
)
2290 next
[src_index
] = g_slist_append (next
[src_index
], GUINT_TO_POINTER (dst_index
));
2294 recursively_make_pred_seq_points (TransformData
*td
, InterpBasicBlock
*bb
)
2296 SeqPoint
** const MONO_SEQ_SEEN_LOOP
= (SeqPoint
**)GINT_TO_POINTER(-1);
2299 GArray
*predecessors
= g_array_new (FALSE
, TRUE
, sizeof (gpointer
));
2300 GHashTable
*seen
= g_hash_table_new_full (g_direct_hash
, NULL
, NULL
, NULL
);
2302 // Insert/remove sentinel into the memoize table to detect loops containing bb
2303 bb
->pred_seq_points
= MONO_SEQ_SEEN_LOOP
;
2305 for (l
= bb
->preds
; l
; l
= l
->next
) {
2306 InterpBasicBlock
*in_bb
= (InterpBasicBlock
*)l
->data
;
2308 // This bb has the last seq point, append it and continue
2309 if (in_bb
->last_seq_point
!= NULL
) {
2310 predecessors
= g_array_append_val (predecessors
, in_bb
->last_seq_point
);
2314 // We've looped or handled this before, exit early.
2315 // No last sequence points to find.
2316 if (in_bb
->pred_seq_points
== MONO_SEQ_SEEN_LOOP
)
2319 // Take sequence points from incoming basic blocks
2321 if (in_bb
== td
->entry_bb
)
2324 if (in_bb
->pred_seq_points
== NULL
)
2325 recursively_make_pred_seq_points (td
, in_bb
);
2327 // Union sequence points with incoming bb's
2328 for (int i
=0; i
< in_bb
->num_pred_seq_points
; i
++) {
2329 if (!g_hash_table_lookup (seen
, in_bb
->pred_seq_points
[i
])) {
2330 g_array_append_val (predecessors
, in_bb
->pred_seq_points
[i
]);
2331 g_hash_table_insert (seen
, in_bb
->pred_seq_points
[i
], (gpointer
)&MONO_SEQ_SEEN_LOOP
);
2334 // predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
2337 g_hash_table_destroy (seen
);
2339 if (predecessors
->len
!= 0) {
2340 bb
->pred_seq_points
= (SeqPoint
**)mono_mempool_alloc0 (td
->mempool
, sizeof (SeqPoint
*) * predecessors
->len
);
2341 bb
->num_pred_seq_points
= predecessors
->len
;
2343 for (int newer
= 0; newer
< bb
->num_pred_seq_points
; newer
++) {
2344 bb
->pred_seq_points
[newer
] = (SeqPoint
*)g_array_index (predecessors
, gpointer
, newer
);
2348 g_array_free (predecessors
, TRUE
);
2352 collect_pred_seq_points (TransformData
*td
, InterpBasicBlock
*bb
, SeqPoint
*seqp
, GSList
**next
)
2354 // Doesn't have a last sequence point, must find from incoming basic blocks
2355 if (bb
->pred_seq_points
== NULL
&& bb
!= td
->entry_bb
)
2356 recursively_make_pred_seq_points (td
, bb
);
2358 for (int i
= 0; i
< bb
->num_pred_seq_points
; i
++)
2359 insert_pred_seq_point (bb
->pred_seq_points
[i
], seqp
, next
);
2365 save_seq_points (TransformData
*td
, MonoJitInfo
*jinfo
)
2368 int i
, seq_info_size
;
2369 MonoSeqPointInfo
*info
;
2370 GSList
**next
= NULL
;
2373 if (!td
->gen_sdb_seq_points
)
2377 * For each sequence point, compute the list of sequence points immediately
2378 * following it, this is needed to implement 'step over' in the debugger agent.
2379 * Similar to the code in mono_save_seq_point_info ().
2381 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2382 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2384 /* Store the seq point index here temporarily */
2385 sp
->next_offset
= i
;
2387 next
= (GSList
**)mono_mempool_alloc0 (td
->mempool
, sizeof (GList
*) * td
->seq_points
->len
);
2388 for (bblist
= td
->basic_blocks
; bblist
; bblist
= bblist
->next
) {
2389 InterpBasicBlock
*bb
= (InterpBasicBlock
*)bblist
->data
;
2391 GSList
*bb_seq_points
= g_slist_reverse (bb
->seq_points
);
2392 SeqPoint
*last
= NULL
;
2393 for (GSList
*l
= bb_seq_points
; l
; l
= l
->next
) {
2394 SeqPoint
*sp
= (SeqPoint
*)l
->data
;
2396 if (sp
->il_offset
== METHOD_ENTRY_IL_OFFSET
|| sp
->il_offset
== METHOD_EXIT_IL_OFFSET
)
2397 /* Used to implement method entry/exit events */
2401 /* Link with the previous seq point in the same bb */
2402 next
[last
->next_offset
] = g_slist_append_mempool (td
->mempool
, next
[last
->next_offset
], GINT_TO_POINTER (sp
->next_offset
));
2404 /* Link with the last bb in the previous bblocks */
2405 collect_pred_seq_points (td
, bb
, sp
, next
);
2411 /* Serialize the seq points into a byte array */
2412 array
= g_byte_array_new ();
2413 SeqPoint zero_seq_point
= {0};
2414 SeqPoint
* last_seq_point
= &zero_seq_point
;
2415 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2416 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2418 sp
->next_offset
= 0;
2419 if (mono_seq_point_info_add_seq_point (array
, sp
, last_seq_point
, next
[i
], TRUE
))
2420 last_seq_point
= sp
;
2423 if (td
->verbose_level
) {
2424 g_print ("\nSEQ POINT MAP FOR %s: \n", td
->method
->name
);
2426 for (i
= 0; i
< td
->seq_points
->len
; ++i
) {
2427 SeqPoint
*sp
= (SeqPoint
*)g_ptr_array_index (td
->seq_points
, i
);
2433 g_print ("\tIL0x%x[0x%0x] ->", sp
->il_offset
, sp
->native_offset
);
2434 for (l
= next
[i
]; l
; l
= l
->next
) {
2435 int next_index
= GPOINTER_TO_UINT (l
->data
);
2436 g_print (" IL0x%x", ((SeqPoint
*)g_ptr_array_index (td
->seq_points
, next_index
))->il_offset
);
2442 info
= mono_seq_point_info_new (array
->len
, TRUE
, array
->data
, TRUE
, &seq_info_size
);
2443 mono_atomic_fetch_add_i32 (&mono_jit_stats
.allocated_seq_points_size
, seq_info_size
);
2445 g_byte_array_free (array
, TRUE
);
2447 jinfo
->seq_points
= info
;
2450 #define BARRIER_IF_VOLATILE(td) \
2453 interp_add_ins (td, MINT_MONO_MEMORY_BARRIER); \
2454 volatile_ = FALSE; \
2458 #define INLINE_FAILURE \
2460 if (td->method != method) \
2465 interp_method_compute_offsets (InterpMethod
*imethod
, MonoMethodSignature
*signature
, MonoMethodHeader
*header
)
2467 int i
, offset
, size
, align
;
2469 imethod
->local_offsets
= (guint32
*)g_malloc (header
->num_locals
* sizeof(guint32
));
2471 for (i
= 0; i
< header
->num_locals
; ++i
) {
2472 size
= mono_type_size (header
->locals
[i
], &align
);
2473 offset
+= align
- 1;
2474 offset
&= ~(align
- 1);
2475 imethod
->local_offsets
[i
] = offset
;
2478 offset
= (offset
+ 7) & ~7;
2480 imethod
->exvar_offsets
= (guint32
*)g_malloc (header
->num_clauses
* sizeof (guint32
));
2481 for (i
= 0; i
< header
->num_clauses
; i
++) {
2482 imethod
->exvar_offsets
[i
] = offset
;
2483 offset
+= sizeof (MonoObject
*);
2485 offset
= (offset
+ 7) & ~7;
2487 imethod
->locals_size
= offset
;
2488 g_assert (imethod
->locals_size
< 65536);
2492 get_arg_type (MonoMethodSignature
*signature
, int arg_n
)
2494 if (signature
->hasthis
&& arg_n
== 0)
2495 return mono_get_object_type ();
2496 return signature
->params
[arg_n
- !!signature
->hasthis
];
2500 init_bb_start (TransformData
*td
, MonoMethodHeader
*header
)
2502 const unsigned char *ip
, *end
;
2503 const MonoOpcode
*opcode
;
2504 int offset
, i
, in
, backwards
;
2506 /* intern the strings in the method. */
2508 end
= ip
+ header
->code_size
;
2510 td
->is_bb_start
[0] = 1;
2517 else if (in
== 0xf0) {
2519 in
= *ip
+ MONO_CEE_MONO_ICALL
;
2521 opcode
= &mono_opcodes
[in
];
2522 switch (opcode
->argument
) {
2523 case MonoInlineNone
:
2526 case MonoInlineString
:
2529 case MonoInlineType
:
2532 case MonoInlineMethod
:
2535 case MonoInlineField
:
2539 case MonoShortInlineR
:
2542 case MonoInlineBrTarget
:
2543 offset
= read32 (ip
+ 1);
2545 backwards
= offset
< 0;
2546 offset
+= ip
- header
->code
;
2547 g_assert (offset
>= 0 && offset
< header
->code_size
);
2548 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2550 case MonoShortInlineBrTarget
:
2551 offset
= ((gint8
*)ip
) [1];
2553 backwards
= offset
< 0;
2554 offset
+= ip
- header
->code
;
2555 g_assert (offset
>= 0 && offset
< header
->code_size
);
2556 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2561 case MonoShortInlineVar
:
2562 case MonoShortInlineI
:
2565 case MonoInlineSwitch
: {
2567 const unsigned char *next_ip
;
2571 next_ip
= ip
+ 4 * n
;
2572 for (i
= 0; i
< n
; i
++) {
2573 offset
= read32 (ip
);
2574 backwards
= offset
< 0;
2575 offset
+= next_ip
- header
->code
;
2576 g_assert (offset
>= 0 && offset
< header
->code_size
);
2577 td
->is_bb_start
[offset
] |= backwards
? 2 : 1;
2587 g_assert_not_reached ();
2592 #ifdef NO_UNALIGNED_ACCESS
2594 get_unaligned_opcode (int opcode
)
2598 return MINT_LDFLD_I8_UNALIGNED
;
2600 return MINT_LDFLD_R8_UNALIGNED
;
2602 return MINT_STFLD_I8_UNALIGNED
;
2604 return MINT_STFLD_R8_UNALIGNED
;
2606 g_assert_not_reached ();
2613 interp_handle_isinst (TransformData
*td
, MonoClass
*klass
, gboolean isinst_instr
)
2615 /* Follow the logic from jit's handle_isinst */
2616 if (!mono_class_has_variant_generic_params (klass
)) {
2617 if (mono_class_is_interface (klass
))
2618 interp_add_ins (td
, isinst_instr
? MINT_ISINST_INTERFACE
: MINT_CASTCLASS_INTERFACE
);
2619 else if (!mono_class_is_marshalbyref (klass
) && m_class_get_rank (klass
) == 0 && !mono_class_is_nullable (klass
))
2620 interp_add_ins (td
, isinst_instr
? MINT_ISINST_COMMON
: MINT_CASTCLASS_COMMON
);
2622 interp_add_ins (td
, isinst_instr
? MINT_ISINST
: MINT_CASTCLASS
);
2624 interp_add_ins (td
, isinst_instr
? MINT_ISINST
: MINT_CASTCLASS
);
2626 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
2631 interp_emit_ldobj (TransformData
*td
, MonoClass
*klass
)
2633 int mt
= mint_type (m_class_get_byval_arg (klass
));
2636 if (mt
== MINT_TYPE_VT
) {
2637 interp_add_ins (td
, MINT_LDOBJ_VT
);
2638 size
= mono_class_value_size (klass
, NULL
);
2639 WRITE32_INS (td
->last_ins
, 0, &size
);
2642 int opcode
= interp_get_ldind_for_mt (mt
);
2643 interp_add_ins (td
, opcode
);
2646 SET_TYPE (td
->sp
- 1, stack_type
[mt
], klass
);
2650 interp_emit_stobj (TransformData
*td
, MonoClass
*klass
)
2652 int mt
= mint_type (m_class_get_byval_arg (klass
));
2654 if (mt
== MINT_TYPE_VT
) {
2656 interp_add_ins (td
, MINT_STOBJ_VT
);
2657 td
->last_ins
->data
[0] = get_data_item_index(td
, klass
);
2658 size
= mono_class_value_size (klass
, NULL
);
2665 opcode
= MINT_STIND_I1
;
2669 opcode
= MINT_STIND_I2
;
2672 opcode
= MINT_STIND_I4
;
2675 opcode
= MINT_STIND_I8
;
2678 opcode
= MINT_STIND_R4
;
2681 opcode
= MINT_STIND_R8
;
2684 opcode
= MINT_STIND_REF
;
2686 default: g_assert_not_reached (); break;
2688 interp_add_ins (td
, opcode
);
2694 interp_emit_sfld_access (TransformData
*td
, MonoClassField
*field
, MonoClass
*field_class
, int mt
, gboolean is_load
, MonoError
*error
)
2696 MonoDomain
*domain
= td
->rtm
->domain
;
2697 // Initialize the offset for the field
2698 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, field
->parent
, error
);
2699 return_if_nok (error
);
2701 if (mono_class_field_is_special_static (field
)) {
2704 mono_domain_lock (domain
);
2705 g_assert (domain
->special_static_fields
);
2706 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (domain
->special_static_fields
, field
));
2707 mono_domain_unlock (domain
);
2710 // Offset is SpecialStaticOffset
2711 if ((offset
& 0x80000000) == 0 && mt
!= MINT_TYPE_VT
) {
2712 // This field is thread static
2713 interp_add_ins (td
, (is_load
? MINT_LDTSFLD_I1
: MINT_STTSFLD_I1
) + mt
);
2714 WRITE32_INS(td
->last_ins
, 0, &offset
);
2716 if (mt
== MINT_TYPE_VT
) {
2717 interp_add_ins (td
, is_load
? MINT_LDSSFLD_VT
: MINT_STSSFLD_VT
);
2718 WRITE32_INS(td
->last_ins
, 0, &offset
);
2720 int size
= mono_class_value_size (field_class
, NULL
);
2721 WRITE32_INS(td
->last_ins
, 2, &size
);
2723 interp_add_ins (td
, is_load
? MINT_LDSSFLD
: MINT_STSSFLD
);
2724 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
2725 WRITE32_INS(td
->last_ins
, 1, &offset
);
2730 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_LDSFLD_VT
: (MINT_LDSFLD_I1
+ mt
- MINT_TYPE_I1
));
2732 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_STSFLD_VT
: (MINT_STSFLD_I1
+ mt
- MINT_TYPE_I1
));
2734 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
2735 td
->last_ins
->data
[1] = get_data_item_index (td
, (char*)mono_vtable_get_static_field_data (vtable
) + field
->offset
);
2737 if (mt
== MINT_TYPE_VT
) {
2738 int size
= mono_class_value_size (field_class
, NULL
);
2739 WRITE32_INS(td
->last_ins
, 2, &size
);
2745 generate_code (TransformData
*td
, MonoMethod
*method
, MonoMethodHeader
*header
, MonoGenericContext
*generic_context
, MonoError
*error
)
2748 int offset
, mt
, i
, i32
;
2751 const unsigned char *end
;
2752 MonoSimpleBasicBlock
*bb
= NULL
, *original_bb
= NULL
;
2753 gboolean sym_seq_points
= FALSE
;
2754 MonoBitSet
*seq_point_locs
= NULL
;
2755 gboolean readonly
= FALSE
;
2756 gboolean volatile_
= FALSE
;
2757 MonoClass
*constrained_class
= NULL
;
2759 MonoClassField
*field
;
2760 MonoImage
*image
= m_class_get_image (method
->klass
);
2761 InterpMethod
*rtm
= td
->rtm
;
2762 MonoDomain
*domain
= rtm
->domain
;
2763 MonoMethodSignature
*signature
= mono_method_signature_internal (method
);
2764 gboolean ret
= TRUE
;
2765 gboolean emitted_funccall_seq_point
= FALSE
;
2766 guint32
*arg_offsets
= NULL
;
2767 InterpInst
*last_seq_point
= NULL
;
2769 original_bb
= bb
= mono_basic_block_split (method
, error
, header
);
2770 goto_if_nok (error
, exit
);
2773 td
->il_code
= header
->code
;
2774 td
->in_start
= td
->ip
= header
->code
;
2775 end
= td
->ip
+ header
->code_size
;
2776 td
->stack_state
= (StackInfo
**)g_malloc0(header
->code_size
* sizeof(StackInfo
*));
2777 td
->stack_height
= (int*)g_malloc(header
->code_size
* sizeof(int));
2778 td
->vt_stack_size
= (int*)g_malloc(header
->code_size
* sizeof(int));
2779 td
->clause_indexes
= (int*)g_malloc (header
->code_size
* sizeof (int));
2780 td
->is_bb_start
= (guint8
*)g_malloc0(header
->code_size
);
2782 init_bb_start (td
, header
);
2784 for (i
= 0; i
< header
->code_size
; i
++) {
2785 td
->stack_height
[i
] = -1;
2786 td
->clause_indexes
[i
] = -1;
2789 for (i
= 0; i
< header
->num_clauses
; i
++) {
2790 MonoExceptionClause
*c
= header
->clauses
+ i
;
2791 td
->stack_height
[c
->handler_offset
] = 0;
2792 td
->vt_stack_size
[c
->handler_offset
] = 0;
2793 td
->is_bb_start
[c
->handler_offset
] = 1;
2795 td
->stack_height
[c
->handler_offset
] = 1;
2796 td
->stack_state
[c
->handler_offset
] = (StackInfo
*)g_malloc0(sizeof(StackInfo
));
2797 td
->stack_state
[c
->handler_offset
][0].type
= STACK_TYPE_O
;
2798 td
->stack_state
[c
->handler_offset
][0].klass
= NULL
; /*FIX*/
2800 if (c
->flags
& MONO_EXCEPTION_CLAUSE_FILTER
) {
2801 td
->stack_height
[c
->data
.filter_offset
] = 0;
2802 td
->vt_stack_size
[c
->data
.filter_offset
] = 0;
2803 td
->is_bb_start
[c
->data
.filter_offset
] = 1;
2805 td
->stack_height
[c
->data
.filter_offset
] = 1;
2806 td
->stack_state
[c
->data
.filter_offset
] = (StackInfo
*)g_malloc0(sizeof(StackInfo
));
2807 td
->stack_state
[c
->data
.filter_offset
][0].type
= STACK_TYPE_O
;
2808 td
->stack_state
[c
->data
.filter_offset
][0].klass
= NULL
; /*FIX*/
2811 for (int j
= c
->handler_offset
; j
< c
->handler_offset
+ c
->handler_len
; ++j
) {
2812 if (td
->clause_indexes
[j
] == -1)
2813 td
->clause_indexes
[j
] = i
;
2817 if (td
->gen_sdb_seq_points
&& td
->method
== method
) {
2818 MonoDebugMethodInfo
*minfo
;
2819 get_basic_blocks (td
);
2821 minfo
= mono_debug_lookup_method (method
);
2824 MonoSymSeqPoint
*sps
;
2825 int i
, n_il_offsets
;
2827 mono_debug_get_seq_points (minfo
, NULL
, NULL
, NULL
, &sps
, &n_il_offsets
);
2829 seq_point_locs
= mono_bitset_mem_new (mono_mempool_alloc0 (td
->mempool
, mono_bitset_alloc_size (header
->code_size
, 0)), header
->code_size
, 0);
2830 sym_seq_points
= TRUE
;
2832 for (i
= 0; i
< n_il_offsets
; ++i
) {
2833 if (sps
[i
].il_offset
< header
->code_size
)
2834 mono_bitset_set_fast (seq_point_locs
, sps
[i
].il_offset
);
2838 MonoDebugMethodAsyncInfo
* asyncMethod
= mono_debug_lookup_method_async_debug_info (method
);
2840 for (i
= 0; asyncMethod
!= NULL
&& i
< asyncMethod
->num_awaits
; i
++) {
2841 mono_bitset_set_fast (seq_point_locs
, asyncMethod
->resume_offsets
[i
]);
2842 mono_bitset_set_fast (seq_point_locs
, asyncMethod
->yield_offsets
[i
]);
2844 mono_debug_free_method_async_debug_info (asyncMethod
);
2846 } else if (!method
->wrapper_type
&& !method
->dynamic
&& mono_debug_image_has_debug_info (m_class_get_image (method
->klass
))) {
2847 /* Methods without line number info like auto-generated property accessors */
2848 seq_point_locs
= mono_bitset_new (header
->code_size
, 0);
2849 sym_seq_points
= TRUE
;
2853 if (sym_seq_points
) {
2854 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
2855 last_seq_point
->flags
= INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY
;
2858 if (mono_debugger_method_has_breakpoint (method
))
2859 interp_add_ins (td
, MINT_BREAKPOINT
);
2861 if (method
== td
->method
) {
2862 if (td
->verbose_level
) {
2863 char *tmp
= mono_disasm_code (NULL
, method
, td
->ip
, end
);
2864 char *name
= mono_method_full_name (method
, TRUE
);
2865 g_print ("Method %s, original code:\n", name
);
2866 g_print ("%s\n", tmp
);
2871 if (header
->num_locals
&& header
->init_locals
)
2872 interp_add_ins (td
, MINT_INITLOCALS
);
2874 if (rtm
->prof_flags
& MONO_PROFILER_CALL_INSTRUMENTATION_ENTER
)
2875 interp_add_ins (td
, MINT_PROF_ENTER
);
2877 /* safepoint is required on method entry */
2878 if (mono_threads_are_safepoints_enabled ())
2879 interp_add_ins (td
, MINT_SAFEPOINT
);
2882 arg_offsets
= (guint32
*) g_malloc ((!!signature
->hasthis
+ signature
->param_count
) * sizeof (guint32
));
2883 /* Allocate locals to store inlined method args from stack */
2884 for (i
= signature
->param_count
- 1; i
>= 0; i
--) {
2885 offset
= create_interp_local (td
, signature
->params
[i
]);
2886 arg_offsets
[i
+ !!signature
->hasthis
] = offset
;
2887 store_local_general (td
, offset
, signature
->params
[i
]);
2890 if (signature
->hasthis
) {
2892 * If this is value type, it is passed by address and not by value.
2893 * FIXME We should use MINT_TYPE_P instead of MINT_TYPE_O
2895 MonoType
*type
= mono_get_object_type ();
2896 offset
= create_interp_local (td
, type
);
2897 arg_offsets
[0] = offset
;
2898 store_local_general (td
, offset
, type
);
2902 while (td
->ip
< end
) {
2903 g_assert (td
->sp
>= td
->stack
);
2904 g_assert (td
->vt_sp
< 0x10000000);
2905 in_offset
= td
->ip
- header
->code
;
2906 td
->in_start
= td
->ip
;
2907 InterpInst
*prev_last_ins
= td
->last_ins
;
2909 if (td
->stack_height
[in_offset
] >= 0) {
2910 g_assert (td
->is_bb_start
[in_offset
]);
2911 if (td
->stack_height
[in_offset
] > 0)
2912 memcpy (td
->stack
, td
->stack_state
[in_offset
], td
->stack_height
[in_offset
] * sizeof(td
->stack
[0]));
2913 td
->sp
= td
->stack
+ td
->stack_height
[in_offset
];
2914 td
->vt_sp
= td
->vt_stack_size
[in_offset
];
2917 if (in_offset
== bb
->end
)
2921 int op_size
= mono_opcode_size (td
->ip
, end
);
2922 g_assert (op_size
> 0); /* The BB formation pass must catch all bad ops */
2924 if (td
->verbose_level
> 1)
2925 g_print ("SKIPPING DEAD OP at %x\n", in_offset
);
2931 if (td
->verbose_level
> 1) {
2932 g_print ("IL_%04lx %s %-10s, sp %ld, %s %-12s vt_sp %u (max %u)\n",
2933 td
->ip
- td
->il_code
,
2934 td
->is_bb_start
[td
->ip
- td
->il_code
] == 3 ? "<>" :
2935 td
->is_bb_start
[td
->ip
- td
->il_code
] == 2 ? "< " :
2936 td
->is_bb_start
[td
->ip
- td
->il_code
] == 1 ? " >" : " ",
2937 mono_opcode_name (*td
->ip
), td
->sp
- td
->stack
,
2938 td
->sp
> td
->stack
? stack_type_string
[td
->sp
[-1].type
] : " ",
2939 (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
)) : "",
2940 td
->vt_sp
, td
->max_vt_sp
);
2943 if (sym_seq_points
&& mono_bitset_test_fast (seq_point_locs
, td
->ip
- header
->code
)) {
2944 InterpBasicBlock
*cbb
= td
->offset_to_bb
[td
->ip
- header
->code
];
2948 * Make methods interruptable at the beginning, and at the targets of
2949 * backward branches.
2951 if (in_offset
== 0 || g_slist_length (cbb
->preds
) > 1)
2952 interp_add_ins (td
, MINT_SDB_INTR_LOC
);
2954 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
2957 if (td
->is_bb_start
[in_offset
]) {
2958 int index
= td
->clause_indexes
[in_offset
];
2960 MonoExceptionClause
*clause
= &header
->clauses
[index
];
2961 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
||
2962 clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
2963 in_offset
== clause
->handler_offset
)
2964 interp_add_ins (td
, MINT_START_ABORT_PROT
);
2971 emitted_funccall_seq_point
= FALSE
;
2975 SIMPLE_OP(td
, MINT_BREAK
);
2981 int arg_n
= *td
->ip
- CEE_LDARG_0
;
2982 if (td
->method
== method
)
2983 load_arg (td
, arg_n
);
2985 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
2993 load_local (td
, *td
->ip
- CEE_LDLOC_0
);
3000 store_local (td
, *td
->ip
- CEE_STLOC_0
);
3004 int arg_n
= ((guint8
*)td
->ip
)[1];
3005 if (td
->method
== method
)
3006 load_arg (td
, arg_n
);
3008 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
3012 case CEE_LDARGA_S
: {
3013 /* NOTE: n includes this */
3014 INLINE_FAILURE
; // probably uncommon
3015 int n
= ((guint8
*) td
->ip
) [1];
3017 get_arg_type_exact (td
, n
, &mt
);
3018 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDARGA_VT
: MINT_LDARGA
);
3019 td
->last_ins
->data
[0] = n
;
3020 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
3025 int arg_n
= ((guint8
*)td
->ip
)[1];
3026 if (td
->method
== method
)
3027 store_arg (td
, arg_n
);
3029 store_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
3034 load_local (td
, ((guint8
*)td
->ip
)[1]);
3038 interp_add_ins (td
, MINT_LDLOCA_S
);
3039 td
->last_ins
->data
[0] = td
->rtm
->local_offsets
[((guint8
*)td
->ip
)[1]];
3040 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
3044 store_local (td
, ((guint8
*)td
->ip
)[1]);
3048 SIMPLE_OP(td
, MINT_LDNULL
);
3049 PUSH_TYPE(td
, STACK_TYPE_O
, NULL
);
3052 SIMPLE_OP(td
, MINT_LDC_I4_M1
);
3053 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3056 if (!td
->is_bb_start
[td
->ip
+ 1 - td
->il_code
] && td
->ip
[1] == 0xfe && td
->ip
[2] == CEE_CEQ
&&
3057 td
->sp
> td
->stack
&& td
->sp
[-1].type
== STACK_TYPE_I4
) {
3058 SIMPLE_OP(td
, MINT_CEQ0_I4
);
3061 SIMPLE_OP(td
, MINT_LDC_I4_0
);
3062 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3066 if (!td
->is_bb_start
[td
->ip
+ 1 - td
->il_code
] &&
3067 (td
->ip
[1] == CEE_ADD
|| td
->ip
[1] == CEE_SUB
) && td
->sp
[-1].type
== STACK_TYPE_I4
) {
3068 interp_add_ins (td
, td
->ip
[1] == CEE_ADD
? MINT_ADD1_I4
: MINT_SUB1_I4
);
3071 SIMPLE_OP(td
, MINT_LDC_I4_1
);
3072 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3082 SIMPLE_OP(td
, (*td
->ip
- CEE_LDC_I4_0
) + MINT_LDC_I4_0
);
3083 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3086 interp_add_ins (td
, MINT_LDC_I4_S
);
3087 td
->last_ins
->data
[0] = ((gint8
*) td
->ip
) [1];
3089 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3092 i32
= read32 (td
->ip
+ 1);
3093 interp_add_ins (td
, MINT_LDC_I4
);
3094 WRITE32_INS (td
->last_ins
, 0, &i32
);
3096 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
3099 gint64 val
= read64 (td
->ip
+ 1);
3100 interp_add_ins (td
, MINT_LDC_I8
);
3101 WRITE64_INS (td
->last_ins
, 0, &val
);
3103 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I8
);
3108 readr4 (td
->ip
+ 1, &val
);
3109 interp_add_ins (td
, MINT_LDC_R4
);
3110 WRITE32_INS (td
->last_ins
, 0, &val
);
3112 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_R4
);
3117 readr8 (td
->ip
+ 1, &val
);
3118 interp_add_ins (td
, MINT_LDC_R8
);
3119 WRITE64_INS (td
->last_ins
, 0, &val
);
3121 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_R8
);
3125 int type
= td
->sp
[-1].type
;
3126 MonoClass
*klass
= td
->sp
[-1].klass
;
3127 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
3128 gint32 size
= mono_class_value_size (klass
, NULL
);
3130 interp_add_ins (td
, MINT_DUP_VT
);
3131 WRITE32_INS (td
->last_ins
, 0, &size
);
3134 SIMPLE_OP(td
, MINT_DUP
);
3135 PUSH_TYPE(td
, type
, klass
);
3140 SIMPLE_OP(td
, MINT_POP
);
3141 td
->last_ins
->data
[0] = 0;
3142 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
3143 int size
= mono_class_value_size (td
->sp
[-1].klass
, NULL
);
3144 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
3145 interp_add_ins (td
, MINT_VTRESULT
);
3146 td
->last_ins
->data
[0] = 0;
3147 WRITE32_INS (td
->last_ins
, 1, &size
);
3155 if (td
->sp
> td
->stack
)
3156 g_warning ("CEE_JMP: stack must be empty");
3157 token
= read32 (td
->ip
+ 1);
3158 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
3159 goto_if_nok (error
, exit
);
3160 interp_add_ins (td
, MINT_JMP
);
3161 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3162 goto_if_nok (error
, exit
);
3166 case CEE_CALLVIRT
: /* Fall through */
3167 case CEE_CALLI
: /* Fall through */
3169 gboolean need_seq_point
= FALSE
;
3171 if (sym_seq_points
&& !mono_bitset_test_fast (seq_point_locs
, td
->ip
+ 5 - header
->code
))
3172 need_seq_point
= TRUE
;
3174 if (!interp_transform_call (td
, method
, NULL
, domain
, generic_context
, td
->is_bb_start
, constrained_class
, readonly
, error
, TRUE
))
3177 if (need_seq_point
) {
3178 //check is is a nested call and remove the MONO_INST_NONEMPTY_STACK of the last breakpoint, only for non native methods
3179 if (!(method
->flags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
3180 if (emitted_funccall_seq_point
) {
3182 last_seq_point
->flags
|= INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL
;
3185 emitted_funccall_seq_point
= TRUE
;
3187 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3188 // This seq point is actually associated with the instruction following the call
3189 last_seq_point
->il_offset
= td
->ip
- header
->code
;
3190 last_seq_point
->flags
= INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK
;
3193 constrained_class
= NULL
;
3198 /* Return from inlined method, return value is on top of stack */
3199 if (td
->method
!= method
) {
3205 MonoType
*ult
= mini_type_get_underlying_type (signature
->ret
);
3206 if (ult
->type
!= MONO_TYPE_VOID
) {
3207 CHECK_STACK (td
, 1);
3209 if (mint_type (ult
) == MINT_TYPE_VT
) {
3210 MonoClass
*klass
= mono_class_from_mono_type_internal (ult
);
3211 vt_size
= mono_class_value_size (klass
, NULL
);
3214 if (td
->sp
> td
->stack
) {
3215 mono_error_set_generic_error (error
, "System", "InvalidProgramException", "");
3218 if (td
->vt_sp
!= ALIGN_TO (vt_size
, MINT_VT_ALIGNMENT
))
3219 g_error ("%s: CEE_RET: value type stack: %d vs. %d", mono_method_full_name (td
->method
, TRUE
), td
->vt_sp
, vt_size
);
3221 if (sym_seq_points
) {
3222 last_seq_point
= interp_add_ins (td
, MINT_SDB_SEQ_POINT
);
3223 td
->last_ins
->flags
= INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT
;
3227 SIMPLE_OP(td
, ult
->type
== MONO_TYPE_VOID
? MINT_RET_VOID
: MINT_RET
);
3229 interp_add_ins (td
, MINT_RET_VT
);
3230 WRITE32_INS (td
->last_ins
, 0, &vt_size
);
3237 handle_branch (td
, MINT_BR_S
, MINT_BR
, 5 + read32 (td
->ip
+ 1));
3242 handle_branch (td
, MINT_BR_S
, MINT_BR
, 2 + (gint8
)td
->ip
[1]);
3247 one_arg_branch (td
, MINT_BRFALSE_I4
, 5 + read32 (td
->ip
+ 1));
3252 one_arg_branch (td
, MINT_BRFALSE_I4
, 2 + (gint8
)td
->ip
[1]);
3257 one_arg_branch (td
, MINT_BRTRUE_I4
, 5 + read32 (td
->ip
+ 1));
3262 one_arg_branch (td
, MINT_BRTRUE_I4
, 2 + (gint8
)td
->ip
[1]);
3267 two_arg_branch (td
, MINT_BEQ_I4
, 5 + read32 (td
->ip
+ 1));
3272 two_arg_branch (td
, MINT_BEQ_I4
, 2 + (gint8
) td
->ip
[1]);
3277 two_arg_branch (td
, MINT_BGE_I4
, 5 + read32 (td
->ip
+ 1));
3282 two_arg_branch (td
, MINT_BGE_I4
, 2 + (gint8
) td
->ip
[1]);
3287 two_arg_branch (td
, MINT_BGT_I4
, 5 + read32 (td
->ip
+ 1));
3292 two_arg_branch (td
, MINT_BGT_I4
, 2 + (gint8
) td
->ip
[1]);
3297 two_arg_branch (td
, MINT_BLT_I4
, 5 + read32 (td
->ip
+ 1));
3302 two_arg_branch (td
, MINT_BLT_I4
, 2 + (gint8
) td
->ip
[1]);
3307 two_arg_branch (td
, MINT_BLE_I4
, 5 + read32 (td
->ip
+ 1));
3312 two_arg_branch (td
, MINT_BLE_I4
, 2 + (gint8
) td
->ip
[1]);
3317 two_arg_branch (td
, MINT_BNE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3322 two_arg_branch (td
, MINT_BNE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3327 two_arg_branch (td
, MINT_BGE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3332 two_arg_branch (td
, MINT_BGE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3337 two_arg_branch (td
, MINT_BGT_UN_I4
, 5 + read32 (td
->ip
+ 1));
3342 two_arg_branch (td
, MINT_BGT_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3347 two_arg_branch (td
, MINT_BLE_UN_I4
, 5 + read32 (td
->ip
+ 1));
3352 two_arg_branch (td
, MINT_BLE_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3357 two_arg_branch (td
, MINT_BLT_UN_I4
, 5 + read32 (td
->ip
+ 1));
3362 two_arg_branch (td
, MINT_BLT_UN_I4
, 2 + (gint8
) td
->ip
[1]);
3368 const unsigned char *next_ip
;
3370 n
= read32 (td
->ip
);
3371 interp_add_ins_explicit (td
, MINT_SWITCH
, MINT_SWITCH_LEN (n
));
3372 WRITE32_INS (td
->last_ins
, 0, &n
);
3374 next_ip
= td
->ip
+ n
* 4;
3376 int stack_height
= td
->sp
- td
->stack
;
3377 for (i
= 0; i
< n
; i
++) {
3378 offset
= read32 (td
->ip
);
3379 target
= next_ip
- td
->il_code
+ offset
;
3382 if (stack_height
> 0 && stack_height
!= td
->stack_height
[target
])
3383 g_warning ("SWITCH with back branch and non-empty stack");
3386 td
->stack_height
[target
] = stack_height
;
3387 td
->vt_stack_size
[target
] = td
->vt_sp
;
3388 if (stack_height
> 0)
3389 td
->stack_state
[target
] = (StackInfo
*)g_memdup (td
->stack
, stack_height
* sizeof (td
->stack
[0]));
3391 WRITE32_INS (td
->last_ins
, 2 + i
* 2, &target
);
3397 CHECK_STACK (td
, 1);
3398 SIMPLE_OP (td
, MINT_LDIND_I1_CHECK
);
3399 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3400 BARRIER_IF_VOLATILE (td
);
3403 CHECK_STACK (td
, 1);
3404 SIMPLE_OP (td
, MINT_LDIND_U1_CHECK
);
3405 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3406 BARRIER_IF_VOLATILE (td
);
3409 CHECK_STACK (td
, 1);
3410 SIMPLE_OP (td
, MINT_LDIND_I2_CHECK
);
3411 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3412 BARRIER_IF_VOLATILE (td
);
3415 CHECK_STACK (td
, 1);
3416 SIMPLE_OP (td
, MINT_LDIND_U2_CHECK
);
3417 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3418 BARRIER_IF_VOLATILE (td
);
3421 CHECK_STACK (td
, 1);
3422 SIMPLE_OP (td
, MINT_LDIND_I4_CHECK
);
3423 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3424 BARRIER_IF_VOLATILE (td
);
3427 CHECK_STACK (td
, 1);
3428 SIMPLE_OP (td
, MINT_LDIND_U4_CHECK
);
3429 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3430 BARRIER_IF_VOLATILE (td
);
3433 CHECK_STACK (td
, 1);
3434 SIMPLE_OP (td
, MINT_LDIND_I8_CHECK
);
3435 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
3436 BARRIER_IF_VOLATILE (td
);
3439 CHECK_STACK (td
, 1);
3440 SIMPLE_OP (td
, MINT_LDIND_REF_CHECK
);
3441 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3442 BARRIER_IF_VOLATILE (td
);
3445 CHECK_STACK (td
, 1);
3446 SIMPLE_OP (td
, MINT_LDIND_R4_CHECK
);
3447 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
3448 BARRIER_IF_VOLATILE (td
);
3451 CHECK_STACK (td
, 1);
3452 SIMPLE_OP (td
, MINT_LDIND_R8_CHECK
);
3453 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
3454 BARRIER_IF_VOLATILE (td
);
3457 CHECK_STACK (td
, 1);
3458 SIMPLE_OP (td
, MINT_LDIND_REF_CHECK
);
3459 BARRIER_IF_VOLATILE (td
);
3460 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
3463 CHECK_STACK (td
, 2);
3464 BARRIER_IF_VOLATILE (td
);
3465 SIMPLE_OP (td
, MINT_STIND_REF
);
3469 CHECK_STACK (td
, 2);
3470 BARRIER_IF_VOLATILE (td
);
3471 SIMPLE_OP (td
, MINT_STIND_I1
);
3475 CHECK_STACK (td
, 2);
3476 BARRIER_IF_VOLATILE (td
);
3477 SIMPLE_OP (td
, MINT_STIND_I2
);
3481 CHECK_STACK (td
, 2);
3482 BARRIER_IF_VOLATILE (td
);
3483 SIMPLE_OP (td
, MINT_STIND_I4
);
3487 CHECK_STACK (td
, 2);
3488 BARRIER_IF_VOLATILE (td
);
3489 SIMPLE_OP (td
, MINT_STIND_I
);
3493 CHECK_STACK (td
, 2);
3494 BARRIER_IF_VOLATILE (td
);
3495 SIMPLE_OP (td
, MINT_STIND_I8
);
3499 CHECK_STACK (td
, 2);
3500 BARRIER_IF_VOLATILE (td
);
3501 SIMPLE_OP (td
, MINT_STIND_R4
);
3505 CHECK_STACK (td
, 2);
3506 BARRIER_IF_VOLATILE (td
);
3507 SIMPLE_OP (td
, MINT_STIND_R8
);
3511 binary_arith_op(td
, MINT_ADD_I4
);
3515 binary_arith_op(td
, MINT_SUB_I4
);
3519 binary_arith_op(td
, MINT_MUL_I4
);
3523 binary_arith_op(td
, MINT_DIV_I4
);
3527 binary_arith_op(td
, MINT_DIV_UN_I4
);
3531 binary_arith_op (td
, MINT_REM_I4
);
3535 binary_arith_op (td
, MINT_REM_UN_I4
);
3539 binary_arith_op (td
, MINT_AND_I4
);
3543 binary_arith_op (td
, MINT_OR_I4
);
3547 binary_arith_op (td
, MINT_XOR_I4
);
3551 shift_op (td
, MINT_SHL_I4
);
3555 shift_op (td
, MINT_SHR_I4
);
3559 shift_op (td
, MINT_SHR_UN_I4
);
3563 unary_arith_op (td
, MINT_NEG_I4
);
3567 unary_arith_op (td
, MINT_NOT_I4
);
3571 CHECK_STACK (td
, 1);
3572 switch (td
->sp
[-1].type
) {
3574 interp_add_ins (td
, MINT_CONV_U1_R4
);
3577 interp_add_ins (td
, MINT_CONV_U1_R8
);
3580 interp_add_ins (td
, MINT_CONV_U1_I4
);
3583 interp_add_ins (td
, MINT_CONV_U1_I8
);
3586 g_assert_not_reached ();
3589 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3592 CHECK_STACK (td
, 1);
3593 switch (td
->sp
[-1].type
) {
3595 interp_add_ins (td
, MINT_CONV_I1_R4
);
3598 interp_add_ins (td
, MINT_CONV_I1_R8
);
3601 interp_add_ins (td
, MINT_CONV_I1_I4
);
3604 interp_add_ins (td
, MINT_CONV_I1_I8
);
3607 g_assert_not_reached ();
3610 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3613 CHECK_STACK (td
, 1);
3614 switch (td
->sp
[-1].type
) {
3616 interp_add_ins (td
, MINT_CONV_U2_R4
);
3619 interp_add_ins (td
, MINT_CONV_U2_R8
);
3622 interp_add_ins (td
, MINT_CONV_U2_I4
);
3625 interp_add_ins (td
, MINT_CONV_U2_I8
);
3628 g_assert_not_reached ();
3631 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3634 CHECK_STACK (td
, 1);
3635 switch (td
->sp
[-1].type
) {
3637 interp_add_ins (td
, MINT_CONV_I2_R4
);
3640 interp_add_ins (td
, MINT_CONV_I2_R8
);
3643 interp_add_ins (td
, MINT_CONV_I2_I4
);
3646 interp_add_ins (td
, MINT_CONV_I2_I8
);
3649 g_assert_not_reached ();
3652 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3655 CHECK_STACK (td
, 1);
3656 switch (td
->sp
[-1].type
) {
3658 #if SIZEOF_VOID_P == 4
3659 interp_add_ins (td
, MINT_CONV_U4_R8
);
3661 interp_add_ins (td
, MINT_CONV_U8_R8
);
3665 #if SIZEOF_VOID_P == 8
3666 interp_add_ins (td
, MINT_CONV_U8_I4
);
3670 #if SIZEOF_VOID_P == 4
3671 interp_add_ins (td
, MINT_CONV_U4_I8
);
3678 g_assert_not_reached ();
3681 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3684 CHECK_STACK (td
, 1);
3685 switch (td
->sp
[-1].type
) {
3687 #if SIZEOF_VOID_P == 8
3688 interp_add_ins (td
, MINT_CONV_I8_R8
);
3690 interp_add_ins (td
, MINT_CONV_I4_R8
);
3694 #if SIZEOF_VOID_P == 8
3695 interp_add_ins (td
, MINT_CONV_I8_I4
);
3703 #if SIZEOF_VOID_P == 4
3704 interp_add_ins (td
, MINT_CONV_I4_I8
);
3708 g_assert_not_reached ();
3711 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
3714 CHECK_STACK (td
, 1);
3715 switch (td
->sp
[-1].type
) {
3717 interp_add_ins (td
, MINT_CONV_U4_R4
);
3720 interp_add_ins (td
, MINT_CONV_U4_R8
);
3725 interp_add_ins (td
, MINT_CONV_U4_I8
);
3728 #if SIZEOF_VOID_P == 8
3729 interp_add_ins (td
, MINT_CONV_U4_I8
);
3733 g_assert_not_reached ();
3736 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3739 CHECK_STACK (td
, 1);
3740 switch (td
->sp
[-1].type
) {
3742 interp_add_ins (td
, MINT_CONV_I4_R4
);
3745 interp_add_ins (td
, MINT_CONV_I4_R8
);
3750 interp_add_ins (td
, MINT_CONV_I4_I8
);
3753 #if SIZEOF_VOID_P == 8
3754 interp_add_ins (td
, MINT_CONV_I4_I8
);
3758 g_assert_not_reached ();
3761 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
3764 CHECK_STACK (td
, 1);
3765 switch (td
->sp
[-1].type
) {
3767 interp_add_ins (td
, MINT_CONV_I8_R4
);
3770 interp_add_ins (td
, MINT_CONV_I8_R8
);
3772 case STACK_TYPE_I4
: {
3773 if (interp_ins_is_ldc (td
->last_ins
) && !td
->is_bb_start
[in_offset
]) {
3774 gint64 ct
= interp_ldc_i4_get_const (td
->last_ins
);
3775 interp_remove_ins (td
, td
->last_ins
);
3777 interp_add_ins (td
, MINT_LDC_I8
);
3778 WRITE64_INS (td
->last_ins
, 0, &ct
);
3780 interp_add_ins (td
, MINT_CONV_I8_I4
);
3787 #if SIZEOF_VOID_P == 4
3788 interp_add_ins (td
, MINT_CONV_I8_I4
);
3792 g_assert_not_reached ();
3795 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
3798 CHECK_STACK (td
, 1);
3799 switch (td
->sp
[-1].type
) {
3801 interp_add_ins (td
, MINT_CONV_R4_R8
);
3804 interp_add_ins (td
, MINT_CONV_R4_I8
);
3807 interp_add_ins (td
, MINT_CONV_R4_I4
);
3813 g_assert_not_reached ();
3816 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
3819 CHECK_STACK (td
, 1);
3820 switch (td
->sp
[-1].type
) {
3822 interp_add_ins (td
, MINT_CONV_R8_I4
);
3825 interp_add_ins (td
, MINT_CONV_R8_I8
);
3828 interp_add_ins (td
, MINT_CONV_R8_R4
);
3833 g_assert_not_reached ();
3836 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
3839 CHECK_STACK (td
, 1);
3840 switch (td
->sp
[-1].type
) {
3842 if (interp_ins_is_ldc (td
->last_ins
) && !td
->is_bb_start
[in_offset
]) {
3843 gint64 ct
= (guint32
)interp_ldc_i4_get_const (td
->last_ins
);
3844 interp_remove_ins (td
, td
->last_ins
);
3846 interp_add_ins (td
, MINT_LDC_I8
);
3847 WRITE64_INS (td
->last_ins
, 0, &ct
);
3849 interp_add_ins (td
, MINT_CONV_U8_I4
);
3855 interp_add_ins (td
, MINT_CONV_U8_R4
);
3858 interp_add_ins (td
, MINT_CONV_U8_R8
);
3861 #if SIZEOF_VOID_P == 4
3862 interp_add_ins (td
, MINT_CONV_U8_I4
);
3866 g_assert_not_reached ();
3869 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
3872 CHECK_STACK (td
, 2);
3874 token
= read32 (td
->ip
+ 1);
3875 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
3876 goto_if_nok (error
, exit
);
3878 if (m_class_is_valuetype (klass
)) {
3879 int mt
= mint_type (m_class_get_byval_arg (klass
));
3880 interp_add_ins (td
, (mt
== MINT_TYPE_VT
) ? MINT_CPOBJ_VT
: MINT_CPOBJ
);
3881 td
->last_ins
->data
[0] = get_data_item_index(td
, klass
);
3883 interp_add_ins (td
, MINT_LDIND_REF
);
3884 interp_add_ins (td
, MINT_STIND_REF
);
3891 CHECK_STACK (td
, 1);
3893 token
= read32 (td
->ip
+ 1);
3895 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
3896 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
3898 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
3899 goto_if_nok (error
, exit
);
3902 interp_emit_ldobj (td
, klass
);
3905 BARRIER_IF_VOLATILE (td
);
3909 token
= mono_metadata_token_index (read32 (td
->ip
+ 1));
3911 if (method
->wrapper_type
== MONO_WRAPPER_NONE
) {
3912 MonoString
*s
= mono_ldstr_checked (domain
, image
, token
, error
);
3913 goto_if_nok (error
, exit
);
3914 /* GC won't scan code stream, but reference is held by metadata
3915 * machinery so we are good here */
3916 interp_add_ins (td
, MINT_LDSTR
);
3917 td
->last_ins
->data
[0] = get_data_item_index (td
, s
);
3919 /* defer allocation to execution-time */
3920 interp_add_ins (td
, MINT_LDSTR_TOKEN
);
3921 td
->last_ins
->data
[0] = get_data_item_index (td
, GUINT_TO_POINTER (token
));
3923 PUSH_TYPE(td
, STACK_TYPE_O
, mono_defaults
.string_class
);
3928 MonoMethodSignature
*csignature
;
3929 guint32 vt_stack_used
= 0;
3930 guint32 vt_res_size
= 0;
3933 token
= read32 (td
->ip
);
3936 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
3937 m
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
3939 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
3940 goto_if_nok (error
, exit
);
3943 csignature
= mono_method_signature_internal (m
);
3946 if (!mono_class_init_internal (klass
)) {
3947 mono_error_set_for_class_failure (error
, klass
);
3948 goto_if_nok (error
, exit
);
3951 if (mono_class_get_flags (klass
) & TYPE_ATTRIBUTE_ABSTRACT
) {
3952 char* full_name
= mono_type_get_full_name (klass
);
3953 mono_error_set_member_access (error
, "Cannot create an abstract class: %s", full_name
);
3955 goto_if_nok (error
, exit
);
3958 td
->sp
-= csignature
->param_count
;
3959 if (mono_class_is_magic_int (klass
) || mono_class_is_magic_float (klass
)) {
3960 #if SIZEOF_VOID_P == 8
3961 if (mono_class_is_magic_int (klass
) && td
->sp
[0].type
== STACK_TYPE_I4
)
3962 interp_add_ins (td
, MINT_CONV_I8_I4
);
3963 else if (mono_class_is_magic_float (klass
) && td
->sp
[0].type
== STACK_TYPE_R4
)
3964 interp_add_ins (td
, MINT_CONV_R8_R4
);
3966 interp_add_ins (td
, MINT_NEWOBJ_MAGIC
);
3967 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3968 goto_if_nok (error
, exit
);
3970 PUSH_TYPE (td
, stack_type
[mint_type (m_class_get_byval_arg (klass
))], klass
);
3972 if (m_class_get_parent (klass
) == mono_defaults
.array_class
) {
3973 interp_add_ins (td
, MINT_NEWOBJ_ARRAY
);
3974 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3975 td
->last_ins
->data
[1] = csignature
->param_count
;
3976 } else if (m_class_get_image (klass
) == mono_defaults
.corlib
&&
3977 !strcmp (m_class_get_name (m
->klass
), "ByReference`1") &&
3978 !strcmp (m
->name
, ".ctor")) {
3979 /* public ByReference(ref T value) */
3980 g_assert (csignature
->hasthis
&& csignature
->param_count
== 1);
3981 interp_add_ins (td
, MINT_INTRINS_BYREFERENCE_CTOR
);
3982 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3983 } else if (klass
!= mono_defaults
.string_class
&&
3984 !mono_class_is_marshalbyref (klass
) &&
3985 !mono_class_has_finalizer (klass
) &&
3986 !m_class_has_weak_fields (klass
)) {
3987 if (!m_class_is_valuetype (klass
))
3988 interp_add_ins (td
, MINT_NEWOBJ_FAST
);
3989 else if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
)
3990 interp_add_ins (td
, MINT_NEWOBJ_VTST_FAST
);
3992 interp_add_ins (td
, MINT_NEWOBJ_VT_FAST
);
3994 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
3995 td
->last_ins
->data
[1] = csignature
->param_count
;
3997 if (!m_class_is_valuetype (klass
)) {
3998 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
3999 goto_if_nok (error
, exit
);
4000 td
->last_ins
->data
[2] = get_data_item_index (td
, vtable
);
4001 } else if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4002 td
->last_ins
->data
[2] = mono_class_value_size (klass
, NULL
);
4005 interp_add_ins (td
, MINT_NEWOBJ
);
4006 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
4008 goto_if_nok (error
, exit
);
4010 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4011 vt_res_size
= mono_class_value_size (klass
, NULL
);
4012 PUSH_VT (td
, vt_res_size
);
4014 for (i
= 0; i
< csignature
->param_count
; ++i
) {
4015 int mt
= mint_type(csignature
->params
[i
]);
4016 if (mt
== MINT_TYPE_VT
) {
4017 MonoClass
*k
= mono_class_from_mono_type_internal (csignature
->params
[i
]);
4018 gint32 size
= mono_class_value_size (k
, NULL
);
4019 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4020 vt_stack_used
+= size
;
4023 if (vt_stack_used
!= 0 || vt_res_size
!= 0) {
4024 interp_add_ins (td
, MINT_VTRESULT
);
4025 td
->last_ins
->data
[0] = vt_res_size
;
4026 WRITE32_INS (td
->last_ins
, 1, &vt_stack_used
);
4027 td
->vt_sp
-= vt_stack_used
;
4029 PUSH_TYPE (td
, stack_type
[mint_type (m_class_get_byval_arg (klass
))], klass
);
4035 gboolean isinst_instr
= *td
->ip
== CEE_ISINST
;
4036 CHECK_STACK (td
, 1);
4037 token
= read32 (td
->ip
+ 1);
4038 klass
= mini_get_class (method
, token
, generic_context
);
4039 CHECK_TYPELOAD (klass
);
4040 interp_handle_isinst (td
, klass
, isinst_instr
);
4042 td
->sp
[-1].klass
= klass
;
4046 switch (td
->sp
[-1].type
) {
4050 interp_add_ins (td
, MINT_CONV_R_UN_I8
);
4053 interp_add_ins (td
, MINT_CONV_R_UN_I4
);
4056 g_assert_not_reached ();
4058 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4062 CHECK_STACK (td
, 1);
4063 token
= read32 (td
->ip
+ 1);
4065 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4066 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4068 klass
= mono_class_get_and_inflate_typespec_checked (image
, token
, generic_context
, error
);
4069 goto_if_nok (error
, exit
);
4072 if (mono_class_is_nullable (klass
)) {
4073 MonoMethod
*target_method
;
4074 if (m_class_is_enumtype (mono_class_get_nullable_param_internal (klass
)))
4075 target_method
= mono_class_get_method_from_name_checked (klass
, "UnboxExact", 1, 0, error
);
4077 target_method
= mono_class_get_method_from_name_checked (klass
, "Unbox", 1, 0, error
);
4078 goto_if_nok (error
, exit
);
4079 /* td->ip is incremented by interp_transform_call */
4080 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
))
4083 * CEE_UNBOX needs to push address of vtype while Nullable.Unbox returns the value type
4084 * We create a local variable in the frame so that we can fetch its address.
4086 int local_offset
= create_interp_local (td
, m_class_get_byval_arg (klass
));
4087 store_local_general (td
, local_offset
, m_class_get_byval_arg (klass
));
4088 interp_add_ins (td
, MINT_LDLOCA_S
);
4089 td
->last_ins
->data
[0] = local_offset
;
4090 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
4092 interp_add_ins (td
, MINT_UNBOX
);
4093 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4094 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_MP
);
4099 CHECK_STACK (td
, 1);
4100 token
= read32 (td
->ip
+ 1);
4102 klass
= mini_get_class (method
, token
, generic_context
);
4103 CHECK_TYPELOAD (klass
);
4105 if (mini_type_is_reference (m_class_get_byval_arg (klass
))) {
4106 int mt
= mint_type (m_class_get_byval_arg (klass
));
4107 interp_handle_isinst (td
, klass
, FALSE
);
4108 SET_TYPE (td
->sp
- 1, stack_type
[mt
], klass
);
4109 } else if (mono_class_is_nullable (klass
)) {
4110 MonoMethod
*target_method
;
4111 if (m_class_is_enumtype (mono_class_get_nullable_param_internal (klass
)))
4112 target_method
= mono_class_get_method_from_name_checked (klass
, "UnboxExact", 1, 0, error
);
4114 target_method
= mono_class_get_method_from_name_checked (klass
, "Unbox", 1, 0, error
);
4115 goto_if_nok (error
, exit
);
4116 /* td->ip is incremented by interp_transform_call */
4117 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
))
4120 interp_add_ins (td
, MINT_UNBOX
);
4121 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4123 interp_emit_ldobj (td
, klass
);
4131 CHECK_STACK (td
, 1);
4132 SIMPLE_OP (td
, MINT_THROW
);
4136 CHECK_STACK (td
, 1);
4137 token
= read32 (td
->ip
+ 1);
4138 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4139 goto_if_nok (error
, exit
);
4140 MonoType
*ftype
= mono_field_get_type_internal (field
);
4141 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4142 mono_class_init_internal (klass
);
4143 #ifndef DISABLE_REMOTING
4144 if (m_class_get_marshalbyref (klass
) || mono_class_is_contextbound (klass
) || klass
== mono_defaults
.marshalbyrefobject_class
) {
4145 g_assert (!is_static
);
4146 int offset
= m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4148 interp_add_ins (td
, MINT_MONO_LDPTR
);
4149 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4150 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
4151 interp_add_ins (td
, MINT_MONO_LDPTR
);
4152 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4153 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
4154 interp_add_ins (td
, MINT_LDC_I4
);
4155 WRITE32_INS (td
->last_ins
, 0, &offset
);
4156 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I4
);
4157 #if SIZEOF_VOID_P == 8
4158 interp_add_ins (td
, MINT_CONV_I8_I4
);
4161 MonoMethod
*wrapper
= mono_marshal_get_ldflda_wrapper (field
->type
);
4162 /* td->ip is incremented by interp_transform_call */
4163 if (!interp_transform_call (td
, method
, wrapper
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
))
4169 interp_add_ins (td
, MINT_POP
);
4170 td
->last_ins
->data
[0] = 0;
4171 interp_add_ins (td
, MINT_LDSFLDA
);
4172 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4174 if ((td
->sp
- 1)->type
== STACK_TYPE_O
) {
4175 interp_add_ins (td
, MINT_LDFLDA
);
4177 g_assert ((td
->sp
-1)->type
== STACK_TYPE_MP
);
4178 interp_add_ins (td
, MINT_LDFLDA_UNSAFE
);
4180 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4184 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
4188 CHECK_STACK (td
, 1);
4189 token
= read32 (td
->ip
+ 1);
4190 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4191 goto_if_nok (error
, exit
);
4192 MonoType
*ftype
= mono_field_get_type_internal (field
);
4193 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4194 mono_class_init_internal (klass
);
4196 MonoClass
*field_klass
= mono_class_from_mono_type_internal (ftype
);
4197 mt
= mint_type (m_class_get_byval_arg (field_klass
));
4198 #ifndef DISABLE_REMOTING
4199 if (m_class_get_marshalbyref (klass
)) {
4200 g_assert (!is_static
);
4201 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDRMFLD_VT
: MINT_LDRMFLD
);
4202 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4207 interp_add_ins (td
, MINT_POP
);
4208 td
->last_ins
->data
[0] = 0;
4209 interp_emit_sfld_access (td
, field
, field_klass
, mt
, TRUE
, error
);
4210 goto_if_nok (error
, exit
);
4212 int opcode
= MINT_LDFLD_I1
+ mt
- MINT_TYPE_I1
;
4213 #ifdef NO_UNALIGNED_ACCESS
4214 if ((mt
== MINT_TYPE_I8
|| mt
== MINT_TYPE_R8
) && field
->offset
% SIZEOF_VOID_P
!= 0)
4215 opcode
= get_unaligned_opcode (opcode
);
4217 interp_add_ins (td
, opcode
);
4218 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4219 if (mt
== MINT_TYPE_VT
) {
4220 int size
= mono_class_value_size (field_klass
, NULL
);
4221 WRITE32_INS (td
->last_ins
, 1, &size
);
4225 if (mt
== MINT_TYPE_VT
) {
4226 int size
= mono_class_value_size (field_klass
, NULL
);
4229 if (td
->sp
[-1].type
== STACK_TYPE_VT
) {
4230 int size
= mono_class_value_size (klass
, NULL
);
4231 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4232 int field_vt_size
= 0;
4233 if (mt
== MINT_TYPE_VT
) {
4235 * Pop the loaded field from the vtstack (it will still be present
4236 * at the same vtstack address) and we will load it in place of the
4237 * containing valuetype with the second MINT_VTRESULT.
4239 field_vt_size
= mono_class_value_size (field_klass
, NULL
);
4240 field_vt_size
= ALIGN_TO (field_vt_size
, MINT_VT_ALIGNMENT
);
4241 interp_add_ins (td
, MINT_VTRESULT
);
4242 td
->last_ins
->data
[0] = 0;
4243 WRITE32_INS (td
->last_ins
, 1, &field_vt_size
);
4246 interp_add_ins (td
, MINT_VTRESULT
);
4247 td
->last_ins
->data
[0] = field_vt_size
;
4248 WRITE32_INS (td
->last_ins
, 1, &size
);
4251 SET_TYPE (td
->sp
- 1, stack_type
[mt
], field_klass
);
4252 BARRIER_IF_VOLATILE (td
);
4256 CHECK_STACK (td
, 2);
4257 token
= read32 (td
->ip
+ 1);
4258 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4259 goto_if_nok (error
, exit
);
4260 MonoType
*ftype
= mono_field_get_type_internal (field
);
4261 gboolean is_static
= !!(ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
);
4262 MonoClass
*field_klass
= mono_class_from_mono_type_internal (ftype
);
4263 mono_class_init_internal (klass
);
4264 mt
= mint_type (ftype
);
4266 BARRIER_IF_VOLATILE (td
);
4268 #ifndef DISABLE_REMOTING
4269 if (m_class_get_marshalbyref (klass
)) {
4270 g_assert (!is_static
);
4271 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_STRMFLD_VT
: MINT_STRMFLD
);
4272 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4277 interp_add_ins (td
, MINT_POP
);
4278 td
->last_ins
->data
[0] = 1;
4279 interp_emit_sfld_access (td
, field
, field_klass
, mt
, FALSE
, error
);
4280 goto_if_nok (error
, exit
);
4282 /* the vtable of the field might not be initialized at this point */
4283 mono_class_vtable_checked (domain
, field_klass
, error
);
4284 goto_if_nok (error
, exit
);
4286 int opcode
= MINT_STFLD_I1
+ mt
- MINT_TYPE_I1
;
4287 #ifdef NO_UNALIGNED_ACCESS
4288 if ((mt
== MINT_TYPE_I8
|| mt
== MINT_TYPE_R8
) && field
->offset
% SIZEOF_VOID_P
!= 0)
4289 opcode
= get_unaligned_opcode (opcode
);
4291 interp_add_ins (td
, opcode
);
4292 td
->last_ins
->data
[0] = m_class_is_valuetype (klass
) ? field
->offset
- MONO_ABI_SIZEOF (MonoObject
) : field
->offset
;
4293 if (mt
== MINT_TYPE_VT
) {
4294 /* the vtable of the field might not be initialized at this point */
4295 mono_class_vtable_checked (domain
, field_klass
, error
);
4296 goto_if_nok (error
, exit
);
4298 td
->last_ins
->data
[1] = get_data_item_index (td
, field_klass
);
4302 if (mt
== MINT_TYPE_VT
) {
4303 int size
= mono_class_value_size (field_klass
, NULL
);
4311 token
= read32 (td
->ip
+ 1);
4312 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4313 goto_if_nok (error
, exit
);
4314 mono_field_get_type_internal (field
);
4315 interp_add_ins (td
, MINT_LDSFLDA
);
4316 td
->last_ins
->data
[0] = get_data_item_index (td
, field
);
4318 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
4322 token
= read32 (td
->ip
+ 1);
4323 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4324 goto_if_nok (error
, exit
);
4325 MonoType
*ftype
= mono_field_get_type_internal (field
);
4326 mt
= mint_type (ftype
);
4327 klass
= mono_class_from_mono_type_internal (ftype
);
4329 interp_emit_sfld_access (td
, field
, klass
, mt
, TRUE
, error
);
4330 goto_if_nok (error
, exit
);
4332 if (mt
== MINT_TYPE_VT
) {
4333 int size
= mono_class_value_size (klass
, NULL
);
4337 PUSH_TYPE(td
, stack_type
[mt
], klass
);
4341 CHECK_STACK (td
, 1);
4342 token
= read32 (td
->ip
+ 1);
4343 field
= interp_field_from_token (method
, token
, &klass
, generic_context
, error
);
4344 goto_if_nok (error
, exit
);
4345 MonoType
*ftype
= mono_field_get_type_internal (field
);
4346 mt
= mint_type (ftype
);
4348 /* the vtable of the field might not be initialized at this point */
4349 MonoClass
*fld_klass
= mono_class_from_mono_type_internal (ftype
);
4350 mono_class_vtable_checked (domain
, fld_klass
, error
);
4351 goto_if_nok (error
, exit
);
4353 interp_emit_sfld_access (td
, field
, fld_klass
, mt
, FALSE
, error
);
4354 goto_if_nok (error
, exit
);
4356 if (mt
== MINT_TYPE_VT
) {
4357 int size
= mono_class_value_size (fld_klass
, NULL
);
4365 token
= read32 (td
->ip
+ 1);
4367 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4368 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4370 klass
= mini_get_class (method
, token
, generic_context
);
4371 CHECK_TYPELOAD (klass
);
4373 BARRIER_IF_VOLATILE (td
);
4375 interp_emit_stobj (td
, klass
);
4380 case CEE_CONV_OVF_I_UN
:
4381 case CEE_CONV_OVF_U_UN
:
4382 CHECK_STACK (td
, 1);
4383 switch (td
->sp
[-1].type
) {
4385 #if SIZEOF_VOID_P == 8
4386 interp_add_ins (td
, MINT_CONV_OVF_I8_UN_R8
);
4388 interp_add_ins (td
, MINT_CONV_OVF_I4_UN_R8
);
4392 #if SIZEOF_VOID_P == 4
4393 interp_add_ins (td
, MINT_CONV_OVF_I4_UN_I8
);
4397 #if SIZEOF_VOID_P == 8
4398 interp_add_ins (td
, MINT_CONV_I8_U4
);
4399 #elif SIZEOF_VOID_P == 4
4400 if (*td
->ip
== CEE_CONV_OVF_I_UN
)
4401 interp_add_ins (td
, MINT_CONV_OVF_I4_U4
);
4405 g_assert_not_reached ();
4408 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4411 case CEE_CONV_OVF_I8_UN
:
4412 case CEE_CONV_OVF_U8_UN
:
4413 CHECK_STACK (td
, 1);
4414 switch (td
->sp
[-1].type
) {
4416 interp_add_ins (td
, MINT_CONV_OVF_I8_UN_R8
);
4419 if (*td
->ip
== CEE_CONV_OVF_I8_UN
)
4420 interp_add_ins (td
, MINT_CONV_OVF_I8_U8
);
4423 interp_add_ins (td
, MINT_CONV_I8_U4
);
4426 g_assert_not_reached ();
4429 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4434 CHECK_STACK (td
, 1);
4435 token
= read32 (td
->ip
+ 1);
4436 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4437 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4439 klass
= mini_get_class (method
, token
, generic_context
);
4440 CHECK_TYPELOAD (klass
);
4442 if (mono_class_is_nullable (klass
)) {
4443 MonoMethod
*target_method
= mono_class_get_method_from_name_checked (klass
, "Box", 1, 0, error
);
4444 goto_if_nok (error
, exit
);
4445 /* td->ip is incremented by interp_transform_call */
4446 if (!interp_transform_call (td
, method
, target_method
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
))
4448 } else if (!m_class_is_valuetype (klass
)) {
4449 /* already boxed, do nothing. */
4452 if (G_UNLIKELY (m_class_is_byreflike (klass
))) {
4453 mono_error_set_bad_image (error
, image
, "Cannot box IsByRefLike type '%s.%s'", m_class_get_name_space (klass
), m_class_get_name (klass
));
4456 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
) {
4457 size
= mono_class_value_size (klass
, NULL
);
4458 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
4460 } else if (td
->sp
[-1].type
== STACK_TYPE_R8
&& m_class_get_byval_arg (klass
)->type
== MONO_TYPE_R4
) {
4461 interp_add_ins (td
, MINT_CONV_R4_R8
);
4463 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
4464 goto_if_nok (error
, exit
);
4466 if (mint_type (m_class_get_byval_arg (klass
)) == MINT_TYPE_VT
)
4467 interp_add_ins (td
, MINT_BOX_VT
);
4469 interp_add_ins (td
, MINT_BOX
);
4470 td
->last_ins
->data
[0] = get_data_item_index (td
, vtable
);
4471 td
->last_ins
->data
[1] = 0;
4472 SET_TYPE(td
->sp
- 1, STACK_TYPE_O
, klass
);
4479 CHECK_STACK (td
, 1);
4480 token
= read32 (td
->ip
+ 1);
4482 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4483 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
4485 klass
= mini_get_class (method
, token
, generic_context
);
4486 CHECK_TYPELOAD (klass
);
4488 unsigned char lentype
= (td
->sp
- 1)->type
;
4489 if (lentype
== STACK_TYPE_I8
) {
4490 /* mimic mini behaviour */
4491 interp_add_ins (td
, MINT_CONV_OVF_U4_I8
);
4493 g_assert (lentype
== STACK_TYPE_I4
);
4494 interp_add_ins (td
, MINT_CONV_OVF_U4_I4
);
4496 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
4497 interp_add_ins (td
, MINT_NEWARR
);
4498 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4499 SET_TYPE (td
->sp
- 1, STACK_TYPE_O
, klass
);
4504 CHECK_STACK (td
, 1);
4505 SIMPLE_OP (td
, MINT_LDLEN
);
4506 #ifdef MONO_BIG_ARRAYS
4507 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I8
);
4509 SET_SIMPLE_TYPE (td
->sp
- 1, STACK_TYPE_I4
);
4513 CHECK_STACK (td
, 2);
4515 token
= read32 (td
->ip
+ 1);
4517 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4518 klass
= (MonoClass
*) mono_method_get_wrapper_data (method
, token
);
4520 klass
= mini_get_class (method
, token
, generic_context
);
4522 CHECK_TYPELOAD (klass
);
4524 if (!m_class_is_valuetype (klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
&& !readonly
) {
4526 * Check the class for failures before the type check, which can
4527 * throw other exceptions.
4529 mono_class_setup_vtable (klass
);
4530 CHECK_TYPELOAD (klass
);
4531 interp_add_ins (td
, MINT_LDELEMA_TC
);
4533 interp_add_ins (td
, MINT_LDELEMA
);
4535 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4536 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
4537 td
->last_ins
->data
[1] = 2;
4542 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
4545 CHECK_STACK (td
, 2);
4547 SIMPLE_OP (td
, MINT_LDELEM_I1
);
4549 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4552 CHECK_STACK (td
, 2);
4554 SIMPLE_OP (td
, MINT_LDELEM_U1
);
4556 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4559 CHECK_STACK (td
, 2);
4561 SIMPLE_OP (td
, MINT_LDELEM_I2
);
4563 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4566 CHECK_STACK (td
, 2);
4568 SIMPLE_OP (td
, MINT_LDELEM_U2
);
4570 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4573 CHECK_STACK (td
, 2);
4575 SIMPLE_OP (td
, MINT_LDELEM_I4
);
4577 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4580 CHECK_STACK (td
, 2);
4582 SIMPLE_OP (td
, MINT_LDELEM_U4
);
4584 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4587 CHECK_STACK (td
, 2);
4589 SIMPLE_OP (td
, MINT_LDELEM_I8
);
4591 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4594 CHECK_STACK (td
, 2);
4596 SIMPLE_OP (td
, MINT_LDELEM_I
);
4598 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I
);
4601 CHECK_STACK (td
, 2);
4603 SIMPLE_OP (td
, MINT_LDELEM_R4
);
4605 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
4608 CHECK_STACK (td
, 2);
4610 SIMPLE_OP (td
, MINT_LDELEM_R8
);
4612 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4614 case CEE_LDELEM_REF
:
4615 CHECK_STACK (td
, 2);
4617 SIMPLE_OP (td
, MINT_LDELEM_REF
);
4619 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
4622 CHECK_STACK (td
, 2);
4623 token
= read32 (td
->ip
+ 1);
4624 klass
= mini_get_class (method
, token
, generic_context
);
4625 CHECK_TYPELOAD (klass
);
4626 switch (mint_type (m_class_get_byval_arg (klass
))) {
4629 SIMPLE_OP (td
, MINT_LDELEM_I1
);
4631 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4635 SIMPLE_OP (td
, MINT_LDELEM_U1
);
4637 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4641 SIMPLE_OP (td
, MINT_LDELEM_U2
);
4643 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4647 SIMPLE_OP (td
, MINT_LDELEM_I2
);
4649 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4653 SIMPLE_OP (td
, MINT_LDELEM_I4
);
4655 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4659 SIMPLE_OP (td
, MINT_LDELEM_I8
);
4661 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
4665 SIMPLE_OP (td
, MINT_LDELEM_R4
);
4667 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R4
);
4671 SIMPLE_OP (td
, MINT_LDELEM_R8
);
4673 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_R8
);
4677 SIMPLE_OP (td
, MINT_LDELEM_REF
);
4679 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_O
);
4681 case MINT_TYPE_VT
: {
4682 int size
= mono_class_value_size (klass
, NULL
);
4684 SIMPLE_OP (td
, MINT_LDELEM_VT
);
4685 WRITE32_INS (td
->last_ins
, 0, &size
);
4687 SET_TYPE (td
->sp
- 1, STACK_TYPE_VT
, klass
);
4692 GString
*res
= g_string_new ("");
4693 mono_type_get_desc (res
, m_class_get_byval_arg (klass
), TRUE
);
4694 g_print ("LDELEM: %s -> %d (%s)\n", m_class_get_name (klass
), mint_type (m_class_get_byval_arg (klass
)), res
->str
);
4695 g_string_free (res
, TRUE
);
4703 CHECK_STACK (td
, 3);
4705 SIMPLE_OP (td
, MINT_STELEM_I
);
4709 CHECK_STACK (td
, 3);
4711 SIMPLE_OP (td
, MINT_STELEM_I1
);
4715 CHECK_STACK (td
, 3);
4717 SIMPLE_OP (td
, MINT_STELEM_I2
);
4721 CHECK_STACK (td
, 3);
4723 SIMPLE_OP (td
, MINT_STELEM_I4
);
4727 CHECK_STACK (td
, 3);
4729 SIMPLE_OP (td
, MINT_STELEM_I8
);
4733 CHECK_STACK (td
, 3);
4735 SIMPLE_OP (td
, MINT_STELEM_R4
);
4739 CHECK_STACK (td
, 3);
4741 SIMPLE_OP (td
, MINT_STELEM_R8
);
4744 case CEE_STELEM_REF
:
4745 CHECK_STACK (td
, 3);
4747 SIMPLE_OP (td
, MINT_STELEM_REF
);
4751 CHECK_STACK (td
, 3);
4753 token
= read32 (td
->ip
+ 1);
4754 klass
= mini_get_class (method
, token
, generic_context
);
4755 CHECK_TYPELOAD (klass
);
4756 switch (mint_type (m_class_get_byval_arg (klass
))) {
4758 SIMPLE_OP (td
, MINT_STELEM_I1
);
4761 SIMPLE_OP (td
, MINT_STELEM_U1
);
4764 SIMPLE_OP (td
, MINT_STELEM_I2
);
4767 SIMPLE_OP (td
, MINT_STELEM_U2
);
4770 SIMPLE_OP (td
, MINT_STELEM_I4
);
4773 SIMPLE_OP (td
, MINT_STELEM_I8
);
4776 SIMPLE_OP (td
, MINT_STELEM_R4
);
4779 SIMPLE_OP (td
, MINT_STELEM_R8
);
4782 SIMPLE_OP (td
, MINT_STELEM_REF
);
4784 case MINT_TYPE_VT
: {
4785 int size
= mono_class_value_size (klass
, NULL
);
4786 SIMPLE_OP (td
, MINT_STELEM_VT
);
4787 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4788 WRITE32_INS (td
->last_ins
, 1, &size
);
4793 GString
*res
= g_string_new ("");
4794 mono_type_get_desc (res
, m_class_get_byval_arg (klass
), TRUE
);
4795 g_print ("STELEM: %s -> %d (%s)\n", m_class_get_name (klass
), mint_type (m_class_get_byval_arg (klass
)), res
->str
);
4796 g_string_free (res
, TRUE
);
4805 case CEE_CONV_OVF_U1
:
4807 case CEE_CONV_OVF_I8
:
4809 #if SIZEOF_VOID_P == 8
4810 case CEE_CONV_OVF_U
:
4814 CHECK_STACK (td
, 1);
4815 SIMPLE_OP (td
, MINT_CKFINITE
);
4818 CHECK_STACK (td
, 1);
4820 token
= read32 (td
->ip
+ 1);
4821 klass
= mini_get_class (method
, token
, generic_context
);
4822 CHECK_TYPELOAD (klass
);
4824 interp_add_ins (td
, MINT_MKREFANY
);
4825 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4828 PUSH_VT (td
, sizeof (MonoTypedRef
));
4829 SET_TYPE(td
->sp
- 1, STACK_TYPE_VT
, mono_defaults
.typed_reference_class
);
4831 case CEE_REFANYVAL
: {
4832 CHECK_STACK (td
, 1);
4834 token
= read32 (td
->ip
+ 1);
4835 klass
= mini_get_class (method
, token
, generic_context
);
4836 CHECK_TYPELOAD (klass
);
4838 interp_add_ins (td
, MINT_REFANYVAL
);
4839 td
->last_ins
->data
[0] = get_data_item_index (td
, klass
);
4841 POP_VT (td
, sizeof (MonoTypedRef
));
4842 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
4847 case CEE_CONV_OVF_I1
:
4848 case CEE_CONV_OVF_I1_UN
: {
4849 gboolean is_un
= *td
->ip
== CEE_CONV_OVF_I1_UN
;
4850 CHECK_STACK (td
, 1);
4851 switch (td
->sp
[-1].type
) {
4853 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_UN_R8
: MINT_CONV_OVF_I1_R8
);
4856 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_U4
: MINT_CONV_OVF_I1_I4
);
4859 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I1_U8
: MINT_CONV_OVF_I1_I8
);
4862 g_assert_not_reached ();
4865 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4868 case CEE_CONV_OVF_U1
:
4869 case CEE_CONV_OVF_U1_UN
:
4870 CHECK_STACK (td
, 1);
4871 switch (td
->sp
[-1].type
) {
4873 interp_add_ins (td
, MINT_CONV_OVF_U1_R8
);
4876 interp_add_ins (td
, MINT_CONV_OVF_U1_I4
);
4879 interp_add_ins (td
, MINT_CONV_OVF_U1_I8
);
4882 g_assert_not_reached ();
4885 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4887 case CEE_CONV_OVF_I2
:
4888 case CEE_CONV_OVF_I2_UN
: {
4889 gboolean is_un
= *td
->ip
== CEE_CONV_OVF_I2_UN
;
4890 CHECK_STACK (td
, 1);
4891 switch (td
->sp
[-1].type
) {
4893 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_UN_R8
: MINT_CONV_OVF_I2_R8
);
4896 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_U4
: MINT_CONV_OVF_I2_I4
);
4899 interp_add_ins (td
, is_un
? MINT_CONV_OVF_I2_U8
: MINT_CONV_OVF_I2_I8
);
4902 g_assert_not_reached ();
4905 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4908 case CEE_CONV_OVF_U2_UN
:
4909 case CEE_CONV_OVF_U2
:
4910 CHECK_STACK (td
, 1);
4911 switch (td
->sp
[-1].type
) {
4913 interp_add_ins (td
, MINT_CONV_OVF_U2_R8
);
4916 interp_add_ins (td
, MINT_CONV_OVF_U2_I4
);
4919 interp_add_ins (td
, MINT_CONV_OVF_U2_I8
);
4922 g_assert_not_reached ();
4925 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4927 #if SIZEOF_VOID_P == 4
4928 case CEE_CONV_OVF_I
:
4930 case CEE_CONV_OVF_I4
:
4931 case CEE_CONV_OVF_I4_UN
:
4932 CHECK_STACK (td
, 1);
4933 switch (td
->sp
[-1].type
) {
4935 interp_add_ins (td
, MINT_CONV_OVF_I4_R4
);
4938 interp_add_ins (td
, MINT_CONV_OVF_I4_R8
);
4941 if (*td
->ip
== CEE_CONV_OVF_I4_UN
)
4942 interp_add_ins (td
, MINT_CONV_OVF_I4_U4
);
4945 if (*td
->ip
== CEE_CONV_OVF_I4_UN
)
4946 interp_add_ins (td
, MINT_CONV_OVF_I4_U8
);
4948 interp_add_ins (td
, MINT_CONV_OVF_I4_I8
);
4951 g_assert_not_reached ();
4954 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4956 #if SIZEOF_VOID_P == 4
4957 case CEE_CONV_OVF_U
:
4959 case CEE_CONV_OVF_U4
:
4960 case CEE_CONV_OVF_U4_UN
:
4961 CHECK_STACK (td
, 1);
4962 switch (td
->sp
[-1].type
) {
4964 interp_add_ins (td
, MINT_CONV_OVF_U4_R4
);
4967 interp_add_ins (td
, MINT_CONV_OVF_U4_R8
);
4970 if (*td
->ip
!= CEE_CONV_OVF_U4_UN
)
4971 interp_add_ins (td
, MINT_CONV_OVF_U4_I4
);
4974 interp_add_ins (td
, MINT_CONV_OVF_U4_I8
);
4977 g_assert_not_reached ();
4980 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
4982 #if SIZEOF_VOID_P == 8
4983 case CEE_CONV_OVF_I
:
4985 case CEE_CONV_OVF_I8
:
4986 CHECK_STACK (td
, 1);
4987 switch (td
->sp
[-1].type
) {
4989 interp_add_ins (td
, MINT_CONV_OVF_I8_R4
);
4992 interp_add_ins (td
, MINT_CONV_OVF_I8_R8
);
4995 interp_add_ins (td
, MINT_CONV_I8_I4
);
5000 g_assert_not_reached ();
5003 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
5005 #if SIZEOF_VOID_P == 8
5006 case CEE_CONV_OVF_U
:
5008 case CEE_CONV_OVF_U8
:
5009 CHECK_STACK (td
, 1);
5010 switch (td
->sp
[-1].type
) {
5012 interp_add_ins (td
, MINT_CONV_OVF_U8_R4
);
5015 interp_add_ins (td
, MINT_CONV_OVF_U8_R8
);
5018 interp_add_ins (td
, MINT_CONV_OVF_U8_I4
);
5021 interp_add_ins (td
, MINT_CONV_OVF_U8_I8
);
5024 g_assert_not_reached ();
5027 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I8
);
5032 token
= read32 (td
->ip
+ 1);
5033 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
|| method
->wrapper_type
== MONO_WRAPPER_SYNCHRONIZED
) {
5034 handle
= mono_method_get_wrapper_data (method
, token
);
5035 klass
= (MonoClass
*) mono_method_get_wrapper_data (method
, token
+ 1);
5036 if (klass
== mono_defaults
.typehandle_class
)
5037 handle
= m_class_get_byval_arg ((MonoClass
*) handle
);
5039 if (generic_context
) {
5040 handle
= mono_class_inflate_generic_type_checked ((MonoType
*)handle
, generic_context
, error
);
5041 goto_if_nok (error
, exit
);
5044 handle
= mono_ldtoken_checked (image
, token
, &klass
, generic_context
, error
);
5045 goto_if_nok (error
, exit
);
5047 mono_class_init_internal (klass
);
5048 mt
= mint_type (m_class_get_byval_arg (klass
));
5049 g_assert (mt
== MINT_TYPE_VT
);
5050 size
= mono_class_value_size (klass
, NULL
);
5051 g_assert (size
== sizeof(gpointer
));
5053 const unsigned char *next_ip
= td
->ip
+ 5;
5054 MonoMethod
*cmethod
;
5055 if (next_ip
< end
&&
5056 !td
->is_bb_start
[next_ip
- td
->il_code
] &&
5057 (*next_ip
== CEE_CALL
|| *next_ip
== CEE_CALLVIRT
) &&
5058 (cmethod
= mono_get_method_checked (image
, read32 (next_ip
+ 1), NULL
, generic_context
, error
)) &&
5059 (cmethod
->klass
== mono_defaults
.systemtype_class
) &&
5060 (strcmp (cmethod
->name
, "GetTypeFromHandle") == 0)) {
5061 interp_add_ins (td
, MINT_MONO_LDPTR
);
5062 gpointer systype
= mono_type_get_object_checked (domain
, (MonoType
*)handle
, error
);
5063 goto_if_nok (error
, exit
);
5064 td
->last_ins
->data
[0] = get_data_item_index (td
, systype
);
5065 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
5066 td
->ip
= next_ip
+ 5;
5068 PUSH_VT (td
, sizeof(gpointer
));
5069 interp_add_ins (td
, MINT_LDTOKEN
);
5070 td
->last_ins
->data
[0] = get_data_item_index (td
, handle
);
5071 PUSH_TYPE (td
, stack_type
[mt
], klass
);
5078 binary_arith_op(td
, MINT_ADD_OVF_I4
);
5081 case CEE_ADD_OVF_UN
:
5082 binary_arith_op(td
, MINT_ADD_OVF_UN_I4
);
5086 binary_arith_op(td
, MINT_MUL_OVF_I4
);
5089 case CEE_MUL_OVF_UN
:
5090 binary_arith_op(td
, MINT_MUL_OVF_UN_I4
);
5094 binary_arith_op(td
, MINT_SUB_OVF_I4
);
5097 case CEE_SUB_OVF_UN
:
5098 binary_arith_op(td
, MINT_SUB_OVF_UN_I4
);
5101 case CEE_ENDFINALLY
: {
5102 g_assert (td
->clause_indexes
[in_offset
] != -1);
5104 SIMPLE_OP (td
, MINT_ENDFINALLY
);
5105 td
->last_ins
->data
[0] = td
->clause_indexes
[in_offset
];
5112 if (*td
->ip
== CEE_LEAVE
)
5113 offset
= 5 + read32 (td
->ip
+ 1);
5115 offset
= 2 + (gint8
)td
->ip
[1];
5118 if (td
->clause_indexes
[in_offset
] != -1) {
5119 /* LEAVE instructions in catch clauses need to check for abort exceptions */
5120 handle_branch (td
, MINT_LEAVE_S_CHECK
, MINT_LEAVE_CHECK
, offset
);
5122 handle_branch (td
, MINT_LEAVE_S
, MINT_LEAVE
, offset
);
5125 if (*td
->ip
== CEE_LEAVE
)
5131 case MONO_CUSTOM_PREFIX
:
5134 case CEE_MONO_RETHROW
:
5135 CHECK_STACK (td
, 1);
5136 SIMPLE_OP (td
, MINT_MONO_RETHROW
);
5140 case CEE_MONO_LD_DELEGATE_METHOD_PTR
:
5143 interp_add_ins (td
, MINT_LD_DELEGATE_METHOD_PTR
);
5144 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
5146 case CEE_MONO_CALLI_EXTRA_ARG
:
5147 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
5148 interp_add_ins (td
, MINT_POP
);
5149 td
->last_ins
->data
[0] = 1;
5151 if (!interp_transform_call (td
, method
, NULL
, domain
, generic_context
, td
->is_bb_start
, NULL
, FALSE
, error
, FALSE
))
5154 case CEE_MONO_JIT_ICALL_ADDR
: {
5158 token
= read32 (td
->ip
+ 1);
5160 func
= mono_method_get_wrapper_data (method
, token
);
5162 interp_add_ins (td
, MINT_LDFTN
);
5163 td
->last_ins
->data
[0] = get_data_item_index (td
, func
);
5164 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_I
);
5167 case CEE_MONO_ICALL
: {
5170 MonoJitICallInfo
*info
;
5173 token
= read32 (td
->ip
+ 1);
5175 func
= mono_method_get_wrapper_data (method
, token
);
5176 info
= mono_find_jit_icall_by_addr (func
);
5179 CHECK_STACK (td
, info
->sig
->param_count
);
5180 if (!strcmp (info
->name
, "mono_threads_attach_coop")) {
5181 rtm
->needs_thread_attach
= 1;
5183 /* attach needs two arguments, and has one return value: leave one element on the stack */
5184 interp_add_ins (td
, MINT_POP
);
5185 td
->last_ins
->data
[0] = 0;
5186 } else if (!strcmp (info
->name
, "mono_threads_detach_coop")) {
5187 g_assert (rtm
->needs_thread_attach
);
5189 /* detach consumes two arguments, and no return value: drop both of them */
5190 interp_add_ins (td
, MINT_POP
);
5191 td
->last_ins
->data
[0] = 0;
5192 interp_add_ins (td
, MINT_POP
);
5193 td
->last_ins
->data
[0] = 0;
5195 icall_op
= interp_icall_op_for_sig (info
->sig
);
5196 g_assert (icall_op
!= -1);
5198 interp_add_ins (td
, icall_op
);
5199 td
->last_ins
->data
[0] = get_data_item_index (td
, func
);
5201 td
->sp
-= info
->sig
->param_count
;
5203 if (!MONO_TYPE_IS_VOID (info
->sig
->ret
)) {
5204 int mt
= mint_type (info
->sig
->ret
);
5205 PUSH_SIMPLE_TYPE(td
, stack_type
[mt
]);
5209 case CEE_MONO_VTADDR
: {
5211 CHECK_STACK (td
, 1);
5212 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
5213 size
= mono_class_native_size(td
->sp
[-1].klass
, NULL
);
5215 size
= mono_class_value_size(td
->sp
[-1].klass
, NULL
);
5216 size
= ALIGN_TO (size
, MINT_VT_ALIGNMENT
);
5217 interp_add_ins (td
, MINT_VTRESULT
);
5218 td
->last_ins
->data
[0] = 0;
5219 WRITE32_INS (td
->last_ins
, 1, &size
);
5222 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5225 case CEE_MONO_LDPTR
:
5226 case CEE_MONO_CLASSCONST
:
5227 token
= read32 (td
->ip
+ 1);
5229 interp_add_ins (td
, MINT_MONO_LDPTR
);
5230 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_method_get_wrapper_data (method
, token
));
5231 td
->sp
[0].type
= STACK_TYPE_I
;
5234 case CEE_MONO_OBJADDR
:
5235 CHECK_STACK (td
, 1);
5237 td
->sp
[-1].type
= STACK_TYPE_MP
;
5240 case CEE_MONO_NEWOBJ
:
5241 token
= read32 (td
->ip
+ 1);
5243 interp_add_ins (td
, MINT_MONO_NEWOBJ
);
5244 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_method_get_wrapper_data (method
, token
));
5245 td
->sp
[0].type
= STACK_TYPE_O
;
5248 case CEE_MONO_RETOBJ
:
5249 CHECK_STACK (td
, 1);
5250 token
= read32 (td
->ip
+ 1);
5252 interp_add_ins (td
, MINT_MONO_RETOBJ
);
5255 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
5257 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
5259 if (td
->sp
> td
->stack
)
5260 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td
->sp
-td
->stack
);
5262 case CEE_MONO_LDNATIVEOBJ
:
5263 token
= read32 (td
->ip
+ 1);
5265 klass
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
);
5266 g_assert(m_class_is_valuetype (klass
));
5267 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5269 case CEE_MONO_TLS
: {
5270 gint32 key
= read32 (td
->ip
+ 1);
5272 g_assert (key
< TLS_KEY_NUM
);
5273 interp_add_ins (td
, MINT_MONO_TLS
);
5274 WRITE32_INS (td
->last_ins
, 0, &key
);
5275 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_MP
);
5278 case CEE_MONO_ATOMIC_STORE_I4
:
5279 CHECK_STACK (td
, 2);
5280 SIMPLE_OP (td
, MINT_MONO_ATOMIC_STORE_I4
);
5284 case CEE_MONO_SAVE_LMF
:
5285 case CEE_MONO_RESTORE_LMF
:
5286 case CEE_MONO_NOT_TAKEN
:
5289 case CEE_MONO_LDPTR_INT_REQ_FLAG
:
5290 interp_add_ins (td
, MINT_MONO_LDPTR
);
5291 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_thread_interruption_request_flag ());
5292 PUSH_TYPE (td
, STACK_TYPE_MP
, NULL
);
5295 case CEE_MONO_MEMORY_BARRIER
:
5296 interp_add_ins (td
, MINT_MONO_MEMORY_BARRIER
);
5299 case CEE_MONO_LDDOMAIN
:
5300 interp_add_ins (td
, MINT_MONO_LDDOMAIN
);
5301 td
->sp
[0].type
= STACK_TYPE_I
;
5306 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td
->ip
, td
->ip
-header
->code
);
5316 case CEE_PREFIXREF
: ves_abort(); break;
5319 * Note: Exceptions thrown when executing a prefixed opcode need
5320 * to take into account the number of prefix bytes (usually the
5321 * throw point is just (ip - n_prefix_bytes).
5327 interp_add_ins (td
, MINT_ARGLIST
);
5328 PUSH_VT (td
, SIZEOF_VOID_P
);
5329 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_VT
);
5334 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
) {
5335 interp_add_ins (td
, MINT_CEQ_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5337 if (td
->sp
[-1].type
== STACK_TYPE_R4
&& td
->sp
[-2].type
== STACK_TYPE_R8
)
5338 interp_add_ins (td
, MINT_CONV_R8_R4
);
5339 if (td
->sp
[-1].type
== STACK_TYPE_R8
&& td
->sp
[-2].type
== STACK_TYPE_R4
)
5340 interp_add_ins (td
, MINT_CONV_R8_R4_SP
);
5341 interp_add_ins (td
, MINT_CEQ_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5344 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5349 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5350 interp_add_ins (td
, MINT_CGT_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5352 interp_add_ins (td
, MINT_CGT_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5354 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5359 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5360 interp_add_ins (td
, MINT_CGT_UN_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5362 interp_add_ins (td
, MINT_CGT_UN_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5364 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5369 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5370 interp_add_ins (td
, MINT_CLT_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5372 interp_add_ins (td
, MINT_CLT_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5374 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5379 if (td
->sp
[-1].type
== STACK_TYPE_O
|| td
->sp
[-1].type
== STACK_TYPE_MP
)
5380 interp_add_ins (td
, MINT_CLT_UN_I4
+ STACK_TYPE_I
- STACK_TYPE_I4
);
5382 interp_add_ins (td
, MINT_CLT_UN_I4
+ td
->sp
[-1].type
- STACK_TYPE_I4
);
5384 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_I4
);
5387 case CEE_LDVIRTFTN
: /* fallthrough */
5390 if (*td
->ip
== CEE_LDVIRTFTN
) {
5391 CHECK_STACK (td
, 1);
5394 token
= read32 (td
->ip
+ 1);
5395 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
5396 m
= (MonoMethod
*)mono_method_get_wrapper_data (method
, token
);
5398 m
= mono_get_method_checked (image
, token
, NULL
, generic_context
, error
);
5399 goto_if_nok (error
, exit
);
5402 if (!mono_method_can_access_method (method
, m
))
5403 interp_generate_mae_throw (td
, method
, m
);
5405 if (method
->wrapper_type
== MONO_WRAPPER_NONE
&& m
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)
5406 m
= mono_marshal_get_synchronized_wrapper (m
);
5408 interp_add_ins (td
, *td
->ip
== CEE_LDFTN
? MINT_LDFTN
: MINT_LDVIRTFTN
);
5409 td
->last_ins
->data
[0] = get_data_item_index (td
, mono_interp_get_imethod (domain
, m
, error
));
5410 goto_if_nok (error
, exit
);
5412 PUSH_SIMPLE_TYPE (td
, STACK_TYPE_F
);
5416 int arg_n
= read16 (td
->ip
+ 1);
5417 if (td
->method
== method
)
5418 load_arg (td
, arg_n
);
5420 load_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
5426 int n
= read16 (td
->ip
+ 1);
5428 get_arg_type_exact (td
, n
, &mt
);
5429 interp_add_ins (td
, mt
== MINT_TYPE_VT
? MINT_LDARGA_VT
: MINT_LDARGA
);
5430 td
->last_ins
->data
[0] = n
;
5431 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
5436 int arg_n
= read16 (td
->ip
+ 1);
5437 if (td
->method
== method
)
5438 store_arg (td
, arg_n
);
5440 store_local_general (td
, arg_offsets
[arg_n
], get_arg_type (signature
, arg_n
));
5445 load_local (td
, read16 (td
->ip
+ 1));
5449 interp_add_ins (td
, MINT_LDLOCA_S
);
5450 td
->last_ins
->data
[0] = td
->rtm
->local_offsets
[read16 (td
->ip
+ 1)];
5451 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_MP
);
5455 store_local (td
, read16 (td
->ip
+ 1));
5459 CHECK_STACK (td
, 1);
5460 #if SIZEOF_VOID_P == 8
5461 if (td
->sp
[-1].type
== STACK_TYPE_I8
)
5462 interp_add_ins (td
, MINT_CONV_I4_I8
);
5464 interp_add_ins (td
, MINT_LOCALLOC
);
5465 if (td
->sp
!= td
->stack
+ 1)
5466 g_warning("CEE_LOCALLOC: stack not empty");
5468 SET_SIMPLE_TYPE(td
->sp
- 1, STACK_TYPE_MP
);
5471 case CEE_UNUSED57
: ves_abort(); break;
5474 interp_add_ins (td
, MINT_ENDFILTER
);
5477 case CEE_UNALIGNED_
:
5486 /* FIX: should do something? */;
5487 // TODO: This should raise a method_tail_call profiler event.
5491 token
= read32 (td
->ip
+ 1);
5492 klass
= mini_get_class (method
, token
, generic_context
);
5493 CHECK_TYPELOAD (klass
);
5494 if (m_class_is_valuetype (klass
)) {
5495 interp_add_ins (td
, MINT_INITOBJ
);
5496 i32
= mono_class_value_size (klass
, NULL
);
5497 WRITE32_INS (td
->last_ins
, 0, &i32
);
5499 interp_add_ins (td
, MINT_LDNULL
);
5500 interp_add_ins (td
, MINT_STIND_REF
);
5507 /* FIX? convert length to I8? */
5509 interp_add_ins (td
, MINT_MONO_MEMORY_BARRIER
);
5510 interp_add_ins (td
, MINT_CPBLK
);
5511 BARRIER_IF_VOLATILE (td
);
5519 case CEE_CONSTRAINED_
:
5520 token
= read32 (td
->ip
+ 1);
5521 constrained_class
= mini_get_class (method
, token
, generic_context
);
5522 CHECK_TYPELOAD (constrained_class
);
5527 BARRIER_IF_VOLATILE (td
);
5528 interp_add_ins (td
, MINT_INITBLK
);
5533 /* FIXME: implement */
5537 int clause_index
= td
->clause_indexes
[in_offset
];
5538 g_assert (clause_index
!= -1);
5539 SIMPLE_OP (td
, MINT_RETHROW
);
5540 td
->last_ins
->data
[0] = rtm
->exvar_offsets
[clause_index
];
5546 token
= read32 (td
->ip
+ 1);
5548 if (mono_metadata_token_table (token
) == MONO_TABLE_TYPESPEC
&& !image_is_dynamic (m_class_get_image (method
->klass
)) && !generic_context
) {
5550 MonoType
*type
= mono_type_create_from_typespec_checked (image
, token
, error
);
5551 goto_if_nok (error
, exit
);
5552 size
= mono_type_size (type
, &align
);
5555 MonoClass
*szclass
= mini_get_class (method
, token
, generic_context
);
5556 CHECK_TYPELOAD (szclass
);
5558 if (!szclass
->valuetype
)
5559 THROW_EX (mono_exception_from_name (mono_defaults
.corlib
, "System", "InvalidProgramException"), ip
- 5);
5561 size
= mono_type_size (m_class_get_byval_arg (szclass
), &align
);
5563 interp_add_ins (td
, MINT_LDC_I4
);
5564 WRITE32_INS (td
->last_ins
, 0, &size
);
5565 PUSH_SIMPLE_TYPE(td
, STACK_TYPE_I4
);
5568 case CEE_REFANYTYPE
:
5569 interp_add_ins (td
, MINT_REFANYTYPE
);
5571 POP_VT (td
, sizeof (MonoTypedRef
));
5572 PUSH_VT (td
, sizeof (gpointer
));
5573 SET_TYPE(td
->sp
- 1, STACK_TYPE_VT
, NULL
);
5576 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
);
5580 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td
->ip
, td
->ip
-header
->code
);
5583 // No IR instructions were added as part of the IL instruction. Extend bb_start
5584 if (prev_last_ins
== td
->last_ins
&& td
->is_bb_start
[in_offset
] && td
->ip
< end
)
5585 td
->is_bb_start
[td
->ip
- td
->il_code
] = 1;
5588 g_assert (td
->ip
== end
);
5591 g_free (arg_offsets
);
5592 mono_basic_block_free (original_bb
);
5593 for (i
= 0; i
< header
->code_size
; ++i
)
5594 g_free (td
->stack_state
[i
]);
5595 g_free (td
->stack_state
);
5596 g_free (td
->stack_height
);
5597 g_free (td
->vt_stack_size
);
5598 g_free (td
->clause_indexes
);
5599 g_free (td
->is_bb_start
);
5607 // We are trying to branch to an il offset that has no associated ir instruction with it.
5608 // We will branch instead to the next instruction that we find
5610 resolve_in_offset (TransformData
*td
, int il_offset
)
5613 g_assert (!td
->in_offsets
[il_offset
]);
5614 while (!td
->in_offsets
[i
])
5616 td
->in_offsets
[il_offset
] = td
->in_offsets
[i
];
5617 return td
->in_offsets
[il_offset
];
5620 // We store in the in_offset array the native_offset + 1, so 0 can mean only that the il
5621 // offset is uninitialized. Otherwise 0 is valid value for first interp instruction.
5623 get_in_offset (TransformData
*td
, int il_offset
)
5625 int target_offset
= td
->in_offsets
[il_offset
];
5627 return target_offset
- 1;
5628 return resolve_in_offset (td
, il_offset
) - 1;
5632 handle_relocations (TransformData
*td
)
5634 // Handle relocations
5635 for (int i
= 0; i
< td
->relocs
->len
; ++i
) {
5636 Reloc
*reloc
= (Reloc
*)g_ptr_array_index (td
->relocs
, i
);
5637 int offset
= get_in_offset (td
, reloc
->target
) - reloc
->offset
;
5639 switch (reloc
->type
) {
5640 case RELOC_SHORT_BRANCH
:
5641 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xdead);
5642 td
->new_code
[reloc
->offset
+ 1] = offset
;
5644 case RELOC_LONG_BRANCH
: {
5645 guint16
*v
= (guint16
*) &offset
;
5646 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xdead);
5647 g_assert (td
->new_code
[reloc
->offset
+ 2] == 0xbeef);
5648 td
->new_code
[reloc
->offset
+ 1] = *(guint16
*) v
;
5649 td
->new_code
[reloc
->offset
+ 2] = *(guint16
*) (v
+ 1);
5652 case RELOC_SWITCH
: {
5653 guint16
*v
= (guint16
*)&offset
;
5654 g_assert (td
->new_code
[reloc
->offset
] == 0xdead);
5655 g_assert (td
->new_code
[reloc
->offset
+ 1] == 0xbeef);
5656 td
->new_code
[reloc
->offset
] = *(guint16
*)v
;
5657 td
->new_code
[reloc
->offset
+ 1] = *(guint16
*)(v
+ 1);
5661 g_assert_not_reached ();
5669 get_inst_length (InterpInst
*ins
)
5671 if (ins
->opcode
== MINT_SWITCH
)
5672 return MINT_SWITCH_LEN (READ32 (&ins
->data
[0]));
5674 return mono_interp_oplen
[ins
->opcode
];
5678 emit_compacted_instruction (TransformData
*td
, guint16
* start_ip
, InterpInst
*ins
)
5680 guint16 opcode
= ins
->opcode
;
5681 guint16
*ip
= start_ip
;
5683 // We know what IL offset this instruction was created for. We can now map the IL offset
5684 // to the IR offset. We use this array to resolve the relocations, which reference the IL.
5685 if (ins
->il_offset
!= -1 && !td
->in_offsets
[ins
->il_offset
]) {
5686 g_assert (ins
->il_offset
>= 0 && ins
->il_offset
< td
->header
->code_size
);
5687 td
->in_offsets
[ins
->il_offset
] = start_ip
- td
->new_code
+ 1;
5689 MonoDebugLineNumberEntry lne
;
5690 lne
.native_offset
= (guint8
*)start_ip
- (guint8
*)td
->new_code
;
5691 lne
.il_offset
= ins
->il_offset
;
5692 g_array_append_val (td
->line_numbers
, lne
);
5696 if (opcode
== MINT_SWITCH
) {
5697 int labels
= READ32 (&ins
->data
[0]);
5698 // Write number of switch labels
5699 *ip
++ = ins
->data
[0];
5700 *ip
++ = ins
->data
[1];
5701 // Add relocation for each label
5702 for (int i
= 0; i
< labels
; i
++) {
5703 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
5704 reloc
->type
= RELOC_SWITCH
;
5705 reloc
->offset
= ip
- td
->new_code
;
5706 reloc
->target
= READ32 (&ins
->data
[2 + i
* 2]);
5707 g_ptr_array_add (td
->relocs
, reloc
);
5711 } else if ((opcode
>= MINT_BRFALSE_I4_S
&& opcode
<= MINT_BRTRUE_R8_S
) ||
5712 (opcode
>= MINT_BEQ_I4_S
&& opcode
<= MINT_BLT_UN_R8_S
) ||
5713 opcode
== MINT_BR_S
|| opcode
== MINT_LEAVE_S
|| opcode
== MINT_LEAVE_S_CHECK
) {
5714 const int br_offset
= start_ip
- td
->new_code
;
5715 if (ins
->data
[0] < ins
->il_offset
) {
5716 // Backwards branch. We can already patch it.
5717 *ip
++ = get_in_offset (td
, ins
->data
[0]) - br_offset
;
5719 // We don't know the in_offset of the target, add a reloc
5720 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
5721 reloc
->type
= RELOC_SHORT_BRANCH
;
5722 reloc
->offset
= br_offset
;
5723 reloc
->target
= ins
->data
[0];
5724 g_ptr_array_add (td
->relocs
, reloc
);
5727 } else if ((opcode
>= MINT_BRFALSE_I4
&& opcode
<= MINT_BRTRUE_R8
) ||
5728 (opcode
>= MINT_BEQ_I4
&& opcode
<= MINT_BLT_UN_R8
) ||
5729 opcode
== MINT_BR
|| opcode
== MINT_LEAVE
|| opcode
== MINT_LEAVE_CHECK
) {
5730 const int br_offset
= start_ip
- td
->new_code
;
5731 int target_il
= READ32 (&ins
->data
[0]);
5732 if (target_il
< ins
->il_offset
) {
5733 // Backwards branch. We can already patch it
5734 const int br_offset
= start_ip
- td
->new_code
;
5735 int target_offset
= get_in_offset (td
, target_il
) - br_offset
;
5736 WRITE32 (ip
, &target_offset
);
5738 Reloc
*reloc
= (Reloc
*)mono_mempool_alloc0 (td
->mempool
, sizeof (Reloc
));
5739 reloc
->type
= RELOC_LONG_BRANCH
;
5740 reloc
->offset
= br_offset
;
5741 reloc
->target
= target_il
;
5742 g_ptr_array_add (td
->relocs
, reloc
);
5746 } else if (opcode
== MINT_SDB_SEQ_POINT
) {
5747 SeqPoint
*seqp
= (SeqPoint
*)mono_mempool_alloc0 (td
->mempool
, sizeof (SeqPoint
));
5748 InterpBasicBlock
*cbb
;
5750 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY
) {
5751 seqp
->il_offset
= METHOD_ENTRY_IL_OFFSET
;
5752 cbb
= td
->offset_to_bb
[0];
5754 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT
)
5755 seqp
->il_offset
= METHOD_EXIT_IL_OFFSET
;
5757 seqp
->il_offset
= ins
->il_offset
;
5758 cbb
= td
->offset_to_bb
[ins
->il_offset
];
5760 seqp
->native_offset
= (guint8
*)start_ip
- (guint8
*)td
->new_code
;
5761 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK
)
5762 seqp
->flags
|= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK
;
5763 if (ins
->flags
& INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL
)
5764 seqp
->flags
|= MONO_SEQ_POINT_FLAG_NESTED_CALL
;
5765 g_ptr_array_add (td
->seq_points
, seqp
);
5767 cbb
->seq_points
= g_slist_prepend_mempool (td
->mempool
, cbb
->seq_points
, seqp
);
5768 cbb
->last_seq_point
= seqp
;
5770 int size
= get_inst_length (ins
) - 1;
5771 // Emit the rest of the data
5772 for (int i
= 0; i
< size
; i
++)
5773 *ip
++ = ins
->data
[i
];
5778 // Generates the final code, after we are done with all the passes
5780 generate_compacted_code (TransformData
*td
)
5784 td
->relocs
= g_ptr_array_new ();
5786 // Iterate once to compute the exact size of the compacted code
5787 InterpInst
*ins
= td
->first_ins
;
5789 size
+= get_inst_length (ins
);
5793 // Generate the compacted stream of instructions
5794 td
->new_code
= ip
= (guint16
*)mono_domain_alloc0 (td
->rtm
->domain
, size
* sizeof (guint16
));
5795 ins
= td
->first_ins
;
5797 ip
= emit_compacted_instruction (td
, ip
, ins
);
5800 td
->new_code_end
= ip
;
5801 td
->in_offsets
[td
->header
->code_size
] = td
->new_code_end
- td
->new_code
;
5803 // Patch all branches
5804 handle_relocations (td
);
5806 g_ptr_array_free (td
->relocs
, TRUE
);
5810 generate (MonoMethod
*method
, MonoMethodHeader
*header
, InterpMethod
*rtm
, MonoGenericContext
*generic_context
, MonoError
*error
)
5812 MonoDomain
*domain
= rtm
->domain
;
5814 TransformData transform_data
;
5816 static gboolean verbose_method_inited
;
5817 static char* verbose_method_name
;
5819 if (!verbose_method_inited
) {
5820 verbose_method_name
= g_getenv ("MONO_VERBOSE_METHOD");
5821 verbose_method_inited
= TRUE
;
5824 memset (&transform_data
, 0, sizeof(transform_data
));
5825 td
= &transform_data
;
5827 td
->method
= method
;
5829 td
->code_size
= header
->code_size
;
5830 td
->header
= header
;
5831 td
->max_code_size
= td
->code_size
;
5832 td
->in_offsets
= (int*)g_malloc0((header
->code_size
+ 1) * sizeof(int));
5833 td
->mempool
= mono_mempool_new ();
5834 td
->n_data_items
= 0;
5835 td
->max_data_items
= 0;
5836 td
->data_items
= NULL
;
5837 td
->data_hash
= g_hash_table_new (NULL
, NULL
);
5838 td
->gen_sdb_seq_points
= mini_debug_options
.gen_sdb_seq_points
;
5839 td
->seq_points
= g_ptr_array_new ();
5840 td
->verbose_level
= mono_interp_traceopt
;
5841 td
->total_locals_size
= rtm
->locals_size
;
5842 rtm
->data_items
= td
->data_items
;
5844 if (verbose_method_name
) {
5845 const char *name
= verbose_method_name
;
5847 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
5848 MonoMethodDesc
*desc
;
5850 desc
= mono_method_desc_new (name
, TRUE
);
5851 if (mono_method_desc_full_match (desc
, method
)) {
5852 td
->verbose_level
= 4;
5854 mono_method_desc_free (desc
);
5856 if (strcmp (method
->name
, name
) == 0)
5857 td
->verbose_level
= 4;
5861 td
->stack
= (StackInfo
*)g_malloc0 ((header
->max_stack
+ 1) * sizeof (td
->stack
[0]));
5862 td
->stack_capacity
= header
->max_stack
+ 1;
5864 td
->max_stack_height
= 0;
5865 td
->line_numbers
= g_array_new (FALSE
, TRUE
, sizeof (MonoDebugLineNumberEntry
));
5867 generate_code (td
, method
, header
, generic_context
, error
);
5868 goto_if_nok (error
, exit
);
5870 generate_compacted_code (td
);
5872 if (td
->verbose_level
) {
5873 g_print ("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method
, TRUE
), rtm
, td
->max_vt_sp
);
5874 g_print ("Calculated stack size: %d, stated size: %d\n", td
->max_stack_height
, header
->max_stack
);
5875 dump_mint_code (td
->new_code
, td
->new_code_end
);
5878 /* Check if we use excessive stack space */
5879 if (td
->max_stack_height
> header
->max_stack
* 3 && header
->max_stack
> 16)
5880 g_warning ("Excessive stack space usage for method %s, %d/%d", method
->name
, td
->max_stack_height
, header
->max_stack
);
5883 code_len
= td
->new_code_end
- td
->new_code
;
5885 rtm
->clauses
= (MonoExceptionClause
*)mono_domain_alloc0 (domain
, header
->num_clauses
* sizeof (MonoExceptionClause
));
5886 memcpy (rtm
->clauses
, header
->clauses
, header
->num_clauses
* sizeof(MonoExceptionClause
));
5887 rtm
->code
= (gushort
*)td
->new_code
;
5888 rtm
->init_locals
= header
->init_locals
;
5889 rtm
->num_clauses
= header
->num_clauses
;
5890 for (i
= 0; i
< header
->num_clauses
; i
++) {
5891 MonoExceptionClause
*c
= rtm
->clauses
+ i
;
5892 int end_off
= c
->try_offset
+ c
->try_len
;
5893 c
->try_offset
= get_in_offset (td
, c
->try_offset
);
5894 c
->try_len
= get_in_offset (td
, end_off
) - c
->try_offset
;
5895 g_assert ((c
->try_offset
+ c
->try_len
) < code_len
);
5896 end_off
= c
->handler_offset
+ c
->handler_len
;
5897 c
->handler_offset
= get_in_offset (td
, c
->handler_offset
);
5898 c
->handler_len
= get_in_offset (td
, end_off
) - c
->handler_offset
;
5899 g_assert (c
->handler_len
>= 0 && (c
->handler_offset
+ c
->handler_len
) <= code_len
);
5900 if (c
->flags
& MONO_EXCEPTION_CLAUSE_FILTER
)
5901 c
->data
.filter_offset
= get_in_offset (td
, c
->data
.filter_offset
);
5903 rtm
->stack_size
= (sizeof (stackval
)) * (td
->max_stack_height
+ 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
5904 rtm
->stack_size
= ALIGN_TO (rtm
->stack_size
, MINT_VT_ALIGNMENT
);
5905 rtm
->vt_stack_size
= td
->max_vt_sp
;
5906 rtm
->total_locals_size
= td
->total_locals_size
;
5907 rtm
->alloca_size
= rtm
->total_locals_size
+ rtm
->vt_stack_size
+ rtm
->stack_size
;
5908 rtm
->data_items
= (gpointer
*)mono_domain_alloc0 (domain
, td
->n_data_items
* sizeof (td
->data_items
[0]));
5909 memcpy (rtm
->data_items
, td
->data_items
, td
->n_data_items
* sizeof (td
->data_items
[0]));
5911 /* Save debug info */
5912 interp_save_debug_info (rtm
, header
, td
, td
->line_numbers
);
5914 /* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
5916 jinfo_len
= mono_jit_info_size ((MonoJitInfoFlags
)0, header
->num_clauses
, 0);
5918 jinfo
= (MonoJitInfo
*)mono_domain_alloc0 (domain
, jinfo_len
);
5919 jinfo
->is_interp
= 1;
5921 mono_jit_info_init (jinfo
, method
, (guint8
*)rtm
->code
, code_len
, (MonoJitInfoFlags
)0, header
->num_clauses
, 0);
5922 for (i
= 0; i
< jinfo
->num_clauses
; ++i
) {
5923 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
5924 MonoExceptionClause
*c
= rtm
->clauses
+ i
;
5926 ei
->flags
= c
->flags
;
5927 ei
->try_start
= (guint8
*)(rtm
->code
+ c
->try_offset
);
5928 ei
->try_end
= (guint8
*)(rtm
->code
+ c
->try_offset
+ c
->try_len
);
5929 ei
->handler_start
= (guint8
*)(rtm
->code
+ c
->handler_offset
);
5930 ei
->exvar_offset
= rtm
->exvar_offsets
[i
];
5931 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
5932 ei
->data
.filter
= (guint8
*)(rtm
->code
+ c
->data
.filter_offset
);
5933 } else if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
5934 ei
->data
.handler_end
= (guint8
*)(rtm
->code
+ c
->handler_offset
+ c
->handler_len
);
5936 ei
->data
.catch_class
= c
->data
.catch_class
;
5940 save_seq_points (td
, jinfo
);
5943 g_free (td
->in_offsets
);
5944 g_free (td
->data_items
);
5946 g_hash_table_destroy (td
->data_hash
);
5947 g_ptr_array_free (td
->seq_points
, TRUE
);
5948 g_array_free (td
->line_numbers
, TRUE
);
5949 mono_mempool_destroy (td
->mempool
);
5952 static mono_mutex_t calc_section
;
5955 mono_interp_transform_init (void)
5957 mono_os_mutex_init_recursive(&calc_section
);
5961 mono_interp_transform_method (InterpMethod
*imethod
, ThreadContext
*context
, MonoError
*error
)
5963 MonoMethod
*method
= imethod
->method
;
5964 MonoMethodHeader
*header
= NULL
;
5965 MonoMethodSignature
*signature
= mono_method_signature_internal (method
);
5966 MonoVTable
*method_class_vt
;
5967 MonoGenericContext
*generic_context
= NULL
;
5968 MonoDomain
*domain
= imethod
->domain
;
5969 InterpMethod tmp_imethod
;
5970 InterpMethod
*real_imethod
;
5974 if (mono_class_is_open_constructed_type (m_class_get_byval_arg (method
->klass
))) {
5975 mono_error_set_invalid_operation (error
, "%s", "Could not execute the method because the containing type is not fully instantiated.");
5979 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
5980 method_class_vt
= mono_class_vtable_checked (domain
, imethod
->method
->klass
, error
);
5981 return_if_nok (error
);
5983 if (!method_class_vt
->initialized
) {
5984 mono_runtime_class_init_full (method_class_vt
, error
);
5985 return_if_nok (error
);
5988 MONO_PROFILER_RAISE (jit_begin
, (method
));
5990 if (mono_method_signature_internal (method
)->is_inflated
)
5991 generic_context
= mono_method_get_context (method
);
5993 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (method
);
5994 if (generic_container
)
5995 generic_context
= &generic_container
->context
;
5998 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
5999 MonoMethod
*nm
= NULL
;
6000 mono_os_mutex_lock (&calc_section
);
6001 if (imethod
->transformed
) {
6002 mono_os_mutex_unlock (&calc_section
);
6003 MONO_PROFILER_RAISE (jit_done
, (method
, imethod
->jinfo
));
6007 /* assumes all internal calls with an array this are built in... */
6008 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
&& (! mono_method_signature_internal (method
)->hasthis
|| m_class_get_rank (method
->klass
) == 0)) {
6009 nm
= mono_marshal_get_native_wrapper (method
, FALSE
, FALSE
);
6010 signature
= mono_method_signature_internal (nm
);
6012 const char *name
= method
->name
;
6013 if (m_class_get_parent (method
->klass
) == mono_defaults
.multicastdelegate_class
) {
6014 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
6015 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor_interp");
6017 nm
= mono_marshal_get_icall_wrapper (mi
, TRUE
);
6018 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
6020 * Usually handled during transformation of the caller, but
6021 * when the caller is handled by another execution engine
6022 * (for example fullAOT) we need to handle it here. That's
6023 * known to be wrong in cases where the reference to
6024 * `MonoDelegate` would be needed (FIXME).
6026 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
6027 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
6028 nm
= mono_marshal_get_delegate_begin_invoke (method
);
6029 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
6030 nm
= mono_marshal_get_delegate_end_invoke (method
);
6034 g_assert_not_reached ();
6037 imethod
->stack_size
= sizeof (stackval
); /* for tracing */
6038 imethod
->alloca_size
= imethod
->stack_size
;
6039 imethod
->transformed
= TRUE
;
6040 mono_os_mutex_unlock(&calc_section
);
6041 MONO_PROFILER_RAISE (jit_done
, (method
, NULL
));
6045 header
= interp_method_get_header (nm
, error
);
6046 mono_os_mutex_unlock (&calc_section
);
6047 return_if_nok (error
);
6051 header
= mono_method_get_header_checked (method
, error
);
6052 return_if_nok (error
);
6055 g_assert ((signature
->param_count
+ signature
->hasthis
) < 1000);
6056 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
6058 /* Make modifications to a copy of imethod, copy them back inside the lock */
6059 real_imethod
= imethod
;
6060 memcpy (&tmp_imethod
, imethod
, sizeof (InterpMethod
));
6061 imethod
= &tmp_imethod
;
6063 interp_method_compute_offsets (imethod
, signature
, header
);
6065 MONO_TIME_TRACK (mono_interp_stats
.transform_time
, generate (method
, header
, imethod
, generic_context
, error
));
6067 mono_metadata_free_mh (header
);
6069 return_if_nok (error
);
6071 /* Copy changes back */
6072 imethod
= real_imethod
;
6073 mono_os_mutex_lock (&calc_section
);
6074 if (!imethod
->transformed
) {
6075 // Ignore the first two fields which are unchanged. next_jit_code_hash shouldn't
6076 // be modified because it is racy with internal hash table insert.
6077 const int start_offset
= 2 * sizeof (gpointer
);
6078 memcpy ((char*)imethod
+ start_offset
, (char*)&tmp_imethod
+ start_offset
, sizeof (InterpMethod
) - start_offset
);
6079 mono_memory_barrier ();
6080 imethod
->transformed
= TRUE
;
6081 mono_atomic_fetch_add_i32 (&mono_jit_stats
.methods_with_interp
, 1);
6084 mono_os_mutex_unlock (&calc_section
);
6086 mono_domain_lock (domain
);
6087 if (!g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, imethod
->method
))
6088 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, imethod
->method
, imethod
->jinfo
->seq_points
);
6089 mono_domain_unlock (domain
);
6091 // FIXME: Add a different callback ?
6092 MONO_PROFILER_RAISE (jit_done
, (method
, imethod
->jinfo
));