Add logic to object array typecheck to handle arrays of unmanaged pointers (#14733)
[mono-project.git] / mono / mini / type-checking.c
blob33638d8d6b23425c322ee8919e24b7e00dec0b2a
1 /**
2 * \file
3 */
5 #include <config.h>
6 #include <mono/utils/mono-compiler.h>
8 #ifndef DISABLE_JIT
10 #include "mini.h"
11 #include "ir-emit.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)
18 static int
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;
26 static void
27 emit_cached_check_args (MonoCompile *cfg, MonoInst *obj, MonoClass *klass, int context_used, MonoInst *args[3])
29 args [0] = obj;
31 if (context_used) {
32 MonoInst *cache_ins;
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, TARGET_SIZEOF_VOID_P);
39 args [2] = cache_ins; /* cache */
40 } else {
41 int idx;
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));
50 static MonoInst*
51 emit_isinst_with_cache (MonoCompile *cfg, MonoInst *obj, MonoClass *klass, int context_used)
53 MonoInst *args [3];
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);
60 static MonoInst*
61 emit_castclass_with_cache_no_details (MonoCompile *cfg, MonoInst *obj, MonoClass *klass, int context_used)
63 MonoInst *args [3];
64 MonoMethod *mono_castclass = mono_marshal_get_castclass_with_cache ();
65 MonoInst *res;
67 emit_cached_check_args (cfg, obj, klass, context_used, args);
69 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
71 return res;
74 static MonoInst*
75 emit_castclass_with_cache (MonoCompile *cfg, MonoInst *obj, MonoClass *klass, int context_used)
77 MonoInst *args [3];
78 MonoMethod *mono_castclass = mono_marshal_get_castclass_with_cache ();
79 MonoInst *res;
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);
87 return res;
90 static inline void
91 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
93 if (klass_inst) {
94 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
95 } else {
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");
103 static void
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) * TARGET_SIZEOF_VOID_P));
119 if (klass_ins) {
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);
125 } else {
126 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, (gsize)klass);
128 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
132 static void
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
137 MonoInst *args [2];
138 MonoInst *res, *ins;
139 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
140 MONO_ADD_INS (cfg->cbb, ins);
141 args [0] = 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);
145 #else
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);
165 } else {
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));
169 #endif
173 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
174 * stored in "klass_reg" implements the interface "klass".
176 static void
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".
186 static void
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.
196 static void
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);
205 else
206 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, m_class_get_interface_id (klass));
207 if (false_target)
208 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
209 else
210 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
213 /* Same as above, but obtains max_iid from a vtable */
214 static void
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 */
225 static void
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);
235 static inline void
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);
242 } else {
243 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, (gsize)klass);
245 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
249 static void
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);
255 static void
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);
263 if (true_target)
264 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
265 else
266 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
270 * Variant of the above that takes a register to the class, not the vtable.
272 static void
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);
280 if (true_target)
281 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
282 else
283 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
287 static void
288 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
290 static void
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);
299 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, m_class_offsetof_rank ());
300 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, m_class_get_rank (klass));
301 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
303 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
304 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, m_class_offsetof_cast_class ());
305 if (m_class_is_array_special_interface (m_class_get_cast_class (klass))) {
306 MonoInst *src;
308 MONO_INST_NEW (cfg, src, OP_LOCAL);
309 src->dreg = obj_reg;
310 emit_castclass_with_cache_no_details (cfg, src, klass, 0);
311 } else if (m_class_get_cast_class (klass) == mono_defaults.object_class) {
312 int parent_reg = alloc_preg (cfg);
313 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, m_class_offsetof_parent ());
314 mini_emit_class_check_branch (cfg, parent_reg, m_class_get_parent (mono_defaults.enum_class), OP_PBNE_UN, object_is_null);
315 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
316 } else if (m_class_get_cast_class (klass) == m_class_get_parent (mono_defaults.enum_class)) {
317 mini_emit_class_check_branch (cfg, eclass_reg, m_class_get_parent (mono_defaults.enum_class), OP_PBEQ, object_is_null);
318 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
319 } else if (m_class_get_cast_class (klass) == mono_defaults.enum_class) {
320 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
321 } else if (mono_class_is_interface (m_class_get_cast_class (klass))) {
322 mini_emit_iface_class_cast (cfg, eclass_reg, m_class_get_cast_class (klass), NULL, NULL);
323 } else {
324 // Pass -1 as obj_reg to skip the check below for arrays of arrays
325 mini_emit_castclass (cfg, -1, eclass_reg, m_class_get_cast_class (klass), object_is_null);
328 if ((m_class_get_rank (klass) == 1) && (m_class_get_byval_arg (klass)->type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
329 /* Check that the object is a vector too */
330 int bounds_reg = alloc_preg (cfg);
331 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
332 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
333 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
335 } else {
336 int idepth_reg = alloc_preg (cfg);
337 int stypes_reg = alloc_preg (cfg);
338 int stype = alloc_preg (cfg);
340 mono_class_setup_supertypes (klass);
342 if (m_class_get_idepth (klass) > MONO_DEFAULT_SUPERTABLE_SIZE) {
343 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, m_class_offsetof_idepth ());
344 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, m_class_get_idepth (klass));
345 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
347 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, m_class_offsetof_supertypes ());
348 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((m_class_get_idepth (klass) - 1) * TARGET_SIZEOF_VOID_P));
349 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
353 static void
354 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
356 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
359 static void
360 emit_special_array_iface_check (MonoCompile *cfg, MonoInst *src, MonoClass* klass, int vtable_reg, MonoBasicBlock *true_bb, int context_used)
362 MonoBasicBlock *not_an_array;
363 int rank_reg;
365 if (!m_class_is_array_special_interface (klass))
366 return;
368 rank_reg = alloc_ireg (cfg);
370 NEW_BBLOCK (cfg, not_an_array);
371 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
372 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 1);
373 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, not_an_array);
375 emit_castclass_with_cache_no_details (cfg, src, klass, context_used);
376 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, true_bb);
378 MONO_START_BB (cfg, not_an_array);
383 * Returns NULL and set the cfg exception on error.
385 static MonoInst*
386 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
388 MonoBasicBlock *is_null_bb;
389 int obj_reg = src->dreg;
390 MonoInst *klass_inst = NULL;
392 if (MONO_INS_IS_PCONST_NULL (src))
393 return src;
395 if (context_used) {
397 if (is_complex_isinst (klass))
398 return emit_castclass_with_cache (cfg, src, klass, context_used);
400 klass_inst = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
403 NEW_BBLOCK (cfg, is_null_bb);
405 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
406 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
408 mini_save_cast_details (cfg, klass, obj_reg, FALSE);
410 if (mono_class_is_interface (klass)) {
411 int tmp_reg = alloc_preg (cfg);
412 #ifndef DISABLE_REMOTING
413 MonoBasicBlock *interface_fail_bb;
414 int klass_reg = alloc_preg (cfg);
416 NEW_BBLOCK (cfg, interface_fail_bb);
418 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
419 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, is_null_bb);
421 // iface bitmap check failed
422 MONO_START_BB (cfg, interface_fail_bb);
424 //Check if it's a rank zero array and emit fallback casting
425 emit_special_array_iface_check (cfg, src, klass, tmp_reg, is_null_bb, context_used);
427 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
429 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
431 tmp_reg = alloc_preg (cfg);
432 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
433 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
434 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
436 MonoInst *args [1] = { src };
437 MonoInst *proxy_test_inst = mono_emit_method_call (cfg, mono_marshal_get_proxy_cancast (klass), args, NULL);
438 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, proxy_test_inst->dreg, 0);
439 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
441 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
442 #else
443 MonoBasicBlock *interface_fail_bb = NULL;
445 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
447 if (m_class_is_array_special_interface (klass)) {
448 NEW_BBLOCK (cfg, interface_fail_bb);
449 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, is_null_bb);
450 // iface bitmap check failed
451 MONO_START_BB (cfg, interface_fail_bb);
453 //Check if it's a rank zero array and emit fallback casting
454 emit_special_array_iface_check (cfg, src, klass, tmp_reg, is_null_bb, context_used);
455 } else {
456 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
457 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
459 #endif
460 } else if (mono_class_is_marshalbyref (klass)) {
461 #ifndef DISABLE_REMOTING
462 MonoBasicBlock *no_proxy_bb, *fail_1_bb;
463 int tmp_reg = alloc_preg (cfg);
464 int klass_reg = alloc_preg (cfg);
466 NEW_BBLOCK (cfg, no_proxy_bb);
468 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
469 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
470 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
472 tmp_reg = alloc_preg (cfg);
473 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
474 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
476 tmp_reg = alloc_preg (cfg);
477 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
478 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
479 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
481 NEW_BBLOCK (cfg, fail_1_bb);
483 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, is_null_bb);
485 MONO_START_BB (cfg, fail_1_bb);
487 MonoInst *args [1] = { src };
488 MonoInst *proxy_test_inst = mono_emit_method_call (cfg, mono_marshal_get_proxy_cancast (klass), args, NULL);
489 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, proxy_test_inst->dreg, 0);
490 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
492 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
494 MONO_START_BB (cfg, no_proxy_bb);
496 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
497 #else
498 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
499 #endif
500 } else {
501 int vtable_reg = alloc_preg (cfg);
502 int klass_reg = alloc_preg (cfg);
504 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
506 if (!m_class_get_rank (klass) && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && mono_class_is_sealed (klass)) {
507 /* the remoting code is broken, access the class for now */
508 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
509 MonoVTable *vt = mono_class_vtable_checked (cfg->domain, klass, &cfg->error);
510 if (!is_ok (&cfg->error)) {
511 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
512 return NULL;
514 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, (gsize)vt);
515 } else {
516 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
517 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, (gsize)klass);
519 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
520 } else {
521 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
522 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
526 MONO_START_BB (cfg, is_null_bb);
528 mini_reset_cast_details (cfg);
530 return src;
534 * Returns NULL and set the cfg exception on error.
536 static MonoInst*
537 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
539 MonoInst *ins;
540 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
541 int obj_reg = src->dreg;
542 int vtable_reg = alloc_preg (cfg);
543 int res_reg = alloc_ireg_ref (cfg);
544 MonoInst *klass_inst = NULL;
546 if (context_used) {
547 if(is_complex_isinst (klass))
548 return emit_isinst_with_cache (cfg, src, klass, context_used);
550 klass_inst = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
553 NEW_BBLOCK (cfg, is_null_bb);
554 NEW_BBLOCK (cfg, false_bb);
555 NEW_BBLOCK (cfg, end_bb);
557 /* Do the assignment at the beginning, so the other assignment can be if converted */
558 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
559 ins->type = STACK_OBJ;
560 ins->klass = klass;
562 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
563 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
565 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
567 if (mono_class_is_interface (klass)) {
568 MonoBasicBlock *interface_fail_bb;
570 NEW_BBLOCK (cfg, interface_fail_bb);
572 mini_emit_iface_cast (cfg, vtable_reg, klass, interface_fail_bb, is_null_bb);
573 MONO_START_BB (cfg, interface_fail_bb);
575 if (m_class_is_array_special_interface (klass)) {
576 MonoBasicBlock *not_an_array;
577 MonoInst *move;
578 int rank_reg = alloc_ireg (cfg);
580 NEW_BBLOCK (cfg, not_an_array);
581 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
582 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 1);
583 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, not_an_array);
585 MonoInst *res_inst = emit_isinst_with_cache (cfg, src, klass, context_used);
586 EMIT_NEW_UNALU (cfg, move, OP_MOVE, res_reg, res_inst->dreg);
587 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
589 MONO_START_BB (cfg, not_an_array);
592 #ifndef DISABLE_REMOTING
593 int tmp_reg, klass_reg;
594 MonoBasicBlock *call_proxy_isinst;
596 NEW_BBLOCK (cfg, call_proxy_isinst);
598 klass_reg = alloc_preg (cfg);
599 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
601 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
603 tmp_reg = alloc_preg (cfg);
604 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
605 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
606 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
608 MONO_START_BB (cfg, call_proxy_isinst);
610 MonoInst *args [1] = { src };
611 MonoInst *proxy_test_inst = mono_emit_method_call (cfg, mono_marshal_get_proxy_cancast (klass), args, NULL);
612 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, proxy_test_inst->dreg, 0);
613 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_null_bb);
614 #else
615 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
616 #endif
618 } else if (mono_class_is_marshalbyref (klass)) {
620 #ifndef DISABLE_REMOTING
621 int tmp_reg, klass_reg;
622 MonoBasicBlock *no_proxy_bb, *call_proxy_isinst;
624 NEW_BBLOCK (cfg, no_proxy_bb);
625 NEW_BBLOCK (cfg, call_proxy_isinst);
627 klass_reg = alloc_preg (cfg);
628 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
630 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
632 tmp_reg = alloc_preg (cfg);
633 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
634 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
636 tmp_reg = alloc_preg (cfg);
637 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
638 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
639 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
641 mini_emit_isninst_cast (cfg, klass_reg, klass, call_proxy_isinst, is_null_bb);
643 MONO_START_BB (cfg, call_proxy_isinst);
645 MonoInst *args [1] = { src };
646 MonoInst *proxy_test_inst = mono_emit_method_call (cfg, mono_marshal_get_proxy_cancast (klass), args, NULL);
647 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, proxy_test_inst->dreg, 0);
648 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_null_bb);
649 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
651 MONO_START_BB (cfg, no_proxy_bb);
653 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, is_null_bb);
654 #else
655 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
656 #endif
657 } else {
658 int klass_reg = alloc_preg (cfg);
660 if (m_class_get_rank (klass)) {
661 int rank_reg = alloc_preg (cfg);
662 int eclass_reg = alloc_preg (cfg);
664 if ((m_class_get_rank (klass) == 1) && (m_class_get_byval_arg (klass)->type == MONO_TYPE_SZARRAY)) {
665 /* Check that the object is a vector too */
666 int bounds_reg = alloc_preg (cfg);
667 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
668 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
669 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
672 g_assert (!context_used);
673 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
674 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, m_class_get_rank (klass));
675 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
676 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
677 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, m_class_offsetof_cast_class ());
678 if (m_class_is_array_special_interface (m_class_get_cast_class (klass))) {
679 MonoInst *move, *res_inst;
681 res_inst = emit_isinst_with_cache (cfg, src, klass, context_used);
682 EMIT_NEW_UNALU (cfg, move, OP_MOVE, res_reg, res_inst->dreg);
683 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
684 } else if (m_class_get_cast_class (klass) == mono_defaults.object_class) {
685 int parent_reg, class_kind_reg;
686 MonoBasicBlock *pointer_check_bb;
688 NEW_BBLOCK (cfg, pointer_check_bb);
690 parent_reg = alloc_preg (cfg);
691 class_kind_reg = alloc_preg (cfg);
692 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, m_class_offsetof_parent ());
693 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, class_kind_reg, eclass_reg, m_class_offsetof_class_kind ());
695 // Check if the parent class of the element is not System.ValueType
696 mini_emit_class_check_branch (cfg, parent_reg, m_class_get_parent (mono_defaults.enum_class), OP_PBNE_UN, pointer_check_bb);
697 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
698 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
700 MONO_START_BB (cfg, pointer_check_bb);
701 // Check if the parent class of the element is non-null, else manually check the type
702 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, parent_reg, NULL);
703 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_null_bb);
704 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, class_kind_reg, MONO_CLASS_POINTER);
705 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
706 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
707 } else if (m_class_get_cast_class (klass) == m_class_get_parent (mono_defaults.enum_class)) {
708 mini_emit_class_check_branch (cfg, eclass_reg, m_class_get_parent (mono_defaults.enum_class), OP_PBEQ, is_null_bb);
709 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
710 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
711 } else if (m_class_get_cast_class (klass) == mono_defaults.enum_class) {
712 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
713 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
714 } else if (mono_class_is_interface (m_class_get_cast_class (klass))) {
715 mini_emit_iface_class_cast (cfg, eclass_reg, m_class_get_cast_class (klass), false_bb, is_null_bb);
716 } else {
717 /* the is_null_bb target simply copies the input register to the output */
718 mini_emit_isninst_cast (cfg, eclass_reg, m_class_get_cast_class (klass), false_bb, is_null_bb);
720 } else if (mono_class_is_nullable (klass)) {
721 g_assert (!context_used);
722 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
723 /* the is_null_bb target simply copies the input register to the output */
724 mini_emit_isninst_cast (cfg, klass_reg, m_class_get_cast_class (klass), false_bb, is_null_bb);
725 } else {
726 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && mono_class_is_sealed (klass)) {
727 g_assert (!context_used);
728 /* the remoting code is broken, access the class for now */
729 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
730 MonoVTable *vt = mono_class_vtable_checked (cfg->domain, klass, &cfg->error);
731 if (!is_ok (&cfg->error)) {
732 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
733 return NULL;
735 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, (gsize)vt);
736 } else {
737 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
738 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, (gsize)klass);
740 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
741 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
742 } else {
743 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
744 /* the is_null_bb target simply copies the input register to the output */
745 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
750 MONO_START_BB (cfg, false_bb);
752 MONO_EMIT_NEW_PCONST (cfg, res_reg, NULL);
753 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
755 MONO_START_BB (cfg, is_null_bb);
757 MONO_START_BB (cfg, end_bb);
759 return ins;
762 static void
763 mono_decompose_typecheck (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
765 MonoInst *ret, *move, *source;
766 MonoClass *klass = ins->klass;
767 int context_used = mini_class_check_context_used (cfg, klass);
768 int is_isinst = ins->opcode == OP_ISINST;
769 g_assert (is_isinst || ins->opcode == OP_CASTCLASS);
770 source = get_vreg_to_inst (cfg, ins->sreg1);
771 if (!source || source == (MonoInst *) -1)
772 source = mono_compile_create_var_for_vreg (cfg, mono_get_object_type (), OP_LOCAL, ins->sreg1);
773 g_assert (source && source != (MonoInst *) -1);
775 MonoBasicBlock *first_bb;
776 NEW_BBLOCK (cfg, first_bb);
777 cfg->cbb = first_bb;
779 if (mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
780 if (is_isinst)
781 ret = emit_isinst_with_cache (cfg, source, klass, context_used);
782 else
783 ret = emit_castclass_with_cache (cfg, source, klass, context_used);
785 } else {
786 if (is_isinst)
787 ret = handle_isinst (cfg, klass, source, context_used);
788 else
789 ret = handle_castclass (cfg, klass, source, context_used);
791 EMIT_NEW_UNALU (cfg, move, OP_MOVE, ins->dreg, ret->dreg);
793 g_assert (cfg->cbb->code || first_bb->code);
794 MonoInst *prev = ins->prev;
795 mono_replace_ins (cfg, bb, ins, &prev, first_bb, cfg->cbb);
798 void
799 mono_decompose_typechecks (MonoCompile *cfg)
801 gboolean found_typetest = FALSE;
802 for (MonoBasicBlock *bb = cfg->bb_entry; bb; bb = bb->next_bb) {
803 MonoInst *ins;
804 MONO_BB_FOR_EACH_INS (bb, ins) {
805 switch (ins->opcode) {
806 case OP_ISINST:
807 case OP_CASTCLASS:
808 found_typetest = TRUE;
809 mono_decompose_typecheck (cfg, bb, ins);
810 break;
814 if ((cfg->verbose_level > 2) && found_typetest)
815 mono_print_code (cfg, "AFTER DECOMPOSE TYPE_CHECKS");
820 //API used by method-to-ir.c
821 void
822 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
824 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
827 #else
829 MONO_EMPTY_SOURCE_FILE (type_checking);
830 #endif