6 #include <mono/utils/mono-compiler.h>
12 #include <mono/metadata/abi-details.h>
13 #include <mono/metadata/class-abi-details.h>
16 #define is_complex_isinst(klass) (mono_class_is_interface (klass) || m_class_get_rank (klass) || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || mono_class_is_sealed (klass) || m_class_get_byval_arg (klass)->type == MONO_TYPE_VAR || m_class_get_byval_arg (klass)->type == MONO_TYPE_MVAR)
19 get_castclass_cache_idx (MonoCompile
*cfg
)
21 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
22 cfg
->castclass_cache_index
++;
23 return (cfg
->method_index
<< 16) | cfg
->castclass_cache_index
;
27 emit_cached_check_args (MonoCompile
*cfg
, MonoInst
*obj
, MonoClass
*klass
, int context_used
, MonoInst
*args
[3])
34 cache_ins
= mini_emit_get_rgctx_klass (cfg
, context_used
, klass
, MONO_RGCTX_INFO_CAST_CACHE
);
36 /* klass - it's the second element of the cache entry*/
37 EMIT_NEW_LOAD_MEMBASE (cfg
, args
[1], OP_LOAD_MEMBASE
, alloc_preg (cfg
), cache_ins
->dreg
, sizeof (gpointer
));
39 args
[2] = cache_ins
; /* cache */
43 EMIT_NEW_CLASSCONST (cfg
, args
[1], klass
); /* klass */
45 idx
= get_castclass_cache_idx (cfg
); /* inline cache*/
46 args
[2] = mini_emit_runtime_constant (cfg
, MONO_PATCH_INFO_CASTCLASS_CACHE
, GINT_TO_POINTER (idx
));
51 emit_isinst_with_cache (MonoCompile
*cfg
, MonoInst
*obj
, MonoClass
*klass
, int context_used
)
54 MonoMethod
*mono_isinst
= mono_marshal_get_isinst_with_cache ();
56 emit_cached_check_args (cfg
, obj
, klass
, context_used
, args
);
57 return mono_emit_method_call (cfg
, mono_isinst
, args
, NULL
);
61 emit_castclass_with_cache_no_details (MonoCompile
*cfg
, MonoInst
*obj
, MonoClass
*klass
, int context_used
)
64 MonoMethod
*mono_castclass
= mono_marshal_get_castclass_with_cache ();
67 emit_cached_check_args (cfg
, obj
, klass
, context_used
, args
);
69 res
= mono_emit_method_call (cfg
, mono_castclass
, args
, NULL
);
75 emit_castclass_with_cache (MonoCompile
*cfg
, MonoInst
*obj
, MonoClass
*klass
, int context_used
)
78 MonoMethod
*mono_castclass
= mono_marshal_get_castclass_with_cache ();
81 emit_cached_check_args (cfg
, obj
, klass
, context_used
, args
);
83 mini_save_cast_details (cfg
, klass
, args
[0]->dreg
, TRUE
);
84 res
= mono_emit_method_call (cfg
, mono_castclass
, args
, NULL
);
85 mini_reset_cast_details (cfg
);
91 mini_emit_class_check_inst (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
, MonoInst
*klass_inst
)
94 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, klass_reg
, klass_inst
->dreg
);
96 MonoInst
*ins
= mini_emit_runtime_constant (cfg
, MONO_PATCH_INFO_CLASS
, klass
);
97 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, klass_reg
, ins
->dreg
);
99 MONO_EMIT_NEW_COND_EXC (cfg
, NE_UN
, "InvalidCastException");
104 mini_emit_isninst_cast_inst (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
, MonoInst
*klass_ins
, MonoBasicBlock
*false_target
, MonoBasicBlock
*true_target
)
106 int idepth_reg
= alloc_preg (cfg
);
107 int stypes_reg
= alloc_preg (cfg
);
108 int stype
= alloc_preg (cfg
);
110 mono_class_setup_supertypes (klass
);
112 if (m_class_get_idepth (klass
) > MONO_DEFAULT_SUPERTABLE_SIZE
) {
113 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU2_MEMBASE
, idepth_reg
, klass_reg
, m_class_offsetof_idepth ());
114 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, idepth_reg
, m_class_get_idepth (klass
));
115 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBLT_UN
, false_target
);
117 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, stypes_reg
, klass_reg
, m_class_offsetof_supertypes ());
118 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, stype
, stypes_reg
, ((m_class_get_idepth (klass
) - 1) * SIZEOF_VOID_P
));
120 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, stype
, klass_ins
->dreg
);
121 } else if (cfg
->compile_aot
) {
122 int const_reg
= alloc_preg (cfg
);
123 MONO_EMIT_NEW_CLASSCONST (cfg
, const_reg
, klass
);
124 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, stype
, const_reg
);
126 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, stype
, klass
);
128 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBEQ
, true_target
);
133 mini_emit_interface_bitmap_check (MonoCompile
*cfg
, int intf_bit_reg
, int base_reg
, int offset
, MonoClass
*klass
)
135 int ibitmap_reg
= alloc_preg (cfg
);
136 #ifdef COMPRESSED_INTERFACE_BITMAP
139 NEW_LOAD_MEMBASE (cfg
, ins
, OP_LOAD_MEMBASE
, ibitmap_reg
, base_reg
, offset
);
140 MONO_ADD_INS (cfg
->cbb
, ins
);
142 args
[1] = mini_emit_runtime_constant (cfg
, MONO_PATCH_INFO_IID
, klass
);
143 res
= mono_emit_jit_icall (cfg
, mono_class_interface_match
, args
);
144 MONO_EMIT_NEW_UNALU (cfg
, OP_MOVE
, intf_bit_reg
, res
->dreg
);
146 int ibitmap_byte_reg
= alloc_preg (cfg
);
148 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, ibitmap_reg
, base_reg
, offset
);
150 if (cfg
->compile_aot
) {
151 int iid_reg
= alloc_preg (cfg
);
152 int shifted_iid_reg
= alloc_preg (cfg
);
153 int ibitmap_byte_address_reg
= alloc_preg (cfg
);
154 int masked_iid_reg
= alloc_preg (cfg
);
155 int iid_one_bit_reg
= alloc_preg (cfg
);
156 int iid_bit_reg
= alloc_preg (cfg
);
157 MONO_EMIT_NEW_AOTCONST (cfg
, iid_reg
, klass
, MONO_PATCH_INFO_IID
);
158 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_SHR_IMM
, shifted_iid_reg
, iid_reg
, 3);
159 MONO_EMIT_NEW_BIALU (cfg
, OP_PADD
, ibitmap_byte_address_reg
, ibitmap_reg
, shifted_iid_reg
);
160 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU1_MEMBASE
, ibitmap_byte_reg
, ibitmap_byte_address_reg
, 0);
161 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_IAND_IMM
, masked_iid_reg
, iid_reg
, 7);
162 MONO_EMIT_NEW_ICONST (cfg
, iid_one_bit_reg
, 1);
163 MONO_EMIT_NEW_BIALU (cfg
, OP_ISHL
, iid_bit_reg
, iid_one_bit_reg
, masked_iid_reg
);
164 MONO_EMIT_NEW_BIALU (cfg
, OP_IAND
, intf_bit_reg
, ibitmap_byte_reg
, iid_bit_reg
);
166 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADI1_MEMBASE
, ibitmap_byte_reg
, ibitmap_reg
, m_class_get_interface_id (klass
) >> 3);
167 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_AND_IMM
, intf_bit_reg
, ibitmap_byte_reg
, 1 << (m_class_get_interface_id (klass
) & 7));
173 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
174 * stored in "klass_reg" implements the interface "klass".
177 mini_emit_load_intf_bit_reg_class (MonoCompile
*cfg
, int intf_bit_reg
, int klass_reg
, MonoClass
*klass
)
179 mini_emit_interface_bitmap_check (cfg
, intf_bit_reg
, klass_reg
, m_class_offsetof_interface_bitmap (), klass
);
183 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
184 * stored in "vtable_reg" implements the interface "klass".
187 mini_emit_load_intf_bit_reg_vtable (MonoCompile
*cfg
, int intf_bit_reg
, int vtable_reg
, MonoClass
*klass
)
189 mini_emit_interface_bitmap_check (cfg
, intf_bit_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, interface_bitmap
), klass
);
193 * Emit code which checks whenever the interface id of @klass is smaller than
194 * than the value given by max_iid_reg.
197 mini_emit_max_iid_check (MonoCompile
*cfg
, int max_iid_reg
, MonoClass
*klass
,
198 MonoBasicBlock
*false_target
)
200 if (cfg
->compile_aot
) {
201 int iid_reg
= alloc_preg (cfg
);
202 MONO_EMIT_NEW_AOTCONST (cfg
, iid_reg
, klass
, MONO_PATCH_INFO_IID
);
203 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, max_iid_reg
, iid_reg
);
206 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, max_iid_reg
, m_class_get_interface_id (klass
));
208 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBLT_UN
, false_target
);
210 MONO_EMIT_NEW_COND_EXC (cfg
, LT_UN
, "InvalidCastException");
213 /* Same as above, but obtains max_iid from a vtable */
215 mini_emit_max_iid_check_vtable (MonoCompile
*cfg
, int vtable_reg
, MonoClass
*klass
,
216 MonoBasicBlock
*false_target
)
218 int max_iid_reg
= alloc_preg (cfg
);
220 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU4_MEMBASE
, max_iid_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, max_interface_id
));
221 mini_emit_max_iid_check (cfg
, max_iid_reg
, klass
, false_target
);
224 /* Same as above, but obtains max_iid from a klass */
226 mini_emit_max_iid_check_class (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
,
227 MonoBasicBlock
*false_target
)
229 int max_iid_reg
= alloc_preg (cfg
);
231 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU4_MEMBASE
, max_iid_reg
, klass_reg
, m_class_offsetof_max_interface_id ());
232 mini_emit_max_iid_check (cfg
, max_iid_reg
, klass
, false_target
);
236 mini_emit_class_check_branch (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
, int branch_op
, MonoBasicBlock
*target
)
238 if (cfg
->compile_aot
) {
239 int const_reg
= alloc_preg (cfg
);
240 MONO_EMIT_NEW_CLASSCONST (cfg
, const_reg
, klass
);
241 MONO_EMIT_NEW_BIALU (cfg
, OP_COMPARE
, -1, klass_reg
, const_reg
);
243 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, klass_reg
, klass
);
245 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, branch_op
, target
);
250 mini_emit_isninst_cast (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
, MonoBasicBlock
*false_target
, MonoBasicBlock
*true_target
)
252 mini_emit_isninst_cast_inst (cfg
, klass_reg
, klass
, NULL
, false_target
, true_target
);
256 mini_emit_iface_cast (MonoCompile
*cfg
, int vtable_reg
, MonoClass
*klass
, MonoBasicBlock
*false_target
, MonoBasicBlock
*true_target
)
258 int intf_reg
= alloc_preg (cfg
);
260 mini_emit_max_iid_check_vtable (cfg
, vtable_reg
, klass
, false_target
);
261 mini_emit_load_intf_bit_reg_vtable (cfg
, intf_reg
, vtable_reg
, klass
);
262 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, intf_reg
, 0);
264 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, true_target
);
266 MONO_EMIT_NEW_COND_EXC (cfg
, EQ
, "InvalidCastException");
270 * Variant of the above that takes a register to the class, not the vtable.
273 mini_emit_iface_class_cast (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
, MonoBasicBlock
*false_target
, MonoBasicBlock
*true_target
)
275 int intf_bit_reg
= alloc_preg (cfg
);
277 mini_emit_max_iid_check_class (cfg
, klass_reg
, klass
, false_target
);
278 mini_emit_load_intf_bit_reg_class (cfg
, intf_bit_reg
, klass_reg
, klass
);
279 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, intf_bit_reg
, 0);
281 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, true_target
);
283 MONO_EMIT_NEW_COND_EXC (cfg
, EQ
, "InvalidCastException");
288 mini_emit_castclass (MonoCompile
*cfg
, int obj_reg
, int klass_reg
, MonoClass
*klass
, MonoBasicBlock
*object_is_null
);
291 mini_emit_castclass_inst (MonoCompile
*cfg
, int obj_reg
, int klass_reg
, MonoClass
*klass
, MonoInst
*klass_inst
, MonoBasicBlock
*object_is_null
)
293 if (m_class_get_rank (klass
)) {
294 int rank_reg
= alloc_preg (cfg
);
295 int eclass_reg
= alloc_preg (cfg
);
297 g_assert (!klass_inst
);
298 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU1_MEMBASE
, rank_reg
, klass_reg
, m_class_offsetof_rank ());
299 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, rank_reg
, m_class_get_rank (klass
));
300 MONO_EMIT_NEW_COND_EXC (cfg
, NE_UN
, "InvalidCastException");
301 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
302 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, eclass_reg
, klass_reg
, m_class_offsetof_cast_class ());
303 if (m_class_get_cast_class (klass
) == mono_defaults
.object_class
) {
304 int parent_reg
= alloc_preg (cfg
);
305 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, parent_reg
, eclass_reg
, m_class_offsetof_parent ());
306 mini_emit_class_check_branch (cfg
, parent_reg
, m_class_get_parent (mono_defaults
.enum_class
), OP_PBNE_UN
, object_is_null
);
307 mini_emit_class_check (cfg
, eclass_reg
, mono_defaults
.enum_class
);
308 } else if (m_class_get_cast_class (klass
) == m_class_get_parent (mono_defaults
.enum_class
)) {
309 mini_emit_class_check_branch (cfg
, eclass_reg
, m_class_get_parent (mono_defaults
.enum_class
), OP_PBEQ
, object_is_null
);
310 mini_emit_class_check (cfg
, eclass_reg
, mono_defaults
.enum_class
);
311 } else if (m_class_get_cast_class (klass
) == mono_defaults
.enum_class
) {
312 mini_emit_class_check (cfg
, eclass_reg
, mono_defaults
.enum_class
);
313 } else if (mono_class_is_interface (m_class_get_cast_class (klass
))) {
314 mini_emit_iface_class_cast (cfg
, eclass_reg
, m_class_get_cast_class (klass
), NULL
, NULL
);
316 // Pass -1 as obj_reg to skip the check below for arrays of arrays
317 mini_emit_castclass (cfg
, -1, eclass_reg
, m_class_get_cast_class (klass
), object_is_null
);
320 if ((m_class_get_rank (klass
) == 1) && (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_SZARRAY
) && (obj_reg
!= -1)) {
321 /* Check that the object is a vector too */
322 int bounds_reg
= alloc_preg (cfg
);
323 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, bounds_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoArray
, bounds
));
324 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, bounds_reg
, 0);
325 MONO_EMIT_NEW_COND_EXC (cfg
, NE_UN
, "InvalidCastException");
328 int idepth_reg
= alloc_preg (cfg
);
329 int stypes_reg
= alloc_preg (cfg
);
330 int stype
= alloc_preg (cfg
);
332 mono_class_setup_supertypes (klass
);
334 if (m_class_get_idepth (klass
) > MONO_DEFAULT_SUPERTABLE_SIZE
) {
335 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU2_MEMBASE
, idepth_reg
, klass_reg
, m_class_offsetof_idepth ());
336 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, idepth_reg
, m_class_get_idepth (klass
));
337 MONO_EMIT_NEW_COND_EXC (cfg
, LT_UN
, "InvalidCastException");
339 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, stypes_reg
, klass_reg
, m_class_offsetof_supertypes ());
340 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, stype
, stypes_reg
, ((m_class_get_idepth (klass
) - 1) * SIZEOF_VOID_P
));
341 mini_emit_class_check_inst (cfg
, stype
, klass
, klass_inst
);
346 mini_emit_castclass (MonoCompile
*cfg
, int obj_reg
, int klass_reg
, MonoClass
*klass
, MonoBasicBlock
*object_is_null
)
348 mini_emit_castclass_inst (cfg
, obj_reg
, klass_reg
, klass
, NULL
, object_is_null
);
352 emit_special_array_iface_check (MonoCompile
*cfg
, MonoInst
*src
, MonoClass
* klass
, int vtable_reg
, MonoBasicBlock
*true_bb
, int context_used
)
354 MonoBasicBlock
*not_an_array
;
357 if (!m_class_is_array_special_interface (klass
))
360 rank_reg
= alloc_ireg (cfg
);
362 NEW_BBLOCK (cfg
, not_an_array
);
363 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU1_MEMBASE
, rank_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, rank
));
364 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, rank_reg
, 1);
365 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_IBNE_UN
, not_an_array
);
367 emit_castclass_with_cache_no_details (cfg
, src
, klass
, context_used
);
368 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, true_bb
);
370 MONO_START_BB (cfg
, not_an_array
);
375 * Returns NULL and set the cfg exception on error.
378 handle_castclass (MonoCompile
*cfg
, MonoClass
*klass
, MonoInst
*src
, int context_used
)
380 MonoBasicBlock
*is_null_bb
;
381 int obj_reg
= src
->dreg
;
382 MonoInst
*klass_inst
= NULL
;
384 if (MONO_INS_IS_PCONST_NULL (src
))
389 if (is_complex_isinst (klass
))
390 return emit_castclass_with_cache (cfg
, src
, klass
, context_used
);
392 klass_inst
= mini_emit_get_rgctx_klass (cfg
, context_used
, klass
, MONO_RGCTX_INFO_KLASS
);
395 NEW_BBLOCK (cfg
, is_null_bb
);
397 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, obj_reg
, 0);
398 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBEQ
, is_null_bb
);
400 mini_save_cast_details (cfg
, klass
, obj_reg
, FALSE
);
402 if (mono_class_is_interface (klass
)) {
403 int tmp_reg
= alloc_preg (cfg
);
404 #ifndef DISABLE_REMOTING
405 MonoBasicBlock
*interface_fail_bb
;
406 int klass_reg
= alloc_preg (cfg
);
408 NEW_BBLOCK (cfg
, interface_fail_bb
);
410 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
411 mini_emit_iface_cast (cfg
, tmp_reg
, klass
, interface_fail_bb
, is_null_bb
);
413 // iface bitmap check failed
414 MONO_START_BB (cfg
, interface_fail_bb
);
416 //Check if it's a rank zero array and emit fallback casting
417 emit_special_array_iface_check (cfg
, src
, klass
, tmp_reg
, is_null_bb
, context_used
);
419 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, tmp_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
421 mini_emit_class_check (cfg
, klass_reg
, mono_defaults
.transparent_proxy_class
);
423 tmp_reg
= alloc_preg (cfg
);
424 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, custom_type_info
));
425 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, tmp_reg
, 0);
426 MONO_EMIT_NEW_COND_EXC (cfg
, EQ
, "InvalidCastException");
428 MonoInst
*args
[1] = { src
};
429 MonoInst
*proxy_test_inst
= mono_emit_method_call (cfg
, mono_marshal_get_proxy_cancast (klass
), args
, NULL
);
430 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, proxy_test_inst
->dreg
, 0);
431 MONO_EMIT_NEW_COND_EXC (cfg
, EQ
, "InvalidCastException");
433 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, is_null_bb
);
435 MonoBasicBlock
*interface_fail_bb
= NULL
;
437 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
439 if (klass
->is_array_special_interface
) {
440 NEW_BBLOCK (cfg
, interface_fail_bb
);
441 mini_emit_iface_cast (cfg
, tmp_reg
, klass
, interface_fail_bb
, is_null_bb
);
442 // iface bitmap check failed
443 MONO_START_BB (cfg
, interface_fail_bb
);
445 //Check if it's a rank zero array and emit fallback casting
446 emit_special_array_iface_check (cfg
, src
, klass
, tmp_reg
, is_null_bb
, context_used
);
448 mini_emit_iface_cast (cfg
, tmp_reg
, klass
, NULL
, NULL
);
449 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, is_null_bb
);
452 } else if (mono_class_is_marshalbyref (klass
)) {
453 #ifndef DISABLE_REMOTING
454 MonoBasicBlock
*no_proxy_bb
, *fail_1_bb
;
455 int tmp_reg
= alloc_preg (cfg
);
456 int klass_reg
= alloc_preg (cfg
);
458 NEW_BBLOCK (cfg
, no_proxy_bb
);
460 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
461 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, tmp_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
462 mini_emit_class_check_branch (cfg
, klass_reg
, mono_defaults
.transparent_proxy_class
, OP_PBNE_UN
, no_proxy_bb
);
464 tmp_reg
= alloc_preg (cfg
);
465 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
466 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, tmp_reg
, MONO_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
468 tmp_reg
= alloc_preg (cfg
);
469 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, custom_type_info
));
470 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, tmp_reg
, 0);
471 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBEQ
, no_proxy_bb
);
473 NEW_BBLOCK (cfg
, fail_1_bb
);
475 mini_emit_isninst_cast (cfg
, klass_reg
, klass
, fail_1_bb
, is_null_bb
);
477 MONO_START_BB (cfg
, fail_1_bb
);
479 MonoInst
*args
[1] = { src
};
480 MonoInst
*proxy_test_inst
= mono_emit_method_call (cfg
, mono_marshal_get_proxy_cancast (klass
), args
, NULL
);
481 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, proxy_test_inst
->dreg
, 0);
482 MONO_EMIT_NEW_COND_EXC (cfg
, EQ
, "InvalidCastException");
484 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, is_null_bb
);
486 MONO_START_BB (cfg
, no_proxy_bb
);
488 mini_emit_castclass_inst (cfg
, obj_reg
, klass_reg
, klass
, klass_inst
, is_null_bb
);
490 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
493 int vtable_reg
= alloc_preg (cfg
);
494 int klass_reg
= alloc_preg (cfg
);
496 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, vtable_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
498 if (!m_class_get_rank (klass
) && !cfg
->compile_aot
&& !(cfg
->opt
& MONO_OPT_SHARED
) && mono_class_is_sealed (klass
)) {
499 /* the remoting code is broken, access the class for now */
500 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
501 MonoVTable
*vt
= mono_class_vtable_checked (cfg
->domain
, klass
, &cfg
->error
);
502 if (!is_ok (&cfg
->error
)) {
503 mono_cfg_set_exception (cfg
, MONO_EXCEPTION_MONO_ERROR
);
506 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, vtable_reg
, vt
);
508 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
509 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, klass_reg
, klass
);
511 MONO_EMIT_NEW_COND_EXC (cfg
, NE_UN
, "InvalidCastException");
513 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
514 mini_emit_castclass_inst (cfg
, obj_reg
, klass_reg
, klass
, klass_inst
, is_null_bb
);
518 MONO_START_BB (cfg
, is_null_bb
);
520 mini_reset_cast_details (cfg
);
526 * Returns NULL and set the cfg exception on error.
529 handle_isinst (MonoCompile
*cfg
, MonoClass
*klass
, MonoInst
*src
, int context_used
)
532 MonoBasicBlock
*is_null_bb
, *false_bb
, *end_bb
;
533 int obj_reg
= src
->dreg
;
534 int vtable_reg
= alloc_preg (cfg
);
535 int res_reg
= alloc_ireg_ref (cfg
);
536 MonoInst
*klass_inst
= NULL
;
539 if(is_complex_isinst (klass
))
540 return emit_isinst_with_cache (cfg
, src
, klass
, context_used
);
542 klass_inst
= mini_emit_get_rgctx_klass (cfg
, context_used
, klass
, MONO_RGCTX_INFO_KLASS
);
545 NEW_BBLOCK (cfg
, is_null_bb
);
546 NEW_BBLOCK (cfg
, false_bb
);
547 NEW_BBLOCK (cfg
, end_bb
);
549 /* Do the assignment at the beginning, so the other assignment can be if converted */
550 EMIT_NEW_UNALU (cfg
, ins
, OP_MOVE
, res_reg
, obj_reg
);
551 ins
->type
= STACK_OBJ
;
554 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, obj_reg
, 0);
555 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_IBEQ
, is_null_bb
);
557 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, vtable_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
559 if (mono_class_is_interface (klass
)) {
560 MonoBasicBlock
*interface_fail_bb
;
562 NEW_BBLOCK (cfg
, interface_fail_bb
);
564 mini_emit_iface_cast (cfg
, vtable_reg
, klass
, interface_fail_bb
, is_null_bb
);
565 MONO_START_BB (cfg
, interface_fail_bb
);
567 if (m_class_is_array_special_interface (klass
)) {
568 MonoBasicBlock
*not_an_array
;
570 int rank_reg
= alloc_ireg (cfg
);
572 NEW_BBLOCK (cfg
, not_an_array
);
573 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU1_MEMBASE
, rank_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, rank
));
574 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, rank_reg
, 1);
575 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_IBNE_UN
, not_an_array
);
577 MonoInst
*res_inst
= emit_isinst_with_cache (cfg
, src
, klass
, context_used
);
578 EMIT_NEW_UNALU (cfg
, move
, OP_MOVE
, res_reg
, res_inst
->dreg
);
579 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, end_bb
);
581 MONO_START_BB (cfg
, not_an_array
);
584 #ifndef DISABLE_REMOTING
585 int tmp_reg
, klass_reg
;
586 MonoBasicBlock
*call_proxy_isinst
;
588 NEW_BBLOCK (cfg
, call_proxy_isinst
);
590 klass_reg
= alloc_preg (cfg
);
591 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
593 mini_emit_class_check_branch (cfg
, klass_reg
, mono_defaults
.transparent_proxy_class
, OP_PBNE_UN
, false_bb
);
595 tmp_reg
= alloc_preg (cfg
);
596 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, custom_type_info
));
597 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, tmp_reg
, 0);
598 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBEQ
, false_bb
);
600 MONO_START_BB (cfg
, call_proxy_isinst
);
602 MonoInst
*args
[1] = { src
};
603 MonoInst
*proxy_test_inst
= mono_emit_method_call (cfg
, mono_marshal_get_proxy_cancast (klass
), args
, NULL
);
604 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, proxy_test_inst
->dreg
, 0);
605 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, is_null_bb
);
607 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, false_bb
);
610 } else if (mono_class_is_marshalbyref (klass
)) {
612 #ifndef DISABLE_REMOTING
613 int tmp_reg
, klass_reg
;
614 MonoBasicBlock
*no_proxy_bb
, *call_proxy_isinst
;
616 NEW_BBLOCK (cfg
, no_proxy_bb
);
617 NEW_BBLOCK (cfg
, call_proxy_isinst
);
619 klass_reg
= alloc_preg (cfg
);
620 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
622 mini_emit_class_check_branch (cfg
, klass_reg
, mono_defaults
.transparent_proxy_class
, OP_PBNE_UN
, no_proxy_bb
);
624 tmp_reg
= alloc_preg (cfg
);
625 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
626 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, tmp_reg
, MONO_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
628 tmp_reg
= alloc_preg (cfg
);
629 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, tmp_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, custom_type_info
));
630 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, tmp_reg
, 0);
631 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBEQ
, false_bb
);
633 mini_emit_isninst_cast (cfg
, klass_reg
, klass
, call_proxy_isinst
, is_null_bb
);
635 MONO_START_BB (cfg
, call_proxy_isinst
);
637 MonoInst
*args
[1] = { src
};
638 MonoInst
*proxy_test_inst
= mono_emit_method_call (cfg
, mono_marshal_get_proxy_cancast (klass
), args
, NULL
);
639 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, proxy_test_inst
->dreg
, 0);
640 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, is_null_bb
);
641 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, false_bb
);
643 MONO_START_BB (cfg
, no_proxy_bb
);
645 mini_emit_isninst_cast (cfg
, klass_reg
, klass
, false_bb
, is_null_bb
);
647 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
650 int klass_reg
= alloc_preg (cfg
);
652 if (m_class_get_rank (klass
)) {
653 int rank_reg
= alloc_preg (cfg
);
654 int eclass_reg
= alloc_preg (cfg
);
656 if ((m_class_get_rank (klass
) == 1) && (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_SZARRAY
)) {
657 /* Check that the object is a vector too */
658 int bounds_reg
= alloc_preg (cfg
);
659 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, bounds_reg
, obj_reg
, MONO_STRUCT_OFFSET (MonoArray
, bounds
));
660 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, bounds_reg
, 0);
661 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, false_bb
);
664 g_assert (!context_used
);
665 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg
, OP_LOADU1_MEMBASE
, rank_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, rank
));
666 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, rank_reg
, m_class_get_rank (klass
));
667 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, false_bb
);
668 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
669 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, eclass_reg
, klass_reg
, m_class_offsetof_cast_class ());
670 if (m_class_get_cast_class (klass
) == mono_defaults
.object_class
) {
671 int parent_reg
= alloc_preg (cfg
);
672 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, parent_reg
, eclass_reg
, m_class_offsetof_parent ());
673 mini_emit_class_check_branch (cfg
, parent_reg
, m_class_get_parent (mono_defaults
.enum_class
), OP_PBNE_UN
, is_null_bb
);
674 mini_emit_class_check_branch (cfg
, eclass_reg
, mono_defaults
.enum_class
, OP_PBEQ
, is_null_bb
);
675 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, false_bb
);
676 } else if (m_class_get_cast_class (klass
) == m_class_get_parent (mono_defaults
.enum_class
)) {
677 mini_emit_class_check_branch (cfg
, eclass_reg
, m_class_get_parent (mono_defaults
.enum_class
), OP_PBEQ
, is_null_bb
);
678 mini_emit_class_check_branch (cfg
, eclass_reg
, mono_defaults
.enum_class
, OP_PBEQ
, is_null_bb
);
679 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, false_bb
);
680 } else if (m_class_get_cast_class (klass
) == mono_defaults
.enum_class
) {
681 mini_emit_class_check_branch (cfg
, eclass_reg
, mono_defaults
.enum_class
, OP_PBEQ
, is_null_bb
);
682 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, false_bb
);
683 } else if (mono_class_is_interface (m_class_get_cast_class (klass
))) {
684 mini_emit_iface_class_cast (cfg
, eclass_reg
, m_class_get_cast_class (klass
), false_bb
, is_null_bb
);
686 /* the is_null_bb target simply copies the input register to the output */
687 mini_emit_isninst_cast (cfg
, eclass_reg
, m_class_get_cast_class (klass
), false_bb
, is_null_bb
);
689 } else if (mono_class_is_nullable (klass
)) {
690 g_assert (!context_used
);
691 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
692 /* the is_null_bb target simply copies the input register to the output */
693 mini_emit_isninst_cast (cfg
, klass_reg
, m_class_get_cast_class (klass
), false_bb
, is_null_bb
);
695 if (!cfg
->compile_aot
&& !(cfg
->opt
& MONO_OPT_SHARED
) && mono_class_is_sealed (klass
)) {
696 g_assert (!context_used
);
697 /* the remoting code is broken, access the class for now */
698 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
699 MonoVTable
*vt
= mono_class_vtable_checked (cfg
->domain
, klass
, &cfg
->error
);
700 if (!is_ok (&cfg
->error
)) {
701 mono_cfg_set_exception (cfg
, MONO_EXCEPTION_MONO_ERROR
);
704 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, vtable_reg
, vt
);
706 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
707 MONO_EMIT_NEW_BIALU_IMM (cfg
, OP_COMPARE_IMM
, -1, klass_reg
, klass
);
709 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_PBNE_UN
, false_bb
);
710 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, is_null_bb
);
712 MONO_EMIT_NEW_LOAD_MEMBASE (cfg
, klass_reg
, vtable_reg
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
713 /* the is_null_bb target simply copies the input register to the output */
714 mini_emit_isninst_cast_inst (cfg
, klass_reg
, klass
, klass_inst
, false_bb
, is_null_bb
);
719 MONO_START_BB (cfg
, false_bb
);
721 MONO_EMIT_NEW_PCONST (cfg
, res_reg
, NULL
);
722 MONO_EMIT_NEW_BRANCH_BLOCK (cfg
, OP_BR
, end_bb
);
724 MONO_START_BB (cfg
, is_null_bb
);
726 MONO_START_BB (cfg
, end_bb
);
732 mono_decompose_typecheck (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
734 MonoInst
*ret
, *move
, *source
;
735 MonoClass
*klass
= ins
->klass
;
736 int context_used
= mini_class_check_context_used (cfg
, klass
);
737 int is_isinst
= ins
->opcode
== OP_ISINST
;
738 g_assert (is_isinst
|| ins
->opcode
== OP_CASTCLASS
);
739 source
= get_vreg_to_inst (cfg
, ins
->sreg1
);
740 if (!source
|| source
== (MonoInst
*) -1)
741 source
= mono_compile_create_var_for_vreg (cfg
, m_class_get_byval_arg (mono_defaults
.object_class
), OP_LOCAL
, ins
->sreg1
);
742 g_assert (source
&& source
!= (MonoInst
*) -1);
744 MonoBasicBlock
*first_bb
;
745 NEW_BBLOCK (cfg
, first_bb
);
748 if (mini_class_has_reference_variant_generic_argument (cfg
, klass
, context_used
)) {
750 ret
= emit_isinst_with_cache (cfg
, source
, klass
, context_used
);
752 ret
= emit_castclass_with_cache (cfg
, source
, klass
, context_used
);
756 ret
= handle_isinst (cfg
, klass
, source
, context_used
);
758 ret
= handle_castclass (cfg
, klass
, source
, context_used
);
760 EMIT_NEW_UNALU (cfg
, move
, OP_MOVE
, ins
->dreg
, ret
->dreg
);
762 g_assert (cfg
->cbb
->code
|| first_bb
->code
);
763 MonoInst
*prev
= ins
->prev
;
764 mono_replace_ins (cfg
, bb
, ins
, &prev
, first_bb
, cfg
->cbb
);
768 mono_decompose_typechecks (MonoCompile
*cfg
)
770 gboolean found_typetest
= FALSE
;
771 for (MonoBasicBlock
*bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
773 MONO_BB_FOR_EACH_INS (bb
, ins
) {
774 switch (ins
->opcode
) {
777 found_typetest
= TRUE
;
778 mono_decompose_typecheck (cfg
, bb
, ins
);
783 if ((cfg
->verbose_level
> 2) && found_typetest
)
784 mono_print_code (cfg
, "AFTER DECOMPOSE TYPE_CHECKS");
789 //API used by method-to-ir.c
791 mini_emit_class_check (MonoCompile
*cfg
, int klass_reg
, MonoClass
*klass
)
793 mini_emit_class_check_inst (cfg
, klass_reg
, klass
, NULL
);
798 MONO_EMPTY_SOURCE_FILE (type_checking
);