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)
27 #ifdef HAVE_SYS_TIME_H
35 #include <mono/utils/memcheck.h>
37 #include <mono/metadata/abi-details.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/attrdefs.h>
40 #include <mono/metadata/loader.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/class.h>
43 #include <mono/metadata/object.h>
44 #include <mono/metadata/exception.h>
45 #include <mono/metadata/opcodes.h>
46 #include <mono/metadata/mono-endian.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/debug-helpers.h>
51 #include <mono/metadata/mono-debug.h>
52 #include <mono/metadata/mono-debug-debugger.h>
53 #include <mono/metadata/gc-internals.h>
54 #include <mono/metadata/security-manager.h>
55 #include <mono/metadata/threads-types.h>
56 #include <mono/metadata/security-core-clr.h>
57 #include <mono/metadata/profiler-private.h>
58 #include <mono/metadata/profiler.h>
59 #include <mono/metadata/debug-mono-symfile.h>
60 #include <mono/utils/mono-compiler.h>
61 #include <mono/utils/mono-memory-model.h>
62 #include <mono/metadata/mono-basic-block.h>
63 #include <mono/metadata/reflection-internals.h>
69 #include "jit-icalls.h"
71 #include "debugger-agent.h"
72 #include "seq-points.h"
73 #include "aot-compiler.h"
74 #include "mini-llvm.h"
76 #define BRANCH_COST 10
77 #define INLINE_LENGTH_LIMIT 20
79 /* These have 'cfg' as an implicit argument */
80 #define INLINE_FAILURE(msg) do { \
81 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
82 inline_failure (cfg, msg); \
83 goto exception_exit; \
86 #define CHECK_CFG_EXCEPTION do {\
87 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
88 goto exception_exit; \
90 #define METHOD_ACCESS_FAILURE(method, cmethod) do { \
91 method_access_failure ((cfg), (method), (cmethod)); \
92 goto exception_exit; \
94 #define FIELD_ACCESS_FAILURE(method, field) do { \
95 field_access_failure ((cfg), (method), (field)); \
96 goto exception_exit; \
98 #define GENERIC_SHARING_FAILURE(opcode) do { \
100 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
101 goto exception_exit; \
104 #define GSHAREDVT_FAILURE(opcode) do { \
105 if (cfg->gsharedvt) { \
106 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
107 goto exception_exit; \
110 #define OUT_OF_MEMORY_FAILURE do { \
111 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
112 mono_error_set_out_of_memory (&cfg->error, ""); \
113 goto exception_exit; \
115 #define DISABLE_AOT(cfg) do { \
116 if ((cfg)->verbose_level >= 2) \
117 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
118 (cfg)->disable_aot = TRUE; \
120 #define LOAD_ERROR do { \
121 break_on_unverified (); \
122 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
123 goto exception_exit; \
126 #define TYPE_LOAD_ERROR(klass) do { \
127 cfg->exception_ptr = klass; \
131 #define CHECK_CFG_ERROR do {\
132 if (!mono_error_ok (&cfg->error)) { \
133 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
134 goto mono_error_exit; \
138 /* Determine whenever 'ins' represents a load of the 'this' argument */
139 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
141 static int ldind_to_load_membase (int opcode);
142 static int stind_to_store_membase (int opcode);
144 int mono_op_to_op_imm (int opcode);
145 int mono_op_to_op_imm_noemul (int opcode);
147 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
149 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
150 guchar *ip, guint real_offset, gboolean inline_always);
152 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
154 /* helper methods signatures */
155 static MonoMethodSignature *helper_sig_domain_get;
156 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
157 static MonoMethodSignature *helper_sig_llvmonly_imt_thunk;
160 * Instruction metadata
168 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
169 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
175 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
180 /* keep in sync with the enum in mini.h */
183 #include "mini-ops.h"
188 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
189 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
191 * This should contain the index of the last sreg + 1. This is not the same
192 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
194 const gint8 ins_sreg_counts[] = {
195 #include "mini-ops.h"
200 #define MONO_INIT_VARINFO(vi,id) do { \
201 (vi)->range.first_use.pos.bid = 0xffff; \
207 mono_alloc_ireg (MonoCompile *cfg)
209 return alloc_ireg (cfg);
213 mono_alloc_lreg (MonoCompile *cfg)
215 return alloc_lreg (cfg);
219 mono_alloc_freg (MonoCompile *cfg)
221 return alloc_freg (cfg);
225 mono_alloc_preg (MonoCompile *cfg)
227 return alloc_preg (cfg);
231 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
233 return alloc_dreg (cfg, stack_type);
237 * mono_alloc_ireg_ref:
239 * Allocate an IREG, and mark it as holding a GC ref.
242 mono_alloc_ireg_ref (MonoCompile *cfg)
244 return alloc_ireg_ref (cfg);
248 * mono_alloc_ireg_mp:
250 * Allocate an IREG, and mark it as holding a managed pointer.
253 mono_alloc_ireg_mp (MonoCompile *cfg)
255 return alloc_ireg_mp (cfg);
259 * mono_alloc_ireg_copy:
261 * Allocate an IREG with the same GC type as VREG.
264 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
266 if (vreg_is_ref (cfg, vreg))
267 return alloc_ireg_ref (cfg);
268 else if (vreg_is_mp (cfg, vreg))
269 return alloc_ireg_mp (cfg);
271 return alloc_ireg (cfg);
275 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
280 type = mini_get_underlying_type (type);
282 switch (type->type) {
295 case MONO_TYPE_FNPTR:
297 case MONO_TYPE_CLASS:
298 case MONO_TYPE_STRING:
299 case MONO_TYPE_OBJECT:
300 case MONO_TYPE_SZARRAY:
301 case MONO_TYPE_ARRAY:
305 #if SIZEOF_REGISTER == 8
311 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
314 case MONO_TYPE_VALUETYPE:
315 if (type->data.klass->enumtype) {
316 type = mono_class_enum_basetype (type->data.klass);
319 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
322 case MONO_TYPE_TYPEDBYREF:
324 case MONO_TYPE_GENERICINST:
325 type = &type->data.generic_class->container_class->byval_arg;
329 g_assert (cfg->gshared);
330 if (mini_type_var_is_vt (type))
333 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
335 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
341 mono_print_bb (MonoBasicBlock *bb, const char *msg)
346 printf ("\n%s %d: [IN: ", msg, bb->block_num);
347 for (i = 0; i < bb->in_count; ++i)
348 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
350 for (i = 0; i < bb->out_count; ++i)
351 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
353 for (tree = bb->code; tree; tree = tree->next)
354 mono_print_ins_index (-1, tree);
358 mono_create_helper_signatures (void)
360 helper_sig_domain_get = mono_create_icall_signature ("ptr");
361 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
362 helper_sig_llvmonly_imt_thunk = mono_create_icall_signature ("ptr ptr ptr");
365 static MONO_NEVER_INLINE void
366 break_on_unverified (void)
368 if (mini_get_debug_options ()->break_on_unverified)
372 static MONO_NEVER_INLINE void
373 method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
375 char *method_fname = mono_method_full_name (method, TRUE);
376 char *cil_method_fname = mono_method_full_name (cil_method, TRUE);
377 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
378 mono_error_set_generic_error (&cfg->error, "System", "MethodAccessException", "Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);
379 g_free (method_fname);
380 g_free (cil_method_fname);
383 static MONO_NEVER_INLINE void
384 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
386 char *method_fname = mono_method_full_name (method, TRUE);
387 char *field_fname = mono_field_full_name (field);
388 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
389 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
390 g_free (method_fname);
391 g_free (field_fname);
394 static MONO_NEVER_INLINE void
395 inline_failure (MonoCompile *cfg, const char *msg)
397 if (cfg->verbose_level >= 2)
398 printf ("inline failed: %s\n", msg);
399 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
402 static MONO_NEVER_INLINE void
403 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
405 if (cfg->verbose_level > 2) \
406 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);
407 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
410 static MONO_NEVER_INLINE void
411 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
413 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);
414 if (cfg->verbose_level >= 2)
415 printf ("%s\n", cfg->exception_message);
416 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
420 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
421 * foo<T> (int i) { ldarg.0; box T; }
423 #define UNVERIFIED do { \
424 if (cfg->gsharedvt) { \
425 if (cfg->verbose_level > 2) \
426 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
427 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
428 goto exception_exit; \
430 break_on_unverified (); \
434 #define GET_BBLOCK(cfg,tblock,ip) do { \
435 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
437 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
438 NEW_BBLOCK (cfg, (tblock)); \
439 (tblock)->cil_code = (ip); \
440 ADD_BBLOCK (cfg, (tblock)); \
444 #if defined(TARGET_X86) || defined(TARGET_AMD64)
445 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
446 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
447 (dest)->dreg = alloc_ireg_mp ((cfg)); \
448 (dest)->sreg1 = (sr1); \
449 (dest)->sreg2 = (sr2); \
450 (dest)->inst_imm = (imm); \
451 (dest)->backend.shift_amount = (shift); \
452 MONO_ADD_INS ((cfg)->cbb, (dest)); \
456 /* Emit conversions so both operands of a binary opcode are of the same type */
458 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
460 MonoInst *arg1 = *arg1_ref;
461 MonoInst *arg2 = *arg2_ref;
464 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
465 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
468 /* Mixing r4/r8 is allowed by the spec */
469 if (arg1->type == STACK_R4) {
470 int dreg = alloc_freg (cfg);
472 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
473 conv->type = STACK_R8;
477 if (arg2->type == STACK_R4) {
478 int dreg = alloc_freg (cfg);
480 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
481 conv->type = STACK_R8;
487 #if SIZEOF_REGISTER == 8
488 /* FIXME: Need to add many more cases */
489 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
492 int dr = alloc_preg (cfg);
493 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
494 (ins)->sreg2 = widen->dreg;
499 #define ADD_BINOP(op) do { \
500 MONO_INST_NEW (cfg, ins, (op)); \
502 ins->sreg1 = sp [0]->dreg; \
503 ins->sreg2 = sp [1]->dreg; \
504 type_from_op (cfg, ins, sp [0], sp [1]); \
506 /* Have to insert a widening op */ \
507 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
508 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
509 MONO_ADD_INS ((cfg)->cbb, (ins)); \
510 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
513 #define ADD_UNOP(op) do { \
514 MONO_INST_NEW (cfg, ins, (op)); \
516 ins->sreg1 = sp [0]->dreg; \
517 type_from_op (cfg, ins, sp [0], NULL); \
519 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
520 MONO_ADD_INS ((cfg)->cbb, (ins)); \
521 *sp++ = mono_decompose_opcode (cfg, ins); \
524 #define ADD_BINCOND(next_block) do { \
527 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
528 cmp->sreg1 = sp [0]->dreg; \
529 cmp->sreg2 = sp [1]->dreg; \
530 type_from_op (cfg, cmp, sp [0], sp [1]); \
532 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
533 type_from_op (cfg, ins, sp [0], sp [1]); \
534 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
535 GET_BBLOCK (cfg, tblock, target); \
536 link_bblock (cfg, cfg->cbb, tblock); \
537 ins->inst_true_bb = tblock; \
538 if ((next_block)) { \
539 link_bblock (cfg, cfg->cbb, (next_block)); \
540 ins->inst_false_bb = (next_block); \
541 start_new_bblock = 1; \
543 GET_BBLOCK (cfg, tblock, ip); \
544 link_bblock (cfg, cfg->cbb, tblock); \
545 ins->inst_false_bb = tblock; \
546 start_new_bblock = 2; \
548 if (sp != stack_start) { \
549 handle_stack_args (cfg, stack_start, sp - stack_start); \
550 CHECK_UNVERIFIABLE (cfg); \
552 MONO_ADD_INS (cfg->cbb, cmp); \
553 MONO_ADD_INS (cfg->cbb, ins); \
557 * link_bblock: Links two basic blocks
559 * links two basic blocks in the control flow graph, the 'from'
560 * argument is the starting block and the 'to' argument is the block
561 * the control flow ends to after 'from'.
564 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
566 MonoBasicBlock **newa;
570 if (from->cil_code) {
572 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
574 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
577 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
579 printf ("edge from entry to exit\n");
584 for (i = 0; i < from->out_count; ++i) {
585 if (to == from->out_bb [i]) {
591 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
592 for (i = 0; i < from->out_count; ++i) {
593 newa [i] = from->out_bb [i];
601 for (i = 0; i < to->in_count; ++i) {
602 if (from == to->in_bb [i]) {
608 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
609 for (i = 0; i < to->in_count; ++i) {
610 newa [i] = to->in_bb [i];
619 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
621 link_bblock (cfg, from, to);
625 * mono_find_block_region:
627 * We mark each basic block with a region ID. We use that to avoid BB
628 * optimizations when blocks are in different regions.
631 * A region token that encodes where this region is, and information
632 * about the clause owner for this block.
634 * The region encodes the try/catch/filter clause that owns this block
635 * as well as the type. -1 is a special value that represents a block
636 * that is in none of try/catch/filter.
639 mono_find_block_region (MonoCompile *cfg, int offset)
641 MonoMethodHeader *header = cfg->header;
642 MonoExceptionClause *clause;
645 for (i = 0; i < header->num_clauses; ++i) {
646 clause = &header->clauses [i];
647 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
648 (offset < (clause->handler_offset)))
649 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
651 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
652 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
653 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
654 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
655 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
657 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
660 for (i = 0; i < header->num_clauses; ++i) {
661 clause = &header->clauses [i];
663 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
664 return ((i + 1) << 8) | clause->flags;
671 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
673 MonoMethodHeader *header = cfg->header;
674 MonoExceptionClause *clause;
678 for (i = 0; i < header->num_clauses; ++i) {
679 clause = &header->clauses [i];
680 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
681 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
682 if (clause->flags == type)
683 res = g_list_append (res, clause);
690 mono_create_spvar_for_region (MonoCompile *cfg, int region)
694 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
698 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
699 /* prevent it from being register allocated */
700 var->flags |= MONO_INST_VOLATILE;
702 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
706 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
708 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
712 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
716 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
720 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
721 /* prevent it from being register allocated */
722 var->flags |= MONO_INST_VOLATILE;
724 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
730 * Returns the type used in the eval stack when @type is loaded.
731 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
734 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
738 type = mini_get_underlying_type (type);
739 inst->klass = klass = mono_class_from_mono_type (type);
741 inst->type = STACK_MP;
746 switch (type->type) {
748 inst->type = STACK_INV;
756 inst->type = STACK_I4;
761 case MONO_TYPE_FNPTR:
762 inst->type = STACK_PTR;
764 case MONO_TYPE_CLASS:
765 case MONO_TYPE_STRING:
766 case MONO_TYPE_OBJECT:
767 case MONO_TYPE_SZARRAY:
768 case MONO_TYPE_ARRAY:
769 inst->type = STACK_OBJ;
773 inst->type = STACK_I8;
776 inst->type = cfg->r4_stack_type;
779 inst->type = STACK_R8;
781 case MONO_TYPE_VALUETYPE:
782 if (type->data.klass->enumtype) {
783 type = mono_class_enum_basetype (type->data.klass);
787 inst->type = STACK_VTYPE;
790 case MONO_TYPE_TYPEDBYREF:
791 inst->klass = mono_defaults.typed_reference_class;
792 inst->type = STACK_VTYPE;
794 case MONO_TYPE_GENERICINST:
795 type = &type->data.generic_class->container_class->byval_arg;
799 g_assert (cfg->gshared);
800 if (mini_is_gsharedvt_type (type)) {
801 g_assert (cfg->gsharedvt);
802 inst->type = STACK_VTYPE;
804 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
808 g_error ("unknown type 0x%02x in eval stack type", type->type);
813 * The following tables are used to quickly validate the IL code in type_from_op ().
816 bin_num_table [STACK_MAX] [STACK_MAX] = {
817 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
818 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
819 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
820 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
821 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
822 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
823 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
824 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
825 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
830 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
833 /* reduce the size of this table */
835 bin_int_table [STACK_MAX] [STACK_MAX] = {
836 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
837 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
838 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
839 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, 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_INV, STACK_INV, STACK_INV, STACK_INV},
842 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
843 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
847 bin_comp_table [STACK_MAX] [STACK_MAX] = {
848 /* Inv i L p F & O vt r4 */
850 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
851 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
852 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
853 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
854 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
855 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
856 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
857 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
860 /* reduce the size of this table */
862 shift_table [STACK_MAX] [STACK_MAX] = {
863 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
864 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
865 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
866 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
867 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
868 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
869 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
870 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
874 * Tables to map from the non-specific opcode to the matching
875 * type-specific opcode.
877 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
879 binops_op_map [STACK_MAX] = {
880 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
883 /* handles from CEE_NEG to CEE_CONV_U8 */
885 unops_op_map [STACK_MAX] = {
886 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
889 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
891 ovfops_op_map [STACK_MAX] = {
892 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
895 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
897 ovf2ops_op_map [STACK_MAX] = {
898 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
901 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
903 ovf3ops_op_map [STACK_MAX] = {
904 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
907 /* handles from CEE_BEQ to CEE_BLT_UN */
909 beqops_op_map [STACK_MAX] = {
910 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
913 /* handles from CEE_CEQ to CEE_CLT_UN */
915 ceqops_op_map [STACK_MAX] = {
916 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
920 * Sets ins->type (the type on the eval stack) according to the
921 * type of the opcode and the arguments to it.
922 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
924 * FIXME: this function sets ins->type unconditionally in some cases, but
925 * it should set it to invalid for some types (a conv.x on an object)
928 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
930 switch (ins->opcode) {
937 /* FIXME: check unverifiable args for STACK_MP */
938 ins->type = bin_num_table [src1->type] [src2->type];
939 ins->opcode += binops_op_map [ins->type];
946 ins->type = bin_int_table [src1->type] [src2->type];
947 ins->opcode += binops_op_map [ins->type];
952 ins->type = shift_table [src1->type] [src2->type];
953 ins->opcode += binops_op_map [ins->type];
958 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
959 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
960 ins->opcode = OP_LCOMPARE;
961 else if (src1->type == STACK_R4)
962 ins->opcode = OP_RCOMPARE;
963 else if (src1->type == STACK_R8)
964 ins->opcode = OP_FCOMPARE;
966 ins->opcode = OP_ICOMPARE;
968 case OP_ICOMPARE_IMM:
969 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
970 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
971 ins->opcode = OP_LCOMPARE_IMM;
983 ins->opcode += beqops_op_map [src1->type];
986 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
987 ins->opcode += ceqops_op_map [src1->type];
993 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
994 ins->opcode += ceqops_op_map [src1->type];
998 ins->type = neg_table [src1->type];
999 ins->opcode += unops_op_map [ins->type];
1002 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1003 ins->type = src1->type;
1005 ins->type = STACK_INV;
1006 ins->opcode += unops_op_map [ins->type];
1012 ins->type = STACK_I4;
1013 ins->opcode += unops_op_map [src1->type];
1016 ins->type = STACK_R8;
1017 switch (src1->type) {
1020 ins->opcode = OP_ICONV_TO_R_UN;
1023 ins->opcode = OP_LCONV_TO_R_UN;
1027 case CEE_CONV_OVF_I1:
1028 case CEE_CONV_OVF_U1:
1029 case CEE_CONV_OVF_I2:
1030 case CEE_CONV_OVF_U2:
1031 case CEE_CONV_OVF_I4:
1032 case CEE_CONV_OVF_U4:
1033 ins->type = STACK_I4;
1034 ins->opcode += ovf3ops_op_map [src1->type];
1036 case CEE_CONV_OVF_I_UN:
1037 case CEE_CONV_OVF_U_UN:
1038 ins->type = STACK_PTR;
1039 ins->opcode += ovf2ops_op_map [src1->type];
1041 case CEE_CONV_OVF_I1_UN:
1042 case CEE_CONV_OVF_I2_UN:
1043 case CEE_CONV_OVF_I4_UN:
1044 case CEE_CONV_OVF_U1_UN:
1045 case CEE_CONV_OVF_U2_UN:
1046 case CEE_CONV_OVF_U4_UN:
1047 ins->type = STACK_I4;
1048 ins->opcode += ovf2ops_op_map [src1->type];
1051 ins->type = STACK_PTR;
1052 switch (src1->type) {
1054 ins->opcode = OP_ICONV_TO_U;
1058 #if SIZEOF_VOID_P == 8
1059 ins->opcode = OP_LCONV_TO_U;
1061 ins->opcode = OP_MOVE;
1065 ins->opcode = OP_LCONV_TO_U;
1068 ins->opcode = OP_FCONV_TO_U;
1074 ins->type = STACK_I8;
1075 ins->opcode += unops_op_map [src1->type];
1077 case CEE_CONV_OVF_I8:
1078 case CEE_CONV_OVF_U8:
1079 ins->type = STACK_I8;
1080 ins->opcode += ovf3ops_op_map [src1->type];
1082 case CEE_CONV_OVF_U8_UN:
1083 case CEE_CONV_OVF_I8_UN:
1084 ins->type = STACK_I8;
1085 ins->opcode += ovf2ops_op_map [src1->type];
1088 ins->type = cfg->r4_stack_type;
1089 ins->opcode += unops_op_map [src1->type];
1092 ins->type = STACK_R8;
1093 ins->opcode += unops_op_map [src1->type];
1096 ins->type = STACK_R8;
1100 ins->type = STACK_I4;
1101 ins->opcode += ovfops_op_map [src1->type];
1104 case CEE_CONV_OVF_I:
1105 case CEE_CONV_OVF_U:
1106 ins->type = STACK_PTR;
1107 ins->opcode += ovfops_op_map [src1->type];
1110 case CEE_ADD_OVF_UN:
1112 case CEE_MUL_OVF_UN:
1114 case CEE_SUB_OVF_UN:
1115 ins->type = bin_num_table [src1->type] [src2->type];
1116 ins->opcode += ovfops_op_map [src1->type];
1117 if (ins->type == STACK_R8)
1118 ins->type = STACK_INV;
1120 case OP_LOAD_MEMBASE:
1121 ins->type = STACK_PTR;
1123 case OP_LOADI1_MEMBASE:
1124 case OP_LOADU1_MEMBASE:
1125 case OP_LOADI2_MEMBASE:
1126 case OP_LOADU2_MEMBASE:
1127 case OP_LOADI4_MEMBASE:
1128 case OP_LOADU4_MEMBASE:
1129 ins->type = STACK_PTR;
1131 case OP_LOADI8_MEMBASE:
1132 ins->type = STACK_I8;
1134 case OP_LOADR4_MEMBASE:
1135 ins->type = cfg->r4_stack_type;
1137 case OP_LOADR8_MEMBASE:
1138 ins->type = STACK_R8;
1141 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1145 if (ins->type == STACK_MP)
1146 ins->klass = mono_defaults.object_class;
1151 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1157 param_table [STACK_MAX] [STACK_MAX] = {
1162 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1167 switch (args->type) {
1177 for (i = 0; i < sig->param_count; ++i) {
1178 switch (args [i].type) {
1182 if (!sig->params [i]->byref)
1186 if (sig->params [i]->byref)
1188 switch (sig->params [i]->type) {
1189 case MONO_TYPE_CLASS:
1190 case MONO_TYPE_STRING:
1191 case MONO_TYPE_OBJECT:
1192 case MONO_TYPE_SZARRAY:
1193 case MONO_TYPE_ARRAY:
1200 if (sig->params [i]->byref)
1202 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1211 /*if (!param_table [args [i].type] [sig->params [i]->type])
1219 * When we need a pointer to the current domain many times in a method, we
1220 * call mono_domain_get() once and we store the result in a local variable.
1221 * This function returns the variable that represents the MonoDomain*.
1223 inline static MonoInst *
1224 mono_get_domainvar (MonoCompile *cfg)
1226 if (!cfg->domainvar)
1227 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1228 return cfg->domainvar;
1232 * The got_var contains the address of the Global Offset Table when AOT
1236 mono_get_got_var (MonoCompile *cfg)
1238 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1240 if (!cfg->got_var) {
1241 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1243 return cfg->got_var;
1247 mono_get_vtable_var (MonoCompile *cfg)
1249 g_assert (cfg->gshared);
1251 if (!cfg->rgctx_var) {
1252 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1253 /* force the var to be stack allocated */
1254 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1257 return cfg->rgctx_var;
1261 type_from_stack_type (MonoInst *ins) {
1262 switch (ins->type) {
1263 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1264 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1265 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1266 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1267 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1269 return &ins->klass->this_arg;
1270 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1271 case STACK_VTYPE: return &ins->klass->byval_arg;
1273 g_error ("stack type %d to monotype not handled\n", ins->type);
1278 static G_GNUC_UNUSED int
1279 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1281 t = mono_type_get_underlying_type (t);
1293 case MONO_TYPE_FNPTR:
1295 case MONO_TYPE_CLASS:
1296 case MONO_TYPE_STRING:
1297 case MONO_TYPE_OBJECT:
1298 case MONO_TYPE_SZARRAY:
1299 case MONO_TYPE_ARRAY:
1305 return cfg->r4_stack_type;
1308 case MONO_TYPE_VALUETYPE:
1309 case MONO_TYPE_TYPEDBYREF:
1311 case MONO_TYPE_GENERICINST:
1312 if (mono_type_generic_inst_is_valuetype (t))
1318 g_assert_not_reached ();
1325 array_access_to_klass (int opcode)
1329 return mono_defaults.byte_class;
1331 return mono_defaults.uint16_class;
1334 return mono_defaults.int_class;
1337 return mono_defaults.sbyte_class;
1340 return mono_defaults.int16_class;
1343 return mono_defaults.int32_class;
1345 return mono_defaults.uint32_class;
1348 return mono_defaults.int64_class;
1351 return mono_defaults.single_class;
1354 return mono_defaults.double_class;
1355 case CEE_LDELEM_REF:
1356 case CEE_STELEM_REF:
1357 return mono_defaults.object_class;
1359 g_assert_not_reached ();
1365 * We try to share variables when possible
1368 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1373 /* inlining can result in deeper stacks */
1374 if (slot >= cfg->header->max_stack)
1375 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1377 pos = ins->type - 1 + slot * STACK_MAX;
1379 switch (ins->type) {
1386 if ((vnum = cfg->intvars [pos]))
1387 return cfg->varinfo [vnum];
1388 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1389 cfg->intvars [pos] = res->inst_c0;
1392 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1398 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1401 * Don't use this if a generic_context is set, since that means AOT can't
1402 * look up the method using just the image+token.
1403 * table == 0 means this is a reference made from a wrapper.
1405 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1406 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1407 jump_info_token->image = image;
1408 jump_info_token->token = token;
1409 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1414 * This function is called to handle items that are left on the evaluation stack
1415 * at basic block boundaries. What happens is that we save the values to local variables
1416 * and we reload them later when first entering the target basic block (with the
1417 * handle_loaded_temps () function).
1418 * A single joint point will use the same variables (stored in the array bb->out_stack or
1419 * bb->in_stack, if the basic block is before or after the joint point).
1421 * This function needs to be called _before_ emitting the last instruction of
1422 * the bb (i.e. before emitting a branch).
1423 * If the stack merge fails at a join point, cfg->unverifiable is set.
1426 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1429 MonoBasicBlock *bb = cfg->cbb;
1430 MonoBasicBlock *outb;
1431 MonoInst *inst, **locals;
1436 if (cfg->verbose_level > 3)
1437 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1438 if (!bb->out_scount) {
1439 bb->out_scount = count;
1440 //printf ("bblock %d has out:", bb->block_num);
1442 for (i = 0; i < bb->out_count; ++i) {
1443 outb = bb->out_bb [i];
1444 /* exception handlers are linked, but they should not be considered for stack args */
1445 if (outb->flags & BB_EXCEPTION_HANDLER)
1447 //printf (" %d", outb->block_num);
1448 if (outb->in_stack) {
1450 bb->out_stack = outb->in_stack;
1456 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1457 for (i = 0; i < count; ++i) {
1459 * try to reuse temps already allocated for this purpouse, if they occupy the same
1460 * stack slot and if they are of the same type.
1461 * This won't cause conflicts since if 'local' is used to
1462 * store one of the values in the in_stack of a bblock, then
1463 * the same variable will be used for the same outgoing stack
1465 * This doesn't work when inlining methods, since the bblocks
1466 * in the inlined methods do not inherit their in_stack from
1467 * the bblock they are inlined to. See bug #58863 for an
1470 if (cfg->inlined_method)
1471 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1473 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1478 for (i = 0; i < bb->out_count; ++i) {
1479 outb = bb->out_bb [i];
1480 /* exception handlers are linked, but they should not be considered for stack args */
1481 if (outb->flags & BB_EXCEPTION_HANDLER)
1483 if (outb->in_scount) {
1484 if (outb->in_scount != bb->out_scount) {
1485 cfg->unverifiable = TRUE;
1488 continue; /* check they are the same locals */
1490 outb->in_scount = count;
1491 outb->in_stack = bb->out_stack;
1494 locals = bb->out_stack;
1496 for (i = 0; i < count; ++i) {
1497 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1498 inst->cil_code = sp [i]->cil_code;
1499 sp [i] = locals [i];
1500 if (cfg->verbose_level > 3)
1501 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1505 * It is possible that the out bblocks already have in_stack assigned, and
1506 * the in_stacks differ. In this case, we will store to all the different
1513 /* Find a bblock which has a different in_stack */
1515 while (bindex < bb->out_count) {
1516 outb = bb->out_bb [bindex];
1517 /* exception handlers are linked, but they should not be considered for stack args */
1518 if (outb->flags & BB_EXCEPTION_HANDLER) {
1522 if (outb->in_stack != locals) {
1523 for (i = 0; i < count; ++i) {
1524 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1525 inst->cil_code = sp [i]->cil_code;
1526 sp [i] = locals [i];
1527 if (cfg->verbose_level > 3)
1528 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1530 locals = outb->in_stack;
1540 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1544 if (cfg->compile_aot) {
1545 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1550 ji.type = patch_type;
1551 ji.data.target = data;
1552 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE);
1554 EMIT_NEW_PCONST (cfg, ins, target);
1560 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1562 int ibitmap_reg = alloc_preg (cfg);
1563 #ifdef COMPRESSED_INTERFACE_BITMAP
1565 MonoInst *res, *ins;
1566 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1567 MONO_ADD_INS (cfg->cbb, ins);
1569 args [1] = emit_runtime_constant (cfg, MONO_PATCH_INFO_IID, klass);
1570 res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1571 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1573 int ibitmap_byte_reg = alloc_preg (cfg);
1575 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1577 if (cfg->compile_aot) {
1578 int iid_reg = alloc_preg (cfg);
1579 int shifted_iid_reg = alloc_preg (cfg);
1580 int ibitmap_byte_address_reg = alloc_preg (cfg);
1581 int masked_iid_reg = alloc_preg (cfg);
1582 int iid_one_bit_reg = alloc_preg (cfg);
1583 int iid_bit_reg = alloc_preg (cfg);
1584 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1585 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1586 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1587 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1588 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1589 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1590 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1591 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1593 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1594 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1600 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1601 * stored in "klass_reg" implements the interface "klass".
1604 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1606 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1610 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1611 * stored in "vtable_reg" implements the interface "klass".
1614 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1616 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1620 * Emit code which checks whenever the interface id of @klass is smaller than
1621 * than the value given by max_iid_reg.
1624 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1625 MonoBasicBlock *false_target)
1627 if (cfg->compile_aot) {
1628 int iid_reg = alloc_preg (cfg);
1629 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1630 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1633 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1635 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1637 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1640 /* Same as above, but obtains max_iid from a vtable */
1642 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1643 MonoBasicBlock *false_target)
1645 int max_iid_reg = alloc_preg (cfg);
1647 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1648 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1651 /* Same as above, but obtains max_iid from a klass */
1653 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1654 MonoBasicBlock *false_target)
1656 int max_iid_reg = alloc_preg (cfg);
1658 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1659 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1663 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1665 int idepth_reg = alloc_preg (cfg);
1666 int stypes_reg = alloc_preg (cfg);
1667 int stype = alloc_preg (cfg);
1669 mono_class_setup_supertypes (klass);
1671 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1672 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1673 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1674 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1676 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1677 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1679 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1680 } else if (cfg->compile_aot) {
1681 int const_reg = alloc_preg (cfg);
1682 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1683 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1685 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1687 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1691 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1693 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1697 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1699 int intf_reg = alloc_preg (cfg);
1701 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1702 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1703 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1705 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1707 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1711 * Variant of the above that takes a register to the class, not the vtable.
1714 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1716 int intf_bit_reg = alloc_preg (cfg);
1718 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1719 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1720 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1722 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1724 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1728 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1731 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1733 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
1734 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, ins->dreg);
1736 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1740 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1742 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1746 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1748 if (cfg->compile_aot) {
1749 int const_reg = alloc_preg (cfg);
1750 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1751 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1753 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1755 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1759 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1762 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1765 int rank_reg = alloc_preg (cfg);
1766 int eclass_reg = alloc_preg (cfg);
1768 g_assert (!klass_inst);
1769 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1770 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1771 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1772 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1773 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1774 if (klass->cast_class == mono_defaults.object_class) {
1775 int parent_reg = alloc_preg (cfg);
1776 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1777 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1778 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1779 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1780 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1781 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1782 } else if (klass->cast_class == mono_defaults.enum_class) {
1783 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1784 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1785 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1787 // Pass -1 as obj_reg to skip the check below for arrays of arrays
1788 mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1791 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1792 /* Check that the object is a vector too */
1793 int bounds_reg = alloc_preg (cfg);
1794 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1795 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1796 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1799 int idepth_reg = alloc_preg (cfg);
1800 int stypes_reg = alloc_preg (cfg);
1801 int stype = alloc_preg (cfg);
1803 mono_class_setup_supertypes (klass);
1805 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1806 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1807 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1808 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1810 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1811 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1812 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1817 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1819 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1823 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1827 g_assert (val == 0);
1832 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1835 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1838 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1841 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1843 #if SIZEOF_REGISTER == 8
1845 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1851 val_reg = alloc_preg (cfg);
1853 if (SIZEOF_REGISTER == 8)
1854 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1856 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1859 /* This could be optimized further if neccesary */
1861 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1868 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1870 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1875 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1882 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1887 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1892 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1899 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1906 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1907 g_assert (size < 10000);
1910 /* This could be optimized further if neccesary */
1912 cur_reg = alloc_preg (cfg);
1913 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1914 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1921 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1923 cur_reg = alloc_preg (cfg);
1924 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1925 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1933 cur_reg = alloc_preg (cfg);
1934 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1935 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1941 cur_reg = alloc_preg (cfg);
1942 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1943 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1949 cur_reg = alloc_preg (cfg);
1950 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1951 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1959 emit_tls_set (MonoCompile *cfg, int sreg1, MonoTlsKey tls_key)
1963 if (cfg->compile_aot) {
1964 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1965 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1967 ins->sreg2 = c->dreg;
1968 MONO_ADD_INS (cfg->cbb, ins);
1970 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1972 ins->inst_offset = mini_get_tls_offset (tls_key);
1973 MONO_ADD_INS (cfg->cbb, ins);
1980 * Emit IR to push the current LMF onto the LMF stack.
1983 emit_push_lmf (MonoCompile *cfg)
1986 * Emit IR to push the LMF:
1987 * lmf_addr = <lmf_addr from tls>
1988 * lmf->lmf_addr = lmf_addr
1989 * lmf->prev_lmf = *lmf_addr
1992 int lmf_reg, prev_lmf_reg;
1993 MonoInst *ins, *lmf_ins;
1998 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
1999 /* Load current lmf */
2000 lmf_ins = mono_get_lmf_intrinsic (cfg);
2002 MONO_ADD_INS (cfg->cbb, lmf_ins);
2003 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2004 lmf_reg = ins->dreg;
2005 /* Save previous_lmf */
2006 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
2008 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
2011 * Store lmf_addr in a variable, so it can be allocated to a global register.
2013 if (!cfg->lmf_addr_var)
2014 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2017 ins = mono_get_jit_tls_intrinsic (cfg);
2019 int jit_tls_dreg = ins->dreg;
2021 MONO_ADD_INS (cfg->cbb, ins);
2022 lmf_reg = alloc_preg (cfg);
2023 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2025 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2028 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2030 MONO_ADD_INS (cfg->cbb, lmf_ins);
2033 MonoInst *args [16], *jit_tls_ins, *ins;
2035 /* Inline mono_get_lmf_addr () */
2036 /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2038 /* Load mono_jit_tls_id */
2039 if (cfg->compile_aot)
2040 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2042 EMIT_NEW_ICONST (cfg, args [0], mono_jit_tls_id);
2043 /* call pthread_getspecific () */
2044 jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2045 /* lmf_addr = &jit_tls->lmf */
2046 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2049 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2053 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2055 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2056 lmf_reg = ins->dreg;
2058 prev_lmf_reg = alloc_preg (cfg);
2059 /* Save previous_lmf */
2060 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2061 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2063 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2070 * Emit IR to pop the current LMF from the LMF stack.
2073 emit_pop_lmf (MonoCompile *cfg)
2075 int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2081 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2082 lmf_reg = ins->dreg;
2084 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2085 /* Load previous_lmf */
2086 prev_lmf_reg = alloc_preg (cfg);
2087 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2089 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2092 * Emit IR to pop the LMF:
2093 * *(lmf->lmf_addr) = lmf->prev_lmf
2095 /* This could be called before emit_push_lmf () */
2096 if (!cfg->lmf_addr_var)
2097 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2098 lmf_addr_reg = cfg->lmf_addr_var->dreg;
2100 prev_lmf_reg = alloc_preg (cfg);
2101 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2102 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2107 emit_instrumentation_call (MonoCompile *cfg, void *func)
2109 MonoInst *iargs [1];
2112 * Avoid instrumenting inlined methods since it can
2113 * distort profiling results.
2115 if (cfg->method != cfg->current_method)
2118 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2119 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2120 mono_emit_jit_icall (cfg, func, iargs);
2125 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
2128 type = mini_get_underlying_type (type);
2129 switch (type->type) {
2130 case MONO_TYPE_VOID:
2131 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2138 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2142 case MONO_TYPE_FNPTR:
2143 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2144 case MONO_TYPE_CLASS:
2145 case MONO_TYPE_STRING:
2146 case MONO_TYPE_OBJECT:
2147 case MONO_TYPE_SZARRAY:
2148 case MONO_TYPE_ARRAY:
2149 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2152 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2155 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2157 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2159 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2160 case MONO_TYPE_VALUETYPE:
2161 if (type->data.klass->enumtype) {
2162 type = mono_class_enum_basetype (type->data.klass);
2165 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2166 case MONO_TYPE_TYPEDBYREF:
2167 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2168 case MONO_TYPE_GENERICINST:
2169 type = &type->data.generic_class->container_class->byval_arg;
2172 case MONO_TYPE_MVAR:
2174 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2176 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2182 * target_type_is_incompatible:
2183 * @cfg: MonoCompile context
2185 * Check that the item @arg on the evaluation stack can be stored
2186 * in the target type (can be a local, or field, etc).
2187 * The cfg arg can be used to check if we need verification or just
2190 * Returns: non-0 value if arg can't be stored on a target.
2193 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2195 MonoType *simple_type;
2198 if (target->byref) {
2199 /* FIXME: check that the pointed to types match */
2200 if (arg->type == STACK_MP) {
2201 MonoClass *base_class = mono_class_from_mono_type (target);
2202 /* This is needed to handle gshared types + ldaddr */
2203 simple_type = mini_get_underlying_type (&base_class->byval_arg);
2204 return target->type != MONO_TYPE_I && arg->klass != base_class && arg->klass != mono_class_from_mono_type (simple_type);
2206 if (arg->type == STACK_PTR)
2211 simple_type = mini_get_underlying_type (target);
2212 switch (simple_type->type) {
2213 case MONO_TYPE_VOID:
2221 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2225 /* STACK_MP is needed when setting pinned locals */
2226 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2231 case MONO_TYPE_FNPTR:
2233 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2234 * in native int. (#688008).
2236 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2239 case MONO_TYPE_CLASS:
2240 case MONO_TYPE_STRING:
2241 case MONO_TYPE_OBJECT:
2242 case MONO_TYPE_SZARRAY:
2243 case MONO_TYPE_ARRAY:
2244 if (arg->type != STACK_OBJ)
2246 /* FIXME: check type compatibility */
2250 if (arg->type != STACK_I8)
2254 if (arg->type != cfg->r4_stack_type)
2258 if (arg->type != STACK_R8)
2261 case MONO_TYPE_VALUETYPE:
2262 if (arg->type != STACK_VTYPE)
2264 klass = mono_class_from_mono_type (simple_type);
2265 if (klass != arg->klass)
2268 case MONO_TYPE_TYPEDBYREF:
2269 if (arg->type != STACK_VTYPE)
2271 klass = mono_class_from_mono_type (simple_type);
2272 if (klass != arg->klass)
2275 case MONO_TYPE_GENERICINST:
2276 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2277 MonoClass *target_class;
2278 if (arg->type != STACK_VTYPE)
2280 klass = mono_class_from_mono_type (simple_type);
2281 target_class = mono_class_from_mono_type (target);
2282 /* The second cases is needed when doing partial sharing */
2283 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2287 if (arg->type != STACK_OBJ)
2289 /* FIXME: check type compatibility */
2293 case MONO_TYPE_MVAR:
2294 g_assert (cfg->gshared);
2295 if (mini_type_var_is_vt (simple_type)) {
2296 if (arg->type != STACK_VTYPE)
2299 if (arg->type != STACK_OBJ)
2304 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2310 * Prepare arguments for passing to a function call.
2311 * Return a non-zero value if the arguments can't be passed to the given
2313 * The type checks are not yet complete and some conversions may need
2314 * casts on 32 or 64 bit architectures.
2316 * FIXME: implement this using target_type_is_incompatible ()
2319 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2321 MonoType *simple_type;
2325 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2329 for (i = 0; i < sig->param_count; ++i) {
2330 if (sig->params [i]->byref) {
2331 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2335 simple_type = mini_get_underlying_type (sig->params [i]);
2337 switch (simple_type->type) {
2338 case MONO_TYPE_VOID:
2347 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2353 case MONO_TYPE_FNPTR:
2354 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2357 case MONO_TYPE_CLASS:
2358 case MONO_TYPE_STRING:
2359 case MONO_TYPE_OBJECT:
2360 case MONO_TYPE_SZARRAY:
2361 case MONO_TYPE_ARRAY:
2362 if (args [i]->type != STACK_OBJ)
2367 if (args [i]->type != STACK_I8)
2371 if (args [i]->type != cfg->r4_stack_type)
2375 if (args [i]->type != STACK_R8)
2378 case MONO_TYPE_VALUETYPE:
2379 if (simple_type->data.klass->enumtype) {
2380 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2383 if (args [i]->type != STACK_VTYPE)
2386 case MONO_TYPE_TYPEDBYREF:
2387 if (args [i]->type != STACK_VTYPE)
2390 case MONO_TYPE_GENERICINST:
2391 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2394 case MONO_TYPE_MVAR:
2396 if (args [i]->type != STACK_VTYPE)
2400 g_error ("unknown type 0x%02x in check_call_signature",
2408 callvirt_to_call (int opcode)
2411 case OP_CALL_MEMBASE:
2413 case OP_VOIDCALL_MEMBASE:
2415 case OP_FCALL_MEMBASE:
2417 case OP_RCALL_MEMBASE:
2419 case OP_VCALL_MEMBASE:
2421 case OP_LCALL_MEMBASE:
2424 g_assert_not_reached ();
2431 callvirt_to_call_reg (int opcode)
2434 case OP_CALL_MEMBASE:
2436 case OP_VOIDCALL_MEMBASE:
2437 return OP_VOIDCALL_REG;
2438 case OP_FCALL_MEMBASE:
2439 return OP_FCALL_REG;
2440 case OP_RCALL_MEMBASE:
2441 return OP_RCALL_REG;
2442 case OP_VCALL_MEMBASE:
2443 return OP_VCALL_REG;
2444 case OP_LCALL_MEMBASE:
2445 return OP_LCALL_REG;
2447 g_assert_not_reached ();
2453 /* Either METHOD or IMT_ARG needs to be set */
2455 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2459 if (COMPILE_LLVM (cfg)) {
2461 method_reg = alloc_preg (cfg);
2462 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2464 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2465 method_reg = ins->dreg;
2469 call->imt_arg_reg = method_reg;
2471 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2476 method_reg = alloc_preg (cfg);
2477 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2479 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2480 method_reg = ins->dreg;
2483 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2486 static MonoJumpInfo *
2487 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2489 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2493 ji->data.target = target;
2499 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2502 return mono_class_check_context_used (klass);
2508 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2511 return mono_method_check_context_used (method);
2517 * check_method_sharing:
2519 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2522 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2524 gboolean pass_vtable = FALSE;
2525 gboolean pass_mrgctx = FALSE;
2527 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2528 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
2529 gboolean sharable = FALSE;
2531 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2535 * Pass vtable iff target method might
2536 * be shared, which means that sharing
2537 * is enabled for its class and its
2538 * context is sharable (and it's not a
2541 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2545 if (mini_method_get_context (cmethod) &&
2546 mini_method_get_context (cmethod)->method_inst) {
2547 g_assert (!pass_vtable);
2549 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2552 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2557 if (out_pass_vtable)
2558 *out_pass_vtable = pass_vtable;
2559 if (out_pass_mrgctx)
2560 *out_pass_mrgctx = pass_mrgctx;
2563 inline static MonoCallInst *
2564 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2565 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2569 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2577 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2579 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2581 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2584 call->signature = sig;
2585 call->rgctx_reg = rgctx;
2586 sig_ret = mini_get_underlying_type (sig->ret);
2588 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2591 if (mini_type_is_vtype (sig_ret)) {
2592 call->vret_var = cfg->vret_addr;
2593 //g_assert_not_reached ();
2595 } else if (mini_type_is_vtype (sig_ret)) {
2596 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2599 temp->backend.is_pinvoke = sig->pinvoke;
2602 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2603 * address of return value to increase optimization opportunities.
2604 * Before vtype decomposition, the dreg of the call ins itself represents the
2605 * fact the call modifies the return value. After decomposition, the call will
2606 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2607 * will be transformed into an LDADDR.
2609 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2610 loada->dreg = alloc_preg (cfg);
2611 loada->inst_p0 = temp;
2612 /* We reference the call too since call->dreg could change during optimization */
2613 loada->inst_p1 = call;
2614 MONO_ADD_INS (cfg->cbb, loada);
2616 call->inst.dreg = temp->dreg;
2618 call->vret_var = loada;
2619 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2620 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2622 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2623 if (COMPILE_SOFT_FLOAT (cfg)) {
2625 * If the call has a float argument, we would need to do an r8->r4 conversion using
2626 * an icall, but that cannot be done during the call sequence since it would clobber
2627 * the call registers + the stack. So we do it before emitting the call.
2629 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2631 MonoInst *in = call->args [i];
2633 if (i >= sig->hasthis)
2634 t = sig->params [i - sig->hasthis];
2636 t = &mono_defaults.int_class->byval_arg;
2637 t = mono_type_get_underlying_type (t);
2639 if (!t->byref && t->type == MONO_TYPE_R4) {
2640 MonoInst *iargs [1];
2644 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2646 /* The result will be in an int vreg */
2647 call->args [i] = conv;
2653 call->need_unbox_trampoline = unbox_trampoline;
2656 if (COMPILE_LLVM (cfg))
2657 mono_llvm_emit_call (cfg, call);
2659 mono_arch_emit_call (cfg, call);
2661 mono_arch_emit_call (cfg, call);
2664 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2665 cfg->flags |= MONO_CFG_HAS_CALLS;
2671 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2673 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2674 cfg->uses_rgctx_reg = TRUE;
2675 call->rgctx_reg = TRUE;
2677 call->rgctx_arg_reg = rgctx_reg;
2681 inline static MonoInst*
2682 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2687 gboolean check_sp = FALSE;
2689 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2690 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2692 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2697 rgctx_reg = mono_alloc_preg (cfg);
2698 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2702 if (!cfg->stack_inbalance_var)
2703 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2705 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2706 ins->dreg = cfg->stack_inbalance_var->dreg;
2707 MONO_ADD_INS (cfg->cbb, ins);
2710 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2712 call->inst.sreg1 = addr->dreg;
2715 emit_imt_argument (cfg, call, NULL, imt_arg);
2717 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2722 sp_reg = mono_alloc_preg (cfg);
2724 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2726 MONO_ADD_INS (cfg->cbb, ins);
2728 /* Restore the stack so we don't crash when throwing the exception */
2729 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2730 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2731 MONO_ADD_INS (cfg->cbb, ins);
2733 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2734 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2738 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2740 return (MonoInst*)call;
2744 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2747 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2749 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2752 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2753 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2755 #ifndef DISABLE_REMOTING
2756 gboolean might_be_remote = FALSE;
2758 gboolean virtual_ = this_ins != NULL;
2759 gboolean enable_for_aot = TRUE;
2762 MonoInst *call_target = NULL;
2764 gboolean need_unbox_trampoline;
2767 sig = mono_method_signature (method);
2769 if (cfg->llvm_only && (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
2770 g_assert_not_reached ();
2773 rgctx_reg = mono_alloc_preg (cfg);
2774 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2777 if (method->string_ctor) {
2778 /* Create the real signature */
2779 /* FIXME: Cache these */
2780 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2781 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2786 context_used = mini_method_check_context_used (cfg, method);
2788 #ifndef DISABLE_REMOTING
2789 might_be_remote = this_ins && sig->hasthis &&
2790 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2791 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2793 if (might_be_remote && context_used) {
2796 g_assert (cfg->gshared);
2798 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2800 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2804 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2805 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2807 need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
2809 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2811 #ifndef DISABLE_REMOTING
2812 if (might_be_remote)
2813 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2816 call->method = method;
2817 call->inst.flags |= MONO_INST_HAS_METHOD;
2818 call->inst.inst_left = this_ins;
2819 call->tail_call = tail;
2822 int vtable_reg, slot_reg, this_reg;
2825 this_reg = this_ins->dreg;
2827 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2828 MonoInst *dummy_use;
2830 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2832 /* Make a call to delegate->invoke_impl */
2833 call->inst.inst_basereg = this_reg;
2834 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2835 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2837 /* We must emit a dummy use here because the delegate trampoline will
2838 replace the 'this' argument with the delegate target making this activation
2839 no longer a root for the delegate.
2840 This is an issue for delegates that target collectible code such as dynamic
2841 methods of GC'able assemblies.
2843 For a test case look into #667921.
2845 FIXME: a dummy use is not the best way to do it as the local register allocator
2846 will put it on a caller save register and spil it around the call.
2847 Ideally, we would either put it on a callee save register or only do the store part.
2849 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2851 return (MonoInst*)call;
2854 if ((!cfg->compile_aot || enable_for_aot) &&
2855 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2856 (MONO_METHOD_IS_FINAL (method) &&
2857 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2858 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2860 * the method is not virtual, we just need to ensure this is not null
2861 * and then we can call the method directly.
2863 #ifndef DISABLE_REMOTING
2864 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2866 * The check above ensures method is not gshared, this is needed since
2867 * gshared methods can't have wrappers.
2869 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2873 if (!method->string_ctor)
2874 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2876 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2877 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2879 * the method is virtual, but we can statically dispatch since either
2880 * it's class or the method itself are sealed.
2881 * But first we need to ensure it's not a null reference.
2883 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2885 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2886 } else if (call_target) {
2887 vtable_reg = alloc_preg (cfg);
2888 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2890 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2891 call->inst.sreg1 = call_target->dreg;
2892 call->inst.flags &= !MONO_INST_HAS_METHOD;
2894 vtable_reg = alloc_preg (cfg);
2895 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2896 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2897 guint32 imt_slot = mono_method_get_imt_slot (method);
2898 emit_imt_argument (cfg, call, call->method, imt_arg);
2899 slot_reg = vtable_reg;
2900 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2902 slot_reg = vtable_reg;
2903 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2904 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2906 g_assert (mono_method_signature (method)->generic_param_count);
2907 emit_imt_argument (cfg, call, call->method, imt_arg);
2911 call->inst.sreg1 = slot_reg;
2912 call->inst.inst_offset = offset;
2913 call->is_virtual = TRUE;
2917 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2920 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2922 return (MonoInst*)call;
2926 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2928 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2932 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2939 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2942 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2944 return (MonoInst*)call;
2948 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2950 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2954 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2958 * mono_emit_abs_call:
2960 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2962 inline static MonoInst*
2963 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2964 MonoMethodSignature *sig, MonoInst **args)
2966 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2970 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2973 if (cfg->abs_patches == NULL)
2974 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2975 g_hash_table_insert (cfg->abs_patches, ji, ji);
2976 ins = mono_emit_native_call (cfg, ji, sig, args);
2977 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2981 static MonoMethodSignature*
2982 sig_to_rgctx_sig (MonoMethodSignature *sig)
2984 // FIXME: memory allocation
2985 MonoMethodSignature *res;
2988 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2989 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2990 res->param_count = sig->param_count + 1;
2991 for (i = 0; i < sig->param_count; ++i)
2992 res->params [i] = sig->params [i];
2993 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2997 /* Make an indirect call to FSIG passing an additional argument */
2999 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
3001 MonoMethodSignature *csig;
3002 MonoInst *args_buf [16];
3004 int i, pindex, tmp_reg;
3006 /* Make a call with an rgctx/extra arg */
3007 if (fsig->param_count + 2 < 16)
3010 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
3013 args [pindex ++] = orig_args [0];
3014 for (i = 0; i < fsig->param_count; ++i)
3015 args [pindex ++] = orig_args [fsig->hasthis + i];
3016 tmp_reg = alloc_preg (cfg);
3017 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
3018 csig = sig_to_rgctx_sig (fsig);
3019 return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
3022 /* Emit an indirect call to the function descriptor ADDR */
3024 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
3026 int addr_reg, arg_reg;
3027 MonoInst *call_target;
3029 g_assert (cfg->llvm_only);
3032 * addr points to a <addr, arg> pair, load both of them, and
3033 * make a call to addr, passing arg as an extra arg.
3035 addr_reg = alloc_preg (cfg);
3036 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
3037 arg_reg = alloc_preg (cfg);
3038 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
3040 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
3044 direct_icalls_enabled (MonoCompile *cfg)
3046 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3048 if (cfg->compile_llvm)
3051 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
3057 mono_emit_jit_icall_by_info (MonoCompile *cfg, MonoJitICallInfo *info, MonoInst **args)
3060 * Call the jit icall without a wrapper if possible.
3061 * The wrapper is needed for the following reasons:
3062 * - to handle exceptions thrown using mono_raise_exceptions () from the
3063 * icall function. The EH code needs the lmf frame pushed by the
3064 * wrapper to be able to unwind back to managed code.
3065 * - to be able to do stack walks for asynchronously suspended
3066 * threads when debugging.
3068 if (info->no_raise && direct_icalls_enabled (cfg)) {
3072 if (!info->wrapper_method) {
3073 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
3074 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
3076 mono_memory_barrier ();
3080 * Inline the wrapper method, which is basically a call to the C icall, and
3081 * an exception check.
3083 costs = inline_method (cfg, info->wrapper_method, NULL,
3084 args, NULL, cfg->real_offset, TRUE);
3085 g_assert (costs > 0);
3086 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
3090 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
3095 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
3097 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3098 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
3102 * Native code might return non register sized integers
3103 * without initializing the upper bits.
3105 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3106 case OP_LOADI1_MEMBASE:
3107 widen_op = OP_ICONV_TO_I1;
3109 case OP_LOADU1_MEMBASE:
3110 widen_op = OP_ICONV_TO_U1;
3112 case OP_LOADI2_MEMBASE:
3113 widen_op = OP_ICONV_TO_I2;
3115 case OP_LOADU2_MEMBASE:
3116 widen_op = OP_ICONV_TO_U2;
3122 if (widen_op != -1) {
3123 int dreg = alloc_preg (cfg);
3126 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3127 widen->type = ins->type;
3137 get_memcpy_method (void)
3139 static MonoMethod *memcpy_method = NULL;
3140 if (!memcpy_method) {
3141 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3143 g_error ("Old corlib found. Install a new one");
3145 return memcpy_method;
3149 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3151 MonoClassField *field;
3152 gpointer iter = NULL;
3154 while ((field = mono_class_get_fields (klass, &iter))) {
3157 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3159 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3160 if (mini_type_is_reference (mono_field_get_type (field))) {
3161 g_assert ((foffset % SIZEOF_VOID_P) == 0);
3162 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3164 MonoClass *field_class = mono_class_from_mono_type (field->type);
3165 if (field_class->has_references)
3166 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3172 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3174 int card_table_shift_bits;
3175 gpointer card_table_mask;
3177 MonoInst *dummy_use;
3178 int nursery_shift_bits;
3179 size_t nursery_size;
3181 if (!cfg->gen_write_barriers)
3184 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3186 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3188 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3191 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3192 wbarrier->sreg1 = ptr->dreg;
3193 wbarrier->sreg2 = value->dreg;
3194 MONO_ADD_INS (cfg->cbb, wbarrier);
3195 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3196 int offset_reg = alloc_preg (cfg);
3200 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3201 if (card_table_mask)
3202 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3204 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3205 * IMM's larger than 32bits.
3207 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3208 card_reg = ins->dreg;
3210 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3211 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3213 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3214 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3217 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3221 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3223 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3224 unsigned need_wb = 0;
3229 /*types with references can't have alignment smaller than sizeof(void*) */
3230 if (align < SIZEOF_VOID_P)
3233 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3234 if (size > 32 * SIZEOF_VOID_P)
3237 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3239 /* We don't unroll more than 5 stores to avoid code bloat. */
3240 if (size > 5 * SIZEOF_VOID_P) {
3241 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3242 size += (SIZEOF_VOID_P - 1);
3243 size &= ~(SIZEOF_VOID_P - 1);
3245 EMIT_NEW_ICONST (cfg, iargs [2], size);
3246 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3247 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3251 destreg = iargs [0]->dreg;
3252 srcreg = iargs [1]->dreg;
3255 dest_ptr_reg = alloc_preg (cfg);
3256 tmp_reg = alloc_preg (cfg);
3259 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3261 while (size >= SIZEOF_VOID_P) {
3262 MonoInst *load_inst;
3263 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3264 load_inst->dreg = tmp_reg;
3265 load_inst->inst_basereg = srcreg;
3266 load_inst->inst_offset = offset;
3267 MONO_ADD_INS (cfg->cbb, load_inst);
3269 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3272 emit_write_barrier (cfg, iargs [0], load_inst);
3274 offset += SIZEOF_VOID_P;
3275 size -= SIZEOF_VOID_P;
3278 /*tmp += sizeof (void*)*/
3279 if (size >= SIZEOF_VOID_P) {
3280 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3281 MONO_ADD_INS (cfg->cbb, iargs [0]);
3285 /* Those cannot be references since size < sizeof (void*) */
3287 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3288 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3294 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3295 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3301 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3302 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3311 * Emit code to copy a valuetype of type @klass whose address is stored in
3312 * @src->dreg to memory whose address is stored at @dest->dreg.
3315 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3317 MonoInst *iargs [4];
3320 MonoMethod *memcpy_method;
3321 MonoInst *size_ins = NULL;
3322 MonoInst *memcpy_ins = NULL;
3326 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3329 * This check breaks with spilled vars... need to handle it during verification anyway.
3330 * g_assert (klass && klass == src->klass && klass == dest->klass);
3333 if (mini_is_gsharedvt_klass (klass)) {
3335 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3336 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3340 n = mono_class_native_size (klass, &align);
3342 n = mono_class_value_size (klass, &align);
3344 /* if native is true there should be no references in the struct */
3345 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3346 /* Avoid barriers when storing to the stack */
3347 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3348 (dest->opcode == OP_LDADDR))) {
3354 context_used = mini_class_check_context_used (cfg, klass);
3356 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3357 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3359 } else if (context_used) {
3360 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3362 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3363 if (!cfg->compile_aot)
3364 mono_class_compute_gc_descriptor (klass);
3368 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3370 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3375 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3376 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3377 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3382 iargs [2] = size_ins;
3384 EMIT_NEW_ICONST (cfg, iargs [2], n);
3386 memcpy_method = get_memcpy_method ();
3388 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3390 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3395 get_memset_method (void)
3397 static MonoMethod *memset_method = NULL;
3398 if (!memset_method) {
3399 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3401 g_error ("Old corlib found. Install a new one");
3403 return memset_method;
3407 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3409 MonoInst *iargs [3];
3412 MonoMethod *memset_method;
3413 MonoInst *size_ins = NULL;
3414 MonoInst *bzero_ins = NULL;
3415 static MonoMethod *bzero_method;
3417 /* FIXME: Optimize this for the case when dest is an LDADDR */
3418 mono_class_init (klass);
3419 if (mini_is_gsharedvt_klass (klass)) {
3420 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3421 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3423 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3424 g_assert (bzero_method);
3426 iargs [1] = size_ins;
3427 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3431 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3433 n = mono_class_value_size (klass, &align);
3435 if (n <= sizeof (gpointer) * 8) {
3436 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3439 memset_method = get_memset_method ();
3441 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3442 EMIT_NEW_ICONST (cfg, iargs [2], n);
3443 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3450 * Emit IR to return either the this pointer for instance method,
3451 * or the mrgctx for static methods.
3454 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3456 MonoInst *this_ins = NULL;
3458 g_assert (cfg->gshared);
3460 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3461 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3462 !method->klass->valuetype)
3463 EMIT_NEW_ARGLOAD (cfg, this_ins, 0);
3465 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3466 MonoInst *mrgctx_loc, *mrgctx_var;
3468 g_assert (!this_ins);
3469 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3471 mrgctx_loc = mono_get_vtable_var (cfg);
3472 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3475 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3476 MonoInst *vtable_loc, *vtable_var;
3478 g_assert (!this_ins);
3480 vtable_loc = mono_get_vtable_var (cfg);
3481 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3483 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3484 MonoInst *mrgctx_var = vtable_var;
3487 vtable_reg = alloc_preg (cfg);
3488 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3489 vtable_var->type = STACK_PTR;
3497 vtable_reg = alloc_preg (cfg);
3498 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3503 static MonoJumpInfoRgctxEntry *
3504 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3506 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3507 res->method = method;
3508 res->in_mrgctx = in_mrgctx;
3509 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3510 res->data->type = patch_type;
3511 res->data->data.target = patch_data;
3512 res->info_type = info_type;
3517 static inline MonoInst*
3518 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3520 MonoInst *args [16];
3523 // FIXME: No fastpath since the slot is not a compile time constant
3525 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3526 if (entry->in_mrgctx)
3527 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3529 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3533 * FIXME: This can be called during decompose, which is a problem since it creates
3535 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3537 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3539 MonoBasicBlock *is_null_bb, *end_bb;
3540 MonoInst *res, *ins, *call;
3543 slot = mini_get_rgctx_entry_slot (entry);
3545 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3546 index = MONO_RGCTX_SLOT_INDEX (slot);
3548 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3549 for (depth = 0; ; ++depth) {
3550 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3552 if (index < size - 1)
3557 NEW_BBLOCK (cfg, end_bb);
3558 NEW_BBLOCK (cfg, is_null_bb);
3561 rgctx_reg = rgctx->dreg;
3563 rgctx_reg = alloc_preg (cfg);
3565 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3566 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3567 NEW_BBLOCK (cfg, is_null_bb);
3569 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3570 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3573 for (i = 0; i < depth; ++i) {
3574 int array_reg = alloc_preg (cfg);
3576 /* load ptr to next array */
3577 if (mrgctx && i == 0)
3578 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3580 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3581 rgctx_reg = array_reg;
3582 /* is the ptr null? */
3583 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3584 /* if yes, jump to actual trampoline */
3585 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3589 val_reg = alloc_preg (cfg);
3590 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3591 /* is the slot null? */
3592 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3593 /* if yes, jump to actual trampoline */
3594 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3597 res_reg = alloc_preg (cfg);
3598 MONO_INST_NEW (cfg, ins, OP_MOVE);
3599 ins->dreg = res_reg;
3600 ins->sreg1 = val_reg;
3601 MONO_ADD_INS (cfg->cbb, ins);
3603 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3606 MONO_START_BB (cfg, is_null_bb);
3608 EMIT_NEW_ICONST (cfg, args [1], index);
3610 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3612 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3613 MONO_INST_NEW (cfg, ins, OP_MOVE);
3614 ins->dreg = res_reg;
3615 ins->sreg1 = call->dreg;
3616 MONO_ADD_INS (cfg->cbb, ins);
3617 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3619 MONO_START_BB (cfg, end_bb);
3628 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3631 static inline MonoInst*
3632 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3635 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3637 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3641 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3642 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3644 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3645 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3647 return emit_rgctx_fetch (cfg, rgctx, entry);
3651 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3652 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3654 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3655 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3657 return emit_rgctx_fetch (cfg, rgctx, entry);
3661 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3662 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3664 MonoJumpInfoGSharedVtCall *call_info;
3665 MonoJumpInfoRgctxEntry *entry;
3668 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3669 call_info->sig = sig;
3670 call_info->method = cmethod;
3672 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3673 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3675 return emit_rgctx_fetch (cfg, rgctx, entry);
3679 * emit_get_rgctx_virt_method:
3681 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3684 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3685 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3687 MonoJumpInfoVirtMethod *info;
3688 MonoJumpInfoRgctxEntry *entry;
3691 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3692 info->klass = klass;
3693 info->method = virt_method;
3695 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_VIRT_METHOD, info, rgctx_type);
3696 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3698 return emit_rgctx_fetch (cfg, rgctx, entry);
3702 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3703 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3705 MonoJumpInfoRgctxEntry *entry;
3708 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3709 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3711 return emit_rgctx_fetch (cfg, rgctx, entry);
3715 * emit_get_rgctx_method:
3717 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3718 * normal constants, else emit a load from the rgctx.
3721 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3722 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3724 if (!context_used) {
3727 switch (rgctx_type) {
3728 case MONO_RGCTX_INFO_METHOD:
3729 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3731 case MONO_RGCTX_INFO_METHOD_RGCTX:
3732 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3735 g_assert_not_reached ();
3738 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3739 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3741 return emit_rgctx_fetch (cfg, rgctx, entry);
3746 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3747 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3749 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3750 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3752 return emit_rgctx_fetch (cfg, rgctx, entry);
3756 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3758 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3759 MonoRuntimeGenericContextInfoTemplate *template_;
3764 for (i = 0; i < info->num_entries; ++i) {
3765 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3767 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3771 if (info->num_entries == info->count_entries) {
3772 MonoRuntimeGenericContextInfoTemplate *new_entries;
3773 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3775 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3777 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3778 info->entries = new_entries;
3779 info->count_entries = new_count_entries;
3782 idx = info->num_entries;
3783 template_ = &info->entries [idx];
3784 template_->info_type = rgctx_type;
3785 template_->data = data;
3787 info->num_entries ++;
3793 * emit_get_gsharedvt_info:
3795 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3798 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3803 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3804 /* Load info->entries [idx] */
3805 dreg = alloc_preg (cfg);
3806 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3812 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3814 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3818 * On return the caller must check @klass for load errors.
3821 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3823 MonoInst *vtable_arg;
3826 context_used = mini_class_check_context_used (cfg, klass);
3829 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3830 klass, MONO_RGCTX_INFO_VTABLE);
3832 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3836 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3839 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3843 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3844 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3846 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3847 ins->sreg1 = vtable_arg->dreg;
3848 MONO_ADD_INS (cfg->cbb, ins);
3850 static int byte_offset = -1;
3851 static guint8 bitmask;
3852 int bits_reg, inited_reg;
3853 MonoBasicBlock *inited_bb;
3854 MonoInst *args [16];
3856 if (byte_offset < 0)
3857 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
3859 bits_reg = alloc_ireg (cfg);
3860 inited_reg = alloc_ireg (cfg);
3862 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, bits_reg, vtable_arg->dreg, byte_offset);
3863 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, inited_reg, bits_reg, bitmask);
3865 NEW_BBLOCK (cfg, inited_bb);
3867 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3868 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3870 args [0] = vtable_arg;
3871 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3873 MONO_START_BB (cfg, inited_bb);
3878 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3882 if (cfg->gen_seq_points && cfg->method == method) {
3883 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3885 ins->flags |= MONO_INST_NONEMPTY_STACK;
3886 MONO_ADD_INS (cfg->cbb, ins);
3891 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3893 if (mini_get_debug_options ()->better_cast_details) {
3894 int vtable_reg = alloc_preg (cfg);
3895 int klass_reg = alloc_preg (cfg);
3896 MonoBasicBlock *is_null_bb = NULL;
3898 int to_klass_reg, context_used;
3901 NEW_BBLOCK (cfg, is_null_bb);
3903 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3904 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3907 tls_get = mono_get_jit_tls_intrinsic (cfg);
3909 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3913 MONO_ADD_INS (cfg->cbb, tls_get);
3914 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3915 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3917 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3919 context_used = mini_class_check_context_used (cfg, klass);
3921 MonoInst *class_ins;
3923 class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3924 to_klass_reg = class_ins->dreg;
3926 to_klass_reg = alloc_preg (cfg);
3927 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3929 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3932 MONO_START_BB (cfg, is_null_bb);
3937 reset_cast_details (MonoCompile *cfg)
3939 /* Reset the variables holding the cast details */
3940 if (mini_get_debug_options ()->better_cast_details) {
3941 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3943 MONO_ADD_INS (cfg->cbb, tls_get);
3944 /* It is enough to reset the from field */
3945 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3950 * On return the caller must check @array_class for load errors
3953 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3955 int vtable_reg = alloc_preg (cfg);
3958 context_used = mini_class_check_context_used (cfg, array_class);
3960 save_cast_details (cfg, array_class, obj->dreg, FALSE);
3962 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3964 if (cfg->opt & MONO_OPT_SHARED) {
3965 int class_reg = alloc_preg (cfg);
3968 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3969 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3970 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3971 } else if (context_used) {
3972 MonoInst *vtable_ins;
3974 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3975 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3977 if (cfg->compile_aot) {
3981 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3983 vt_reg = alloc_preg (cfg);
3984 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3985 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3988 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3990 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3994 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3996 reset_cast_details (cfg);
4000 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
4001 * generic code is generated.
4004 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
4006 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4009 MonoInst *rgctx, *addr;
4011 /* FIXME: What if the class is shared? We might not
4012 have to get the address of the method from the
4014 addr = emit_get_rgctx_method (cfg, context_used, method,
4015 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4016 if (cfg->llvm_only && cfg->gsharedvt) {
4017 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4019 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4021 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4024 gboolean pass_vtable, pass_mrgctx;
4025 MonoInst *rgctx_arg = NULL;
4027 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4028 g_assert (!pass_mrgctx);
4031 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4034 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4037 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4042 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
4046 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
4047 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
4048 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
4049 int rank_reg = alloc_dreg (cfg ,STACK_I4);
4051 obj_reg = sp [0]->dreg;
4052 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4053 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4055 /* FIXME: generics */
4056 g_assert (klass->rank == 0);
4059 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
4060 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4062 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4063 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
4066 MonoInst *element_class;
4068 /* This assertion is from the unboxcast insn */
4069 g_assert (klass->rank == 0);
4071 element_class = emit_get_rgctx_klass (cfg, context_used,
4072 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
4074 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
4075 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4077 save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
4078 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
4079 reset_cast_details (cfg);
4082 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
4083 MONO_ADD_INS (cfg->cbb, add);
4084 add->type = STACK_MP;
4091 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
4093 MonoInst *addr, *klass_inst, *is_ref, *args[16];
4094 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4098 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
4104 args [1] = klass_inst;
4107 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
4109 NEW_BBLOCK (cfg, is_ref_bb);
4110 NEW_BBLOCK (cfg, is_nullable_bb);
4111 NEW_BBLOCK (cfg, end_bb);
4112 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4113 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4114 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4116 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4117 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4119 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
4120 addr_reg = alloc_dreg (cfg, STACK_MP);
4124 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
4125 MONO_ADD_INS (cfg->cbb, addr);
4127 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4130 MONO_START_BB (cfg, is_ref_bb);
4132 /* Save the ref to a temporary */
4133 dreg = alloc_ireg (cfg);
4134 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
4135 addr->dreg = addr_reg;
4136 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
4137 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4140 MONO_START_BB (cfg, is_nullable_bb);
4143 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
4144 MonoInst *unbox_call;
4145 MonoMethodSignature *unbox_sig;
4147 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4148 unbox_sig->ret = &klass->byval_arg;
4149 unbox_sig->param_count = 1;
4150 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
4153 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
4155 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
4157 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
4158 addr->dreg = addr_reg;
4161 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4164 MONO_START_BB (cfg, end_bb);
4167 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
4173 * Returns NULL and set the cfg exception on error.
4176 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
4178 MonoInst *iargs [2];
4183 MonoRgctxInfoType rgctx_info;
4184 MonoInst *iargs [2];
4185 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4187 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4189 if (cfg->opt & MONO_OPT_SHARED)
4190 rgctx_info = MONO_RGCTX_INFO_KLASS;
4192 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4193 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4195 if (cfg->opt & MONO_OPT_SHARED) {
4196 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4198 alloc_ftn = ves_icall_object_new;
4201 alloc_ftn = ves_icall_object_new_specific;
4204 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4205 if (known_instance_size) {
4206 int size = mono_class_instance_size (klass);
4207 if (size < sizeof (MonoObject))
4208 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4210 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4212 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4215 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4218 if (cfg->opt & MONO_OPT_SHARED) {
4219 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4220 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4222 alloc_ftn = ves_icall_object_new;
4223 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !klass->generic_class) {
4224 /* This happens often in argument checking code, eg. throw new FooException... */
4225 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4226 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4227 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4229 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4230 MonoMethod *managed_alloc = NULL;
4234 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4235 cfg->exception_ptr = klass;
4239 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4241 if (managed_alloc) {
4242 int size = mono_class_instance_size (klass);
4243 if (size < sizeof (MonoObject))
4244 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4246 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4247 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4248 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4250 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4252 guint32 lw = vtable->klass->instance_size;
4253 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4254 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4255 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4258 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4262 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4266 * Returns NULL and set the cfg exception on error.
4269 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4271 MonoInst *alloc, *ins;
4273 if (mono_class_is_nullable (klass)) {
4274 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4277 if (cfg->llvm_only && cfg->gsharedvt) {
4278 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4279 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4280 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4282 /* FIXME: What if the class is shared? We might not
4283 have to get the method address from the RGCTX. */
4284 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4285 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4286 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4288 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4291 gboolean pass_vtable, pass_mrgctx;
4292 MonoInst *rgctx_arg = NULL;
4294 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4295 g_assert (!pass_mrgctx);
4298 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4301 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4304 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4308 if (mini_is_gsharedvt_klass (klass)) {
4309 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4310 MonoInst *res, *is_ref, *src_var, *addr;
4313 dreg = alloc_ireg (cfg);
4315 NEW_BBLOCK (cfg, is_ref_bb);
4316 NEW_BBLOCK (cfg, is_nullable_bb);
4317 NEW_BBLOCK (cfg, end_bb);
4318 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4319 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4320 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4322 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4323 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4326 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4329 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4330 ins->opcode = OP_STOREV_MEMBASE;
4332 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4333 res->type = STACK_OBJ;
4335 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4338 MONO_START_BB (cfg, is_ref_bb);
4340 /* val is a vtype, so has to load the value manually */
4341 src_var = get_vreg_to_inst (cfg, val->dreg);
4343 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4344 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4345 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4346 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4349 MONO_START_BB (cfg, is_nullable_bb);
4352 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4353 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4355 MonoMethodSignature *box_sig;
4358 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4359 * construct that method at JIT time, so have to do things by hand.
4361 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4362 box_sig->ret = &mono_defaults.object_class->byval_arg;
4363 box_sig->param_count = 1;
4364 box_sig->params [0] = &klass->byval_arg;
4367 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4369 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4370 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4371 res->type = STACK_OBJ;
4375 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4377 MONO_START_BB (cfg, end_bb);
4381 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4385 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4391 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4394 MonoGenericContainer *container;
4395 MonoGenericInst *ginst;
4397 if (klass->generic_class) {
4398 container = klass->generic_class->container_class->generic_container;
4399 ginst = klass->generic_class->context.class_inst;
4400 } else if (klass->generic_container && context_used) {
4401 container = klass->generic_container;
4402 ginst = container->context.class_inst;
4407 for (i = 0; i < container->type_argc; ++i) {
4409 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4411 type = ginst->type_argv [i];
4412 if (mini_type_is_reference (type))
4418 static GHashTable* direct_icall_type_hash;
4421 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4423 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4424 if (!direct_icalls_enabled (cfg))
4428 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4429 * Whitelist a few icalls for now.
4431 if (!direct_icall_type_hash) {
4432 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4434 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4435 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4436 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4437 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4438 mono_memory_barrier ();
4439 direct_icall_type_hash = h;
4442 if (cmethod->klass == mono_defaults.math_class)
4444 /* No locking needed */
4445 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4450 /* Return whenever METHOD calls Assembly.GetCallingAssembly () */
4451 // FIXME: It would be better to generalize this using some kind of method attribute
4452 // or automatic detection
4454 method_needs_calling_assembly (MonoMethod *method)
4456 MonoClass *klass = method->klass;
4458 if (klass->image != mono_defaults.corlib)
4460 if (!strcmp (klass->name_space, "System") && !strcmp (klass->name, "Activator") &&
4461 !strcmp (method->name, "CreateInstance"))
4466 #define is_complex_isinst(klass) ((klass->flags & TYPE_ATTRIBUTE_INTERFACE) || klass->rank || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || (klass->flags & TYPE_ATTRIBUTE_SEALED) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
4469 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4471 MonoMethod *mono_castclass;
4474 mono_castclass = mono_marshal_get_castclass_with_cache ();
4476 save_cast_details (cfg, klass, args [0]->dreg, TRUE);
4477 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4478 reset_cast_details (cfg);
4484 get_castclass_cache_idx (MonoCompile *cfg)
4486 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4487 cfg->castclass_cache_index ++;
4488 return (cfg->method_index << 16) | cfg->castclass_cache_index;
4492 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4501 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4504 idx = get_castclass_cache_idx (cfg);
4505 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4507 /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4508 return emit_castclass_with_cache (cfg, klass, args);
4512 * Returns NULL and set the cfg exception on error.
4515 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, guint8 *ip, int *inline_costs)
4517 MonoBasicBlock *is_null_bb;
4518 int obj_reg = src->dreg;
4519 int vtable_reg = alloc_preg (cfg);
4521 MonoInst *klass_inst = NULL, *res;
4523 context_used = mini_class_check_context_used (cfg, klass);
4525 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
4526 res = emit_castclass_with_cache_nonshared (cfg, src, klass);
4527 (*inline_costs) += 2;
4529 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
4530 MonoMethod *mono_castclass;
4531 MonoInst *iargs [1];
4534 mono_castclass = mono_marshal_get_castclass (klass);
4537 save_cast_details (cfg, klass, src->dreg, TRUE);
4538 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
4539 iargs, ip, cfg->real_offset, TRUE);
4540 reset_cast_details (cfg);
4541 CHECK_CFG_EXCEPTION;
4542 g_assert (costs > 0);
4544 cfg->real_offset += 5;
4546 (*inline_costs) += costs;
4554 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4555 MonoInst *cache_ins;
4557 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4562 /* klass - it's the second element of the cache entry*/
4563 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4566 args [2] = cache_ins;
4568 return emit_castclass_with_cache (cfg, klass, args);
4571 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4574 NEW_BBLOCK (cfg, is_null_bb);
4576 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4577 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4579 save_cast_details (cfg, klass, obj_reg, FALSE);
4581 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4582 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4583 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4585 int klass_reg = alloc_preg (cfg);
4587 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4589 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4590 /* the remoting code is broken, access the class for now */
4591 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4592 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4594 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4595 cfg->exception_ptr = klass;
4598 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4600 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4601 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4603 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4605 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4606 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4610 MONO_START_BB (cfg, is_null_bb);
4612 reset_cast_details (cfg);
4621 * Returns NULL and set the cfg exception on error.
4624 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4627 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4628 int obj_reg = src->dreg;
4629 int vtable_reg = alloc_preg (cfg);
4630 int res_reg = alloc_ireg_ref (cfg);
4631 MonoInst *klass_inst = NULL;
4636 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4637 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4638 MonoInst *cache_ins;
4640 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4645 /* klass - it's the second element of the cache entry*/
4646 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4649 args [2] = cache_ins;
4651 return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4654 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4657 NEW_BBLOCK (cfg, is_null_bb);
4658 NEW_BBLOCK (cfg, false_bb);
4659 NEW_BBLOCK (cfg, end_bb);
4661 /* Do the assignment at the beginning, so the other assignment can be if converted */
4662 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4663 ins->type = STACK_OBJ;
4666 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4667 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4669 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4671 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4672 g_assert (!context_used);
4673 /* the is_null_bb target simply copies the input register to the output */
4674 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4676 int klass_reg = alloc_preg (cfg);
4679 int rank_reg = alloc_preg (cfg);
4680 int eclass_reg = alloc_preg (cfg);
4682 g_assert (!context_used);
4683 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4684 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4685 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4686 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4687 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4688 if (klass->cast_class == mono_defaults.object_class) {
4689 int parent_reg = alloc_preg (cfg);
4690 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4691 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4692 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4693 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4694 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4695 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4696 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4697 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4698 } else if (klass->cast_class == mono_defaults.enum_class) {
4699 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4700 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4701 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4702 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4704 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4705 /* Check that the object is a vector too */
4706 int bounds_reg = alloc_preg (cfg);
4707 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4708 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4709 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4712 /* the is_null_bb target simply copies the input register to the output */
4713 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4715 } else if (mono_class_is_nullable (klass)) {
4716 g_assert (!context_used);
4717 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4718 /* the is_null_bb target simply copies the input register to the output */
4719 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4721 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4722 g_assert (!context_used);
4723 /* the remoting code is broken, access the class for now */
4724 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4725 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4727 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4728 cfg->exception_ptr = klass;
4731 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4733 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4734 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4736 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4737 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4739 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4740 /* the is_null_bb target simply copies the input register to the output */
4741 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4746 MONO_START_BB (cfg, false_bb);
4748 MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4749 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4751 MONO_START_BB (cfg, is_null_bb);
4753 MONO_START_BB (cfg, end_bb);
4759 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4761 /* This opcode takes as input an object reference and a class, and returns:
4762 0) if the object is an instance of the class,
4763 1) if the object is not instance of the class,
4764 2) if the object is a proxy whose type cannot be determined */
4767 #ifndef DISABLE_REMOTING
4768 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4770 MonoBasicBlock *true_bb, *false_bb, *end_bb;
4772 int obj_reg = src->dreg;
4773 int dreg = alloc_ireg (cfg);
4775 #ifndef DISABLE_REMOTING
4776 int klass_reg = alloc_preg (cfg);
4779 NEW_BBLOCK (cfg, true_bb);
4780 NEW_BBLOCK (cfg, false_bb);
4781 NEW_BBLOCK (cfg, end_bb);
4782 #ifndef DISABLE_REMOTING
4783 NEW_BBLOCK (cfg, false2_bb);
4784 NEW_BBLOCK (cfg, no_proxy_bb);
4787 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4788 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4790 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4791 #ifndef DISABLE_REMOTING
4792 NEW_BBLOCK (cfg, interface_fail_bb);
4795 tmp_reg = alloc_preg (cfg);
4796 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4797 #ifndef DISABLE_REMOTING
4798 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4799 MONO_START_BB (cfg, interface_fail_bb);
4800 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4802 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4804 tmp_reg = alloc_preg (cfg);
4805 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4806 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4807 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
4809 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4812 #ifndef DISABLE_REMOTING
4813 tmp_reg = alloc_preg (cfg);
4814 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4815 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4817 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4818 tmp_reg = alloc_preg (cfg);
4819 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4820 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4822 tmp_reg = alloc_preg (cfg);
4823 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4824 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4825 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4827 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4828 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4830 MONO_START_BB (cfg, no_proxy_bb);
4832 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4834 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4838 MONO_START_BB (cfg, false_bb);
4840 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4841 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4843 #ifndef DISABLE_REMOTING
4844 MONO_START_BB (cfg, false2_bb);
4846 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4847 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4850 MONO_START_BB (cfg, true_bb);
4852 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4854 MONO_START_BB (cfg, end_bb);
4857 MONO_INST_NEW (cfg, ins, OP_ICONST);
4859 ins->type = STACK_I4;
4865 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4867 /* This opcode takes as input an object reference and a class, and returns:
4868 0) if the object is an instance of the class,
4869 1) if the object is a proxy whose type cannot be determined
4870 an InvalidCastException exception is thrown otherwhise*/
4873 #ifndef DISABLE_REMOTING
4874 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4876 MonoBasicBlock *ok_result_bb;
4878 int obj_reg = src->dreg;
4879 int dreg = alloc_ireg (cfg);
4880 int tmp_reg = alloc_preg (cfg);
4882 #ifndef DISABLE_REMOTING
4883 int klass_reg = alloc_preg (cfg);
4884 NEW_BBLOCK (cfg, end_bb);
4887 NEW_BBLOCK (cfg, ok_result_bb);
4889 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4890 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4892 save_cast_details (cfg, klass, obj_reg, FALSE);
4894 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4895 #ifndef DISABLE_REMOTING
4896 NEW_BBLOCK (cfg, interface_fail_bb);
4898 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4899 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4900 MONO_START_BB (cfg, interface_fail_bb);
4901 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4903 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4905 tmp_reg = alloc_preg (cfg);
4906 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4907 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4908 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4910 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4911 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4913 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4914 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
4915 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4918 #ifndef DISABLE_REMOTING
4919 NEW_BBLOCK (cfg, no_proxy_bb);
4921 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4922 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4923 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4925 tmp_reg = alloc_preg (cfg);
4926 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4927 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4929 tmp_reg = alloc_preg (cfg);
4930 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4931 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4932 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4934 NEW_BBLOCK (cfg, fail_1_bb);
4936 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4938 MONO_START_BB (cfg, fail_1_bb);
4940 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4941 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4943 MONO_START_BB (cfg, no_proxy_bb);
4945 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4947 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4951 MONO_START_BB (cfg, ok_result_bb);
4953 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4955 #ifndef DISABLE_REMOTING
4956 MONO_START_BB (cfg, end_bb);
4960 MONO_INST_NEW (cfg, ins, OP_ICONST);
4962 ins->type = STACK_I4;
4967 static G_GNUC_UNUSED MonoInst*
4968 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4970 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4971 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4974 switch (enum_type->type) {
4977 #if SIZEOF_REGISTER == 8
4989 MonoInst *load, *and_, *cmp, *ceq;
4990 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4991 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4992 int dest_reg = alloc_ireg (cfg);
4994 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4995 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4996 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4997 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4999 ceq->type = STACK_I4;
5002 load = mono_decompose_opcode (cfg, load);
5003 and_ = mono_decompose_opcode (cfg, and_);
5004 cmp = mono_decompose_opcode (cfg, cmp);
5005 ceq = mono_decompose_opcode (cfg, ceq);
5013 * Returns NULL and set the cfg exception on error.
5015 static G_GNUC_UNUSED MonoInst*
5016 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
5020 gpointer trampoline;
5021 MonoInst *obj, *method_ins, *tramp_ins;
5025 if (virtual_ && !cfg->llvm_only) {
5026 MonoMethod *invoke = mono_get_delegate_invoke (klass);
5029 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
5033 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
5037 /* Inline the contents of mono_delegate_ctor */
5039 /* Set target field */
5040 /* Optimize away setting of NULL target */
5041 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
5042 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
5043 if (cfg->gen_write_barriers) {
5044 dreg = alloc_preg (cfg);
5045 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
5046 emit_write_barrier (cfg, ptr, target);
5050 /* Set method field */
5051 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5052 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
5055 * To avoid looking up the compiled code belonging to the target method
5056 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
5057 * store it, and we fill it after the method has been compiled.
5059 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
5060 MonoInst *code_slot_ins;
5063 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
5065 domain = mono_domain_get ();
5066 mono_domain_lock (domain);
5067 if (!domain_jit_info (domain)->method_code_hash)
5068 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
5069 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
5071 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
5072 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
5074 mono_domain_unlock (domain);
5076 code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
5078 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
5081 if (cfg->llvm_only) {
5082 MonoInst *args [16];
5087 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5088 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
5091 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
5097 if (cfg->compile_aot) {
5098 MonoDelegateClassMethodPair *del_tramp;
5100 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
5101 del_tramp->klass = klass;
5102 del_tramp->method = context_used ? NULL : method;
5103 del_tramp->is_virtual = virtual_;
5104 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
5107 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
5109 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
5110 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
5113 /* Set invoke_impl field */
5115 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
5117 dreg = alloc_preg (cfg);
5118 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
5119 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
5121 dreg = alloc_preg (cfg);
5122 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
5123 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
5126 dreg = alloc_preg (cfg);
5127 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
5128 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
5130 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
5136 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
5138 MonoJitICallInfo *info;
5140 /* Need to register the icall so it gets an icall wrapper */
5141 info = mono_get_array_new_va_icall (rank);
5143 cfg->flags |= MONO_CFG_HAS_VARARGS;
5145 /* mono_array_new_va () needs a vararg calling convention */
5146 cfg->exception_message = g_strdup ("array-new");
5147 cfg->disable_llvm = TRUE;
5149 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
5150 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
5154 * handle_constrained_gsharedvt_call:
5156 * Handle constrained calls where the receiver is a gsharedvt type.
5157 * Return the instruction representing the call. Set the cfg exception on failure.
5160 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
5161 gboolean *ref_emit_widen)
5163 MonoInst *ins = NULL;
5164 gboolean emit_widen = *ref_emit_widen;
5167 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
5168 * 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
5169 * pack the arguments into an array, and do the rest of the work in in an icall.
5171 if (((cmethod->klass == mono_defaults.object_class) || (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
5172 (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)) &&
5173 (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]))))) {
5174 MonoInst *args [16];
5177 * This case handles calls to
5178 * - object:ToString()/Equals()/GetHashCode(),
5179 * - System.IComparable<T>:CompareTo()
5180 * - System.IEquatable<T>:Equals ()
5181 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
5185 if (mono_method_check_context_used (cmethod))
5186 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
5188 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
5189 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
5191 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
5192 if (fsig->hasthis && fsig->param_count) {
5193 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
5194 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
5195 ins->dreg = alloc_preg (cfg);
5196 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
5197 MONO_ADD_INS (cfg->cbb, ins);
5200 if (mini_is_gsharedvt_type (fsig->params [0])) {
5201 int addr_reg, deref_arg_reg;
5203 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
5204 deref_arg_reg = alloc_preg (cfg);
5205 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
5206 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
5208 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
5209 addr_reg = ins->dreg;
5210 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
5212 EMIT_NEW_ICONST (cfg, args [3], 0);
5213 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
5216 EMIT_NEW_ICONST (cfg, args [3], 0);
5217 EMIT_NEW_ICONST (cfg, args [4], 0);
5219 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
5222 if (mini_is_gsharedvt_type (fsig->ret)) {
5223 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
5224 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
5228 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
5229 MONO_ADD_INS (cfg->cbb, add);
5231 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
5232 MONO_ADD_INS (cfg->cbb, ins);
5233 /* ins represents the call result */
5236 GSHAREDVT_FAILURE (CEE_CALLVIRT);
5239 *ref_emit_widen = emit_widen;
5248 mono_emit_load_got_addr (MonoCompile *cfg)
5250 MonoInst *getaddr, *dummy_use;
5252 if (!cfg->got_var || cfg->got_var_allocated)
5255 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
5256 getaddr->cil_code = cfg->header->code;
5257 getaddr->dreg = cfg->got_var->dreg;
5259 /* Add it to the start of the first bblock */
5260 if (cfg->bb_entry->code) {
5261 getaddr->next = cfg->bb_entry->code;
5262 cfg->bb_entry->code = getaddr;
5265 MONO_ADD_INS (cfg->bb_entry, getaddr);
5267 cfg->got_var_allocated = TRUE;
5270 * Add a dummy use to keep the got_var alive, since real uses might
5271 * only be generated by the back ends.
5272 * Add it to end_bblock, so the variable's lifetime covers the whole
5274 * It would be better to make the usage of the got var explicit in all
5275 * cases when the backend needs it (i.e. calls, throw etc.), so this
5276 * wouldn't be needed.
5278 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5279 MONO_ADD_INS (cfg->bb_exit, dummy_use);
5282 static int inline_limit;
5283 static gboolean inline_limit_inited;
5286 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5288 MonoMethodHeaderSummary header;
5290 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5291 MonoMethodSignature *sig = mono_method_signature (method);
5295 if (cfg->disable_inline)
5300 if (cfg->inline_depth > 10)
5303 if (!mono_method_get_header_summary (method, &header))
5306 /*runtime, icall and pinvoke are checked by summary call*/
5307 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5308 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5309 (mono_class_is_marshalbyref (method->klass)) ||
5313 /* also consider num_locals? */
5314 /* Do the size check early to avoid creating vtables */
5315 if (!inline_limit_inited) {
5316 if (g_getenv ("MONO_INLINELIMIT"))
5317 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5319 inline_limit = INLINE_LENGTH_LIMIT;
5320 inline_limit_inited = TRUE;
5322 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5326 * if we can initialize the class of the method right away, we do,
5327 * otherwise we don't allow inlining if the class needs initialization,
5328 * since it would mean inserting a call to mono_runtime_class_init()
5329 * inside the inlined code
5331 if (!(cfg->opt & MONO_OPT_SHARED)) {
5332 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5333 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5334 vtable = mono_class_vtable (cfg->domain, method->klass);
5337 if (!cfg->compile_aot)
5338 mono_runtime_class_init (vtable);
5339 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5340 if (cfg->run_cctors && method->klass->has_cctor) {
5341 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5342 if (!method->klass->runtime_info)
5343 /* No vtable created yet */
5345 vtable = mono_class_vtable (cfg->domain, method->klass);
5348 /* This makes so that inline cannot trigger */
5349 /* .cctors: too many apps depend on them */
5350 /* running with a specific order... */
5351 if (! vtable->initialized)
5353 mono_runtime_class_init (vtable);
5355 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5356 if (!method->klass->runtime_info)
5357 /* No vtable created yet */
5359 vtable = mono_class_vtable (cfg->domain, method->klass);
5362 if (!vtable->initialized)
5367 * If we're compiling for shared code
5368 * the cctor will need to be run at aot method load time, for example,
5369 * or at the end of the compilation of the inlining method.
5371 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5375 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5376 if (mono_arch_is_soft_float ()) {
5378 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5380 for (i = 0; i < sig->param_count; ++i)
5381 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5386 if (g_list_find (cfg->dont_inline, method))
5393 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5395 if (!cfg->compile_aot) {
5397 if (vtable->initialized)
5401 if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5402 if (cfg->method == method)
5406 if (!mono_class_needs_cctor_run (klass, method))
5409 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5410 /* The initialization is already done before the method is called */
5417 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5421 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5424 if (mini_is_gsharedvt_variable_klass (klass)) {
5427 mono_class_init (klass);
5428 size = mono_class_array_element_size (klass);
5431 mult_reg = alloc_preg (cfg);
5432 array_reg = arr->dreg;
5433 index_reg = index->dreg;
5435 #if SIZEOF_REGISTER == 8
5436 /* The array reg is 64 bits but the index reg is only 32 */
5437 if (COMPILE_LLVM (cfg)) {
5439 index2_reg = index_reg;
5441 index2_reg = alloc_preg (cfg);
5442 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5445 if (index->type == STACK_I8) {
5446 index2_reg = alloc_preg (cfg);
5447 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5449 index2_reg = index_reg;
5454 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5456 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5457 if (size == 1 || size == 2 || size == 4 || size == 8) {
5458 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5460 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5461 ins->klass = mono_class_get_element_class (klass);
5462 ins->type = STACK_MP;
5468 add_reg = alloc_ireg_mp (cfg);
5471 MonoInst *rgctx_ins;
5474 g_assert (cfg->gshared);
5475 context_used = mini_class_check_context_used (cfg, klass);
5476 g_assert (context_used);
5477 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5478 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5480 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5482 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5483 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5484 ins->klass = mono_class_get_element_class (klass);
5485 ins->type = STACK_MP;
5486 MONO_ADD_INS (cfg->cbb, ins);
5492 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5494 int bounds_reg = alloc_preg (cfg);
5495 int add_reg = alloc_ireg_mp (cfg);
5496 int mult_reg = alloc_preg (cfg);
5497 int mult2_reg = alloc_preg (cfg);
5498 int low1_reg = alloc_preg (cfg);
5499 int low2_reg = alloc_preg (cfg);
5500 int high1_reg = alloc_preg (cfg);
5501 int high2_reg = alloc_preg (cfg);
5502 int realidx1_reg = alloc_preg (cfg);
5503 int realidx2_reg = alloc_preg (cfg);
5504 int sum_reg = alloc_preg (cfg);
5505 int index1, index2, tmpreg;
5509 mono_class_init (klass);
5510 size = mono_class_array_element_size (klass);
5512 index1 = index_ins1->dreg;
5513 index2 = index_ins2->dreg;
5515 #if SIZEOF_REGISTER == 8
5516 /* The array reg is 64 bits but the index reg is only 32 */
5517 if (COMPILE_LLVM (cfg)) {
5520 tmpreg = alloc_preg (cfg);
5521 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5523 tmpreg = alloc_preg (cfg);
5524 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5528 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5532 /* range checking */
5533 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
5534 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5536 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
5537 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5538 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5539 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
5540 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5541 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5542 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5544 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
5545 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5546 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5547 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
5548 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5549 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5550 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5552 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5553 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5554 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5555 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5556 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5558 ins->type = STACK_MP;
5560 MONO_ADD_INS (cfg->cbb, ins);
5566 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5570 MonoMethod *addr_method;
5572 MonoClass *eclass = cmethod->klass->element_class;
5574 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5577 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5579 /* emit_ldelema_2 depends on OP_LMUL */
5580 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5581 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5584 if (mini_is_gsharedvt_variable_klass (eclass))
5587 element_size = mono_class_array_element_size (eclass);
5588 addr_method = mono_marshal_get_array_address (rank, element_size);
5589 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5594 static MonoBreakPolicy
5595 always_insert_breakpoint (MonoMethod *method)
5597 return MONO_BREAK_POLICY_ALWAYS;
5600 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5603 * mono_set_break_policy:
5604 * policy_callback: the new callback function
5606 * Allow embedders to decide wherther to actually obey breakpoint instructions
5607 * (both break IL instructions and Debugger.Break () method calls), for example
5608 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5609 * untrusted or semi-trusted code.
5611 * @policy_callback will be called every time a break point instruction needs to
5612 * be inserted with the method argument being the method that calls Debugger.Break()
5613 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5614 * if it wants the breakpoint to not be effective in the given method.
5615 * #MONO_BREAK_POLICY_ALWAYS is the default.
5618 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5620 if (policy_callback)
5621 break_policy_func = policy_callback;
5623 break_policy_func = always_insert_breakpoint;
5627 should_insert_brekpoint (MonoMethod *method) {
5628 switch (break_policy_func (method)) {
5629 case MONO_BREAK_POLICY_ALWAYS:
5631 case MONO_BREAK_POLICY_NEVER:
5633 case MONO_BREAK_POLICY_ON_DBG:
5634 g_warning ("mdb no longer supported");
5637 g_warning ("Incorrect value returned from break policy callback");
5642 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5644 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5646 MonoInst *addr, *store, *load;
5647 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5649 /* the bounds check is already done by the callers */
5650 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5652 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5653 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5654 if (mini_type_is_reference (fsig->params [2]))
5655 emit_write_barrier (cfg, addr, load);
5657 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5658 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5665 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5667 return mini_type_is_reference (&klass->byval_arg);
5671 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5673 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5674 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5675 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5676 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5677 MonoInst *iargs [3];
5680 mono_class_setup_vtable (obj_array);
5681 g_assert (helper->slot);
5683 if (sp [0]->type != STACK_OBJ)
5685 if (sp [2]->type != STACK_OBJ)
5692 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5696 if (mini_is_gsharedvt_variable_klass (klass)) {
5699 // FIXME-VT: OP_ICONST optimization
5700 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5701 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5702 ins->opcode = OP_STOREV_MEMBASE;
5703 } else if (sp [1]->opcode == OP_ICONST) {
5704 int array_reg = sp [0]->dreg;
5705 int index_reg = sp [1]->dreg;
5706 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5708 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5709 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5712 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5713 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5715 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5716 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5717 if (generic_class_is_reference_type (cfg, klass))
5718 emit_write_barrier (cfg, addr, sp [2]);
5725 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5730 eklass = mono_class_from_mono_type (fsig->params [2]);
5732 eklass = mono_class_from_mono_type (fsig->ret);
5735 return emit_array_store (cfg, eklass, args, FALSE);
5737 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5738 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5744 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5747 int param_size, return_size;
5749 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
5750 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5752 if (cfg->verbose_level > 3)
5753 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5755 //Don't allow mixing reference types with value types
5756 if (param_klass->valuetype != return_klass->valuetype) {
5757 if (cfg->verbose_level > 3)
5758 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5762 if (!param_klass->valuetype) {
5763 if (cfg->verbose_level > 3)
5764 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5769 if (param_klass->has_references || return_klass->has_references)
5772 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5773 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5774 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5775 if (cfg->verbose_level > 3)
5776 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5780 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5781 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5782 if (cfg->verbose_level > 3)
5783 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5787 param_size = mono_class_value_size (param_klass, &align);
5788 return_size = mono_class_value_size (return_klass, &align);
5790 //We can do it if sizes match
5791 if (param_size == return_size) {
5792 if (cfg->verbose_level > 3)
5793 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5797 //No simple way to handle struct if sizes don't match
5798 if (MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg)) {
5799 if (cfg->verbose_level > 3)
5800 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5805 * Same reg size category.
5806 * A quick note on why we don't require widening here.
5807 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5809 * Since the source value comes from a function argument, the JIT will already have
5810 * the value in a VREG and performed any widening needed before (say, when loading from a field).
5812 if (param_size <= 4 && return_size <= 4) {
5813 if (cfg->verbose_level > 3)
5814 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5822 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5824 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5825 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5827 if (mini_is_gsharedvt_variable_type (fsig->ret))
5830 //Valuetypes that are semantically equivalent or numbers than can be widened to
5831 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5834 //Arrays of valuetypes that are semantically equivalent
5835 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5842 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5844 #ifdef MONO_ARCH_SIMD_INTRINSICS
5845 MonoInst *ins = NULL;
5847 if (cfg->opt & MONO_OPT_SIMD) {
5848 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5854 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5858 emit_memory_barrier (MonoCompile *cfg, int kind)
5860 MonoInst *ins = NULL;
5861 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5862 MONO_ADD_INS (cfg->cbb, ins);
5863 ins->backend.memory_barrier_kind = kind;
5869 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5871 MonoInst *ins = NULL;
5874 /* The LLVM backend supports these intrinsics */
5875 if (cmethod->klass == mono_defaults.math_class) {
5876 if (strcmp (cmethod->name, "Sin") == 0) {
5878 } else if (strcmp (cmethod->name, "Cos") == 0) {
5880 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5882 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5886 if (opcode && fsig->param_count == 1) {
5887 MONO_INST_NEW (cfg, ins, opcode);
5888 ins->type = STACK_R8;
5889 ins->dreg = mono_alloc_freg (cfg);
5890 ins->sreg1 = args [0]->dreg;
5891 MONO_ADD_INS (cfg->cbb, ins);
5895 if (cfg->opt & MONO_OPT_CMOV) {
5896 if (strcmp (cmethod->name, "Min") == 0) {
5897 if (fsig->params [0]->type == MONO_TYPE_I4)
5899 if (fsig->params [0]->type == MONO_TYPE_U4)
5900 opcode = OP_IMIN_UN;
5901 else if (fsig->params [0]->type == MONO_TYPE_I8)
5903 else if (fsig->params [0]->type == MONO_TYPE_U8)
5904 opcode = OP_LMIN_UN;
5905 } else if (strcmp (cmethod->name, "Max") == 0) {
5906 if (fsig->params [0]->type == MONO_TYPE_I4)
5908 if (fsig->params [0]->type == MONO_TYPE_U4)
5909 opcode = OP_IMAX_UN;
5910 else if (fsig->params [0]->type == MONO_TYPE_I8)
5912 else if (fsig->params [0]->type == MONO_TYPE_U8)
5913 opcode = OP_LMAX_UN;
5917 if (opcode && fsig->param_count == 2) {
5918 MONO_INST_NEW (cfg, ins, opcode);
5919 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5920 ins->dreg = mono_alloc_ireg (cfg);
5921 ins->sreg1 = args [0]->dreg;
5922 ins->sreg2 = args [1]->dreg;
5923 MONO_ADD_INS (cfg->cbb, ins);
5931 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5933 if (cmethod->klass == mono_defaults.array_class) {
5934 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5935 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5936 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5937 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5938 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5939 return emit_array_unsafe_mov (cfg, fsig, args);
5946 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5948 MonoInst *ins = NULL;
5950 static MonoClass *runtime_helpers_class = NULL;
5951 if (! runtime_helpers_class)
5952 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
5953 "System.Runtime.CompilerServices", "RuntimeHelpers");
5955 if (cmethod->klass == mono_defaults.string_class) {
5956 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5957 int dreg = alloc_ireg (cfg);
5958 int index_reg = alloc_preg (cfg);
5959 int add_reg = alloc_preg (cfg);
5961 #if SIZEOF_REGISTER == 8
5962 if (COMPILE_LLVM (cfg)) {
5963 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5965 /* The array reg is 64 bits but the index reg is only 32 */
5966 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5969 index_reg = args [1]->dreg;
5971 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5973 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5974 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5975 add_reg = ins->dreg;
5976 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5979 int mult_reg = alloc_preg (cfg);
5980 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5981 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5982 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5983 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5985 type_from_op (cfg, ins, NULL, NULL);
5987 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5988 int dreg = alloc_ireg (cfg);
5989 /* Decompose later to allow more optimizations */
5990 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5991 ins->type = STACK_I4;
5992 ins->flags |= MONO_INST_FAULT;
5993 cfg->cbb->has_array_access = TRUE;
5994 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5999 } else if (cmethod->klass == mono_defaults.object_class) {
6001 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
6002 int dreg = alloc_ireg_ref (cfg);
6003 int vt_reg = alloc_preg (cfg);
6004 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6005 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
6006 type_from_op (cfg, ins, NULL, NULL);
6009 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
6010 int dreg = alloc_ireg (cfg);
6011 int t1 = alloc_ireg (cfg);
6013 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
6014 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
6015 ins->type = STACK_I4;
6018 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
6019 MONO_INST_NEW (cfg, ins, OP_NOP);
6020 MONO_ADD_INS (cfg->cbb, ins);
6024 } else if (cmethod->klass == mono_defaults.array_class) {
6025 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6026 return emit_array_generic_access (cfg, fsig, args, FALSE);
6027 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6028 return emit_array_generic_access (cfg, fsig, args, TRUE);
6030 #ifndef MONO_BIG_ARRAYS
6032 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
6035 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
6036 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
6037 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
6038 int dreg = alloc_ireg (cfg);
6039 int bounds_reg = alloc_ireg_mp (cfg);
6040 MonoBasicBlock *end_bb, *szarray_bb;
6041 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
6043 NEW_BBLOCK (cfg, end_bb);
6044 NEW_BBLOCK (cfg, szarray_bb);
6046 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
6047 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
6048 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
6049 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
6050 /* Non-szarray case */
6052 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6053 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
6055 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6056 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
6057 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6058 MONO_START_BB (cfg, szarray_bb);
6061 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6062 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6064 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6065 MONO_START_BB (cfg, end_bb);
6067 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
6068 ins->type = STACK_I4;
6074 if (cmethod->name [0] != 'g')
6077 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
6078 int dreg = alloc_ireg (cfg);
6079 int vtable_reg = alloc_preg (cfg);
6080 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
6081 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6082 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
6083 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
6084 type_from_op (cfg, ins, NULL, NULL);
6087 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6088 int dreg = alloc_ireg (cfg);
6090 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6091 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6092 type_from_op (cfg, ins, NULL, NULL);
6097 } else if (cmethod->klass == runtime_helpers_class) {
6099 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
6100 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
6104 } else if (cmethod->klass == mono_defaults.thread_class) {
6105 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
6106 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
6107 MONO_ADD_INS (cfg->cbb, ins);
6109 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
6110 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6111 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
6113 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6115 if (fsig->params [0]->type == MONO_TYPE_I1)
6116 opcode = OP_LOADI1_MEMBASE;
6117 else if (fsig->params [0]->type == MONO_TYPE_U1)
6118 opcode = OP_LOADU1_MEMBASE;
6119 else if (fsig->params [0]->type == MONO_TYPE_I2)
6120 opcode = OP_LOADI2_MEMBASE;
6121 else if (fsig->params [0]->type == MONO_TYPE_U2)
6122 opcode = OP_LOADU2_MEMBASE;
6123 else if (fsig->params [0]->type == MONO_TYPE_I4)
6124 opcode = OP_LOADI4_MEMBASE;
6125 else if (fsig->params [0]->type == MONO_TYPE_U4)
6126 opcode = OP_LOADU4_MEMBASE;
6127 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6128 opcode = OP_LOADI8_MEMBASE;
6129 else if (fsig->params [0]->type == MONO_TYPE_R4)
6130 opcode = OP_LOADR4_MEMBASE;
6131 else if (fsig->params [0]->type == MONO_TYPE_R8)
6132 opcode = OP_LOADR8_MEMBASE;
6133 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6134 opcode = OP_LOAD_MEMBASE;
6137 MONO_INST_NEW (cfg, ins, opcode);
6138 ins->inst_basereg = args [0]->dreg;
6139 ins->inst_offset = 0;
6140 MONO_ADD_INS (cfg->cbb, ins);
6142 switch (fsig->params [0]->type) {
6149 ins->dreg = mono_alloc_ireg (cfg);
6150 ins->type = STACK_I4;
6154 ins->dreg = mono_alloc_lreg (cfg);
6155 ins->type = STACK_I8;
6159 ins->dreg = mono_alloc_ireg (cfg);
6160 #if SIZEOF_REGISTER == 8
6161 ins->type = STACK_I8;
6163 ins->type = STACK_I4;
6168 ins->dreg = mono_alloc_freg (cfg);
6169 ins->type = STACK_R8;
6172 g_assert (mini_type_is_reference (fsig->params [0]));
6173 ins->dreg = mono_alloc_ireg_ref (cfg);
6174 ins->type = STACK_OBJ;
6178 if (opcode == OP_LOADI8_MEMBASE)
6179 ins = mono_decompose_opcode (cfg, ins);
6181 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6185 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
6187 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6189 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
6190 opcode = OP_STOREI1_MEMBASE_REG;
6191 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
6192 opcode = OP_STOREI2_MEMBASE_REG;
6193 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
6194 opcode = OP_STOREI4_MEMBASE_REG;
6195 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6196 opcode = OP_STOREI8_MEMBASE_REG;
6197 else if (fsig->params [0]->type == MONO_TYPE_R4)
6198 opcode = OP_STORER4_MEMBASE_REG;
6199 else if (fsig->params [0]->type == MONO_TYPE_R8)
6200 opcode = OP_STORER8_MEMBASE_REG;
6201 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6202 opcode = OP_STORE_MEMBASE_REG;
6205 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6207 MONO_INST_NEW (cfg, ins, opcode);
6208 ins->sreg1 = args [1]->dreg;
6209 ins->inst_destbasereg = args [0]->dreg;
6210 ins->inst_offset = 0;
6211 MONO_ADD_INS (cfg->cbb, ins);
6213 if (opcode == OP_STOREI8_MEMBASE_REG)
6214 ins = mono_decompose_opcode (cfg, ins);
6219 } else if (cmethod->klass->image == mono_defaults.corlib &&
6220 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6221 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6224 #if SIZEOF_REGISTER == 8
6225 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6226 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6227 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6228 ins->dreg = mono_alloc_preg (cfg);
6229 ins->sreg1 = args [0]->dreg;
6230 ins->type = STACK_I8;
6231 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6232 MONO_ADD_INS (cfg->cbb, ins);
6236 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6238 /* 64 bit reads are already atomic */
6239 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6240 load_ins->dreg = mono_alloc_preg (cfg);
6241 load_ins->inst_basereg = args [0]->dreg;
6242 load_ins->inst_offset = 0;
6243 load_ins->type = STACK_I8;
6244 MONO_ADD_INS (cfg->cbb, load_ins);
6246 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6253 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6254 MonoInst *ins_iconst;
6257 if (fsig->params [0]->type == MONO_TYPE_I4) {
6258 opcode = OP_ATOMIC_ADD_I4;
6259 cfg->has_atomic_add_i4 = TRUE;
6261 #if SIZEOF_REGISTER == 8
6262 else if (fsig->params [0]->type == MONO_TYPE_I8)
6263 opcode = OP_ATOMIC_ADD_I8;
6266 if (!mono_arch_opcode_supported (opcode))
6268 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6269 ins_iconst->inst_c0 = 1;
6270 ins_iconst->dreg = mono_alloc_ireg (cfg);
6271 MONO_ADD_INS (cfg->cbb, ins_iconst);
6273 MONO_INST_NEW (cfg, ins, opcode);
6274 ins->dreg = mono_alloc_ireg (cfg);
6275 ins->inst_basereg = args [0]->dreg;
6276 ins->inst_offset = 0;
6277 ins->sreg2 = ins_iconst->dreg;
6278 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6279 MONO_ADD_INS (cfg->cbb, ins);
6281 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6282 MonoInst *ins_iconst;
6285 if (fsig->params [0]->type == MONO_TYPE_I4) {
6286 opcode = OP_ATOMIC_ADD_I4;
6287 cfg->has_atomic_add_i4 = TRUE;
6289 #if SIZEOF_REGISTER == 8
6290 else if (fsig->params [0]->type == MONO_TYPE_I8)
6291 opcode = OP_ATOMIC_ADD_I8;
6294 if (!mono_arch_opcode_supported (opcode))
6296 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6297 ins_iconst->inst_c0 = -1;
6298 ins_iconst->dreg = mono_alloc_ireg (cfg);
6299 MONO_ADD_INS (cfg->cbb, ins_iconst);
6301 MONO_INST_NEW (cfg, ins, opcode);
6302 ins->dreg = mono_alloc_ireg (cfg);
6303 ins->inst_basereg = args [0]->dreg;
6304 ins->inst_offset = 0;
6305 ins->sreg2 = ins_iconst->dreg;
6306 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6307 MONO_ADD_INS (cfg->cbb, ins);
6309 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6312 if (fsig->params [0]->type == MONO_TYPE_I4) {
6313 opcode = OP_ATOMIC_ADD_I4;
6314 cfg->has_atomic_add_i4 = TRUE;
6316 #if SIZEOF_REGISTER == 8
6317 else if (fsig->params [0]->type == MONO_TYPE_I8)
6318 opcode = OP_ATOMIC_ADD_I8;
6321 if (!mono_arch_opcode_supported (opcode))
6323 MONO_INST_NEW (cfg, ins, opcode);
6324 ins->dreg = mono_alloc_ireg (cfg);
6325 ins->inst_basereg = args [0]->dreg;
6326 ins->inst_offset = 0;
6327 ins->sreg2 = args [1]->dreg;
6328 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6329 MONO_ADD_INS (cfg->cbb, ins);
6332 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6333 MonoInst *f2i = NULL, *i2f;
6334 guint32 opcode, f2i_opcode, i2f_opcode;
6335 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6336 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6338 if (fsig->params [0]->type == MONO_TYPE_I4 ||
6339 fsig->params [0]->type == MONO_TYPE_R4) {
6340 opcode = OP_ATOMIC_EXCHANGE_I4;
6341 f2i_opcode = OP_MOVE_F_TO_I4;
6342 i2f_opcode = OP_MOVE_I4_TO_F;
6343 cfg->has_atomic_exchange_i4 = TRUE;
6345 #if SIZEOF_REGISTER == 8
6347 fsig->params [0]->type == MONO_TYPE_I8 ||
6348 fsig->params [0]->type == MONO_TYPE_R8 ||
6349 fsig->params [0]->type == MONO_TYPE_I) {
6350 opcode = OP_ATOMIC_EXCHANGE_I8;
6351 f2i_opcode = OP_MOVE_F_TO_I8;
6352 i2f_opcode = OP_MOVE_I8_TO_F;
6355 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6356 opcode = OP_ATOMIC_EXCHANGE_I4;
6357 cfg->has_atomic_exchange_i4 = TRUE;
6363 if (!mono_arch_opcode_supported (opcode))
6367 /* TODO: Decompose these opcodes instead of bailing here. */
6368 if (COMPILE_SOFT_FLOAT (cfg))
6371 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6372 f2i->dreg = mono_alloc_ireg (cfg);
6373 f2i->sreg1 = args [1]->dreg;
6374 if (f2i_opcode == OP_MOVE_F_TO_I4)
6375 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6376 MONO_ADD_INS (cfg->cbb, f2i);
6379 MONO_INST_NEW (cfg, ins, opcode);
6380 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6381 ins->inst_basereg = args [0]->dreg;
6382 ins->inst_offset = 0;
6383 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6384 MONO_ADD_INS (cfg->cbb, ins);
6386 switch (fsig->params [0]->type) {
6388 ins->type = STACK_I4;
6391 ins->type = STACK_I8;
6394 #if SIZEOF_REGISTER == 8
6395 ins->type = STACK_I8;
6397 ins->type = STACK_I4;
6402 ins->type = STACK_R8;
6405 g_assert (mini_type_is_reference (fsig->params [0]));
6406 ins->type = STACK_OBJ;
6411 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6412 i2f->dreg = mono_alloc_freg (cfg);
6413 i2f->sreg1 = ins->dreg;
6414 i2f->type = STACK_R8;
6415 if (i2f_opcode == OP_MOVE_I4_TO_F)
6416 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6417 MONO_ADD_INS (cfg->cbb, i2f);
6422 if (cfg->gen_write_barriers && is_ref)
6423 emit_write_barrier (cfg, args [0], args [1]);
6425 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6426 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6427 guint32 opcode, f2i_opcode, i2f_opcode;
6428 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6429 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6431 if (fsig->params [1]->type == MONO_TYPE_I4 ||
6432 fsig->params [1]->type == MONO_TYPE_R4) {
6433 opcode = OP_ATOMIC_CAS_I4;
6434 f2i_opcode = OP_MOVE_F_TO_I4;
6435 i2f_opcode = OP_MOVE_I4_TO_F;
6436 cfg->has_atomic_cas_i4 = TRUE;
6438 #if SIZEOF_REGISTER == 8
6440 fsig->params [1]->type == MONO_TYPE_I8 ||
6441 fsig->params [1]->type == MONO_TYPE_R8 ||
6442 fsig->params [1]->type == MONO_TYPE_I) {
6443 opcode = OP_ATOMIC_CAS_I8;
6444 f2i_opcode = OP_MOVE_F_TO_I8;
6445 i2f_opcode = OP_MOVE_I8_TO_F;
6448 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6449 opcode = OP_ATOMIC_CAS_I4;
6450 cfg->has_atomic_cas_i4 = TRUE;
6456 if (!mono_arch_opcode_supported (opcode))
6460 /* TODO: Decompose these opcodes instead of bailing here. */
6461 if (COMPILE_SOFT_FLOAT (cfg))
6464 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6465 f2i_new->dreg = mono_alloc_ireg (cfg);
6466 f2i_new->sreg1 = args [1]->dreg;
6467 if (f2i_opcode == OP_MOVE_F_TO_I4)
6468 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6469 MONO_ADD_INS (cfg->cbb, f2i_new);
6471 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6472 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6473 f2i_cmp->sreg1 = args [2]->dreg;
6474 if (f2i_opcode == OP_MOVE_F_TO_I4)
6475 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6476 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6479 MONO_INST_NEW (cfg, ins, opcode);
6480 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6481 ins->sreg1 = args [0]->dreg;
6482 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6483 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6484 MONO_ADD_INS (cfg->cbb, ins);
6486 switch (fsig->params [1]->type) {
6488 ins->type = STACK_I4;
6491 ins->type = STACK_I8;
6494 #if SIZEOF_REGISTER == 8
6495 ins->type = STACK_I8;
6497 ins->type = STACK_I4;
6501 ins->type = cfg->r4_stack_type;
6504 ins->type = STACK_R8;
6507 g_assert (mini_type_is_reference (fsig->params [1]));
6508 ins->type = STACK_OBJ;
6513 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6514 i2f->dreg = mono_alloc_freg (cfg);
6515 i2f->sreg1 = ins->dreg;
6516 i2f->type = STACK_R8;
6517 if (i2f_opcode == OP_MOVE_I4_TO_F)
6518 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6519 MONO_ADD_INS (cfg->cbb, i2f);
6524 if (cfg->gen_write_barriers && is_ref)
6525 emit_write_barrier (cfg, args [0], args [1]);
6527 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6528 fsig->params [1]->type == MONO_TYPE_I4) {
6529 MonoInst *cmp, *ceq;
6531 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6534 /* int32 r = CAS (location, value, comparand); */
6535 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6536 ins->dreg = alloc_ireg (cfg);
6537 ins->sreg1 = args [0]->dreg;
6538 ins->sreg2 = args [1]->dreg;
6539 ins->sreg3 = args [2]->dreg;
6540 ins->type = STACK_I4;
6541 MONO_ADD_INS (cfg->cbb, ins);
6543 /* bool result = r == comparand; */
6544 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6545 cmp->sreg1 = ins->dreg;
6546 cmp->sreg2 = args [2]->dreg;
6547 cmp->type = STACK_I4;
6548 MONO_ADD_INS (cfg->cbb, cmp);
6550 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6551 ceq->dreg = alloc_ireg (cfg);
6552 ceq->type = STACK_I4;
6553 MONO_ADD_INS (cfg->cbb, ceq);
6555 /* *success = result; */
6556 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6558 cfg->has_atomic_cas_i4 = TRUE;
6560 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6561 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6565 } else if (cmethod->klass->image == mono_defaults.corlib &&
6566 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6567 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6570 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6572 MonoType *t = fsig->params [0];
6574 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
6576 g_assert (t->byref);
6577 /* t is a byref type, so the reference check is more complicated */
6578 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6579 if (t->type == MONO_TYPE_I1)
6580 opcode = OP_ATOMIC_LOAD_I1;
6581 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6582 opcode = OP_ATOMIC_LOAD_U1;
6583 else if (t->type == MONO_TYPE_I2)
6584 opcode = OP_ATOMIC_LOAD_I2;
6585 else if (t->type == MONO_TYPE_U2)
6586 opcode = OP_ATOMIC_LOAD_U2;
6587 else if (t->type == MONO_TYPE_I4)
6588 opcode = OP_ATOMIC_LOAD_I4;
6589 else if (t->type == MONO_TYPE_U4)
6590 opcode = OP_ATOMIC_LOAD_U4;
6591 else if (t->type == MONO_TYPE_R4)
6592 opcode = OP_ATOMIC_LOAD_R4;
6593 else if (t->type == MONO_TYPE_R8)
6594 opcode = OP_ATOMIC_LOAD_R8;
6595 #if SIZEOF_REGISTER == 8
6596 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6597 opcode = OP_ATOMIC_LOAD_I8;
6598 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6599 opcode = OP_ATOMIC_LOAD_U8;
6601 else if (t->type == MONO_TYPE_I)
6602 opcode = OP_ATOMIC_LOAD_I4;
6603 else if (is_ref || t->type == MONO_TYPE_U)
6604 opcode = OP_ATOMIC_LOAD_U4;
6608 if (!mono_arch_opcode_supported (opcode))
6611 MONO_INST_NEW (cfg, ins, opcode);
6612 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6613 ins->sreg1 = args [0]->dreg;
6614 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6615 MONO_ADD_INS (cfg->cbb, ins);
6618 case MONO_TYPE_BOOLEAN:
6625 ins->type = STACK_I4;
6629 ins->type = STACK_I8;
6633 #if SIZEOF_REGISTER == 8
6634 ins->type = STACK_I8;
6636 ins->type = STACK_I4;
6640 ins->type = cfg->r4_stack_type;
6643 ins->type = STACK_R8;
6647 ins->type = STACK_OBJ;
6653 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6655 MonoType *t = fsig->params [0];
6658 g_assert (t->byref);
6659 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6660 if (t->type == MONO_TYPE_I1)
6661 opcode = OP_ATOMIC_STORE_I1;
6662 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6663 opcode = OP_ATOMIC_STORE_U1;
6664 else if (t->type == MONO_TYPE_I2)
6665 opcode = OP_ATOMIC_STORE_I2;
6666 else if (t->type == MONO_TYPE_U2)
6667 opcode = OP_ATOMIC_STORE_U2;
6668 else if (t->type == MONO_TYPE_I4)
6669 opcode = OP_ATOMIC_STORE_I4;
6670 else if (t->type == MONO_TYPE_U4)
6671 opcode = OP_ATOMIC_STORE_U4;
6672 else if (t->type == MONO_TYPE_R4)
6673 opcode = OP_ATOMIC_STORE_R4;
6674 else if (t->type == MONO_TYPE_R8)
6675 opcode = OP_ATOMIC_STORE_R8;
6676 #if SIZEOF_REGISTER == 8
6677 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6678 opcode = OP_ATOMIC_STORE_I8;
6679 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6680 opcode = OP_ATOMIC_STORE_U8;
6682 else if (t->type == MONO_TYPE_I)
6683 opcode = OP_ATOMIC_STORE_I4;
6684 else if (is_ref || t->type == MONO_TYPE_U)
6685 opcode = OP_ATOMIC_STORE_U4;
6689 if (!mono_arch_opcode_supported (opcode))
6692 MONO_INST_NEW (cfg, ins, opcode);
6693 ins->dreg = args [0]->dreg;
6694 ins->sreg1 = args [1]->dreg;
6695 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6696 MONO_ADD_INS (cfg->cbb, ins);
6698 if (cfg->gen_write_barriers && is_ref)
6699 emit_write_barrier (cfg, args [0], args [1]);
6705 } else if (cmethod->klass->image == mono_defaults.corlib &&
6706 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6707 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6708 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6709 if (should_insert_brekpoint (cfg->method)) {
6710 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6712 MONO_INST_NEW (cfg, ins, OP_NOP);
6713 MONO_ADD_INS (cfg->cbb, ins);
6717 } else if (cmethod->klass->image == mono_defaults.corlib &&
6718 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6719 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6720 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6722 EMIT_NEW_ICONST (cfg, ins, 1);
6724 EMIT_NEW_ICONST (cfg, ins, 0);
6727 } else if (cmethod->klass->image == mono_defaults.corlib &&
6728 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6729 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6730 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6731 /* No stack walks are currently available, so implement this as an intrinsic */
6732 MonoInst *assembly_ins;
6734 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6735 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6738 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCallingAssembly")) {
6739 /* No stack walks are currently available, so implement this as an intrinsic */
6740 ins = mono_emit_jit_icall (cfg, mono_llvmonly_get_calling_assembly, NULL);
6743 } else if (cmethod->klass->image == mono_defaults.corlib &&
6744 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6745 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6746 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6747 /* No stack walks are currently available, so implement this as an intrinsic */
6748 MonoInst *method_ins;
6749 MonoMethod *declaring = cfg->method;
6751 /* This returns the declaring generic method */
6752 if (declaring->is_inflated)
6753 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6754 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6755 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6756 cfg->no_inline = TRUE;
6757 if (cfg->method != cfg->current_method)
6758 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6761 } else if (cmethod->klass == mono_defaults.math_class) {
6763 * There is general branchless code for Min/Max, but it does not work for
6765 * http://everything2.com/?node_id=1051618
6767 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6768 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6769 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6770 !strcmp (cmethod->klass->name, "Selector")) ||
6771 (!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") &&
6772 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6773 !strcmp (cmethod->klass->name, "Selector"))
6775 if (cfg->backend->have_objc_get_selector &&
6776 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6777 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6778 cfg->compile_aot && !cfg->llvm_only) {
6780 MonoJumpInfoToken *ji;
6785 cfg->exception_message = g_strdup ("GetHandle");
6786 cfg->disable_llvm = TRUE;
6788 if (args [0]->opcode == OP_GOT_ENTRY) {
6789 pi = (MonoInst *)args [0]->inst_p1;
6790 g_assert (pi->opcode == OP_PATCH_INFO);
6791 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6792 ji = (MonoJumpInfoToken *)pi->inst_p0;
6794 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6795 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6798 NULLIFY_INS (args [0]);
6801 s = mono_ldstr (cfg->domain, ji->image, mono_metadata_token_index (ji->token));
6802 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6803 ins->dreg = mono_alloc_ireg (cfg);
6805 ins->inst_p0 = mono_string_to_utf8 (s);
6806 MONO_ADD_INS (cfg->cbb, ins);
6811 #ifdef MONO_ARCH_SIMD_INTRINSICS
6812 if (cfg->opt & MONO_OPT_SIMD) {
6813 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6819 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6823 if (COMPILE_LLVM (cfg)) {
6824 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6829 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6833 * This entry point could be used later for arbitrary method
6836 inline static MonoInst*
6837 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6838 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6840 if (method->klass == mono_defaults.string_class) {
6841 /* managed string allocation support */
6842 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6843 MonoInst *iargs [2];
6844 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6845 MonoMethod *managed_alloc = NULL;
6847 g_assert (vtable); /*Should not fail since it System.String*/
6848 #ifndef MONO_CROSS_COMPILE
6849 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6853 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6854 iargs [1] = args [0];
6855 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6862 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6864 MonoInst *store, *temp;
6867 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6868 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6871 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6872 * would be different than the MonoInst's used to represent arguments, and
6873 * the ldelema implementation can't deal with that.
6874 * Solution: When ldelema is used on an inline argument, create a var for
6875 * it, emit ldelema on that var, and emit the saving code below in
6876 * inline_method () if needed.
6878 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6879 cfg->args [i] = temp;
6880 /* This uses cfg->args [i] which is set by the preceeding line */
6881 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6882 store->cil_code = sp [0]->cil_code;
6887 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6888 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6890 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6892 check_inline_called_method_name_limit (MonoMethod *called_method)
6895 static const char *limit = NULL;
6897 if (limit == NULL) {
6898 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6900 if (limit_string != NULL)
6901 limit = limit_string;
6906 if (limit [0] != '\0') {
6907 char *called_method_name = mono_method_full_name (called_method, TRUE);
6909 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6910 g_free (called_method_name);
6912 //return (strncmp_result <= 0);
6913 return (strncmp_result == 0);
6920 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6922 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6925 static const char *limit = NULL;
6927 if (limit == NULL) {
6928 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6929 if (limit_string != NULL) {
6930 limit = limit_string;
6936 if (limit [0] != '\0') {
6937 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6939 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6940 g_free (caller_method_name);
6942 //return (strncmp_result <= 0);
6943 return (strncmp_result == 0);
6951 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6953 static double r8_0 = 0.0;
6954 static float r4_0 = 0.0;
6958 rtype = mini_get_underlying_type (rtype);
6962 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6963 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6964 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6965 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6966 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6967 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6968 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6969 ins->type = STACK_R4;
6970 ins->inst_p0 = (void*)&r4_0;
6972 MONO_ADD_INS (cfg->cbb, ins);
6973 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6974 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6975 ins->type = STACK_R8;
6976 ins->inst_p0 = (void*)&r8_0;
6978 MONO_ADD_INS (cfg->cbb, ins);
6979 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6980 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6981 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6982 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6983 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6985 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6990 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6994 rtype = mini_get_underlying_type (rtype);
6998 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6999 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7000 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
7001 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7002 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
7003 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7004 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
7005 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7006 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
7007 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7008 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7009 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7010 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7011 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7013 emit_init_rvar (cfg, dreg, rtype);
7017 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
7019 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
7021 MonoInst *var = cfg->locals [local];
7022 if (COMPILE_SOFT_FLOAT (cfg)) {
7024 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
7025 emit_init_rvar (cfg, reg, type);
7026 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
7029 emit_init_rvar (cfg, var->dreg, type);
7031 emit_dummy_init_rvar (cfg, var->dreg, type);
7038 * Return the cost of inlining CMETHOD.
7041 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
7042 guchar *ip, guint real_offset, gboolean inline_always)
7044 MonoInst *ins, *rvar = NULL;
7045 MonoMethodHeader *cheader;
7046 MonoBasicBlock *ebblock, *sbblock;
7048 MonoMethod *prev_inlined_method;
7049 MonoInst **prev_locals, **prev_args;
7050 MonoType **prev_arg_types;
7051 guint prev_real_offset;
7052 GHashTable *prev_cbb_hash;
7053 MonoBasicBlock **prev_cil_offset_to_bb;
7054 MonoBasicBlock *prev_cbb;
7055 unsigned char* prev_cil_start;
7056 guint32 prev_cil_offset_to_bb_len;
7057 MonoMethod *prev_current_method;
7058 MonoGenericContext *prev_generic_context;
7059 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
7061 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
7063 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
7064 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
7067 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
7068 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
7073 fsig = mono_method_signature (cmethod);
7075 if (cfg->verbose_level > 2)
7076 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7078 if (!cmethod->inline_info) {
7079 cfg->stat_inlineable_methods++;
7080 cmethod->inline_info = 1;
7083 /* allocate local variables */
7084 cheader = mono_method_get_header (cmethod);
7086 if (cheader == NULL || mono_loader_get_last_error ()) {
7088 mono_metadata_free_mh (cheader);
7089 if (inline_always && mono_loader_get_last_error ()) {
7090 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7091 mono_error_set_from_loader_error (&cfg->error);
7094 mono_loader_clear_error ();
7098 /*Must verify before creating locals as it can cause the JIT to assert.*/
7099 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
7100 mono_metadata_free_mh (cheader);
7104 /* allocate space to store the return value */
7105 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
7106 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
7109 prev_locals = cfg->locals;
7110 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
7111 for (i = 0; i < cheader->num_locals; ++i)
7112 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
7114 /* allocate start and end blocks */
7115 /* This is needed so if the inline is aborted, we can clean up */
7116 NEW_BBLOCK (cfg, sbblock);
7117 sbblock->real_offset = real_offset;
7119 NEW_BBLOCK (cfg, ebblock);
7120 ebblock->block_num = cfg->num_bblocks++;
7121 ebblock->real_offset = real_offset;
7123 prev_args = cfg->args;
7124 prev_arg_types = cfg->arg_types;
7125 prev_inlined_method = cfg->inlined_method;
7126 cfg->inlined_method = cmethod;
7127 cfg->ret_var_set = FALSE;
7128 cfg->inline_depth ++;
7129 prev_real_offset = cfg->real_offset;
7130 prev_cbb_hash = cfg->cbb_hash;
7131 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
7132 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
7133 prev_cil_start = cfg->cil_start;
7134 prev_cbb = cfg->cbb;
7135 prev_current_method = cfg->current_method;
7136 prev_generic_context = cfg->generic_context;
7137 prev_ret_var_set = cfg->ret_var_set;
7138 prev_disable_inline = cfg->disable_inline;
7140 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
7143 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
7145 ret_var_set = cfg->ret_var_set;
7147 cfg->inlined_method = prev_inlined_method;
7148 cfg->real_offset = prev_real_offset;
7149 cfg->cbb_hash = prev_cbb_hash;
7150 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
7151 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
7152 cfg->cil_start = prev_cil_start;
7153 cfg->locals = prev_locals;
7154 cfg->args = prev_args;
7155 cfg->arg_types = prev_arg_types;
7156 cfg->current_method = prev_current_method;
7157 cfg->generic_context = prev_generic_context;
7158 cfg->ret_var_set = prev_ret_var_set;
7159 cfg->disable_inline = prev_disable_inline;
7160 cfg->inline_depth --;
7162 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
7163 if (cfg->verbose_level > 2)
7164 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7166 cfg->stat_inlined_methods++;
7168 /* always add some code to avoid block split failures */
7169 MONO_INST_NEW (cfg, ins, OP_NOP);
7170 MONO_ADD_INS (prev_cbb, ins);
7172 prev_cbb->next_bb = sbblock;
7173 link_bblock (cfg, prev_cbb, sbblock);
7176 * Get rid of the begin and end bblocks if possible to aid local
7179 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
7181 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
7182 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
7184 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
7185 MonoBasicBlock *prev = ebblock->in_bb [0];
7187 if (prev->next_bb == ebblock) {
7188 mono_merge_basic_blocks (cfg, prev, ebblock);
7190 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
7191 mono_merge_basic_blocks (cfg, prev_cbb, prev);
7192 cfg->cbb = prev_cbb;
7195 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
7200 * Its possible that the rvar is set in some prev bblock, but not in others.
7206 for (i = 0; i < ebblock->in_count; ++i) {
7207 bb = ebblock->in_bb [i];
7209 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
7212 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7222 * If the inlined method contains only a throw, then the ret var is not
7223 * set, so set it to a dummy value.
7226 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7228 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
7231 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7234 if (cfg->verbose_level > 2)
7235 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
7236 cfg->exception_type = MONO_EXCEPTION_NONE;
7237 mono_loader_clear_error ();
7239 /* This gets rid of the newly added bblocks */
7240 cfg->cbb = prev_cbb;
7242 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7247 * Some of these comments may well be out-of-date.
7248 * Design decisions: we do a single pass over the IL code (and we do bblock
7249 * splitting/merging in the few cases when it's required: a back jump to an IL
7250 * address that was not already seen as bblock starting point).
7251 * Code is validated as we go (full verification is still better left to metadata/verify.c).
7252 * Complex operations are decomposed in simpler ones right away. We need to let the
7253 * arch-specific code peek and poke inside this process somehow (except when the
7254 * optimizations can take advantage of the full semantic info of coarse opcodes).
7255 * All the opcodes of the form opcode.s are 'normalized' to opcode.
7256 * MonoInst->opcode initially is the IL opcode or some simplification of that
7257 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
7258 * opcode with value bigger than OP_LAST.
7259 * At this point the IR can be handed over to an interpreter, a dumb code generator
7260 * or to the optimizing code generator that will translate it to SSA form.
7262 * Profiling directed optimizations.
7263 * We may compile by default with few or no optimizations and instrument the code
7264 * or the user may indicate what methods to optimize the most either in a config file
7265 * or through repeated runs where the compiler applies offline the optimizations to
7266 * each method and then decides if it was worth it.
7269 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7270 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7271 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7272 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7273 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7274 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7275 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7276 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) TYPE_LOAD_ERROR ((klass))
7278 /* offset from br.s -> br like opcodes */
7279 #define BIG_BRANCH_OFFSET 13
7282 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7284 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7286 return b == NULL || b == bb;
7290 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7292 unsigned char *ip = start;
7293 unsigned char *target;
7296 MonoBasicBlock *bblock;
7297 const MonoOpcode *opcode;
7300 cli_addr = ip - start;
7301 i = mono_opcode_value ((const guint8 **)&ip, end);
7304 opcode = &mono_opcodes [i];
7305 switch (opcode->argument) {
7306 case MonoInlineNone:
7309 case MonoInlineString:
7310 case MonoInlineType:
7311 case MonoInlineField:
7312 case MonoInlineMethod:
7315 case MonoShortInlineR:
7322 case MonoShortInlineVar:
7323 case MonoShortInlineI:
7326 case MonoShortInlineBrTarget:
7327 target = start + cli_addr + 2 + (signed char)ip [1];
7328 GET_BBLOCK (cfg, bblock, target);
7331 GET_BBLOCK (cfg, bblock, ip);
7333 case MonoInlineBrTarget:
7334 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7335 GET_BBLOCK (cfg, bblock, target);
7338 GET_BBLOCK (cfg, bblock, ip);
7340 case MonoInlineSwitch: {
7341 guint32 n = read32 (ip + 1);
7344 cli_addr += 5 + 4 * n;
7345 target = start + cli_addr;
7346 GET_BBLOCK (cfg, bblock, target);
7348 for (j = 0; j < n; ++j) {
7349 target = start + cli_addr + (gint32)read32 (ip);
7350 GET_BBLOCK (cfg, bblock, target);
7360 g_assert_not_reached ();
7363 if (i == CEE_THROW) {
7364 unsigned char *bb_start = ip - 1;
7366 /* Find the start of the bblock containing the throw */
7368 while ((bb_start >= start) && !bblock) {
7369 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7373 bblock->out_of_line = 1;
7383 static inline MonoMethod *
7384 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
7388 mono_error_init (error);
7390 if (m->wrapper_type != MONO_WRAPPER_NONE) {
7391 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
7393 method = mono_class_inflate_generic_method_checked (method, context, error);
7396 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
7402 static inline MonoMethod *
7403 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7406 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
7408 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
7409 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
7413 if (!method && !cfg)
7414 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7419 static inline MonoClass*
7420 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7425 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7426 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7428 klass = mono_class_inflate_generic_class (klass, context);
7430 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7431 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7434 mono_class_init (klass);
7438 static inline MonoMethodSignature*
7439 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
7441 MonoMethodSignature *fsig;
7443 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7444 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7446 fsig = mono_metadata_parse_signature (method->klass->image, token);
7450 fsig = mono_inflate_generic_signature(fsig, context, &error);
7452 g_assert(mono_error_ok(&error));
7458 throw_exception (void)
7460 static MonoMethod *method = NULL;
7463 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7464 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7471 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7473 MonoMethod *thrower = throw_exception ();
7476 EMIT_NEW_PCONST (cfg, args [0], ex);
7477 mono_emit_method_call (cfg, thrower, args, NULL);
7481 * Return the original method is a wrapper is specified. We can only access
7482 * the custom attributes from the original method.
7485 get_original_method (MonoMethod *method)
7487 if (method->wrapper_type == MONO_WRAPPER_NONE)
7490 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7491 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7494 /* in other cases we need to find the original method */
7495 return mono_marshal_method_from_wrapper (method);
7499 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7501 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7502 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7504 emit_throw_exception (cfg, ex);
7508 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7510 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7511 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7513 emit_throw_exception (cfg, ex);
7517 * Check that the IL instructions at ip are the array initialization
7518 * sequence and return the pointer to the data and the size.
7521 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7524 * newarr[System.Int32]
7526 * ldtoken field valuetype ...
7527 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7529 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7531 guint32 token = read32 (ip + 7);
7532 guint32 field_token = read32 (ip + 2);
7533 guint32 field_index = field_token & 0xffffff;
7535 const char *data_ptr;
7537 MonoMethod *cmethod;
7538 MonoClass *dummy_class;
7539 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7543 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7547 *out_field_token = field_token;
7549 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7552 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7554 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7555 case MONO_TYPE_BOOLEAN:
7559 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7560 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7561 case MONO_TYPE_CHAR:
7578 if (size > mono_type_size (field->type, &dummy_align))
7581 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7582 if (!image_is_dynamic (method->klass->image)) {
7583 field_index = read32 (ip + 2) & 0xffffff;
7584 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7585 data_ptr = mono_image_rva_map (method->klass->image, rva);
7586 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7587 /* for aot code we do the lookup on load */
7588 if (aot && data_ptr)
7589 return (const char *)GUINT_TO_POINTER (rva);
7591 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
7593 data_ptr = mono_field_get_data (field);
7601 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7603 char *method_fname = mono_method_full_name (method, TRUE);
7605 MonoMethodHeader *header = mono_method_get_header (method);
7607 if (header->code_size == 0)
7608 method_code = g_strdup ("method body is empty.");
7610 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7611 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
7612 g_free (method_fname);
7613 g_free (method_code);
7614 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7618 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7621 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7622 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
7623 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7624 /* Optimize reg-reg moves away */
7626 * Can't optimize other opcodes, since sp[0] might point to
7627 * the last ins of a decomposed opcode.
7629 sp [0]->dreg = (cfg)->locals [n]->dreg;
7631 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7636 * ldloca inhibits many optimizations so try to get rid of it in common
7639 static inline unsigned char *
7640 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7650 local = read16 (ip + 2);
7654 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7655 /* From the INITOBJ case */
7656 token = read32 (ip + 2);
7657 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7658 CHECK_TYPELOAD (klass);
7659 type = mini_get_underlying_type (&klass->byval_arg);
7660 emit_init_local (cfg, local, type, TRUE);
7668 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7670 MonoInst *icall_args [16];
7671 MonoInst *call_target, *ins, *vtable_ins;
7672 int arg_reg, this_reg, vtable_reg;
7673 gboolean is_iface = cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE;
7674 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7675 gboolean variant_iface = FALSE;
7680 * In llvm-only mode, vtables contain function descriptors instead of
7681 * method addresses/trampolines.
7683 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7686 slot = mono_method_get_imt_slot (cmethod);
7688 slot = mono_method_get_vtable_index (cmethod);
7690 this_reg = sp [0]->dreg;
7692 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7693 variant_iface = TRUE;
7695 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7697 * The simplest case, a normal virtual call.
7699 int slot_reg = alloc_preg (cfg);
7700 int addr_reg = alloc_preg (cfg);
7701 int arg_reg = alloc_preg (cfg);
7702 MonoBasicBlock *non_null_bb;
7704 vtable_reg = alloc_preg (cfg);
7705 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7706 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7708 /* Load the vtable slot, which contains a function descriptor. */
7709 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7711 NEW_BBLOCK (cfg, non_null_bb);
7713 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7714 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7715 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7718 // FIXME: Make the wrapper use the preserveall cconv
7719 // FIXME: Use one icall per slot for small slot numbers ?
7720 icall_args [0] = vtable_ins;
7721 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7722 /* Make the icall return the vtable slot value to save some code space */
7723 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7724 ins->dreg = slot_reg;
7725 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7728 MONO_START_BB (cfg, non_null_bb);
7729 /* Load the address + arg from the vtable slot */
7730 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7731 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7733 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7736 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7738 * A simple interface call
7740 * We make a call through an imt slot to obtain the function descriptor we need to call.
7741 * The imt slot contains a function descriptor for a runtime function + arg.
7743 int slot_reg = alloc_preg (cfg);
7744 int addr_reg = alloc_preg (cfg);
7745 int arg_reg = alloc_preg (cfg);
7746 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7748 vtable_reg = alloc_preg (cfg);
7749 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7750 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7753 * The slot is already initialized when the vtable is created so there is no need
7757 /* Load the imt slot, which contains a function descriptor. */
7758 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7760 /* Load the address + arg of the imt thunk from the imt slot */
7761 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7762 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7764 * IMT thunks in llvm-only mode are C functions which take an info argument
7765 * plus the imt method and return the ftndesc to call.
7767 icall_args [0] = thunk_arg_ins;
7768 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7769 cmethod, MONO_RGCTX_INFO_METHOD);
7770 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7772 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7775 if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7777 * This is similar to the interface case, the vtable slot points to an imt thunk which is
7778 * dynamically extended as more instantiations are discovered.
7779 * This handles generic virtual methods both on classes and interfaces.
7781 int slot_reg = alloc_preg (cfg);
7782 int addr_reg = alloc_preg (cfg);
7783 int arg_reg = alloc_preg (cfg);
7784 int ftndesc_reg = alloc_preg (cfg);
7785 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7786 MonoBasicBlock *slowpath_bb, *end_bb;
7788 NEW_BBLOCK (cfg, slowpath_bb);
7789 NEW_BBLOCK (cfg, end_bb);
7791 vtable_reg = alloc_preg (cfg);
7792 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7794 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7796 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7798 /* Load the slot, which contains a function descriptor. */
7799 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7801 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7802 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7803 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7804 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7807 /* Same as with iface calls */
7808 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7809 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7810 icall_args [0] = thunk_arg_ins;
7811 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7812 cmethod, MONO_RGCTX_INFO_METHOD);
7813 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7814 ftndesc_ins->dreg = ftndesc_reg;
7816 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7817 * they don't know about yet. Fall back to the slowpath in that case.
7819 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7820 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7822 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7825 MONO_START_BB (cfg, slowpath_bb);
7826 icall_args [0] = vtable_ins;
7827 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7828 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7829 cmethod, MONO_RGCTX_INFO_METHOD);
7831 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7833 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7834 ftndesc_ins->dreg = ftndesc_reg;
7835 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7838 MONO_START_BB (cfg, end_bb);
7839 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7843 * Non-optimized cases
7845 icall_args [0] = sp [0];
7846 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7848 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7849 cmethod, MONO_RGCTX_INFO_METHOD);
7851 arg_reg = alloc_preg (cfg);
7852 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7853 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7855 g_assert (is_gsharedvt);
7857 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7859 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7862 * Pass the extra argument even if the callee doesn't receive it, most
7863 * calling conventions allow this.
7865 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7869 is_exception_class (MonoClass *klass)
7872 if (klass == mono_defaults.exception_class)
7874 klass = klass->parent;
7880 * is_jit_optimizer_disabled:
7882 * Determine whenever M's assembly has a DebuggableAttribute with the
7883 * IsJITOptimizerDisabled flag set.
7886 is_jit_optimizer_disabled (MonoMethod *m)
7888 MonoAssembly *ass = m->klass->image->assembly;
7889 MonoCustomAttrInfo* attrs;
7890 static MonoClass *klass;
7892 gboolean val = FALSE;
7895 if (ass->jit_optimizer_disabled_inited)
7896 return ass->jit_optimizer_disabled;
7899 klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggableAttribute");
7902 ass->jit_optimizer_disabled = FALSE;
7903 mono_memory_barrier ();
7904 ass->jit_optimizer_disabled_inited = TRUE;
7908 attrs = mono_custom_attrs_from_assembly (ass);
7910 for (i = 0; i < attrs->num_attrs; ++i) {
7911 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7913 MonoMethodSignature *sig;
7915 if (!attr->ctor || attr->ctor->klass != klass)
7917 /* Decode the attribute. See reflection.c */
7918 p = (const char*)attr->data;
7919 g_assert (read16 (p) == 0x0001);
7922 // FIXME: Support named parameters
7923 sig = mono_method_signature (attr->ctor);
7924 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7926 /* Two boolean arguments */
7930 mono_custom_attrs_free (attrs);
7933 ass->jit_optimizer_disabled = val;
7934 mono_memory_barrier ();
7935 ass->jit_optimizer_disabled_inited = TRUE;
7941 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7943 gboolean supported_tail_call;
7946 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7948 for (i = 0; i < fsig->param_count; ++i) {
7949 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7950 /* These can point to the current method's stack */
7951 supported_tail_call = FALSE;
7953 if (fsig->hasthis && cmethod->klass->valuetype)
7954 /* this might point to the current method's stack */
7955 supported_tail_call = FALSE;
7956 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7957 supported_tail_call = FALSE;
7958 if (cfg->method->save_lmf)
7959 supported_tail_call = FALSE;
7960 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7961 supported_tail_call = FALSE;
7962 if (call_opcode != CEE_CALL)
7963 supported_tail_call = FALSE;
7965 /* Debugging support */
7967 if (supported_tail_call) {
7968 if (!mono_debug_count ())
7969 supported_tail_call = FALSE;
7973 return supported_tail_call;
7979 * Handle calls made to ctors from NEWOBJ opcodes.
7982 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7983 MonoInst **sp, guint8 *ip, int *inline_costs)
7985 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7987 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7988 mono_method_is_generic_sharable (cmethod, TRUE)) {
7989 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7990 mono_class_vtable (cfg->domain, cmethod->klass);
7991 CHECK_TYPELOAD (cmethod->klass);
7993 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7994 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7997 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
7998 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8000 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8002 CHECK_TYPELOAD (cmethod->klass);
8003 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8008 /* Avoid virtual calls to ctors if possible */
8009 if (mono_class_is_marshalbyref (cmethod->klass))
8010 callvirt_this_arg = sp [0];
8012 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
8013 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
8014 CHECK_CFG_EXCEPTION;
8015 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
8016 mono_method_check_inlining (cfg, cmethod) &&
8017 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
8020 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
8021 cfg->real_offset += 5;
8023 *inline_costs += costs - 5;
8025 INLINE_FAILURE ("inline failure");
8026 // FIXME-VT: Clean this up
8027 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8028 GSHAREDVT_FAILURE(*ip);
8029 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
8031 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8034 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
8036 if (cfg->llvm_only) {
8037 // FIXME: Avoid initializing vtable_arg
8038 emit_llvmonly_calli (cfg, fsig, sp, addr);
8040 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
8042 } else if (context_used &&
8043 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8044 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
8045 MonoInst *cmethod_addr;
8047 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
8049 if (cfg->llvm_only) {
8050 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
8051 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8052 emit_llvmonly_calli (cfg, fsig, sp, addr);
8054 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
8055 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8057 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
8060 INLINE_FAILURE ("ctor call");
8061 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
8062 callvirt_this_arg, NULL, vtable_arg);
8069 emit_setret (MonoCompile *cfg, MonoInst *val)
8071 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
8074 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
8077 if (!cfg->vret_addr) {
8078 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
8080 EMIT_NEW_RETLOADA (cfg, ret_addr);
8082 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
8083 ins->klass = mono_class_from_mono_type (ret_type);
8086 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
8087 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
8088 MonoInst *iargs [1];
8092 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
8093 mono_arch_emit_setret (cfg, cfg->method, conv);
8095 mono_arch_emit_setret (cfg, cfg->method, val);
8098 mono_arch_emit_setret (cfg, cfg->method, val);
8104 * mono_method_to_ir:
8106 * Translate the .net IL into linear IR.
8109 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
8110 MonoInst *return_var, MonoInst **inline_args,
8111 guint inline_offset, gboolean is_virtual_call)
8114 MonoInst *ins, **sp, **stack_start;
8115 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
8116 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
8117 MonoMethod *cmethod, *method_definition;
8118 MonoInst **arg_array;
8119 MonoMethodHeader *header;
8121 guint32 token, ins_flag;
8123 MonoClass *constrained_class = NULL;
8124 unsigned char *ip, *end, *target, *err_pos;
8125 MonoMethodSignature *sig;
8126 MonoGenericContext *generic_context = NULL;
8127 MonoGenericContainer *generic_container = NULL;
8128 MonoType **param_types;
8129 int i, n, start_new_bblock, dreg;
8130 int num_calls = 0, inline_costs = 0;
8131 int breakpoint_id = 0;
8133 GSList *class_inits = NULL;
8134 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
8136 gboolean init_locals, seq_points, skip_dead_blocks;
8137 gboolean sym_seq_points = FALSE;
8138 MonoDebugMethodInfo *minfo;
8139 MonoBitSet *seq_point_locs = NULL;
8140 MonoBitSet *seq_point_set_locs = NULL;
8142 cfg->disable_inline = is_jit_optimizer_disabled (method);
8144 /* serialization and xdomain stuff may need access to private fields and methods */
8145 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
8146 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
8147 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
8148 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
8149 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
8150 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
8152 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
8153 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
8154 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
8155 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
8156 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
8158 image = method->klass->image;
8159 header = mono_method_get_header (method);
8161 if (mono_loader_get_last_error ()) {
8162 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
8163 mono_error_set_from_loader_error (&cfg->error);
8165 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name));
8167 goto exception_exit;
8169 generic_container = mono_method_get_generic_container (method);
8170 sig = mono_method_signature (method);
8171 num_args = sig->hasthis + sig->param_count;
8172 ip = (unsigned char*)header->code;
8173 cfg->cil_start = ip;
8174 end = ip + header->code_size;
8175 cfg->stat_cil_code_size += header->code_size;
8177 seq_points = cfg->gen_seq_points && cfg->method == method;
8179 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8180 /* We could hit a seq point before attaching to the JIT (#8338) */
8184 if (cfg->gen_sdb_seq_points && cfg->method == method) {
8185 minfo = mono_debug_lookup_method (method);
8187 MonoSymSeqPoint *sps;
8188 int i, n_il_offsets;
8190 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
8191 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8192 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);
8193 sym_seq_points = TRUE;
8194 for (i = 0; i < n_il_offsets; ++i) {
8195 if (sps [i].il_offset < header->code_size)
8196 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
8199 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
8200 /* Methods without line number info like auto-generated property accessors */
8201 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8202 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);
8203 sym_seq_points = TRUE;
8208 * Methods without init_locals set could cause asserts in various passes
8209 * (#497220). To work around this, we emit dummy initialization opcodes
8210 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
8211 * on some platforms.
8213 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
8214 init_locals = header->init_locals;
8218 method_definition = method;
8219 while (method_definition->is_inflated) {
8220 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
8221 method_definition = imethod->declaring;
8224 /* SkipVerification is not allowed if core-clr is enabled */
8225 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
8227 dont_verify_stloc = TRUE;
8230 if (sig->is_inflated)
8231 generic_context = mono_method_get_context (method);
8232 else if (generic_container)
8233 generic_context = &generic_container->context;
8234 cfg->generic_context = generic_context;
8237 g_assert (!sig->has_type_parameters);
8239 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
8240 g_assert (method->is_inflated);
8241 g_assert (mono_method_get_context (method)->method_inst);
8243 if (method->is_inflated && mono_method_get_context (method)->method_inst)
8244 g_assert (sig->generic_param_count);
8246 if (cfg->method == method) {
8247 cfg->real_offset = 0;
8249 cfg->real_offset = inline_offset;
8252 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
8253 cfg->cil_offset_to_bb_len = header->code_size;
8255 cfg->current_method = method;
8257 if (cfg->verbose_level > 2)
8258 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
8260 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
8262 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
8263 for (n = 0; n < sig->param_count; ++n)
8264 param_types [n + sig->hasthis] = sig->params [n];
8265 cfg->arg_types = param_types;
8267 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
8268 if (cfg->method == method) {
8270 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
8271 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
8274 NEW_BBLOCK (cfg, start_bblock);
8275 cfg->bb_entry = start_bblock;
8276 start_bblock->cil_code = NULL;
8277 start_bblock->cil_length = 0;
8280 NEW_BBLOCK (cfg, end_bblock);
8281 cfg->bb_exit = end_bblock;
8282 end_bblock->cil_code = NULL;
8283 end_bblock->cil_length = 0;
8284 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
8285 g_assert (cfg->num_bblocks == 2);
8287 arg_array = cfg->args;
8289 if (header->num_clauses) {
8290 cfg->spvars = g_hash_table_new (NULL, NULL);
8291 cfg->exvars = g_hash_table_new (NULL, NULL);
8293 /* handle exception clauses */
8294 for (i = 0; i < header->num_clauses; ++i) {
8295 MonoBasicBlock *try_bb;
8296 MonoExceptionClause *clause = &header->clauses [i];
8297 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
8299 try_bb->real_offset = clause->try_offset;
8300 try_bb->try_start = TRUE;
8301 try_bb->region = ((i + 1) << 8) | clause->flags;
8302 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
8303 tblock->real_offset = clause->handler_offset;
8304 tblock->flags |= BB_EXCEPTION_HANDLER;
8307 * Linking the try block with the EH block hinders inlining as we won't be able to
8308 * merge the bblocks from inlining and produce an artificial hole for no good reason.
8310 if (COMPILE_LLVM (cfg))
8311 link_bblock (cfg, try_bb, tblock);
8313 if (*(ip + clause->handler_offset) == CEE_POP)
8314 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
8316 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
8317 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
8318 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
8319 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8320 MONO_ADD_INS (tblock, ins);
8322 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
8323 /* finally clauses already have a seq point */
8324 /* seq points for filter clauses are emitted below */
8325 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8326 MONO_ADD_INS (tblock, ins);
8329 /* todo: is a fault block unsafe to optimize? */
8330 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
8331 tblock->flags |= BB_EXCEPTION_UNSAFE;
8334 /*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);
8336 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
8338 /* catch and filter blocks get the exception object on the stack */
8339 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
8340 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8342 /* mostly like handle_stack_args (), but just sets the input args */
8343 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
8344 tblock->in_scount = 1;
8345 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8346 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8350 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
8351 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
8352 if (!cfg->compile_llvm) {
8353 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
8354 ins->dreg = tblock->in_stack [0]->dreg;
8355 MONO_ADD_INS (tblock, ins);
8358 MonoInst *dummy_use;
8361 * Add a dummy use for the exvar so its liveness info will be
8364 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
8367 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8368 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8369 MONO_ADD_INS (tblock, ins);
8372 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8373 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
8374 tblock->flags |= BB_EXCEPTION_HANDLER;
8375 tblock->real_offset = clause->data.filter_offset;
8376 tblock->in_scount = 1;
8377 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8378 /* The filter block shares the exvar with the handler block */
8379 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8380 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8381 MONO_ADD_INS (tblock, ins);
8385 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
8386 clause->data.catch_class &&
8388 mono_class_check_context_used (clause->data.catch_class)) {
8390 * In shared generic code with catch
8391 * clauses containing type variables
8392 * the exception handling code has to
8393 * be able to get to the rgctx.
8394 * Therefore we have to make sure that
8395 * the vtable/mrgctx argument (for
8396 * static or generic methods) or the
8397 * "this" argument (for non-static
8398 * methods) are live.
8400 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8401 mini_method_get_context (method)->method_inst ||
8402 method->klass->valuetype) {
8403 mono_get_vtable_var (cfg);
8405 MonoInst *dummy_use;
8407 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
8412 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
8413 cfg->cbb = start_bblock;
8414 cfg->args = arg_array;
8415 mono_save_args (cfg, sig, inline_args);
8418 /* FIRST CODE BLOCK */
8419 NEW_BBLOCK (cfg, tblock);
8420 tblock->cil_code = ip;
8424 ADD_BBLOCK (cfg, tblock);
8426 if (cfg->method == method) {
8427 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8428 if (breakpoint_id) {
8429 MONO_INST_NEW (cfg, ins, OP_BREAK);
8430 MONO_ADD_INS (cfg->cbb, ins);
8434 /* we use a separate basic block for the initialization code */
8435 NEW_BBLOCK (cfg, init_localsbb);
8436 cfg->bb_init = init_localsbb;
8437 init_localsbb->real_offset = cfg->real_offset;
8438 start_bblock->next_bb = init_localsbb;
8439 init_localsbb->next_bb = cfg->cbb;
8440 link_bblock (cfg, start_bblock, init_localsbb);
8441 link_bblock (cfg, init_localsbb, cfg->cbb);
8443 cfg->cbb = init_localsbb;
8445 if (cfg->gsharedvt && cfg->method == method) {
8446 MonoGSharedVtMethodInfo *info;
8447 MonoInst *var, *locals_var;
8450 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8451 info->method = cfg->method;
8452 info->count_entries = 16;
8453 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8454 cfg->gsharedvt_info = info;
8456 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8457 /* prevent it from being register allocated */
8458 //var->flags |= MONO_INST_VOLATILE;
8459 cfg->gsharedvt_info_var = var;
8461 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8462 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8464 /* Allocate locals */
8465 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8466 /* prevent it from being register allocated */
8467 //locals_var->flags |= MONO_INST_VOLATILE;
8468 cfg->gsharedvt_locals_var = locals_var;
8470 dreg = alloc_ireg (cfg);
8471 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8473 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8474 ins->dreg = locals_var->dreg;
8476 MONO_ADD_INS (cfg->cbb, ins);
8477 cfg->gsharedvt_locals_var_ins = ins;
8479 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8482 ins->flags |= MONO_INST_INIT;
8486 if (mono_security_core_clr_enabled ()) {
8487 /* check if this is native code, e.g. an icall or a p/invoke */
8488 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8489 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8491 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8492 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8494 /* if this ia a native call then it can only be JITted from platform code */
8495 if ((icall || pinvk) && method->klass && method->klass->image) {
8496 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8497 MonoException *ex = icall ? mono_get_exception_security () :
8498 mono_get_exception_method_access ();
8499 emit_throw_exception (cfg, ex);
8506 CHECK_CFG_EXCEPTION;
8508 if (header->code_size == 0)
8511 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8516 if (cfg->method == method)
8517 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8519 for (n = 0; n < header->num_locals; ++n) {
8520 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8525 /* We force the vtable variable here for all shared methods
8526 for the possibility that they might show up in a stack
8527 trace where their exact instantiation is needed. */
8528 if (cfg->gshared && method == cfg->method) {
8529 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8530 mini_method_get_context (method)->method_inst ||
8531 method->klass->valuetype) {
8532 mono_get_vtable_var (cfg);
8534 /* FIXME: Is there a better way to do this?
8535 We need the variable live for the duration
8536 of the whole method. */
8537 cfg->args [0]->flags |= MONO_INST_VOLATILE;
8541 /* add a check for this != NULL to inlined methods */
8542 if (is_virtual_call) {
8545 NEW_ARGLOAD (cfg, arg_ins, 0);
8546 MONO_ADD_INS (cfg->cbb, arg_ins);
8547 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8550 skip_dead_blocks = !dont_verify;
8551 if (skip_dead_blocks) {
8552 original_bb = bb = mono_basic_block_split (method, &cfg->error);
8557 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8558 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8561 start_new_bblock = 0;
8563 if (cfg->method == method)
8564 cfg->real_offset = ip - header->code;
8566 cfg->real_offset = inline_offset;
8571 if (start_new_bblock) {
8572 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8573 if (start_new_bblock == 2) {
8574 g_assert (ip == tblock->cil_code);
8576 GET_BBLOCK (cfg, tblock, ip);
8578 cfg->cbb->next_bb = tblock;
8580 start_new_bblock = 0;
8581 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8582 if (cfg->verbose_level > 3)
8583 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8584 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8588 g_slist_free (class_inits);
8591 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8592 link_bblock (cfg, cfg->cbb, tblock);
8593 if (sp != stack_start) {
8594 handle_stack_args (cfg, stack_start, sp - stack_start);
8596 CHECK_UNVERIFIABLE (cfg);
8598 cfg->cbb->next_bb = tblock;
8600 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8601 if (cfg->verbose_level > 3)
8602 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8603 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8606 g_slist_free (class_inits);
8611 if (skip_dead_blocks) {
8612 int ip_offset = ip - header->code;
8614 if (ip_offset == bb->end)
8618 int op_size = mono_opcode_size (ip, end);
8619 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8621 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8623 if (ip_offset + op_size == bb->end) {
8624 MONO_INST_NEW (cfg, ins, OP_NOP);
8625 MONO_ADD_INS (cfg->cbb, ins);
8626 start_new_bblock = 1;
8634 * Sequence points are points where the debugger can place a breakpoint.
8635 * Currently, we generate these automatically at points where the IL
8638 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8640 * Make methods interruptable at the beginning, and at the targets of
8641 * backward branches.
8642 * Also, do this at the start of every bblock in methods with clauses too,
8643 * to be able to handle instructions with inprecise control flow like
8645 * Backward branches are handled at the end of method-to-ir ().
8647 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8648 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8650 /* Avoid sequence points on empty IL like .volatile */
8651 // FIXME: Enable this
8652 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8653 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8654 if ((sp != stack_start) && !sym_seq_point)
8655 ins->flags |= MONO_INST_NONEMPTY_STACK;
8656 MONO_ADD_INS (cfg->cbb, ins);
8659 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8662 cfg->cbb->real_offset = cfg->real_offset;
8664 if ((cfg->method == method) && cfg->coverage_info) {
8665 guint32 cil_offset = ip - header->code;
8666 cfg->coverage_info->data [cil_offset].cil_code = ip;
8668 /* TODO: Use an increment here */
8669 #if defined(TARGET_X86)
8670 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8671 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8673 MONO_ADD_INS (cfg->cbb, ins);
8675 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8676 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8680 if (cfg->verbose_level > 3)
8681 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8685 if (seq_points && !sym_seq_points && sp != stack_start) {
8687 * The C# compiler uses these nops to notify the JIT that it should
8688 * insert seq points.
8690 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8691 MONO_ADD_INS (cfg->cbb, ins);
8693 if (cfg->keep_cil_nops)
8694 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8696 MONO_INST_NEW (cfg, ins, OP_NOP);
8698 MONO_ADD_INS (cfg->cbb, ins);
8701 if (should_insert_brekpoint (cfg->method)) {
8702 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8704 MONO_INST_NEW (cfg, ins, OP_NOP);
8707 MONO_ADD_INS (cfg->cbb, ins);
8713 CHECK_STACK_OVF (1);
8714 n = (*ip)-CEE_LDARG_0;
8716 EMIT_NEW_ARGLOAD (cfg, ins, n);
8724 CHECK_STACK_OVF (1);
8725 n = (*ip)-CEE_LDLOC_0;
8727 EMIT_NEW_LOCLOAD (cfg, ins, n);
8736 n = (*ip)-CEE_STLOC_0;
8739 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8741 emit_stloc_ir (cfg, sp, header, n);
8748 CHECK_STACK_OVF (1);
8751 EMIT_NEW_ARGLOAD (cfg, ins, n);
8757 CHECK_STACK_OVF (1);
8760 NEW_ARGLOADA (cfg, ins, n);
8761 MONO_ADD_INS (cfg->cbb, ins);
8771 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8773 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8778 CHECK_STACK_OVF (1);
8781 EMIT_NEW_LOCLOAD (cfg, ins, n);
8785 case CEE_LDLOCA_S: {
8786 unsigned char *tmp_ip;
8788 CHECK_STACK_OVF (1);
8789 CHECK_LOCAL (ip [1]);
8791 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8797 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8806 CHECK_LOCAL (ip [1]);
8807 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8809 emit_stloc_ir (cfg, sp, header, ip [1]);
8814 CHECK_STACK_OVF (1);
8815 EMIT_NEW_PCONST (cfg, ins, NULL);
8816 ins->type = STACK_OBJ;
8821 CHECK_STACK_OVF (1);
8822 EMIT_NEW_ICONST (cfg, ins, -1);
8835 CHECK_STACK_OVF (1);
8836 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8842 CHECK_STACK_OVF (1);
8844 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8850 CHECK_STACK_OVF (1);
8851 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8857 CHECK_STACK_OVF (1);
8858 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8859 ins->type = STACK_I8;
8860 ins->dreg = alloc_dreg (cfg, STACK_I8);
8862 ins->inst_l = (gint64)read64 (ip);
8863 MONO_ADD_INS (cfg->cbb, ins);
8869 gboolean use_aotconst = FALSE;
8871 #ifdef TARGET_POWERPC
8872 /* FIXME: Clean this up */
8873 if (cfg->compile_aot)
8874 use_aotconst = TRUE;
8877 /* FIXME: we should really allocate this only late in the compilation process */
8878 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8880 CHECK_STACK_OVF (1);
8886 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8888 dreg = alloc_freg (cfg);
8889 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8890 ins->type = cfg->r4_stack_type;
8892 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8893 ins->type = cfg->r4_stack_type;
8894 ins->dreg = alloc_dreg (cfg, STACK_R8);
8896 MONO_ADD_INS (cfg->cbb, ins);
8906 gboolean use_aotconst = FALSE;
8908 #ifdef TARGET_POWERPC
8909 /* FIXME: Clean this up */
8910 if (cfg->compile_aot)
8911 use_aotconst = TRUE;
8914 /* FIXME: we should really allocate this only late in the compilation process */
8915 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8917 CHECK_STACK_OVF (1);
8923 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8925 dreg = alloc_freg (cfg);
8926 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8927 ins->type = STACK_R8;
8929 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8930 ins->type = STACK_R8;
8931 ins->dreg = alloc_dreg (cfg, STACK_R8);
8933 MONO_ADD_INS (cfg->cbb, ins);
8942 MonoInst *temp, *store;
8944 CHECK_STACK_OVF (1);
8948 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8949 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8951 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8954 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8967 if (sp [0]->type == STACK_R8)
8968 /* we need to pop the value from the x86 FP stack */
8969 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8974 MonoMethodSignature *fsig;
8977 INLINE_FAILURE ("jmp");
8978 GSHAREDVT_FAILURE (*ip);
8981 if (stack_start != sp)
8983 token = read32 (ip + 1);
8984 /* FIXME: check the signature matches */
8985 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8988 if (cfg->gshared && mono_method_check_context_used (cmethod))
8989 GENERIC_SHARING_FAILURE (CEE_JMP);
8991 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8993 fsig = mono_method_signature (cmethod);
8994 n = fsig->param_count + fsig->hasthis;
8995 if (cfg->llvm_only) {
8998 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8999 for (i = 0; i < n; ++i)
9000 EMIT_NEW_ARGLOAD (cfg, args [i], i);
9001 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
9003 * The code in mono-basic-block.c treats the rest of the code as dead, but we
9004 * have to emit a normal return since llvm expects it.
9007 emit_setret (cfg, ins);
9008 MONO_INST_NEW (cfg, ins, OP_BR);
9009 ins->inst_target_bb = end_bblock;
9010 MONO_ADD_INS (cfg->cbb, ins);
9011 link_bblock (cfg, cfg->cbb, end_bblock);
9014 } else if (cfg->backend->have_op_tail_call) {
9015 /* Handle tail calls similarly to calls */
9018 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
9019 call->method = cmethod;
9020 call->tail_call = TRUE;
9021 call->signature = mono_method_signature (cmethod);
9022 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9023 call->inst.inst_p0 = cmethod;
9024 for (i = 0; i < n; ++i)
9025 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
9027 mono_arch_emit_call (cfg, call);
9028 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
9029 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
9031 for (i = 0; i < num_args; ++i)
9032 /* Prevent arguments from being optimized away */
9033 arg_array [i]->flags |= MONO_INST_VOLATILE;
9035 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9036 ins = (MonoInst*)call;
9037 ins->inst_p0 = cmethod;
9038 MONO_ADD_INS (cfg->cbb, ins);
9042 start_new_bblock = 1;
9047 MonoMethodSignature *fsig;
9050 token = read32 (ip + 1);
9054 //GSHAREDVT_FAILURE (*ip);
9059 fsig = mini_get_signature (method, token, generic_context);
9061 if (method->dynamic && fsig->pinvoke) {
9065 * This is a call through a function pointer using a pinvoke
9066 * signature. Have to create a wrapper and call that instead.
9067 * FIXME: This is very slow, need to create a wrapper at JIT time
9068 * instead based on the signature.
9070 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
9071 EMIT_NEW_PCONST (cfg, args [1], fsig);
9073 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
9076 n = fsig->param_count + fsig->hasthis;
9080 //g_assert (!virtual_ || fsig->hasthis);
9084 inline_costs += 10 * num_calls++;
9087 * Making generic calls out of gsharedvt methods.
9088 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9089 * patching gshared method addresses into a gsharedvt method.
9091 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
9093 * We pass the address to the gsharedvt trampoline in the rgctx reg
9095 MonoInst *callee = addr;
9097 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9099 GSHAREDVT_FAILURE (*ip);
9103 GSHAREDVT_FAILURE (*ip);
9105 addr = emit_get_rgctx_sig (cfg, context_used,
9106 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9107 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9111 /* Prevent inlining of methods with indirect calls */
9112 INLINE_FAILURE ("indirect call");
9114 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9115 MonoJumpInfoType info_type;
9119 * Instead of emitting an indirect call, emit a direct call
9120 * with the contents of the aotconst as the patch info.
9122 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9123 info_type = (MonoJumpInfoType)addr->inst_c1;
9124 info_data = addr->inst_p0;
9126 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
9127 info_data = addr->inst_right->inst_left;
9130 if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9131 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9136 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9140 /* End of call, INS should contain the result of the call, if any */
9142 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9144 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9147 CHECK_CFG_EXCEPTION;
9151 constrained_class = NULL;
9155 case CEE_CALLVIRT: {
9156 MonoInst *addr = NULL;
9157 MonoMethodSignature *fsig = NULL;
9159 int virtual_ = *ip == CEE_CALLVIRT;
9160 gboolean pass_imt_from_rgctx = FALSE;
9161 MonoInst *imt_arg = NULL;
9162 MonoInst *keep_this_alive = NULL;
9163 gboolean pass_vtable = FALSE;
9164 gboolean pass_mrgctx = FALSE;
9165 MonoInst *vtable_arg = NULL;
9166 gboolean check_this = FALSE;
9167 gboolean supported_tail_call = FALSE;
9168 gboolean tail_call = FALSE;
9169 gboolean need_seq_point = FALSE;
9170 guint32 call_opcode = *ip;
9171 gboolean emit_widen = TRUE;
9172 gboolean push_res = TRUE;
9173 gboolean skip_ret = FALSE;
9174 gboolean delegate_invoke = FALSE;
9175 gboolean direct_icall = FALSE;
9176 gboolean constrained_partial_call = FALSE;
9177 gboolean needs_calling_assembly = FALSE;
9178 MonoMethod *cil_method;
9181 token = read32 (ip + 1);
9185 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9188 cil_method = cmethod;
9190 if (constrained_class) {
9191 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9192 if (!mini_is_gsharedvt_klass (constrained_class)) {
9193 g_assert (!cmethod->klass->valuetype);
9194 if (!mini_type_is_reference (&constrained_class->byval_arg))
9195 constrained_partial_call = TRUE;
9199 if (method->wrapper_type != MONO_WRAPPER_NONE) {
9200 if (cfg->verbose_level > 2)
9201 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9202 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
9203 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
9205 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
9209 if (cfg->verbose_level > 2)
9210 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9212 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9214 * This is needed since get_method_constrained can't find
9215 * the method in klass representing a type var.
9216 * The type var is guaranteed to be a reference type in this
9219 if (!mini_is_gsharedvt_klass (constrained_class))
9220 g_assert (!cmethod->klass->valuetype);
9222 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
9228 if (!cmethod || mono_loader_get_last_error ()) {
9229 if (mono_loader_get_last_error ()) {
9230 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
9231 mono_error_set_from_loader_error (&cfg->error);
9237 if (!dont_verify && !cfg->skip_visibility) {
9238 MonoMethod *target_method = cil_method;
9239 if (method->is_inflated) {
9240 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9243 if (!mono_method_can_access_method (method_definition, target_method) &&
9244 !mono_method_can_access_method (method, cil_method))
9245 METHOD_ACCESS_FAILURE (method, cil_method);
9248 if (mono_security_core_clr_enabled ())
9249 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
9251 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
9252 /* MS.NET seems to silently convert this to a callvirt */
9257 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
9258 * converts to a callvirt.
9260 * tests/bug-515884.il is an example of this behavior
9262 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
9263 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
9264 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
9268 if (!cmethod->klass->inited)
9269 if (!mono_class_init (cmethod->klass))
9270 TYPE_LOAD_ERROR (cmethod->klass);
9272 fsig = mono_method_signature (cmethod);
9275 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
9276 mini_class_is_system_array (cmethod->klass)) {
9277 array_rank = cmethod->klass->rank;
9278 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
9279 direct_icall = TRUE;
9280 } else if (fsig->pinvoke) {
9281 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9282 fsig = mono_method_signature (wrapper);
9283 } else if (constrained_class) {
9285 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9289 if (cfg->llvm_only && !cfg->method->wrapper_type)
9290 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
9292 /* See code below */
9293 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9294 MonoBasicBlock *tbb;
9296 GET_BBLOCK (cfg, tbb, ip + 5);
9297 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9299 * We want to extend the try block to cover the call, but we can't do it if the
9300 * call is made directly since its followed by an exception check.
9302 direct_icall = FALSE;
9307 * Stack walks are not supported in llvmonly mode, so
9308 * when calling methods which call GetCallingAssembly (), save the
9309 * current assembly in the caller. The call to GetCallingAssembly ()
9310 * will be turned into an icall which will retrieve the value.
9312 if (cfg->llvm_only && cmethod && method_needs_calling_assembly (cmethod)) {
9313 needs_calling_assembly = TRUE;
9315 MonoInst *assembly_ins;
9317 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
9318 ins = mono_emit_jit_icall (cfg, mono_llvmonly_set_calling_assembly, &assembly_ins);
9321 mono_save_token_info (cfg, image, token, cil_method);
9323 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
9324 need_seq_point = TRUE;
9326 /* Don't support calls made using type arguments for now */
9328 if (cfg->gsharedvt) {
9329 if (mini_is_gsharedvt_signature (fsig))
9330 GSHAREDVT_FAILURE (*ip);
9334 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
9335 g_assert_not_reached ();
9337 n = fsig->param_count + fsig->hasthis;
9339 if (!cfg->gshared && cmethod->klass->generic_container)
9343 g_assert (!mono_method_check_context_used (cmethod));
9347 //g_assert (!virtual_ || fsig->hasthis);
9352 * We have the `constrained.' prefix opcode.
9354 if (constrained_class) {
9355 if (mini_is_gsharedvt_klass (constrained_class)) {
9356 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
9357 /* The 'Own method' case below */
9358 } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
9359 /* 'The type parameter is instantiated as a reference type' case below. */
9361 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
9362 CHECK_CFG_EXCEPTION;
9368 if (constrained_partial_call) {
9369 gboolean need_box = TRUE;
9372 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
9373 * called method is not known at compile time either. The called method could end up being
9374 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
9375 * to box the receiver.
9376 * A simple solution would be to box always and make a normal virtual call, but that would
9377 * be bad performance wise.
9379 if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE && cmethod->klass->generic_class) {
9381 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
9386 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)) {
9387 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
9388 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9389 ins->klass = constrained_class;
9390 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9391 CHECK_CFG_EXCEPTION;
9392 } else if (need_box) {
9394 MonoBasicBlock *is_ref_bb, *end_bb;
9395 MonoInst *nonbox_call;
9398 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
9400 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
9401 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
9403 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9405 NEW_BBLOCK (cfg, is_ref_bb);
9406 NEW_BBLOCK (cfg, end_bb);
9408 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);
9409 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
9410 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
9413 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9415 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9418 MONO_START_BB (cfg, is_ref_bb);
9419 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9420 ins->klass = constrained_class;
9421 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9422 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9424 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9426 MONO_START_BB (cfg, end_bb);
9429 nonbox_call->dreg = ins->dreg;
9432 g_assert (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
9433 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9434 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9437 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9439 * The type parameter is instantiated as a valuetype,
9440 * but that type doesn't override the method we're
9441 * calling, so we need to box `this'.
9443 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9444 ins->klass = constrained_class;
9445 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9446 CHECK_CFG_EXCEPTION;
9447 } else if (!constrained_class->valuetype) {
9448 int dreg = alloc_ireg_ref (cfg);
9451 * The type parameter is instantiated as a reference
9452 * type. We have a managed pointer on the stack, so
9453 * we need to dereference it here.
9455 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
9456 ins->type = STACK_OBJ;
9459 if (cmethod->klass->valuetype) {
9462 /* Interface method */
9465 mono_class_setup_vtable (constrained_class);
9466 CHECK_TYPELOAD (constrained_class);
9467 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
9469 TYPE_LOAD_ERROR (constrained_class);
9470 slot = mono_method_get_vtable_slot (cmethod);
9472 TYPE_LOAD_ERROR (cmethod->klass);
9473 cmethod = constrained_class->vtable [ioffset + slot];
9475 if (cmethod->klass == mono_defaults.enum_class) {
9476 /* Enum implements some interfaces, so treat this as the first case */
9477 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9478 ins->klass = constrained_class;
9479 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9480 CHECK_CFG_EXCEPTION;
9485 constrained_class = NULL;
9488 if (check_call_signature (cfg, fsig, sp))
9491 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
9492 delegate_invoke = TRUE;
9494 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
9495 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9496 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9504 * If the callee is a shared method, then its static cctor
9505 * might not get called after the call was patched.
9507 if (cfg->gshared && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
9508 emit_class_init (cfg, cmethod->klass);
9509 CHECK_TYPELOAD (cmethod->klass);
9512 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
9515 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
9517 context_used = mini_method_check_context_used (cfg, cmethod);
9519 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
9520 /* Generic method interface
9521 calls are resolved via a
9522 helper function and don't
9524 if (!cmethod_context || !cmethod_context->method_inst)
9525 pass_imt_from_rgctx = TRUE;
9529 * If a shared method calls another
9530 * shared method then the caller must
9531 * have a generic sharing context
9532 * because the magic trampoline
9533 * requires it. FIXME: We shouldn't
9534 * have to force the vtable/mrgctx
9535 * variable here. Instead there
9536 * should be a flag in the cfg to
9537 * request a generic sharing context.
9540 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9541 mono_get_vtable_var (cfg);
9546 vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9548 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9550 CHECK_TYPELOAD (cmethod->klass);
9551 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9556 g_assert (!vtable_arg);
9558 if (!cfg->compile_aot) {
9560 * emit_get_rgctx_method () calls mono_class_vtable () so check
9561 * for type load errors before.
9563 mono_class_setup_vtable (cmethod->klass);
9564 CHECK_TYPELOAD (cmethod->klass);
9567 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9569 /* !marshalbyref is needed to properly handle generic methods + remoting */
9570 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9571 MONO_METHOD_IS_FINAL (cmethod)) &&
9572 !mono_class_is_marshalbyref (cmethod->klass)) {
9579 if (pass_imt_from_rgctx) {
9580 g_assert (!pass_vtable);
9582 imt_arg = emit_get_rgctx_method (cfg, context_used,
9583 cmethod, MONO_RGCTX_INFO_METHOD);
9587 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9589 /* Calling virtual generic methods */
9590 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9591 !(MONO_METHOD_IS_FINAL (cmethod) &&
9592 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9593 fsig->generic_param_count &&
9594 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
9596 MonoInst *this_temp, *this_arg_temp, *store;
9597 MonoInst *iargs [4];
9599 g_assert (fsig->is_inflated);
9601 /* Prevent inlining of methods that contain indirect calls */
9602 INLINE_FAILURE ("virtual generic call");
9604 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9605 GSHAREDVT_FAILURE (*ip);
9607 if (cfg->backend->have_generalized_imt_thunk && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
9608 g_assert (!imt_arg);
9610 g_assert (cmethod->is_inflated);
9611 imt_arg = emit_get_rgctx_method (cfg, context_used,
9612 cmethod, MONO_RGCTX_INFO_METHOD);
9613 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9615 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9616 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9617 MONO_ADD_INS (cfg->cbb, store);
9619 /* FIXME: This should be a managed pointer */
9620 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9622 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9623 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9624 cmethod, MONO_RGCTX_INFO_METHOD);
9625 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9626 addr = mono_emit_jit_icall (cfg,
9627 mono_helper_compile_generic_method, iargs);
9629 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9631 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9638 * Implement a workaround for the inherent races involved in locking:
9644 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9645 * try block, the Exit () won't be executed, see:
9646 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9647 * To work around this, we extend such try blocks to include the last x bytes
9648 * of the Monitor.Enter () call.
9650 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9651 MonoBasicBlock *tbb;
9653 GET_BBLOCK (cfg, tbb, ip + 5);
9655 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9656 * from Monitor.Enter like ArgumentNullException.
9658 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9659 /* Mark this bblock as needing to be extended */
9660 tbb->extend_try_block = TRUE;
9664 /* Conversion to a JIT intrinsic */
9665 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9666 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9667 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9674 if ((cfg->opt & MONO_OPT_INLINE) &&
9675 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9676 mono_method_check_inlining (cfg, cmethod)) {
9678 gboolean always = FALSE;
9680 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9681 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9682 /* Prevent inlining of methods that call wrappers */
9683 INLINE_FAILURE ("wrapper call");
9684 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9688 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9690 cfg->real_offset += 5;
9692 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9693 /* *sp is already set by inline_method */
9698 inline_costs += costs;
9704 /* Tail recursion elimination */
9705 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9706 gboolean has_vtargs = FALSE;
9709 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9710 INLINE_FAILURE ("tail call");
9712 /* keep it simple */
9713 for (i = fsig->param_count - 1; i >= 0; i--) {
9714 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9719 for (i = 0; i < n; ++i)
9720 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9721 MONO_INST_NEW (cfg, ins, OP_BR);
9722 MONO_ADD_INS (cfg->cbb, ins);
9723 tblock = start_bblock->out_bb [0];
9724 link_bblock (cfg, cfg->cbb, tblock);
9725 ins->inst_target_bb = tblock;
9726 start_new_bblock = 1;
9728 /* skip the CEE_RET, too */
9729 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9736 inline_costs += 10 * num_calls++;
9739 * Making generic calls out of gsharedvt methods.
9740 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9741 * patching gshared method addresses into a gsharedvt method.
9743 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9744 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9745 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9746 MonoRgctxInfoType info_type;
9749 //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9750 //GSHAREDVT_FAILURE (*ip);
9751 // disable for possible remoting calls
9752 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9753 GSHAREDVT_FAILURE (*ip);
9754 if (fsig->generic_param_count) {
9755 /* virtual generic call */
9756 g_assert (!imt_arg);
9757 /* Same as the virtual generic case above */
9758 imt_arg = emit_get_rgctx_method (cfg, context_used,
9759 cmethod, MONO_RGCTX_INFO_METHOD);
9760 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9762 } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9763 /* This can happen when we call a fully instantiated iface method */
9764 imt_arg = emit_get_rgctx_method (cfg, context_used,
9765 cmethod, MONO_RGCTX_INFO_METHOD);
9770 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9771 keep_this_alive = sp [0];
9773 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9774 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9776 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9777 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9779 if (cfg->llvm_only) {
9780 // FIXME: Avoid initializing vtable_arg
9781 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9783 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9788 /* Generic sharing */
9791 * Use this if the callee is gsharedvt sharable too, since
9792 * at runtime we might find an instantiation so the call cannot
9793 * be patched (the 'no_patch' code path in mini-trampolines.c).
9795 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9796 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9797 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9798 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9799 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9800 INLINE_FAILURE ("gshared");
9802 g_assert (cfg->gshared && cmethod);
9806 * We are compiling a call to a
9807 * generic method from shared code,
9808 * which means that we have to look up
9809 * the method in the rgctx and do an
9813 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9815 if (cfg->llvm_only) {
9816 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9817 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9819 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9820 // FIXME: Avoid initializing imt_arg/vtable_arg
9821 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9823 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9824 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9829 /* Direct calls to icalls */
9831 MonoMethod *wrapper;
9834 /* Inline the wrapper */
9835 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9837 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9838 g_assert (costs > 0);
9839 cfg->real_offset += 5;
9841 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9842 /* *sp is already set by inline_method */
9847 inline_costs += costs;
9856 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9857 MonoInst *val = sp [fsig->param_count];
9859 if (val->type == STACK_OBJ) {
9860 MonoInst *iargs [2];
9865 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9868 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9869 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9870 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9871 emit_write_barrier (cfg, addr, val);
9872 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9873 GSHAREDVT_FAILURE (*ip);
9874 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9875 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9877 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9878 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9879 if (!cmethod->klass->element_class->valuetype && !readonly)
9880 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9881 CHECK_TYPELOAD (cmethod->klass);
9884 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9887 g_assert_not_reached ();
9894 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9898 /* Tail prefix / tail call optimization */
9900 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9901 /* FIXME: runtime generic context pointer for jumps? */
9902 /* FIXME: handle this for generic sharing eventually */
9903 if ((ins_flag & MONO_INST_TAILCALL) &&
9904 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9905 supported_tail_call = TRUE;
9907 if (supported_tail_call) {
9910 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9911 INLINE_FAILURE ("tail call");
9913 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9915 if (cfg->backend->have_op_tail_call) {
9916 /* Handle tail calls similarly to normal calls */
9919 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9921 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9922 call->tail_call = TRUE;
9923 call->method = cmethod;
9924 call->signature = mono_method_signature (cmethod);
9927 * We implement tail calls by storing the actual arguments into the
9928 * argument variables, then emitting a CEE_JMP.
9930 for (i = 0; i < n; ++i) {
9931 /* Prevent argument from being register allocated */
9932 arg_array [i]->flags |= MONO_INST_VOLATILE;
9933 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9935 ins = (MonoInst*)call;
9936 ins->inst_p0 = cmethod;
9937 ins->inst_p1 = arg_array [0];
9938 MONO_ADD_INS (cfg->cbb, ins);
9939 link_bblock (cfg, cfg->cbb, end_bblock);
9940 start_new_bblock = 1;
9942 // FIXME: Eliminate unreachable epilogs
9945 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9946 * only reachable from this call.
9948 GET_BBLOCK (cfg, tblock, ip + 5);
9949 if (tblock == cfg->cbb || tblock->in_count == 0)
9958 * Synchronized wrappers.
9959 * Its hard to determine where to replace a method with its synchronized
9960 * wrapper without causing an infinite recursion. The current solution is
9961 * to add the synchronized wrapper in the trampolines, and to
9962 * change the called method to a dummy wrapper, and resolve that wrapper
9963 * to the real method in mono_jit_compile_method ().
9965 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9966 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9967 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9968 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9972 * Virtual calls in llvm-only mode.
9974 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9975 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9980 INLINE_FAILURE ("call");
9981 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9982 imt_arg, vtable_arg);
9984 if (tail_call && !cfg->llvm_only) {
9985 link_bblock (cfg, cfg->cbb, end_bblock);
9986 start_new_bblock = 1;
9988 // FIXME: Eliminate unreachable epilogs
9991 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9992 * only reachable from this call.
9994 GET_BBLOCK (cfg, tblock, ip + 5);
9995 if (tblock == cfg->cbb || tblock->in_count == 0)
10002 /* End of call, INS should contain the result of the call, if any */
10004 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
10007 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
10012 if (keep_this_alive) {
10013 MonoInst *dummy_use;
10015 /* See mono_emit_method_call_full () */
10016 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
10019 if (needs_calling_assembly) {
10021 * Clear the calling assembly.
10022 * This is not EH safe, but this is not a problem in practice, since
10023 * the null value is only used for error checking.
10025 MonoInst *assembly_ins;
10027 EMIT_NEW_PCONST (cfg, assembly_ins, NULL);
10028 ins = mono_emit_jit_icall (cfg, mono_llvmonly_set_calling_assembly, &assembly_ins);
10031 CHECK_CFG_EXCEPTION;
10035 g_assert (*ip == CEE_RET);
10039 constrained_class = NULL;
10040 if (need_seq_point)
10041 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10045 if (cfg->method != method) {
10046 /* return from inlined method */
10048 * If in_count == 0, that means the ret is unreachable due to
10049 * being preceeded by a throw. In that case, inline_method () will
10050 * handle setting the return value
10051 * (test case: test_0_inline_throw ()).
10053 if (return_var && cfg->cbb->in_count) {
10054 MonoType *ret_type = mono_method_signature (method)->ret;
10060 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10063 //g_assert (returnvar != -1);
10064 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
10065 cfg->ret_var_set = TRUE;
10068 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10070 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
10071 emit_pop_lmf (cfg);
10074 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
10076 if (seq_points && !sym_seq_points) {
10078 * Place a seq point here too even through the IL stack is not
10079 * empty, so a step over on
10082 * will work correctly.
10084 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
10085 MONO_ADD_INS (cfg->cbb, ins);
10088 g_assert (!return_var);
10092 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10095 emit_setret (cfg, *sp);
10098 if (sp != stack_start)
10100 MONO_INST_NEW (cfg, ins, OP_BR);
10102 ins->inst_target_bb = end_bblock;
10103 MONO_ADD_INS (cfg->cbb, ins);
10104 link_bblock (cfg, cfg->cbb, end_bblock);
10105 start_new_bblock = 1;
10109 MONO_INST_NEW (cfg, ins, OP_BR);
10111 target = ip + 1 + (signed char)(*ip);
10113 GET_BBLOCK (cfg, tblock, target);
10114 link_bblock (cfg, cfg->cbb, tblock);
10115 ins->inst_target_bb = tblock;
10116 if (sp != stack_start) {
10117 handle_stack_args (cfg, stack_start, sp - stack_start);
10119 CHECK_UNVERIFIABLE (cfg);
10121 MONO_ADD_INS (cfg->cbb, ins);
10122 start_new_bblock = 1;
10123 inline_costs += BRANCH_COST;
10137 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
10139 target = ip + 1 + *(signed char*)ip;
10142 ADD_BINCOND (NULL);
10145 inline_costs += BRANCH_COST;
10149 MONO_INST_NEW (cfg, ins, OP_BR);
10152 target = ip + 4 + (gint32)read32(ip);
10154 GET_BBLOCK (cfg, tblock, target);
10155 link_bblock (cfg, cfg->cbb, tblock);
10156 ins->inst_target_bb = tblock;
10157 if (sp != stack_start) {
10158 handle_stack_args (cfg, stack_start, sp - stack_start);
10160 CHECK_UNVERIFIABLE (cfg);
10163 MONO_ADD_INS (cfg->cbb, ins);
10165 start_new_bblock = 1;
10166 inline_costs += BRANCH_COST;
10168 case CEE_BRFALSE_S:
10173 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
10174 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
10175 guint32 opsize = is_short ? 1 : 4;
10177 CHECK_OPSIZE (opsize);
10179 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
10182 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
10187 GET_BBLOCK (cfg, tblock, target);
10188 link_bblock (cfg, cfg->cbb, tblock);
10189 GET_BBLOCK (cfg, tblock, ip);
10190 link_bblock (cfg, cfg->cbb, tblock);
10192 if (sp != stack_start) {
10193 handle_stack_args (cfg, stack_start, sp - stack_start);
10194 CHECK_UNVERIFIABLE (cfg);
10197 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
10198 cmp->sreg1 = sp [0]->dreg;
10199 type_from_op (cfg, cmp, sp [0], NULL);
10202 #if SIZEOF_REGISTER == 4
10203 if (cmp->opcode == OP_LCOMPARE_IMM) {
10204 /* Convert it to OP_LCOMPARE */
10205 MONO_INST_NEW (cfg, ins, OP_I8CONST);
10206 ins->type = STACK_I8;
10207 ins->dreg = alloc_dreg (cfg, STACK_I8);
10209 MONO_ADD_INS (cfg->cbb, ins);
10210 cmp->opcode = OP_LCOMPARE;
10211 cmp->sreg2 = ins->dreg;
10214 MONO_ADD_INS (cfg->cbb, cmp);
10216 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
10217 type_from_op (cfg, ins, sp [0], NULL);
10218 MONO_ADD_INS (cfg->cbb, ins);
10219 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
10220 GET_BBLOCK (cfg, tblock, target);
10221 ins->inst_true_bb = tblock;
10222 GET_BBLOCK (cfg, tblock, ip);
10223 ins->inst_false_bb = tblock;
10224 start_new_bblock = 2;
10227 inline_costs += BRANCH_COST;
10242 MONO_INST_NEW (cfg, ins, *ip);
10244 target = ip + 4 + (gint32)read32(ip);
10247 ADD_BINCOND (NULL);
10250 inline_costs += BRANCH_COST;
10254 MonoBasicBlock **targets;
10255 MonoBasicBlock *default_bblock;
10256 MonoJumpInfoBBTable *table;
10257 int offset_reg = alloc_preg (cfg);
10258 int target_reg = alloc_preg (cfg);
10259 int table_reg = alloc_preg (cfg);
10260 int sum_reg = alloc_preg (cfg);
10261 gboolean use_op_switch;
10265 n = read32 (ip + 1);
10268 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
10272 CHECK_OPSIZE (n * sizeof (guint32));
10273 target = ip + n * sizeof (guint32);
10275 GET_BBLOCK (cfg, default_bblock, target);
10276 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
10278 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
10279 for (i = 0; i < n; ++i) {
10280 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
10281 targets [i] = tblock;
10282 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
10286 if (sp != stack_start) {
10288 * Link the current bb with the targets as well, so handle_stack_args
10289 * will set their in_stack correctly.
10291 link_bblock (cfg, cfg->cbb, default_bblock);
10292 for (i = 0; i < n; ++i)
10293 link_bblock (cfg, cfg->cbb, targets [i]);
10295 handle_stack_args (cfg, stack_start, sp - stack_start);
10297 CHECK_UNVERIFIABLE (cfg);
10299 /* Undo the links */
10300 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
10301 for (i = 0; i < n; ++i)
10302 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
10305 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
10306 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
10308 for (i = 0; i < n; ++i)
10309 link_bblock (cfg, cfg->cbb, targets [i]);
10311 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
10312 table->table = targets;
10313 table->table_size = n;
10315 use_op_switch = FALSE;
10317 /* ARM implements SWITCH statements differently */
10318 /* FIXME: Make it use the generic implementation */
10319 if (!cfg->compile_aot)
10320 use_op_switch = TRUE;
10323 if (COMPILE_LLVM (cfg))
10324 use_op_switch = TRUE;
10326 cfg->cbb->has_jump_table = 1;
10328 if (use_op_switch) {
10329 MONO_INST_NEW (cfg, ins, OP_SWITCH);
10330 ins->sreg1 = src1->dreg;
10331 ins->inst_p0 = table;
10332 ins->inst_many_bb = targets;
10333 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
10334 MONO_ADD_INS (cfg->cbb, ins);
10336 if (sizeof (gpointer) == 8)
10337 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
10339 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
10341 #if SIZEOF_REGISTER == 8
10342 /* The upper word might not be zero, and we add it to a 64 bit address later */
10343 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
10346 if (cfg->compile_aot) {
10347 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
10349 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
10350 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
10351 ins->inst_p0 = table;
10352 ins->dreg = table_reg;
10353 MONO_ADD_INS (cfg->cbb, ins);
10356 /* FIXME: Use load_memindex */
10357 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
10358 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
10359 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
10361 start_new_bblock = 1;
10362 inline_costs += (BRANCH_COST * 2);
10375 case CEE_LDIND_REF:
10382 dreg = alloc_freg (cfg);
10385 dreg = alloc_lreg (cfg);
10387 case CEE_LDIND_REF:
10388 dreg = alloc_ireg_ref (cfg);
10391 dreg = alloc_preg (cfg);
10394 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
10395 ins->type = ldind_type [*ip - CEE_LDIND_I1];
10396 if (*ip == CEE_LDIND_R4)
10397 ins->type = cfg->r4_stack_type;
10398 ins->flags |= ins_flag;
10399 MONO_ADD_INS (cfg->cbb, ins);
10401 if (ins_flag & MONO_INST_VOLATILE) {
10402 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10403 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10408 case CEE_STIND_REF:
10419 if (ins_flag & MONO_INST_VOLATILE) {
10420 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10421 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10424 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
10425 ins->flags |= ins_flag;
10428 MONO_ADD_INS (cfg->cbb, ins);
10430 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [1]->opcode == OP_PCONST) && (sp [1]->inst_p0 == 0)))
10431 emit_write_barrier (cfg, sp [0], sp [1]);
10440 MONO_INST_NEW (cfg, ins, (*ip));
10442 ins->sreg1 = sp [0]->dreg;
10443 ins->sreg2 = sp [1]->dreg;
10444 type_from_op (cfg, ins, sp [0], sp [1]);
10446 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10448 /* Use the immediate opcodes if possible */
10449 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
10450 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10451 if (imm_opcode != -1) {
10452 ins->opcode = imm_opcode;
10453 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
10456 NULLIFY_INS (sp [1]);
10460 MONO_ADD_INS ((cfg)->cbb, (ins));
10462 *sp++ = mono_decompose_opcode (cfg, ins);
10479 MONO_INST_NEW (cfg, ins, (*ip));
10481 ins->sreg1 = sp [0]->dreg;
10482 ins->sreg2 = sp [1]->dreg;
10483 type_from_op (cfg, ins, sp [0], sp [1]);
10485 add_widen_op (cfg, ins, &sp [0], &sp [1]);
10486 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10488 /* FIXME: Pass opcode to is_inst_imm */
10490 /* Use the immediate opcodes if possible */
10491 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)) {
10492 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10493 if (imm_opcode != -1) {
10494 ins->opcode = imm_opcode;
10495 if (sp [1]->opcode == OP_I8CONST) {
10496 #if SIZEOF_REGISTER == 8
10497 ins->inst_imm = sp [1]->inst_l;
10499 ins->inst_ls_word = sp [1]->inst_ls_word;
10500 ins->inst_ms_word = sp [1]->inst_ms_word;
10504 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10507 /* Might be followed by an instruction added by add_widen_op */
10508 if (sp [1]->next == NULL)
10509 NULLIFY_INS (sp [1]);
10512 MONO_ADD_INS ((cfg)->cbb, (ins));
10514 *sp++ = mono_decompose_opcode (cfg, ins);
10527 case CEE_CONV_OVF_I8:
10528 case CEE_CONV_OVF_U8:
10529 case CEE_CONV_R_UN:
10532 /* Special case this earlier so we have long constants in the IR */
10533 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10534 int data = sp [-1]->inst_c0;
10535 sp [-1]->opcode = OP_I8CONST;
10536 sp [-1]->type = STACK_I8;
10537 #if SIZEOF_REGISTER == 8
10538 if ((*ip) == CEE_CONV_U8)
10539 sp [-1]->inst_c0 = (guint32)data;
10541 sp [-1]->inst_c0 = data;
10543 sp [-1]->inst_ls_word = data;
10544 if ((*ip) == CEE_CONV_U8)
10545 sp [-1]->inst_ms_word = 0;
10547 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10549 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10556 case CEE_CONV_OVF_I4:
10557 case CEE_CONV_OVF_I1:
10558 case CEE_CONV_OVF_I2:
10559 case CEE_CONV_OVF_I:
10560 case CEE_CONV_OVF_U:
10563 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10564 ADD_UNOP (CEE_CONV_OVF_I8);
10571 case CEE_CONV_OVF_U1:
10572 case CEE_CONV_OVF_U2:
10573 case CEE_CONV_OVF_U4:
10576 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10577 ADD_UNOP (CEE_CONV_OVF_U8);
10584 case CEE_CONV_OVF_I1_UN:
10585 case CEE_CONV_OVF_I2_UN:
10586 case CEE_CONV_OVF_I4_UN:
10587 case CEE_CONV_OVF_I8_UN:
10588 case CEE_CONV_OVF_U1_UN:
10589 case CEE_CONV_OVF_U2_UN:
10590 case CEE_CONV_OVF_U4_UN:
10591 case CEE_CONV_OVF_U8_UN:
10592 case CEE_CONV_OVF_I_UN:
10593 case CEE_CONV_OVF_U_UN:
10600 CHECK_CFG_EXCEPTION;
10604 case CEE_ADD_OVF_UN:
10606 case CEE_MUL_OVF_UN:
10608 case CEE_SUB_OVF_UN:
10614 GSHAREDVT_FAILURE (*ip);
10617 token = read32 (ip + 1);
10618 klass = mini_get_class (method, token, generic_context);
10619 CHECK_TYPELOAD (klass);
10621 if (generic_class_is_reference_type (cfg, klass)) {
10622 MonoInst *store, *load;
10623 int dreg = alloc_ireg_ref (cfg);
10625 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10626 load->flags |= ins_flag;
10627 MONO_ADD_INS (cfg->cbb, load);
10629 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10630 store->flags |= ins_flag;
10631 MONO_ADD_INS (cfg->cbb, store);
10633 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10634 emit_write_barrier (cfg, sp [0], sp [1]);
10636 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10642 int loc_index = -1;
10648 token = read32 (ip + 1);
10649 klass = mini_get_class (method, token, generic_context);
10650 CHECK_TYPELOAD (klass);
10652 /* Optimize the common ldobj+stloc combination */
10655 loc_index = ip [6];
10662 loc_index = ip [5] - CEE_STLOC_0;
10669 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10670 CHECK_LOCAL (loc_index);
10672 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10673 ins->dreg = cfg->locals [loc_index]->dreg;
10674 ins->flags |= ins_flag;
10677 if (ins_flag & MONO_INST_VOLATILE) {
10678 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10679 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10685 /* Optimize the ldobj+stobj combination */
10686 /* The reference case ends up being a load+store anyway */
10687 /* Skip this if the operation is volatile. */
10688 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)) {
10693 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10700 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10701 ins->flags |= ins_flag;
10704 if (ins_flag & MONO_INST_VOLATILE) {
10705 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10706 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10715 CHECK_STACK_OVF (1);
10717 n = read32 (ip + 1);
10719 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10720 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10721 ins->type = STACK_OBJ;
10724 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10725 MonoInst *iargs [1];
10726 char *str = (char *)mono_method_get_wrapper_data (method, n);
10728 if (cfg->compile_aot)
10729 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10731 EMIT_NEW_PCONST (cfg, iargs [0], str);
10732 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10734 if (cfg->opt & MONO_OPT_SHARED) {
10735 MonoInst *iargs [3];
10737 if (cfg->compile_aot) {
10738 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10740 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10741 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10742 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10743 *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
10744 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10746 if (cfg->cbb->out_of_line) {
10747 MonoInst *iargs [2];
10749 if (image == mono_defaults.corlib) {
10751 * Avoid relocations in AOT and save some space by using a
10752 * version of helper_ldstr specialized to mscorlib.
10754 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10755 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10757 /* Avoid creating the string object */
10758 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10759 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10760 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10764 if (cfg->compile_aot) {
10765 NEW_LDSTRCONST (cfg, ins, image, n);
10767 MONO_ADD_INS (cfg->cbb, ins);
10770 NEW_PCONST (cfg, ins, NULL);
10771 ins->type = STACK_OBJ;
10772 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10774 OUT_OF_MEMORY_FAILURE;
10777 MONO_ADD_INS (cfg->cbb, ins);
10786 MonoInst *iargs [2];
10787 MonoMethodSignature *fsig;
10790 MonoInst *vtable_arg = NULL;
10793 token = read32 (ip + 1);
10794 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10797 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10800 mono_save_token_info (cfg, image, token, cmethod);
10802 if (!mono_class_init (cmethod->klass))
10803 TYPE_LOAD_ERROR (cmethod->klass);
10805 context_used = mini_method_check_context_used (cfg, cmethod);
10807 if (mono_security_core_clr_enabled ())
10808 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10810 if (cfg->gshared && cmethod && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
10811 emit_class_init (cfg, cmethod->klass);
10812 CHECK_TYPELOAD (cmethod->klass);
10816 if (cfg->gsharedvt) {
10817 if (mini_is_gsharedvt_variable_signature (sig))
10818 GSHAREDVT_FAILURE (*ip);
10822 n = fsig->param_count;
10826 * Generate smaller code for the common newobj <exception> instruction in
10827 * argument checking code.
10829 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10830 is_exception_class (cmethod->klass) && n <= 2 &&
10831 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10832 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10833 MonoInst *iargs [3];
10837 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10840 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10843 iargs [1] = sp [0];
10844 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10847 iargs [1] = sp [0];
10848 iargs [2] = sp [1];
10849 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10852 g_assert_not_reached ();
10860 /* move the args to allow room for 'this' in the first position */
10866 /* check_call_signature () requires sp[0] to be set */
10867 this_ins.type = STACK_OBJ;
10868 sp [0] = &this_ins;
10869 if (check_call_signature (cfg, fsig, sp))
10874 if (mini_class_is_system_array (cmethod->klass)) {
10875 *sp = emit_get_rgctx_method (cfg, context_used,
10876 cmethod, MONO_RGCTX_INFO_METHOD);
10878 /* Avoid varargs in the common case */
10879 if (fsig->param_count == 1)
10880 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10881 else if (fsig->param_count == 2)
10882 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10883 else if (fsig->param_count == 3)
10884 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10885 else if (fsig->param_count == 4)
10886 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10888 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10889 } else if (cmethod->string_ctor) {
10890 g_assert (!context_used);
10891 g_assert (!vtable_arg);
10892 /* we simply pass a null pointer */
10893 EMIT_NEW_PCONST (cfg, *sp, NULL);
10894 /* now call the string ctor */
10895 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10897 if (cmethod->klass->valuetype) {
10898 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10899 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10900 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10905 * The code generated by mini_emit_virtual_call () expects
10906 * iargs [0] to be a boxed instance, but luckily the vcall
10907 * will be transformed into a normal call there.
10909 } else if (context_used) {
10910 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10913 MonoVTable *vtable = NULL;
10915 if (!cfg->compile_aot)
10916 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10917 CHECK_TYPELOAD (cmethod->klass);
10920 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10921 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10922 * As a workaround, we call class cctors before allocating objects.
10924 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10925 emit_class_init (cfg, cmethod->klass);
10926 if (cfg->verbose_level > 2)
10927 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10928 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10931 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10934 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10937 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10939 /* Now call the actual ctor */
10940 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10941 CHECK_CFG_EXCEPTION;
10944 if (alloc == NULL) {
10946 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10947 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10955 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10956 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10959 case CEE_CASTCLASS:
10963 token = read32 (ip + 1);
10964 klass = mini_get_class (method, token, generic_context);
10965 CHECK_TYPELOAD (klass);
10966 if (sp [0]->type != STACK_OBJ)
10969 ins = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10970 CHECK_CFG_EXCEPTION;
10979 token = read32 (ip + 1);
10980 klass = mini_get_class (method, token, generic_context);
10981 CHECK_TYPELOAD (klass);
10982 if (sp [0]->type != STACK_OBJ)
10985 context_used = mini_class_check_context_used (cfg, klass);
10987 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10988 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10989 MonoInst *args [3];
10996 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10999 idx = get_castclass_cache_idx (cfg);
11000 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
11002 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
11005 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
11006 MonoMethod *mono_isinst;
11007 MonoInst *iargs [1];
11010 mono_isinst = mono_marshal_get_isinst (klass);
11011 iargs [0] = sp [0];
11013 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst),
11014 iargs, ip, cfg->real_offset, TRUE);
11015 CHECK_CFG_EXCEPTION;
11016 g_assert (costs > 0);
11019 cfg->real_offset += 5;
11023 inline_costs += costs;
11026 ins = handle_isinst (cfg, klass, *sp, context_used);
11027 CHECK_CFG_EXCEPTION;
11033 case CEE_UNBOX_ANY: {
11034 MonoInst *res, *addr;
11039 token = read32 (ip + 1);
11040 klass = mini_get_class (method, token, generic_context);
11041 CHECK_TYPELOAD (klass);
11043 mono_save_token_info (cfg, image, token, klass);
11045 context_used = mini_class_check_context_used (cfg, klass);
11047 if (mini_is_gsharedvt_klass (klass)) {
11048 res = handle_unbox_gsharedvt (cfg, klass, *sp);
11050 } else if (generic_class_is_reference_type (cfg, klass)) {
11051 res = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
11052 CHECK_CFG_EXCEPTION;
11053 } else if (mono_class_is_nullable (klass)) {
11054 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
11056 addr = handle_unbox (cfg, klass, sp, context_used);
11058 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11069 MonoClass *enum_class;
11070 MonoMethod *has_flag;
11076 token = read32 (ip + 1);
11077 klass = mini_get_class (method, token, generic_context);
11078 CHECK_TYPELOAD (klass);
11080 mono_save_token_info (cfg, image, token, klass);
11082 context_used = mini_class_check_context_used (cfg, klass);
11084 if (generic_class_is_reference_type (cfg, klass)) {
11090 if (klass == mono_defaults.void_class)
11092 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
11094 /* frequent check in generic code: box (struct), brtrue */
11099 * <push int/long ptr>
11102 * constrained. MyFlags
11103 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
11105 * If we find this sequence and the operand types on box and constrained
11106 * are equal, we can emit a specialized instruction sequence instead of
11107 * the very slow HasFlag () call.
11109 if ((cfg->opt & MONO_OPT_INTRINS) &&
11110 /* Cheap checks first. */
11111 ip + 5 + 6 + 5 < end &&
11112 ip [5] == CEE_PREFIX1 &&
11113 ip [6] == CEE_CONSTRAINED_ &&
11114 ip [11] == CEE_CALLVIRT &&
11115 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
11116 mono_class_is_enum (klass) &&
11117 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
11118 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
11119 has_flag->klass == mono_defaults.enum_class &&
11120 !strcmp (has_flag->name, "HasFlag") &&
11121 has_flag->signature->hasthis &&
11122 has_flag->signature->param_count == 1) {
11123 CHECK_TYPELOAD (enum_class);
11125 if (enum_class == klass) {
11126 MonoInst *enum_this, *enum_flag;
11131 enum_this = sp [0];
11132 enum_flag = sp [1];
11134 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
11139 // FIXME: LLVM can't handle the inconsistent bb linking
11140 if (!mono_class_is_nullable (klass) &&
11141 !mini_is_gsharedvt_klass (klass) &&
11142 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11143 (ip [5] == CEE_BRTRUE ||
11144 ip [5] == CEE_BRTRUE_S ||
11145 ip [5] == CEE_BRFALSE ||
11146 ip [5] == CEE_BRFALSE_S)) {
11147 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
11149 MonoBasicBlock *true_bb, *false_bb;
11153 if (cfg->verbose_level > 3) {
11154 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
11155 printf ("<box+brtrue opt>\n");
11160 case CEE_BRFALSE_S:
11163 target = ip + 1 + (signed char)(*ip);
11170 target = ip + 4 + (gint)(read32 (ip));
11174 g_assert_not_reached ();
11178 * We need to link both bblocks, since it is needed for handling stack
11179 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
11180 * Branching to only one of them would lead to inconsistencies, so
11181 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
11183 GET_BBLOCK (cfg, true_bb, target);
11184 GET_BBLOCK (cfg, false_bb, ip);
11186 mono_link_bblock (cfg, cfg->cbb, true_bb);
11187 mono_link_bblock (cfg, cfg->cbb, false_bb);
11189 if (sp != stack_start) {
11190 handle_stack_args (cfg, stack_start, sp - stack_start);
11192 CHECK_UNVERIFIABLE (cfg);
11195 if (COMPILE_LLVM (cfg)) {
11196 dreg = alloc_ireg (cfg);
11197 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
11198 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
11200 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
11202 /* The JIT can't eliminate the iconst+compare */
11203 MONO_INST_NEW (cfg, ins, OP_BR);
11204 ins->inst_target_bb = is_true ? true_bb : false_bb;
11205 MONO_ADD_INS (cfg->cbb, ins);
11208 start_new_bblock = 1;
11212 *sp++ = handle_box (cfg, val, klass, context_used);
11214 CHECK_CFG_EXCEPTION;
11223 token = read32 (ip + 1);
11224 klass = mini_get_class (method, token, generic_context);
11225 CHECK_TYPELOAD (klass);
11227 mono_save_token_info (cfg, image, token, klass);
11229 context_used = mini_class_check_context_used (cfg, klass);
11231 if (mono_class_is_nullable (klass)) {
11234 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
11235 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
11239 ins = handle_unbox (cfg, klass, sp, context_used);
11252 MonoClassField *field;
11253 #ifndef DISABLE_REMOTING
11257 gboolean is_instance;
11259 gpointer addr = NULL;
11260 gboolean is_special_static;
11262 MonoInst *store_val = NULL;
11263 MonoInst *thread_ins;
11266 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
11268 if (op == CEE_STFLD) {
11271 store_val = sp [1];
11276 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
11278 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
11281 if (op == CEE_STSFLD) {
11284 store_val = sp [0];
11289 token = read32 (ip + 1);
11290 if (method->wrapper_type != MONO_WRAPPER_NONE) {
11291 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
11292 klass = field->parent;
11295 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
11298 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
11299 FIELD_ACCESS_FAILURE (method, field);
11300 mono_class_init (klass);
11302 /* if the class is Critical then transparent code cannot access it's fields */
11303 if (!is_instance && mono_security_core_clr_enabled ())
11304 ensure_method_is_allowed_to_access_field (cfg, method, field);
11306 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
11307 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
11308 if (mono_security_core_clr_enabled ())
11309 ensure_method_is_allowed_to_access_field (cfg, method, field);
11312 ftype = mono_field_get_type (field);
11315 * LDFLD etc. is usable on static fields as well, so convert those cases to
11318 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
11330 g_assert_not_reached ();
11332 is_instance = FALSE;
11335 context_used = mini_class_check_context_used (cfg, klass);
11337 /* INSTANCE CASE */
11339 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
11340 if (op == CEE_STFLD) {
11341 if (target_type_is_incompatible (cfg, field->type, sp [1]))
11343 #ifndef DISABLE_REMOTING
11344 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
11345 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
11346 MonoInst *iargs [5];
11348 GSHAREDVT_FAILURE (op);
11350 iargs [0] = sp [0];
11351 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11352 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11353 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
11355 iargs [4] = sp [1];
11357 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11358 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
11359 iargs, ip, cfg->real_offset, TRUE);
11360 CHECK_CFG_EXCEPTION;
11361 g_assert (costs > 0);
11363 cfg->real_offset += 5;
11365 inline_costs += costs;
11367 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
11374 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11376 if (mini_is_gsharedvt_klass (klass)) {
11377 MonoInst *offset_ins;
11379 context_used = mini_class_check_context_used (cfg, klass);
11381 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11382 /* The value is offset by 1 */
11383 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11384 dreg = alloc_ireg_mp (cfg);
11385 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11386 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
11387 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
11389 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
11391 if (sp [0]->opcode != OP_LDADDR)
11392 store->flags |= MONO_INST_FAULT;
11394 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
11395 /* insert call to write barrier */
11399 dreg = alloc_ireg_mp (cfg);
11400 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11401 emit_write_barrier (cfg, ptr, sp [1]);
11404 store->flags |= ins_flag;
11411 #ifndef DISABLE_REMOTING
11412 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
11413 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
11414 MonoInst *iargs [4];
11416 GSHAREDVT_FAILURE (op);
11418 iargs [0] = sp [0];
11419 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11420 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11421 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
11422 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11423 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
11424 iargs, ip, cfg->real_offset, TRUE);
11425 CHECK_CFG_EXCEPTION;
11426 g_assert (costs > 0);
11428 cfg->real_offset += 5;
11432 inline_costs += costs;
11434 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
11440 if (sp [0]->type == STACK_VTYPE) {
11443 /* Have to compute the address of the variable */
11445 var = get_vreg_to_inst (cfg, sp [0]->dreg);
11447 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
11449 g_assert (var->klass == klass);
11451 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
11455 if (op == CEE_LDFLDA) {
11456 if (sp [0]->type == STACK_OBJ) {
11457 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
11458 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
11461 dreg = alloc_ireg_mp (cfg);
11463 if (mini_is_gsharedvt_klass (klass)) {
11464 MonoInst *offset_ins;
11466 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11467 /* The value is offset by 1 */
11468 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11469 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11471 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11473 ins->klass = mono_class_from_mono_type (field->type);
11474 ins->type = STACK_MP;
11479 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11481 if (mini_is_gsharedvt_klass (klass)) {
11482 MonoInst *offset_ins;
11484 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11485 /* The value is offset by 1 */
11486 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11487 dreg = alloc_ireg_mp (cfg);
11488 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11489 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
11491 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
11493 load->flags |= ins_flag;
11494 if (sp [0]->opcode != OP_LDADDR)
11495 load->flags |= MONO_INST_FAULT;
11507 context_used = mini_class_check_context_used (cfg, klass);
11509 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL)
11512 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11513 * to be called here.
11515 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11516 mono_class_vtable (cfg->domain, klass);
11517 CHECK_TYPELOAD (klass);
11519 mono_domain_lock (cfg->domain);
11520 if (cfg->domain->special_static_fields)
11521 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11522 mono_domain_unlock (cfg->domain);
11524 is_special_static = mono_class_field_is_special_static (field);
11526 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11527 thread_ins = mono_get_thread_intrinsic (cfg);
11531 /* Generate IR to compute the field address */
11532 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11534 * Fast access to TLS data
11535 * Inline version of get_thread_static_data () in
11539 int idx, static_data_reg, array_reg, dreg;
11541 GSHAREDVT_FAILURE (op);
11543 MONO_ADD_INS (cfg->cbb, thread_ins);
11544 static_data_reg = alloc_ireg (cfg);
11545 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11547 if (cfg->compile_aot) {
11548 int offset_reg, offset2_reg, idx_reg;
11550 /* For TLS variables, this will return the TLS offset */
11551 EMIT_NEW_SFLDACONST (cfg, ins, field);
11552 offset_reg = ins->dreg;
11553 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11554 idx_reg = alloc_ireg (cfg);
11555 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11556 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11557 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11558 array_reg = alloc_ireg (cfg);
11559 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11560 offset2_reg = alloc_ireg (cfg);
11561 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11562 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11563 dreg = alloc_ireg (cfg);
11564 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11566 offset = (gsize)addr & 0x7fffffff;
11567 idx = offset & 0x3f;
11569 array_reg = alloc_ireg (cfg);
11570 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11571 dreg = alloc_ireg (cfg);
11572 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11574 } else if ((cfg->opt & MONO_OPT_SHARED) ||
11575 (cfg->compile_aot && is_special_static) ||
11576 (context_used && is_special_static)) {
11577 MonoInst *iargs [2];
11579 g_assert (field->parent);
11580 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11581 if (context_used) {
11582 iargs [1] = emit_get_rgctx_field (cfg, context_used,
11583 field, MONO_RGCTX_INFO_CLASS_FIELD);
11585 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11587 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11588 } else if (context_used) {
11589 MonoInst *static_data;
11592 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11593 method->klass->name_space, method->klass->name, method->name,
11594 depth, field->offset);
11597 if (mono_class_needs_cctor_run (klass, method))
11598 emit_class_init (cfg, klass);
11601 * The pointer we're computing here is
11603 * super_info.static_data + field->offset
11605 static_data = emit_get_rgctx_klass (cfg, context_used,
11606 klass, MONO_RGCTX_INFO_STATIC_DATA);
11608 if (mini_is_gsharedvt_klass (klass)) {
11609 MonoInst *offset_ins;
11611 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11612 /* The value is offset by 1 */
11613 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11614 dreg = alloc_ireg_mp (cfg);
11615 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11616 } else if (field->offset == 0) {
11619 int addr_reg = mono_alloc_preg (cfg);
11620 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11622 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11623 MonoInst *iargs [2];
11625 g_assert (field->parent);
11626 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11627 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11628 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11630 MonoVTable *vtable = NULL;
11632 if (!cfg->compile_aot)
11633 vtable = mono_class_vtable (cfg->domain, klass);
11634 CHECK_TYPELOAD (klass);
11637 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11638 if (!(g_slist_find (class_inits, klass))) {
11639 emit_class_init (cfg, klass);
11640 if (cfg->verbose_level > 2)
11641 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11642 class_inits = g_slist_prepend (class_inits, klass);
11645 if (cfg->run_cctors) {
11647 /* This makes so that inline cannot trigger */
11648 /* .cctors: too many apps depend on them */
11649 /* running with a specific order... */
11651 if (! vtable->initialized)
11652 INLINE_FAILURE ("class init");
11653 ex = mono_runtime_class_init_full (vtable, FALSE);
11655 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11656 mono_error_set_exception_instance (&cfg->error, ex);
11657 g_assert_not_reached ();
11658 goto exception_exit;
11662 if (cfg->compile_aot)
11663 EMIT_NEW_SFLDACONST (cfg, ins, field);
11666 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11668 EMIT_NEW_PCONST (cfg, ins, addr);
11671 MonoInst *iargs [1];
11672 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11673 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11677 /* Generate IR to do the actual load/store operation */
11679 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11680 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11681 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11684 if (op == CEE_LDSFLDA) {
11685 ins->klass = mono_class_from_mono_type (ftype);
11686 ins->type = STACK_PTR;
11688 } else if (op == CEE_STSFLD) {
11691 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11692 store->flags |= ins_flag;
11694 gboolean is_const = FALSE;
11695 MonoVTable *vtable = NULL;
11696 gpointer addr = NULL;
11698 if (!context_used) {
11699 vtable = mono_class_vtable (cfg->domain, klass);
11700 CHECK_TYPELOAD (klass);
11702 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11703 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11704 int ro_type = ftype->type;
11706 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11707 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11708 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11711 GSHAREDVT_FAILURE (op);
11713 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11716 case MONO_TYPE_BOOLEAN:
11718 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11722 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11725 case MONO_TYPE_CHAR:
11727 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11731 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11736 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11740 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11745 case MONO_TYPE_PTR:
11746 case MONO_TYPE_FNPTR:
11747 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11748 type_to_eval_stack_type ((cfg), field->type, *sp);
11751 case MONO_TYPE_STRING:
11752 case MONO_TYPE_OBJECT:
11753 case MONO_TYPE_CLASS:
11754 case MONO_TYPE_SZARRAY:
11755 case MONO_TYPE_ARRAY:
11756 if (!mono_gc_is_moving ()) {
11757 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11758 type_to_eval_stack_type ((cfg), field->type, *sp);
11766 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11771 case MONO_TYPE_VALUETYPE:
11781 CHECK_STACK_OVF (1);
11783 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11784 load->flags |= ins_flag;
11790 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11791 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11792 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11803 token = read32 (ip + 1);
11804 klass = mini_get_class (method, token, generic_context);
11805 CHECK_TYPELOAD (klass);
11806 if (ins_flag & MONO_INST_VOLATILE) {
11807 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11808 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11810 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11811 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11812 ins->flags |= ins_flag;
11813 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11814 generic_class_is_reference_type (cfg, klass)) {
11815 /* insert call to write barrier */
11816 emit_write_barrier (cfg, sp [0], sp [1]);
11828 const char *data_ptr;
11830 guint32 field_token;
11836 token = read32 (ip + 1);
11838 klass = mini_get_class (method, token, generic_context);
11839 CHECK_TYPELOAD (klass);
11841 context_used = mini_class_check_context_used (cfg, klass);
11843 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11844 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11845 ins->sreg1 = sp [0]->dreg;
11846 ins->type = STACK_I4;
11847 ins->dreg = alloc_ireg (cfg);
11848 MONO_ADD_INS (cfg->cbb, ins);
11849 *sp = mono_decompose_opcode (cfg, ins);
11852 if (context_used) {
11853 MonoInst *args [3];
11854 MonoClass *array_class = mono_array_class_get (klass, 1);
11855 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11857 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11860 args [0] = emit_get_rgctx_klass (cfg, context_used,
11861 array_class, MONO_RGCTX_INFO_VTABLE);
11866 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11868 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11870 if (cfg->opt & MONO_OPT_SHARED) {
11871 /* Decompose now to avoid problems with references to the domainvar */
11872 MonoInst *iargs [3];
11874 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11875 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11876 iargs [2] = sp [0];
11878 ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
11880 /* Decompose later since it is needed by abcrem */
11881 MonoClass *array_type = mono_array_class_get (klass, 1);
11882 mono_class_vtable (cfg->domain, array_type);
11883 CHECK_TYPELOAD (array_type);
11885 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11886 ins->dreg = alloc_ireg_ref (cfg);
11887 ins->sreg1 = sp [0]->dreg;
11888 ins->inst_newa_class = klass;
11889 ins->type = STACK_OBJ;
11890 ins->klass = array_type;
11891 MONO_ADD_INS (cfg->cbb, ins);
11892 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11893 cfg->cbb->has_array_access = TRUE;
11895 /* Needed so mono_emit_load_get_addr () gets called */
11896 mono_get_got_var (cfg);
11906 * we inline/optimize the initialization sequence if possible.
11907 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11908 * for small sizes open code the memcpy
11909 * ensure the rva field is big enough
11911 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))) {
11912 MonoMethod *memcpy_method = get_memcpy_method ();
11913 MonoInst *iargs [3];
11914 int add_reg = alloc_ireg_mp (cfg);
11916 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11917 if (cfg->compile_aot) {
11918 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11920 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11922 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11923 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11932 if (sp [0]->type != STACK_OBJ)
11935 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11936 ins->dreg = alloc_preg (cfg);
11937 ins->sreg1 = sp [0]->dreg;
11938 ins->type = STACK_I4;
11939 /* This flag will be inherited by the decomposition */
11940 ins->flags |= MONO_INST_FAULT;
11941 MONO_ADD_INS (cfg->cbb, ins);
11942 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11943 cfg->cbb->has_array_access = TRUE;
11951 if (sp [0]->type != STACK_OBJ)
11954 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11956 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11957 CHECK_TYPELOAD (klass);
11958 /* we need to make sure that this array is exactly the type it needs
11959 * to be for correctness. the wrappers are lax with their usage
11960 * so we need to ignore them here
11962 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11963 MonoClass *array_class = mono_array_class_get (klass, 1);
11964 mini_emit_check_array_type (cfg, sp [0], array_class);
11965 CHECK_TYPELOAD (array_class);
11969 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11974 case CEE_LDELEM_I1:
11975 case CEE_LDELEM_U1:
11976 case CEE_LDELEM_I2:
11977 case CEE_LDELEM_U2:
11978 case CEE_LDELEM_I4:
11979 case CEE_LDELEM_U4:
11980 case CEE_LDELEM_I8:
11982 case CEE_LDELEM_R4:
11983 case CEE_LDELEM_R8:
11984 case CEE_LDELEM_REF: {
11990 if (*ip == CEE_LDELEM) {
11992 token = read32 (ip + 1);
11993 klass = mini_get_class (method, token, generic_context);
11994 CHECK_TYPELOAD (klass);
11995 mono_class_init (klass);
11998 klass = array_access_to_klass (*ip);
12000 if (sp [0]->type != STACK_OBJ)
12003 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12005 if (mini_is_gsharedvt_variable_klass (klass)) {
12006 // FIXME-VT: OP_ICONST optimization
12007 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12008 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12009 ins->opcode = OP_LOADV_MEMBASE;
12010 } else if (sp [1]->opcode == OP_ICONST) {
12011 int array_reg = sp [0]->dreg;
12012 int index_reg = sp [1]->dreg;
12013 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
12015 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
12016 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
12018 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
12019 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
12021 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12022 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12025 if (*ip == CEE_LDELEM)
12032 case CEE_STELEM_I1:
12033 case CEE_STELEM_I2:
12034 case CEE_STELEM_I4:
12035 case CEE_STELEM_I8:
12036 case CEE_STELEM_R4:
12037 case CEE_STELEM_R8:
12038 case CEE_STELEM_REF:
12043 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12045 if (*ip == CEE_STELEM) {
12047 token = read32 (ip + 1);
12048 klass = mini_get_class (method, token, generic_context);
12049 CHECK_TYPELOAD (klass);
12050 mono_class_init (klass);
12053 klass = array_access_to_klass (*ip);
12055 if (sp [0]->type != STACK_OBJ)
12058 emit_array_store (cfg, klass, sp, TRUE);
12060 if (*ip == CEE_STELEM)
12067 case CEE_CKFINITE: {
12071 if (cfg->llvm_only) {
12072 MonoInst *iargs [1];
12074 iargs [0] = sp [0];
12075 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
12077 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
12078 ins->sreg1 = sp [0]->dreg;
12079 ins->dreg = alloc_freg (cfg);
12080 ins->type = STACK_R8;
12081 MONO_ADD_INS (cfg->cbb, ins);
12083 *sp++ = mono_decompose_opcode (cfg, ins);
12089 case CEE_REFANYVAL: {
12090 MonoInst *src_var, *src;
12092 int klass_reg = alloc_preg (cfg);
12093 int dreg = alloc_preg (cfg);
12095 GSHAREDVT_FAILURE (*ip);
12098 MONO_INST_NEW (cfg, ins, *ip);
12101 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12102 CHECK_TYPELOAD (klass);
12104 context_used = mini_class_check_context_used (cfg, klass);
12107 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12109 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12110 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12111 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
12113 if (context_used) {
12114 MonoInst *klass_ins;
12116 klass_ins = emit_get_rgctx_klass (cfg, context_used,
12117 klass, MONO_RGCTX_INFO_KLASS);
12120 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
12121 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
12123 mini_emit_class_check (cfg, klass_reg, klass);
12125 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
12126 ins->type = STACK_MP;
12127 ins->klass = klass;
12132 case CEE_MKREFANY: {
12133 MonoInst *loc, *addr;
12135 GSHAREDVT_FAILURE (*ip);
12138 MONO_INST_NEW (cfg, ins, *ip);
12141 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12142 CHECK_TYPELOAD (klass);
12144 context_used = mini_class_check_context_used (cfg, klass);
12146 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
12147 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
12149 if (context_used) {
12150 MonoInst *const_ins;
12151 int type_reg = alloc_preg (cfg);
12153 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
12154 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
12155 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12156 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12157 } else if (cfg->compile_aot) {
12158 int const_reg = alloc_preg (cfg);
12159 int type_reg = alloc_preg (cfg);
12161 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
12162 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
12163 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12164 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12166 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
12167 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
12169 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
12171 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
12172 ins->type = STACK_VTYPE;
12173 ins->klass = mono_defaults.typed_reference_class;
12178 case CEE_LDTOKEN: {
12180 MonoClass *handle_class;
12182 CHECK_STACK_OVF (1);
12185 n = read32 (ip + 1);
12187 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
12188 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
12189 handle = mono_method_get_wrapper_data (method, n);
12190 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
12191 if (handle_class == mono_defaults.typehandle_class)
12192 handle = &((MonoClass*)handle)->byval_arg;
12195 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
12200 mono_class_init (handle_class);
12201 if (cfg->gshared) {
12202 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
12203 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
12204 /* This case handles ldtoken
12205 of an open type, like for
12208 } else if (handle_class == mono_defaults.typehandle_class) {
12209 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
12210 } else if (handle_class == mono_defaults.fieldhandle_class)
12211 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
12212 else if (handle_class == mono_defaults.methodhandle_class)
12213 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
12215 g_assert_not_reached ();
12218 if ((cfg->opt & MONO_OPT_SHARED) &&
12219 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
12220 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
12221 MonoInst *addr, *vtvar, *iargs [3];
12222 int method_context_used;
12224 method_context_used = mini_method_check_context_used (cfg, method);
12226 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12228 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
12229 EMIT_NEW_ICONST (cfg, iargs [1], n);
12230 if (method_context_used) {
12231 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
12232 method, MONO_RGCTX_INFO_METHOD);
12233 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
12235 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
12236 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
12238 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12240 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12242 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12244 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
12245 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
12246 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
12247 (cmethod->klass == mono_defaults.systemtype_class) &&
12248 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
12249 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
12251 mono_class_init (tclass);
12252 if (context_used) {
12253 ins = emit_get_rgctx_klass (cfg, context_used,
12254 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
12255 } else if (cfg->compile_aot) {
12256 if (method->wrapper_type) {
12257 mono_error_init (&error); //got to do it since there are multiple conditionals below
12258 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
12259 /* Special case for static synchronized wrappers */
12260 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
12262 mono_error_cleanup (&error); /* FIXME don't swallow the error */
12263 /* FIXME: n is not a normal token */
12265 EMIT_NEW_PCONST (cfg, ins, NULL);
12268 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
12272 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &error);
12273 mono_error_raise_exception (&error); /* FIXME don't raise here */
12275 EMIT_NEW_PCONST (cfg, ins, rt);
12277 ins->type = STACK_OBJ;
12278 ins->klass = cmethod->klass;
12281 MonoInst *addr, *vtvar;
12283 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12285 if (context_used) {
12286 if (handle_class == mono_defaults.typehandle_class) {
12287 ins = emit_get_rgctx_klass (cfg, context_used,
12288 mono_class_from_mono_type ((MonoType *)handle),
12289 MONO_RGCTX_INFO_TYPE);
12290 } else if (handle_class == mono_defaults.methodhandle_class) {
12291 ins = emit_get_rgctx_method (cfg, context_used,
12292 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
12293 } else if (handle_class == mono_defaults.fieldhandle_class) {
12294 ins = emit_get_rgctx_field (cfg, context_used,
12295 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
12297 g_assert_not_reached ();
12299 } else if (cfg->compile_aot) {
12300 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
12302 EMIT_NEW_PCONST (cfg, ins, handle);
12304 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12305 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12306 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12316 MONO_INST_NEW (cfg, ins, OP_THROW);
12318 ins->sreg1 = sp [0]->dreg;
12320 cfg->cbb->out_of_line = TRUE;
12321 MONO_ADD_INS (cfg->cbb, ins);
12322 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12323 MONO_ADD_INS (cfg->cbb, ins);
12326 link_bblock (cfg, cfg->cbb, end_bblock);
12327 start_new_bblock = 1;
12328 /* This can complicate code generation for llvm since the return value might not be defined */
12329 if (COMPILE_LLVM (cfg))
12330 INLINE_FAILURE ("throw");
12332 case CEE_ENDFINALLY:
12333 /* mono_save_seq_point_info () depends on this */
12334 if (sp != stack_start)
12335 emit_seq_point (cfg, method, ip, FALSE, FALSE);
12336 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
12337 MONO_ADD_INS (cfg->cbb, ins);
12339 start_new_bblock = 1;
12342 * Control will leave the method so empty the stack, otherwise
12343 * the next basic block will start with a nonempty stack.
12345 while (sp != stack_start) {
12350 case CEE_LEAVE_S: {
12353 if (*ip == CEE_LEAVE) {
12355 target = ip + 5 + (gint32)read32(ip + 1);
12358 target = ip + 2 + (signed char)(ip [1]);
12361 /* empty the stack */
12362 while (sp != stack_start) {
12367 * If this leave statement is in a catch block, check for a
12368 * pending exception, and rethrow it if necessary.
12369 * We avoid doing this in runtime invoke wrappers, since those are called
12370 * by native code which excepts the wrapper to catch all exceptions.
12372 for (i = 0; i < header->num_clauses; ++i) {
12373 MonoExceptionClause *clause = &header->clauses [i];
12376 * Use <= in the final comparison to handle clauses with multiple
12377 * leave statements, like in bug #78024.
12378 * The ordering of the exception clauses guarantees that we find the
12379 * innermost clause.
12381 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) {
12383 MonoBasicBlock *dont_throw;
12388 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
12391 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
12393 NEW_BBLOCK (cfg, dont_throw);
12396 * Currently, we always rethrow the abort exception, despite the
12397 * fact that this is not correct. See thread6.cs for an example.
12398 * But propagating the abort exception is more important than
12399 * getting the sematics right.
12401 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
12402 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
12403 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
12405 MONO_START_BB (cfg, dont_throw);
12410 cfg->cbb->try_end = (intptr_t)(ip - header->code);
12413 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
12415 MonoExceptionClause *clause;
12417 for (tmp = handlers; tmp; tmp = tmp->next) {
12418 clause = (MonoExceptionClause *)tmp->data;
12419 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
12421 link_bblock (cfg, cfg->cbb, tblock);
12422 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
12423 ins->inst_target_bb = tblock;
12424 ins->inst_eh_block = clause;
12425 MONO_ADD_INS (cfg->cbb, ins);
12426 cfg->cbb->has_call_handler = 1;
12427 if (COMPILE_LLVM (cfg)) {
12428 MonoBasicBlock *target_bb;
12431 * Link the finally bblock with the target, since it will
12432 * conceptually branch there.
12434 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
12435 GET_BBLOCK (cfg, target_bb, target);
12436 link_bblock (cfg, tblock, target_bb);
12439 g_list_free (handlers);
12442 MONO_INST_NEW (cfg, ins, OP_BR);
12443 MONO_ADD_INS (cfg->cbb, ins);
12444 GET_BBLOCK (cfg, tblock, target);
12445 link_bblock (cfg, cfg->cbb, tblock);
12446 ins->inst_target_bb = tblock;
12448 start_new_bblock = 1;
12450 if (*ip == CEE_LEAVE)
12459 * Mono specific opcodes
12461 case MONO_CUSTOM_PREFIX: {
12463 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
12467 case CEE_MONO_ICALL: {
12469 MonoJitICallInfo *info;
12471 token = read32 (ip + 2);
12472 func = mono_method_get_wrapper_data (method, token);
12473 info = mono_find_jit_icall_by_addr (func);
12475 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
12478 CHECK_STACK (info->sig->param_count);
12479 sp -= info->sig->param_count;
12481 ins = mono_emit_jit_icall (cfg, info->func, sp);
12482 if (!MONO_TYPE_IS_VOID (info->sig->ret))
12486 inline_costs += 10 * num_calls++;
12490 case CEE_MONO_LDPTR_CARD_TABLE:
12491 case CEE_MONO_LDPTR_NURSERY_START:
12492 case CEE_MONO_LDPTR_NURSERY_BITS:
12493 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
12494 CHECK_STACK_OVF (1);
12497 case CEE_MONO_LDPTR_CARD_TABLE:
12498 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
12500 case CEE_MONO_LDPTR_NURSERY_START:
12501 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
12503 case CEE_MONO_LDPTR_NURSERY_BITS:
12504 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
12506 case CEE_MONO_LDPTR_INT_REQ_FLAG:
12507 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12513 inline_costs += 10 * num_calls++;
12516 case CEE_MONO_LDPTR: {
12519 CHECK_STACK_OVF (1);
12521 token = read32 (ip + 2);
12523 ptr = mono_method_get_wrapper_data (method, token);
12524 EMIT_NEW_PCONST (cfg, ins, ptr);
12527 inline_costs += 10 * num_calls++;
12528 /* Can't embed random pointers into AOT code */
12532 case CEE_MONO_JIT_ICALL_ADDR: {
12533 MonoJitICallInfo *callinfo;
12536 CHECK_STACK_OVF (1);
12538 token = read32 (ip + 2);
12540 ptr = mono_method_get_wrapper_data (method, token);
12541 callinfo = mono_find_jit_icall_by_addr (ptr);
12542 g_assert (callinfo);
12543 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12546 inline_costs += 10 * num_calls++;
12549 case CEE_MONO_ICALL_ADDR: {
12550 MonoMethod *cmethod;
12553 CHECK_STACK_OVF (1);
12555 token = read32 (ip + 2);
12557 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
12559 if (cfg->compile_aot) {
12560 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12562 ptr = mono_lookup_internal_call (cmethod);
12564 EMIT_NEW_PCONST (cfg, ins, ptr);
12570 case CEE_MONO_VTADDR: {
12571 MonoInst *src_var, *src;
12577 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12578 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12583 case CEE_MONO_NEWOBJ: {
12584 MonoInst *iargs [2];
12586 CHECK_STACK_OVF (1);
12588 token = read32 (ip + 2);
12589 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12590 mono_class_init (klass);
12591 NEW_DOMAINCONST (cfg, iargs [0]);
12592 MONO_ADD_INS (cfg->cbb, iargs [0]);
12593 NEW_CLASSCONST (cfg, iargs [1], klass);
12594 MONO_ADD_INS (cfg->cbb, iargs [1]);
12595 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
12597 inline_costs += 10 * num_calls++;
12600 case CEE_MONO_OBJADDR:
12603 MONO_INST_NEW (cfg, ins, OP_MOVE);
12604 ins->dreg = alloc_ireg_mp (cfg);
12605 ins->sreg1 = sp [0]->dreg;
12606 ins->type = STACK_MP;
12607 MONO_ADD_INS (cfg->cbb, ins);
12611 case CEE_MONO_LDNATIVEOBJ:
12613 * Similar to LDOBJ, but instead load the unmanaged
12614 * representation of the vtype to the stack.
12619 token = read32 (ip + 2);
12620 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12621 g_assert (klass->valuetype);
12622 mono_class_init (klass);
12625 MonoInst *src, *dest, *temp;
12628 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12629 temp->backend.is_pinvoke = 1;
12630 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12631 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12633 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12634 dest->type = STACK_VTYPE;
12635 dest->klass = klass;
12641 case CEE_MONO_RETOBJ: {
12643 * Same as RET, but return the native representation of a vtype
12646 g_assert (cfg->ret);
12647 g_assert (mono_method_signature (method)->pinvoke);
12652 token = read32 (ip + 2);
12653 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12655 if (!cfg->vret_addr) {
12656 g_assert (cfg->ret_var_is_local);
12658 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12660 EMIT_NEW_RETLOADA (cfg, ins);
12662 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12664 if (sp != stack_start)
12667 MONO_INST_NEW (cfg, ins, OP_BR);
12668 ins->inst_target_bb = end_bblock;
12669 MONO_ADD_INS (cfg->cbb, ins);
12670 link_bblock (cfg, cfg->cbb, end_bblock);
12671 start_new_bblock = 1;
12675 case CEE_MONO_CISINST:
12676 case CEE_MONO_CCASTCLASS: {
12681 token = read32 (ip + 2);
12682 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12683 if (ip [1] == CEE_MONO_CISINST)
12684 ins = handle_cisinst (cfg, klass, sp [0]);
12686 ins = handle_ccastclass (cfg, klass, sp [0]);
12691 case CEE_MONO_SAVE_LMF:
12692 case CEE_MONO_RESTORE_LMF:
12695 case CEE_MONO_CLASSCONST:
12696 CHECK_STACK_OVF (1);
12698 token = read32 (ip + 2);
12699 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12702 inline_costs += 10 * num_calls++;
12704 case CEE_MONO_NOT_TAKEN:
12705 cfg->cbb->out_of_line = TRUE;
12708 case CEE_MONO_TLS: {
12711 CHECK_STACK_OVF (1);
12713 key = (MonoTlsKey)read32 (ip + 2);
12714 g_assert (key < TLS_KEY_NUM);
12716 ins = mono_create_tls_get (cfg, key);
12718 if (cfg->compile_aot) {
12720 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12721 ins->dreg = alloc_preg (cfg);
12722 ins->type = STACK_PTR;
12724 g_assert_not_reached ();
12727 ins->type = STACK_PTR;
12728 MONO_ADD_INS (cfg->cbb, ins);
12733 case CEE_MONO_DYN_CALL: {
12734 MonoCallInst *call;
12736 /* It would be easier to call a trampoline, but that would put an
12737 * extra frame on the stack, confusing exception handling. So
12738 * implement it inline using an opcode for now.
12741 if (!cfg->dyn_call_var) {
12742 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12743 /* prevent it from being register allocated */
12744 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12747 /* Has to use a call inst since it local regalloc expects it */
12748 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12749 ins = (MonoInst*)call;
12751 ins->sreg1 = sp [0]->dreg;
12752 ins->sreg2 = sp [1]->dreg;
12753 MONO_ADD_INS (cfg->cbb, ins);
12755 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12758 inline_costs += 10 * num_calls++;
12762 case CEE_MONO_MEMORY_BARRIER: {
12764 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12768 case CEE_MONO_JIT_ATTACH: {
12769 MonoInst *args [16], *domain_ins;
12770 MonoInst *ad_ins, *jit_tls_ins;
12771 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12773 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12775 EMIT_NEW_PCONST (cfg, ins, NULL);
12776 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12778 ad_ins = mono_get_domain_intrinsic (cfg);
12779 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12781 if (cfg->backend->have_tls_get && ad_ins && jit_tls_ins) {
12782 NEW_BBLOCK (cfg, next_bb);
12783 NEW_BBLOCK (cfg, call_bb);
12785 if (cfg->compile_aot) {
12786 /* AOT code is only used in the root domain */
12787 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12789 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12791 MONO_ADD_INS (cfg->cbb, ad_ins);
12792 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12793 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12795 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12796 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12797 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12799 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12800 MONO_START_BB (cfg, call_bb);
12803 if (cfg->compile_aot) {
12804 /* AOT code is only used in the root domain */
12805 EMIT_NEW_PCONST (cfg, args [0], NULL);
12807 EMIT_NEW_PCONST (cfg, args [0], cfg->domain);
12809 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12810 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12813 MONO_START_BB (cfg, next_bb);
12817 case CEE_MONO_JIT_DETACH: {
12818 MonoInst *args [16];
12820 /* Restore the original domain */
12821 dreg = alloc_ireg (cfg);
12822 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12823 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12827 case CEE_MONO_CALLI_EXTRA_ARG: {
12829 MonoMethodSignature *fsig;
12833 * This is the same as CEE_CALLI, but passes an additional argument
12834 * to the called method in llvmonly mode.
12835 * This is only used by delegate invoke wrappers to call the
12836 * actual delegate method.
12838 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12841 token = read32 (ip + 2);
12849 fsig = mini_get_signature (method, token, generic_context);
12851 if (cfg->llvm_only)
12852 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12854 n = fsig->param_count + fsig->hasthis + 1;
12861 if (cfg->llvm_only) {
12863 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12864 * cconv. This is set by mono_init_delegate ().
12866 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12867 MonoInst *callee = addr;
12868 MonoInst *call, *localloc_ins;
12869 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12870 int low_bit_reg = alloc_preg (cfg);
12872 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12873 NEW_BBLOCK (cfg, end_bb);
12875 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12876 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12877 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12879 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12880 addr = emit_get_rgctx_sig (cfg, context_used,
12881 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12883 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12885 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12886 ins->dreg = alloc_preg (cfg);
12887 ins->inst_imm = 2 * SIZEOF_VOID_P;
12888 MONO_ADD_INS (cfg->cbb, ins);
12889 localloc_ins = ins;
12890 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12891 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12892 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12894 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12895 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12897 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12898 MONO_START_BB (cfg, is_gsharedvt_bb);
12899 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12900 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12901 ins->dreg = call->dreg;
12903 MONO_START_BB (cfg, end_bb);
12905 /* Caller uses a normal calling conv */
12907 MonoInst *callee = addr;
12908 MonoInst *call, *localloc_ins;
12909 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12910 int low_bit_reg = alloc_preg (cfg);
12912 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12913 NEW_BBLOCK (cfg, end_bb);
12915 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12916 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12917 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12919 /* Normal case: callee uses a normal cconv, no conversion is needed */
12920 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12921 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12922 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12923 MONO_START_BB (cfg, is_gsharedvt_bb);
12924 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12925 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12926 MONO_ADD_INS (cfg->cbb, addr);
12928 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12930 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12931 ins->dreg = alloc_preg (cfg);
12932 ins->inst_imm = 2 * SIZEOF_VOID_P;
12933 MONO_ADD_INS (cfg->cbb, ins);
12934 localloc_ins = ins;
12935 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12936 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12937 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12939 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12940 ins->dreg = call->dreg;
12941 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12943 MONO_START_BB (cfg, end_bb);
12946 /* Same as CEE_CALLI */
12947 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12949 * We pass the address to the gsharedvt trampoline in the rgctx reg
12951 MonoInst *callee = addr;
12953 addr = emit_get_rgctx_sig (cfg, context_used,
12954 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12955 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12957 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12961 if (!MONO_TYPE_IS_VOID (fsig->ret))
12962 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12964 CHECK_CFG_EXCEPTION;
12968 constrained_class = NULL;
12972 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12978 case CEE_PREFIX1: {
12981 case CEE_ARGLIST: {
12982 /* somewhat similar to LDTOKEN */
12983 MonoInst *addr, *vtvar;
12984 CHECK_STACK_OVF (1);
12985 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12987 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12988 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12990 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12991 ins->type = STACK_VTYPE;
12992 ins->klass = mono_defaults.argumenthandle_class;
13002 MonoInst *cmp, *arg1, *arg2;
13010 * The following transforms:
13011 * CEE_CEQ into OP_CEQ
13012 * CEE_CGT into OP_CGT
13013 * CEE_CGT_UN into OP_CGT_UN
13014 * CEE_CLT into OP_CLT
13015 * CEE_CLT_UN into OP_CLT_UN
13017 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
13019 MONO_INST_NEW (cfg, ins, cmp->opcode);
13020 cmp->sreg1 = arg1->dreg;
13021 cmp->sreg2 = arg2->dreg;
13022 type_from_op (cfg, cmp, arg1, arg2);
13024 add_widen_op (cfg, cmp, &arg1, &arg2);
13025 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
13026 cmp->opcode = OP_LCOMPARE;
13027 else if (arg1->type == STACK_R4)
13028 cmp->opcode = OP_RCOMPARE;
13029 else if (arg1->type == STACK_R8)
13030 cmp->opcode = OP_FCOMPARE;
13032 cmp->opcode = OP_ICOMPARE;
13033 MONO_ADD_INS (cfg->cbb, cmp);
13034 ins->type = STACK_I4;
13035 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
13036 type_from_op (cfg, ins, arg1, arg2);
13038 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
13040 * The backends expect the fceq opcodes to do the
13043 ins->sreg1 = cmp->sreg1;
13044 ins->sreg2 = cmp->sreg2;
13047 MONO_ADD_INS (cfg->cbb, ins);
13053 MonoInst *argconst;
13054 MonoMethod *cil_method;
13056 CHECK_STACK_OVF (1);
13058 n = read32 (ip + 2);
13059 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13062 mono_class_init (cmethod->klass);
13064 mono_save_token_info (cfg, image, n, cmethod);
13066 context_used = mini_method_check_context_used (cfg, cmethod);
13068 cil_method = cmethod;
13069 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
13070 METHOD_ACCESS_FAILURE (method, cil_method);
13072 if (mono_security_core_clr_enabled ())
13073 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13076 * Optimize the common case of ldftn+delegate creation
13078 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
13079 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13080 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13081 MonoInst *target_ins, *handle_ins;
13082 MonoMethod *invoke;
13083 int invoke_context_used;
13085 invoke = mono_get_delegate_invoke (ctor_method->klass);
13086 if (!invoke || !mono_method_signature (invoke))
13089 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13091 target_ins = sp [-1];
13093 if (mono_security_core_clr_enabled ())
13094 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13096 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
13097 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
13098 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
13099 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
13100 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
13104 /* FIXME: SGEN support */
13105 if (invoke_context_used == 0 || cfg->llvm_only) {
13107 if (cfg->verbose_level > 3)
13108 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));
13109 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
13112 CHECK_CFG_EXCEPTION;
13122 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
13123 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
13127 inline_costs += 10 * num_calls++;
13130 case CEE_LDVIRTFTN: {
13131 MonoInst *args [2];
13135 n = read32 (ip + 2);
13136 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13139 mono_class_init (cmethod->klass);
13141 context_used = mini_method_check_context_used (cfg, cmethod);
13143 if (mono_security_core_clr_enabled ())
13144 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13147 * Optimize the common case of ldvirtftn+delegate creation
13149 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)) {
13150 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13151 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13152 MonoInst *target_ins, *handle_ins;
13153 MonoMethod *invoke;
13154 int invoke_context_used;
13155 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
13157 invoke = mono_get_delegate_invoke (ctor_method->klass);
13158 if (!invoke || !mono_method_signature (invoke))
13161 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13163 target_ins = sp [-1];
13165 if (mono_security_core_clr_enabled ())
13166 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13168 /* FIXME: SGEN support */
13169 if (invoke_context_used == 0 || cfg->llvm_only) {
13171 if (cfg->verbose_level > 3)
13172 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));
13173 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
13176 CHECK_CFG_EXCEPTION;
13189 args [1] = emit_get_rgctx_method (cfg, context_used,
13190 cmethod, MONO_RGCTX_INFO_METHOD);
13193 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
13195 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
13198 inline_costs += 10 * num_calls++;
13202 CHECK_STACK_OVF (1);
13204 n = read16 (ip + 2);
13206 EMIT_NEW_ARGLOAD (cfg, ins, n);
13211 CHECK_STACK_OVF (1);
13213 n = read16 (ip + 2);
13215 NEW_ARGLOADA (cfg, ins, n);
13216 MONO_ADD_INS (cfg->cbb, ins);
13224 n = read16 (ip + 2);
13226 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
13228 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
13232 CHECK_STACK_OVF (1);
13234 n = read16 (ip + 2);
13236 EMIT_NEW_LOCLOAD (cfg, ins, n);
13241 unsigned char *tmp_ip;
13242 CHECK_STACK_OVF (1);
13244 n = read16 (ip + 2);
13247 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
13253 EMIT_NEW_LOCLOADA (cfg, ins, n);
13262 n = read16 (ip + 2);
13264 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
13266 emit_stloc_ir (cfg, sp, header, n);
13273 if (sp != stack_start)
13275 if (cfg->method != method)
13277 * Inlining this into a loop in a parent could lead to
13278 * stack overflows which is different behavior than the
13279 * non-inlined case, thus disable inlining in this case.
13281 INLINE_FAILURE("localloc");
13283 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
13284 ins->dreg = alloc_preg (cfg);
13285 ins->sreg1 = sp [0]->dreg;
13286 ins->type = STACK_PTR;
13287 MONO_ADD_INS (cfg->cbb, ins);
13289 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13291 ins->flags |= MONO_INST_INIT;
13296 case CEE_ENDFILTER: {
13297 MonoExceptionClause *clause, *nearest;
13302 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
13304 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
13305 ins->sreg1 = (*sp)->dreg;
13306 MONO_ADD_INS (cfg->cbb, ins);
13307 start_new_bblock = 1;
13311 for (cc = 0; cc < header->num_clauses; ++cc) {
13312 clause = &header->clauses [cc];
13313 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
13314 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
13315 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
13318 g_assert (nearest);
13319 if ((ip - header->code) != nearest->handler_offset)
13324 case CEE_UNALIGNED_:
13325 ins_flag |= MONO_INST_UNALIGNED;
13326 /* FIXME: record alignment? we can assume 1 for now */
13330 case CEE_VOLATILE_:
13331 ins_flag |= MONO_INST_VOLATILE;
13335 ins_flag |= MONO_INST_TAILCALL;
13336 cfg->flags |= MONO_CFG_HAS_TAIL;
13337 /* Can't inline tail calls at this time */
13338 inline_costs += 100000;
13345 token = read32 (ip + 2);
13346 klass = mini_get_class (method, token, generic_context);
13347 CHECK_TYPELOAD (klass);
13348 if (generic_class_is_reference_type (cfg, klass))
13349 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
13351 mini_emit_initobj (cfg, *sp, NULL, klass);
13355 case CEE_CONSTRAINED_:
13357 token = read32 (ip + 2);
13358 constrained_class = mini_get_class (method, token, generic_context);
13359 CHECK_TYPELOAD (constrained_class);
13363 case CEE_INITBLK: {
13364 MonoInst *iargs [3];
13368 /* Skip optimized paths for volatile operations. */
13369 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)) {
13370 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
13371 } 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)) {
13372 /* emit_memset only works when val == 0 */
13373 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
13376 iargs [0] = sp [0];
13377 iargs [1] = sp [1];
13378 iargs [2] = sp [2];
13379 if (ip [1] == CEE_CPBLK) {
13381 * FIXME: It's unclear whether we should be emitting both the acquire
13382 * and release barriers for cpblk. It is technically both a load and
13383 * store operation, so it seems like that's the sensible thing to do.
13385 * FIXME: We emit full barriers on both sides of the operation for
13386 * simplicity. We should have a separate atomic memcpy method instead.
13388 MonoMethod *memcpy_method = get_memcpy_method ();
13390 if (ins_flag & MONO_INST_VOLATILE)
13391 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13393 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
13394 call->flags |= ins_flag;
13396 if (ins_flag & MONO_INST_VOLATILE)
13397 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13399 MonoMethod *memset_method = get_memset_method ();
13400 if (ins_flag & MONO_INST_VOLATILE) {
13401 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
13402 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
13404 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
13405 call->flags |= ins_flag;
13416 ins_flag |= MONO_INST_NOTYPECHECK;
13418 ins_flag |= MONO_INST_NORANGECHECK;
13419 /* we ignore the no-nullcheck for now since we
13420 * really do it explicitly only when doing callvirt->call
13424 case CEE_RETHROW: {
13426 int handler_offset = -1;
13428 for (i = 0; i < header->num_clauses; ++i) {
13429 MonoExceptionClause *clause = &header->clauses [i];
13430 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
13431 handler_offset = clause->handler_offset;
13436 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
13438 if (handler_offset == -1)
13441 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
13442 MONO_INST_NEW (cfg, ins, OP_RETHROW);
13443 ins->sreg1 = load->dreg;
13444 MONO_ADD_INS (cfg->cbb, ins);
13446 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
13447 MONO_ADD_INS (cfg->cbb, ins);
13450 link_bblock (cfg, cfg->cbb, end_bblock);
13451 start_new_bblock = 1;
13459 CHECK_STACK_OVF (1);
13461 token = read32 (ip + 2);
13462 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
13463 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
13466 val = mono_type_size (type, &ialign);
13468 MonoClass *klass = mini_get_class (method, token, generic_context);
13469 CHECK_TYPELOAD (klass);
13471 val = mono_type_size (&klass->byval_arg, &ialign);
13473 if (mini_is_gsharedvt_klass (klass))
13474 GSHAREDVT_FAILURE (*ip);
13476 EMIT_NEW_ICONST (cfg, ins, val);
13481 case CEE_REFANYTYPE: {
13482 MonoInst *src_var, *src;
13484 GSHAREDVT_FAILURE (*ip);
13490 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
13492 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
13493 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
13494 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
13499 case CEE_READONLY_:
13512 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
13522 g_warning ("opcode 0x%02x not handled", *ip);
13526 if (start_new_bblock != 1)
13529 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
13530 if (cfg->cbb->next_bb) {
13531 /* This could already be set because of inlining, #693905 */
13532 MonoBasicBlock *bb = cfg->cbb;
13534 while (bb->next_bb)
13536 bb->next_bb = end_bblock;
13538 cfg->cbb->next_bb = end_bblock;
13541 if (cfg->method == method && cfg->domainvar) {
13543 MonoInst *get_domain;
13545 cfg->cbb = init_localsbb;
13547 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
13548 MONO_ADD_INS (cfg->cbb, get_domain);
13550 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
13552 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
13553 MONO_ADD_INS (cfg->cbb, store);
13556 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
13557 if (cfg->compile_aot)
13558 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
13559 mono_get_got_var (cfg);
13562 if (cfg->method == method && cfg->got_var)
13563 mono_emit_load_got_addr (cfg);
13565 if (init_localsbb) {
13566 cfg->cbb = init_localsbb;
13568 for (i = 0; i < header->num_locals; ++i) {
13569 emit_init_local (cfg, i, header->locals [i], init_locals);
13573 if (cfg->init_ref_vars && cfg->method == method) {
13574 /* Emit initialization for ref vars */
13575 // FIXME: Avoid duplication initialization for IL locals.
13576 for (i = 0; i < cfg->num_varinfo; ++i) {
13577 MonoInst *ins = cfg->varinfo [i];
13579 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
13580 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
13584 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
13585 cfg->cbb = init_localsbb;
13586 emit_push_lmf (cfg);
13589 cfg->cbb = init_localsbb;
13590 emit_instrumentation_call (cfg, mono_profiler_method_enter);
13593 MonoBasicBlock *bb;
13596 * Make seq points at backward branch targets interruptable.
13598 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13599 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13600 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13603 /* Add a sequence point for method entry/exit events */
13604 if (seq_points && cfg->gen_sdb_seq_points) {
13605 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13606 MONO_ADD_INS (init_localsbb, ins);
13607 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13608 MONO_ADD_INS (cfg->bb_exit, ins);
13612 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13613 * the code they refer to was dead (#11880).
13615 if (sym_seq_points) {
13616 for (i = 0; i < header->code_size; ++i) {
13617 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13620 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13621 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13628 if (cfg->method == method) {
13629 MonoBasicBlock *bb;
13630 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13631 bb->region = mono_find_block_region (cfg, bb->real_offset);
13633 mono_create_spvar_for_region (cfg, bb->region);
13634 if (cfg->verbose_level > 2)
13635 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13639 if (inline_costs < 0) {
13642 /* Method is too large */
13643 mname = mono_method_full_name (method, TRUE);
13644 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13648 if ((cfg->verbose_level > 2) && (cfg->method == method))
13649 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13654 g_assert (!mono_error_ok (&cfg->error));
13658 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13662 set_exception_type_from_invalid_il (cfg, method, ip);
13666 g_slist_free (class_inits);
13667 mono_basic_block_free (original_bb);
13668 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13669 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
13670 if (cfg->exception_type)
13673 return inline_costs;
13677 store_membase_reg_to_store_membase_imm (int opcode)
13680 case OP_STORE_MEMBASE_REG:
13681 return OP_STORE_MEMBASE_IMM;
13682 case OP_STOREI1_MEMBASE_REG:
13683 return OP_STOREI1_MEMBASE_IMM;
13684 case OP_STOREI2_MEMBASE_REG:
13685 return OP_STOREI2_MEMBASE_IMM;
13686 case OP_STOREI4_MEMBASE_REG:
13687 return OP_STOREI4_MEMBASE_IMM;
13688 case OP_STOREI8_MEMBASE_REG:
13689 return OP_STOREI8_MEMBASE_IMM;
13691 g_assert_not_reached ();
13698 mono_op_to_op_imm (int opcode)
13702 return OP_IADD_IMM;
13704 return OP_ISUB_IMM;
13706 return OP_IDIV_IMM;
13708 return OP_IDIV_UN_IMM;
13710 return OP_IREM_IMM;
13712 return OP_IREM_UN_IMM;
13714 return OP_IMUL_IMM;
13716 return OP_IAND_IMM;
13720 return OP_IXOR_IMM;
13722 return OP_ISHL_IMM;
13724 return OP_ISHR_IMM;
13726 return OP_ISHR_UN_IMM;
13729 return OP_LADD_IMM;
13731 return OP_LSUB_IMM;
13733 return OP_LAND_IMM;
13737 return OP_LXOR_IMM;
13739 return OP_LSHL_IMM;
13741 return OP_LSHR_IMM;
13743 return OP_LSHR_UN_IMM;
13744 #if SIZEOF_REGISTER == 8
13746 return OP_LREM_IMM;
13750 return OP_COMPARE_IMM;
13752 return OP_ICOMPARE_IMM;
13754 return OP_LCOMPARE_IMM;
13756 case OP_STORE_MEMBASE_REG:
13757 return OP_STORE_MEMBASE_IMM;
13758 case OP_STOREI1_MEMBASE_REG:
13759 return OP_STOREI1_MEMBASE_IMM;
13760 case OP_STOREI2_MEMBASE_REG:
13761 return OP_STOREI2_MEMBASE_IMM;
13762 case OP_STOREI4_MEMBASE_REG:
13763 return OP_STOREI4_MEMBASE_IMM;
13765 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13767 return OP_X86_PUSH_IMM;
13768 case OP_X86_COMPARE_MEMBASE_REG:
13769 return OP_X86_COMPARE_MEMBASE_IMM;
13771 #if defined(TARGET_AMD64)
13772 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13773 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13775 case OP_VOIDCALL_REG:
13776 return OP_VOIDCALL;
13784 return OP_LOCALLOC_IMM;
13791 ldind_to_load_membase (int opcode)
13795 return OP_LOADI1_MEMBASE;
13797 return OP_LOADU1_MEMBASE;
13799 return OP_LOADI2_MEMBASE;
13801 return OP_LOADU2_MEMBASE;
13803 return OP_LOADI4_MEMBASE;
13805 return OP_LOADU4_MEMBASE;
13807 return OP_LOAD_MEMBASE;
13808 case CEE_LDIND_REF:
13809 return OP_LOAD_MEMBASE;
13811 return OP_LOADI8_MEMBASE;
13813 return OP_LOADR4_MEMBASE;
13815 return OP_LOADR8_MEMBASE;
13817 g_assert_not_reached ();
13824 stind_to_store_membase (int opcode)
13828 return OP_STOREI1_MEMBASE_REG;
13830 return OP_STOREI2_MEMBASE_REG;
13832 return OP_STOREI4_MEMBASE_REG;
13834 case CEE_STIND_REF:
13835 return OP_STORE_MEMBASE_REG;
13837 return OP_STOREI8_MEMBASE_REG;
13839 return OP_STORER4_MEMBASE_REG;
13841 return OP_STORER8_MEMBASE_REG;
13843 g_assert_not_reached ();
13850 mono_load_membase_to_load_mem (int opcode)
13852 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13853 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13855 case OP_LOAD_MEMBASE:
13856 return OP_LOAD_MEM;
13857 case OP_LOADU1_MEMBASE:
13858 return OP_LOADU1_MEM;
13859 case OP_LOADU2_MEMBASE:
13860 return OP_LOADU2_MEM;
13861 case OP_LOADI4_MEMBASE:
13862 return OP_LOADI4_MEM;
13863 case OP_LOADU4_MEMBASE:
13864 return OP_LOADU4_MEM;
13865 #if SIZEOF_REGISTER == 8
13866 case OP_LOADI8_MEMBASE:
13867 return OP_LOADI8_MEM;
13876 op_to_op_dest_membase (int store_opcode, int opcode)
13878 #if defined(TARGET_X86)
13879 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13884 return OP_X86_ADD_MEMBASE_REG;
13886 return OP_X86_SUB_MEMBASE_REG;
13888 return OP_X86_AND_MEMBASE_REG;
13890 return OP_X86_OR_MEMBASE_REG;
13892 return OP_X86_XOR_MEMBASE_REG;
13895 return OP_X86_ADD_MEMBASE_IMM;
13898 return OP_X86_SUB_MEMBASE_IMM;
13901 return OP_X86_AND_MEMBASE_IMM;
13904 return OP_X86_OR_MEMBASE_IMM;
13907 return OP_X86_XOR_MEMBASE_IMM;
13913 #if defined(TARGET_AMD64)
13914 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13919 return OP_X86_ADD_MEMBASE_REG;
13921 return OP_X86_SUB_MEMBASE_REG;
13923 return OP_X86_AND_MEMBASE_REG;
13925 return OP_X86_OR_MEMBASE_REG;
13927 return OP_X86_XOR_MEMBASE_REG;
13929 return OP_X86_ADD_MEMBASE_IMM;
13931 return OP_X86_SUB_MEMBASE_IMM;
13933 return OP_X86_AND_MEMBASE_IMM;
13935 return OP_X86_OR_MEMBASE_IMM;
13937 return OP_X86_XOR_MEMBASE_IMM;
13939 return OP_AMD64_ADD_MEMBASE_REG;
13941 return OP_AMD64_SUB_MEMBASE_REG;
13943 return OP_AMD64_AND_MEMBASE_REG;
13945 return OP_AMD64_OR_MEMBASE_REG;
13947 return OP_AMD64_XOR_MEMBASE_REG;
13950 return OP_AMD64_ADD_MEMBASE_IMM;
13953 return OP_AMD64_SUB_MEMBASE_IMM;
13956 return OP_AMD64_AND_MEMBASE_IMM;
13959 return OP_AMD64_OR_MEMBASE_IMM;
13962 return OP_AMD64_XOR_MEMBASE_IMM;
13972 op_to_op_store_membase (int store_opcode, int opcode)
13974 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13977 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13978 return OP_X86_SETEQ_MEMBASE;
13980 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13981 return OP_X86_SETNE_MEMBASE;
13989 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13992 /* FIXME: This has sign extension issues */
13994 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13995 return OP_X86_COMPARE_MEMBASE8_IMM;
13998 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14003 return OP_X86_PUSH_MEMBASE;
14004 case OP_COMPARE_IMM:
14005 case OP_ICOMPARE_IMM:
14006 return OP_X86_COMPARE_MEMBASE_IMM;
14009 return OP_X86_COMPARE_MEMBASE_REG;
14013 #ifdef TARGET_AMD64
14014 /* FIXME: This has sign extension issues */
14016 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14017 return OP_X86_COMPARE_MEMBASE8_IMM;
14022 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14023 return OP_X86_PUSH_MEMBASE;
14025 /* FIXME: This only works for 32 bit immediates
14026 case OP_COMPARE_IMM:
14027 case OP_LCOMPARE_IMM:
14028 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
14029 return OP_AMD64_COMPARE_MEMBASE_IMM;
14031 case OP_ICOMPARE_IMM:
14032 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14033 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
14037 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
14038 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14039 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14040 return OP_AMD64_COMPARE_MEMBASE_REG;
14043 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14044 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14053 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
14056 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14062 return OP_X86_COMPARE_REG_MEMBASE;
14064 return OP_X86_ADD_REG_MEMBASE;
14066 return OP_X86_SUB_REG_MEMBASE;
14068 return OP_X86_AND_REG_MEMBASE;
14070 return OP_X86_OR_REG_MEMBASE;
14072 return OP_X86_XOR_REG_MEMBASE;
14076 #ifdef TARGET_AMD64
14077 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
14080 return OP_AMD64_ICOMPARE_REG_MEMBASE;
14082 return OP_X86_ADD_REG_MEMBASE;
14084 return OP_X86_SUB_REG_MEMBASE;
14086 return OP_X86_AND_REG_MEMBASE;
14088 return OP_X86_OR_REG_MEMBASE;
14090 return OP_X86_XOR_REG_MEMBASE;
14092 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
14096 return OP_AMD64_COMPARE_REG_MEMBASE;
14098 return OP_AMD64_ADD_REG_MEMBASE;
14100 return OP_AMD64_SUB_REG_MEMBASE;
14102 return OP_AMD64_AND_REG_MEMBASE;
14104 return OP_AMD64_OR_REG_MEMBASE;
14106 return OP_AMD64_XOR_REG_MEMBASE;
14115 mono_op_to_op_imm_noemul (int opcode)
14118 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
14124 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14131 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
14136 return mono_op_to_op_imm (opcode);
14141 * mono_handle_global_vregs:
14143 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
14147 mono_handle_global_vregs (MonoCompile *cfg)
14149 gint32 *vreg_to_bb;
14150 MonoBasicBlock *bb;
14153 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
14155 #ifdef MONO_ARCH_SIMD_INTRINSICS
14156 if (cfg->uses_simd_intrinsics)
14157 mono_simd_simplify_indirection (cfg);
14160 /* Find local vregs used in more than one bb */
14161 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14162 MonoInst *ins = bb->code;
14163 int block_num = bb->block_num;
14165 if (cfg->verbose_level > 2)
14166 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
14169 for (; ins; ins = ins->next) {
14170 const char *spec = INS_INFO (ins->opcode);
14171 int regtype = 0, regindex;
14174 if (G_UNLIKELY (cfg->verbose_level > 2))
14175 mono_print_ins (ins);
14177 g_assert (ins->opcode >= MONO_CEE_LAST);
14179 for (regindex = 0; regindex < 4; regindex ++) {
14182 if (regindex == 0) {
14183 regtype = spec [MONO_INST_DEST];
14184 if (regtype == ' ')
14187 } else if (regindex == 1) {
14188 regtype = spec [MONO_INST_SRC1];
14189 if (regtype == ' ')
14192 } else if (regindex == 2) {
14193 regtype = spec [MONO_INST_SRC2];
14194 if (regtype == ' ')
14197 } else if (regindex == 3) {
14198 regtype = spec [MONO_INST_SRC3];
14199 if (regtype == ' ')
14204 #if SIZEOF_REGISTER == 4
14205 /* In the LLVM case, the long opcodes are not decomposed */
14206 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
14208 * Since some instructions reference the original long vreg,
14209 * and some reference the two component vregs, it is quite hard
14210 * to determine when it needs to be global. So be conservative.
14212 if (!get_vreg_to_inst (cfg, vreg)) {
14213 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14215 if (cfg->verbose_level > 2)
14216 printf ("LONG VREG R%d made global.\n", vreg);
14220 * Make the component vregs volatile since the optimizations can
14221 * get confused otherwise.
14223 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
14224 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
14228 g_assert (vreg != -1);
14230 prev_bb = vreg_to_bb [vreg];
14231 if (prev_bb == 0) {
14232 /* 0 is a valid block num */
14233 vreg_to_bb [vreg] = block_num + 1;
14234 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
14235 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
14238 if (!get_vreg_to_inst (cfg, vreg)) {
14239 if (G_UNLIKELY (cfg->verbose_level > 2))
14240 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
14244 if (vreg_is_ref (cfg, vreg))
14245 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
14247 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
14250 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14253 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
14256 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
14259 g_assert_not_reached ();
14263 /* Flag as having been used in more than one bb */
14264 vreg_to_bb [vreg] = -1;
14270 /* If a variable is used in only one bblock, convert it into a local vreg */
14271 for (i = 0; i < cfg->num_varinfo; i++) {
14272 MonoInst *var = cfg->varinfo [i];
14273 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
14275 switch (var->type) {
14281 #if SIZEOF_REGISTER == 8
14284 #if !defined(TARGET_X86)
14285 /* Enabling this screws up the fp stack on x86 */
14288 if (mono_arch_is_soft_float ())
14292 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
14296 /* Arguments are implicitly global */
14297 /* Putting R4 vars into registers doesn't work currently */
14298 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
14299 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) {
14301 * Make that the variable's liveness interval doesn't contain a call, since
14302 * that would cause the lvreg to be spilled, making the whole optimization
14305 /* This is too slow for JIT compilation */
14307 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
14309 int def_index, call_index, ins_index;
14310 gboolean spilled = FALSE;
14315 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
14316 const char *spec = INS_INFO (ins->opcode);
14318 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
14319 def_index = ins_index;
14321 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
14322 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
14323 if (call_index > def_index) {
14329 if (MONO_IS_CALL (ins))
14330 call_index = ins_index;
14340 if (G_UNLIKELY (cfg->verbose_level > 2))
14341 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
14342 var->flags |= MONO_INST_IS_DEAD;
14343 cfg->vreg_to_inst [var->dreg] = NULL;
14350 * Compress the varinfo and vars tables so the liveness computation is faster and
14351 * takes up less space.
14354 for (i = 0; i < cfg->num_varinfo; ++i) {
14355 MonoInst *var = cfg->varinfo [i];
14356 if (pos < i && cfg->locals_start == i)
14357 cfg->locals_start = pos;
14358 if (!(var->flags & MONO_INST_IS_DEAD)) {
14360 cfg->varinfo [pos] = cfg->varinfo [i];
14361 cfg->varinfo [pos]->inst_c0 = pos;
14362 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
14363 cfg->vars [pos].idx = pos;
14364 #if SIZEOF_REGISTER == 4
14365 if (cfg->varinfo [pos]->type == STACK_I8) {
14366 /* Modify the two component vars too */
14369 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
14370 var1->inst_c0 = pos;
14371 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
14372 var1->inst_c0 = pos;
14379 cfg->num_varinfo = pos;
14380 if (cfg->locals_start > cfg->num_varinfo)
14381 cfg->locals_start = cfg->num_varinfo;
14385 * mono_allocate_gsharedvt_vars:
14387 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
14388 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
14391 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
14395 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
14397 for (i = 0; i < cfg->num_varinfo; ++i) {
14398 MonoInst *ins = cfg->varinfo [i];
14401 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
14402 if (i >= cfg->locals_start) {
14404 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
14405 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
14406 ins->opcode = OP_GSHAREDVT_LOCAL;
14407 ins->inst_imm = idx;
14410 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
14411 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
14418 * mono_spill_global_vars:
14420 * Generate spill code for variables which are not allocated to registers,
14421 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
14422 * code is generated which could be optimized by the local optimization passes.
14425 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
14427 MonoBasicBlock *bb;
14429 int orig_next_vreg;
14430 guint32 *vreg_to_lvreg;
14432 guint32 i, lvregs_len;
14433 gboolean dest_has_lvreg = FALSE;
14434 MonoStackType stacktypes [128];
14435 MonoInst **live_range_start, **live_range_end;
14436 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
14438 *need_local_opts = FALSE;
14440 memset (spec2, 0, sizeof (spec2));
14442 /* FIXME: Move this function to mini.c */
14443 stacktypes ['i'] = STACK_PTR;
14444 stacktypes ['l'] = STACK_I8;
14445 stacktypes ['f'] = STACK_R8;
14446 #ifdef MONO_ARCH_SIMD_INTRINSICS
14447 stacktypes ['x'] = STACK_VTYPE;
14450 #if SIZEOF_REGISTER == 4
14451 /* Create MonoInsts for longs */
14452 for (i = 0; i < cfg->num_varinfo; i++) {
14453 MonoInst *ins = cfg->varinfo [i];
14455 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
14456 switch (ins->type) {
14461 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
14464 g_assert (ins->opcode == OP_REGOFFSET);
14466 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
14468 tree->opcode = OP_REGOFFSET;
14469 tree->inst_basereg = ins->inst_basereg;
14470 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
14472 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
14474 tree->opcode = OP_REGOFFSET;
14475 tree->inst_basereg = ins->inst_basereg;
14476 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
14486 if (cfg->compute_gc_maps) {
14487 /* registers need liveness info even for !non refs */
14488 for (i = 0; i < cfg->num_varinfo; i++) {
14489 MonoInst *ins = cfg->varinfo [i];
14491 if (ins->opcode == OP_REGVAR)
14492 ins->flags |= MONO_INST_GC_TRACK;
14496 /* FIXME: widening and truncation */
14499 * As an optimization, when a variable allocated to the stack is first loaded into
14500 * an lvreg, we will remember the lvreg and use it the next time instead of loading
14501 * the variable again.
14503 orig_next_vreg = cfg->next_vreg;
14504 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
14505 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
14509 * These arrays contain the first and last instructions accessing a given
14511 * Since we emit bblocks in the same order we process them here, and we
14512 * don't split live ranges, these will precisely describe the live range of
14513 * the variable, i.e. the instruction range where a valid value can be found
14514 * in the variables location.
14515 * The live range is computed using the liveness info computed by the liveness pass.
14516 * We can't use vmv->range, since that is an abstract live range, and we need
14517 * one which is instruction precise.
14518 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
14520 /* FIXME: Only do this if debugging info is requested */
14521 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
14522 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
14523 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14524 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14526 /* Add spill loads/stores */
14527 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14530 if (cfg->verbose_level > 2)
14531 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
14533 /* Clear vreg_to_lvreg array */
14534 for (i = 0; i < lvregs_len; i++)
14535 vreg_to_lvreg [lvregs [i]] = 0;
14539 MONO_BB_FOR_EACH_INS (bb, ins) {
14540 const char *spec = INS_INFO (ins->opcode);
14541 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
14542 gboolean store, no_lvreg;
14543 int sregs [MONO_MAX_SRC_REGS];
14545 if (G_UNLIKELY (cfg->verbose_level > 2))
14546 mono_print_ins (ins);
14548 if (ins->opcode == OP_NOP)
14552 * We handle LDADDR here as well, since it can only be decomposed
14553 * when variable addresses are known.
14555 if (ins->opcode == OP_LDADDR) {
14556 MonoInst *var = (MonoInst *)ins->inst_p0;
14558 if (var->opcode == OP_VTARG_ADDR) {
14559 /* Happens on SPARC/S390 where vtypes are passed by reference */
14560 MonoInst *vtaddr = var->inst_left;
14561 if (vtaddr->opcode == OP_REGVAR) {
14562 ins->opcode = OP_MOVE;
14563 ins->sreg1 = vtaddr->dreg;
14565 else if (var->inst_left->opcode == OP_REGOFFSET) {
14566 ins->opcode = OP_LOAD_MEMBASE;
14567 ins->inst_basereg = vtaddr->inst_basereg;
14568 ins->inst_offset = vtaddr->inst_offset;
14571 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
14572 /* gsharedvt arg passed by ref */
14573 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
14575 ins->opcode = OP_LOAD_MEMBASE;
14576 ins->inst_basereg = var->inst_basereg;
14577 ins->inst_offset = var->inst_offset;
14578 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
14579 MonoInst *load, *load2, *load3;
14580 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
14581 int reg1, reg2, reg3;
14582 MonoInst *info_var = cfg->gsharedvt_info_var;
14583 MonoInst *locals_var = cfg->gsharedvt_locals_var;
14587 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14590 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14592 g_assert (info_var);
14593 g_assert (locals_var);
14595 /* Mark the instruction used to compute the locals var as used */
14596 cfg->gsharedvt_locals_var_ins = NULL;
14598 /* Load the offset */
14599 if (info_var->opcode == OP_REGOFFSET) {
14600 reg1 = alloc_ireg (cfg);
14601 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14602 } else if (info_var->opcode == OP_REGVAR) {
14604 reg1 = info_var->dreg;
14606 g_assert_not_reached ();
14608 reg2 = alloc_ireg (cfg);
14609 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14610 /* Load the locals area address */
14611 reg3 = alloc_ireg (cfg);
14612 if (locals_var->opcode == OP_REGOFFSET) {
14613 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14614 } else if (locals_var->opcode == OP_REGVAR) {
14615 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14617 g_assert_not_reached ();
14619 /* Compute the address */
14620 ins->opcode = OP_PADD;
14624 mono_bblock_insert_before_ins (bb, ins, load3);
14625 mono_bblock_insert_before_ins (bb, load3, load2);
14627 mono_bblock_insert_before_ins (bb, load2, load);
14629 g_assert (var->opcode == OP_REGOFFSET);
14631 ins->opcode = OP_ADD_IMM;
14632 ins->sreg1 = var->inst_basereg;
14633 ins->inst_imm = var->inst_offset;
14636 *need_local_opts = TRUE;
14637 spec = INS_INFO (ins->opcode);
14640 if (ins->opcode < MONO_CEE_LAST) {
14641 mono_print_ins (ins);
14642 g_assert_not_reached ();
14646 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14650 if (MONO_IS_STORE_MEMBASE (ins)) {
14651 tmp_reg = ins->dreg;
14652 ins->dreg = ins->sreg2;
14653 ins->sreg2 = tmp_reg;
14656 spec2 [MONO_INST_DEST] = ' ';
14657 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14658 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14659 spec2 [MONO_INST_SRC3] = ' ';
14661 } else if (MONO_IS_STORE_MEMINDEX (ins))
14662 g_assert_not_reached ();
14667 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14668 printf ("\t %.3s %d", spec, ins->dreg);
14669 num_sregs = mono_inst_get_src_registers (ins, sregs);
14670 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14671 printf (" %d", sregs [srcindex]);
14678 regtype = spec [MONO_INST_DEST];
14679 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14682 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14683 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14684 MonoInst *store_ins;
14686 MonoInst *def_ins = ins;
14687 int dreg = ins->dreg; /* The original vreg */
14689 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14691 if (var->opcode == OP_REGVAR) {
14692 ins->dreg = var->dreg;
14693 } 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)) {
14695 * Instead of emitting a load+store, use a _membase opcode.
14697 g_assert (var->opcode == OP_REGOFFSET);
14698 if (ins->opcode == OP_MOVE) {
14702 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14703 ins->inst_basereg = var->inst_basereg;
14704 ins->inst_offset = var->inst_offset;
14707 spec = INS_INFO (ins->opcode);
14711 g_assert (var->opcode == OP_REGOFFSET);
14713 prev_dreg = ins->dreg;
14715 /* Invalidate any previous lvreg for this vreg */
14716 vreg_to_lvreg [ins->dreg] = 0;
14720 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14722 store_opcode = OP_STOREI8_MEMBASE_REG;
14725 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14727 #if SIZEOF_REGISTER != 8
14728 if (regtype == 'l') {
14729 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));
14730 mono_bblock_insert_after_ins (bb, ins, store_ins);
14731 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));
14732 mono_bblock_insert_after_ins (bb, ins, store_ins);
14733 def_ins = store_ins;
14738 g_assert (store_opcode != OP_STOREV_MEMBASE);
14740 /* Try to fuse the store into the instruction itself */
14741 /* FIXME: Add more instructions */
14742 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14743 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14744 ins->inst_imm = ins->inst_c0;
14745 ins->inst_destbasereg = var->inst_basereg;
14746 ins->inst_offset = var->inst_offset;
14747 spec = INS_INFO (ins->opcode);
14748 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14749 ins->opcode = store_opcode;
14750 ins->inst_destbasereg = var->inst_basereg;
14751 ins->inst_offset = var->inst_offset;
14755 tmp_reg = ins->dreg;
14756 ins->dreg = ins->sreg2;
14757 ins->sreg2 = tmp_reg;
14760 spec2 [MONO_INST_DEST] = ' ';
14761 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14762 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14763 spec2 [MONO_INST_SRC3] = ' ';
14765 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14766 // FIXME: The backends expect the base reg to be in inst_basereg
14767 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14769 ins->inst_basereg = var->inst_basereg;
14770 ins->inst_offset = var->inst_offset;
14771 spec = INS_INFO (ins->opcode);
14773 /* printf ("INS: "); mono_print_ins (ins); */
14774 /* Create a store instruction */
14775 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14777 /* Insert it after the instruction */
14778 mono_bblock_insert_after_ins (bb, ins, store_ins);
14780 def_ins = store_ins;
14783 * We can't assign ins->dreg to var->dreg here, since the
14784 * sregs could use it. So set a flag, and do it after
14787 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)))
14788 dest_has_lvreg = TRUE;
14793 if (def_ins && !live_range_start [dreg]) {
14794 live_range_start [dreg] = def_ins;
14795 live_range_start_bb [dreg] = bb;
14798 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14801 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14802 tmp->inst_c1 = dreg;
14803 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14810 num_sregs = mono_inst_get_src_registers (ins, sregs);
14811 for (srcindex = 0; srcindex < 3; ++srcindex) {
14812 regtype = spec [MONO_INST_SRC1 + srcindex];
14813 sreg = sregs [srcindex];
14815 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14816 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14817 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14818 MonoInst *use_ins = ins;
14819 MonoInst *load_ins;
14820 guint32 load_opcode;
14822 if (var->opcode == OP_REGVAR) {
14823 sregs [srcindex] = var->dreg;
14824 //mono_inst_set_src_registers (ins, sregs);
14825 live_range_end [sreg] = use_ins;
14826 live_range_end_bb [sreg] = bb;
14828 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14831 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14832 /* var->dreg is a hreg */
14833 tmp->inst_c1 = sreg;
14834 mono_bblock_insert_after_ins (bb, ins, tmp);
14840 g_assert (var->opcode == OP_REGOFFSET);
14842 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14844 g_assert (load_opcode != OP_LOADV_MEMBASE);
14846 if (vreg_to_lvreg [sreg]) {
14847 g_assert (vreg_to_lvreg [sreg] != -1);
14849 /* The variable is already loaded to an lvreg */
14850 if (G_UNLIKELY (cfg->verbose_level > 2))
14851 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14852 sregs [srcindex] = vreg_to_lvreg [sreg];
14853 //mono_inst_set_src_registers (ins, sregs);
14857 /* Try to fuse the load into the instruction */
14858 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14859 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14860 sregs [0] = var->inst_basereg;
14861 //mono_inst_set_src_registers (ins, sregs);
14862 ins->inst_offset = var->inst_offset;
14863 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14864 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14865 sregs [1] = var->inst_basereg;
14866 //mono_inst_set_src_registers (ins, sregs);
14867 ins->inst_offset = var->inst_offset;
14869 if (MONO_IS_REAL_MOVE (ins)) {
14870 ins->opcode = OP_NOP;
14873 //printf ("%d ", srcindex); mono_print_ins (ins);
14875 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14877 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) {
14878 if (var->dreg == prev_dreg) {
14880 * sreg refers to the value loaded by the load
14881 * emitted below, but we need to use ins->dreg
14882 * since it refers to the store emitted earlier.
14886 g_assert (sreg != -1);
14887 vreg_to_lvreg [var->dreg] = sreg;
14888 g_assert (lvregs_len < 1024);
14889 lvregs [lvregs_len ++] = var->dreg;
14893 sregs [srcindex] = sreg;
14894 //mono_inst_set_src_registers (ins, sregs);
14896 #if SIZEOF_REGISTER != 8
14897 if (regtype == 'l') {
14898 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14899 mono_bblock_insert_before_ins (bb, ins, load_ins);
14900 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14901 mono_bblock_insert_before_ins (bb, ins, load_ins);
14902 use_ins = load_ins;
14907 #if SIZEOF_REGISTER == 4
14908 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14910 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14911 mono_bblock_insert_before_ins (bb, ins, load_ins);
14912 use_ins = load_ins;
14916 if (var->dreg < orig_next_vreg) {
14917 live_range_end [var->dreg] = use_ins;
14918 live_range_end_bb [var->dreg] = bb;
14921 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14924 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14925 tmp->inst_c1 = var->dreg;
14926 mono_bblock_insert_after_ins (bb, ins, tmp);
14930 mono_inst_set_src_registers (ins, sregs);
14932 if (dest_has_lvreg) {
14933 g_assert (ins->dreg != -1);
14934 vreg_to_lvreg [prev_dreg] = ins->dreg;
14935 g_assert (lvregs_len < 1024);
14936 lvregs [lvregs_len ++] = prev_dreg;
14937 dest_has_lvreg = FALSE;
14941 tmp_reg = ins->dreg;
14942 ins->dreg = ins->sreg2;
14943 ins->sreg2 = tmp_reg;
14946 if (MONO_IS_CALL (ins)) {
14947 /* Clear vreg_to_lvreg array */
14948 for (i = 0; i < lvregs_len; i++)
14949 vreg_to_lvreg [lvregs [i]] = 0;
14951 } else if (ins->opcode == OP_NOP) {
14953 MONO_INST_NULLIFY_SREGS (ins);
14956 if (cfg->verbose_level > 2)
14957 mono_print_ins_index (1, ins);
14960 /* Extend the live range based on the liveness info */
14961 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14962 for (i = 0; i < cfg->num_varinfo; i ++) {
14963 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14965 if (vreg_is_volatile (cfg, vi->vreg))
14966 /* The liveness info is incomplete */
14969 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14970 /* Live from at least the first ins of this bb */
14971 live_range_start [vi->vreg] = bb->code;
14972 live_range_start_bb [vi->vreg] = bb;
14975 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14976 /* Live at least until the last ins of this bb */
14977 live_range_end [vi->vreg] = bb->last_ins;
14978 live_range_end_bb [vi->vreg] = bb;
14985 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14986 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14988 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14989 for (i = 0; i < cfg->num_varinfo; ++i) {
14990 int vreg = MONO_VARINFO (cfg, i)->vreg;
14993 if (live_range_start [vreg]) {
14994 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14996 ins->inst_c1 = vreg;
14997 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14999 if (live_range_end [vreg]) {
15000 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
15002 ins->inst_c1 = vreg;
15003 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
15004 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
15006 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
15011 if (cfg->gsharedvt_locals_var_ins) {
15012 /* Nullify if unused */
15013 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
15014 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
15017 g_free (live_range_start);
15018 g_free (live_range_end);
15019 g_free (live_range_start_bb);
15020 g_free (live_range_end_bb);
15025 * - use 'iadd' instead of 'int_add'
15026 * - handling ovf opcodes: decompose in method_to_ir.
15027 * - unify iregs/fregs
15028 * -> partly done, the missing parts are:
15029 * - a more complete unification would involve unifying the hregs as well, so
15030 * code wouldn't need if (fp) all over the place. but that would mean the hregs
15031 * would no longer map to the machine hregs, so the code generators would need to
15032 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
15033 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
15034 * fp/non-fp branches speeds it up by about 15%.
15035 * - use sext/zext opcodes instead of shifts
15037 * - get rid of TEMPLOADs if possible and use vregs instead
15038 * - clean up usage of OP_P/OP_ opcodes
15039 * - cleanup usage of DUMMY_USE
15040 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
15042 * - set the stack type and allocate a dreg in the EMIT_NEW macros
15043 * - get rid of all the <foo>2 stuff when the new JIT is ready.
15044 * - make sure handle_stack_args () is called before the branch is emitted
15045 * - when the new IR is done, get rid of all unused stuff
15046 * - COMPARE/BEQ as separate instructions or unify them ?
15047 * - keeping them separate allows specialized compare instructions like
15048 * compare_imm, compare_membase
15049 * - most back ends unify fp compare+branch, fp compare+ceq
15050 * - integrate mono_save_args into inline_method
15051 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
15052 * - handle long shift opts on 32 bit platforms somehow: they require
15053 * 3 sregs (2 for arg1 and 1 for arg2)
15054 * - make byref a 'normal' type.
15055 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
15056 * variable if needed.
15057 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
15058 * like inline_method.
15059 * - remove inlining restrictions
15060 * - fix LNEG and enable cfold of INEG
15061 * - generalize x86 optimizations like ldelema as a peephole optimization
15062 * - add store_mem_imm for amd64
15063 * - optimize the loading of the interruption flag in the managed->native wrappers
15064 * - avoid special handling of OP_NOP in passes
15065 * - move code inserting instructions into one function/macro.
15066 * - try a coalescing phase after liveness analysis
15067 * - add float -> vreg conversion + local optimizations on !x86
15068 * - figure out how to handle decomposed branches during optimizations, ie.
15069 * compare+branch, op_jump_table+op_br etc.
15070 * - promote RuntimeXHandles to vregs
15071 * - vtype cleanups:
15072 * - add a NEW_VARLOADA_VREG macro
15073 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
15074 * accessing vtype fields.
15075 * - get rid of I8CONST on 64 bit platforms
15076 * - dealing with the increase in code size due to branches created during opcode
15078 * - use extended basic blocks
15079 * - all parts of the JIT
15080 * - handle_global_vregs () && local regalloc
15081 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
15082 * - sources of increase in code size:
15085 * - isinst and castclass
15086 * - lvregs not allocated to global registers even if used multiple times
15087 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
15089 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
15090 * - add all micro optimizations from the old JIT
15091 * - put tree optimizations into the deadce pass
15092 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
15093 * specific function.
15094 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
15095 * fcompare + branchCC.
15096 * - create a helper function for allocating a stack slot, taking into account
15097 * MONO_CFG_HAS_SPILLUP.
15099 * - merge the ia64 switch changes.
15100 * - optimize mono_regstate2_alloc_int/float.
15101 * - fix the pessimistic handling of variables accessed in exception handler blocks.
15102 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
15103 * parts of the tree could be separated by other instructions, killing the tree
15104 * arguments, or stores killing loads etc. Also, should we fold loads into other
15105 * instructions if the result of the load is used multiple times ?
15106 * - make the REM_IMM optimization in mini-x86.c arch-independent.
15107 * - LAST MERGE: 108395.
15108 * - when returning vtypes in registers, generate IR and append it to the end of the
15109 * last bb instead of doing it in the epilog.
15110 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
15118 - When to decompose opcodes:
15119 - earlier: this makes some optimizations hard to implement, since the low level IR
15120 no longer contains the neccessary information. But it is easier to do.
15121 - later: harder to implement, enables more optimizations.
15122 - Branches inside bblocks:
15123 - created when decomposing complex opcodes.
15124 - branches to another bblock: harmless, but not tracked by the branch
15125 optimizations, so need to branch to a label at the start of the bblock.
15126 - branches to inside the same bblock: very problematic, trips up the local
15127 reg allocator. Can be fixed by spitting the current bblock, but that is a
15128 complex operation, since some local vregs can become global vregs etc.
15129 - Local/global vregs:
15130 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
15131 local register allocator.
15132 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
15133 structure, created by mono_create_var (). Assigned to hregs or the stack by
15134 the global register allocator.
15135 - When to do optimizations like alu->alu_imm:
15136 - earlier -> saves work later on since the IR will be smaller/simpler
15137 - later -> can work on more instructions
15138 - Handling of valuetypes:
15139 - When a vtype is pushed on the stack, a new temporary is created, an
15140 instruction computing its address (LDADDR) is emitted and pushed on
15141 the stack. Need to optimize cases when the vtype is used immediately as in
15142 argument passing, stloc etc.
15143 - Instead of the to_end stuff in the old JIT, simply call the function handling
15144 the values on the stack before emitting the last instruction of the bb.
15147 #endif /* DISABLE_JIT */