2 * method-to-ir.c: Convert CIL to the JIT internal representation
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <mono/utils/mono-compiler.h>
29 #ifdef HAVE_SYS_TIME_H
37 #include <mono/utils/memcheck.h>
39 #include <mono/metadata/abi-details.h>
40 #include <mono/metadata/assembly.h>
41 #include <mono/metadata/attrdefs.h>
42 #include <mono/metadata/loader.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/class.h>
45 #include <mono/metadata/object.h>
46 #include <mono/metadata/exception.h>
47 #include <mono/metadata/opcodes.h>
48 #include <mono/metadata/mono-endian.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/marshal.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/mono-debug.h>
54 #include <mono/metadata/mono-debug-debugger.h>
55 #include <mono/metadata/gc-internals.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/profiler-private.h>
60 #include <mono/metadata/profiler.h>
61 #include <mono/metadata/monitor.h>
62 #include <mono/metadata/debug-mono-symfile.h>
63 #include <mono/utils/mono-compiler.h>
64 #include <mono/utils/mono-memory-model.h>
65 #include <mono/utils/mono-error-internals.h>
66 #include <mono/metadata/mono-basic-block.h>
67 #include <mono/metadata/reflection-internals.h>
68 #include <mono/utils/mono-threads-coop.h>
74 #include "jit-icalls.h"
76 #include "debugger-agent.h"
77 #include "seq-points.h"
78 #include "aot-compiler.h"
79 #include "mini-llvm.h"
81 #define BRANCH_COST 10
82 #define INLINE_LENGTH_LIMIT 20
84 /* These have 'cfg' as an implicit argument */
85 #define INLINE_FAILURE(msg) do { \
86 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
87 inline_failure (cfg, msg); \
88 goto exception_exit; \
91 #define CHECK_CFG_EXCEPTION do {\
92 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
93 goto exception_exit; \
95 #define FIELD_ACCESS_FAILURE(method, field) do { \
96 field_access_failure ((cfg), (method), (field)); \
97 goto exception_exit; \
99 #define GENERIC_SHARING_FAILURE(opcode) do { \
100 if (cfg->gshared) { \
101 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
102 goto exception_exit; \
105 #define GSHAREDVT_FAILURE(opcode) do { \
106 if (cfg->gsharedvt) { \
107 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
108 goto exception_exit; \
111 #define OUT_OF_MEMORY_FAILURE do { \
112 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
113 mono_error_set_out_of_memory (&cfg->error, ""); \
114 goto exception_exit; \
116 #define DISABLE_AOT(cfg) do { \
117 if ((cfg)->verbose_level >= 2) \
118 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
119 (cfg)->disable_aot = TRUE; \
121 #define LOAD_ERROR do { \
122 break_on_unverified (); \
123 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
124 goto exception_exit; \
127 #define TYPE_LOAD_ERROR(klass) do { \
128 cfg->exception_ptr = klass; \
132 #define CHECK_CFG_ERROR do {\
133 if (!mono_error_ok (&cfg->error)) { \
134 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
135 goto mono_error_exit; \
139 /* Determine whenever 'ins' represents a load of the 'this' argument */
140 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
142 static int ldind_to_load_membase (int opcode);
143 static int stind_to_store_membase (int opcode);
145 int mono_op_to_op_imm (int opcode);
146 int mono_op_to_op_imm_noemul (int opcode);
148 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
150 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
151 guchar *ip, guint real_offset, gboolean inline_always);
153 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
155 /* helper methods signatures */
156 static MonoMethodSignature *helper_sig_domain_get;
157 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
158 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
159 static MonoMethodSignature *helper_sig_jit_thread_attach;
161 /* type loading helpers */
162 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, System.Runtime.CompilerServices, RuntimeHelpers)
163 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, System.Diagnostics, DebuggableAttribute)
166 * Instruction metadata
174 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
175 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
181 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
186 /* keep in sync with the enum in mini.h */
189 #include "mini-ops.h"
194 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
195 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
197 * This should contain the index of the last sreg + 1. This is not the same
198 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
200 const gint8 ins_sreg_counts[] = {
201 #include "mini-ops.h"
206 #define MONO_INIT_VARINFO(vi,id) do { \
207 (vi)->range.first_use.pos.bid = 0xffff; \
213 mono_alloc_ireg (MonoCompile *cfg)
215 return alloc_ireg (cfg);
219 mono_alloc_lreg (MonoCompile *cfg)
221 return alloc_lreg (cfg);
225 mono_alloc_freg (MonoCompile *cfg)
227 return alloc_freg (cfg);
231 mono_alloc_preg (MonoCompile *cfg)
233 return alloc_preg (cfg);
237 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
239 return alloc_dreg (cfg, stack_type);
243 * mono_alloc_ireg_ref:
245 * Allocate an IREG, and mark it as holding a GC ref.
248 mono_alloc_ireg_ref (MonoCompile *cfg)
250 return alloc_ireg_ref (cfg);
254 * mono_alloc_ireg_mp:
256 * Allocate an IREG, and mark it as holding a managed pointer.
259 mono_alloc_ireg_mp (MonoCompile *cfg)
261 return alloc_ireg_mp (cfg);
265 * mono_alloc_ireg_copy:
267 * Allocate an IREG with the same GC type as VREG.
270 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
272 if (vreg_is_ref (cfg, vreg))
273 return alloc_ireg_ref (cfg);
274 else if (vreg_is_mp (cfg, vreg))
275 return alloc_ireg_mp (cfg);
277 return alloc_ireg (cfg);
281 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
286 type = mini_get_underlying_type (type);
288 switch (type->type) {
301 case MONO_TYPE_FNPTR:
303 case MONO_TYPE_CLASS:
304 case MONO_TYPE_STRING:
305 case MONO_TYPE_OBJECT:
306 case MONO_TYPE_SZARRAY:
307 case MONO_TYPE_ARRAY:
311 #if SIZEOF_REGISTER == 8
317 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
320 case MONO_TYPE_VALUETYPE:
321 if (type->data.klass->enumtype) {
322 type = mono_class_enum_basetype (type->data.klass);
325 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
328 case MONO_TYPE_TYPEDBYREF:
330 case MONO_TYPE_GENERICINST:
331 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
333 type = &type->data.generic_class->container_class->byval_arg;
337 g_assert (cfg->gshared);
338 if (mini_type_var_is_vt (type))
341 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
343 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
349 mono_print_bb (MonoBasicBlock *bb, const char *msg)
354 printf ("\n%s %d: [IN: ", msg, bb->block_num);
355 for (i = 0; i < bb->in_count; ++i)
356 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
358 for (i = 0; i < bb->out_count; ++i)
359 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
361 for (tree = bb->code; tree; tree = tree->next)
362 mono_print_ins_index (-1, tree);
366 mono_create_helper_signatures (void)
368 helper_sig_domain_get = mono_create_icall_signature ("ptr");
369 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
370 helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
371 helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
374 static MONO_NEVER_INLINE void
375 break_on_unverified (void)
377 if (mini_get_debug_options ()->break_on_unverified)
381 static MONO_NEVER_INLINE void
382 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
384 char *method_fname = mono_method_full_name (method, TRUE);
385 char *field_fname = mono_field_full_name (field);
386 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
387 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
388 g_free (method_fname);
389 g_free (field_fname);
392 static MONO_NEVER_INLINE void
393 inline_failure (MonoCompile *cfg, const char *msg)
395 if (cfg->verbose_level >= 2)
396 printf ("inline failed: %s\n", msg);
397 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
400 static MONO_NEVER_INLINE void
401 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
403 if (cfg->verbose_level > 2) \
404 printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), line);
405 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
408 static MONO_NEVER_INLINE void
409 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
411 cfg->exception_message = g_strdup_printf ("gsharedvt failed for method %s.%s.%s/%d opcode %s %s:%d", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), file, line);
412 if (cfg->verbose_level >= 2)
413 printf ("%s\n", cfg->exception_message);
414 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
418 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
419 * foo<T> (int i) { ldarg.0; box T; }
421 #define UNVERIFIED do { \
422 if (cfg->gsharedvt) { \
423 if (cfg->verbose_level > 2) \
424 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
425 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
426 goto exception_exit; \
428 break_on_unverified (); \
432 #define GET_BBLOCK(cfg,tblock,ip) do { \
433 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
435 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
436 NEW_BBLOCK (cfg, (tblock)); \
437 (tblock)->cil_code = (ip); \
438 ADD_BBLOCK (cfg, (tblock)); \
442 #if defined(TARGET_X86) || defined(TARGET_AMD64)
443 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
444 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
445 (dest)->dreg = alloc_ireg_mp ((cfg)); \
446 (dest)->sreg1 = (sr1); \
447 (dest)->sreg2 = (sr2); \
448 (dest)->inst_imm = (imm); \
449 (dest)->backend.shift_amount = (shift); \
450 MONO_ADD_INS ((cfg)->cbb, (dest)); \
454 /* Emit conversions so both operands of a binary opcode are of the same type */
456 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
458 MonoInst *arg1 = *arg1_ref;
459 MonoInst *arg2 = *arg2_ref;
462 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
463 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
466 /* Mixing r4/r8 is allowed by the spec */
467 if (arg1->type == STACK_R4) {
468 int dreg = alloc_freg (cfg);
470 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
471 conv->type = STACK_R8;
475 if (arg2->type == STACK_R4) {
476 int dreg = alloc_freg (cfg);
478 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
479 conv->type = STACK_R8;
485 #if SIZEOF_REGISTER == 8
486 /* FIXME: Need to add many more cases */
487 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
490 int dr = alloc_preg (cfg);
491 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
492 (ins)->sreg2 = widen->dreg;
497 #define ADD_BINOP(op) do { \
498 MONO_INST_NEW (cfg, ins, (op)); \
500 ins->sreg1 = sp [0]->dreg; \
501 ins->sreg2 = sp [1]->dreg; \
502 type_from_op (cfg, ins, sp [0], sp [1]); \
504 /* Have to insert a widening op */ \
505 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
506 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
507 MONO_ADD_INS ((cfg)->cbb, (ins)); \
508 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
511 #define ADD_UNOP(op) do { \
512 MONO_INST_NEW (cfg, ins, (op)); \
514 ins->sreg1 = sp [0]->dreg; \
515 type_from_op (cfg, ins, sp [0], NULL); \
517 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
518 MONO_ADD_INS ((cfg)->cbb, (ins)); \
519 *sp++ = mono_decompose_opcode (cfg, ins); \
522 #define ADD_BINCOND(next_block) do { \
525 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
526 cmp->sreg1 = sp [0]->dreg; \
527 cmp->sreg2 = sp [1]->dreg; \
528 type_from_op (cfg, cmp, sp [0], sp [1]); \
530 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
531 type_from_op (cfg, ins, sp [0], sp [1]); \
532 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
533 GET_BBLOCK (cfg, tblock, target); \
534 link_bblock (cfg, cfg->cbb, tblock); \
535 ins->inst_true_bb = tblock; \
536 if ((next_block)) { \
537 link_bblock (cfg, cfg->cbb, (next_block)); \
538 ins->inst_false_bb = (next_block); \
539 start_new_bblock = 1; \
541 GET_BBLOCK (cfg, tblock, ip); \
542 link_bblock (cfg, cfg->cbb, tblock); \
543 ins->inst_false_bb = tblock; \
544 start_new_bblock = 2; \
546 if (sp != stack_start) { \
547 handle_stack_args (cfg, stack_start, sp - stack_start); \
548 CHECK_UNVERIFIABLE (cfg); \
550 MONO_ADD_INS (cfg->cbb, cmp); \
551 MONO_ADD_INS (cfg->cbb, ins); \
555 * link_bblock: Links two basic blocks
557 * links two basic blocks in the control flow graph, the 'from'
558 * argument is the starting block and the 'to' argument is the block
559 * the control flow ends to after 'from'.
562 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
564 MonoBasicBlock **newa;
568 if (from->cil_code) {
570 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
572 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
575 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
577 printf ("edge from entry to exit\n");
582 for (i = 0; i < from->out_count; ++i) {
583 if (to == from->out_bb [i]) {
589 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
590 for (i = 0; i < from->out_count; ++i) {
591 newa [i] = from->out_bb [i];
599 for (i = 0; i < to->in_count; ++i) {
600 if (from == to->in_bb [i]) {
606 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
607 for (i = 0; i < to->in_count; ++i) {
608 newa [i] = to->in_bb [i];
617 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
619 link_bblock (cfg, from, to);
623 * mono_find_block_region:
625 * We mark each basic block with a region ID. We use that to avoid BB
626 * optimizations when blocks are in different regions.
629 * A region token that encodes where this region is, and information
630 * about the clause owner for this block.
632 * The region encodes the try/catch/filter clause that owns this block
633 * as well as the type. -1 is a special value that represents a block
634 * that is in none of try/catch/filter.
637 mono_find_block_region (MonoCompile *cfg, int offset)
639 MonoMethodHeader *header = cfg->header;
640 MonoExceptionClause *clause;
643 for (i = 0; i < header->num_clauses; ++i) {
644 clause = &header->clauses [i];
645 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
646 (offset < (clause->handler_offset)))
647 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
649 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
650 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
651 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
652 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
653 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
655 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
658 for (i = 0; i < header->num_clauses; ++i) {
659 clause = &header->clauses [i];
661 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
662 return ((i + 1) << 8) | clause->flags;
669 ip_in_finally_clause (MonoCompile *cfg, int offset)
671 MonoMethodHeader *header = cfg->header;
672 MonoExceptionClause *clause;
675 for (i = 0; i < header->num_clauses; ++i) {
676 clause = &header->clauses [i];
677 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
680 if (MONO_OFFSET_IN_HANDLER (clause, offset))
687 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
689 MonoMethodHeader *header = cfg->header;
690 MonoExceptionClause *clause;
694 for (i = 0; i < header->num_clauses; ++i) {
695 clause = &header->clauses [i];
696 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
697 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
698 if (clause->flags == type)
699 res = g_list_append (res, clause);
706 mono_create_spvar_for_region (MonoCompile *cfg, int region)
710 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
714 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
715 /* prevent it from being register allocated */
716 var->flags |= MONO_INST_VOLATILE;
718 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
722 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
724 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
728 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
732 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
736 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
737 /* prevent it from being register allocated */
738 var->flags |= MONO_INST_VOLATILE;
740 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
746 * Returns the type used in the eval stack when @type is loaded.
747 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
750 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
754 type = mini_get_underlying_type (type);
755 inst->klass = klass = mono_class_from_mono_type (type);
757 inst->type = STACK_MP;
762 switch (type->type) {
764 inst->type = STACK_INV;
772 inst->type = STACK_I4;
777 case MONO_TYPE_FNPTR:
778 inst->type = STACK_PTR;
780 case MONO_TYPE_CLASS:
781 case MONO_TYPE_STRING:
782 case MONO_TYPE_OBJECT:
783 case MONO_TYPE_SZARRAY:
784 case MONO_TYPE_ARRAY:
785 inst->type = STACK_OBJ;
789 inst->type = STACK_I8;
792 inst->type = cfg->r4_stack_type;
795 inst->type = STACK_R8;
797 case MONO_TYPE_VALUETYPE:
798 if (type->data.klass->enumtype) {
799 type = mono_class_enum_basetype (type->data.klass);
803 inst->type = STACK_VTYPE;
806 case MONO_TYPE_TYPEDBYREF:
807 inst->klass = mono_defaults.typed_reference_class;
808 inst->type = STACK_VTYPE;
810 case MONO_TYPE_GENERICINST:
811 type = &type->data.generic_class->container_class->byval_arg;
815 g_assert (cfg->gshared);
816 if (mini_is_gsharedvt_type (type)) {
817 g_assert (cfg->gsharedvt);
818 inst->type = STACK_VTYPE;
820 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
824 g_error ("unknown type 0x%02x in eval stack type", type->type);
829 * The following tables are used to quickly validate the IL code in type_from_op ().
832 bin_num_table [STACK_MAX] [STACK_MAX] = {
833 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
834 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
835 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
836 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
837 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
838 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
839 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
840 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
841 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
846 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
849 /* reduce the size of this table */
851 bin_int_table [STACK_MAX] [STACK_MAX] = {
852 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
853 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
854 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
855 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
859 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
863 bin_comp_table [STACK_MAX] [STACK_MAX] = {
864 /* Inv i L p F & O vt r4 */
866 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
867 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
868 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
869 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
870 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
871 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
872 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
873 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
876 /* reduce the size of this table */
878 shift_table [STACK_MAX] [STACK_MAX] = {
879 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
880 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
881 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
882 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
883 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
884 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
885 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
886 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
890 * Tables to map from the non-specific opcode to the matching
891 * type-specific opcode.
893 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
895 binops_op_map [STACK_MAX] = {
896 0, OP_IADD-CEE_ADD, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD, 0, 0, OP_RADD-CEE_ADD
899 /* handles from CEE_NEG to CEE_CONV_U8 */
901 unops_op_map [STACK_MAX] = {
902 0, OP_INEG-CEE_NEG, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG, 0, 0, OP_RNEG-CEE_NEG
905 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
907 ovfops_op_map [STACK_MAX] = {
908 0, OP_ICONV_TO_U2-CEE_CONV_U2, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, 0, OP_RCONV_TO_U2-CEE_CONV_U2
911 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
913 ovf2ops_op_map [STACK_MAX] = {
914 0, OP_ICONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, 0, 0, OP_RCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
917 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
919 ovf3ops_op_map [STACK_MAX] = {
920 0, OP_ICONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, 0, 0, OP_RCONV_TO_OVF_I1-CEE_CONV_OVF_I1
923 /* handles from CEE_BEQ to CEE_BLT_UN */
925 beqops_op_map [STACK_MAX] = {
926 0, OP_IBEQ-CEE_BEQ, OP_LBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_FBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, 0, OP_FBEQ-CEE_BEQ
929 /* handles from CEE_CEQ to CEE_CLT_UN */
931 ceqops_op_map [STACK_MAX] = {
932 0, OP_ICEQ-OP_CEQ, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, 0, OP_RCEQ-OP_CEQ
936 * Sets ins->type (the type on the eval stack) according to the
937 * type of the opcode and the arguments to it.
938 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
940 * FIXME: this function sets ins->type unconditionally in some cases, but
941 * it should set it to invalid for some types (a conv.x on an object)
944 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
946 switch (ins->opcode) {
953 /* FIXME: check unverifiable args for STACK_MP */
954 ins->type = bin_num_table [src1->type] [src2->type];
955 ins->opcode += binops_op_map [ins->type];
962 ins->type = bin_int_table [src1->type] [src2->type];
963 ins->opcode += binops_op_map [ins->type];
968 ins->type = shift_table [src1->type] [src2->type];
969 ins->opcode += binops_op_map [ins->type];
974 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
975 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
976 ins->opcode = OP_LCOMPARE;
977 else if (src1->type == STACK_R4)
978 ins->opcode = OP_RCOMPARE;
979 else if (src1->type == STACK_R8)
980 ins->opcode = OP_FCOMPARE;
982 ins->opcode = OP_ICOMPARE;
984 case OP_ICOMPARE_IMM:
985 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
986 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
987 ins->opcode = OP_LCOMPARE_IMM;
999 ins->opcode += beqops_op_map [src1->type];
1002 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1003 ins->opcode += ceqops_op_map [src1->type];
1009 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1010 ins->opcode += ceqops_op_map [src1->type];
1014 ins->type = neg_table [src1->type];
1015 ins->opcode += unops_op_map [ins->type];
1018 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1019 ins->type = src1->type;
1021 ins->type = STACK_INV;
1022 ins->opcode += unops_op_map [ins->type];
1028 ins->type = STACK_I4;
1029 ins->opcode += unops_op_map [src1->type];
1032 ins->type = STACK_R8;
1033 switch (src1->type) {
1036 ins->opcode = OP_ICONV_TO_R_UN;
1039 ins->opcode = OP_LCONV_TO_R_UN;
1043 case CEE_CONV_OVF_I1:
1044 case CEE_CONV_OVF_U1:
1045 case CEE_CONV_OVF_I2:
1046 case CEE_CONV_OVF_U2:
1047 case CEE_CONV_OVF_I4:
1048 case CEE_CONV_OVF_U4:
1049 ins->type = STACK_I4;
1050 ins->opcode += ovf3ops_op_map [src1->type];
1052 case CEE_CONV_OVF_I_UN:
1053 case CEE_CONV_OVF_U_UN:
1054 ins->type = STACK_PTR;
1055 ins->opcode += ovf2ops_op_map [src1->type];
1057 case CEE_CONV_OVF_I1_UN:
1058 case CEE_CONV_OVF_I2_UN:
1059 case CEE_CONV_OVF_I4_UN:
1060 case CEE_CONV_OVF_U1_UN:
1061 case CEE_CONV_OVF_U2_UN:
1062 case CEE_CONV_OVF_U4_UN:
1063 ins->type = STACK_I4;
1064 ins->opcode += ovf2ops_op_map [src1->type];
1067 ins->type = STACK_PTR;
1068 switch (src1->type) {
1070 ins->opcode = OP_ICONV_TO_U;
1074 #if SIZEOF_VOID_P == 8
1075 ins->opcode = OP_LCONV_TO_U;
1077 ins->opcode = OP_MOVE;
1081 ins->opcode = OP_LCONV_TO_U;
1084 ins->opcode = OP_FCONV_TO_U;
1090 ins->type = STACK_I8;
1091 ins->opcode += unops_op_map [src1->type];
1093 case CEE_CONV_OVF_I8:
1094 case CEE_CONV_OVF_U8:
1095 ins->type = STACK_I8;
1096 ins->opcode += ovf3ops_op_map [src1->type];
1098 case CEE_CONV_OVF_U8_UN:
1099 case CEE_CONV_OVF_I8_UN:
1100 ins->type = STACK_I8;
1101 ins->opcode += ovf2ops_op_map [src1->type];
1104 ins->type = cfg->r4_stack_type;
1105 ins->opcode += unops_op_map [src1->type];
1108 ins->type = STACK_R8;
1109 ins->opcode += unops_op_map [src1->type];
1112 ins->type = STACK_R8;
1116 ins->type = STACK_I4;
1117 ins->opcode += ovfops_op_map [src1->type];
1120 case CEE_CONV_OVF_I:
1121 case CEE_CONV_OVF_U:
1122 ins->type = STACK_PTR;
1123 ins->opcode += ovfops_op_map [src1->type];
1126 case CEE_ADD_OVF_UN:
1128 case CEE_MUL_OVF_UN:
1130 case CEE_SUB_OVF_UN:
1131 ins->type = bin_num_table [src1->type] [src2->type];
1132 ins->opcode += ovfops_op_map [src1->type];
1133 if (ins->type == STACK_R8)
1134 ins->type = STACK_INV;
1136 case OP_LOAD_MEMBASE:
1137 ins->type = STACK_PTR;
1139 case OP_LOADI1_MEMBASE:
1140 case OP_LOADU1_MEMBASE:
1141 case OP_LOADI2_MEMBASE:
1142 case OP_LOADU2_MEMBASE:
1143 case OP_LOADI4_MEMBASE:
1144 case OP_LOADU4_MEMBASE:
1145 ins->type = STACK_PTR;
1147 case OP_LOADI8_MEMBASE:
1148 ins->type = STACK_I8;
1150 case OP_LOADR4_MEMBASE:
1151 ins->type = cfg->r4_stack_type;
1153 case OP_LOADR8_MEMBASE:
1154 ins->type = STACK_R8;
1157 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1161 if (ins->type == STACK_MP)
1162 ins->klass = mono_defaults.object_class;
1167 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1173 param_table [STACK_MAX] [STACK_MAX] = {
1178 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1183 switch (args->type) {
1193 for (i = 0; i < sig->param_count; ++i) {
1194 switch (args [i].type) {
1198 if (!sig->params [i]->byref)
1202 if (sig->params [i]->byref)
1204 switch (sig->params [i]->type) {
1205 case MONO_TYPE_CLASS:
1206 case MONO_TYPE_STRING:
1207 case MONO_TYPE_OBJECT:
1208 case MONO_TYPE_SZARRAY:
1209 case MONO_TYPE_ARRAY:
1216 if (sig->params [i]->byref)
1218 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1227 /*if (!param_table [args [i].type] [sig->params [i]->type])
1235 * When we need a pointer to the current domain many times in a method, we
1236 * call mono_domain_get() once and we store the result in a local variable.
1237 * This function returns the variable that represents the MonoDomain*.
1239 inline static MonoInst *
1240 mono_get_domainvar (MonoCompile *cfg)
1242 if (!cfg->domainvar)
1243 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1244 return cfg->domainvar;
1248 * The got_var contains the address of the Global Offset Table when AOT
1252 mono_get_got_var (MonoCompile *cfg)
1254 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1256 if (!cfg->got_var) {
1257 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1259 return cfg->got_var;
1263 mono_get_vtable_var (MonoCompile *cfg)
1265 g_assert (cfg->gshared);
1267 if (!cfg->rgctx_var) {
1268 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1269 /* force the var to be stack allocated */
1270 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1273 return cfg->rgctx_var;
1277 type_from_stack_type (MonoInst *ins) {
1278 switch (ins->type) {
1279 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1280 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1281 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1282 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1283 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1285 return &ins->klass->this_arg;
1286 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1287 case STACK_VTYPE: return &ins->klass->byval_arg;
1289 g_error ("stack type %d to monotype not handled\n", ins->type);
1294 static G_GNUC_UNUSED int
1295 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1297 t = mono_type_get_underlying_type (t);
1309 case MONO_TYPE_FNPTR:
1311 case MONO_TYPE_CLASS:
1312 case MONO_TYPE_STRING:
1313 case MONO_TYPE_OBJECT:
1314 case MONO_TYPE_SZARRAY:
1315 case MONO_TYPE_ARRAY:
1321 return cfg->r4_stack_type;
1324 case MONO_TYPE_VALUETYPE:
1325 case MONO_TYPE_TYPEDBYREF:
1327 case MONO_TYPE_GENERICINST:
1328 if (mono_type_generic_inst_is_valuetype (t))
1334 g_assert_not_reached ();
1341 array_access_to_klass (int opcode)
1345 return mono_defaults.byte_class;
1347 return mono_defaults.uint16_class;
1350 return mono_defaults.int_class;
1353 return mono_defaults.sbyte_class;
1356 return mono_defaults.int16_class;
1359 return mono_defaults.int32_class;
1361 return mono_defaults.uint32_class;
1364 return mono_defaults.int64_class;
1367 return mono_defaults.single_class;
1370 return mono_defaults.double_class;
1371 case CEE_LDELEM_REF:
1372 case CEE_STELEM_REF:
1373 return mono_defaults.object_class;
1375 g_assert_not_reached ();
1381 * We try to share variables when possible
1384 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1389 /* inlining can result in deeper stacks */
1390 if (slot >= cfg->header->max_stack)
1391 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1393 pos = ins->type - 1 + slot * STACK_MAX;
1395 switch (ins->type) {
1402 if ((vnum = cfg->intvars [pos]))
1403 return cfg->varinfo [vnum];
1404 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1405 cfg->intvars [pos] = res->inst_c0;
1408 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1414 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1417 * Don't use this if a generic_context is set, since that means AOT can't
1418 * look up the method using just the image+token.
1419 * table == 0 means this is a reference made from a wrapper.
1421 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1422 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1423 jump_info_token->image = image;
1424 jump_info_token->token = token;
1425 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1430 * This function is called to handle items that are left on the evaluation stack
1431 * at basic block boundaries. What happens is that we save the values to local variables
1432 * and we reload them later when first entering the target basic block (with the
1433 * handle_loaded_temps () function).
1434 * A single joint point will use the same variables (stored in the array bb->out_stack or
1435 * bb->in_stack, if the basic block is before or after the joint point).
1437 * This function needs to be called _before_ emitting the last instruction of
1438 * the bb (i.e. before emitting a branch).
1439 * If the stack merge fails at a join point, cfg->unverifiable is set.
1442 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1445 MonoBasicBlock *bb = cfg->cbb;
1446 MonoBasicBlock *outb;
1447 MonoInst *inst, **locals;
1452 if (cfg->verbose_level > 3)
1453 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1454 if (!bb->out_scount) {
1455 bb->out_scount = count;
1456 //printf ("bblock %d has out:", bb->block_num);
1458 for (i = 0; i < bb->out_count; ++i) {
1459 outb = bb->out_bb [i];
1460 /* exception handlers are linked, but they should not be considered for stack args */
1461 if (outb->flags & BB_EXCEPTION_HANDLER)
1463 //printf (" %d", outb->block_num);
1464 if (outb->in_stack) {
1466 bb->out_stack = outb->in_stack;
1472 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1473 for (i = 0; i < count; ++i) {
1475 * try to reuse temps already allocated for this purpouse, if they occupy the same
1476 * stack slot and if they are of the same type.
1477 * This won't cause conflicts since if 'local' is used to
1478 * store one of the values in the in_stack of a bblock, then
1479 * the same variable will be used for the same outgoing stack
1481 * This doesn't work when inlining methods, since the bblocks
1482 * in the inlined methods do not inherit their in_stack from
1483 * the bblock they are inlined to. See bug #58863 for an
1486 if (cfg->inlined_method)
1487 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1489 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1494 for (i = 0; i < bb->out_count; ++i) {
1495 outb = bb->out_bb [i];
1496 /* exception handlers are linked, but they should not be considered for stack args */
1497 if (outb->flags & BB_EXCEPTION_HANDLER)
1499 if (outb->in_scount) {
1500 if (outb->in_scount != bb->out_scount) {
1501 cfg->unverifiable = TRUE;
1504 continue; /* check they are the same locals */
1506 outb->in_scount = count;
1507 outb->in_stack = bb->out_stack;
1510 locals = bb->out_stack;
1512 for (i = 0; i < count; ++i) {
1513 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1514 inst->cil_code = sp [i]->cil_code;
1515 sp [i] = locals [i];
1516 if (cfg->verbose_level > 3)
1517 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1521 * It is possible that the out bblocks already have in_stack assigned, and
1522 * the in_stacks differ. In this case, we will store to all the different
1529 /* Find a bblock which has a different in_stack */
1531 while (bindex < bb->out_count) {
1532 outb = bb->out_bb [bindex];
1533 /* exception handlers are linked, but they should not be considered for stack args */
1534 if (outb->flags & BB_EXCEPTION_HANDLER) {
1538 if (outb->in_stack != locals) {
1539 for (i = 0; i < count; ++i) {
1540 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1541 inst->cil_code = sp [i]->cil_code;
1542 sp [i] = locals [i];
1543 if (cfg->verbose_level > 3)
1544 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1546 locals = outb->in_stack;
1556 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1560 if (cfg->compile_aot) {
1561 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1567 ji.type = patch_type;
1568 ji.data.target = data;
1569 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1570 mono_error_assert_ok (&error);
1572 EMIT_NEW_PCONST (cfg, ins, target);
1578 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1580 int ibitmap_reg = alloc_preg (cfg);
1581 #ifdef COMPRESSED_INTERFACE_BITMAP
1583 MonoInst *res, *ins;
1584 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1585 MONO_ADD_INS (cfg->cbb, ins);
1587 args [1] = emit_runtime_constant (cfg, MONO_PATCH_INFO_IID, klass);
1588 res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1589 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1591 int ibitmap_byte_reg = alloc_preg (cfg);
1593 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1595 if (cfg->compile_aot) {
1596 int iid_reg = alloc_preg (cfg);
1597 int shifted_iid_reg = alloc_preg (cfg);
1598 int ibitmap_byte_address_reg = alloc_preg (cfg);
1599 int masked_iid_reg = alloc_preg (cfg);
1600 int iid_one_bit_reg = alloc_preg (cfg);
1601 int iid_bit_reg = alloc_preg (cfg);
1602 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1603 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1604 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1605 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1606 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1607 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1608 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1609 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1611 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1612 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1618 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1619 * stored in "klass_reg" implements the interface "klass".
1622 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1624 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1628 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1629 * stored in "vtable_reg" implements the interface "klass".
1632 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1634 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1638 * Emit code which checks whenever the interface id of @klass is smaller than
1639 * than the value given by max_iid_reg.
1642 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1643 MonoBasicBlock *false_target)
1645 if (cfg->compile_aot) {
1646 int iid_reg = alloc_preg (cfg);
1647 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1648 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1651 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1653 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1655 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1658 /* Same as above, but obtains max_iid from a vtable */
1660 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1661 MonoBasicBlock *false_target)
1663 int max_iid_reg = alloc_preg (cfg);
1665 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU4_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1666 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1669 /* Same as above, but obtains max_iid from a klass */
1671 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1672 MonoBasicBlock *false_target)
1674 int max_iid_reg = alloc_preg (cfg);
1676 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU4_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1677 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1681 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1683 int idepth_reg = alloc_preg (cfg);
1684 int stypes_reg = alloc_preg (cfg);
1685 int stype = alloc_preg (cfg);
1687 mono_class_setup_supertypes (klass);
1689 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1690 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1691 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1692 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1694 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1695 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1697 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1698 } else if (cfg->compile_aot) {
1699 int const_reg = alloc_preg (cfg);
1700 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1701 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1703 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1705 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1709 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1711 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1715 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1717 int intf_reg = alloc_preg (cfg);
1719 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1720 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1721 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1723 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1725 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1729 * Variant of the above that takes a register to the class, not the vtable.
1732 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1734 int intf_bit_reg = alloc_preg (cfg);
1736 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1737 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1738 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1740 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1742 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1746 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1749 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1751 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
1752 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, ins->dreg);
1754 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1758 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1760 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1764 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1766 if (cfg->compile_aot) {
1767 int const_reg = alloc_preg (cfg);
1768 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1769 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1771 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1773 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1777 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1780 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1783 int rank_reg = alloc_preg (cfg);
1784 int eclass_reg = alloc_preg (cfg);
1786 g_assert (!klass_inst);
1787 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1788 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1789 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1790 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1791 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1792 if (klass->cast_class == mono_defaults.object_class) {
1793 int parent_reg = alloc_preg (cfg);
1794 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1795 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1796 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1797 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1798 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1799 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1800 } else if (klass->cast_class == mono_defaults.enum_class) {
1801 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1802 } else if (mono_class_is_interface (klass->cast_class)) {
1803 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1805 // Pass -1 as obj_reg to skip the check below for arrays of arrays
1806 mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1809 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1810 /* Check that the object is a vector too */
1811 int bounds_reg = alloc_preg (cfg);
1812 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1813 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1814 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1817 int idepth_reg = alloc_preg (cfg);
1818 int stypes_reg = alloc_preg (cfg);
1819 int stype = alloc_preg (cfg);
1821 mono_class_setup_supertypes (klass);
1823 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1824 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1825 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1826 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1828 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1829 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1830 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1835 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1837 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1841 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1845 g_assert (val == 0);
1850 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1853 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1856 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1859 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1861 #if SIZEOF_REGISTER == 8
1863 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1869 val_reg = alloc_preg (cfg);
1871 if (SIZEOF_REGISTER == 8)
1872 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1874 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1877 /* This could be optimized further if neccesary */
1879 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1886 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1888 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1893 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1900 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1905 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1910 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1917 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1924 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1925 g_assert (size < 10000);
1928 /* This could be optimized further if neccesary */
1930 cur_reg = alloc_preg (cfg);
1931 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1932 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1939 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1941 cur_reg = alloc_preg (cfg);
1942 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1943 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1951 cur_reg = alloc_preg (cfg);
1952 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1953 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1959 cur_reg = alloc_preg (cfg);
1960 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1961 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1967 cur_reg = alloc_preg (cfg);
1968 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1969 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1977 emit_tls_set (MonoCompile *cfg, int sreg1, MonoTlsKey tls_key)
1981 if (cfg->compile_aot) {
1982 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1983 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1985 ins->sreg2 = c->dreg;
1986 MONO_ADD_INS (cfg->cbb, ins);
1988 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1990 ins->inst_offset = mini_get_tls_offset (tls_key);
1991 MONO_ADD_INS (cfg->cbb, ins);
1998 * Emit IR to push the current LMF onto the LMF stack.
2001 emit_push_lmf (MonoCompile *cfg)
2004 * Emit IR to push the LMF:
2005 * lmf_addr = <lmf_addr from tls>
2006 * lmf->lmf_addr = lmf_addr
2007 * lmf->prev_lmf = *lmf_addr
2010 int lmf_reg, prev_lmf_reg;
2011 MonoInst *ins, *lmf_ins;
2016 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2017 /* Load current lmf */
2018 lmf_ins = mono_get_lmf_intrinsic (cfg);
2020 MONO_ADD_INS (cfg->cbb, lmf_ins);
2021 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2022 lmf_reg = ins->dreg;
2023 /* Save previous_lmf */
2024 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
2026 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
2029 * Store lmf_addr in a variable, so it can be allocated to a global register.
2031 if (!cfg->lmf_addr_var)
2032 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2035 ins = mono_get_jit_tls_intrinsic (cfg);
2037 int jit_tls_dreg = ins->dreg;
2039 MONO_ADD_INS (cfg->cbb, ins);
2040 lmf_reg = alloc_preg (cfg);
2041 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2043 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2046 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2048 MONO_ADD_INS (cfg->cbb, lmf_ins);
2051 MonoInst *args [16], *jit_tls_ins, *ins;
2053 /* Inline mono_get_lmf_addr () */
2054 /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2056 /* Load mono_jit_tls_id */
2057 if (cfg->compile_aot)
2058 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2060 EMIT_NEW_ICONST (cfg, args [0], mono_jit_tls_id);
2061 /* call pthread_getspecific () */
2062 jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2063 /* lmf_addr = &jit_tls->lmf */
2064 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2067 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2071 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2073 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2074 lmf_reg = ins->dreg;
2076 prev_lmf_reg = alloc_preg (cfg);
2077 /* Save previous_lmf */
2078 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2079 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2081 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2088 * Emit IR to pop the current LMF from the LMF stack.
2091 emit_pop_lmf (MonoCompile *cfg)
2093 int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2099 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2100 lmf_reg = ins->dreg;
2102 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2103 /* Load previous_lmf */
2104 prev_lmf_reg = alloc_preg (cfg);
2105 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2107 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2110 * Emit IR to pop the LMF:
2111 * *(lmf->lmf_addr) = lmf->prev_lmf
2113 /* This could be called before emit_push_lmf () */
2114 if (!cfg->lmf_addr_var)
2115 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2116 lmf_addr_reg = cfg->lmf_addr_var->dreg;
2118 prev_lmf_reg = alloc_preg (cfg);
2119 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2120 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2125 emit_instrumentation_call (MonoCompile *cfg, void *func)
2127 MonoInst *iargs [1];
2130 * Avoid instrumenting inlined methods since it can
2131 * distort profiling results.
2133 if (cfg->method != cfg->current_method)
2136 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2137 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2138 mono_emit_jit_icall (cfg, func, iargs);
2143 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
2146 type = mini_get_underlying_type (type);
2147 switch (type->type) {
2148 case MONO_TYPE_VOID:
2149 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2156 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2160 case MONO_TYPE_FNPTR:
2161 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2162 case MONO_TYPE_CLASS:
2163 case MONO_TYPE_STRING:
2164 case MONO_TYPE_OBJECT:
2165 case MONO_TYPE_SZARRAY:
2166 case MONO_TYPE_ARRAY:
2167 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2170 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2173 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2175 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2177 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2178 case MONO_TYPE_VALUETYPE:
2179 if (type->data.klass->enumtype) {
2180 type = mono_class_enum_basetype (type->data.klass);
2183 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2184 case MONO_TYPE_TYPEDBYREF:
2185 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2186 case MONO_TYPE_GENERICINST:
2187 type = &type->data.generic_class->container_class->byval_arg;
2190 case MONO_TYPE_MVAR:
2192 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2194 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2199 //XXX this ignores if t is byref
2200 #define MONO_TYPE_IS_PRIMITIVE_SCALAR(t) ((((((t)->type >= MONO_TYPE_BOOLEAN && (t)->type <= MONO_TYPE_U8) || ((t)->type >= MONO_TYPE_I && (t)->type <= MONO_TYPE_U)))))
2203 * target_type_is_incompatible:
2204 * @cfg: MonoCompile context
2206 * Check that the item @arg on the evaluation stack can be stored
2207 * in the target type (can be a local, or field, etc).
2208 * The cfg arg can be used to check if we need verification or just
2211 * Returns: non-0 value if arg can't be stored on a target.
2214 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2216 MonoType *simple_type;
2219 if (target->byref) {
2220 /* FIXME: check that the pointed to types match */
2221 if (arg->type == STACK_MP) {
2222 /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
2223 MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
2224 MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
2226 /* if the target is native int& or same type */
2227 if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
2230 /* Both are primitive type byrefs and the source points to a larger type that the destination */
2231 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
2232 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
2236 if (arg->type == STACK_PTR)
2241 simple_type = mini_get_underlying_type (target);
2242 switch (simple_type->type) {
2243 case MONO_TYPE_VOID:
2251 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2255 /* STACK_MP is needed when setting pinned locals */
2256 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2261 case MONO_TYPE_FNPTR:
2263 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2264 * in native int. (#688008).
2266 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2269 case MONO_TYPE_CLASS:
2270 case MONO_TYPE_STRING:
2271 case MONO_TYPE_OBJECT:
2272 case MONO_TYPE_SZARRAY:
2273 case MONO_TYPE_ARRAY:
2274 if (arg->type != STACK_OBJ)
2276 /* FIXME: check type compatibility */
2280 if (arg->type != STACK_I8)
2284 if (arg->type != cfg->r4_stack_type)
2288 if (arg->type != STACK_R8)
2291 case MONO_TYPE_VALUETYPE:
2292 if (arg->type != STACK_VTYPE)
2294 klass = mono_class_from_mono_type (simple_type);
2295 if (klass != arg->klass)
2298 case MONO_TYPE_TYPEDBYREF:
2299 if (arg->type != STACK_VTYPE)
2301 klass = mono_class_from_mono_type (simple_type);
2302 if (klass != arg->klass)
2305 case MONO_TYPE_GENERICINST:
2306 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2307 MonoClass *target_class;
2308 if (arg->type != STACK_VTYPE)
2310 klass = mono_class_from_mono_type (simple_type);
2311 target_class = mono_class_from_mono_type (target);
2312 /* The second cases is needed when doing partial sharing */
2313 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2317 if (arg->type != STACK_OBJ)
2319 /* FIXME: check type compatibility */
2323 case MONO_TYPE_MVAR:
2324 g_assert (cfg->gshared);
2325 if (mini_type_var_is_vt (simple_type)) {
2326 if (arg->type != STACK_VTYPE)
2329 if (arg->type != STACK_OBJ)
2334 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2340 * Prepare arguments for passing to a function call.
2341 * Return a non-zero value if the arguments can't be passed to the given
2343 * The type checks are not yet complete and some conversions may need
2344 * casts on 32 or 64 bit architectures.
2346 * FIXME: implement this using target_type_is_incompatible ()
2349 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2351 MonoType *simple_type;
2355 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2359 for (i = 0; i < sig->param_count; ++i) {
2360 if (sig->params [i]->byref) {
2361 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2365 simple_type = mini_get_underlying_type (sig->params [i]);
2367 switch (simple_type->type) {
2368 case MONO_TYPE_VOID:
2377 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2383 case MONO_TYPE_FNPTR:
2384 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2387 case MONO_TYPE_CLASS:
2388 case MONO_TYPE_STRING:
2389 case MONO_TYPE_OBJECT:
2390 case MONO_TYPE_SZARRAY:
2391 case MONO_TYPE_ARRAY:
2392 if (args [i]->type != STACK_OBJ)
2397 if (args [i]->type != STACK_I8)
2401 if (args [i]->type != cfg->r4_stack_type)
2405 if (args [i]->type != STACK_R8)
2408 case MONO_TYPE_VALUETYPE:
2409 if (simple_type->data.klass->enumtype) {
2410 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2413 if (args [i]->type != STACK_VTYPE)
2416 case MONO_TYPE_TYPEDBYREF:
2417 if (args [i]->type != STACK_VTYPE)
2420 case MONO_TYPE_GENERICINST:
2421 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2424 case MONO_TYPE_MVAR:
2426 if (args [i]->type != STACK_VTYPE)
2430 g_error ("unknown type 0x%02x in check_call_signature",
2438 callvirt_to_call (int opcode)
2441 case OP_CALL_MEMBASE:
2443 case OP_VOIDCALL_MEMBASE:
2445 case OP_FCALL_MEMBASE:
2447 case OP_RCALL_MEMBASE:
2449 case OP_VCALL_MEMBASE:
2451 case OP_LCALL_MEMBASE:
2454 g_assert_not_reached ();
2461 callvirt_to_call_reg (int opcode)
2464 case OP_CALL_MEMBASE:
2466 case OP_VOIDCALL_MEMBASE:
2467 return OP_VOIDCALL_REG;
2468 case OP_FCALL_MEMBASE:
2469 return OP_FCALL_REG;
2470 case OP_RCALL_MEMBASE:
2471 return OP_RCALL_REG;
2472 case OP_VCALL_MEMBASE:
2473 return OP_VCALL_REG;
2474 case OP_LCALL_MEMBASE:
2475 return OP_LCALL_REG;
2477 g_assert_not_reached ();
2483 /* Either METHOD or IMT_ARG needs to be set */
2485 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2489 if (COMPILE_LLVM (cfg)) {
2491 method_reg = alloc_preg (cfg);
2492 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2494 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2495 method_reg = ins->dreg;
2499 call->imt_arg_reg = method_reg;
2501 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2506 method_reg = alloc_preg (cfg);
2507 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2509 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2510 method_reg = ins->dreg;
2513 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2516 static MonoJumpInfo *
2517 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2519 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2523 ji->data.target = target;
2529 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2532 return mono_class_check_context_used (klass);
2538 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2541 return mono_method_check_context_used (method);
2547 * check_method_sharing:
2549 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2552 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2554 gboolean pass_vtable = FALSE;
2555 gboolean pass_mrgctx = FALSE;
2557 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2558 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2559 gboolean sharable = FALSE;
2561 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2565 * Pass vtable iff target method might
2566 * be shared, which means that sharing
2567 * is enabled for its class and its
2568 * context is sharable (and it's not a
2571 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2575 if (mini_method_get_context (cmethod) &&
2576 mini_method_get_context (cmethod)->method_inst) {
2577 g_assert (!pass_vtable);
2579 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2582 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2587 if (out_pass_vtable)
2588 *out_pass_vtable = pass_vtable;
2589 if (out_pass_mrgctx)
2590 *out_pass_mrgctx = pass_mrgctx;
2593 inline static MonoCallInst *
2594 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2595 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2599 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2607 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2609 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2611 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2614 call->signature = sig;
2615 call->rgctx_reg = rgctx;
2616 sig_ret = mini_get_underlying_type (sig->ret);
2618 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2621 if (mini_type_is_vtype (sig_ret)) {
2622 call->vret_var = cfg->vret_addr;
2623 //g_assert_not_reached ();
2625 } else if (mini_type_is_vtype (sig_ret)) {
2626 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2629 temp->backend.is_pinvoke = sig->pinvoke;
2632 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2633 * address of return value to increase optimization opportunities.
2634 * Before vtype decomposition, the dreg of the call ins itself represents the
2635 * fact the call modifies the return value. After decomposition, the call will
2636 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2637 * will be transformed into an LDADDR.
2639 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2640 loada->dreg = alloc_preg (cfg);
2641 loada->inst_p0 = temp;
2642 /* We reference the call too since call->dreg could change during optimization */
2643 loada->inst_p1 = call;
2644 MONO_ADD_INS (cfg->cbb, loada);
2646 call->inst.dreg = temp->dreg;
2648 call->vret_var = loada;
2649 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2650 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2652 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2653 if (COMPILE_SOFT_FLOAT (cfg)) {
2655 * If the call has a float argument, we would need to do an r8->r4 conversion using
2656 * an icall, but that cannot be done during the call sequence since it would clobber
2657 * the call registers + the stack. So we do it before emitting the call.
2659 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2661 MonoInst *in = call->args [i];
2663 if (i >= sig->hasthis)
2664 t = sig->params [i - sig->hasthis];
2666 t = &mono_defaults.int_class->byval_arg;
2667 t = mono_type_get_underlying_type (t);
2669 if (!t->byref && t->type == MONO_TYPE_R4) {
2670 MonoInst *iargs [1];
2674 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2676 /* The result will be in an int vreg */
2677 call->args [i] = conv;
2683 call->need_unbox_trampoline = unbox_trampoline;
2686 if (COMPILE_LLVM (cfg))
2687 mono_llvm_emit_call (cfg, call);
2689 mono_arch_emit_call (cfg, call);
2691 mono_arch_emit_call (cfg, call);
2694 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2695 cfg->flags |= MONO_CFG_HAS_CALLS;
2701 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2703 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2704 cfg->uses_rgctx_reg = TRUE;
2705 call->rgctx_reg = TRUE;
2707 call->rgctx_arg_reg = rgctx_reg;
2711 inline static MonoInst*
2712 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2717 gboolean check_sp = FALSE;
2719 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2720 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2722 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2727 rgctx_reg = mono_alloc_preg (cfg);
2728 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2732 if (!cfg->stack_inbalance_var)
2733 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2735 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2736 ins->dreg = cfg->stack_inbalance_var->dreg;
2737 MONO_ADD_INS (cfg->cbb, ins);
2740 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2742 call->inst.sreg1 = addr->dreg;
2745 emit_imt_argument (cfg, call, NULL, imt_arg);
2747 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2752 sp_reg = mono_alloc_preg (cfg);
2754 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2756 MONO_ADD_INS (cfg->cbb, ins);
2758 /* Restore the stack so we don't crash when throwing the exception */
2759 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2760 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2761 MONO_ADD_INS (cfg->cbb, ins);
2763 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2764 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2768 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2770 return (MonoInst*)call;
2774 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2777 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2779 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2782 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2783 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2785 #ifndef DISABLE_REMOTING
2786 gboolean might_be_remote = FALSE;
2788 gboolean virtual_ = this_ins != NULL;
2789 gboolean enable_for_aot = TRUE;
2792 MonoInst *call_target = NULL;
2794 gboolean need_unbox_trampoline;
2797 sig = mono_method_signature (method);
2799 if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2800 g_assert_not_reached ();
2803 rgctx_reg = mono_alloc_preg (cfg);
2804 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2807 if (method->string_ctor) {
2808 /* Create the real signature */
2809 /* FIXME: Cache these */
2810 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2811 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2816 context_used = mini_method_check_context_used (cfg, method);
2818 #ifndef DISABLE_REMOTING
2819 might_be_remote = this_ins && sig->hasthis &&
2820 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2821 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2823 if (might_be_remote && context_used) {
2826 g_assert (cfg->gshared);
2828 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2830 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2834 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2835 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2837 need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2839 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2841 #ifndef DISABLE_REMOTING
2842 if (might_be_remote)
2843 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2846 call->method = method;
2847 call->inst.flags |= MONO_INST_HAS_METHOD;
2848 call->inst.inst_left = this_ins;
2849 call->tail_call = tail;
2852 int vtable_reg, slot_reg, this_reg;
2855 this_reg = this_ins->dreg;
2857 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2858 MonoInst *dummy_use;
2860 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2862 /* Make a call to delegate->invoke_impl */
2863 call->inst.inst_basereg = this_reg;
2864 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2865 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2867 /* We must emit a dummy use here because the delegate trampoline will
2868 replace the 'this' argument with the delegate target making this activation
2869 no longer a root for the delegate.
2870 This is an issue for delegates that target collectible code such as dynamic
2871 methods of GC'able assemblies.
2873 For a test case look into #667921.
2875 FIXME: a dummy use is not the best way to do it as the local register allocator
2876 will put it on a caller save register and spil it around the call.
2877 Ideally, we would either put it on a callee save register or only do the store part.
2879 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2881 return (MonoInst*)call;
2884 if ((!cfg->compile_aot || enable_for_aot) &&
2885 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2886 (MONO_METHOD_IS_FINAL (method) &&
2887 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2888 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2890 * the method is not virtual, we just need to ensure this is not null
2891 * and then we can call the method directly.
2893 #ifndef DISABLE_REMOTING
2894 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2896 * The check above ensures method is not gshared, this is needed since
2897 * gshared methods can't have wrappers.
2899 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2903 if (!method->string_ctor)
2904 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2906 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2907 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2909 * the method is virtual, but we can statically dispatch since either
2910 * it's class or the method itself are sealed.
2911 * But first we need to ensure it's not a null reference.
2913 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2915 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2916 } else if (call_target) {
2917 vtable_reg = alloc_preg (cfg);
2918 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2920 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2921 call->inst.sreg1 = call_target->dreg;
2922 call->inst.flags &= !MONO_INST_HAS_METHOD;
2924 vtable_reg = alloc_preg (cfg);
2925 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2926 if (mono_class_is_interface (method->klass)) {
2927 guint32 imt_slot = mono_method_get_imt_slot (method);
2928 emit_imt_argument (cfg, call, call->method, imt_arg);
2929 slot_reg = vtable_reg;
2930 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2932 slot_reg = vtable_reg;
2933 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2934 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2936 g_assert (mono_method_signature (method)->generic_param_count);
2937 emit_imt_argument (cfg, call, call->method, imt_arg);
2941 call->inst.sreg1 = slot_reg;
2942 call->inst.inst_offset = offset;
2943 call->is_virtual = TRUE;
2947 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2950 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2952 return (MonoInst*)call;
2956 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2958 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2962 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2969 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2972 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2974 return (MonoInst*)call;
2978 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2980 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2984 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2988 * mono_emit_abs_call:
2990 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2992 inline static MonoInst*
2993 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2994 MonoMethodSignature *sig, MonoInst **args)
2996 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
3000 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
3003 if (cfg->abs_patches == NULL)
3004 cfg->abs_patches = g_hash_table_new (NULL, NULL);
3005 g_hash_table_insert (cfg->abs_patches, ji, ji);
3006 ins = mono_emit_native_call (cfg, ji, sig, args);
3007 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
3011 static MonoMethodSignature*
3012 sig_to_rgctx_sig (MonoMethodSignature *sig)
3014 // FIXME: memory allocation
3015 MonoMethodSignature *res;
3018 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
3019 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
3020 res->param_count = sig->param_count + 1;
3021 for (i = 0; i < sig->param_count; ++i)
3022 res->params [i] = sig->params [i];
3023 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
3027 /* Make an indirect call to FSIG passing an additional argument */
3029 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
3031 MonoMethodSignature *csig;
3032 MonoInst *args_buf [16];
3034 int i, pindex, tmp_reg;
3036 /* Make a call with an rgctx/extra arg */
3037 if (fsig->param_count + 2 < 16)
3040 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
3043 args [pindex ++] = orig_args [0];
3044 for (i = 0; i < fsig->param_count; ++i)
3045 args [pindex ++] = orig_args [fsig->hasthis + i];
3046 tmp_reg = alloc_preg (cfg);
3047 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
3048 csig = sig_to_rgctx_sig (fsig);
3049 return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
3052 /* Emit an indirect call to the function descriptor ADDR */
3054 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
3056 int addr_reg, arg_reg;
3057 MonoInst *call_target;
3059 g_assert (cfg->llvm_only);
3062 * addr points to a <addr, arg> pair, load both of them, and
3063 * make a call to addr, passing arg as an extra arg.
3065 addr_reg = alloc_preg (cfg);
3066 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
3067 arg_reg = alloc_preg (cfg);
3068 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
3070 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
3074 direct_icalls_enabled (MonoCompile *cfg)
3076 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3078 if (cfg->compile_llvm && !cfg->llvm_only)
3081 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
3087 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
3090 * Call the jit icall without a wrapper if possible.
3091 * The wrapper is needed for the following reasons:
3092 * - to handle exceptions thrown using mono_raise_exceptions () from the
3093 * icall function. The EH code needs the lmf frame pushed by the
3094 * wrapper to be able to unwind back to managed code.
3095 * - to be able to do stack walks for asynchronously suspended
3096 * threads when debugging.
3098 if (info->no_raise && direct_icalls_enabled (cfg)) {
3102 if (!info->wrapper_method) {
3103 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
3104 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
3106 mono_memory_barrier ();
3110 * Inline the wrapper method, which is basically a call to the C icall, and
3111 * an exception check.
3113 costs = inline_method (cfg, info->wrapper_method, NULL,
3114 args, NULL, il_offset, TRUE);
3115 g_assert (costs > 0);
3116 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
3120 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
3125 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
3127 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3128 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
3132 * Native code might return non register sized integers
3133 * without initializing the upper bits.
3135 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3136 case OP_LOADI1_MEMBASE:
3137 widen_op = OP_ICONV_TO_I1;
3139 case OP_LOADU1_MEMBASE:
3140 widen_op = OP_ICONV_TO_U1;
3142 case OP_LOADI2_MEMBASE:
3143 widen_op = OP_ICONV_TO_I2;
3145 case OP_LOADU2_MEMBASE:
3146 widen_op = OP_ICONV_TO_U2;
3152 if (widen_op != -1) {
3153 int dreg = alloc_preg (cfg);
3156 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3157 widen->type = ins->type;
3168 emit_method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
3170 MonoInst *args [16];
3172 args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (method), method, MONO_RGCTX_INFO_METHOD);
3173 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cil_method), cil_method, MONO_RGCTX_INFO_METHOD);
3175 mono_emit_jit_icall (cfg, mono_throw_method_access, args);
3179 get_memcpy_method (void)
3181 static MonoMethod *memcpy_method = NULL;
3182 if (!memcpy_method) {
3183 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3185 g_error ("Old corlib found. Install a new one");
3187 return memcpy_method;
3191 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3193 MonoClassField *field;
3194 gpointer iter = NULL;
3196 while ((field = mono_class_get_fields (klass, &iter))) {
3199 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3201 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3202 if (mini_type_is_reference (mono_field_get_type (field))) {
3203 g_assert ((foffset % SIZEOF_VOID_P) == 0);
3204 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3206 MonoClass *field_class = mono_class_from_mono_type (field->type);
3207 if (field_class->has_references)
3208 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3214 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3216 int card_table_shift_bits;
3217 gpointer card_table_mask;
3219 MonoInst *dummy_use;
3220 int nursery_shift_bits;
3221 size_t nursery_size;
3223 if (!cfg->gen_write_barriers)
3226 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3228 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3230 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3233 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3234 wbarrier->sreg1 = ptr->dreg;
3235 wbarrier->sreg2 = value->dreg;
3236 MONO_ADD_INS (cfg->cbb, wbarrier);
3237 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3238 int offset_reg = alloc_preg (cfg);
3242 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3243 if (card_table_mask)
3244 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3246 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3247 * IMM's larger than 32bits.
3249 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3250 card_reg = ins->dreg;
3252 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3253 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3255 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3256 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3259 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3263 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3265 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3266 unsigned need_wb = 0;
3271 /*types with references can't have alignment smaller than sizeof(void*) */
3272 if (align < SIZEOF_VOID_P)
3275 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3276 if (size > 32 * SIZEOF_VOID_P)
3279 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3281 /* We don't unroll more than 5 stores to avoid code bloat. */
3282 if (size > 5 * SIZEOF_VOID_P) {
3283 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3284 size += (SIZEOF_VOID_P - 1);
3285 size &= ~(SIZEOF_VOID_P - 1);
3287 EMIT_NEW_ICONST (cfg, iargs [2], size);
3288 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3289 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3293 destreg = iargs [0]->dreg;
3294 srcreg = iargs [1]->dreg;
3297 dest_ptr_reg = alloc_preg (cfg);
3298 tmp_reg = alloc_preg (cfg);
3301 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3303 while (size >= SIZEOF_VOID_P) {
3304 MonoInst *load_inst;
3305 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3306 load_inst->dreg = tmp_reg;
3307 load_inst->inst_basereg = srcreg;
3308 load_inst->inst_offset = offset;
3309 MONO_ADD_INS (cfg->cbb, load_inst);
3311 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3314 emit_write_barrier (cfg, iargs [0], load_inst);
3316 offset += SIZEOF_VOID_P;
3317 size -= SIZEOF_VOID_P;
3320 /*tmp += sizeof (void*)*/
3321 if (size >= SIZEOF_VOID_P) {
3322 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3323 MONO_ADD_INS (cfg->cbb, iargs [0]);
3327 /* Those cannot be references since size < sizeof (void*) */
3329 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3330 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3336 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3337 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3343 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3344 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3353 * Emit code to copy a valuetype of type @klass whose address is stored in
3354 * @src->dreg to memory whose address is stored at @dest->dreg.
3357 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3359 MonoInst *iargs [4];
3362 MonoMethod *memcpy_method;
3363 MonoInst *size_ins = NULL;
3364 MonoInst *memcpy_ins = NULL;
3368 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3371 * This check breaks with spilled vars... need to handle it during verification anyway.
3372 * g_assert (klass && klass == src->klass && klass == dest->klass);
3375 if (mini_is_gsharedvt_klass (klass)) {
3377 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3378 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3382 n = mono_class_native_size (klass, &align);
3384 n = mono_class_value_size (klass, &align);
3386 /* if native is true there should be no references in the struct */
3387 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3388 /* Avoid barriers when storing to the stack */
3389 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3390 (dest->opcode == OP_LDADDR))) {
3396 context_used = mini_class_check_context_used (cfg, klass);
3398 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3399 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3401 } else if (context_used) {
3402 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3404 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3405 if (!cfg->compile_aot)
3406 mono_class_compute_gc_descriptor (klass);
3410 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3412 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3417 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3418 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3419 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3424 iargs [2] = size_ins;
3426 EMIT_NEW_ICONST (cfg, iargs [2], n);
3428 memcpy_method = get_memcpy_method ();
3430 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3432 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3437 get_memset_method (void)
3439 static MonoMethod *memset_method = NULL;
3440 if (!memset_method) {
3441 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3443 g_error ("Old corlib found. Install a new one");
3445 return memset_method;
3449 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3451 MonoInst *iargs [3];
3454 MonoMethod *memset_method;
3455 MonoInst *size_ins = NULL;
3456 MonoInst *bzero_ins = NULL;
3457 static MonoMethod *bzero_method;
3459 /* FIXME: Optimize this for the case when dest is an LDADDR */
3460 mono_class_init (klass);
3461 if (mini_is_gsharedvt_klass (klass)) {
3462 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3463 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3465 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3466 g_assert (bzero_method);
3468 iargs [1] = size_ins;
3469 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3473 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3475 n = mono_class_value_size (klass, &align);
3477 if (n <= sizeof (gpointer) * 8) {
3478 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3481 memset_method = get_memset_method ();
3483 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3484 EMIT_NEW_ICONST (cfg, iargs [2], n);
3485 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3492 * Emit IR to return either the this pointer for instance method,
3493 * or the mrgctx for static methods.
3496 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3498 MonoInst *this_ins = NULL;
3500 g_assert (cfg->gshared);
3502 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3503 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3504 !method->klass->valuetype)
3505 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
3507 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3508 MonoInst *mrgctx_loc, *mrgctx_var;
3510 g_assert (!this_ins);
3511 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3513 mrgctx_loc = mono_get_vtable_var (cfg);
3514 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3517 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3518 MonoInst *vtable_loc, *vtable_var;
3520 g_assert (!this_ins);
3522 vtable_loc = mono_get_vtable_var (cfg);
3523 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3525 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3526 MonoInst *mrgctx_var = vtable_var;
3529 vtable_reg = alloc_preg (cfg);
3530 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3531 vtable_var->type = STACK_PTR;
3539 vtable_reg = alloc_preg (cfg);
3540 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3545 static MonoJumpInfoRgctxEntry *
3546 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3548 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3549 res->method = method;
3550 res->in_mrgctx = in_mrgctx;
3551 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3552 res->data->type = patch_type;
3553 res->data->data.target = patch_data;
3554 res->info_type = info_type;
3559 static inline MonoInst*
3560 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3562 MonoInst *args [16];
3565 // FIXME: No fastpath since the slot is not a compile time constant
3567 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3568 if (entry->in_mrgctx)
3569 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3571 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3575 * FIXME: This can be called during decompose, which is a problem since it creates
3577 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3579 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3581 MonoBasicBlock *is_null_bb, *end_bb;
3582 MonoInst *res, *ins, *call;
3585 slot = mini_get_rgctx_entry_slot (entry);
3587 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3588 index = MONO_RGCTX_SLOT_INDEX (slot);
3590 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3591 for (depth = 0; ; ++depth) {
3592 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3594 if (index < size - 1)
3599 NEW_BBLOCK (cfg, end_bb);
3600 NEW_BBLOCK (cfg, is_null_bb);
3603 rgctx_reg = rgctx->dreg;
3605 rgctx_reg = alloc_preg (cfg);
3607 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3608 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3609 NEW_BBLOCK (cfg, is_null_bb);
3611 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3612 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3615 for (i = 0; i < depth; ++i) {
3616 int array_reg = alloc_preg (cfg);
3618 /* load ptr to next array */
3619 if (mrgctx && i == 0)
3620 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3622 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3623 rgctx_reg = array_reg;
3624 /* is the ptr null? */
3625 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3626 /* if yes, jump to actual trampoline */
3627 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3631 val_reg = alloc_preg (cfg);
3632 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3633 /* is the slot null? */
3634 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3635 /* if yes, jump to actual trampoline */
3636 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3639 res_reg = alloc_preg (cfg);
3640 MONO_INST_NEW (cfg, ins, OP_MOVE);
3641 ins->dreg = res_reg;
3642 ins->sreg1 = val_reg;
3643 MONO_ADD_INS (cfg->cbb, ins);
3645 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3648 MONO_START_BB (cfg, is_null_bb);
3650 EMIT_NEW_ICONST (cfg, args [1], index);
3652 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3654 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3655 MONO_INST_NEW (cfg, ins, OP_MOVE);
3656 ins->dreg = res_reg;
3657 ins->sreg1 = call->dreg;
3658 MONO_ADD_INS (cfg->cbb, ins);
3659 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3661 MONO_START_BB (cfg, end_bb);
3670 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3673 static inline MonoInst*
3674 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3677 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3679 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3683 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3684 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3686 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3687 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3689 return emit_rgctx_fetch (cfg, rgctx, entry);
3693 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3694 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3696 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3697 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3699 return emit_rgctx_fetch (cfg, rgctx, entry);
3703 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3704 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3706 MonoJumpInfoGSharedVtCall *call_info;
3707 MonoJumpInfoRgctxEntry *entry;
3710 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3711 call_info->sig = sig;
3712 call_info->method = cmethod;
3714 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3715 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3717 return emit_rgctx_fetch (cfg, rgctx, entry);
3721 * emit_get_rgctx_virt_method:
3723 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3726 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3727 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3729 MonoJumpInfoVirtMethod *info;
3730 MonoJumpInfoRgctxEntry *entry;
3733 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3734 info->klass = klass;
3735 info->method = virt_method;
3737 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_VIRT_METHOD, info, rgctx_type);
3738 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3740 return emit_rgctx_fetch (cfg, rgctx, entry);
3744 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3745 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3747 MonoJumpInfoRgctxEntry *entry;
3750 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3751 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3753 return emit_rgctx_fetch (cfg, rgctx, entry);
3757 * emit_get_rgctx_method:
3759 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3760 * normal constants, else emit a load from the rgctx.
3763 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3764 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3766 if (!context_used) {
3769 switch (rgctx_type) {
3770 case MONO_RGCTX_INFO_METHOD:
3771 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3773 case MONO_RGCTX_INFO_METHOD_RGCTX:
3774 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3777 g_assert_not_reached ();
3780 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3781 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3783 return emit_rgctx_fetch (cfg, rgctx, entry);
3788 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3789 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3791 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3792 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3794 return emit_rgctx_fetch (cfg, rgctx, entry);
3798 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3800 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3801 MonoRuntimeGenericContextInfoTemplate *template_;
3806 for (i = 0; i < info->num_entries; ++i) {
3807 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3809 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3813 if (info->num_entries == info->count_entries) {
3814 MonoRuntimeGenericContextInfoTemplate *new_entries;
3815 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3817 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3819 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3820 info->entries = new_entries;
3821 info->count_entries = new_count_entries;
3824 idx = info->num_entries;
3825 template_ = &info->entries [idx];
3826 template_->info_type = rgctx_type;
3827 template_->data = data;
3829 info->num_entries ++;
3835 * emit_get_gsharedvt_info:
3837 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3840 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3845 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3846 /* Load info->entries [idx] */
3847 dreg = alloc_preg (cfg);
3848 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3854 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3856 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3860 * On return the caller must check @klass for load errors.
3863 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3865 MonoInst *vtable_arg;
3868 context_used = mini_class_check_context_used (cfg, klass);
3871 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3872 klass, MONO_RGCTX_INFO_VTABLE);
3874 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3878 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3881 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3885 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3886 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3888 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3889 ins->sreg1 = vtable_arg->dreg;
3890 MONO_ADD_INS (cfg->cbb, ins);
3892 static int byte_offset = -1;
3893 static guint8 bitmask;
3894 int bits_reg, inited_reg;
3895 MonoBasicBlock *inited_bb;
3896 MonoInst *args [16];
3898 if (byte_offset < 0)
3899 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
3901 bits_reg = alloc_ireg (cfg);
3902 inited_reg = alloc_ireg (cfg);
3904 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, bits_reg, vtable_arg->dreg, byte_offset);
3905 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, inited_reg, bits_reg, bitmask);
3907 NEW_BBLOCK (cfg, inited_bb);
3909 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3910 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3912 args [0] = vtable_arg;
3913 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3915 MONO_START_BB (cfg, inited_bb);
3920 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3924 if (cfg->gen_seq_points && cfg->method == method) {
3925 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3927 ins->flags |= MONO_INST_NONEMPTY_STACK;
3928 MONO_ADD_INS (cfg->cbb, ins);
3933 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3935 if (mini_get_debug_options ()->better_cast_details) {
3936 int vtable_reg = alloc_preg (cfg);
3937 int klass_reg = alloc_preg (cfg);
3938 MonoBasicBlock *is_null_bb = NULL;
3940 int to_klass_reg, context_used;
3943 NEW_BBLOCK (cfg, is_null_bb);
3945 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3946 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3949 tls_get = mono_get_jit_tls_intrinsic (cfg);
3951 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3955 MONO_ADD_INS (cfg->cbb, tls_get);
3956 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3957 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3959 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3961 context_used = mini_class_check_context_used (cfg, klass);
3963 MonoInst *class_ins;
3965 class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3966 to_klass_reg = class_ins->dreg;
3968 to_klass_reg = alloc_preg (cfg);
3969 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3971 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3974 MONO_START_BB (cfg, is_null_bb);
3979 reset_cast_details (MonoCompile *cfg)
3981 /* Reset the variables holding the cast details */
3982 if (mini_get_debug_options ()->better_cast_details) {
3983 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3985 MONO_ADD_INS (cfg->cbb, tls_get);
3986 /* It is enough to reset the from field */
3987 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3992 * On return the caller must check @array_class for load errors
3995 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3997 int vtable_reg = alloc_preg (cfg);
4000 context_used = mini_class_check_context_used (cfg, array_class);
4002 save_cast_details (cfg, array_class, obj->dreg, FALSE);
4004 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4006 if (cfg->opt & MONO_OPT_SHARED) {
4007 int class_reg = alloc_preg (cfg);
4010 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4011 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
4012 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
4013 } else if (context_used) {
4014 MonoInst *vtable_ins;
4016 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
4017 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
4019 if (cfg->compile_aot) {
4023 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
4025 vt_reg = alloc_preg (cfg);
4026 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
4027 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
4030 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
4032 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
4036 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
4038 reset_cast_details (cfg);
4042 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
4043 * generic code is generated.
4046 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
4048 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4051 MonoInst *rgctx, *addr;
4053 /* FIXME: What if the class is shared? We might not
4054 have to get the address of the method from the
4056 addr = emit_get_rgctx_method (cfg, context_used, method,
4057 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4058 if (cfg->llvm_only) {
4059 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
4060 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4062 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
4064 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4067 gboolean pass_vtable, pass_mrgctx;
4068 MonoInst *rgctx_arg = NULL;
4070 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4071 g_assert (!pass_mrgctx);
4074 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4077 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4080 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4085 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
4089 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
4090 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
4091 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
4092 int rank_reg = alloc_dreg (cfg ,STACK_I4);
4094 obj_reg = sp [0]->dreg;
4095 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4096 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4098 /* FIXME: generics */
4099 g_assert (klass->rank == 0);
4102 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
4103 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4105 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4106 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
4109 MonoInst *element_class;
4111 /* This assertion is from the unboxcast insn */
4112 g_assert (klass->rank == 0);
4114 element_class = emit_get_rgctx_klass (cfg, context_used,
4115 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
4117 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
4118 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4120 save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
4121 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
4122 reset_cast_details (cfg);
4125 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
4126 MONO_ADD_INS (cfg->cbb, add);
4127 add->type = STACK_MP;
4134 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
4136 MonoInst *addr, *klass_inst, *is_ref, *args[16];
4137 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4141 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
4147 args [1] = klass_inst;
4150 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
4152 NEW_BBLOCK (cfg, is_ref_bb);
4153 NEW_BBLOCK (cfg, is_nullable_bb);
4154 NEW_BBLOCK (cfg, end_bb);
4155 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4156 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4157 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4159 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4160 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4162 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
4163 addr_reg = alloc_dreg (cfg, STACK_MP);
4167 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
4168 MONO_ADD_INS (cfg->cbb, addr);
4170 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4173 MONO_START_BB (cfg, is_ref_bb);
4175 /* Save the ref to a temporary */
4176 dreg = alloc_ireg (cfg);
4177 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
4178 addr->dreg = addr_reg;
4179 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
4180 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4183 MONO_START_BB (cfg, is_nullable_bb);
4186 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
4187 MonoInst *unbox_call;
4188 MonoMethodSignature *unbox_sig;
4190 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4191 unbox_sig->ret = &klass->byval_arg;
4192 unbox_sig->param_count = 1;
4193 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
4196 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
4198 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
4200 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
4201 addr->dreg = addr_reg;
4204 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4207 MONO_START_BB (cfg, end_bb);
4210 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
4216 * Returns NULL and set the cfg exception on error.
4219 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
4221 MonoInst *iargs [2];
4226 MonoRgctxInfoType rgctx_info;
4227 MonoInst *iargs [2];
4228 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4230 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4232 if (cfg->opt & MONO_OPT_SHARED)
4233 rgctx_info = MONO_RGCTX_INFO_KLASS;
4235 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4236 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4238 if (cfg->opt & MONO_OPT_SHARED) {
4239 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4241 alloc_ftn = ves_icall_object_new;
4244 alloc_ftn = ves_icall_object_new_specific;
4247 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4248 if (known_instance_size) {
4249 int size = mono_class_instance_size (klass);
4250 if (size < sizeof (MonoObject))
4251 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4253 EMIT_NEW_ICONST (cfg, iargs [1], size);
4255 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4258 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4261 if (cfg->opt & MONO_OPT_SHARED) {
4262 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4263 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4265 alloc_ftn = ves_icall_object_new;
4266 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
4267 /* This happens often in argument checking code, eg. throw new FooException... */
4268 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4269 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4270 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4272 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4273 MonoMethod *managed_alloc = NULL;
4277 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4278 cfg->exception_ptr = klass;
4282 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4284 if (managed_alloc) {
4285 int size = mono_class_instance_size (klass);
4286 if (size < sizeof (MonoObject))
4287 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4289 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4290 EMIT_NEW_ICONST (cfg, iargs [1], size);
4291 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4293 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4295 guint32 lw = vtable->klass->instance_size;
4296 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4297 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4298 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4301 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4305 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4309 * Returns NULL and set the cfg exception on error.
4312 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4314 MonoInst *alloc, *ins;
4316 if (mono_class_is_nullable (klass)) {
4317 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4320 if (cfg->llvm_only && cfg->gsharedvt) {
4321 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4322 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4323 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4325 /* FIXME: What if the class is shared? We might not
4326 have to get the method address from the RGCTX. */
4327 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4328 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4329 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
4331 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4334 gboolean pass_vtable, pass_mrgctx;
4335 MonoInst *rgctx_arg = NULL;
4337 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4338 g_assert (!pass_mrgctx);
4341 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4344 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4347 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4351 if (mini_is_gsharedvt_klass (klass)) {
4352 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4353 MonoInst *res, *is_ref, *src_var, *addr;
4356 dreg = alloc_ireg (cfg);
4358 NEW_BBLOCK (cfg, is_ref_bb);
4359 NEW_BBLOCK (cfg, is_nullable_bb);
4360 NEW_BBLOCK (cfg, end_bb);
4361 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4362 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4363 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4365 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4366 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4369 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4372 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4373 ins->opcode = OP_STOREV_MEMBASE;
4375 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4376 res->type = STACK_OBJ;
4378 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4381 MONO_START_BB (cfg, is_ref_bb);
4383 /* val is a vtype, so has to load the value manually */
4384 src_var = get_vreg_to_inst (cfg, val->dreg);
4386 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4387 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4388 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4389 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4392 MONO_START_BB (cfg, is_nullable_bb);
4395 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4396 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4398 MonoMethodSignature *box_sig;
4401 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4402 * construct that method at JIT time, so have to do things by hand.
4404 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4405 box_sig->ret = &mono_defaults.object_class->byval_arg;
4406 box_sig->param_count = 1;
4407 box_sig->params [0] = &klass->byval_arg;
4410 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4412 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4413 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4414 res->type = STACK_OBJ;
4418 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4420 MONO_START_BB (cfg, end_bb);
4424 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4428 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4434 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4437 MonoGenericContainer *container;
4438 MonoGenericInst *ginst;
4440 if (mono_class_is_ginst (klass)) {
4441 container = mono_class_get_generic_container (mono_class_get_generic_class (klass)->container_class);
4442 ginst = mono_class_get_generic_class (klass)->context.class_inst;
4443 } else if (mono_class_is_gtd (klass) && context_used) {
4444 container = mono_class_get_generic_container (klass);
4445 ginst = container->context.class_inst;
4450 for (i = 0; i < container->type_argc; ++i) {
4452 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4454 type = ginst->type_argv [i];
4455 if (mini_type_is_reference (type))
4461 static GHashTable* direct_icall_type_hash;
4464 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4466 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4467 if (!direct_icalls_enabled (cfg))
4471 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4472 * Whitelist a few icalls for now.
4474 if (!direct_icall_type_hash) {
4475 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4477 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4478 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4479 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4480 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4481 mono_memory_barrier ();
4482 direct_icall_type_hash = h;
4485 if (cmethod->klass == mono_defaults.math_class)
4487 /* No locking needed */
4488 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4494 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
4496 if (cmethod->klass == mono_defaults.systemtype_class) {
4497 if (!strcmp (cmethod->name, "GetType"))
4503 #define is_complex_isinst(klass) (mono_class_is_interface (klass) || klass->rank || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || mono_class_is_sealed (klass) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
4506 emit_isinst_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4508 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4509 return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4513 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4515 MonoMethod *mono_castclass = mono_marshal_get_castclass_with_cache ();
4518 save_cast_details (cfg, klass, args [0]->dreg, TRUE);
4519 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4520 reset_cast_details (cfg);
4526 get_castclass_cache_idx (MonoCompile *cfg)
4528 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4529 cfg->castclass_cache_index ++;
4530 return (cfg->method_index << 16) | cfg->castclass_cache_index;
4535 emit_isinst_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4540 args [0] = obj; /* obj */
4541 EMIT_NEW_CLASSCONST (cfg, args [1], klass); /* klass */
4543 idx = get_castclass_cache_idx (cfg); /* inline cache*/
4544 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4546 return emit_isinst_with_cache (cfg, klass, args);
4550 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4559 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4562 idx = get_castclass_cache_idx (cfg);
4563 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4565 /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4566 return emit_castclass_with_cache (cfg, klass, args);
4570 * Returns NULL and set the cfg exception on error.
4573 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4575 MonoBasicBlock *is_null_bb;
4576 int obj_reg = src->dreg;
4577 int vtable_reg = alloc_preg (cfg);
4578 MonoInst *klass_inst = NULL;
4580 if (MONO_INS_IS_PCONST_NULL (src))
4586 if (mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4587 MonoInst *cache_ins;
4589 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4594 /* klass - it's the second element of the cache entry*/
4595 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4598 args [2] = cache_ins;
4600 return emit_castclass_with_cache (cfg, klass, args);
4603 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4606 NEW_BBLOCK (cfg, is_null_bb);
4608 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4609 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4611 save_cast_details (cfg, klass, obj_reg, FALSE);
4613 if (mono_class_is_interface (klass)) {
4614 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4615 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4617 int klass_reg = alloc_preg (cfg);
4619 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4621 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && mono_class_is_sealed (klass)) {
4622 /* the remoting code is broken, access the class for now */
4623 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4624 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4626 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4627 cfg->exception_ptr = klass;
4630 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4632 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4633 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4635 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4637 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4638 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4642 MONO_START_BB (cfg, is_null_bb);
4644 reset_cast_details (cfg);
4650 * Returns NULL and set the cfg exception on error.
4653 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4656 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4657 int obj_reg = src->dreg;
4658 int vtable_reg = alloc_preg (cfg);
4659 int res_reg = alloc_ireg_ref (cfg);
4660 MonoInst *klass_inst = NULL;
4665 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4666 MonoInst *cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4668 args [0] = src; /* obj */
4670 /* klass - it's the second element of the cache entry*/
4671 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4673 args [2] = cache_ins; /* cache */
4674 return emit_isinst_with_cache (cfg, klass, args);
4677 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4680 NEW_BBLOCK (cfg, is_null_bb);
4681 NEW_BBLOCK (cfg, false_bb);
4682 NEW_BBLOCK (cfg, end_bb);
4684 /* Do the assignment at the beginning, so the other assignment can be if converted */
4685 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4686 ins->type = STACK_OBJ;
4689 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4690 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4692 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4694 if (mono_class_is_interface (klass)) {
4695 g_assert (!context_used);
4696 /* the is_null_bb target simply copies the input register to the output */
4697 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4699 int klass_reg = alloc_preg (cfg);
4702 int rank_reg = alloc_preg (cfg);
4703 int eclass_reg = alloc_preg (cfg);
4705 g_assert (!context_used);
4706 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4707 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4708 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4709 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4710 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4711 if (klass->cast_class == mono_defaults.object_class) {
4712 int parent_reg = alloc_preg (cfg);
4713 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4714 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4715 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4716 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4717 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4718 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4719 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4720 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4721 } else if (klass->cast_class == mono_defaults.enum_class) {
4722 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4723 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4724 } else if (mono_class_is_interface (klass->cast_class)) {
4725 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4727 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4728 /* Check that the object is a vector too */
4729 int bounds_reg = alloc_preg (cfg);
4730 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4731 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4732 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4735 /* the is_null_bb target simply copies the input register to the output */
4736 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4738 } else if (mono_class_is_nullable (klass)) {
4739 g_assert (!context_used);
4740 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4741 /* the is_null_bb target simply copies the input register to the output */
4742 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4744 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && mono_class_is_sealed (klass)) {
4745 g_assert (!context_used);
4746 /* the remoting code is broken, access the class for now */
4747 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4748 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4750 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4751 cfg->exception_ptr = klass;
4754 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4756 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4757 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4759 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4760 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4762 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4763 /* the is_null_bb target simply copies the input register to the output */
4764 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4769 MONO_START_BB (cfg, false_bb);
4771 MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4772 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4774 MONO_START_BB (cfg, is_null_bb);
4776 MONO_START_BB (cfg, end_bb);
4782 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4784 /* This opcode takes as input an object reference and a class, and returns:
4785 0) if the object is an instance of the class,
4786 1) if the object is not instance of the class,
4787 2) if the object is a proxy whose type cannot be determined */
4790 #ifndef DISABLE_REMOTING
4791 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4793 MonoBasicBlock *true_bb, *false_bb, *end_bb;
4795 int obj_reg = src->dreg;
4796 int dreg = alloc_ireg (cfg);
4798 #ifndef DISABLE_REMOTING
4799 int klass_reg = alloc_preg (cfg);
4802 NEW_BBLOCK (cfg, true_bb);
4803 NEW_BBLOCK (cfg, false_bb);
4804 NEW_BBLOCK (cfg, end_bb);
4805 #ifndef DISABLE_REMOTING
4806 NEW_BBLOCK (cfg, false2_bb);
4807 NEW_BBLOCK (cfg, no_proxy_bb);
4810 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4811 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4813 if (mono_class_is_interface (klass)) {
4814 #ifndef DISABLE_REMOTING
4815 NEW_BBLOCK (cfg, interface_fail_bb);
4818 tmp_reg = alloc_preg (cfg);
4819 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4820 #ifndef DISABLE_REMOTING
4821 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4822 MONO_START_BB (cfg, interface_fail_bb);
4823 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4825 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4827 tmp_reg = alloc_preg (cfg);
4828 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4829 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4830 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
4832 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4835 #ifndef DISABLE_REMOTING
4836 tmp_reg = alloc_preg (cfg);
4837 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4838 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4840 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4841 tmp_reg = alloc_preg (cfg);
4842 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4843 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4845 tmp_reg = alloc_preg (cfg);
4846 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4847 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4848 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4850 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4851 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4853 MONO_START_BB (cfg, no_proxy_bb);
4855 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4857 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4861 MONO_START_BB (cfg, false_bb);
4863 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4864 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4866 #ifndef DISABLE_REMOTING
4867 MONO_START_BB (cfg, false2_bb);
4869 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4870 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4873 MONO_START_BB (cfg, true_bb);
4875 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4877 MONO_START_BB (cfg, end_bb);
4880 MONO_INST_NEW (cfg, ins, OP_ICONST);
4882 ins->type = STACK_I4;
4888 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4890 /* This opcode takes as input an object reference and a class, and returns:
4891 0) if the object is an instance of the class,
4892 1) if the object is a proxy whose type cannot be determined
4893 an InvalidCastException exception is thrown otherwhise*/
4896 #ifndef DISABLE_REMOTING
4897 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4899 MonoBasicBlock *ok_result_bb;
4901 int obj_reg = src->dreg;
4902 int dreg = alloc_ireg (cfg);
4903 int tmp_reg = alloc_preg (cfg);
4905 #ifndef DISABLE_REMOTING
4906 int klass_reg = alloc_preg (cfg);
4907 NEW_BBLOCK (cfg, end_bb);
4910 NEW_BBLOCK (cfg, ok_result_bb);
4912 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4913 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4915 save_cast_details (cfg, klass, obj_reg, FALSE);
4917 if (mono_class_is_interface (klass)) {
4918 #ifndef DISABLE_REMOTING
4919 NEW_BBLOCK (cfg, interface_fail_bb);
4921 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4922 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4923 MONO_START_BB (cfg, interface_fail_bb);
4924 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4926 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4928 tmp_reg = alloc_preg (cfg);
4929 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4930 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4931 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4933 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4934 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4936 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4937 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
4938 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4941 #ifndef DISABLE_REMOTING
4942 NEW_BBLOCK (cfg, no_proxy_bb);
4944 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4945 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4946 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4948 tmp_reg = alloc_preg (cfg);
4949 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4950 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4952 tmp_reg = alloc_preg (cfg);
4953 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4954 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4955 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4957 NEW_BBLOCK (cfg, fail_1_bb);
4959 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4961 MONO_START_BB (cfg, fail_1_bb);
4963 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4964 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4966 MONO_START_BB (cfg, no_proxy_bb);
4968 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4970 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4974 MONO_START_BB (cfg, ok_result_bb);
4976 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4978 #ifndef DISABLE_REMOTING
4979 MONO_START_BB (cfg, end_bb);
4983 MONO_INST_NEW (cfg, ins, OP_ICONST);
4985 ins->type = STACK_I4;
4990 static G_GNUC_UNUSED MonoInst*
4991 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4993 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4994 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4997 switch (enum_type->type) {
5000 #if SIZEOF_REGISTER == 8
5012 MonoInst *load, *and_, *cmp, *ceq;
5013 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
5014 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
5015 int dest_reg = alloc_ireg (cfg);
5017 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
5018 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
5019 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
5020 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
5022 ceq->type = STACK_I4;
5025 load = mono_decompose_opcode (cfg, load);
5026 and_ = mono_decompose_opcode (cfg, and_);
5027 cmp = mono_decompose_opcode (cfg, cmp);
5028 ceq = mono_decompose_opcode (cfg, ceq);
5036 * Returns NULL and set the cfg exception on error.
5038 static G_GNUC_UNUSED MonoInst*
5039 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
5043 gpointer trampoline;
5044 MonoInst *obj, *method_ins, *tramp_ins;
5048 if (virtual_ && !cfg->llvm_only) {
5049 MonoMethod *invoke = mono_get_delegate_invoke (klass);
5052 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
5056 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
5060 /* Inline the contents of mono_delegate_ctor */
5062 /* Set target field */
5063 /* Optimize away setting of NULL target */
5064 if (!MONO_INS_IS_PCONST_NULL (target)) {
5065 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
5066 if (cfg->gen_write_barriers) {
5067 dreg = alloc_preg (cfg);
5068 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
5069 emit_write_barrier (cfg, ptr, target);
5073 /* Set method field */
5074 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5075 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
5078 * To avoid looking up the compiled code belonging to the target method
5079 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
5080 * store it, and we fill it after the method has been compiled.
5082 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
5083 MonoInst *code_slot_ins;
5086 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
5088 domain = mono_domain_get ();
5089 mono_domain_lock (domain);
5090 if (!domain_jit_info (domain)->method_code_hash)
5091 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
5092 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
5094 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
5095 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
5097 mono_domain_unlock (domain);
5099 code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
5101 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
5104 if (cfg->llvm_only) {
5105 MonoInst *args [16];
5110 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5111 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
5114 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
5120 if (cfg->compile_aot) {
5121 MonoDelegateClassMethodPair *del_tramp;
5123 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
5124 del_tramp->klass = klass;
5125 del_tramp->method = context_used ? NULL : method;
5126 del_tramp->is_virtual = virtual_;
5127 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
5130 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
5132 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
5133 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
5136 /* Set invoke_impl field */
5138 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
5140 dreg = alloc_preg (cfg);
5141 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
5142 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
5144 dreg = alloc_preg (cfg);
5145 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
5146 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
5149 dreg = alloc_preg (cfg);
5150 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
5151 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
5153 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
5159 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
5161 MonoJitICallInfo *info;
5163 /* Need to register the icall so it gets an icall wrapper */
5164 info = mono_get_array_new_va_icall (rank);
5166 cfg->flags |= MONO_CFG_HAS_VARARGS;
5168 /* mono_array_new_va () needs a vararg calling convention */
5169 cfg->exception_message = g_strdup ("array-new");
5170 cfg->disable_llvm = TRUE;
5172 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
5173 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
5177 * handle_constrained_gsharedvt_call:
5179 * Handle constrained calls where the receiver is a gsharedvt type.
5180 * Return the instruction representing the call. Set the cfg exception on failure.
5183 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
5184 gboolean *ref_emit_widen)
5186 MonoInst *ins = NULL;
5187 gboolean emit_widen = *ref_emit_widen;
5190 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
5191 * This is hard to do with the current call code, since we would have to emit a branch and two different calls. So instead, we
5192 * pack the arguments into an array, and do the rest of the work in in an icall.
5194 if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
5195 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mini_is_gsharedvt_type (fsig->ret)) &&
5196 (fsig->param_count == 0 || (!fsig->hasthis && fsig->param_count == 1) || (fsig->param_count == 1 && (MONO_TYPE_IS_REFERENCE (fsig->params [0]) || fsig->params [0]->byref || mini_is_gsharedvt_type (fsig->params [0]))))) {
5197 MonoInst *args [16];
5200 * This case handles calls to
5201 * - object:ToString()/Equals()/GetHashCode(),
5202 * - System.IComparable<T>:CompareTo()
5203 * - System.IEquatable<T>:Equals ()
5204 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
5208 if (mono_method_check_context_used (cmethod))
5209 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
5211 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
5212 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
5214 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
5215 if (fsig->hasthis && fsig->param_count) {
5216 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
5217 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
5218 ins->dreg = alloc_preg (cfg);
5219 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
5220 MONO_ADD_INS (cfg->cbb, ins);
5223 if (mini_is_gsharedvt_type (fsig->params [0])) {
5224 int addr_reg, deref_arg_reg;
5226 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
5227 deref_arg_reg = alloc_preg (cfg);
5228 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
5229 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
5231 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
5232 addr_reg = ins->dreg;
5233 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
5235 EMIT_NEW_ICONST (cfg, args [3], 0);
5236 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
5239 EMIT_NEW_ICONST (cfg, args [3], 0);
5240 EMIT_NEW_ICONST (cfg, args [4], 0);
5242 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
5245 if (mini_is_gsharedvt_type (fsig->ret)) {
5246 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
5247 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
5251 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
5252 MONO_ADD_INS (cfg->cbb, add);
5254 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
5255 MONO_ADD_INS (cfg->cbb, ins);
5256 /* ins represents the call result */
5259 GSHAREDVT_FAILURE (CEE_CALLVIRT);
5262 *ref_emit_widen = emit_widen;
5271 mono_emit_load_got_addr (MonoCompile *cfg)
5273 MonoInst *getaddr, *dummy_use;
5275 if (!cfg->got_var || cfg->got_var_allocated)
5278 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
5279 getaddr->cil_code = cfg->header->code;
5280 getaddr->dreg = cfg->got_var->dreg;
5282 /* Add it to the start of the first bblock */
5283 if (cfg->bb_entry->code) {
5284 getaddr->next = cfg->bb_entry->code;
5285 cfg->bb_entry->code = getaddr;
5288 MONO_ADD_INS (cfg->bb_entry, getaddr);
5290 cfg->got_var_allocated = TRUE;
5293 * Add a dummy use to keep the got_var alive, since real uses might
5294 * only be generated by the back ends.
5295 * Add it to end_bblock, so the variable's lifetime covers the whole
5297 * It would be better to make the usage of the got var explicit in all
5298 * cases when the backend needs it (i.e. calls, throw etc.), so this
5299 * wouldn't be needed.
5301 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5302 MONO_ADD_INS (cfg->bb_exit, dummy_use);
5305 static int inline_limit;
5306 static gboolean inline_limit_inited;
5309 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5311 MonoMethodHeaderSummary header;
5313 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5314 MonoMethodSignature *sig = mono_method_signature (method);
5318 if (cfg->disable_inline)
5323 if (cfg->inline_depth > 10)
5326 if (!mono_method_get_header_summary (method, &header))
5329 /*runtime, icall and pinvoke are checked by summary call*/
5330 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5331 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5332 (mono_class_is_marshalbyref (method->klass)) ||
5336 /* also consider num_locals? */
5337 /* Do the size check early to avoid creating vtables */
5338 if (!inline_limit_inited) {
5339 if (g_getenv ("MONO_INLINELIMIT"))
5340 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5342 inline_limit = INLINE_LENGTH_LIMIT;
5343 inline_limit_inited = TRUE;
5345 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5349 * if we can initialize the class of the method right away, we do,
5350 * otherwise we don't allow inlining if the class needs initialization,
5351 * since it would mean inserting a call to mono_runtime_class_init()
5352 * inside the inlined code
5354 if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
5357 if (!(cfg->opt & MONO_OPT_SHARED)) {
5358 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5359 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5360 if (method->klass->has_cctor) {
5361 vtable = mono_class_vtable (cfg->domain, method->klass);
5364 if (!cfg->compile_aot) {
5366 if (!mono_runtime_class_init_full (vtable, &error)) {
5367 mono_error_cleanup (&error);
5372 } else if (mono_class_is_before_field_init (method->klass)) {
5373 if (cfg->run_cctors && method->klass->has_cctor) {
5374 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5375 if (!method->klass->runtime_info)
5376 /* No vtable created yet */
5378 vtable = mono_class_vtable (cfg->domain, method->klass);
5381 /* This makes so that inline cannot trigger */
5382 /* .cctors: too many apps depend on them */
5383 /* running with a specific order... */
5384 if (! vtable->initialized)
5387 if (!mono_runtime_class_init_full (vtable, &error)) {
5388 mono_error_cleanup (&error);
5392 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5393 if (!method->klass->runtime_info)
5394 /* No vtable created yet */
5396 vtable = mono_class_vtable (cfg->domain, method->klass);
5399 if (!vtable->initialized)
5404 * If we're compiling for shared code
5405 * the cctor will need to be run at aot method load time, for example,
5406 * or at the end of the compilation of the inlining method.
5408 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
5412 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5413 if (mono_arch_is_soft_float ()) {
5415 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5417 for (i = 0; i < sig->param_count; ++i)
5418 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5423 if (g_list_find (cfg->dont_inline, method))
5430 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5432 if (!cfg->compile_aot) {
5434 if (vtable->initialized)
5438 if (mono_class_is_before_field_init (klass)) {
5439 if (cfg->method == method)
5443 if (!mono_class_needs_cctor_run (klass, method))
5446 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5447 /* The initialization is already done before the method is called */
5454 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5458 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5461 if (mini_is_gsharedvt_variable_klass (klass)) {
5464 mono_class_init (klass);
5465 size = mono_class_array_element_size (klass);
5468 mult_reg = alloc_preg (cfg);
5469 array_reg = arr->dreg;
5470 index_reg = index->dreg;
5472 #if SIZEOF_REGISTER == 8
5473 /* The array reg is 64 bits but the index reg is only 32 */
5474 if (COMPILE_LLVM (cfg)) {
5476 index2_reg = index_reg;
5478 index2_reg = alloc_preg (cfg);
5479 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5482 if (index->type == STACK_I8) {
5483 index2_reg = alloc_preg (cfg);
5484 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5486 index2_reg = index_reg;
5491 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5493 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5494 if (size == 1 || size == 2 || size == 4 || size == 8) {
5495 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5497 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5498 ins->klass = mono_class_get_element_class (klass);
5499 ins->type = STACK_MP;
5505 add_reg = alloc_ireg_mp (cfg);
5508 MonoInst *rgctx_ins;
5511 g_assert (cfg->gshared);
5512 context_used = mini_class_check_context_used (cfg, klass);
5513 g_assert (context_used);
5514 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5515 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5517 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5519 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5520 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5521 ins->klass = mono_class_get_element_class (klass);
5522 ins->type = STACK_MP;
5523 MONO_ADD_INS (cfg->cbb, ins);
5529 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5531 int bounds_reg = alloc_preg (cfg);
5532 int add_reg = alloc_ireg_mp (cfg);
5533 int mult_reg = alloc_preg (cfg);
5534 int mult2_reg = alloc_preg (cfg);
5535 int low1_reg = alloc_preg (cfg);
5536 int low2_reg = alloc_preg (cfg);
5537 int high1_reg = alloc_preg (cfg);
5538 int high2_reg = alloc_preg (cfg);
5539 int realidx1_reg = alloc_preg (cfg);
5540 int realidx2_reg = alloc_preg (cfg);
5541 int sum_reg = alloc_preg (cfg);
5542 int index1, index2, tmpreg;
5546 mono_class_init (klass);
5547 size = mono_class_array_element_size (klass);
5549 index1 = index_ins1->dreg;
5550 index2 = index_ins2->dreg;
5552 #if SIZEOF_REGISTER == 8
5553 /* The array reg is 64 bits but the index reg is only 32 */
5554 if (COMPILE_LLVM (cfg)) {
5557 tmpreg = alloc_preg (cfg);
5558 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5560 tmpreg = alloc_preg (cfg);
5561 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5565 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5569 /* range checking */
5570 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
5571 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5573 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
5574 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5575 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5576 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
5577 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5578 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5579 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5581 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
5582 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5583 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5584 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
5585 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5586 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5587 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5589 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5590 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5591 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5592 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5593 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5595 ins->type = STACK_MP;
5597 MONO_ADD_INS (cfg->cbb, ins);
5603 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5607 MonoMethod *addr_method;
5609 MonoClass *eclass = cmethod->klass->element_class;
5611 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5614 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5616 /* emit_ldelema_2 depends on OP_LMUL */
5617 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5618 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5621 if (mini_is_gsharedvt_variable_klass (eclass))
5624 element_size = mono_class_array_element_size (eclass);
5625 addr_method = mono_marshal_get_array_address (rank, element_size);
5626 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5631 static MonoBreakPolicy
5632 always_insert_breakpoint (MonoMethod *method)
5634 return MONO_BREAK_POLICY_ALWAYS;
5637 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5640 * mono_set_break_policy:
5641 * policy_callback: the new callback function
5643 * Allow embedders to decide wherther to actually obey breakpoint instructions
5644 * (both break IL instructions and Debugger.Break () method calls), for example
5645 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5646 * untrusted or semi-trusted code.
5648 * @policy_callback will be called every time a break point instruction needs to
5649 * be inserted with the method argument being the method that calls Debugger.Break()
5650 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5651 * if it wants the breakpoint to not be effective in the given method.
5652 * #MONO_BREAK_POLICY_ALWAYS is the default.
5655 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5657 if (policy_callback)
5658 break_policy_func = policy_callback;
5660 break_policy_func = always_insert_breakpoint;
5664 should_insert_brekpoint (MonoMethod *method) {
5665 switch (break_policy_func (method)) {
5666 case MONO_BREAK_POLICY_ALWAYS:
5668 case MONO_BREAK_POLICY_NEVER:
5670 case MONO_BREAK_POLICY_ON_DBG:
5671 g_warning ("mdb no longer supported");
5674 g_warning ("Incorrect value returned from break policy callback");
5679 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5681 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5683 MonoInst *addr, *store, *load;
5684 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5686 /* the bounds check is already done by the callers */
5687 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5689 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5690 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5691 if (mini_type_is_reference (&eklass->byval_arg))
5692 emit_write_barrier (cfg, addr, load);
5694 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5695 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5702 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5704 return mini_type_is_reference (&klass->byval_arg);
5708 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5710 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5711 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
5712 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5713 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5714 MonoInst *iargs [3];
5717 mono_class_setup_vtable (obj_array);
5718 g_assert (helper->slot);
5720 if (sp [0]->type != STACK_OBJ)
5722 if (sp [2]->type != STACK_OBJ)
5729 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5733 if (mini_is_gsharedvt_variable_klass (klass)) {
5736 // FIXME-VT: OP_ICONST optimization
5737 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5738 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5739 ins->opcode = OP_STOREV_MEMBASE;
5740 } else if (sp [1]->opcode == OP_ICONST) {
5741 int array_reg = sp [0]->dreg;
5742 int index_reg = sp [1]->dreg;
5743 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5745 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5746 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5749 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5750 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5752 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5753 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5754 if (generic_class_is_reference_type (cfg, klass))
5755 emit_write_barrier (cfg, addr, sp [2]);
5762 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5767 eklass = mono_class_from_mono_type (fsig->params [2]);
5769 eklass = mono_class_from_mono_type (fsig->ret);
5772 return emit_array_store (cfg, eklass, args, FALSE);
5774 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5775 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5781 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5784 int param_size, return_size;
5786 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
5787 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5789 if (cfg->verbose_level > 3)
5790 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5792 //Don't allow mixing reference types with value types
5793 if (param_klass->valuetype != return_klass->valuetype) {
5794 if (cfg->verbose_level > 3)
5795 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5799 if (!param_klass->valuetype) {
5800 if (cfg->verbose_level > 3)
5801 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5806 if (param_klass->has_references || return_klass->has_references)
5809 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5810 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5811 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5812 if (cfg->verbose_level > 3)
5813 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5817 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5818 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5819 if (cfg->verbose_level > 3)
5820 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5824 param_size = mono_class_value_size (param_klass, &align);
5825 return_size = mono_class_value_size (return_klass, &align);
5827 //We can do it if sizes match
5828 if (param_size == return_size) {
5829 if (cfg->verbose_level > 3)
5830 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5834 //No simple way to handle struct if sizes don't match
5835 if (MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg)) {
5836 if (cfg->verbose_level > 3)
5837 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5842 * Same reg size category.
5843 * A quick note on why we don't require widening here.
5844 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5846 * Since the source value comes from a function argument, the JIT will already have
5847 * the value in a VREG and performed any widening needed before (say, when loading from a field).
5849 if (param_size <= 4 && return_size <= 4) {
5850 if (cfg->verbose_level > 3)
5851 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5859 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5861 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5862 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5864 if (mini_is_gsharedvt_variable_type (fsig->ret))
5867 //Valuetypes that are semantically equivalent or numbers than can be widened to
5868 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5871 //Arrays of valuetypes that are semantically equivalent
5872 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5879 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5881 #ifdef MONO_ARCH_SIMD_INTRINSICS
5882 MonoInst *ins = NULL;
5884 if (cfg->opt & MONO_OPT_SIMD) {
5885 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5891 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5895 emit_memory_barrier (MonoCompile *cfg, int kind)
5897 MonoInst *ins = NULL;
5898 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5899 MONO_ADD_INS (cfg->cbb, ins);
5900 ins->backend.memory_barrier_kind = kind;
5906 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5908 MonoInst *ins = NULL;
5911 /* The LLVM backend supports these intrinsics */
5912 if (cmethod->klass == mono_defaults.math_class) {
5913 if (strcmp (cmethod->name, "Sin") == 0) {
5915 } else if (strcmp (cmethod->name, "Cos") == 0) {
5917 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5919 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5923 if (opcode && fsig->param_count == 1) {
5924 MONO_INST_NEW (cfg, ins, opcode);
5925 ins->type = STACK_R8;
5926 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5927 ins->sreg1 = args [0]->dreg;
5928 MONO_ADD_INS (cfg->cbb, ins);
5932 if (cfg->opt & MONO_OPT_CMOV) {
5933 if (strcmp (cmethod->name, "Min") == 0) {
5934 if (fsig->params [0]->type == MONO_TYPE_I4)
5936 if (fsig->params [0]->type == MONO_TYPE_U4)
5937 opcode = OP_IMIN_UN;
5938 else if (fsig->params [0]->type == MONO_TYPE_I8)
5940 else if (fsig->params [0]->type == MONO_TYPE_U8)
5941 opcode = OP_LMIN_UN;
5942 } else if (strcmp (cmethod->name, "Max") == 0) {
5943 if (fsig->params [0]->type == MONO_TYPE_I4)
5945 if (fsig->params [0]->type == MONO_TYPE_U4)
5946 opcode = OP_IMAX_UN;
5947 else if (fsig->params [0]->type == MONO_TYPE_I8)
5949 else if (fsig->params [0]->type == MONO_TYPE_U8)
5950 opcode = OP_LMAX_UN;
5954 if (opcode && fsig->param_count == 2) {
5955 MONO_INST_NEW (cfg, ins, opcode);
5956 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5957 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5958 ins->sreg1 = args [0]->dreg;
5959 ins->sreg2 = args [1]->dreg;
5960 MONO_ADD_INS (cfg->cbb, ins);
5968 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5970 if (cmethod->klass == mono_defaults.array_class) {
5971 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5972 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5973 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5974 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5975 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5976 return emit_array_unsafe_mov (cfg, fsig, args);
5983 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5985 MonoInst *ins = NULL;
5987 MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5989 if (cmethod->klass == mono_defaults.string_class) {
5990 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5991 int dreg = alloc_ireg (cfg);
5992 int index_reg = alloc_preg (cfg);
5993 int add_reg = alloc_preg (cfg);
5995 #if SIZEOF_REGISTER == 8
5996 if (COMPILE_LLVM (cfg)) {
5997 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5999 /* The array reg is 64 bits but the index reg is only 32 */
6000 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
6003 index_reg = args [1]->dreg;
6005 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
6007 #if defined(TARGET_X86) || defined(TARGET_AMD64)
6008 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
6009 add_reg = ins->dreg;
6010 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
6013 int mult_reg = alloc_preg (cfg);
6014 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
6015 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
6016 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
6017 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
6019 type_from_op (cfg, ins, NULL, NULL);
6021 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6022 int dreg = alloc_ireg (cfg);
6023 /* Decompose later to allow more optimizations */
6024 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
6025 ins->type = STACK_I4;
6026 ins->flags |= MONO_INST_FAULT;
6027 cfg->cbb->has_array_access = TRUE;
6028 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
6033 } else if (cmethod->klass == mono_defaults.object_class) {
6034 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
6035 int dreg = alloc_ireg_ref (cfg);
6036 int vt_reg = alloc_preg (cfg);
6037 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6038 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
6039 type_from_op (cfg, ins, NULL, NULL);
6042 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
6043 int dreg = alloc_ireg (cfg);
6044 int t1 = alloc_ireg (cfg);
6046 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
6047 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
6048 ins->type = STACK_I4;
6051 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
6052 MONO_INST_NEW (cfg, ins, OP_NOP);
6053 MONO_ADD_INS (cfg->cbb, ins);
6057 } else if (cmethod->klass == mono_defaults.array_class) {
6058 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6059 return emit_array_generic_access (cfg, fsig, args, FALSE);
6060 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6061 return emit_array_generic_access (cfg, fsig, args, TRUE);
6063 #ifndef MONO_BIG_ARRAYS
6065 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
6068 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
6069 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
6070 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
6071 int dreg = alloc_ireg (cfg);
6072 int bounds_reg = alloc_ireg_mp (cfg);
6073 MonoBasicBlock *end_bb, *szarray_bb;
6074 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
6076 NEW_BBLOCK (cfg, end_bb);
6077 NEW_BBLOCK (cfg, szarray_bb);
6079 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
6080 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
6081 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
6082 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
6083 /* Non-szarray case */
6085 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6086 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
6088 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6089 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
6090 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6091 MONO_START_BB (cfg, szarray_bb);
6094 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6095 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6097 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6098 MONO_START_BB (cfg, end_bb);
6100 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
6101 ins->type = STACK_I4;
6107 if (cmethod->name [0] != 'g')
6110 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
6111 int dreg = alloc_ireg (cfg);
6112 int vtable_reg = alloc_preg (cfg);
6113 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
6114 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6115 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
6116 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
6117 type_from_op (cfg, ins, NULL, NULL);
6120 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6121 int dreg = alloc_ireg (cfg);
6123 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6124 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6125 type_from_op (cfg, ins, NULL, NULL);
6130 } else if (cmethod->klass == runtime_helpers_class) {
6131 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
6132 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
6136 } else if (cmethod->klass == mono_defaults.monitor_class) {
6137 gboolean is_enter = FALSE;
6138 gboolean is_v4 = FALSE;
6140 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
6144 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
6149 * To make async stack traces work, icalls which can block should have a wrapper.
6150 * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
6152 MonoBasicBlock *end_bb;
6154 NEW_BBLOCK (cfg, end_bb);
6156 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
6157 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
6158 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
6159 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4 : (gpointer)mono_monitor_enter, args);
6160 MONO_START_BB (cfg, end_bb);
6163 } else if (cmethod->klass == mono_defaults.thread_class) {
6164 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
6165 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
6166 MONO_ADD_INS (cfg->cbb, ins);
6168 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
6169 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6170 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
6172 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6174 if (fsig->params [0]->type == MONO_TYPE_I1)
6175 opcode = OP_LOADI1_MEMBASE;
6176 else if (fsig->params [0]->type == MONO_TYPE_U1)
6177 opcode = OP_LOADU1_MEMBASE;
6178 else if (fsig->params [0]->type == MONO_TYPE_I2)
6179 opcode = OP_LOADI2_MEMBASE;
6180 else if (fsig->params [0]->type == MONO_TYPE_U2)
6181 opcode = OP_LOADU2_MEMBASE;
6182 else if (fsig->params [0]->type == MONO_TYPE_I4)
6183 opcode = OP_LOADI4_MEMBASE;
6184 else if (fsig->params [0]->type == MONO_TYPE_U4)
6185 opcode = OP_LOADU4_MEMBASE;
6186 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6187 opcode = OP_LOADI8_MEMBASE;
6188 else if (fsig->params [0]->type == MONO_TYPE_R4)
6189 opcode = OP_LOADR4_MEMBASE;
6190 else if (fsig->params [0]->type == MONO_TYPE_R8)
6191 opcode = OP_LOADR8_MEMBASE;
6192 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6193 opcode = OP_LOAD_MEMBASE;
6196 MONO_INST_NEW (cfg, ins, opcode);
6197 ins->inst_basereg = args [0]->dreg;
6198 ins->inst_offset = 0;
6199 MONO_ADD_INS (cfg->cbb, ins);
6201 switch (fsig->params [0]->type) {
6208 ins->dreg = mono_alloc_ireg (cfg);
6209 ins->type = STACK_I4;
6213 ins->dreg = mono_alloc_lreg (cfg);
6214 ins->type = STACK_I8;
6218 ins->dreg = mono_alloc_ireg (cfg);
6219 #if SIZEOF_REGISTER == 8
6220 ins->type = STACK_I8;
6222 ins->type = STACK_I4;
6227 ins->dreg = mono_alloc_freg (cfg);
6228 ins->type = STACK_R8;
6231 g_assert (mini_type_is_reference (fsig->params [0]));
6232 ins->dreg = mono_alloc_ireg_ref (cfg);
6233 ins->type = STACK_OBJ;
6237 if (opcode == OP_LOADI8_MEMBASE)
6238 ins = mono_decompose_opcode (cfg, ins);
6240 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6244 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
6246 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6248 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
6249 opcode = OP_STOREI1_MEMBASE_REG;
6250 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
6251 opcode = OP_STOREI2_MEMBASE_REG;
6252 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
6253 opcode = OP_STOREI4_MEMBASE_REG;
6254 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6255 opcode = OP_STOREI8_MEMBASE_REG;
6256 else if (fsig->params [0]->type == MONO_TYPE_R4)
6257 opcode = OP_STORER4_MEMBASE_REG;
6258 else if (fsig->params [0]->type == MONO_TYPE_R8)
6259 opcode = OP_STORER8_MEMBASE_REG;
6260 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6261 opcode = OP_STORE_MEMBASE_REG;
6264 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6266 MONO_INST_NEW (cfg, ins, opcode);
6267 ins->sreg1 = args [1]->dreg;
6268 ins->inst_destbasereg = args [0]->dreg;
6269 ins->inst_offset = 0;
6270 MONO_ADD_INS (cfg->cbb, ins);
6272 if (opcode == OP_STOREI8_MEMBASE_REG)
6273 ins = mono_decompose_opcode (cfg, ins);
6278 } else if (cmethod->klass->image == mono_defaults.corlib &&
6279 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6280 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6283 #if SIZEOF_REGISTER == 8
6284 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6285 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6286 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6287 ins->dreg = mono_alloc_preg (cfg);
6288 ins->sreg1 = args [0]->dreg;
6289 ins->type = STACK_I8;
6290 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6291 MONO_ADD_INS (cfg->cbb, ins);
6295 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6297 /* 64 bit reads are already atomic */
6298 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6299 load_ins->dreg = mono_alloc_preg (cfg);
6300 load_ins->inst_basereg = args [0]->dreg;
6301 load_ins->inst_offset = 0;
6302 load_ins->type = STACK_I8;
6303 MONO_ADD_INS (cfg->cbb, load_ins);
6305 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6312 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6313 MonoInst *ins_iconst;
6316 if (fsig->params [0]->type == MONO_TYPE_I4) {
6317 opcode = OP_ATOMIC_ADD_I4;
6318 cfg->has_atomic_add_i4 = TRUE;
6320 #if SIZEOF_REGISTER == 8
6321 else if (fsig->params [0]->type == MONO_TYPE_I8)
6322 opcode = OP_ATOMIC_ADD_I8;
6325 if (!mono_arch_opcode_supported (opcode))
6327 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6328 ins_iconst->inst_c0 = 1;
6329 ins_iconst->dreg = mono_alloc_ireg (cfg);
6330 MONO_ADD_INS (cfg->cbb, ins_iconst);
6332 MONO_INST_NEW (cfg, ins, opcode);
6333 ins->dreg = mono_alloc_ireg (cfg);
6334 ins->inst_basereg = args [0]->dreg;
6335 ins->inst_offset = 0;
6336 ins->sreg2 = ins_iconst->dreg;
6337 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6338 MONO_ADD_INS (cfg->cbb, ins);
6340 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6341 MonoInst *ins_iconst;
6344 if (fsig->params [0]->type == MONO_TYPE_I4) {
6345 opcode = OP_ATOMIC_ADD_I4;
6346 cfg->has_atomic_add_i4 = TRUE;
6348 #if SIZEOF_REGISTER == 8
6349 else if (fsig->params [0]->type == MONO_TYPE_I8)
6350 opcode = OP_ATOMIC_ADD_I8;
6353 if (!mono_arch_opcode_supported (opcode))
6355 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6356 ins_iconst->inst_c0 = -1;
6357 ins_iconst->dreg = mono_alloc_ireg (cfg);
6358 MONO_ADD_INS (cfg->cbb, ins_iconst);
6360 MONO_INST_NEW (cfg, ins, opcode);
6361 ins->dreg = mono_alloc_ireg (cfg);
6362 ins->inst_basereg = args [0]->dreg;
6363 ins->inst_offset = 0;
6364 ins->sreg2 = ins_iconst->dreg;
6365 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6366 MONO_ADD_INS (cfg->cbb, ins);
6368 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6371 if (fsig->params [0]->type == MONO_TYPE_I4) {
6372 opcode = OP_ATOMIC_ADD_I4;
6373 cfg->has_atomic_add_i4 = TRUE;
6375 #if SIZEOF_REGISTER == 8
6376 else if (fsig->params [0]->type == MONO_TYPE_I8)
6377 opcode = OP_ATOMIC_ADD_I8;
6380 if (!mono_arch_opcode_supported (opcode))
6382 MONO_INST_NEW (cfg, ins, opcode);
6383 ins->dreg = mono_alloc_ireg (cfg);
6384 ins->inst_basereg = args [0]->dreg;
6385 ins->inst_offset = 0;
6386 ins->sreg2 = args [1]->dreg;
6387 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6388 MONO_ADD_INS (cfg->cbb, ins);
6391 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6392 MonoInst *f2i = NULL, *i2f;
6393 guint32 opcode, f2i_opcode, i2f_opcode;
6394 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6395 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6397 if (fsig->params [0]->type == MONO_TYPE_I4 ||
6398 fsig->params [0]->type == MONO_TYPE_R4) {
6399 opcode = OP_ATOMIC_EXCHANGE_I4;
6400 f2i_opcode = OP_MOVE_F_TO_I4;
6401 i2f_opcode = OP_MOVE_I4_TO_F;
6402 cfg->has_atomic_exchange_i4 = TRUE;
6404 #if SIZEOF_REGISTER == 8
6406 fsig->params [0]->type == MONO_TYPE_I8 ||
6407 fsig->params [0]->type == MONO_TYPE_R8 ||
6408 fsig->params [0]->type == MONO_TYPE_I) {
6409 opcode = OP_ATOMIC_EXCHANGE_I8;
6410 f2i_opcode = OP_MOVE_F_TO_I8;
6411 i2f_opcode = OP_MOVE_I8_TO_F;
6414 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6415 opcode = OP_ATOMIC_EXCHANGE_I4;
6416 cfg->has_atomic_exchange_i4 = TRUE;
6422 if (!mono_arch_opcode_supported (opcode))
6426 /* TODO: Decompose these opcodes instead of bailing here. */
6427 if (COMPILE_SOFT_FLOAT (cfg))
6430 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6431 f2i->dreg = mono_alloc_ireg (cfg);
6432 f2i->sreg1 = args [1]->dreg;
6433 if (f2i_opcode == OP_MOVE_F_TO_I4)
6434 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6435 MONO_ADD_INS (cfg->cbb, f2i);
6438 MONO_INST_NEW (cfg, ins, opcode);
6439 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6440 ins->inst_basereg = args [0]->dreg;
6441 ins->inst_offset = 0;
6442 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6443 MONO_ADD_INS (cfg->cbb, ins);
6445 switch (fsig->params [0]->type) {
6447 ins->type = STACK_I4;
6450 ins->type = STACK_I8;
6453 #if SIZEOF_REGISTER == 8
6454 ins->type = STACK_I8;
6456 ins->type = STACK_I4;
6461 ins->type = STACK_R8;
6464 g_assert (mini_type_is_reference (fsig->params [0]));
6465 ins->type = STACK_OBJ;
6470 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6471 i2f->dreg = mono_alloc_freg (cfg);
6472 i2f->sreg1 = ins->dreg;
6473 i2f->type = STACK_R8;
6474 if (i2f_opcode == OP_MOVE_I4_TO_F)
6475 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6476 MONO_ADD_INS (cfg->cbb, i2f);
6481 if (cfg->gen_write_barriers && is_ref)
6482 emit_write_barrier (cfg, args [0], args [1]);
6484 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6485 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6486 guint32 opcode, f2i_opcode, i2f_opcode;
6487 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6488 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6490 if (fsig->params [1]->type == MONO_TYPE_I4 ||
6491 fsig->params [1]->type == MONO_TYPE_R4) {
6492 opcode = OP_ATOMIC_CAS_I4;
6493 f2i_opcode = OP_MOVE_F_TO_I4;
6494 i2f_opcode = OP_MOVE_I4_TO_F;
6495 cfg->has_atomic_cas_i4 = TRUE;
6497 #if SIZEOF_REGISTER == 8
6499 fsig->params [1]->type == MONO_TYPE_I8 ||
6500 fsig->params [1]->type == MONO_TYPE_R8 ||
6501 fsig->params [1]->type == MONO_TYPE_I) {
6502 opcode = OP_ATOMIC_CAS_I8;
6503 f2i_opcode = OP_MOVE_F_TO_I8;
6504 i2f_opcode = OP_MOVE_I8_TO_F;
6507 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6508 opcode = OP_ATOMIC_CAS_I4;
6509 cfg->has_atomic_cas_i4 = TRUE;
6515 if (!mono_arch_opcode_supported (opcode))
6519 /* TODO: Decompose these opcodes instead of bailing here. */
6520 if (COMPILE_SOFT_FLOAT (cfg))
6523 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6524 f2i_new->dreg = mono_alloc_ireg (cfg);
6525 f2i_new->sreg1 = args [1]->dreg;
6526 if (f2i_opcode == OP_MOVE_F_TO_I4)
6527 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6528 MONO_ADD_INS (cfg->cbb, f2i_new);
6530 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6531 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6532 f2i_cmp->sreg1 = args [2]->dreg;
6533 if (f2i_opcode == OP_MOVE_F_TO_I4)
6534 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6535 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6538 MONO_INST_NEW (cfg, ins, opcode);
6539 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6540 ins->sreg1 = args [0]->dreg;
6541 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6542 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6543 MONO_ADD_INS (cfg->cbb, ins);
6545 switch (fsig->params [1]->type) {
6547 ins->type = STACK_I4;
6550 ins->type = STACK_I8;
6553 #if SIZEOF_REGISTER == 8
6554 ins->type = STACK_I8;
6556 ins->type = STACK_I4;
6560 ins->type = cfg->r4_stack_type;
6563 ins->type = STACK_R8;
6566 g_assert (mini_type_is_reference (fsig->params [1]));
6567 ins->type = STACK_OBJ;
6572 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6573 i2f->dreg = mono_alloc_freg (cfg);
6574 i2f->sreg1 = ins->dreg;
6575 i2f->type = STACK_R8;
6576 if (i2f_opcode == OP_MOVE_I4_TO_F)
6577 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6578 MONO_ADD_INS (cfg->cbb, i2f);
6583 if (cfg->gen_write_barriers && is_ref)
6584 emit_write_barrier (cfg, args [0], args [1]);
6586 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6587 fsig->params [1]->type == MONO_TYPE_I4) {
6588 MonoInst *cmp, *ceq;
6590 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6593 /* int32 r = CAS (location, value, comparand); */
6594 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6595 ins->dreg = alloc_ireg (cfg);
6596 ins->sreg1 = args [0]->dreg;
6597 ins->sreg2 = args [1]->dreg;
6598 ins->sreg3 = args [2]->dreg;
6599 ins->type = STACK_I4;
6600 MONO_ADD_INS (cfg->cbb, ins);
6602 /* bool result = r == comparand; */
6603 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6604 cmp->sreg1 = ins->dreg;
6605 cmp->sreg2 = args [2]->dreg;
6606 cmp->type = STACK_I4;
6607 MONO_ADD_INS (cfg->cbb, cmp);
6609 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6610 ceq->dreg = alloc_ireg (cfg);
6611 ceq->type = STACK_I4;
6612 MONO_ADD_INS (cfg->cbb, ceq);
6614 /* *success = result; */
6615 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6617 cfg->has_atomic_cas_i4 = TRUE;
6619 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6620 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6624 } else if (cmethod->klass->image == mono_defaults.corlib &&
6625 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6626 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6629 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6631 MonoType *t = fsig->params [0];
6633 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
6635 g_assert (t->byref);
6636 /* t is a byref type, so the reference check is more complicated */
6637 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6638 if (t->type == MONO_TYPE_I1)
6639 opcode = OP_ATOMIC_LOAD_I1;
6640 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6641 opcode = OP_ATOMIC_LOAD_U1;
6642 else if (t->type == MONO_TYPE_I2)
6643 opcode = OP_ATOMIC_LOAD_I2;
6644 else if (t->type == MONO_TYPE_U2)
6645 opcode = OP_ATOMIC_LOAD_U2;
6646 else if (t->type == MONO_TYPE_I4)
6647 opcode = OP_ATOMIC_LOAD_I4;
6648 else if (t->type == MONO_TYPE_U4)
6649 opcode = OP_ATOMIC_LOAD_U4;
6650 else if (t->type == MONO_TYPE_R4)
6651 opcode = OP_ATOMIC_LOAD_R4;
6652 else if (t->type == MONO_TYPE_R8)
6653 opcode = OP_ATOMIC_LOAD_R8;
6654 #if SIZEOF_REGISTER == 8
6655 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6656 opcode = OP_ATOMIC_LOAD_I8;
6657 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6658 opcode = OP_ATOMIC_LOAD_U8;
6660 else if (t->type == MONO_TYPE_I)
6661 opcode = OP_ATOMIC_LOAD_I4;
6662 else if (is_ref || t->type == MONO_TYPE_U)
6663 opcode = OP_ATOMIC_LOAD_U4;
6667 if (!mono_arch_opcode_supported (opcode))
6670 MONO_INST_NEW (cfg, ins, opcode);
6671 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6672 ins->sreg1 = args [0]->dreg;
6673 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6674 MONO_ADD_INS (cfg->cbb, ins);
6677 case MONO_TYPE_BOOLEAN:
6684 ins->type = STACK_I4;
6688 ins->type = STACK_I8;
6692 #if SIZEOF_REGISTER == 8
6693 ins->type = STACK_I8;
6695 ins->type = STACK_I4;
6699 ins->type = cfg->r4_stack_type;
6702 ins->type = STACK_R8;
6706 ins->type = STACK_OBJ;
6712 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6714 MonoType *t = fsig->params [0];
6717 g_assert (t->byref);
6718 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6719 if (t->type == MONO_TYPE_I1)
6720 opcode = OP_ATOMIC_STORE_I1;
6721 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6722 opcode = OP_ATOMIC_STORE_U1;
6723 else if (t->type == MONO_TYPE_I2)
6724 opcode = OP_ATOMIC_STORE_I2;
6725 else if (t->type == MONO_TYPE_U2)
6726 opcode = OP_ATOMIC_STORE_U2;
6727 else if (t->type == MONO_TYPE_I4)
6728 opcode = OP_ATOMIC_STORE_I4;
6729 else if (t->type == MONO_TYPE_U4)
6730 opcode = OP_ATOMIC_STORE_U4;
6731 else if (t->type == MONO_TYPE_R4)
6732 opcode = OP_ATOMIC_STORE_R4;
6733 else if (t->type == MONO_TYPE_R8)
6734 opcode = OP_ATOMIC_STORE_R8;
6735 #if SIZEOF_REGISTER == 8
6736 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6737 opcode = OP_ATOMIC_STORE_I8;
6738 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6739 opcode = OP_ATOMIC_STORE_U8;
6741 else if (t->type == MONO_TYPE_I)
6742 opcode = OP_ATOMIC_STORE_I4;
6743 else if (is_ref || t->type == MONO_TYPE_U)
6744 opcode = OP_ATOMIC_STORE_U4;
6748 if (!mono_arch_opcode_supported (opcode))
6751 MONO_INST_NEW (cfg, ins, opcode);
6752 ins->dreg = args [0]->dreg;
6753 ins->sreg1 = args [1]->dreg;
6754 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6755 MONO_ADD_INS (cfg->cbb, ins);
6757 if (cfg->gen_write_barriers && is_ref)
6758 emit_write_barrier (cfg, args [0], args [1]);
6764 } else if (cmethod->klass->image == mono_defaults.corlib &&
6765 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6766 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6767 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6768 if (should_insert_brekpoint (cfg->method)) {
6769 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6771 MONO_INST_NEW (cfg, ins, OP_NOP);
6772 MONO_ADD_INS (cfg->cbb, ins);
6776 } else if (cmethod->klass->image == mono_defaults.corlib &&
6777 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6778 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6779 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6781 EMIT_NEW_ICONST (cfg, ins, 1);
6783 EMIT_NEW_ICONST (cfg, ins, 0);
6786 } else if (cmethod->klass->image == mono_defaults.corlib &&
6787 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6788 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6789 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6790 /* No stack walks are currently available, so implement this as an intrinsic */
6791 MonoInst *assembly_ins;
6793 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6794 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6797 } else if (cmethod->klass->image == mono_defaults.corlib &&
6798 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6799 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6800 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6801 /* No stack walks are currently available, so implement this as an intrinsic */
6802 MonoInst *method_ins;
6803 MonoMethod *declaring = cfg->method;
6805 /* This returns the declaring generic method */
6806 if (declaring->is_inflated)
6807 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6808 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6809 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6810 cfg->no_inline = TRUE;
6811 if (cfg->method != cfg->current_method)
6812 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6815 } else if (cmethod->klass == mono_defaults.math_class) {
6817 * There is general branchless code for Min/Max, but it does not work for
6819 * http://everything2.com/?node_id=1051618
6821 } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
6822 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
6823 MONO_INST_NEW (cfg, ins, OP_PCEQ);
6824 ins->dreg = alloc_preg (cfg);
6825 ins->type = STACK_I4;
6826 MONO_ADD_INS (cfg->cbb, ins);
6828 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6829 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6830 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6831 !strcmp (cmethod->klass->name, "Selector")) ||
6832 ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
6833 !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
6834 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6835 !strcmp (cmethod->klass->name, "Selector"))
6837 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
6838 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6839 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6842 MonoJumpInfoToken *ji;
6845 if (args [0]->opcode == OP_GOT_ENTRY) {
6846 pi = (MonoInst *)args [0]->inst_p1;
6847 g_assert (pi->opcode == OP_PATCH_INFO);
6848 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6849 ji = (MonoJumpInfoToken *)pi->inst_p0;
6851 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6852 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6855 NULLIFY_INS (args [0]);
6857 s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
6858 return_val_if_nok (&cfg->error, NULL);
6860 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6861 ins->dreg = mono_alloc_ireg (cfg);
6864 MONO_ADD_INS (cfg->cbb, ins);
6869 #ifdef MONO_ARCH_SIMD_INTRINSICS
6870 if (cfg->opt & MONO_OPT_SIMD) {
6871 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6877 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6881 if (COMPILE_LLVM (cfg)) {
6882 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6887 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6891 * This entry point could be used later for arbitrary method
6894 inline static MonoInst*
6895 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6896 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6898 if (method->klass == mono_defaults.string_class) {
6899 /* managed string allocation support */
6900 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6901 MonoInst *iargs [2];
6902 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6903 MonoMethod *managed_alloc = NULL;
6905 g_assert (vtable); /*Should not fail since it System.String*/
6906 #ifndef MONO_CROSS_COMPILE
6907 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6911 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6912 iargs [1] = args [0];
6913 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6920 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6922 MonoInst *store, *temp;
6925 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6926 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6929 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6930 * would be different than the MonoInst's used to represent arguments, and
6931 * the ldelema implementation can't deal with that.
6932 * Solution: When ldelema is used on an inline argument, create a var for
6933 * it, emit ldelema on that var, and emit the saving code below in
6934 * inline_method () if needed.
6936 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6937 cfg->args [i] = temp;
6938 /* This uses cfg->args [i] which is set by the preceeding line */
6939 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6940 store->cil_code = sp [0]->cil_code;
6945 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6946 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6948 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6950 check_inline_called_method_name_limit (MonoMethod *called_method)
6953 static const char *limit = NULL;
6955 if (limit == NULL) {
6956 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6958 if (limit_string != NULL)
6959 limit = limit_string;
6964 if (limit [0] != '\0') {
6965 char *called_method_name = mono_method_full_name (called_method, TRUE);
6967 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6968 g_free (called_method_name);
6970 //return (strncmp_result <= 0);
6971 return (strncmp_result == 0);
6978 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6980 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6983 static const char *limit = NULL;
6985 if (limit == NULL) {
6986 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6987 if (limit_string != NULL) {
6988 limit = limit_string;
6994 if (limit [0] != '\0') {
6995 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6997 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6998 g_free (caller_method_name);
7000 //return (strncmp_result <= 0);
7001 return (strncmp_result == 0);
7009 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
7011 static double r8_0 = 0.0;
7012 static float r4_0 = 0.0;
7016 rtype = mini_get_underlying_type (rtype);
7020 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
7021 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7022 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
7023 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7024 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
7025 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7026 MONO_INST_NEW (cfg, ins, OP_R4CONST);
7027 ins->type = STACK_R4;
7028 ins->inst_p0 = (void*)&r4_0;
7030 MONO_ADD_INS (cfg->cbb, ins);
7031 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7032 MONO_INST_NEW (cfg, ins, OP_R8CONST);
7033 ins->type = STACK_R8;
7034 ins->inst_p0 = (void*)&r8_0;
7036 MONO_ADD_INS (cfg->cbb, ins);
7037 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7038 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7039 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
7040 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7041 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
7043 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
7048 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
7052 rtype = mini_get_underlying_type (rtype);
7056 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
7057 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7058 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
7059 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7060 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
7061 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7062 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
7063 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7064 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
7065 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7066 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7067 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7068 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7069 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7071 emit_init_rvar (cfg, dreg, rtype);
7075 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
7077 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
7079 MonoInst *var = cfg->locals [local];
7080 if (COMPILE_SOFT_FLOAT (cfg)) {
7082 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
7083 emit_init_rvar (cfg, reg, type);
7084 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
7087 emit_init_rvar (cfg, var->dreg, type);
7089 emit_dummy_init_rvar (cfg, var->dreg, type);
7096 * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
7099 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
7100 guchar *ip, guint real_offset, gboolean inline_always)
7103 MonoInst *ins, *rvar = NULL;
7104 MonoMethodHeader *cheader;
7105 MonoBasicBlock *ebblock, *sbblock;
7107 MonoMethod *prev_inlined_method;
7108 MonoInst **prev_locals, **prev_args;
7109 MonoType **prev_arg_types;
7110 guint prev_real_offset;
7111 GHashTable *prev_cbb_hash;
7112 MonoBasicBlock **prev_cil_offset_to_bb;
7113 MonoBasicBlock *prev_cbb;
7114 const unsigned char *prev_ip;
7115 unsigned char *prev_cil_start;
7116 guint32 prev_cil_offset_to_bb_len;
7117 MonoMethod *prev_current_method;
7118 MonoGenericContext *prev_generic_context;
7119 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
7121 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
7123 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
7124 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
7127 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
7128 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
7133 fsig = mono_method_signature (cmethod);
7135 if (cfg->verbose_level > 2)
7136 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7138 if (!cmethod->inline_info) {
7139 cfg->stat_inlineable_methods++;
7140 cmethod->inline_info = 1;
7143 /* allocate local variables */
7144 cheader = mono_method_get_header_checked (cmethod, &error);
7146 if (inline_always) {
7147 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7148 mono_error_move (&cfg->error, &error);
7150 mono_error_cleanup (&error);
7155 /*Must verify before creating locals as it can cause the JIT to assert.*/
7156 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
7157 mono_metadata_free_mh (cheader);
7161 /* allocate space to store the return value */
7162 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
7163 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
7166 prev_locals = cfg->locals;
7167 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
7168 for (i = 0; i < cheader->num_locals; ++i)
7169 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
7171 /* allocate start and end blocks */
7172 /* This is needed so if the inline is aborted, we can clean up */
7173 NEW_BBLOCK (cfg, sbblock);
7174 sbblock->real_offset = real_offset;
7176 NEW_BBLOCK (cfg, ebblock);
7177 ebblock->block_num = cfg->num_bblocks++;
7178 ebblock->real_offset = real_offset;
7180 prev_args = cfg->args;
7181 prev_arg_types = cfg->arg_types;
7182 prev_inlined_method = cfg->inlined_method;
7183 cfg->inlined_method = cmethod;
7184 cfg->ret_var_set = FALSE;
7185 cfg->inline_depth ++;
7186 prev_real_offset = cfg->real_offset;
7187 prev_cbb_hash = cfg->cbb_hash;
7188 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
7189 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
7190 prev_cil_start = cfg->cil_start;
7192 prev_cbb = cfg->cbb;
7193 prev_current_method = cfg->current_method;
7194 prev_generic_context = cfg->generic_context;
7195 prev_ret_var_set = cfg->ret_var_set;
7196 prev_disable_inline = cfg->disable_inline;
7198 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
7201 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
7203 ret_var_set = cfg->ret_var_set;
7205 cfg->inlined_method = prev_inlined_method;
7206 cfg->real_offset = prev_real_offset;
7207 cfg->cbb_hash = prev_cbb_hash;
7208 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
7209 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
7210 cfg->cil_start = prev_cil_start;
7212 cfg->locals = prev_locals;
7213 cfg->args = prev_args;
7214 cfg->arg_types = prev_arg_types;
7215 cfg->current_method = prev_current_method;
7216 cfg->generic_context = prev_generic_context;
7217 cfg->ret_var_set = prev_ret_var_set;
7218 cfg->disable_inline = prev_disable_inline;
7219 cfg->inline_depth --;
7221 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
7222 if (cfg->verbose_level > 2)
7223 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7225 cfg->stat_inlined_methods++;
7227 /* always add some code to avoid block split failures */
7228 MONO_INST_NEW (cfg, ins, OP_NOP);
7229 MONO_ADD_INS (prev_cbb, ins);
7231 prev_cbb->next_bb = sbblock;
7232 link_bblock (cfg, prev_cbb, sbblock);
7235 * Get rid of the begin and end bblocks if possible to aid local
7238 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
7240 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
7241 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
7243 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
7244 MonoBasicBlock *prev = ebblock->in_bb [0];
7246 if (prev->next_bb == ebblock) {
7247 mono_merge_basic_blocks (cfg, prev, ebblock);
7249 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
7250 mono_merge_basic_blocks (cfg, prev_cbb, prev);
7251 cfg->cbb = prev_cbb;
7254 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
7259 * Its possible that the rvar is set in some prev bblock, but not in others.
7265 for (i = 0; i < ebblock->in_count; ++i) {
7266 bb = ebblock->in_bb [i];
7268 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
7271 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7281 * If the inlined method contains only a throw, then the ret var is not
7282 * set, so set it to a dummy value.
7285 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7287 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
7290 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7293 if (cfg->verbose_level > 2)
7294 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
7295 cfg->exception_type = MONO_EXCEPTION_NONE;
7297 /* This gets rid of the newly added bblocks */
7298 cfg->cbb = prev_cbb;
7300 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7305 * Some of these comments may well be out-of-date.
7306 * Design decisions: we do a single pass over the IL code (and we do bblock
7307 * splitting/merging in the few cases when it's required: a back jump to an IL
7308 * address that was not already seen as bblock starting point).
7309 * Code is validated as we go (full verification is still better left to metadata/verify.c).
7310 * Complex operations are decomposed in simpler ones right away. We need to let the
7311 * arch-specific code peek and poke inside this process somehow (except when the
7312 * optimizations can take advantage of the full semantic info of coarse opcodes).
7313 * All the opcodes of the form opcode.s are 'normalized' to opcode.
7314 * MonoInst->opcode initially is the IL opcode or some simplification of that
7315 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
7316 * opcode with value bigger than OP_LAST.
7317 * At this point the IR can be handed over to an interpreter, a dumb code generator
7318 * or to the optimizing code generator that will translate it to SSA form.
7320 * Profiling directed optimizations.
7321 * We may compile by default with few or no optimizations and instrument the code
7322 * or the user may indicate what methods to optimize the most either in a config file
7323 * or through repeated runs where the compiler applies offline the optimizations to
7324 * each method and then decides if it was worth it.
7327 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7328 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7329 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7330 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7331 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7332 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7333 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7334 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
7336 /* offset from br.s -> br like opcodes */
7337 #define BIG_BRANCH_OFFSET 13
7340 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7342 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7344 return b == NULL || b == bb;
7348 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7350 unsigned char *ip = start;
7351 unsigned char *target;
7354 MonoBasicBlock *bblock;
7355 const MonoOpcode *opcode;
7358 cli_addr = ip - start;
7359 i = mono_opcode_value ((const guint8 **)&ip, end);
7362 opcode = &mono_opcodes [i];
7363 switch (opcode->argument) {
7364 case MonoInlineNone:
7367 case MonoInlineString:
7368 case MonoInlineType:
7369 case MonoInlineField:
7370 case MonoInlineMethod:
7373 case MonoShortInlineR:
7380 case MonoShortInlineVar:
7381 case MonoShortInlineI:
7384 case MonoShortInlineBrTarget:
7385 target = start + cli_addr + 2 + (signed char)ip [1];
7386 GET_BBLOCK (cfg, bblock, target);
7389 GET_BBLOCK (cfg, bblock, ip);
7391 case MonoInlineBrTarget:
7392 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7393 GET_BBLOCK (cfg, bblock, target);
7396 GET_BBLOCK (cfg, bblock, ip);
7398 case MonoInlineSwitch: {
7399 guint32 n = read32 (ip + 1);
7402 cli_addr += 5 + 4 * n;
7403 target = start + cli_addr;
7404 GET_BBLOCK (cfg, bblock, target);
7406 for (j = 0; j < n; ++j) {
7407 target = start + cli_addr + (gint32)read32 (ip);
7408 GET_BBLOCK (cfg, bblock, target);
7418 g_assert_not_reached ();
7421 if (i == CEE_THROW) {
7422 unsigned char *bb_start = ip - 1;
7424 /* Find the start of the bblock containing the throw */
7426 while ((bb_start >= start) && !bblock) {
7427 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7431 bblock->out_of_line = 1;
7441 static inline MonoMethod *
7442 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
7446 mono_error_init (error);
7448 if (m->wrapper_type != MONO_WRAPPER_NONE) {
7449 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
7451 method = mono_class_inflate_generic_method_checked (method, context, error);
7454 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
7460 static inline MonoMethod *
7461 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7464 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
7466 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
7467 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
7471 if (!method && !cfg)
7472 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7477 static inline MonoClass*
7478 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7483 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7484 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7486 klass = mono_class_inflate_generic_class_checked (klass, context, &error);
7487 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7490 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7491 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7494 mono_class_init (klass);
7498 static inline MonoMethodSignature*
7499 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
7501 MonoMethodSignature *fsig;
7503 mono_error_init (error);
7504 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7505 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7507 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
7508 return_val_if_nok (error, NULL);
7511 fsig = mono_inflate_generic_signature(fsig, context, error);
7517 throw_exception (void)
7519 static MonoMethod *method = NULL;
7522 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7523 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7530 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7532 MonoMethod *thrower = throw_exception ();
7535 EMIT_NEW_PCONST (cfg, args [0], ex);
7536 mono_emit_method_call (cfg, thrower, args, NULL);
7540 * Return the original method is a wrapper is specified. We can only access
7541 * the custom attributes from the original method.
7544 get_original_method (MonoMethod *method)
7546 if (method->wrapper_type == MONO_WRAPPER_NONE)
7549 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7550 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7553 /* in other cases we need to find the original method */
7554 return mono_marshal_method_from_wrapper (method);
7558 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7560 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7561 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7563 emit_throw_exception (cfg, ex);
7567 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7569 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7570 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7572 emit_throw_exception (cfg, ex);
7576 * Check that the IL instructions at ip are the array initialization
7577 * sequence and return the pointer to the data and the size.
7580 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7583 * newarr[System.Int32]
7585 * ldtoken field valuetype ...
7586 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7588 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7590 guint32 token = read32 (ip + 7);
7591 guint32 field_token = read32 (ip + 2);
7592 guint32 field_index = field_token & 0xffffff;
7594 const char *data_ptr;
7596 MonoMethod *cmethod;
7597 MonoClass *dummy_class;
7598 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7602 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7606 *out_field_token = field_token;
7608 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7611 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7613 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7614 case MONO_TYPE_BOOLEAN:
7618 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7619 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7620 case MONO_TYPE_CHAR:
7637 if (size > mono_type_size (field->type, &dummy_align))
7640 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7641 if (!image_is_dynamic (method->klass->image)) {
7642 field_index = read32 (ip + 2) & 0xffffff;
7643 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7644 data_ptr = mono_image_rva_map (method->klass->image, rva);
7645 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7646 /* for aot code we do the lookup on load */
7647 if (aot && data_ptr)
7648 return (const char *)GUINT_TO_POINTER (rva);
7650 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
7652 data_ptr = mono_field_get_data (field);
7660 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7663 char *method_fname = mono_method_full_name (method, TRUE);
7665 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
7668 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
7669 mono_error_cleanup (&error);
7670 } else if (header->code_size == 0)
7671 method_code = g_strdup ("method body is empty.");
7673 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7674 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
7675 g_free (method_fname);
7676 g_free (method_code);
7677 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7681 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7684 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7685 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
7686 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7687 /* Optimize reg-reg moves away */
7689 * Can't optimize other opcodes, since sp[0] might point to
7690 * the last ins of a decomposed opcode.
7692 sp [0]->dreg = (cfg)->locals [n]->dreg;
7694 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7699 * ldloca inhibits many optimizations so try to get rid of it in common
7702 static inline unsigned char *
7703 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7713 local = read16 (ip + 2);
7717 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7718 /* From the INITOBJ case */
7719 token = read32 (ip + 2);
7720 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7721 CHECK_TYPELOAD (klass);
7722 type = mini_get_underlying_type (&klass->byval_arg);
7723 emit_init_local (cfg, local, type, TRUE);
7731 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7733 MonoInst *icall_args [16];
7734 MonoInst *call_target, *ins, *vtable_ins;
7735 int arg_reg, this_reg, vtable_reg;
7736 gboolean is_iface = mono_class_is_interface (cmethod->klass);
7737 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7738 gboolean variant_iface = FALSE;
7743 * In llvm-only mode, vtables contain function descriptors instead of
7744 * method addresses/trampolines.
7746 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7749 slot = mono_method_get_imt_slot (cmethod);
7751 slot = mono_method_get_vtable_index (cmethod);
7753 this_reg = sp [0]->dreg;
7755 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7756 variant_iface = TRUE;
7758 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7760 * The simplest case, a normal virtual call.
7762 int slot_reg = alloc_preg (cfg);
7763 int addr_reg = alloc_preg (cfg);
7764 int arg_reg = alloc_preg (cfg);
7765 MonoBasicBlock *non_null_bb;
7767 vtable_reg = alloc_preg (cfg);
7768 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7769 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7771 /* Load the vtable slot, which contains a function descriptor. */
7772 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7774 NEW_BBLOCK (cfg, non_null_bb);
7776 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7777 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7778 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7781 // FIXME: Make the wrapper use the preserveall cconv
7782 // FIXME: Use one icall per slot for small slot numbers ?
7783 icall_args [0] = vtable_ins;
7784 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7785 /* Make the icall return the vtable slot value to save some code space */
7786 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7787 ins->dreg = slot_reg;
7788 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7791 MONO_START_BB (cfg, non_null_bb);
7792 /* Load the address + arg from the vtable slot */
7793 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7794 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7796 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7799 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7801 * A simple interface call
7803 * We make a call through an imt slot to obtain the function descriptor we need to call.
7804 * The imt slot contains a function descriptor for a runtime function + arg.
7806 int slot_reg = alloc_preg (cfg);
7807 int addr_reg = alloc_preg (cfg);
7808 int arg_reg = alloc_preg (cfg);
7809 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7811 vtable_reg = alloc_preg (cfg);
7812 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7813 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7816 * The slot is already initialized when the vtable is created so there is no need
7820 /* Load the imt slot, which contains a function descriptor. */
7821 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7823 /* Load the address + arg of the imt thunk from the imt slot */
7824 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7825 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7827 * IMT thunks in llvm-only mode are C functions which take an info argument
7828 * plus the imt method and return the ftndesc to call.
7830 icall_args [0] = thunk_arg_ins;
7831 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7832 cmethod, MONO_RGCTX_INFO_METHOD);
7833 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7835 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7838 if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7840 * This is similar to the interface case, the vtable slot points to an imt thunk which is
7841 * dynamically extended as more instantiations are discovered.
7842 * This handles generic virtual methods both on classes and interfaces.
7844 int slot_reg = alloc_preg (cfg);
7845 int addr_reg = alloc_preg (cfg);
7846 int arg_reg = alloc_preg (cfg);
7847 int ftndesc_reg = alloc_preg (cfg);
7848 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7849 MonoBasicBlock *slowpath_bb, *end_bb;
7851 NEW_BBLOCK (cfg, slowpath_bb);
7852 NEW_BBLOCK (cfg, end_bb);
7854 vtable_reg = alloc_preg (cfg);
7855 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7857 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7859 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7861 /* Load the slot, which contains a function descriptor. */
7862 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7864 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7865 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7866 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7867 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7870 /* Same as with iface calls */
7871 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7872 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7873 icall_args [0] = thunk_arg_ins;
7874 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7875 cmethod, MONO_RGCTX_INFO_METHOD);
7876 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7877 ftndesc_ins->dreg = ftndesc_reg;
7879 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7880 * they don't know about yet. Fall back to the slowpath in that case.
7882 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7883 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7885 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7888 MONO_START_BB (cfg, slowpath_bb);
7889 icall_args [0] = vtable_ins;
7890 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7891 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7892 cmethod, MONO_RGCTX_INFO_METHOD);
7894 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7896 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7897 ftndesc_ins->dreg = ftndesc_reg;
7898 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7901 MONO_START_BB (cfg, end_bb);
7902 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7906 * Non-optimized cases
7908 icall_args [0] = sp [0];
7909 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7911 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7912 cmethod, MONO_RGCTX_INFO_METHOD);
7914 arg_reg = alloc_preg (cfg);
7915 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7916 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7918 g_assert (is_gsharedvt);
7920 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7922 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7925 * Pass the extra argument even if the callee doesn't receive it, most
7926 * calling conventions allow this.
7928 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7932 is_exception_class (MonoClass *klass)
7935 if (klass == mono_defaults.exception_class)
7937 klass = klass->parent;
7943 * is_jit_optimizer_disabled:
7945 * Determine whenever M's assembly has a DebuggableAttribute with the
7946 * IsJITOptimizerDisabled flag set.
7949 is_jit_optimizer_disabled (MonoMethod *m)
7952 MonoAssembly *ass = m->klass->image->assembly;
7953 MonoCustomAttrInfo* attrs;
7956 gboolean val = FALSE;
7959 if (ass->jit_optimizer_disabled_inited)
7960 return ass->jit_optimizer_disabled;
7962 klass = mono_class_try_get_debuggable_attribute_class ();
7966 ass->jit_optimizer_disabled = FALSE;
7967 mono_memory_barrier ();
7968 ass->jit_optimizer_disabled_inited = TRUE;
7972 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
7973 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7975 for (i = 0; i < attrs->num_attrs; ++i) {
7976 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7978 MonoMethodSignature *sig;
7980 if (!attr->ctor || attr->ctor->klass != klass)
7982 /* Decode the attribute. See reflection.c */
7983 p = (const char*)attr->data;
7984 g_assert (read16 (p) == 0x0001);
7987 // FIXME: Support named parameters
7988 sig = mono_method_signature (attr->ctor);
7989 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7991 /* Two boolean arguments */
7995 mono_custom_attrs_free (attrs);
7998 ass->jit_optimizer_disabled = val;
7999 mono_memory_barrier ();
8000 ass->jit_optimizer_disabled_inited = TRUE;
8006 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
8008 gboolean supported_tail_call;
8011 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
8013 for (i = 0; i < fsig->param_count; ++i) {
8014 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
8015 /* These can point to the current method's stack */
8016 supported_tail_call = FALSE;
8018 if (fsig->hasthis && cmethod->klass->valuetype)
8019 /* this might point to the current method's stack */
8020 supported_tail_call = FALSE;
8021 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
8022 supported_tail_call = FALSE;
8023 if (cfg->method->save_lmf)
8024 supported_tail_call = FALSE;
8025 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
8026 supported_tail_call = FALSE;
8027 if (call_opcode != CEE_CALL)
8028 supported_tail_call = FALSE;
8030 /* Debugging support */
8032 if (supported_tail_call) {
8033 if (!mono_debug_count ())
8034 supported_tail_call = FALSE;
8038 return supported_tail_call;
8044 * Handle calls made to ctors from NEWOBJ opcodes.
8047 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
8048 MonoInst **sp, guint8 *ip, int *inline_costs)
8050 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
8052 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
8053 mono_method_is_generic_sharable (cmethod, TRUE)) {
8054 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
8055 mono_class_vtable (cfg->domain, cmethod->klass);
8056 CHECK_TYPELOAD (cmethod->klass);
8058 vtable_arg = emit_get_rgctx_method (cfg, context_used,
8059 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8062 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
8063 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8065 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8067 CHECK_TYPELOAD (cmethod->klass);
8068 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8073 /* Avoid virtual calls to ctors if possible */
8074 if (mono_class_is_marshalbyref (cmethod->klass))
8075 callvirt_this_arg = sp [0];
8077 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
8078 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
8079 CHECK_CFG_EXCEPTION;
8080 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
8081 mono_method_check_inlining (cfg, cmethod) &&
8082 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
8085 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
8086 cfg->real_offset += 5;
8088 *inline_costs += costs - 5;
8090 INLINE_FAILURE ("inline failure");
8091 // FIXME-VT: Clean this up
8092 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8093 GSHAREDVT_FAILURE(*ip);
8094 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
8096 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8099 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
8101 if (cfg->llvm_only) {
8102 // FIXME: Avoid initializing vtable_arg
8103 emit_llvmonly_calli (cfg, fsig, sp, addr);
8105 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
8107 } else if (context_used &&
8108 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8109 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
8110 MonoInst *cmethod_addr;
8112 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
8114 if (cfg->llvm_only) {
8115 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
8116 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8117 emit_llvmonly_calli (cfg, fsig, sp, addr);
8119 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
8120 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8122 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
8125 INLINE_FAILURE ("ctor call");
8126 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
8127 callvirt_this_arg, NULL, vtable_arg);
8134 emit_setret (MonoCompile *cfg, MonoInst *val)
8136 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
8139 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
8142 if (!cfg->vret_addr) {
8143 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
8145 EMIT_NEW_RETLOADA (cfg, ret_addr);
8147 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
8148 ins->klass = mono_class_from_mono_type (ret_type);
8151 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
8152 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
8153 MonoInst *iargs [1];
8157 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
8158 mono_arch_emit_setret (cfg, cfg->method, conv);
8160 mono_arch_emit_setret (cfg, cfg->method, val);
8163 mono_arch_emit_setret (cfg, cfg->method, val);
8169 * mono_method_to_ir:
8171 * Translate the .net IL into linear IR.
8173 * @start_bblock: if not NULL, the starting basic block, used during inlining.
8174 * @end_bblock: if not NULL, the ending basic block, used during inlining.
8175 * @return_var: if not NULL, the place where the return value is stored, used during inlining.
8176 * @inline_args: if not NULL, contains the arguments to the inline call
8177 * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
8178 * @is_virtual_call: whether this method is being called as a result of a call to callvirt
8180 * This method is used to turn ECMA IL into Mono's internal Linear IR
8181 * reprensetation. It is used both for entire methods, as well as
8182 * inlining existing methods. In the former case, the @start_bblock,
8183 * @end_bblock, @return_var, @inline_args are all set to NULL, and the
8184 * inline_offset is set to zero.
8186 * Returns: the inline cost, or -1 if there was an error processing this method.
8189 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
8190 MonoInst *return_var, MonoInst **inline_args,
8191 guint inline_offset, gboolean is_virtual_call)
8194 MonoInst *ins, **sp, **stack_start;
8195 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
8196 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
8197 MonoMethod *cmethod, *method_definition;
8198 MonoInst **arg_array;
8199 MonoMethodHeader *header;
8201 guint32 token, ins_flag;
8203 MonoClass *constrained_class = NULL;
8204 unsigned char *ip, *end, *target, *err_pos;
8205 MonoMethodSignature *sig;
8206 MonoGenericContext *generic_context = NULL;
8207 MonoGenericContainer *generic_container = NULL;
8208 MonoType **param_types;
8209 int i, n, start_new_bblock, dreg;
8210 int num_calls = 0, inline_costs = 0;
8211 int breakpoint_id = 0;
8213 GSList *class_inits = NULL;
8214 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
8216 gboolean init_locals, seq_points, skip_dead_blocks;
8217 gboolean sym_seq_points = FALSE;
8218 MonoDebugMethodInfo *minfo;
8219 MonoBitSet *seq_point_locs = NULL;
8220 MonoBitSet *seq_point_set_locs = NULL;
8222 cfg->disable_inline = is_jit_optimizer_disabled (method);
8224 /* serialization and xdomain stuff may need access to private fields and methods */
8225 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
8226 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
8227 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
8228 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
8229 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
8230 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
8232 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
8233 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
8234 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
8235 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
8236 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
8238 image = method->klass->image;
8239 header = mono_method_get_header_checked (method, &cfg->error);
8241 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
8242 goto exception_exit;
8244 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
8247 generic_container = mono_method_get_generic_container (method);
8248 sig = mono_method_signature (method);
8249 num_args = sig->hasthis + sig->param_count;
8250 ip = (unsigned char*)header->code;
8251 cfg->cil_start = ip;
8252 end = ip + header->code_size;
8253 cfg->stat_cil_code_size += header->code_size;
8255 seq_points = cfg->gen_seq_points && cfg->method == method;
8257 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8258 /* We could hit a seq point before attaching to the JIT (#8338) */
8262 if (cfg->gen_sdb_seq_points && cfg->method == method) {
8263 minfo = mono_debug_lookup_method (method);
8265 MonoSymSeqPoint *sps;
8266 int i, n_il_offsets;
8268 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
8269 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8270 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8271 sym_seq_points = TRUE;
8272 for (i = 0; i < n_il_offsets; ++i) {
8273 if (sps [i].il_offset < header->code_size)
8274 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
8277 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
8278 /* Methods without line number info like auto-generated property accessors */
8279 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8280 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8281 sym_seq_points = TRUE;
8286 * Methods without init_locals set could cause asserts in various passes
8287 * (#497220). To work around this, we emit dummy initialization opcodes
8288 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
8289 * on some platforms.
8291 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
8292 init_locals = header->init_locals;
8296 method_definition = method;
8297 while (method_definition->is_inflated) {
8298 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
8299 method_definition = imethod->declaring;
8302 /* SkipVerification is not allowed if core-clr is enabled */
8303 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
8305 dont_verify_stloc = TRUE;
8308 if (sig->is_inflated)
8309 generic_context = mono_method_get_context (method);
8310 else if (generic_container)
8311 generic_context = &generic_container->context;
8312 cfg->generic_context = generic_context;
8315 g_assert (!sig->has_type_parameters);
8317 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
8318 g_assert (method->is_inflated);
8319 g_assert (mono_method_get_context (method)->method_inst);
8321 if (method->is_inflated && mono_method_get_context (method)->method_inst)
8322 g_assert (sig->generic_param_count);
8324 if (cfg->method == method) {
8325 cfg->real_offset = 0;
8327 cfg->real_offset = inline_offset;
8330 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
8331 cfg->cil_offset_to_bb_len = header->code_size;
8333 cfg->current_method = method;
8335 if (cfg->verbose_level > 2)
8336 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
8338 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
8340 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
8341 for (n = 0; n < sig->param_count; ++n)
8342 param_types [n + sig->hasthis] = sig->params [n];
8343 cfg->arg_types = param_types;
8345 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
8346 if (cfg->method == method) {
8348 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
8349 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
8352 NEW_BBLOCK (cfg, start_bblock);
8353 cfg->bb_entry = start_bblock;
8354 start_bblock->cil_code = NULL;
8355 start_bblock->cil_length = 0;
8358 NEW_BBLOCK (cfg, end_bblock);
8359 cfg->bb_exit = end_bblock;
8360 end_bblock->cil_code = NULL;
8361 end_bblock->cil_length = 0;
8362 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
8363 g_assert (cfg->num_bblocks == 2);
8365 arg_array = cfg->args;
8367 if (header->num_clauses) {
8368 cfg->spvars = g_hash_table_new (NULL, NULL);
8369 cfg->exvars = g_hash_table_new (NULL, NULL);
8371 /* handle exception clauses */
8372 for (i = 0; i < header->num_clauses; ++i) {
8373 MonoBasicBlock *try_bb;
8374 MonoExceptionClause *clause = &header->clauses [i];
8375 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
8377 try_bb->real_offset = clause->try_offset;
8378 try_bb->try_start = TRUE;
8379 try_bb->region = ((i + 1) << 8) | clause->flags;
8380 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
8381 tblock->real_offset = clause->handler_offset;
8382 tblock->flags |= BB_EXCEPTION_HANDLER;
8385 * Linking the try block with the EH block hinders inlining as we won't be able to
8386 * merge the bblocks from inlining and produce an artificial hole for no good reason.
8388 if (COMPILE_LLVM (cfg))
8389 link_bblock (cfg, try_bb, tblock);
8391 if (*(ip + clause->handler_offset) == CEE_POP)
8392 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
8394 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
8395 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
8396 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
8397 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8398 MONO_ADD_INS (tblock, ins);
8400 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
8401 /* finally clauses already have a seq point */
8402 /* seq points for filter clauses are emitted below */
8403 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8404 MONO_ADD_INS (tblock, ins);
8407 /* todo: is a fault block unsafe to optimize? */
8408 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
8409 tblock->flags |= BB_EXCEPTION_UNSAFE;
8412 /*printf ("clause try IL_%04x to IL_%04x handler %d at IL_%04x to IL_%04x\n", clause->try_offset, clause->try_offset + clause->try_len, clause->flags, clause->handler_offset, clause->handler_offset + clause->handler_len);
8414 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
8416 /* catch and filter blocks get the exception object on the stack */
8417 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
8418 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8420 /* mostly like handle_stack_args (), but just sets the input args */
8421 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
8422 tblock->in_scount = 1;
8423 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8424 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8428 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
8429 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
8430 if (!cfg->compile_llvm) {
8431 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
8432 ins->dreg = tblock->in_stack [0]->dreg;
8433 MONO_ADD_INS (tblock, ins);
8436 MonoInst *dummy_use;
8439 * Add a dummy use for the exvar so its liveness info will be
8442 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
8445 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8446 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8447 MONO_ADD_INS (tblock, ins);
8450 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8451 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
8452 tblock->flags |= BB_EXCEPTION_HANDLER;
8453 tblock->real_offset = clause->data.filter_offset;
8454 tblock->in_scount = 1;
8455 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8456 /* The filter block shares the exvar with the handler block */
8457 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8458 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8459 MONO_ADD_INS (tblock, ins);
8463 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
8464 clause->data.catch_class &&
8466 mono_class_check_context_used (clause->data.catch_class)) {
8468 * In shared generic code with catch
8469 * clauses containing type variables
8470 * the exception handling code has to
8471 * be able to get to the rgctx.
8472 * Therefore we have to make sure that
8473 * the vtable/mrgctx argument (for
8474 * static or generic methods) or the
8475 * "this" argument (for non-static
8476 * methods) are live.
8478 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8479 mini_method_get_context (method)->method_inst ||
8480 method->klass->valuetype) {
8481 mono_get_vtable_var (cfg);
8483 MonoInst *dummy_use;
8485 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
8490 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
8491 cfg->cbb = start_bblock;
8492 cfg->args = arg_array;
8493 mono_save_args (cfg, sig, inline_args);
8496 /* FIRST CODE BLOCK */
8497 NEW_BBLOCK (cfg, tblock);
8498 tblock->cil_code = ip;
8502 ADD_BBLOCK (cfg, tblock);
8504 if (cfg->method == method) {
8505 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8506 if (breakpoint_id) {
8507 MONO_INST_NEW (cfg, ins, OP_BREAK);
8508 MONO_ADD_INS (cfg->cbb, ins);
8512 /* we use a separate basic block for the initialization code */
8513 NEW_BBLOCK (cfg, init_localsbb);
8514 if (cfg->method == method)
8515 cfg->bb_init = init_localsbb;
8516 init_localsbb->real_offset = cfg->real_offset;
8517 start_bblock->next_bb = init_localsbb;
8518 init_localsbb->next_bb = cfg->cbb;
8519 link_bblock (cfg, start_bblock, init_localsbb);
8520 link_bblock (cfg, init_localsbb, cfg->cbb);
8522 cfg->cbb = init_localsbb;
8524 if (cfg->gsharedvt && cfg->method == method) {
8525 MonoGSharedVtMethodInfo *info;
8526 MonoInst *var, *locals_var;
8529 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8530 info->method = cfg->method;
8531 info->count_entries = 16;
8532 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8533 cfg->gsharedvt_info = info;
8535 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8536 /* prevent it from being register allocated */
8537 //var->flags |= MONO_INST_VOLATILE;
8538 cfg->gsharedvt_info_var = var;
8540 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8541 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8543 /* Allocate locals */
8544 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8545 /* prevent it from being register allocated */
8546 //locals_var->flags |= MONO_INST_VOLATILE;
8547 cfg->gsharedvt_locals_var = locals_var;
8549 dreg = alloc_ireg (cfg);
8550 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8552 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8553 ins->dreg = locals_var->dreg;
8555 MONO_ADD_INS (cfg->cbb, ins);
8556 cfg->gsharedvt_locals_var_ins = ins;
8558 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8561 ins->flags |= MONO_INST_INIT;
8565 if (mono_security_core_clr_enabled ()) {
8566 /* check if this is native code, e.g. an icall or a p/invoke */
8567 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8568 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8570 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8571 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8573 /* if this ia a native call then it can only be JITted from platform code */
8574 if ((icall || pinvk) && method->klass && method->klass->image) {
8575 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8576 MonoException *ex = icall ? mono_get_exception_security () :
8577 mono_get_exception_method_access ();
8578 emit_throw_exception (cfg, ex);
8585 CHECK_CFG_EXCEPTION;
8587 if (header->code_size == 0)
8590 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8595 if (cfg->method == method)
8596 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8598 for (n = 0; n < header->num_locals; ++n) {
8599 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8604 /* We force the vtable variable here for all shared methods
8605 for the possibility that they might show up in a stack
8606 trace where their exact instantiation is needed. */
8607 if (cfg->gshared && method == cfg->method) {
8608 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8609 mini_method_get_context (method)->method_inst ||
8610 method->klass->valuetype) {
8611 mono_get_vtable_var (cfg);
8613 /* FIXME: Is there a better way to do this?
8614 We need the variable live for the duration
8615 of the whole method. */
8616 cfg->args [0]->flags |= MONO_INST_VOLATILE;
8620 /* add a check for this != NULL to inlined methods */
8621 if (is_virtual_call) {
8624 NEW_ARGLOAD (cfg, arg_ins, 0);
8625 MONO_ADD_INS (cfg->cbb, arg_ins);
8626 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8629 skip_dead_blocks = !dont_verify;
8630 if (skip_dead_blocks) {
8631 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
8636 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8637 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8640 start_new_bblock = 0;
8642 if (cfg->method == method)
8643 cfg->real_offset = ip - header->code;
8645 cfg->real_offset = inline_offset;
8650 if (start_new_bblock) {
8651 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8652 if (start_new_bblock == 2) {
8653 g_assert (ip == tblock->cil_code);
8655 GET_BBLOCK (cfg, tblock, ip);
8657 cfg->cbb->next_bb = tblock;
8659 start_new_bblock = 0;
8660 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8661 if (cfg->verbose_level > 3)
8662 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8663 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8667 g_slist_free (class_inits);
8670 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8671 link_bblock (cfg, cfg->cbb, tblock);
8672 if (sp != stack_start) {
8673 handle_stack_args (cfg, stack_start, sp - stack_start);
8675 CHECK_UNVERIFIABLE (cfg);
8677 cfg->cbb->next_bb = tblock;
8679 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8680 if (cfg->verbose_level > 3)
8681 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8682 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8685 g_slist_free (class_inits);
8690 if (skip_dead_blocks) {
8691 int ip_offset = ip - header->code;
8693 if (ip_offset == bb->end)
8697 int op_size = mono_opcode_size (ip, end);
8698 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8700 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8702 if (ip_offset + op_size == bb->end) {
8703 MONO_INST_NEW (cfg, ins, OP_NOP);
8704 MONO_ADD_INS (cfg->cbb, ins);
8705 start_new_bblock = 1;
8713 * Sequence points are points where the debugger can place a breakpoint.
8714 * Currently, we generate these automatically at points where the IL
8717 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8719 * Make methods interruptable at the beginning, and at the targets of
8720 * backward branches.
8721 * Also, do this at the start of every bblock in methods with clauses too,
8722 * to be able to handle instructions with inprecise control flow like
8724 * Backward branches are handled at the end of method-to-ir ().
8726 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8727 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8729 /* Avoid sequence points on empty IL like .volatile */
8730 // FIXME: Enable this
8731 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8732 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8733 if ((sp != stack_start) && !sym_seq_point)
8734 ins->flags |= MONO_INST_NONEMPTY_STACK;
8735 MONO_ADD_INS (cfg->cbb, ins);
8738 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8741 cfg->cbb->real_offset = cfg->real_offset;
8743 if ((cfg->method == method) && cfg->coverage_info) {
8744 guint32 cil_offset = ip - header->code;
8745 cfg->coverage_info->data [cil_offset].cil_code = ip;
8747 /* TODO: Use an increment here */
8748 #if defined(TARGET_X86)
8749 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8750 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8752 MONO_ADD_INS (cfg->cbb, ins);
8754 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8755 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8759 if (cfg->verbose_level > 3)
8760 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8764 if (seq_points && !sym_seq_points && sp != stack_start) {
8766 * The C# compiler uses these nops to notify the JIT that it should
8767 * insert seq points.
8769 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8770 MONO_ADD_INS (cfg->cbb, ins);
8772 if (cfg->keep_cil_nops)
8773 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8775 MONO_INST_NEW (cfg, ins, OP_NOP);
8777 MONO_ADD_INS (cfg->cbb, ins);
8780 if (should_insert_brekpoint (cfg->method)) {
8781 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8783 MONO_INST_NEW (cfg, ins, OP_NOP);
8786 MONO_ADD_INS (cfg->cbb, ins);
8792 CHECK_STACK_OVF (1);
8793 n = (*ip)-CEE_LDARG_0;
8795 EMIT_NEW_ARGLOAD (cfg, ins, n);
8803 CHECK_STACK_OVF (1);
8804 n = (*ip)-CEE_LDLOC_0;
8806 EMIT_NEW_LOCLOAD (cfg, ins, n);
8815 n = (*ip)-CEE_STLOC_0;
8818 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8820 emit_stloc_ir (cfg, sp, header, n);
8827 CHECK_STACK_OVF (1);
8830 EMIT_NEW_ARGLOAD (cfg, ins, n);
8836 CHECK_STACK_OVF (1);
8839 NEW_ARGLOADA (cfg, ins, n);
8840 MONO_ADD_INS (cfg->cbb, ins);
8850 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8852 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8857 CHECK_STACK_OVF (1);
8860 EMIT_NEW_LOCLOAD (cfg, ins, n);
8864 case CEE_LDLOCA_S: {
8865 unsigned char *tmp_ip;
8867 CHECK_STACK_OVF (1);
8868 CHECK_LOCAL (ip [1]);
8870 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8876 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8885 CHECK_LOCAL (ip [1]);
8886 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8888 emit_stloc_ir (cfg, sp, header, ip [1]);
8893 CHECK_STACK_OVF (1);
8894 EMIT_NEW_PCONST (cfg, ins, NULL);
8895 ins->type = STACK_OBJ;
8900 CHECK_STACK_OVF (1);
8901 EMIT_NEW_ICONST (cfg, ins, -1);
8914 CHECK_STACK_OVF (1);
8915 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8921 CHECK_STACK_OVF (1);
8923 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8929 CHECK_STACK_OVF (1);
8930 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8936 CHECK_STACK_OVF (1);
8937 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8938 ins->type = STACK_I8;
8939 ins->dreg = alloc_dreg (cfg, STACK_I8);
8941 ins->inst_l = (gint64)read64 (ip);
8942 MONO_ADD_INS (cfg->cbb, ins);
8948 gboolean use_aotconst = FALSE;
8950 #ifdef TARGET_POWERPC
8951 /* FIXME: Clean this up */
8952 if (cfg->compile_aot)
8953 use_aotconst = TRUE;
8956 /* FIXME: we should really allocate this only late in the compilation process */
8957 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8959 CHECK_STACK_OVF (1);
8965 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8967 dreg = alloc_freg (cfg);
8968 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8969 ins->type = cfg->r4_stack_type;
8971 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8972 ins->type = cfg->r4_stack_type;
8973 ins->dreg = alloc_dreg (cfg, STACK_R8);
8975 MONO_ADD_INS (cfg->cbb, ins);
8985 gboolean use_aotconst = FALSE;
8987 #ifdef TARGET_POWERPC
8988 /* FIXME: Clean this up */
8989 if (cfg->compile_aot)
8990 use_aotconst = TRUE;
8993 /* FIXME: we should really allocate this only late in the compilation process */
8994 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8996 CHECK_STACK_OVF (1);
9002 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
9004 dreg = alloc_freg (cfg);
9005 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
9006 ins->type = STACK_R8;
9008 MONO_INST_NEW (cfg, ins, OP_R8CONST);
9009 ins->type = STACK_R8;
9010 ins->dreg = alloc_dreg (cfg, STACK_R8);
9012 MONO_ADD_INS (cfg->cbb, ins);
9021 MonoInst *temp, *store;
9023 CHECK_STACK_OVF (1);
9027 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
9028 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
9030 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
9033 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
9046 if (sp [0]->type == STACK_R8)
9047 /* we need to pop the value from the x86 FP stack */
9048 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
9053 MonoMethodSignature *fsig;
9056 INLINE_FAILURE ("jmp");
9057 GSHAREDVT_FAILURE (*ip);
9060 if (stack_start != sp)
9062 token = read32 (ip + 1);
9063 /* FIXME: check the signature matches */
9064 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9067 if (cfg->gshared && mono_method_check_context_used (cmethod))
9068 GENERIC_SHARING_FAILURE (CEE_JMP);
9070 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9072 fsig = mono_method_signature (cmethod);
9073 n = fsig->param_count + fsig->hasthis;
9074 if (cfg->llvm_only) {
9077 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9078 for (i = 0; i < n; ++i)
9079 EMIT_NEW_ARGLOAD (cfg, args [i], i);
9080 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
9082 * The code in mono-basic-block.c treats the rest of the code as dead, but we
9083 * have to emit a normal return since llvm expects it.
9086 emit_setret (cfg, ins);
9087 MONO_INST_NEW (cfg, ins, OP_BR);
9088 ins->inst_target_bb = end_bblock;
9089 MONO_ADD_INS (cfg->cbb, ins);
9090 link_bblock (cfg, cfg->cbb, end_bblock);
9093 } else if (cfg->backend->have_op_tail_call) {
9094 /* Handle tail calls similarly to calls */
9097 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
9098 call->method = cmethod;
9099 call->tail_call = TRUE;
9100 call->signature = mono_method_signature (cmethod);
9101 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9102 call->inst.inst_p0 = cmethod;
9103 for (i = 0; i < n; ++i)
9104 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
9106 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
9107 call->vret_var = cfg->vret_addr;
9109 mono_arch_emit_call (cfg, call);
9110 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
9111 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
9113 for (i = 0; i < num_args; ++i)
9114 /* Prevent arguments from being optimized away */
9115 arg_array [i]->flags |= MONO_INST_VOLATILE;
9117 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9118 ins = (MonoInst*)call;
9119 ins->inst_p0 = cmethod;
9120 MONO_ADD_INS (cfg->cbb, ins);
9124 start_new_bblock = 1;
9129 MonoMethodSignature *fsig;
9132 token = read32 (ip + 1);
9136 //GSHAREDVT_FAILURE (*ip);
9141 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
9144 if (method->dynamic && fsig->pinvoke) {
9148 * This is a call through a function pointer using a pinvoke
9149 * signature. Have to create a wrapper and call that instead.
9150 * FIXME: This is very slow, need to create a wrapper at JIT time
9151 * instead based on the signature.
9153 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
9154 EMIT_NEW_PCONST (cfg, args [1], fsig);
9156 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
9159 n = fsig->param_count + fsig->hasthis;
9163 //g_assert (!virtual_ || fsig->hasthis);
9167 inline_costs += 10 * num_calls++;
9170 * Making generic calls out of gsharedvt methods.
9171 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9172 * patching gshared method addresses into a gsharedvt method.
9174 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
9176 * We pass the address to the gsharedvt trampoline in the rgctx reg
9178 MonoInst *callee = addr;
9180 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9182 GSHAREDVT_FAILURE (*ip);
9186 GSHAREDVT_FAILURE (*ip);
9188 addr = emit_get_rgctx_sig (cfg, context_used,
9189 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9190 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9194 /* Prevent inlining of methods with indirect calls */
9195 INLINE_FAILURE ("indirect call");
9197 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9198 MonoJumpInfoType info_type;
9202 * Instead of emitting an indirect call, emit a direct call
9203 * with the contents of the aotconst as the patch info.
9205 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9206 info_type = (MonoJumpInfoType)addr->inst_c1;
9207 info_data = addr->inst_p0;
9209 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
9210 info_data = addr->inst_right->inst_left;
9213 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
9214 ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
9217 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9218 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9223 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9227 /* End of call, INS should contain the result of the call, if any */
9229 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9231 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9234 CHECK_CFG_EXCEPTION;
9238 constrained_class = NULL;
9242 case CEE_CALLVIRT: {
9243 MonoInst *addr = NULL;
9244 MonoMethodSignature *fsig = NULL;
9246 int virtual_ = *ip == CEE_CALLVIRT;
9247 gboolean pass_imt_from_rgctx = FALSE;
9248 MonoInst *imt_arg = NULL;
9249 MonoInst *keep_this_alive = NULL;
9250 gboolean pass_vtable = FALSE;
9251 gboolean pass_mrgctx = FALSE;
9252 MonoInst *vtable_arg = NULL;
9253 gboolean check_this = FALSE;
9254 gboolean supported_tail_call = FALSE;
9255 gboolean tail_call = FALSE;
9256 gboolean need_seq_point = FALSE;
9257 guint32 call_opcode = *ip;
9258 gboolean emit_widen = TRUE;
9259 gboolean push_res = TRUE;
9260 gboolean skip_ret = FALSE;
9261 gboolean delegate_invoke = FALSE;
9262 gboolean direct_icall = FALSE;
9263 gboolean constrained_partial_call = FALSE;
9264 MonoMethod *cil_method;
9267 token = read32 (ip + 1);
9271 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9274 cil_method = cmethod;
9276 if (constrained_class) {
9277 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9278 if (!mini_is_gsharedvt_klass (constrained_class)) {
9279 g_assert (!cmethod->klass->valuetype);
9280 if (!mini_type_is_reference (&constrained_class->byval_arg))
9281 constrained_partial_call = TRUE;
9285 if (method->wrapper_type != MONO_WRAPPER_NONE) {
9286 if (cfg->verbose_level > 2)
9287 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9288 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
9289 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
9291 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
9295 if (cfg->verbose_level > 2)
9296 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9298 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9300 * This is needed since get_method_constrained can't find
9301 * the method in klass representing a type var.
9302 * The type var is guaranteed to be a reference type in this
9305 if (!mini_is_gsharedvt_klass (constrained_class))
9306 g_assert (!cmethod->klass->valuetype);
9308 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
9314 if (!dont_verify && !cfg->skip_visibility) {
9315 MonoMethod *target_method = cil_method;
9316 if (method->is_inflated) {
9317 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9320 if (!mono_method_can_access_method (method_definition, target_method) &&
9321 !mono_method_can_access_method (method, cil_method))
9322 emit_method_access_failure (cfg, method, cil_method);
9325 if (mono_security_core_clr_enabled ())
9326 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
9328 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
9329 /* MS.NET seems to silently convert this to a callvirt */
9334 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
9335 * converts to a callvirt.
9337 * tests/bug-515884.il is an example of this behavior
9339 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
9340 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
9341 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
9345 if (!cmethod->klass->inited)
9346 if (!mono_class_init (cmethod->klass))
9347 TYPE_LOAD_ERROR (cmethod->klass);
9349 fsig = mono_method_signature (cmethod);
9352 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
9353 mini_class_is_system_array (cmethod->klass)) {
9354 array_rank = cmethod->klass->rank;
9355 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
9356 direct_icall = TRUE;
9357 } else if (fsig->pinvoke) {
9358 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9359 fsig = mono_method_signature (wrapper);
9360 } else if (constrained_class) {
9362 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9366 if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
9367 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
9369 /* See code below */
9370 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9371 MonoBasicBlock *tbb;
9373 GET_BBLOCK (cfg, tbb, ip + 5);
9374 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9376 * We want to extend the try block to cover the call, but we can't do it if the
9377 * call is made directly since its followed by an exception check.
9379 direct_icall = FALSE;
9383 mono_save_token_info (cfg, image, token, cil_method);
9385 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
9386 need_seq_point = TRUE;
9388 /* Don't support calls made using type arguments for now */
9390 if (cfg->gsharedvt) {
9391 if (mini_is_gsharedvt_signature (fsig))
9392 GSHAREDVT_FAILURE (*ip);
9396 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
9397 g_assert_not_reached ();
9399 n = fsig->param_count + fsig->hasthis;
9401 if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
9405 g_assert (!mono_method_check_context_used (cmethod));
9409 //g_assert (!virtual_ || fsig->hasthis);
9414 * We have the `constrained.' prefix opcode.
9416 if (constrained_class) {
9417 if (mini_is_gsharedvt_klass (constrained_class)) {
9418 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
9419 /* The 'Own method' case below */
9420 } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
9421 /* 'The type parameter is instantiated as a reference type' case below. */
9423 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
9424 CHECK_CFG_EXCEPTION;
9430 if (constrained_partial_call) {
9431 gboolean need_box = TRUE;
9434 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
9435 * called method is not known at compile time either. The called method could end up being
9436 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
9437 * to box the receiver.
9438 * A simple solution would be to box always and make a normal virtual call, but that would
9439 * be bad performance wise.
9441 if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
9443 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
9448 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9449 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
9450 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9451 ins->klass = constrained_class;
9452 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9453 CHECK_CFG_EXCEPTION;
9454 } else if (need_box) {
9456 MonoBasicBlock *is_ref_bb, *end_bb;
9457 MonoInst *nonbox_call;
9460 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
9462 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
9463 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
9465 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9467 NEW_BBLOCK (cfg, is_ref_bb);
9468 NEW_BBLOCK (cfg, end_bb);
9470 box_type = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_BOX_TYPE);
9471 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
9472 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
9475 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9477 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9480 MONO_START_BB (cfg, is_ref_bb);
9481 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9482 ins->klass = constrained_class;
9483 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9484 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9486 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9488 MONO_START_BB (cfg, end_bb);
9491 nonbox_call->dreg = ins->dreg;
9494 g_assert (mono_class_is_interface (cmethod->klass));
9495 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9496 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9499 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9501 * The type parameter is instantiated as a valuetype,
9502 * but that type doesn't override the method we're
9503 * calling, so we need to box `this'.
9505 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9506 ins->klass = constrained_class;
9507 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9508 CHECK_CFG_EXCEPTION;
9509 } else if (!constrained_class->valuetype) {
9510 int dreg = alloc_ireg_ref (cfg);
9513 * The type parameter is instantiated as a reference
9514 * type. We have a managed pointer on the stack, so
9515 * we need to dereference it here.
9517 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
9518 ins->type = STACK_OBJ;
9521 if (cmethod->klass->valuetype) {
9524 /* Interface method */
9527 mono_class_setup_vtable (constrained_class);
9528 CHECK_TYPELOAD (constrained_class);
9529 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
9531 TYPE_LOAD_ERROR (constrained_class);
9532 slot = mono_method_get_vtable_slot (cmethod);
9534 TYPE_LOAD_ERROR (cmethod->klass);
9535 cmethod = constrained_class->vtable [ioffset + slot];
9537 if (cmethod->klass == mono_defaults.enum_class) {
9538 /* Enum implements some interfaces, so treat this as the first case */
9539 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9540 ins->klass = constrained_class;
9541 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9542 CHECK_CFG_EXCEPTION;
9547 constrained_class = NULL;
9550 if (check_call_signature (cfg, fsig, sp))
9553 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
9554 delegate_invoke = TRUE;
9556 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
9557 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9558 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9566 * If the callee is a shared method, then its static cctor
9567 * might not get called after the call was patched.
9569 if (cfg->gshared && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
9570 emit_class_init (cfg, cmethod->klass);
9571 CHECK_TYPELOAD (cmethod->klass);
9574 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
9577 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
9579 context_used = mini_method_check_context_used (cfg, cmethod);
9581 if (context_used && mono_class_is_interface (cmethod->klass)) {
9582 /* Generic method interface
9583 calls are resolved via a
9584 helper function and don't
9586 if (!cmethod_context || !cmethod_context->method_inst)
9587 pass_imt_from_rgctx = TRUE;
9591 * If a shared method calls another
9592 * shared method then the caller must
9593 * have a generic sharing context
9594 * because the magic trampoline
9595 * requires it. FIXME: We shouldn't
9596 * have to force the vtable/mrgctx
9597 * variable here. Instead there
9598 * should be a flag in the cfg to
9599 * request a generic sharing context.
9602 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9603 mono_get_vtable_var (cfg);
9608 vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9610 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9612 CHECK_TYPELOAD (cmethod->klass);
9613 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9618 g_assert (!vtable_arg);
9620 if (!cfg->compile_aot) {
9622 * emit_get_rgctx_method () calls mono_class_vtable () so check
9623 * for type load errors before.
9625 mono_class_setup_vtable (cmethod->klass);
9626 CHECK_TYPELOAD (cmethod->klass);
9629 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9631 /* !marshalbyref is needed to properly handle generic methods + remoting */
9632 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9633 MONO_METHOD_IS_FINAL (cmethod)) &&
9634 !mono_class_is_marshalbyref (cmethod->klass)) {
9641 if (pass_imt_from_rgctx) {
9642 g_assert (!pass_vtable);
9644 imt_arg = emit_get_rgctx_method (cfg, context_used,
9645 cmethod, MONO_RGCTX_INFO_METHOD);
9649 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9651 /* Calling virtual generic methods */
9652 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9653 !(MONO_METHOD_IS_FINAL (cmethod) &&
9654 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9655 fsig->generic_param_count &&
9656 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
9658 MonoInst *this_temp, *this_arg_temp, *store;
9659 MonoInst *iargs [4];
9661 g_assert (fsig->is_inflated);
9663 /* Prevent inlining of methods that contain indirect calls */
9664 INLINE_FAILURE ("virtual generic call");
9666 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9667 GSHAREDVT_FAILURE (*ip);
9669 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
9670 g_assert (!imt_arg);
9672 g_assert (cmethod->is_inflated);
9673 imt_arg = emit_get_rgctx_method (cfg, context_used,
9674 cmethod, MONO_RGCTX_INFO_METHOD);
9675 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9677 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9678 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9679 MONO_ADD_INS (cfg->cbb, store);
9681 /* FIXME: This should be a managed pointer */
9682 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9684 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9685 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9686 cmethod, MONO_RGCTX_INFO_METHOD);
9687 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9688 addr = mono_emit_jit_icall (cfg,
9689 mono_helper_compile_generic_method, iargs);
9691 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9693 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9700 * Implement a workaround for the inherent races involved in locking:
9706 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9707 * try block, the Exit () won't be executed, see:
9708 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9709 * To work around this, we extend such try blocks to include the last x bytes
9710 * of the Monitor.Enter () call.
9712 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9713 MonoBasicBlock *tbb;
9715 GET_BBLOCK (cfg, tbb, ip + 5);
9717 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9718 * from Monitor.Enter like ArgumentNullException.
9720 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9721 /* Mark this bblock as needing to be extended */
9722 tbb->extend_try_block = TRUE;
9726 /* Conversion to a JIT intrinsic */
9727 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9728 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9729 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9737 if ((cfg->opt & MONO_OPT_INLINE) &&
9738 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9739 mono_method_check_inlining (cfg, cmethod)) {
9741 gboolean always = FALSE;
9743 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9744 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9745 /* Prevent inlining of methods that call wrappers */
9746 INLINE_FAILURE ("wrapper call");
9747 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9751 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9753 cfg->real_offset += 5;
9755 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9756 /* *sp is already set by inline_method */
9761 inline_costs += costs;
9767 /* Tail recursion elimination */
9768 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9769 gboolean has_vtargs = FALSE;
9772 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9773 INLINE_FAILURE ("tail call");
9775 /* keep it simple */
9776 for (i = fsig->param_count - 1; i >= 0; i--) {
9777 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9782 if (need_seq_point) {
9783 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9784 need_seq_point = FALSE;
9786 for (i = 0; i < n; ++i)
9787 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9788 MONO_INST_NEW (cfg, ins, OP_BR);
9789 MONO_ADD_INS (cfg->cbb, ins);
9790 tblock = start_bblock->out_bb [0];
9791 link_bblock (cfg, cfg->cbb, tblock);
9792 ins->inst_target_bb = tblock;
9793 start_new_bblock = 1;
9795 /* skip the CEE_RET, too */
9796 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9803 inline_costs += 10 * num_calls++;
9806 * Synchronized wrappers.
9807 * Its hard to determine where to replace a method with its synchronized
9808 * wrapper without causing an infinite recursion. The current solution is
9809 * to add the synchronized wrapper in the trampolines, and to
9810 * change the called method to a dummy wrapper, and resolve that wrapper
9811 * to the real method in mono_jit_compile_method ().
9813 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9814 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9815 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9816 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9820 * Making generic calls out of gsharedvt methods.
9821 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9822 * patching gshared method addresses into a gsharedvt method.
9824 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
9825 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9826 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9827 MonoRgctxInfoType info_type;
9830 //if (mono_class_is_interface (cmethod->klass))
9831 //GSHAREDVT_FAILURE (*ip);
9832 // disable for possible remoting calls
9833 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9834 GSHAREDVT_FAILURE (*ip);
9835 if (fsig->generic_param_count) {
9836 /* virtual generic call */
9837 g_assert (!imt_arg);
9838 /* Same as the virtual generic case above */
9839 imt_arg = emit_get_rgctx_method (cfg, context_used,
9840 cmethod, MONO_RGCTX_INFO_METHOD);
9841 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9843 } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
9844 /* This can happen when we call a fully instantiated iface method */
9845 imt_arg = emit_get_rgctx_method (cfg, context_used,
9846 cmethod, MONO_RGCTX_INFO_METHOD);
9851 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9852 keep_this_alive = sp [0];
9854 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9855 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9857 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9858 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9860 if (cfg->llvm_only) {
9861 // FIXME: Avoid initializing vtable_arg
9862 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9864 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9869 /* Generic sharing */
9872 * Use this if the callee is gsharedvt sharable too, since
9873 * at runtime we might find an instantiation so the call cannot
9874 * be patched (the 'no_patch' code path in mini-trampolines.c).
9876 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9877 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9878 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9879 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9880 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9881 INLINE_FAILURE ("gshared");
9883 g_assert (cfg->gshared && cmethod);
9887 * We are compiling a call to a
9888 * generic method from shared code,
9889 * which means that we have to look up
9890 * the method in the rgctx and do an
9894 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9896 if (cfg->llvm_only) {
9897 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9898 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9900 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9901 // FIXME: Avoid initializing imt_arg/vtable_arg
9902 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9904 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9905 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9910 /* Direct calls to icalls */
9912 MonoMethod *wrapper;
9915 /* Inline the wrapper */
9916 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9918 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9919 g_assert (costs > 0);
9920 cfg->real_offset += 5;
9922 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9923 /* *sp is already set by inline_method */
9928 inline_costs += costs;
9937 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9938 MonoInst *val = sp [fsig->param_count];
9940 if (val->type == STACK_OBJ) {
9941 MonoInst *iargs [2];
9946 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9949 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9950 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9951 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
9952 emit_write_barrier (cfg, addr, val);
9953 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9954 GSHAREDVT_FAILURE (*ip);
9955 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9956 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9958 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9959 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9960 if (!cmethod->klass->element_class->valuetype && !readonly)
9961 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9962 CHECK_TYPELOAD (cmethod->klass);
9965 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9968 g_assert_not_reached ();
9975 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9979 /* Tail prefix / tail call optimization */
9981 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9982 /* FIXME: runtime generic context pointer for jumps? */
9983 /* FIXME: handle this for generic sharing eventually */
9984 if ((ins_flag & MONO_INST_TAILCALL) &&
9985 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9986 supported_tail_call = TRUE;
9988 if (supported_tail_call) {
9991 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9992 INLINE_FAILURE ("tail call");
9994 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9996 if (cfg->backend->have_op_tail_call) {
9997 /* Handle tail calls similarly to normal calls */
10000 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10002 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
10003 call->tail_call = TRUE;
10004 call->method = cmethod;
10005 call->signature = mono_method_signature (cmethod);
10008 * We implement tail calls by storing the actual arguments into the
10009 * argument variables, then emitting a CEE_JMP.
10011 for (i = 0; i < n; ++i) {
10012 /* Prevent argument from being register allocated */
10013 arg_array [i]->flags |= MONO_INST_VOLATILE;
10014 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
10016 ins = (MonoInst*)call;
10017 ins->inst_p0 = cmethod;
10018 ins->inst_p1 = arg_array [0];
10019 MONO_ADD_INS (cfg->cbb, ins);
10020 link_bblock (cfg, cfg->cbb, end_bblock);
10021 start_new_bblock = 1;
10023 // FIXME: Eliminate unreachable epilogs
10026 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
10027 * only reachable from this call.
10029 GET_BBLOCK (cfg, tblock, ip + 5);
10030 if (tblock == cfg->cbb || tblock->in_count == 0)
10039 * Virtual calls in llvm-only mode.
10041 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
10042 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
10047 if (!(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
10048 INLINE_FAILURE ("call");
10049 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
10050 imt_arg, vtable_arg);
10052 if (tail_call && !cfg->llvm_only) {
10053 link_bblock (cfg, cfg->cbb, end_bblock);
10054 start_new_bblock = 1;
10056 // FIXME: Eliminate unreachable epilogs
10059 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
10060 * only reachable from this call.
10062 GET_BBLOCK (cfg, tblock, ip + 5);
10063 if (tblock == cfg->cbb || tblock->in_count == 0)
10070 /* End of call, INS should contain the result of the call, if any */
10072 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
10075 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
10080 if (keep_this_alive) {
10081 MonoInst *dummy_use;
10083 /* See mono_emit_method_call_full () */
10084 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
10087 if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
10089 * Clang can convert these calls to tail calls which screw up the stack
10090 * walk. This happens even when the -fno-optimize-sibling-calls
10091 * option is passed to clang.
10092 * Work around this by emitting a dummy call.
10094 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
10097 CHECK_CFG_EXCEPTION;
10101 g_assert (*ip == CEE_RET);
10105 constrained_class = NULL;
10106 if (need_seq_point)
10107 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10111 if (cfg->method != method) {
10112 /* return from inlined method */
10114 * If in_count == 0, that means the ret is unreachable due to
10115 * being preceeded by a throw. In that case, inline_method () will
10116 * handle setting the return value
10117 * (test case: test_0_inline_throw ()).
10119 if (return_var && cfg->cbb->in_count) {
10120 MonoType *ret_type = mono_method_signature (method)->ret;
10126 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10129 //g_assert (returnvar != -1);
10130 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
10131 cfg->ret_var_set = TRUE;
10134 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10136 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
10137 emit_pop_lmf (cfg);
10140 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
10142 if (seq_points && !sym_seq_points) {
10144 * Place a seq point here too even through the IL stack is not
10145 * empty, so a step over on
10148 * will work correctly.
10150 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
10151 MONO_ADD_INS (cfg->cbb, ins);
10154 g_assert (!return_var);
10158 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10161 emit_setret (cfg, *sp);
10164 if (sp != stack_start)
10166 MONO_INST_NEW (cfg, ins, OP_BR);
10168 ins->inst_target_bb = end_bblock;
10169 MONO_ADD_INS (cfg->cbb, ins);
10170 link_bblock (cfg, cfg->cbb, end_bblock);
10171 start_new_bblock = 1;
10175 MONO_INST_NEW (cfg, ins, OP_BR);
10177 target = ip + 1 + (signed char)(*ip);
10179 GET_BBLOCK (cfg, tblock, target);
10180 link_bblock (cfg, cfg->cbb, tblock);
10181 ins->inst_target_bb = tblock;
10182 if (sp != stack_start) {
10183 handle_stack_args (cfg, stack_start, sp - stack_start);
10185 CHECK_UNVERIFIABLE (cfg);
10187 MONO_ADD_INS (cfg->cbb, ins);
10188 start_new_bblock = 1;
10189 inline_costs += BRANCH_COST;
10203 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
10205 target = ip + 1 + *(signed char*)ip;
10208 ADD_BINCOND (NULL);
10211 inline_costs += BRANCH_COST;
10215 MONO_INST_NEW (cfg, ins, OP_BR);
10218 target = ip + 4 + (gint32)read32(ip);
10220 GET_BBLOCK (cfg, tblock, target);
10221 link_bblock (cfg, cfg->cbb, tblock);
10222 ins->inst_target_bb = tblock;
10223 if (sp != stack_start) {
10224 handle_stack_args (cfg, stack_start, sp - stack_start);
10226 CHECK_UNVERIFIABLE (cfg);
10229 MONO_ADD_INS (cfg->cbb, ins);
10231 start_new_bblock = 1;
10232 inline_costs += BRANCH_COST;
10234 case CEE_BRFALSE_S:
10239 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
10240 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
10241 guint32 opsize = is_short ? 1 : 4;
10243 CHECK_OPSIZE (opsize);
10245 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
10248 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
10253 GET_BBLOCK (cfg, tblock, target);
10254 link_bblock (cfg, cfg->cbb, tblock);
10255 GET_BBLOCK (cfg, tblock, ip);
10256 link_bblock (cfg, cfg->cbb, tblock);
10258 if (sp != stack_start) {
10259 handle_stack_args (cfg, stack_start, sp - stack_start);
10260 CHECK_UNVERIFIABLE (cfg);
10263 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
10264 cmp->sreg1 = sp [0]->dreg;
10265 type_from_op (cfg, cmp, sp [0], NULL);
10268 #if SIZEOF_REGISTER == 4
10269 if (cmp->opcode == OP_LCOMPARE_IMM) {
10270 /* Convert it to OP_LCOMPARE */
10271 MONO_INST_NEW (cfg, ins, OP_I8CONST);
10272 ins->type = STACK_I8;
10273 ins->dreg = alloc_dreg (cfg, STACK_I8);
10275 MONO_ADD_INS (cfg->cbb, ins);
10276 cmp->opcode = OP_LCOMPARE;
10277 cmp->sreg2 = ins->dreg;
10280 MONO_ADD_INS (cfg->cbb, cmp);
10282 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
10283 type_from_op (cfg, ins, sp [0], NULL);
10284 MONO_ADD_INS (cfg->cbb, ins);
10285 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
10286 GET_BBLOCK (cfg, tblock, target);
10287 ins->inst_true_bb = tblock;
10288 GET_BBLOCK (cfg, tblock, ip);
10289 ins->inst_false_bb = tblock;
10290 start_new_bblock = 2;
10293 inline_costs += BRANCH_COST;
10308 MONO_INST_NEW (cfg, ins, *ip);
10310 target = ip + 4 + (gint32)read32(ip);
10313 ADD_BINCOND (NULL);
10316 inline_costs += BRANCH_COST;
10320 MonoBasicBlock **targets;
10321 MonoBasicBlock *default_bblock;
10322 MonoJumpInfoBBTable *table;
10323 int offset_reg = alloc_preg (cfg);
10324 int target_reg = alloc_preg (cfg);
10325 int table_reg = alloc_preg (cfg);
10326 int sum_reg = alloc_preg (cfg);
10327 gboolean use_op_switch;
10331 n = read32 (ip + 1);
10334 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
10338 CHECK_OPSIZE (n * sizeof (guint32));
10339 target = ip + n * sizeof (guint32);
10341 GET_BBLOCK (cfg, default_bblock, target);
10342 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
10344 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
10345 for (i = 0; i < n; ++i) {
10346 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
10347 targets [i] = tblock;
10348 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
10352 if (sp != stack_start) {
10354 * Link the current bb with the targets as well, so handle_stack_args
10355 * will set their in_stack correctly.
10357 link_bblock (cfg, cfg->cbb, default_bblock);
10358 for (i = 0; i < n; ++i)
10359 link_bblock (cfg, cfg->cbb, targets [i]);
10361 handle_stack_args (cfg, stack_start, sp - stack_start);
10363 CHECK_UNVERIFIABLE (cfg);
10365 /* Undo the links */
10366 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
10367 for (i = 0; i < n; ++i)
10368 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
10371 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
10372 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
10374 for (i = 0; i < n; ++i)
10375 link_bblock (cfg, cfg->cbb, targets [i]);
10377 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
10378 table->table = targets;
10379 table->table_size = n;
10381 use_op_switch = FALSE;
10383 /* ARM implements SWITCH statements differently */
10384 /* FIXME: Make it use the generic implementation */
10385 if (!cfg->compile_aot)
10386 use_op_switch = TRUE;
10389 if (COMPILE_LLVM (cfg))
10390 use_op_switch = TRUE;
10392 cfg->cbb->has_jump_table = 1;
10394 if (use_op_switch) {
10395 MONO_INST_NEW (cfg, ins, OP_SWITCH);
10396 ins->sreg1 = src1->dreg;
10397 ins->inst_p0 = table;
10398 ins->inst_many_bb = targets;
10399 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
10400 MONO_ADD_INS (cfg->cbb, ins);
10402 if (sizeof (gpointer) == 8)
10403 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
10405 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
10407 #if SIZEOF_REGISTER == 8
10408 /* The upper word might not be zero, and we add it to a 64 bit address later */
10409 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
10412 if (cfg->compile_aot) {
10413 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
10415 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
10416 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
10417 ins->inst_p0 = table;
10418 ins->dreg = table_reg;
10419 MONO_ADD_INS (cfg->cbb, ins);
10422 /* FIXME: Use load_memindex */
10423 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
10424 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
10425 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
10427 start_new_bblock = 1;
10428 inline_costs += (BRANCH_COST * 2);
10441 case CEE_LDIND_REF:
10448 dreg = alloc_freg (cfg);
10451 dreg = alloc_lreg (cfg);
10453 case CEE_LDIND_REF:
10454 dreg = alloc_ireg_ref (cfg);
10457 dreg = alloc_preg (cfg);
10460 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
10461 ins->type = ldind_type [*ip - CEE_LDIND_I1];
10462 if (*ip == CEE_LDIND_R4)
10463 ins->type = cfg->r4_stack_type;
10464 ins->flags |= ins_flag;
10465 MONO_ADD_INS (cfg->cbb, ins);
10467 if (ins_flag & MONO_INST_VOLATILE) {
10468 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10469 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10474 case CEE_STIND_REF:
10485 if (ins_flag & MONO_INST_VOLATILE) {
10486 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10487 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10490 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
10491 ins->flags |= ins_flag;
10494 MONO_ADD_INS (cfg->cbb, ins);
10496 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
10497 emit_write_barrier (cfg, sp [0], sp [1]);
10506 MONO_INST_NEW (cfg, ins, (*ip));
10508 ins->sreg1 = sp [0]->dreg;
10509 ins->sreg2 = sp [1]->dreg;
10510 type_from_op (cfg, ins, sp [0], sp [1]);
10512 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10514 /* Use the immediate opcodes if possible */
10515 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
10516 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10517 if (imm_opcode != -1) {
10518 ins->opcode = imm_opcode;
10519 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
10522 NULLIFY_INS (sp [1]);
10526 MONO_ADD_INS ((cfg)->cbb, (ins));
10528 *sp++ = mono_decompose_opcode (cfg, ins);
10545 MONO_INST_NEW (cfg, ins, (*ip));
10547 ins->sreg1 = sp [0]->dreg;
10548 ins->sreg2 = sp [1]->dreg;
10549 type_from_op (cfg, ins, sp [0], sp [1]);
10551 add_widen_op (cfg, ins, &sp [0], &sp [1]);
10552 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10554 /* FIXME: Pass opcode to is_inst_imm */
10556 /* Use the immediate opcodes if possible */
10557 if (((sp [1]->opcode == OP_ICONST) || (sp [1]->opcode == OP_I8CONST)) && mono_arch_is_inst_imm (sp [1]->opcode == OP_ICONST ? sp [1]->inst_c0 : sp [1]->inst_l)) {
10558 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10559 if (imm_opcode != -1) {
10560 ins->opcode = imm_opcode;
10561 if (sp [1]->opcode == OP_I8CONST) {
10562 #if SIZEOF_REGISTER == 8
10563 ins->inst_imm = sp [1]->inst_l;
10565 ins->inst_ls_word = sp [1]->inst_ls_word;
10566 ins->inst_ms_word = sp [1]->inst_ms_word;
10570 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10573 /* Might be followed by an instruction added by add_widen_op */
10574 if (sp [1]->next == NULL)
10575 NULLIFY_INS (sp [1]);
10578 MONO_ADD_INS ((cfg)->cbb, (ins));
10580 *sp++ = mono_decompose_opcode (cfg, ins);
10593 case CEE_CONV_OVF_I8:
10594 case CEE_CONV_OVF_U8:
10595 case CEE_CONV_R_UN:
10598 /* Special case this earlier so we have long constants in the IR */
10599 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10600 int data = sp [-1]->inst_c0;
10601 sp [-1]->opcode = OP_I8CONST;
10602 sp [-1]->type = STACK_I8;
10603 #if SIZEOF_REGISTER == 8
10604 if ((*ip) == CEE_CONV_U8)
10605 sp [-1]->inst_c0 = (guint32)data;
10607 sp [-1]->inst_c0 = data;
10609 sp [-1]->inst_ls_word = data;
10610 if ((*ip) == CEE_CONV_U8)
10611 sp [-1]->inst_ms_word = 0;
10613 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10615 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10622 case CEE_CONV_OVF_I4:
10623 case CEE_CONV_OVF_I1:
10624 case CEE_CONV_OVF_I2:
10625 case CEE_CONV_OVF_I:
10626 case CEE_CONV_OVF_U:
10629 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10630 ADD_UNOP (CEE_CONV_OVF_I8);
10637 case CEE_CONV_OVF_U1:
10638 case CEE_CONV_OVF_U2:
10639 case CEE_CONV_OVF_U4:
10642 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10643 ADD_UNOP (CEE_CONV_OVF_U8);
10650 case CEE_CONV_OVF_I1_UN:
10651 case CEE_CONV_OVF_I2_UN:
10652 case CEE_CONV_OVF_I4_UN:
10653 case CEE_CONV_OVF_I8_UN:
10654 case CEE_CONV_OVF_U1_UN:
10655 case CEE_CONV_OVF_U2_UN:
10656 case CEE_CONV_OVF_U4_UN:
10657 case CEE_CONV_OVF_U8_UN:
10658 case CEE_CONV_OVF_I_UN:
10659 case CEE_CONV_OVF_U_UN:
10666 CHECK_CFG_EXCEPTION;
10670 case CEE_ADD_OVF_UN:
10672 case CEE_MUL_OVF_UN:
10674 case CEE_SUB_OVF_UN:
10680 GSHAREDVT_FAILURE (*ip);
10683 token = read32 (ip + 1);
10684 klass = mini_get_class (method, token, generic_context);
10685 CHECK_TYPELOAD (klass);
10687 if (generic_class_is_reference_type (cfg, klass)) {
10688 MonoInst *store, *load;
10689 int dreg = alloc_ireg_ref (cfg);
10691 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10692 load->flags |= ins_flag;
10693 MONO_ADD_INS (cfg->cbb, load);
10695 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10696 store->flags |= ins_flag;
10697 MONO_ADD_INS (cfg->cbb, store);
10699 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10700 emit_write_barrier (cfg, sp [0], sp [1]);
10702 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10708 int loc_index = -1;
10714 token = read32 (ip + 1);
10715 klass = mini_get_class (method, token, generic_context);
10716 CHECK_TYPELOAD (klass);
10718 /* Optimize the common ldobj+stloc combination */
10721 loc_index = ip [6];
10728 loc_index = ip [5] - CEE_STLOC_0;
10735 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10736 CHECK_LOCAL (loc_index);
10738 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10739 ins->dreg = cfg->locals [loc_index]->dreg;
10740 ins->flags |= ins_flag;
10743 if (ins_flag & MONO_INST_VOLATILE) {
10744 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10745 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10751 /* Optimize the ldobj+stobj combination */
10752 /* The reference case ends up being a load+store anyway */
10753 /* Skip this if the operation is volatile. */
10754 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass) && !(ins_flag & MONO_INST_VOLATILE)) {
10759 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10766 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10767 ins->flags |= ins_flag;
10770 if (ins_flag & MONO_INST_VOLATILE) {
10771 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10772 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10781 CHECK_STACK_OVF (1);
10783 n = read32 (ip + 1);
10785 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10786 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10787 ins->type = STACK_OBJ;
10790 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10791 MonoInst *iargs [1];
10792 char *str = (char *)mono_method_get_wrapper_data (method, n);
10794 if (cfg->compile_aot)
10795 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10797 EMIT_NEW_PCONST (cfg, iargs [0], str);
10798 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10800 if (cfg->opt & MONO_OPT_SHARED) {
10801 MonoInst *iargs [3];
10803 if (cfg->compile_aot) {
10804 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10806 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10807 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10808 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10809 *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
10810 mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10813 if (cfg->cbb->out_of_line) {
10814 MonoInst *iargs [2];
10816 if (image == mono_defaults.corlib) {
10818 * Avoid relocations in AOT and save some space by using a
10819 * version of helper_ldstr specialized to mscorlib.
10821 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10822 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10824 /* Avoid creating the string object */
10825 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10826 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10827 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10831 if (cfg->compile_aot) {
10832 NEW_LDSTRCONST (cfg, ins, image, n);
10834 MONO_ADD_INS (cfg->cbb, ins);
10837 NEW_PCONST (cfg, ins, NULL);
10838 ins->type = STACK_OBJ;
10839 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10843 OUT_OF_MEMORY_FAILURE;
10846 MONO_ADD_INS (cfg->cbb, ins);
10855 MonoInst *iargs [2];
10856 MonoMethodSignature *fsig;
10859 MonoInst *vtable_arg = NULL;
10862 token = read32 (ip + 1);
10863 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10866 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10869 mono_save_token_info (cfg, image, token, cmethod);
10871 if (!mono_class_init (cmethod->klass))
10872 TYPE_LOAD_ERROR (cmethod->klass);
10874 context_used = mini_method_check_context_used (cfg, cmethod);
10876 if (mono_security_core_clr_enabled ())
10877 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10879 if (cfg->gshared && cmethod && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
10880 emit_class_init (cfg, cmethod->klass);
10881 CHECK_TYPELOAD (cmethod->klass);
10885 if (cfg->gsharedvt) {
10886 if (mini_is_gsharedvt_variable_signature (sig))
10887 GSHAREDVT_FAILURE (*ip);
10891 n = fsig->param_count;
10895 * Generate smaller code for the common newobj <exception> instruction in
10896 * argument checking code.
10898 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10899 is_exception_class (cmethod->klass) && n <= 2 &&
10900 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10901 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10902 MonoInst *iargs [3];
10906 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10909 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10912 iargs [1] = sp [0];
10913 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10916 iargs [1] = sp [0];
10917 iargs [2] = sp [1];
10918 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10921 g_assert_not_reached ();
10929 /* move the args to allow room for 'this' in the first position */
10935 /* check_call_signature () requires sp[0] to be set */
10936 this_ins.type = STACK_OBJ;
10937 sp [0] = &this_ins;
10938 if (check_call_signature (cfg, fsig, sp))
10943 if (mini_class_is_system_array (cmethod->klass)) {
10944 *sp = emit_get_rgctx_method (cfg, context_used,
10945 cmethod, MONO_RGCTX_INFO_METHOD);
10947 /* Avoid varargs in the common case */
10948 if (fsig->param_count == 1)
10949 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10950 else if (fsig->param_count == 2)
10951 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10952 else if (fsig->param_count == 3)
10953 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10954 else if (fsig->param_count == 4)
10955 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10957 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10958 } else if (cmethod->string_ctor) {
10959 g_assert (!context_used);
10960 g_assert (!vtable_arg);
10961 /* we simply pass a null pointer */
10962 EMIT_NEW_PCONST (cfg, *sp, NULL);
10963 /* now call the string ctor */
10964 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10966 if (cmethod->klass->valuetype) {
10967 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10968 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10969 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10974 * The code generated by mini_emit_virtual_call () expects
10975 * iargs [0] to be a boxed instance, but luckily the vcall
10976 * will be transformed into a normal call there.
10978 } else if (context_used) {
10979 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10982 MonoVTable *vtable = NULL;
10984 if (!cfg->compile_aot)
10985 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10986 CHECK_TYPELOAD (cmethod->klass);
10989 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10990 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10991 * As a workaround, we call class cctors before allocating objects.
10993 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10994 emit_class_init (cfg, cmethod->klass);
10995 if (cfg->verbose_level > 2)
10996 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10997 class_inits = g_slist_prepend (class_inits, cmethod->klass);
11000 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
11003 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
11006 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
11008 /* Now call the actual ctor */
11009 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
11010 CHECK_CFG_EXCEPTION;
11013 if (alloc == NULL) {
11015 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
11016 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
11024 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
11025 emit_seq_point (cfg, method, ip, FALSE, TRUE);
11028 case CEE_CASTCLASS:
11033 token = read32 (ip + 1);
11034 klass = mini_get_class (method, token, generic_context);
11035 CHECK_TYPELOAD (klass);
11036 if (sp [0]->type != STACK_OBJ)
11039 MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
11040 ins->dreg = alloc_preg (cfg);
11041 ins->sreg1 = (*sp)->dreg;
11042 ins->klass = klass;
11043 ins->type = STACK_OBJ;
11044 MONO_ADD_INS (cfg->cbb, ins);
11046 CHECK_CFG_EXCEPTION;
11050 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
11053 case CEE_UNBOX_ANY: {
11054 MonoInst *res, *addr;
11059 token = read32 (ip + 1);
11060 klass = mini_get_class (method, token, generic_context);
11061 CHECK_TYPELOAD (klass);
11063 mono_save_token_info (cfg, image, token, klass);
11065 context_used = mini_class_check_context_used (cfg, klass);
11067 if (mini_is_gsharedvt_klass (klass)) {
11068 res = handle_unbox_gsharedvt (cfg, klass, *sp);
11070 } else if (generic_class_is_reference_type (cfg, klass)) {
11071 if (MONO_INS_IS_PCONST_NULL (*sp)) {
11072 EMIT_NEW_PCONST (cfg, res, NULL);
11073 res->type = STACK_OBJ;
11075 MONO_INST_NEW (cfg, res, OP_CASTCLASS);
11076 res->dreg = alloc_preg (cfg);
11077 res->sreg1 = (*sp)->dreg;
11078 res->klass = klass;
11079 res->type = STACK_OBJ;
11080 MONO_ADD_INS (cfg->cbb, res);
11081 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
11083 } else if (mono_class_is_nullable (klass)) {
11084 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
11086 addr = handle_unbox (cfg, klass, sp, context_used);
11088 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11099 MonoClass *enum_class;
11100 MonoMethod *has_flag;
11106 token = read32 (ip + 1);
11107 klass = mini_get_class (method, token, generic_context);
11108 CHECK_TYPELOAD (klass);
11110 mono_save_token_info (cfg, image, token, klass);
11112 context_used = mini_class_check_context_used (cfg, klass);
11114 if (generic_class_is_reference_type (cfg, klass)) {
11120 if (klass == mono_defaults.void_class)
11122 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
11124 /* frequent check in generic code: box (struct), brtrue */
11129 * <push int/long ptr>
11132 * constrained. MyFlags
11133 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
11135 * If we find this sequence and the operand types on box and constrained
11136 * are equal, we can emit a specialized instruction sequence instead of
11137 * the very slow HasFlag () call.
11139 if ((cfg->opt & MONO_OPT_INTRINS) &&
11140 /* Cheap checks first. */
11141 ip + 5 + 6 + 5 < end &&
11142 ip [5] == CEE_PREFIX1 &&
11143 ip [6] == CEE_CONSTRAINED_ &&
11144 ip [11] == CEE_CALLVIRT &&
11145 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
11146 mono_class_is_enum (klass) &&
11147 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
11148 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
11149 has_flag->klass == mono_defaults.enum_class &&
11150 !strcmp (has_flag->name, "HasFlag") &&
11151 has_flag->signature->hasthis &&
11152 has_flag->signature->param_count == 1) {
11153 CHECK_TYPELOAD (enum_class);
11155 if (enum_class == klass) {
11156 MonoInst *enum_this, *enum_flag;
11161 enum_this = sp [0];
11162 enum_flag = sp [1];
11164 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
11169 // FIXME: LLVM can't handle the inconsistent bb linking
11170 if (!mono_class_is_nullable (klass) &&
11171 !mini_is_gsharedvt_klass (klass) &&
11172 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11173 (ip [5] == CEE_BRTRUE ||
11174 ip [5] == CEE_BRTRUE_S ||
11175 ip [5] == CEE_BRFALSE ||
11176 ip [5] == CEE_BRFALSE_S)) {
11177 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
11179 MonoBasicBlock *true_bb, *false_bb;
11183 if (cfg->verbose_level > 3) {
11184 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
11185 printf ("<box+brtrue opt>\n");
11190 case CEE_BRFALSE_S:
11193 target = ip + 1 + (signed char)(*ip);
11200 target = ip + 4 + (gint)(read32 (ip));
11204 g_assert_not_reached ();
11208 * We need to link both bblocks, since it is needed for handling stack
11209 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
11210 * Branching to only one of them would lead to inconsistencies, so
11211 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
11213 GET_BBLOCK (cfg, true_bb, target);
11214 GET_BBLOCK (cfg, false_bb, ip);
11216 mono_link_bblock (cfg, cfg->cbb, true_bb);
11217 mono_link_bblock (cfg, cfg->cbb, false_bb);
11219 if (sp != stack_start) {
11220 handle_stack_args (cfg, stack_start, sp - stack_start);
11222 CHECK_UNVERIFIABLE (cfg);
11225 if (COMPILE_LLVM (cfg)) {
11226 dreg = alloc_ireg (cfg);
11227 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
11228 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
11230 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
11232 /* The JIT can't eliminate the iconst+compare */
11233 MONO_INST_NEW (cfg, ins, OP_BR);
11234 ins->inst_target_bb = is_true ? true_bb : false_bb;
11235 MONO_ADD_INS (cfg->cbb, ins);
11238 start_new_bblock = 1;
11242 *sp++ = handle_box (cfg, val, klass, context_used);
11244 CHECK_CFG_EXCEPTION;
11253 token = read32 (ip + 1);
11254 klass = mini_get_class (method, token, generic_context);
11255 CHECK_TYPELOAD (klass);
11257 mono_save_token_info (cfg, image, token, klass);
11259 context_used = mini_class_check_context_used (cfg, klass);
11261 if (mono_class_is_nullable (klass)) {
11264 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
11265 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
11269 ins = handle_unbox (cfg, klass, sp, context_used);
11282 MonoClassField *field;
11283 #ifndef DISABLE_REMOTING
11287 gboolean is_instance;
11289 gpointer addr = NULL;
11290 gboolean is_special_static;
11292 MonoInst *store_val = NULL;
11293 MonoInst *thread_ins;
11296 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
11298 if (op == CEE_STFLD) {
11301 store_val = sp [1];
11306 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
11308 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
11311 if (op == CEE_STSFLD) {
11314 store_val = sp [0];
11319 token = read32 (ip + 1);
11320 if (method->wrapper_type != MONO_WRAPPER_NONE) {
11321 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
11322 klass = field->parent;
11325 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
11328 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
11329 FIELD_ACCESS_FAILURE (method, field);
11330 mono_class_init (klass);
11332 /* if the class is Critical then transparent code cannot access it's fields */
11333 if (!is_instance && mono_security_core_clr_enabled ())
11334 ensure_method_is_allowed_to_access_field (cfg, method, field);
11336 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
11337 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
11338 if (mono_security_core_clr_enabled ())
11339 ensure_method_is_allowed_to_access_field (cfg, method, field);
11342 ftype = mono_field_get_type (field);
11345 * LDFLD etc. is usable on static fields as well, so convert those cases to
11348 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
11360 g_assert_not_reached ();
11362 is_instance = FALSE;
11365 context_used = mini_class_check_context_used (cfg, klass);
11367 /* INSTANCE CASE */
11369 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
11370 if (op == CEE_STFLD) {
11371 if (target_type_is_incompatible (cfg, field->type, sp [1]))
11373 #ifndef DISABLE_REMOTING
11374 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
11375 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
11376 MonoInst *iargs [5];
11378 GSHAREDVT_FAILURE (op);
11380 iargs [0] = sp [0];
11381 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11382 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11383 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
11385 iargs [4] = sp [1];
11387 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11388 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
11389 iargs, ip, cfg->real_offset, TRUE);
11390 CHECK_CFG_EXCEPTION;
11391 g_assert (costs > 0);
11393 cfg->real_offset += 5;
11395 inline_costs += costs;
11397 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
11402 MonoInst *store, *wbarrier_ptr_ins = NULL;
11404 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11406 if (ins_flag & MONO_INST_VOLATILE) {
11407 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11408 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11411 if (mini_is_gsharedvt_klass (klass)) {
11412 MonoInst *offset_ins;
11414 context_used = mini_class_check_context_used (cfg, klass);
11416 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11417 /* The value is offset by 1 */
11418 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11419 dreg = alloc_ireg_mp (cfg);
11420 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11421 wbarrier_ptr_ins = ins;
11422 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
11423 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
11425 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
11427 if (sp [0]->opcode != OP_LDADDR)
11428 store->flags |= MONO_INST_FAULT;
11430 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
11431 if (mini_is_gsharedvt_klass (klass)) {
11432 g_assert (wbarrier_ptr_ins);
11433 emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
11435 /* insert call to write barrier */
11439 dreg = alloc_ireg_mp (cfg);
11440 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11441 emit_write_barrier (cfg, ptr, sp [1]);
11445 store->flags |= ins_flag;
11452 #ifndef DISABLE_REMOTING
11453 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
11454 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
11455 MonoInst *iargs [4];
11457 GSHAREDVT_FAILURE (op);
11459 iargs [0] = sp [0];
11460 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11461 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11462 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
11463 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11464 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
11465 iargs, ip, cfg->real_offset, TRUE);
11466 CHECK_CFG_EXCEPTION;
11467 g_assert (costs > 0);
11469 cfg->real_offset += 5;
11473 inline_costs += costs;
11475 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
11481 if (sp [0]->type == STACK_VTYPE) {
11484 /* Have to compute the address of the variable */
11486 var = get_vreg_to_inst (cfg, sp [0]->dreg);
11488 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
11490 g_assert (var->klass == klass);
11492 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
11496 if (op == CEE_LDFLDA) {
11497 if (sp [0]->type == STACK_OBJ) {
11498 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
11499 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
11502 dreg = alloc_ireg_mp (cfg);
11504 if (mini_is_gsharedvt_klass (klass)) {
11505 MonoInst *offset_ins;
11507 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11508 /* The value is offset by 1 */
11509 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11510 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11512 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11514 ins->klass = mono_class_from_mono_type (field->type);
11515 ins->type = STACK_MP;
11520 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11522 if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
11523 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
11532 if (mini_is_gsharedvt_klass (klass)) {
11533 MonoInst *offset_ins;
11535 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11536 /* The value is offset by 1 */
11537 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11538 dreg = alloc_ireg_mp (cfg);
11539 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11540 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
11542 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
11544 load->flags |= ins_flag;
11545 if (sp [0]->opcode != OP_LDADDR)
11546 load->flags |= MONO_INST_FAULT;
11558 context_used = mini_class_check_context_used (cfg, klass);
11560 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
11561 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
11565 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11566 * to be called here.
11568 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11569 mono_class_vtable (cfg->domain, klass);
11570 CHECK_TYPELOAD (klass);
11572 mono_domain_lock (cfg->domain);
11573 if (cfg->domain->special_static_fields)
11574 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11575 mono_domain_unlock (cfg->domain);
11577 is_special_static = mono_class_field_is_special_static (field);
11579 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11580 thread_ins = mono_get_thread_intrinsic (cfg);
11584 /* Generate IR to compute the field address */
11585 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11587 * Fast access to TLS data
11588 * Inline version of get_thread_static_data () in
11592 int idx, static_data_reg, array_reg, dreg;
11594 GSHAREDVT_FAILURE (op);
11596 MONO_ADD_INS (cfg->cbb, thread_ins);
11597 static_data_reg = alloc_ireg (cfg);
11598 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11600 if (cfg->compile_aot) {
11601 int offset_reg, offset2_reg, idx_reg;
11603 /* For TLS variables, this will return the TLS offset */
11604 EMIT_NEW_SFLDACONST (cfg, ins, field);
11605 offset_reg = ins->dreg;
11606 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11607 idx_reg = alloc_ireg (cfg);
11608 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11609 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11610 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11611 array_reg = alloc_ireg (cfg);
11612 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11613 offset2_reg = alloc_ireg (cfg);
11614 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11615 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11616 dreg = alloc_ireg (cfg);
11617 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11619 offset = (gsize)addr & 0x7fffffff;
11620 idx = offset & 0x3f;
11622 array_reg = alloc_ireg (cfg);
11623 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11624 dreg = alloc_ireg (cfg);
11625 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11627 } else if ((cfg->opt & MONO_OPT_SHARED) ||
11628 (cfg->compile_aot && is_special_static) ||
11629 (context_used && is_special_static)) {
11630 MonoInst *iargs [2];
11632 g_assert (field->parent);
11633 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11634 if (context_used) {
11635 iargs [1] = emit_get_rgctx_field (cfg, context_used,
11636 field, MONO_RGCTX_INFO_CLASS_FIELD);
11638 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11640 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11641 } else if (context_used) {
11642 MonoInst *static_data;
11645 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11646 method->klass->name_space, method->klass->name, method->name,
11647 depth, field->offset);
11650 if (mono_class_needs_cctor_run (klass, method))
11651 emit_class_init (cfg, klass);
11654 * The pointer we're computing here is
11656 * super_info.static_data + field->offset
11658 static_data = emit_get_rgctx_klass (cfg, context_used,
11659 klass, MONO_RGCTX_INFO_STATIC_DATA);
11661 if (mini_is_gsharedvt_klass (klass)) {
11662 MonoInst *offset_ins;
11664 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11665 /* The value is offset by 1 */
11666 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11667 dreg = alloc_ireg_mp (cfg);
11668 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11669 } else if (field->offset == 0) {
11672 int addr_reg = mono_alloc_preg (cfg);
11673 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11675 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11676 MonoInst *iargs [2];
11678 g_assert (field->parent);
11679 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11680 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11681 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11683 MonoVTable *vtable = NULL;
11685 if (!cfg->compile_aot)
11686 vtable = mono_class_vtable (cfg->domain, klass);
11687 CHECK_TYPELOAD (klass);
11690 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11691 if (!(g_slist_find (class_inits, klass))) {
11692 emit_class_init (cfg, klass);
11693 if (cfg->verbose_level > 2)
11694 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11695 class_inits = g_slist_prepend (class_inits, klass);
11698 if (cfg->run_cctors) {
11699 /* This makes so that inline cannot trigger */
11700 /* .cctors: too many apps depend on them */
11701 /* running with a specific order... */
11703 if (! vtable->initialized)
11704 INLINE_FAILURE ("class init");
11705 if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
11706 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11707 goto exception_exit;
11711 if (cfg->compile_aot)
11712 EMIT_NEW_SFLDACONST (cfg, ins, field);
11715 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11717 EMIT_NEW_PCONST (cfg, ins, addr);
11720 MonoInst *iargs [1];
11721 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11722 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11726 /* Generate IR to do the actual load/store operation */
11728 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11729 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11730 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11733 if (op == CEE_LDSFLDA) {
11734 ins->klass = mono_class_from_mono_type (ftype);
11735 ins->type = STACK_PTR;
11737 } else if (op == CEE_STSFLD) {
11740 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11741 store->flags |= ins_flag;
11743 gboolean is_const = FALSE;
11744 MonoVTable *vtable = NULL;
11745 gpointer addr = NULL;
11747 if (!context_used) {
11748 vtable = mono_class_vtable (cfg->domain, klass);
11749 CHECK_TYPELOAD (klass);
11751 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11752 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11753 int ro_type = ftype->type;
11755 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11756 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11757 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11760 GSHAREDVT_FAILURE (op);
11762 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11765 case MONO_TYPE_BOOLEAN:
11767 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11771 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11774 case MONO_TYPE_CHAR:
11776 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11780 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11785 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11789 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11794 case MONO_TYPE_PTR:
11795 case MONO_TYPE_FNPTR:
11796 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11797 type_to_eval_stack_type ((cfg), field->type, *sp);
11800 case MONO_TYPE_STRING:
11801 case MONO_TYPE_OBJECT:
11802 case MONO_TYPE_CLASS:
11803 case MONO_TYPE_SZARRAY:
11804 case MONO_TYPE_ARRAY:
11805 if (!mono_gc_is_moving ()) {
11806 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11807 type_to_eval_stack_type ((cfg), field->type, *sp);
11815 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11820 case MONO_TYPE_VALUETYPE:
11830 CHECK_STACK_OVF (1);
11832 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11833 load->flags |= ins_flag;
11839 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11840 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11841 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11852 token = read32 (ip + 1);
11853 klass = mini_get_class (method, token, generic_context);
11854 CHECK_TYPELOAD (klass);
11855 if (ins_flag & MONO_INST_VOLATILE) {
11856 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11857 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11859 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11860 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11861 ins->flags |= ins_flag;
11862 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11863 generic_class_is_reference_type (cfg, klass) && !MONO_INS_IS_PCONST_NULL (sp [1])) {
11864 /* insert call to write barrier */
11865 emit_write_barrier (cfg, sp [0], sp [1]);
11877 const char *data_ptr;
11879 guint32 field_token;
11885 token = read32 (ip + 1);
11887 klass = mini_get_class (method, token, generic_context);
11888 CHECK_TYPELOAD (klass);
11890 context_used = mini_class_check_context_used (cfg, klass);
11892 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11893 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11894 ins->sreg1 = sp [0]->dreg;
11895 ins->type = STACK_I4;
11896 ins->dreg = alloc_ireg (cfg);
11897 MONO_ADD_INS (cfg->cbb, ins);
11898 *sp = mono_decompose_opcode (cfg, ins);
11901 if (context_used) {
11902 MonoInst *args [3];
11903 MonoClass *array_class = mono_array_class_get (klass, 1);
11904 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11906 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11909 args [0] = emit_get_rgctx_klass (cfg, context_used,
11910 array_class, MONO_RGCTX_INFO_VTABLE);
11915 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11917 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11919 if (cfg->opt & MONO_OPT_SHARED) {
11920 /* Decompose now to avoid problems with references to the domainvar */
11921 MonoInst *iargs [3];
11923 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11924 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11925 iargs [2] = sp [0];
11927 ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
11929 /* Decompose later since it is needed by abcrem */
11930 MonoClass *array_type = mono_array_class_get (klass, 1);
11931 mono_class_vtable (cfg->domain, array_type);
11932 CHECK_TYPELOAD (array_type);
11934 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11935 ins->dreg = alloc_ireg_ref (cfg);
11936 ins->sreg1 = sp [0]->dreg;
11937 ins->inst_newa_class = klass;
11938 ins->type = STACK_OBJ;
11939 ins->klass = array_type;
11940 MONO_ADD_INS (cfg->cbb, ins);
11941 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11942 cfg->cbb->has_array_access = TRUE;
11944 /* Needed so mono_emit_load_get_addr () gets called */
11945 mono_get_got_var (cfg);
11955 * we inline/optimize the initialization sequence if possible.
11956 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11957 * for small sizes open code the memcpy
11958 * ensure the rva field is big enough
11960 if ((cfg->opt & MONO_OPT_INTRINS) && ip + 6 < end && ip_in_bb (cfg, cfg->cbb, ip + 6) && (len_ins->opcode == OP_ICONST) && (data_ptr = initialize_array_data (method, cfg->compile_aot, ip, klass, len_ins->inst_c0, &data_size, &field_token))) {
11961 MonoMethod *memcpy_method = get_memcpy_method ();
11962 MonoInst *iargs [3];
11963 int add_reg = alloc_ireg_mp (cfg);
11965 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11966 if (cfg->compile_aot) {
11967 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11969 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11971 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11972 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11981 if (sp [0]->type != STACK_OBJ)
11984 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11985 ins->dreg = alloc_preg (cfg);
11986 ins->sreg1 = sp [0]->dreg;
11987 ins->type = STACK_I4;
11988 /* This flag will be inherited by the decomposition */
11989 ins->flags |= MONO_INST_FAULT;
11990 MONO_ADD_INS (cfg->cbb, ins);
11991 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11992 cfg->cbb->has_array_access = TRUE;
12000 if (sp [0]->type != STACK_OBJ)
12003 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12005 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12006 CHECK_TYPELOAD (klass);
12007 /* we need to make sure that this array is exactly the type it needs
12008 * to be for correctness. the wrappers are lax with their usage
12009 * so we need to ignore them here
12011 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
12012 MonoClass *array_class = mono_array_class_get (klass, 1);
12013 mini_emit_check_array_type (cfg, sp [0], array_class);
12014 CHECK_TYPELOAD (array_class);
12018 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12023 case CEE_LDELEM_I1:
12024 case CEE_LDELEM_U1:
12025 case CEE_LDELEM_I2:
12026 case CEE_LDELEM_U2:
12027 case CEE_LDELEM_I4:
12028 case CEE_LDELEM_U4:
12029 case CEE_LDELEM_I8:
12031 case CEE_LDELEM_R4:
12032 case CEE_LDELEM_R8:
12033 case CEE_LDELEM_REF: {
12039 if (*ip == CEE_LDELEM) {
12041 token = read32 (ip + 1);
12042 klass = mini_get_class (method, token, generic_context);
12043 CHECK_TYPELOAD (klass);
12044 mono_class_init (klass);
12047 klass = array_access_to_klass (*ip);
12049 if (sp [0]->type != STACK_OBJ)
12052 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12054 if (mini_is_gsharedvt_variable_klass (klass)) {
12055 // FIXME-VT: OP_ICONST optimization
12056 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12057 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12058 ins->opcode = OP_LOADV_MEMBASE;
12059 } else if (sp [1]->opcode == OP_ICONST) {
12060 int array_reg = sp [0]->dreg;
12061 int index_reg = sp [1]->dreg;
12062 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
12064 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
12065 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
12067 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
12068 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
12070 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12071 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12074 if (*ip == CEE_LDELEM)
12081 case CEE_STELEM_I1:
12082 case CEE_STELEM_I2:
12083 case CEE_STELEM_I4:
12084 case CEE_STELEM_I8:
12085 case CEE_STELEM_R4:
12086 case CEE_STELEM_R8:
12087 case CEE_STELEM_REF:
12092 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12094 if (*ip == CEE_STELEM) {
12096 token = read32 (ip + 1);
12097 klass = mini_get_class (method, token, generic_context);
12098 CHECK_TYPELOAD (klass);
12099 mono_class_init (klass);
12102 klass = array_access_to_klass (*ip);
12104 if (sp [0]->type != STACK_OBJ)
12107 emit_array_store (cfg, klass, sp, TRUE);
12109 if (*ip == CEE_STELEM)
12116 case CEE_CKFINITE: {
12120 if (cfg->llvm_only) {
12121 MonoInst *iargs [1];
12123 iargs [0] = sp [0];
12124 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
12126 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
12127 ins->sreg1 = sp [0]->dreg;
12128 ins->dreg = alloc_freg (cfg);
12129 ins->type = STACK_R8;
12130 MONO_ADD_INS (cfg->cbb, ins);
12132 *sp++ = mono_decompose_opcode (cfg, ins);
12138 case CEE_REFANYVAL: {
12139 MonoInst *src_var, *src;
12141 int klass_reg = alloc_preg (cfg);
12142 int dreg = alloc_preg (cfg);
12144 GSHAREDVT_FAILURE (*ip);
12147 MONO_INST_NEW (cfg, ins, *ip);
12150 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12151 CHECK_TYPELOAD (klass);
12153 context_used = mini_class_check_context_used (cfg, klass);
12156 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12158 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12159 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12160 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
12162 if (context_used) {
12163 MonoInst *klass_ins;
12165 klass_ins = emit_get_rgctx_klass (cfg, context_used,
12166 klass, MONO_RGCTX_INFO_KLASS);
12169 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
12170 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
12172 mini_emit_class_check (cfg, klass_reg, klass);
12174 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
12175 ins->type = STACK_MP;
12176 ins->klass = klass;
12181 case CEE_MKREFANY: {
12182 MonoInst *loc, *addr;
12184 GSHAREDVT_FAILURE (*ip);
12187 MONO_INST_NEW (cfg, ins, *ip);
12190 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12191 CHECK_TYPELOAD (klass);
12193 context_used = mini_class_check_context_used (cfg, klass);
12195 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
12196 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
12198 if (context_used) {
12199 MonoInst *const_ins;
12200 int type_reg = alloc_preg (cfg);
12202 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
12203 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
12204 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12205 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12207 int const_reg = alloc_preg (cfg);
12208 int type_reg = alloc_preg (cfg);
12210 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
12211 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
12212 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12213 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12215 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
12217 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
12218 ins->type = STACK_VTYPE;
12219 ins->klass = mono_defaults.typed_reference_class;
12224 case CEE_LDTOKEN: {
12226 MonoClass *handle_class;
12228 CHECK_STACK_OVF (1);
12231 n = read32 (ip + 1);
12233 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
12234 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
12235 handle = mono_method_get_wrapper_data (method, n);
12236 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
12237 if (handle_class == mono_defaults.typehandle_class)
12238 handle = &((MonoClass*)handle)->byval_arg;
12241 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
12246 mono_class_init (handle_class);
12247 if (cfg->gshared) {
12248 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
12249 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
12250 /* This case handles ldtoken
12251 of an open type, like for
12254 } else if (handle_class == mono_defaults.typehandle_class) {
12255 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
12256 } else if (handle_class == mono_defaults.fieldhandle_class)
12257 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
12258 else if (handle_class == mono_defaults.methodhandle_class)
12259 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
12261 g_assert_not_reached ();
12264 if ((cfg->opt & MONO_OPT_SHARED) &&
12265 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
12266 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
12267 MonoInst *addr, *vtvar, *iargs [3];
12268 int method_context_used;
12270 method_context_used = mini_method_check_context_used (cfg, method);
12272 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12274 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
12275 EMIT_NEW_ICONST (cfg, iargs [1], n);
12276 if (method_context_used) {
12277 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
12278 method, MONO_RGCTX_INFO_METHOD);
12279 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
12281 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
12282 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
12284 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12286 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12288 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12290 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
12291 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
12292 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
12293 (cmethod->klass == mono_defaults.systemtype_class) &&
12294 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
12295 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
12297 mono_class_init (tclass);
12298 if (context_used) {
12299 ins = emit_get_rgctx_klass (cfg, context_used,
12300 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
12301 } else if (cfg->compile_aot) {
12302 if (method->wrapper_type) {
12303 mono_error_init (&error); //got to do it since there are multiple conditionals below
12304 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
12305 /* Special case for static synchronized wrappers */
12306 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
12308 mono_error_cleanup (&error); /* FIXME don't swallow the error */
12309 /* FIXME: n is not a normal token */
12311 EMIT_NEW_PCONST (cfg, ins, NULL);
12314 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
12317 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
12319 EMIT_NEW_PCONST (cfg, ins, rt);
12321 ins->type = STACK_OBJ;
12322 ins->klass = cmethod->klass;
12325 MonoInst *addr, *vtvar;
12327 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12329 if (context_used) {
12330 if (handle_class == mono_defaults.typehandle_class) {
12331 ins = emit_get_rgctx_klass (cfg, context_used,
12332 mono_class_from_mono_type ((MonoType *)handle),
12333 MONO_RGCTX_INFO_TYPE);
12334 } else if (handle_class == mono_defaults.methodhandle_class) {
12335 ins = emit_get_rgctx_method (cfg, context_used,
12336 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
12337 } else if (handle_class == mono_defaults.fieldhandle_class) {
12338 ins = emit_get_rgctx_field (cfg, context_used,
12339 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
12341 g_assert_not_reached ();
12343 } else if (cfg->compile_aot) {
12344 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
12346 EMIT_NEW_PCONST (cfg, ins, handle);
12348 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12349 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12350 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12360 if (sp [-1]->type != STACK_OBJ)
12363 MONO_INST_NEW (cfg, ins, OP_THROW);
12365 ins->sreg1 = sp [0]->dreg;
12367 cfg->cbb->out_of_line = TRUE;
12368 MONO_ADD_INS (cfg->cbb, ins);
12369 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12370 MONO_ADD_INS (cfg->cbb, ins);
12373 link_bblock (cfg, cfg->cbb, end_bblock);
12374 start_new_bblock = 1;
12375 /* This can complicate code generation for llvm since the return value might not be defined */
12376 if (COMPILE_LLVM (cfg))
12377 INLINE_FAILURE ("throw");
12379 case CEE_ENDFINALLY:
12380 if (!ip_in_finally_clause (cfg, ip - header->code))
12382 /* mono_save_seq_point_info () depends on this */
12383 if (sp != stack_start)
12384 emit_seq_point (cfg, method, ip, FALSE, FALSE);
12385 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
12386 MONO_ADD_INS (cfg->cbb, ins);
12388 start_new_bblock = 1;
12391 * Control will leave the method so empty the stack, otherwise
12392 * the next basic block will start with a nonempty stack.
12394 while (sp != stack_start) {
12399 case CEE_LEAVE_S: {
12402 if (*ip == CEE_LEAVE) {
12404 target = ip + 5 + (gint32)read32(ip + 1);
12407 target = ip + 2 + (signed char)(ip [1]);
12410 /* empty the stack */
12411 while (sp != stack_start) {
12416 * If this leave statement is in a catch block, check for a
12417 * pending exception, and rethrow it if necessary.
12418 * We avoid doing this in runtime invoke wrappers, since those are called
12419 * by native code which excepts the wrapper to catch all exceptions.
12421 for (i = 0; i < header->num_clauses; ++i) {
12422 MonoExceptionClause *clause = &header->clauses [i];
12425 * Use <= in the final comparison to handle clauses with multiple
12426 * leave statements, like in bug #78024.
12427 * The ordering of the exception clauses guarantees that we find the
12428 * innermost clause.
12430 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) && (ip - header->code + ((*ip == CEE_LEAVE) ? 5 : 2)) <= (clause->handler_offset + clause->handler_len) && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
12432 MonoBasicBlock *dont_throw;
12437 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
12440 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
12442 NEW_BBLOCK (cfg, dont_throw);
12445 * Currently, we always rethrow the abort exception, despite the
12446 * fact that this is not correct. See thread6.cs for an example.
12447 * But propagating the abort exception is more important than
12448 * getting the sematics right.
12450 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
12451 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
12452 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
12454 MONO_START_BB (cfg, dont_throw);
12459 cfg->cbb->try_end = (intptr_t)(ip - header->code);
12462 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
12464 MonoExceptionClause *clause;
12466 for (tmp = handlers; tmp; tmp = tmp->next) {
12467 clause = (MonoExceptionClause *)tmp->data;
12468 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
12470 link_bblock (cfg, cfg->cbb, tblock);
12471 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
12472 ins->inst_target_bb = tblock;
12473 ins->inst_eh_block = clause;
12474 MONO_ADD_INS (cfg->cbb, ins);
12475 cfg->cbb->has_call_handler = 1;
12476 if (COMPILE_LLVM (cfg)) {
12477 MonoBasicBlock *target_bb;
12480 * Link the finally bblock with the target, since it will
12481 * conceptually branch there.
12483 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
12484 GET_BBLOCK (cfg, target_bb, target);
12485 link_bblock (cfg, tblock, target_bb);
12488 g_list_free (handlers);
12491 MONO_INST_NEW (cfg, ins, OP_BR);
12492 MONO_ADD_INS (cfg->cbb, ins);
12493 GET_BBLOCK (cfg, tblock, target);
12494 link_bblock (cfg, cfg->cbb, tblock);
12495 ins->inst_target_bb = tblock;
12497 start_new_bblock = 1;
12499 if (*ip == CEE_LEAVE)
12508 * Mono specific opcodes
12510 case MONO_CUSTOM_PREFIX: {
12512 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
12516 case CEE_MONO_ICALL: {
12518 MonoJitICallInfo *info;
12520 token = read32 (ip + 2);
12521 func = mono_method_get_wrapper_data (method, token);
12522 info = mono_find_jit_icall_by_addr (func);
12524 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
12527 CHECK_STACK (info->sig->param_count);
12528 sp -= info->sig->param_count;
12530 ins = mono_emit_jit_icall (cfg, info->func, sp);
12531 if (!MONO_TYPE_IS_VOID (info->sig->ret))
12535 inline_costs += 10 * num_calls++;
12539 case CEE_MONO_LDPTR_CARD_TABLE:
12540 case CEE_MONO_LDPTR_NURSERY_START:
12541 case CEE_MONO_LDPTR_NURSERY_BITS:
12542 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
12543 CHECK_STACK_OVF (1);
12546 case CEE_MONO_LDPTR_CARD_TABLE:
12547 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
12549 case CEE_MONO_LDPTR_NURSERY_START:
12550 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
12552 case CEE_MONO_LDPTR_NURSERY_BITS:
12553 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
12555 case CEE_MONO_LDPTR_INT_REQ_FLAG:
12556 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12562 inline_costs += 10 * num_calls++;
12565 case CEE_MONO_LDPTR: {
12568 CHECK_STACK_OVF (1);
12570 token = read32 (ip + 2);
12572 ptr = mono_method_get_wrapper_data (method, token);
12573 EMIT_NEW_PCONST (cfg, ins, ptr);
12576 inline_costs += 10 * num_calls++;
12577 /* Can't embed random pointers into AOT code */
12581 case CEE_MONO_JIT_ICALL_ADDR: {
12582 MonoJitICallInfo *callinfo;
12585 CHECK_STACK_OVF (1);
12587 token = read32 (ip + 2);
12589 ptr = mono_method_get_wrapper_data (method, token);
12590 callinfo = mono_find_jit_icall_by_addr (ptr);
12591 g_assert (callinfo);
12592 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12595 inline_costs += 10 * num_calls++;
12598 case CEE_MONO_ICALL_ADDR: {
12599 MonoMethod *cmethod;
12602 CHECK_STACK_OVF (1);
12604 token = read32 (ip + 2);
12606 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
12608 if (cfg->compile_aot) {
12609 if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
12611 * This is generated by emit_native_wrapper () to resolve the pinvoke address
12612 * before the call, its not needed when using direct pinvoke.
12613 * This is not an optimization, but its used to avoid looking up pinvokes
12614 * on platforms which don't support dlopen ().
12616 EMIT_NEW_PCONST (cfg, ins, NULL);
12618 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12621 ptr = mono_lookup_internal_call (cmethod);
12623 EMIT_NEW_PCONST (cfg, ins, ptr);
12629 case CEE_MONO_VTADDR: {
12630 MonoInst *src_var, *src;
12636 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12637 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12642 case CEE_MONO_NEWOBJ: {
12643 MonoInst *iargs [2];
12645 CHECK_STACK_OVF (1);
12647 token = read32 (ip + 2);
12648 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12649 mono_class_init (klass);
12650 NEW_DOMAINCONST (cfg, iargs [0]);
12651 MONO_ADD_INS (cfg->cbb, iargs [0]);
12652 NEW_CLASSCONST (cfg, iargs [1], klass);
12653 MONO_ADD_INS (cfg->cbb, iargs [1]);
12654 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
12656 inline_costs += 10 * num_calls++;
12659 case CEE_MONO_OBJADDR:
12662 MONO_INST_NEW (cfg, ins, OP_MOVE);
12663 ins->dreg = alloc_ireg_mp (cfg);
12664 ins->sreg1 = sp [0]->dreg;
12665 ins->type = STACK_MP;
12666 MONO_ADD_INS (cfg->cbb, ins);
12670 case CEE_MONO_LDNATIVEOBJ:
12672 * Similar to LDOBJ, but instead load the unmanaged
12673 * representation of the vtype to the stack.
12678 token = read32 (ip + 2);
12679 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12680 g_assert (klass->valuetype);
12681 mono_class_init (klass);
12684 MonoInst *src, *dest, *temp;
12687 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12688 temp->backend.is_pinvoke = 1;
12689 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12690 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12692 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12693 dest->type = STACK_VTYPE;
12694 dest->klass = klass;
12700 case CEE_MONO_RETOBJ: {
12702 * Same as RET, but return the native representation of a vtype
12705 g_assert (cfg->ret);
12706 g_assert (mono_method_signature (method)->pinvoke);
12711 token = read32 (ip + 2);
12712 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12714 if (!cfg->vret_addr) {
12715 g_assert (cfg->ret_var_is_local);
12717 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12719 EMIT_NEW_RETLOADA (cfg, ins);
12721 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12723 if (sp != stack_start)
12726 MONO_INST_NEW (cfg, ins, OP_BR);
12727 ins->inst_target_bb = end_bblock;
12728 MONO_ADD_INS (cfg->cbb, ins);
12729 link_bblock (cfg, cfg->cbb, end_bblock);
12730 start_new_bblock = 1;
12734 case CEE_MONO_CISINST:
12735 case CEE_MONO_CCASTCLASS: {
12740 token = read32 (ip + 2);
12741 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12742 if (ip [1] == CEE_MONO_CISINST)
12743 ins = handle_cisinst (cfg, klass, sp [0]);
12745 ins = handle_ccastclass (cfg, klass, sp [0]);
12750 case CEE_MONO_SAVE_LMF:
12751 case CEE_MONO_RESTORE_LMF:
12754 case CEE_MONO_CLASSCONST:
12755 CHECK_STACK_OVF (1);
12757 token = read32 (ip + 2);
12758 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12761 inline_costs += 10 * num_calls++;
12763 case CEE_MONO_NOT_TAKEN:
12764 cfg->cbb->out_of_line = TRUE;
12767 case CEE_MONO_TLS: {
12770 CHECK_STACK_OVF (1);
12772 key = (MonoTlsKey)read32 (ip + 2);
12773 g_assert (key < TLS_KEY_NUM);
12775 ins = mono_create_tls_get (cfg, key);
12777 if (cfg->compile_aot) {
12779 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12780 ins->dreg = alloc_preg (cfg);
12781 ins->type = STACK_PTR;
12783 g_assert_not_reached ();
12786 ins->type = STACK_PTR;
12787 MONO_ADD_INS (cfg->cbb, ins);
12792 case CEE_MONO_DYN_CALL: {
12793 MonoCallInst *call;
12795 /* It would be easier to call a trampoline, but that would put an
12796 * extra frame on the stack, confusing exception handling. So
12797 * implement it inline using an opcode for now.
12800 if (!cfg->dyn_call_var) {
12801 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12802 /* prevent it from being register allocated */
12803 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12806 /* Has to use a call inst since it local regalloc expects it */
12807 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12808 ins = (MonoInst*)call;
12810 ins->sreg1 = sp [0]->dreg;
12811 ins->sreg2 = sp [1]->dreg;
12812 MONO_ADD_INS (cfg->cbb, ins);
12814 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12817 inline_costs += 10 * num_calls++;
12821 case CEE_MONO_MEMORY_BARRIER: {
12823 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12827 case CEE_MONO_ATOMIC_STORE_I4: {
12828 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
12834 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
12835 ins->dreg = sp [0]->dreg;
12836 ins->sreg1 = sp [1]->dreg;
12837 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
12838 MONO_ADD_INS (cfg->cbb, ins);
12843 case CEE_MONO_JIT_ATTACH: {
12844 MonoInst *args [16], *domain_ins;
12845 MonoInst *ad_ins, *jit_tls_ins;
12846 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12848 g_assert (!mono_threads_is_coop_enabled ());
12850 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12852 EMIT_NEW_PCONST (cfg, ins, NULL);
12853 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12855 ad_ins = mono_get_domain_intrinsic (cfg);
12856 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12858 if (cfg->backend->have_tls_get && ad_ins && jit_tls_ins) {
12859 NEW_BBLOCK (cfg, next_bb);
12860 NEW_BBLOCK (cfg, call_bb);
12862 if (cfg->compile_aot) {
12863 /* AOT code is only used in the root domain */
12864 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12866 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12868 MONO_ADD_INS (cfg->cbb, ad_ins);
12869 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12870 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12872 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12873 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12874 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12876 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12877 MONO_START_BB (cfg, call_bb);
12880 /* AOT code is only used in the root domain */
12881 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12882 if (cfg->compile_aot) {
12886 * This is called on unattached threads, so it cannot go through the trampoline
12887 * infrastructure. Use an indirect call through a got slot initialized at load time
12890 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
12891 ins = mono_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
12893 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12895 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12898 MONO_START_BB (cfg, next_bb);
12903 case CEE_MONO_JIT_DETACH: {
12904 MonoInst *args [16];
12906 /* Restore the original domain */
12907 dreg = alloc_ireg (cfg);
12908 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12909 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12913 case CEE_MONO_CALLI_EXTRA_ARG: {
12915 MonoMethodSignature *fsig;
12919 * This is the same as CEE_CALLI, but passes an additional argument
12920 * to the called method in llvmonly mode.
12921 * This is only used by delegate invoke wrappers to call the
12922 * actual delegate method.
12924 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12927 token = read32 (ip + 2);
12935 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
12938 if (cfg->llvm_only)
12939 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12941 n = fsig->param_count + fsig->hasthis + 1;
12948 if (cfg->llvm_only) {
12950 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12951 * cconv. This is set by mono_init_delegate ().
12953 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12954 MonoInst *callee = addr;
12955 MonoInst *call, *localloc_ins;
12956 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12957 int low_bit_reg = alloc_preg (cfg);
12959 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12960 NEW_BBLOCK (cfg, end_bb);
12962 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12963 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12964 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12966 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12967 addr = emit_get_rgctx_sig (cfg, context_used,
12968 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12970 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12972 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12973 ins->dreg = alloc_preg (cfg);
12974 ins->inst_imm = 2 * SIZEOF_VOID_P;
12975 MONO_ADD_INS (cfg->cbb, ins);
12976 localloc_ins = ins;
12977 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12978 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12979 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12981 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12982 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12984 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12985 MONO_START_BB (cfg, is_gsharedvt_bb);
12986 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12987 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12988 ins->dreg = call->dreg;
12990 MONO_START_BB (cfg, end_bb);
12992 /* Caller uses a normal calling conv */
12994 MonoInst *callee = addr;
12995 MonoInst *call, *localloc_ins;
12996 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12997 int low_bit_reg = alloc_preg (cfg);
12999 NEW_BBLOCK (cfg, is_gsharedvt_bb);
13000 NEW_BBLOCK (cfg, end_bb);
13002 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
13003 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
13004 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
13006 /* Normal case: callee uses a normal cconv, no conversion is needed */
13007 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
13008 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
13009 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
13010 MONO_START_BB (cfg, is_gsharedvt_bb);
13011 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
13012 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
13013 MONO_ADD_INS (cfg->cbb, addr);
13015 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
13017 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
13018 ins->dreg = alloc_preg (cfg);
13019 ins->inst_imm = 2 * SIZEOF_VOID_P;
13020 MONO_ADD_INS (cfg->cbb, ins);
13021 localloc_ins = ins;
13022 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13023 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
13024 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
13026 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
13027 ins->dreg = call->dreg;
13028 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
13030 MONO_START_BB (cfg, end_bb);
13033 /* Same as CEE_CALLI */
13034 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
13036 * We pass the address to the gsharedvt trampoline in the rgctx reg
13038 MonoInst *callee = addr;
13040 addr = emit_get_rgctx_sig (cfg, context_used,
13041 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
13042 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
13044 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
13048 if (!MONO_TYPE_IS_VOID (fsig->ret))
13049 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
13051 CHECK_CFG_EXCEPTION;
13055 constrained_class = NULL;
13058 case CEE_MONO_LDDOMAIN:
13059 CHECK_STACK_OVF (1);
13060 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
13064 case CEE_MONO_GET_LAST_ERROR:
13066 CHECK_STACK_OVF (1);
13068 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
13069 ins->dreg = alloc_dreg (cfg, STACK_I4);
13070 ins->type = STACK_I4;
13071 MONO_ADD_INS (cfg->cbb, ins);
13077 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
13083 case CEE_PREFIX1: {
13086 case CEE_ARGLIST: {
13087 /* somewhat similar to LDTOKEN */
13088 MonoInst *addr, *vtvar;
13089 CHECK_STACK_OVF (1);
13090 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
13092 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
13093 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
13095 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
13096 ins->type = STACK_VTYPE;
13097 ins->klass = mono_defaults.argumenthandle_class;
13107 MonoInst *cmp, *arg1, *arg2;
13115 * The following transforms:
13116 * CEE_CEQ into OP_CEQ
13117 * CEE_CGT into OP_CGT
13118 * CEE_CGT_UN into OP_CGT_UN
13119 * CEE_CLT into OP_CLT
13120 * CEE_CLT_UN into OP_CLT_UN
13122 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
13124 MONO_INST_NEW (cfg, ins, cmp->opcode);
13125 cmp->sreg1 = arg1->dreg;
13126 cmp->sreg2 = arg2->dreg;
13127 type_from_op (cfg, cmp, arg1, arg2);
13129 add_widen_op (cfg, cmp, &arg1, &arg2);
13130 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
13131 cmp->opcode = OP_LCOMPARE;
13132 else if (arg1->type == STACK_R4)
13133 cmp->opcode = OP_RCOMPARE;
13134 else if (arg1->type == STACK_R8)
13135 cmp->opcode = OP_FCOMPARE;
13137 cmp->opcode = OP_ICOMPARE;
13138 MONO_ADD_INS (cfg->cbb, cmp);
13139 ins->type = STACK_I4;
13140 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
13141 type_from_op (cfg, ins, arg1, arg2);
13143 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
13145 * The backends expect the fceq opcodes to do the
13148 ins->sreg1 = cmp->sreg1;
13149 ins->sreg2 = cmp->sreg2;
13152 MONO_ADD_INS (cfg->cbb, ins);
13158 MonoInst *argconst;
13159 MonoMethod *cil_method;
13161 CHECK_STACK_OVF (1);
13163 n = read32 (ip + 2);
13164 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13167 mono_class_init (cmethod->klass);
13169 mono_save_token_info (cfg, image, n, cmethod);
13171 context_used = mini_method_check_context_used (cfg, cmethod);
13173 cil_method = cmethod;
13174 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
13175 emit_method_access_failure (cfg, method, cil_method);
13177 if (mono_security_core_clr_enabled ())
13178 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13181 * Optimize the common case of ldftn+delegate creation
13183 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
13184 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13185 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13186 MonoInst *target_ins, *handle_ins;
13187 MonoMethod *invoke;
13188 int invoke_context_used;
13190 invoke = mono_get_delegate_invoke (ctor_method->klass);
13191 if (!invoke || !mono_method_signature (invoke))
13194 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13196 target_ins = sp [-1];
13198 if (mono_security_core_clr_enabled ())
13199 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13201 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
13202 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
13203 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
13204 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
13205 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
13209 /* FIXME: SGEN support */
13210 if (invoke_context_used == 0 || cfg->llvm_only) {
13212 if (cfg->verbose_level > 3)
13213 g_print ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
13214 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
13217 CHECK_CFG_EXCEPTION;
13227 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
13228 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
13232 inline_costs += 10 * num_calls++;
13235 case CEE_LDVIRTFTN: {
13236 MonoInst *args [2];
13240 n = read32 (ip + 2);
13241 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13244 mono_class_init (cmethod->klass);
13246 context_used = mini_method_check_context_used (cfg, cmethod);
13248 if (mono_security_core_clr_enabled ())
13249 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13252 * Optimize the common case of ldvirtftn+delegate creation
13254 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ip > header->code && ip [-1] == CEE_DUP)) {
13255 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13256 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13257 MonoInst *target_ins, *handle_ins;
13258 MonoMethod *invoke;
13259 int invoke_context_used;
13260 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
13262 invoke = mono_get_delegate_invoke (ctor_method->klass);
13263 if (!invoke || !mono_method_signature (invoke))
13266 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13268 target_ins = sp [-1];
13270 if (mono_security_core_clr_enabled ())
13271 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13273 /* FIXME: SGEN support */
13274 if (invoke_context_used == 0 || cfg->llvm_only) {
13276 if (cfg->verbose_level > 3)
13277 g_print ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
13278 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
13281 CHECK_CFG_EXCEPTION;
13294 args [1] = emit_get_rgctx_method (cfg, context_used,
13295 cmethod, MONO_RGCTX_INFO_METHOD);
13298 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
13300 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
13303 inline_costs += 10 * num_calls++;
13307 CHECK_STACK_OVF (1);
13309 n = read16 (ip + 2);
13311 EMIT_NEW_ARGLOAD (cfg, ins, n);
13316 CHECK_STACK_OVF (1);
13318 n = read16 (ip + 2);
13320 NEW_ARGLOADA (cfg, ins, n);
13321 MONO_ADD_INS (cfg->cbb, ins);
13329 n = read16 (ip + 2);
13331 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
13333 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
13337 CHECK_STACK_OVF (1);
13339 n = read16 (ip + 2);
13341 EMIT_NEW_LOCLOAD (cfg, ins, n);
13346 unsigned char *tmp_ip;
13347 CHECK_STACK_OVF (1);
13349 n = read16 (ip + 2);
13352 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
13358 EMIT_NEW_LOCLOADA (cfg, ins, n);
13367 n = read16 (ip + 2);
13369 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
13371 emit_stloc_ir (cfg, sp, header, n);
13375 case CEE_LOCALLOC: {
13377 MonoBasicBlock *non_zero_bb, *end_bb;
13378 int alloc_ptr = alloc_preg (cfg);
13380 if (sp != stack_start)
13382 if (cfg->method != method)
13384 * Inlining this into a loop in a parent could lead to
13385 * stack overflows which is different behavior than the
13386 * non-inlined case, thus disable inlining in this case.
13388 INLINE_FAILURE("localloc");
13390 NEW_BBLOCK (cfg, non_zero_bb);
13391 NEW_BBLOCK (cfg, end_bb);
13393 /* if size != zero */
13394 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
13395 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
13397 //size is zero, so result is NULL
13398 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
13399 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
13401 MONO_START_BB (cfg, non_zero_bb);
13402 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
13403 ins->dreg = alloc_ptr;
13404 ins->sreg1 = sp [0]->dreg;
13405 ins->type = STACK_PTR;
13406 MONO_ADD_INS (cfg->cbb, ins);
13408 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13410 ins->flags |= MONO_INST_INIT;
13412 MONO_START_BB (cfg, end_bb);
13413 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
13414 ins->type = STACK_PTR;
13420 case CEE_ENDFILTER: {
13421 MonoExceptionClause *clause, *nearest;
13426 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
13428 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
13429 ins->sreg1 = (*sp)->dreg;
13430 MONO_ADD_INS (cfg->cbb, ins);
13431 start_new_bblock = 1;
13435 for (cc = 0; cc < header->num_clauses; ++cc) {
13436 clause = &header->clauses [cc];
13437 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
13438 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
13439 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
13442 g_assert (nearest);
13443 if ((ip - header->code) != nearest->handler_offset)
13448 case CEE_UNALIGNED_:
13449 ins_flag |= MONO_INST_UNALIGNED;
13450 /* FIXME: record alignment? we can assume 1 for now */
13454 case CEE_VOLATILE_:
13455 ins_flag |= MONO_INST_VOLATILE;
13459 ins_flag |= MONO_INST_TAILCALL;
13460 cfg->flags |= MONO_CFG_HAS_TAIL;
13461 /* Can't inline tail calls at this time */
13462 inline_costs += 100000;
13469 token = read32 (ip + 2);
13470 klass = mini_get_class (method, token, generic_context);
13471 CHECK_TYPELOAD (klass);
13472 if (generic_class_is_reference_type (cfg, klass))
13473 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
13475 mini_emit_initobj (cfg, *sp, NULL, klass);
13479 case CEE_CONSTRAINED_:
13481 token = read32 (ip + 2);
13482 constrained_class = mini_get_class (method, token, generic_context);
13483 CHECK_TYPELOAD (constrained_class);
13487 case CEE_INITBLK: {
13488 MonoInst *iargs [3];
13492 /* Skip optimized paths for volatile operations. */
13493 if ((ip [1] == CEE_CPBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
13494 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
13495 } else if ((ip [1] == CEE_INITBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5) && (sp [1]->opcode == OP_ICONST) && (sp [1]->inst_c0 == 0)) {
13496 /* emit_memset only works when val == 0 */
13497 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
13500 iargs [0] = sp [0];
13501 iargs [1] = sp [1];
13502 iargs [2] = sp [2];
13503 if (ip [1] == CEE_CPBLK) {
13505 * FIXME: It's unclear whether we should be emitting both the acquire
13506 * and release barriers for cpblk. It is technically both a load and
13507 * store operation, so it seems like that's the sensible thing to do.
13509 * FIXME: We emit full barriers on both sides of the operation for
13510 * simplicity. We should have a separate atomic memcpy method instead.
13512 MonoMethod *memcpy_method = get_memcpy_method ();
13514 if (ins_flag & MONO_INST_VOLATILE)
13515 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13517 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
13518 call->flags |= ins_flag;
13520 if (ins_flag & MONO_INST_VOLATILE)
13521 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13523 MonoMethod *memset_method = get_memset_method ();
13524 if (ins_flag & MONO_INST_VOLATILE) {
13525 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
13526 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
13528 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
13529 call->flags |= ins_flag;
13540 ins_flag |= MONO_INST_NOTYPECHECK;
13542 ins_flag |= MONO_INST_NORANGECHECK;
13543 /* we ignore the no-nullcheck for now since we
13544 * really do it explicitly only when doing callvirt->call
13548 case CEE_RETHROW: {
13550 int handler_offset = -1;
13552 for (i = 0; i < header->num_clauses; ++i) {
13553 MonoExceptionClause *clause = &header->clauses [i];
13554 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
13555 handler_offset = clause->handler_offset;
13560 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
13562 if (handler_offset == -1)
13565 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
13566 MONO_INST_NEW (cfg, ins, OP_RETHROW);
13567 ins->sreg1 = load->dreg;
13568 MONO_ADD_INS (cfg->cbb, ins);
13570 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
13571 MONO_ADD_INS (cfg->cbb, ins);
13574 link_bblock (cfg, cfg->cbb, end_bblock);
13575 start_new_bblock = 1;
13583 CHECK_STACK_OVF (1);
13585 token = read32 (ip + 2);
13586 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
13587 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
13590 val = mono_type_size (type, &ialign);
13592 MonoClass *klass = mini_get_class (method, token, generic_context);
13593 CHECK_TYPELOAD (klass);
13595 val = mono_type_size (&klass->byval_arg, &ialign);
13597 if (mini_is_gsharedvt_klass (klass))
13598 GSHAREDVT_FAILURE (*ip);
13600 EMIT_NEW_ICONST (cfg, ins, val);
13605 case CEE_REFANYTYPE: {
13606 MonoInst *src_var, *src;
13608 GSHAREDVT_FAILURE (*ip);
13614 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
13616 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
13617 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
13618 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
13623 case CEE_READONLY_:
13636 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
13646 g_warning ("opcode 0x%02x not handled", *ip);
13650 if (start_new_bblock != 1)
13653 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
13654 if (cfg->cbb->next_bb) {
13655 /* This could already be set because of inlining, #693905 */
13656 MonoBasicBlock *bb = cfg->cbb;
13658 while (bb->next_bb)
13660 bb->next_bb = end_bblock;
13662 cfg->cbb->next_bb = end_bblock;
13665 if (cfg->method == method && cfg->domainvar) {
13667 MonoInst *get_domain;
13669 cfg->cbb = init_localsbb;
13671 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
13672 MONO_ADD_INS (cfg->cbb, get_domain);
13674 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
13676 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
13677 MONO_ADD_INS (cfg->cbb, store);
13680 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
13681 if (cfg->compile_aot)
13682 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
13683 mono_get_got_var (cfg);
13686 if (cfg->method == method && cfg->got_var)
13687 mono_emit_load_got_addr (cfg);
13689 if (init_localsbb) {
13690 cfg->cbb = init_localsbb;
13692 for (i = 0; i < header->num_locals; ++i) {
13693 emit_init_local (cfg, i, header->locals [i], init_locals);
13697 if (cfg->init_ref_vars && cfg->method == method) {
13698 /* Emit initialization for ref vars */
13699 // FIXME: Avoid duplication initialization for IL locals.
13700 for (i = 0; i < cfg->num_varinfo; ++i) {
13701 MonoInst *ins = cfg->varinfo [i];
13703 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
13704 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
13708 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
13709 cfg->cbb = init_localsbb;
13710 emit_push_lmf (cfg);
13713 cfg->cbb = init_localsbb;
13714 emit_instrumentation_call (cfg, mono_profiler_method_enter);
13717 MonoBasicBlock *bb;
13720 * Make seq points at backward branch targets interruptable.
13722 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13723 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13724 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13727 /* Add a sequence point for method entry/exit events */
13728 if (seq_points && cfg->gen_sdb_seq_points) {
13729 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13730 MONO_ADD_INS (init_localsbb, ins);
13731 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13732 MONO_ADD_INS (cfg->bb_exit, ins);
13736 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13737 * the code they refer to was dead (#11880).
13739 if (sym_seq_points) {
13740 for (i = 0; i < header->code_size; ++i) {
13741 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13744 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13745 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13752 if (cfg->method == method) {
13753 MonoBasicBlock *bb;
13754 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13755 if (bb == cfg->bb_init)
13758 bb->region = mono_find_block_region (cfg, bb->real_offset);
13760 mono_create_spvar_for_region (cfg, bb->region);
13761 if (cfg->verbose_level > 2)
13762 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13765 MonoBasicBlock *bb;
13766 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
13767 for (bb = start_bblock; bb != end_bblock; bb = bb->next_bb) {
13768 bb->real_offset = inline_offset;
13772 if (inline_costs < 0) {
13775 /* Method is too large */
13776 mname = mono_method_full_name (method, TRUE);
13777 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13781 if ((cfg->verbose_level > 2) && (cfg->method == method))
13782 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13787 g_assert (!mono_error_ok (&cfg->error));
13791 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13795 set_exception_type_from_invalid_il (cfg, method, ip);
13799 g_slist_free (class_inits);
13800 mono_basic_block_free (original_bb);
13801 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13802 if (cfg->exception_type)
13805 return inline_costs;
13809 store_membase_reg_to_store_membase_imm (int opcode)
13812 case OP_STORE_MEMBASE_REG:
13813 return OP_STORE_MEMBASE_IMM;
13814 case OP_STOREI1_MEMBASE_REG:
13815 return OP_STOREI1_MEMBASE_IMM;
13816 case OP_STOREI2_MEMBASE_REG:
13817 return OP_STOREI2_MEMBASE_IMM;
13818 case OP_STOREI4_MEMBASE_REG:
13819 return OP_STOREI4_MEMBASE_IMM;
13820 case OP_STOREI8_MEMBASE_REG:
13821 return OP_STOREI8_MEMBASE_IMM;
13823 g_assert_not_reached ();
13830 mono_op_to_op_imm (int opcode)
13834 return OP_IADD_IMM;
13836 return OP_ISUB_IMM;
13838 return OP_IDIV_IMM;
13840 return OP_IDIV_UN_IMM;
13842 return OP_IREM_IMM;
13844 return OP_IREM_UN_IMM;
13846 return OP_IMUL_IMM;
13848 return OP_IAND_IMM;
13852 return OP_IXOR_IMM;
13854 return OP_ISHL_IMM;
13856 return OP_ISHR_IMM;
13858 return OP_ISHR_UN_IMM;
13861 return OP_LADD_IMM;
13863 return OP_LSUB_IMM;
13865 return OP_LAND_IMM;
13869 return OP_LXOR_IMM;
13871 return OP_LSHL_IMM;
13873 return OP_LSHR_IMM;
13875 return OP_LSHR_UN_IMM;
13876 #if SIZEOF_REGISTER == 8
13878 return OP_LREM_IMM;
13882 return OP_COMPARE_IMM;
13884 return OP_ICOMPARE_IMM;
13886 return OP_LCOMPARE_IMM;
13888 case OP_STORE_MEMBASE_REG:
13889 return OP_STORE_MEMBASE_IMM;
13890 case OP_STOREI1_MEMBASE_REG:
13891 return OP_STOREI1_MEMBASE_IMM;
13892 case OP_STOREI2_MEMBASE_REG:
13893 return OP_STOREI2_MEMBASE_IMM;
13894 case OP_STOREI4_MEMBASE_REG:
13895 return OP_STOREI4_MEMBASE_IMM;
13897 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13899 return OP_X86_PUSH_IMM;
13900 case OP_X86_COMPARE_MEMBASE_REG:
13901 return OP_X86_COMPARE_MEMBASE_IMM;
13903 #if defined(TARGET_AMD64)
13904 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13905 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13907 case OP_VOIDCALL_REG:
13908 return OP_VOIDCALL;
13916 return OP_LOCALLOC_IMM;
13923 ldind_to_load_membase (int opcode)
13927 return OP_LOADI1_MEMBASE;
13929 return OP_LOADU1_MEMBASE;
13931 return OP_LOADI2_MEMBASE;
13933 return OP_LOADU2_MEMBASE;
13935 return OP_LOADI4_MEMBASE;
13937 return OP_LOADU4_MEMBASE;
13939 return OP_LOAD_MEMBASE;
13940 case CEE_LDIND_REF:
13941 return OP_LOAD_MEMBASE;
13943 return OP_LOADI8_MEMBASE;
13945 return OP_LOADR4_MEMBASE;
13947 return OP_LOADR8_MEMBASE;
13949 g_assert_not_reached ();
13956 stind_to_store_membase (int opcode)
13960 return OP_STOREI1_MEMBASE_REG;
13962 return OP_STOREI2_MEMBASE_REG;
13964 return OP_STOREI4_MEMBASE_REG;
13966 case CEE_STIND_REF:
13967 return OP_STORE_MEMBASE_REG;
13969 return OP_STOREI8_MEMBASE_REG;
13971 return OP_STORER4_MEMBASE_REG;
13973 return OP_STORER8_MEMBASE_REG;
13975 g_assert_not_reached ();
13982 mono_load_membase_to_load_mem (int opcode)
13984 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13985 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13987 case OP_LOAD_MEMBASE:
13988 return OP_LOAD_MEM;
13989 case OP_LOADU1_MEMBASE:
13990 return OP_LOADU1_MEM;
13991 case OP_LOADU2_MEMBASE:
13992 return OP_LOADU2_MEM;
13993 case OP_LOADI4_MEMBASE:
13994 return OP_LOADI4_MEM;
13995 case OP_LOADU4_MEMBASE:
13996 return OP_LOADU4_MEM;
13997 #if SIZEOF_REGISTER == 8
13998 case OP_LOADI8_MEMBASE:
13999 return OP_LOADI8_MEM;
14008 op_to_op_dest_membase (int store_opcode, int opcode)
14010 #if defined(TARGET_X86)
14011 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
14016 return OP_X86_ADD_MEMBASE_REG;
14018 return OP_X86_SUB_MEMBASE_REG;
14020 return OP_X86_AND_MEMBASE_REG;
14022 return OP_X86_OR_MEMBASE_REG;
14024 return OP_X86_XOR_MEMBASE_REG;
14027 return OP_X86_ADD_MEMBASE_IMM;
14030 return OP_X86_SUB_MEMBASE_IMM;
14033 return OP_X86_AND_MEMBASE_IMM;
14036 return OP_X86_OR_MEMBASE_IMM;
14039 return OP_X86_XOR_MEMBASE_IMM;
14045 #if defined(TARGET_AMD64)
14046 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
14051 return OP_X86_ADD_MEMBASE_REG;
14053 return OP_X86_SUB_MEMBASE_REG;
14055 return OP_X86_AND_MEMBASE_REG;
14057 return OP_X86_OR_MEMBASE_REG;
14059 return OP_X86_XOR_MEMBASE_REG;
14061 return OP_X86_ADD_MEMBASE_IMM;
14063 return OP_X86_SUB_MEMBASE_IMM;
14065 return OP_X86_AND_MEMBASE_IMM;
14067 return OP_X86_OR_MEMBASE_IMM;
14069 return OP_X86_XOR_MEMBASE_IMM;
14071 return OP_AMD64_ADD_MEMBASE_REG;
14073 return OP_AMD64_SUB_MEMBASE_REG;
14075 return OP_AMD64_AND_MEMBASE_REG;
14077 return OP_AMD64_OR_MEMBASE_REG;
14079 return OP_AMD64_XOR_MEMBASE_REG;
14082 return OP_AMD64_ADD_MEMBASE_IMM;
14085 return OP_AMD64_SUB_MEMBASE_IMM;
14088 return OP_AMD64_AND_MEMBASE_IMM;
14091 return OP_AMD64_OR_MEMBASE_IMM;
14094 return OP_AMD64_XOR_MEMBASE_IMM;
14104 op_to_op_store_membase (int store_opcode, int opcode)
14106 #if defined(TARGET_X86) || defined(TARGET_AMD64)
14109 if (store_opcode == OP_STOREI1_MEMBASE_REG)
14110 return OP_X86_SETEQ_MEMBASE;
14112 if (store_opcode == OP_STOREI1_MEMBASE_REG)
14113 return OP_X86_SETNE_MEMBASE;
14121 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
14124 /* FIXME: This has sign extension issues */
14126 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14127 return OP_X86_COMPARE_MEMBASE8_IMM;
14130 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14135 return OP_X86_PUSH_MEMBASE;
14136 case OP_COMPARE_IMM:
14137 case OP_ICOMPARE_IMM:
14138 return OP_X86_COMPARE_MEMBASE_IMM;
14141 return OP_X86_COMPARE_MEMBASE_REG;
14145 #ifdef TARGET_AMD64
14146 /* FIXME: This has sign extension issues */
14148 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14149 return OP_X86_COMPARE_MEMBASE8_IMM;
14154 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14155 return OP_X86_PUSH_MEMBASE;
14157 /* FIXME: This only works for 32 bit immediates
14158 case OP_COMPARE_IMM:
14159 case OP_LCOMPARE_IMM:
14160 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
14161 return OP_AMD64_COMPARE_MEMBASE_IMM;
14163 case OP_ICOMPARE_IMM:
14164 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14165 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
14169 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
14170 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14171 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14172 return OP_AMD64_COMPARE_MEMBASE_REG;
14175 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14176 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14185 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
14188 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14194 return OP_X86_COMPARE_REG_MEMBASE;
14196 return OP_X86_ADD_REG_MEMBASE;
14198 return OP_X86_SUB_REG_MEMBASE;
14200 return OP_X86_AND_REG_MEMBASE;
14202 return OP_X86_OR_REG_MEMBASE;
14204 return OP_X86_XOR_REG_MEMBASE;
14208 #ifdef TARGET_AMD64
14209 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
14212 return OP_AMD64_ICOMPARE_REG_MEMBASE;
14214 return OP_X86_ADD_REG_MEMBASE;
14216 return OP_X86_SUB_REG_MEMBASE;
14218 return OP_X86_AND_REG_MEMBASE;
14220 return OP_X86_OR_REG_MEMBASE;
14222 return OP_X86_XOR_REG_MEMBASE;
14224 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
14228 return OP_AMD64_COMPARE_REG_MEMBASE;
14230 return OP_AMD64_ADD_REG_MEMBASE;
14232 return OP_AMD64_SUB_REG_MEMBASE;
14234 return OP_AMD64_AND_REG_MEMBASE;
14236 return OP_AMD64_OR_REG_MEMBASE;
14238 return OP_AMD64_XOR_REG_MEMBASE;
14247 mono_op_to_op_imm_noemul (int opcode)
14250 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
14256 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14263 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
14268 return mono_op_to_op_imm (opcode);
14273 * mono_handle_global_vregs:
14275 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
14279 mono_handle_global_vregs (MonoCompile *cfg)
14281 gint32 *vreg_to_bb;
14282 MonoBasicBlock *bb;
14285 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
14287 #ifdef MONO_ARCH_SIMD_INTRINSICS
14288 if (cfg->uses_simd_intrinsics)
14289 mono_simd_simplify_indirection (cfg);
14292 /* Find local vregs used in more than one bb */
14293 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14294 MonoInst *ins = bb->code;
14295 int block_num = bb->block_num;
14297 if (cfg->verbose_level > 2)
14298 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
14301 for (; ins; ins = ins->next) {
14302 const char *spec = INS_INFO (ins->opcode);
14303 int regtype = 0, regindex;
14306 if (G_UNLIKELY (cfg->verbose_level > 2))
14307 mono_print_ins (ins);
14309 g_assert (ins->opcode >= MONO_CEE_LAST);
14311 for (regindex = 0; regindex < 4; regindex ++) {
14314 if (regindex == 0) {
14315 regtype = spec [MONO_INST_DEST];
14316 if (regtype == ' ')
14319 } else if (regindex == 1) {
14320 regtype = spec [MONO_INST_SRC1];
14321 if (regtype == ' ')
14324 } else if (regindex == 2) {
14325 regtype = spec [MONO_INST_SRC2];
14326 if (regtype == ' ')
14329 } else if (regindex == 3) {
14330 regtype = spec [MONO_INST_SRC3];
14331 if (regtype == ' ')
14336 #if SIZEOF_REGISTER == 4
14337 /* In the LLVM case, the long opcodes are not decomposed */
14338 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
14340 * Since some instructions reference the original long vreg,
14341 * and some reference the two component vregs, it is quite hard
14342 * to determine when it needs to be global. So be conservative.
14344 if (!get_vreg_to_inst (cfg, vreg)) {
14345 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14347 if (cfg->verbose_level > 2)
14348 printf ("LONG VREG R%d made global.\n", vreg);
14352 * Make the component vregs volatile since the optimizations can
14353 * get confused otherwise.
14355 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
14356 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
14360 g_assert (vreg != -1);
14362 prev_bb = vreg_to_bb [vreg];
14363 if (prev_bb == 0) {
14364 /* 0 is a valid block num */
14365 vreg_to_bb [vreg] = block_num + 1;
14366 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
14367 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
14370 if (!get_vreg_to_inst (cfg, vreg)) {
14371 if (G_UNLIKELY (cfg->verbose_level > 2))
14372 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
14376 if (vreg_is_ref (cfg, vreg))
14377 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
14379 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
14382 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14385 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
14389 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
14392 g_assert_not_reached ();
14396 /* Flag as having been used in more than one bb */
14397 vreg_to_bb [vreg] = -1;
14403 /* If a variable is used in only one bblock, convert it into a local vreg */
14404 for (i = 0; i < cfg->num_varinfo; i++) {
14405 MonoInst *var = cfg->varinfo [i];
14406 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
14408 switch (var->type) {
14414 #if SIZEOF_REGISTER == 8
14417 #if !defined(TARGET_X86)
14418 /* Enabling this screws up the fp stack on x86 */
14421 if (mono_arch_is_soft_float ())
14425 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
14429 /* Arguments are implicitly global */
14430 /* Putting R4 vars into registers doesn't work currently */
14431 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
14432 if ((var->opcode != OP_ARG) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && (vreg_to_bb [var->dreg] != -1) && (var->klass->byval_arg.type != MONO_TYPE_R4) && !cfg->disable_vreg_to_lvreg && var != cfg->gsharedvt_info_var && var != cfg->gsharedvt_locals_var && var != cfg->lmf_addr_var) {
14434 * Make that the variable's liveness interval doesn't contain a call, since
14435 * that would cause the lvreg to be spilled, making the whole optimization
14438 /* This is too slow for JIT compilation */
14440 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
14442 int def_index, call_index, ins_index;
14443 gboolean spilled = FALSE;
14448 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
14449 const char *spec = INS_INFO (ins->opcode);
14451 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
14452 def_index = ins_index;
14454 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
14455 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
14456 if (call_index > def_index) {
14462 if (MONO_IS_CALL (ins))
14463 call_index = ins_index;
14473 if (G_UNLIKELY (cfg->verbose_level > 2))
14474 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
14475 var->flags |= MONO_INST_IS_DEAD;
14476 cfg->vreg_to_inst [var->dreg] = NULL;
14483 * Compress the varinfo and vars tables so the liveness computation is faster and
14484 * takes up less space.
14487 for (i = 0; i < cfg->num_varinfo; ++i) {
14488 MonoInst *var = cfg->varinfo [i];
14489 if (pos < i && cfg->locals_start == i)
14490 cfg->locals_start = pos;
14491 if (!(var->flags & MONO_INST_IS_DEAD)) {
14493 cfg->varinfo [pos] = cfg->varinfo [i];
14494 cfg->varinfo [pos]->inst_c0 = pos;
14495 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
14496 cfg->vars [pos].idx = pos;
14497 #if SIZEOF_REGISTER == 4
14498 if (cfg->varinfo [pos]->type == STACK_I8) {
14499 /* Modify the two component vars too */
14502 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
14503 var1->inst_c0 = pos;
14504 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
14505 var1->inst_c0 = pos;
14512 cfg->num_varinfo = pos;
14513 if (cfg->locals_start > cfg->num_varinfo)
14514 cfg->locals_start = cfg->num_varinfo;
14518 * mono_allocate_gsharedvt_vars:
14520 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
14521 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
14524 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
14528 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
14530 for (i = 0; i < cfg->num_varinfo; ++i) {
14531 MonoInst *ins = cfg->varinfo [i];
14534 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
14535 if (i >= cfg->locals_start) {
14537 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
14538 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
14539 ins->opcode = OP_GSHAREDVT_LOCAL;
14540 ins->inst_imm = idx;
14543 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
14544 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
14551 * mono_spill_global_vars:
14553 * Generate spill code for variables which are not allocated to registers,
14554 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
14555 * code is generated which could be optimized by the local optimization passes.
14558 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
14560 MonoBasicBlock *bb;
14562 int orig_next_vreg;
14563 guint32 *vreg_to_lvreg;
14565 guint32 i, lvregs_len;
14566 gboolean dest_has_lvreg = FALSE;
14567 MonoStackType stacktypes [128];
14568 MonoInst **live_range_start, **live_range_end;
14569 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
14571 *need_local_opts = FALSE;
14573 memset (spec2, 0, sizeof (spec2));
14575 /* FIXME: Move this function to mini.c */
14576 stacktypes ['i'] = STACK_PTR;
14577 stacktypes ['l'] = STACK_I8;
14578 stacktypes ['f'] = STACK_R8;
14579 #ifdef MONO_ARCH_SIMD_INTRINSICS
14580 stacktypes ['x'] = STACK_VTYPE;
14583 #if SIZEOF_REGISTER == 4
14584 /* Create MonoInsts for longs */
14585 for (i = 0; i < cfg->num_varinfo; i++) {
14586 MonoInst *ins = cfg->varinfo [i];
14588 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
14589 switch (ins->type) {
14594 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
14597 g_assert (ins->opcode == OP_REGOFFSET);
14599 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
14601 tree->opcode = OP_REGOFFSET;
14602 tree->inst_basereg = ins->inst_basereg;
14603 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
14605 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
14607 tree->opcode = OP_REGOFFSET;
14608 tree->inst_basereg = ins->inst_basereg;
14609 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
14619 if (cfg->compute_gc_maps) {
14620 /* registers need liveness info even for !non refs */
14621 for (i = 0; i < cfg->num_varinfo; i++) {
14622 MonoInst *ins = cfg->varinfo [i];
14624 if (ins->opcode == OP_REGVAR)
14625 ins->flags |= MONO_INST_GC_TRACK;
14629 /* FIXME: widening and truncation */
14632 * As an optimization, when a variable allocated to the stack is first loaded into
14633 * an lvreg, we will remember the lvreg and use it the next time instead of loading
14634 * the variable again.
14636 orig_next_vreg = cfg->next_vreg;
14637 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
14638 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
14642 * These arrays contain the first and last instructions accessing a given
14644 * Since we emit bblocks in the same order we process them here, and we
14645 * don't split live ranges, these will precisely describe the live range of
14646 * the variable, i.e. the instruction range where a valid value can be found
14647 * in the variables location.
14648 * The live range is computed using the liveness info computed by the liveness pass.
14649 * We can't use vmv->range, since that is an abstract live range, and we need
14650 * one which is instruction precise.
14651 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
14653 /* FIXME: Only do this if debugging info is requested */
14654 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
14655 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
14656 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14657 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14659 /* Add spill loads/stores */
14660 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14663 if (cfg->verbose_level > 2)
14664 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
14666 /* Clear vreg_to_lvreg array */
14667 for (i = 0; i < lvregs_len; i++)
14668 vreg_to_lvreg [lvregs [i]] = 0;
14672 MONO_BB_FOR_EACH_INS (bb, ins) {
14673 const char *spec = INS_INFO (ins->opcode);
14674 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
14675 gboolean store, no_lvreg;
14676 int sregs [MONO_MAX_SRC_REGS];
14678 if (G_UNLIKELY (cfg->verbose_level > 2))
14679 mono_print_ins (ins);
14681 if (ins->opcode == OP_NOP)
14685 * We handle LDADDR here as well, since it can only be decomposed
14686 * when variable addresses are known.
14688 if (ins->opcode == OP_LDADDR) {
14689 MonoInst *var = (MonoInst *)ins->inst_p0;
14691 if (var->opcode == OP_VTARG_ADDR) {
14692 /* Happens on SPARC/S390 where vtypes are passed by reference */
14693 MonoInst *vtaddr = var->inst_left;
14694 if (vtaddr->opcode == OP_REGVAR) {
14695 ins->opcode = OP_MOVE;
14696 ins->sreg1 = vtaddr->dreg;
14698 else if (var->inst_left->opcode == OP_REGOFFSET) {
14699 ins->opcode = OP_LOAD_MEMBASE;
14700 ins->inst_basereg = vtaddr->inst_basereg;
14701 ins->inst_offset = vtaddr->inst_offset;
14704 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
14705 /* gsharedvt arg passed by ref */
14706 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
14708 ins->opcode = OP_LOAD_MEMBASE;
14709 ins->inst_basereg = var->inst_basereg;
14710 ins->inst_offset = var->inst_offset;
14711 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
14712 MonoInst *load, *load2, *load3;
14713 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
14714 int reg1, reg2, reg3;
14715 MonoInst *info_var = cfg->gsharedvt_info_var;
14716 MonoInst *locals_var = cfg->gsharedvt_locals_var;
14720 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14723 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14725 g_assert (info_var);
14726 g_assert (locals_var);
14728 /* Mark the instruction used to compute the locals var as used */
14729 cfg->gsharedvt_locals_var_ins = NULL;
14731 /* Load the offset */
14732 if (info_var->opcode == OP_REGOFFSET) {
14733 reg1 = alloc_ireg (cfg);
14734 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14735 } else if (info_var->opcode == OP_REGVAR) {
14737 reg1 = info_var->dreg;
14739 g_assert_not_reached ();
14741 reg2 = alloc_ireg (cfg);
14742 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14743 /* Load the locals area address */
14744 reg3 = alloc_ireg (cfg);
14745 if (locals_var->opcode == OP_REGOFFSET) {
14746 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14747 } else if (locals_var->opcode == OP_REGVAR) {
14748 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14750 g_assert_not_reached ();
14752 /* Compute the address */
14753 ins->opcode = OP_PADD;
14757 mono_bblock_insert_before_ins (bb, ins, load3);
14758 mono_bblock_insert_before_ins (bb, load3, load2);
14760 mono_bblock_insert_before_ins (bb, load2, load);
14762 g_assert (var->opcode == OP_REGOFFSET);
14764 ins->opcode = OP_ADD_IMM;
14765 ins->sreg1 = var->inst_basereg;
14766 ins->inst_imm = var->inst_offset;
14769 *need_local_opts = TRUE;
14770 spec = INS_INFO (ins->opcode);
14773 if (ins->opcode < MONO_CEE_LAST) {
14774 mono_print_ins (ins);
14775 g_assert_not_reached ();
14779 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14783 if (MONO_IS_STORE_MEMBASE (ins)) {
14784 tmp_reg = ins->dreg;
14785 ins->dreg = ins->sreg2;
14786 ins->sreg2 = tmp_reg;
14789 spec2 [MONO_INST_DEST] = ' ';
14790 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14791 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14792 spec2 [MONO_INST_SRC3] = ' ';
14794 } else if (MONO_IS_STORE_MEMINDEX (ins))
14795 g_assert_not_reached ();
14800 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14801 printf ("\t %.3s %d", spec, ins->dreg);
14802 num_sregs = mono_inst_get_src_registers (ins, sregs);
14803 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14804 printf (" %d", sregs [srcindex]);
14811 regtype = spec [MONO_INST_DEST];
14812 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14815 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14816 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14817 MonoInst *store_ins;
14819 MonoInst *def_ins = ins;
14820 int dreg = ins->dreg; /* The original vreg */
14822 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14824 if (var->opcode == OP_REGVAR) {
14825 ins->dreg = var->dreg;
14826 } else if ((ins->dreg == ins->sreg1) && (spec [MONO_INST_DEST] == 'i') && (spec [MONO_INST_SRC1] == 'i') && !vreg_to_lvreg [ins->dreg] && (op_to_op_dest_membase (store_opcode, ins->opcode) != -1)) {
14828 * Instead of emitting a load+store, use a _membase opcode.
14830 g_assert (var->opcode == OP_REGOFFSET);
14831 if (ins->opcode == OP_MOVE) {
14835 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14836 ins->inst_basereg = var->inst_basereg;
14837 ins->inst_offset = var->inst_offset;
14840 spec = INS_INFO (ins->opcode);
14844 g_assert (var->opcode == OP_REGOFFSET);
14846 prev_dreg = ins->dreg;
14848 /* Invalidate any previous lvreg for this vreg */
14849 vreg_to_lvreg [ins->dreg] = 0;
14853 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14855 store_opcode = OP_STOREI8_MEMBASE_REG;
14858 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14860 #if SIZEOF_REGISTER != 8
14861 if (regtype == 'l') {
14862 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, MONO_LVREG_LS (ins->dreg));
14863 mono_bblock_insert_after_ins (bb, ins, store_ins);
14864 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, MONO_LVREG_MS (ins->dreg));
14865 mono_bblock_insert_after_ins (bb, ins, store_ins);
14866 def_ins = store_ins;
14871 g_assert (store_opcode != OP_STOREV_MEMBASE);
14873 /* Try to fuse the store into the instruction itself */
14874 /* FIXME: Add more instructions */
14875 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14876 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14877 ins->inst_imm = ins->inst_c0;
14878 ins->inst_destbasereg = var->inst_basereg;
14879 ins->inst_offset = var->inst_offset;
14880 spec = INS_INFO (ins->opcode);
14881 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14882 ins->opcode = store_opcode;
14883 ins->inst_destbasereg = var->inst_basereg;
14884 ins->inst_offset = var->inst_offset;
14888 tmp_reg = ins->dreg;
14889 ins->dreg = ins->sreg2;
14890 ins->sreg2 = tmp_reg;
14893 spec2 [MONO_INST_DEST] = ' ';
14894 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14895 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14896 spec2 [MONO_INST_SRC3] = ' ';
14898 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14899 // FIXME: The backends expect the base reg to be in inst_basereg
14900 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14902 ins->inst_basereg = var->inst_basereg;
14903 ins->inst_offset = var->inst_offset;
14904 spec = INS_INFO (ins->opcode);
14906 /* printf ("INS: "); mono_print_ins (ins); */
14907 /* Create a store instruction */
14908 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14910 /* Insert it after the instruction */
14911 mono_bblock_insert_after_ins (bb, ins, store_ins);
14913 def_ins = store_ins;
14916 * We can't assign ins->dreg to var->dreg here, since the
14917 * sregs could use it. So set a flag, and do it after
14920 if ((!cfg->backend->use_fpstack || ((store_opcode != OP_STORER8_MEMBASE_REG) && (store_opcode != OP_STORER4_MEMBASE_REG))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
14921 dest_has_lvreg = TRUE;
14926 if (def_ins && !live_range_start [dreg]) {
14927 live_range_start [dreg] = def_ins;
14928 live_range_start_bb [dreg] = bb;
14931 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14934 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14935 tmp->inst_c1 = dreg;
14936 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14943 num_sregs = mono_inst_get_src_registers (ins, sregs);
14944 for (srcindex = 0; srcindex < 3; ++srcindex) {
14945 regtype = spec [MONO_INST_SRC1 + srcindex];
14946 sreg = sregs [srcindex];
14948 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14949 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14950 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14951 MonoInst *use_ins = ins;
14952 MonoInst *load_ins;
14953 guint32 load_opcode;
14955 if (var->opcode == OP_REGVAR) {
14956 sregs [srcindex] = var->dreg;
14957 //mono_inst_set_src_registers (ins, sregs);
14958 live_range_end [sreg] = use_ins;
14959 live_range_end_bb [sreg] = bb;
14961 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14964 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14965 /* var->dreg is a hreg */
14966 tmp->inst_c1 = sreg;
14967 mono_bblock_insert_after_ins (bb, ins, tmp);
14973 g_assert (var->opcode == OP_REGOFFSET);
14975 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14977 g_assert (load_opcode != OP_LOADV_MEMBASE);
14979 if (vreg_to_lvreg [sreg]) {
14980 g_assert (vreg_to_lvreg [sreg] != -1);
14982 /* The variable is already loaded to an lvreg */
14983 if (G_UNLIKELY (cfg->verbose_level > 2))
14984 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14985 sregs [srcindex] = vreg_to_lvreg [sreg];
14986 //mono_inst_set_src_registers (ins, sregs);
14990 /* Try to fuse the load into the instruction */
14991 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14992 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14993 sregs [0] = var->inst_basereg;
14994 //mono_inst_set_src_registers (ins, sregs);
14995 ins->inst_offset = var->inst_offset;
14996 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14997 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14998 sregs [1] = var->inst_basereg;
14999 //mono_inst_set_src_registers (ins, sregs);
15000 ins->inst_offset = var->inst_offset;
15002 if (MONO_IS_REAL_MOVE (ins)) {
15003 ins->opcode = OP_NOP;
15006 //printf ("%d ", srcindex); mono_print_ins (ins);
15008 sreg = alloc_dreg (cfg, stacktypes [regtype]);
15010 if ((!cfg->backend->use_fpstack || ((load_opcode != OP_LOADR8_MEMBASE) && (load_opcode != OP_LOADR4_MEMBASE))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && !no_lvreg) {
15011 if (var->dreg == prev_dreg) {
15013 * sreg refers to the value loaded by the load
15014 * emitted below, but we need to use ins->dreg
15015 * since it refers to the store emitted earlier.
15019 g_assert (sreg != -1);
15020 vreg_to_lvreg [var->dreg] = sreg;
15021 g_assert (lvregs_len < 1024);
15022 lvregs [lvregs_len ++] = var->dreg;
15026 sregs [srcindex] = sreg;
15027 //mono_inst_set_src_registers (ins, sregs);
15029 #if SIZEOF_REGISTER != 8
15030 if (regtype == 'l') {
15031 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
15032 mono_bblock_insert_before_ins (bb, ins, load_ins);
15033 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
15034 mono_bblock_insert_before_ins (bb, ins, load_ins);
15035 use_ins = load_ins;
15040 #if SIZEOF_REGISTER == 4
15041 g_assert (load_opcode != OP_LOADI8_MEMBASE);
15043 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
15044 mono_bblock_insert_before_ins (bb, ins, load_ins);
15045 use_ins = load_ins;
15049 if (var->dreg < orig_next_vreg) {
15050 live_range_end [var->dreg] = use_ins;
15051 live_range_end_bb [var->dreg] = bb;
15054 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
15057 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
15058 tmp->inst_c1 = var->dreg;
15059 mono_bblock_insert_after_ins (bb, ins, tmp);
15063 mono_inst_set_src_registers (ins, sregs);
15065 if (dest_has_lvreg) {
15066 g_assert (ins->dreg != -1);
15067 vreg_to_lvreg [prev_dreg] = ins->dreg;
15068 g_assert (lvregs_len < 1024);
15069 lvregs [lvregs_len ++] = prev_dreg;
15070 dest_has_lvreg = FALSE;
15074 tmp_reg = ins->dreg;
15075 ins->dreg = ins->sreg2;
15076 ins->sreg2 = tmp_reg;
15079 if (MONO_IS_CALL (ins)) {
15080 /* Clear vreg_to_lvreg array */
15081 for (i = 0; i < lvregs_len; i++)
15082 vreg_to_lvreg [lvregs [i]] = 0;
15084 } else if (ins->opcode == OP_NOP) {
15086 MONO_INST_NULLIFY_SREGS (ins);
15089 if (cfg->verbose_level > 2)
15090 mono_print_ins_index (1, ins);
15093 /* Extend the live range based on the liveness info */
15094 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
15095 for (i = 0; i < cfg->num_varinfo; i ++) {
15096 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
15098 if (vreg_is_volatile (cfg, vi->vreg))
15099 /* The liveness info is incomplete */
15102 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
15103 /* Live from at least the first ins of this bb */
15104 live_range_start [vi->vreg] = bb->code;
15105 live_range_start_bb [vi->vreg] = bb;
15108 if (mono_bitset_test_fast (bb->live_out_set, i)) {
15109 /* Live at least until the last ins of this bb */
15110 live_range_end [vi->vreg] = bb->last_ins;
15111 live_range_end_bb [vi->vreg] = bb;
15118 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
15119 * by storing the current native offset into MonoMethodVar->live_range_start/end.
15121 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
15122 for (i = 0; i < cfg->num_varinfo; ++i) {
15123 int vreg = MONO_VARINFO (cfg, i)->vreg;
15126 if (live_range_start [vreg]) {
15127 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
15129 ins->inst_c1 = vreg;
15130 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
15132 if (live_range_end [vreg]) {
15133 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
15135 ins->inst_c1 = vreg;
15136 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
15137 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
15139 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
15144 if (cfg->gsharedvt_locals_var_ins) {
15145 /* Nullify if unused */
15146 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
15147 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
15150 g_free (live_range_start);
15151 g_free (live_range_end);
15152 g_free (live_range_start_bb);
15153 g_free (live_range_end_bb);
15157 mono_decompose_typecheck (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
15159 MonoInst *ret, *move, *source;
15160 MonoClass *klass = ins->klass;
15161 int context_used = mini_class_check_context_used (cfg, klass);
15162 int is_isinst = ins->opcode == OP_ISINST;
15163 g_assert (is_isinst || ins->opcode == OP_CASTCLASS);
15164 source = get_vreg_to_inst (cfg, ins->sreg1);
15165 if (!source || source == (MonoInst *) -1)
15166 source = mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, ins->sreg1);
15167 g_assert (source && source != (MonoInst *) -1);
15169 MonoBasicBlock *first_bb;
15170 NEW_BBLOCK (cfg, first_bb);
15171 cfg->cbb = first_bb;
15173 if (!context_used && (mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || klass->is_array_special_interface)) {
15175 ret = emit_isinst_with_cache_nonshared (cfg, source, klass);
15177 ret = emit_castclass_with_cache_nonshared (cfg, source, klass);
15178 } else if (!context_used && (mono_class_is_marshalbyref (klass) || mono_class_is_interface (klass))) {
15179 MonoInst *iargs [1];
15182 iargs [0] = source;
15184 MonoMethod *wrapper = mono_marshal_get_isinst (klass);
15185 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), iargs, 0, 0, TRUE);
15187 MonoMethod *wrapper = mono_marshal_get_castclass (klass);
15188 save_cast_details (cfg, klass, source->dreg, TRUE);
15189 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), iargs, 0, 0, TRUE);
15190 reset_cast_details (cfg);
15192 g_assert (costs > 0);
15196 ret = handle_isinst (cfg, klass, source, context_used);
15198 ret = handle_castclass (cfg, klass, source, context_used);
15200 EMIT_NEW_UNALU (cfg, move, OP_MOVE, ins->dreg, ret->dreg);
15202 g_assert (cfg->cbb->code || first_bb->code);
15203 MonoInst *prev = ins->prev;
15204 mono_replace_ins (cfg, bb, ins, &prev, first_bb, cfg->cbb);
15208 mono_decompose_typechecks (MonoCompile *cfg)
15210 for (MonoBasicBlock *bb = cfg->bb_entry; bb; bb = bb->next_bb) {
15212 MONO_BB_FOR_EACH_INS (bb, ins) {
15213 switch (ins->opcode) {
15216 mono_decompose_typecheck (cfg, bb, ins);
15226 * - use 'iadd' instead of 'int_add'
15227 * - handling ovf opcodes: decompose in method_to_ir.
15228 * - unify iregs/fregs
15229 * -> partly done, the missing parts are:
15230 * - a more complete unification would involve unifying the hregs as well, so
15231 * code wouldn't need if (fp) all over the place. but that would mean the hregs
15232 * would no longer map to the machine hregs, so the code generators would need to
15233 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
15234 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
15235 * fp/non-fp branches speeds it up by about 15%.
15236 * - use sext/zext opcodes instead of shifts
15238 * - get rid of TEMPLOADs if possible and use vregs instead
15239 * - clean up usage of OP_P/OP_ opcodes
15240 * - cleanup usage of DUMMY_USE
15241 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
15243 * - set the stack type and allocate a dreg in the EMIT_NEW macros
15244 * - get rid of all the <foo>2 stuff when the new JIT is ready.
15245 * - make sure handle_stack_args () is called before the branch is emitted
15246 * - when the new IR is done, get rid of all unused stuff
15247 * - COMPARE/BEQ as separate instructions or unify them ?
15248 * - keeping them separate allows specialized compare instructions like
15249 * compare_imm, compare_membase
15250 * - most back ends unify fp compare+branch, fp compare+ceq
15251 * - integrate mono_save_args into inline_method
15252 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
15253 * - handle long shift opts on 32 bit platforms somehow: they require
15254 * 3 sregs (2 for arg1 and 1 for arg2)
15255 * - make byref a 'normal' type.
15256 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
15257 * variable if needed.
15258 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
15259 * like inline_method.
15260 * - remove inlining restrictions
15261 * - fix LNEG and enable cfold of INEG
15262 * - generalize x86 optimizations like ldelema as a peephole optimization
15263 * - add store_mem_imm for amd64
15264 * - optimize the loading of the interruption flag in the managed->native wrappers
15265 * - avoid special handling of OP_NOP in passes
15266 * - move code inserting instructions into one function/macro.
15267 * - try a coalescing phase after liveness analysis
15268 * - add float -> vreg conversion + local optimizations on !x86
15269 * - figure out how to handle decomposed branches during optimizations, ie.
15270 * compare+branch, op_jump_table+op_br etc.
15271 * - promote RuntimeXHandles to vregs
15272 * - vtype cleanups:
15273 * - add a NEW_VARLOADA_VREG macro
15274 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
15275 * accessing vtype fields.
15276 * - get rid of I8CONST on 64 bit platforms
15277 * - dealing with the increase in code size due to branches created during opcode
15279 * - use extended basic blocks
15280 * - all parts of the JIT
15281 * - handle_global_vregs () && local regalloc
15282 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
15283 * - sources of increase in code size:
15286 * - isinst and castclass
15287 * - lvregs not allocated to global registers even if used multiple times
15288 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
15290 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
15291 * - add all micro optimizations from the old JIT
15292 * - put tree optimizations into the deadce pass
15293 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
15294 * specific function.
15295 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
15296 * fcompare + branchCC.
15297 * - create a helper function for allocating a stack slot, taking into account
15298 * MONO_CFG_HAS_SPILLUP.
15300 * - merge the ia64 switch changes.
15301 * - optimize mono_regstate2_alloc_int/float.
15302 * - fix the pessimistic handling of variables accessed in exception handler blocks.
15303 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
15304 * parts of the tree could be separated by other instructions, killing the tree
15305 * arguments, or stores killing loads etc. Also, should we fold loads into other
15306 * instructions if the result of the load is used multiple times ?
15307 * - make the REM_IMM optimization in mini-x86.c arch-independent.
15308 * - LAST MERGE: 108395.
15309 * - when returning vtypes in registers, generate IR and append it to the end of the
15310 * last bb instead of doing it in the epilog.
15311 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
15319 - When to decompose opcodes:
15320 - earlier: this makes some optimizations hard to implement, since the low level IR
15321 no longer contains the neccessary information. But it is easier to do.
15322 - later: harder to implement, enables more optimizations.
15323 - Branches inside bblocks:
15324 - created when decomposing complex opcodes.
15325 - branches to another bblock: harmless, but not tracked by the branch
15326 optimizations, so need to branch to a label at the start of the bblock.
15327 - branches to inside the same bblock: very problematic, trips up the local
15328 reg allocator. Can be fixed by spitting the current bblock, but that is a
15329 complex operation, since some local vregs can become global vregs etc.
15330 - Local/global vregs:
15331 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
15332 local register allocator.
15333 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
15334 structure, created by mono_create_var (). Assigned to hregs or the stack by
15335 the global register allocator.
15336 - When to do optimizations like alu->alu_imm:
15337 - earlier -> saves work later on since the IR will be smaller/simpler
15338 - later -> can work on more instructions
15339 - Handling of valuetypes:
15340 - When a vtype is pushed on the stack, a new temporary is created, an
15341 instruction computing its address (LDADDR) is emitted and pushed on
15342 the stack. Need to optimize cases when the vtype is used immediately as in
15343 argument passing, stloc etc.
15344 - Instead of the to_end stuff in the old JIT, simply call the function handling
15345 the values on the stack before emitting the last instruction of the bb.
15348 #else /* !DISABLE_JIT */
15350 MONO_EMPTY_SOURCE_FILE (method_to_ir);
15352 #endif /* !DISABLE_JIT */