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-internal.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>
68 #include "jit-icalls.h"
70 #include "debugger-agent.h"
71 #include "seq-points.h"
73 #define BRANCH_COST 10
74 #define INLINE_LENGTH_LIMIT 20
76 /* These have 'cfg' as an implicit argument */
77 #define INLINE_FAILURE(msg) do { \
78 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
79 inline_failure (cfg, msg); \
80 goto exception_exit; \
83 #define CHECK_CFG_EXCEPTION do {\
84 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
85 goto exception_exit; \
87 #define METHOD_ACCESS_FAILURE(method, cmethod) do { \
88 method_access_failure ((cfg), (method), (cmethod)); \
89 goto exception_exit; \
91 #define FIELD_ACCESS_FAILURE(method, field) do { \
92 field_access_failure ((cfg), (method), (field)); \
93 goto exception_exit; \
95 #define GENERIC_SHARING_FAILURE(opcode) do { \
97 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
98 goto exception_exit; \
101 #define GSHAREDVT_FAILURE(opcode) do { \
102 if (cfg->gsharedvt) { \
103 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
104 goto exception_exit; \
107 #define OUT_OF_MEMORY_FAILURE do { \
108 mono_cfg_set_exception (cfg, MONO_EXCEPTION_OUT_OF_MEMORY); \
109 goto exception_exit; \
111 #define DISABLE_AOT(cfg) do { \
112 if ((cfg)->verbose_level >= 2) \
113 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
114 (cfg)->disable_aot = TRUE; \
116 #define LOAD_ERROR do { \
117 break_on_unverified (); \
118 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
119 goto exception_exit; \
122 #define TYPE_LOAD_ERROR(klass) do { \
123 cfg->exception_ptr = klass; \
127 #define CHECK_CFG_ERROR do {\
128 if (!mono_error_ok (&cfg->error)) { \
129 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
130 goto mono_error_exit; \
134 /* Determine whenever 'ins' represents a load of the 'this' argument */
135 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
137 static int ldind_to_load_membase (int opcode);
138 static int stind_to_store_membase (int opcode);
140 int mono_op_to_op_imm (int opcode);
141 int mono_op_to_op_imm_noemul (int opcode);
143 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
145 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
146 guchar *ip, guint real_offset, gboolean inline_always);
148 /* helper methods signatures */
149 static MonoMethodSignature *helper_sig_domain_get;
150 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
153 * Instruction metadata
161 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
162 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
168 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
173 /* keep in sync with the enum in mini.h */
176 #include "mini-ops.h"
181 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
182 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
184 * This should contain the index of the last sreg + 1. This is not the same
185 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
187 const gint8 ins_sreg_counts[] = {
188 #include "mini-ops.h"
193 #define MONO_INIT_VARINFO(vi,id) do { \
194 (vi)->range.first_use.pos.bid = 0xffff; \
200 mono_alloc_ireg (MonoCompile *cfg)
202 return alloc_ireg (cfg);
206 mono_alloc_lreg (MonoCompile *cfg)
208 return alloc_lreg (cfg);
212 mono_alloc_freg (MonoCompile *cfg)
214 return alloc_freg (cfg);
218 mono_alloc_preg (MonoCompile *cfg)
220 return alloc_preg (cfg);
224 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
226 return alloc_dreg (cfg, stack_type);
230 * mono_alloc_ireg_ref:
232 * Allocate an IREG, and mark it as holding a GC ref.
235 mono_alloc_ireg_ref (MonoCompile *cfg)
237 return alloc_ireg_ref (cfg);
241 * mono_alloc_ireg_mp:
243 * Allocate an IREG, and mark it as holding a managed pointer.
246 mono_alloc_ireg_mp (MonoCompile *cfg)
248 return alloc_ireg_mp (cfg);
252 * mono_alloc_ireg_copy:
254 * Allocate an IREG with the same GC type as VREG.
257 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
259 if (vreg_is_ref (cfg, vreg))
260 return alloc_ireg_ref (cfg);
261 else if (vreg_is_mp (cfg, vreg))
262 return alloc_ireg_mp (cfg);
264 return alloc_ireg (cfg);
268 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
273 type = mini_get_underlying_type (type);
275 switch (type->type) {
288 case MONO_TYPE_FNPTR:
290 case MONO_TYPE_CLASS:
291 case MONO_TYPE_STRING:
292 case MONO_TYPE_OBJECT:
293 case MONO_TYPE_SZARRAY:
294 case MONO_TYPE_ARRAY:
298 #if SIZEOF_REGISTER == 8
304 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
307 case MONO_TYPE_VALUETYPE:
308 if (type->data.klass->enumtype) {
309 type = mono_class_enum_basetype (type->data.klass);
312 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
315 case MONO_TYPE_TYPEDBYREF:
317 case MONO_TYPE_GENERICINST:
318 type = &type->data.generic_class->container_class->byval_arg;
322 g_assert (cfg->gshared);
323 if (mini_type_var_is_vt (type))
326 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
328 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
334 mono_print_bb (MonoBasicBlock *bb, const char *msg)
339 printf ("\n%s %d: [IN: ", msg, bb->block_num);
340 for (i = 0; i < bb->in_count; ++i)
341 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
343 for (i = 0; i < bb->out_count; ++i)
344 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
346 for (tree = bb->code; tree; tree = tree->next)
347 mono_print_ins_index (-1, tree);
351 mono_create_helper_signatures (void)
353 helper_sig_domain_get = mono_create_icall_signature ("ptr");
354 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
357 static MONO_NEVER_INLINE void
358 break_on_unverified (void)
360 if (mini_get_debug_options ()->break_on_unverified)
364 static MONO_NEVER_INLINE void
365 method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
367 char *method_fname = mono_method_full_name (method, TRUE);
368 char *cil_method_fname = mono_method_full_name (cil_method, TRUE);
369 mono_cfg_set_exception (cfg, MONO_EXCEPTION_METHOD_ACCESS);
370 cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);
371 g_free (method_fname);
372 g_free (cil_method_fname);
375 static MONO_NEVER_INLINE void
376 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
378 char *method_fname = mono_method_full_name (method, TRUE);
379 char *field_fname = mono_field_full_name (field);
380 mono_cfg_set_exception (cfg, MONO_EXCEPTION_FIELD_ACCESS);
381 cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
382 g_free (method_fname);
383 g_free (field_fname);
386 static MONO_NEVER_INLINE void
387 inline_failure (MonoCompile *cfg, const char *msg)
389 if (cfg->verbose_level >= 2)
390 printf ("inline failed: %s\n", msg);
391 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
394 static MONO_NEVER_INLINE void
395 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
397 if (cfg->verbose_level > 2) \
398 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);
399 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
402 static MONO_NEVER_INLINE void
403 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
405 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);
406 if (cfg->verbose_level >= 2)
407 printf ("%s\n", cfg->exception_message);
408 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
412 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
413 * foo<T> (int i) { ldarg.0; box T; }
415 #define UNVERIFIED do { \
416 if (cfg->gsharedvt) { \
417 if (cfg->verbose_level > 2) \
418 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
419 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
420 goto exception_exit; \
422 break_on_unverified (); \
426 #define GET_BBLOCK(cfg,tblock,ip) do { \
427 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
429 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
430 NEW_BBLOCK (cfg, (tblock)); \
431 (tblock)->cil_code = (ip); \
432 ADD_BBLOCK (cfg, (tblock)); \
436 #if defined(TARGET_X86) || defined(TARGET_AMD64)
437 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
438 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
439 (dest)->dreg = alloc_ireg_mp ((cfg)); \
440 (dest)->sreg1 = (sr1); \
441 (dest)->sreg2 = (sr2); \
442 (dest)->inst_imm = (imm); \
443 (dest)->backend.shift_amount = (shift); \
444 MONO_ADD_INS ((cfg)->cbb, (dest)); \
448 /* Emit conversions so both operands of a binary opcode are of the same type */
450 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
452 MonoInst *arg1 = *arg1_ref;
453 MonoInst *arg2 = *arg2_ref;
456 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
457 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
460 /* Mixing r4/r8 is allowed by the spec */
461 if (arg1->type == STACK_R4) {
462 int dreg = alloc_freg (cfg);
464 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
465 conv->type = STACK_R8;
469 if (arg2->type == STACK_R4) {
470 int dreg = alloc_freg (cfg);
472 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
473 conv->type = STACK_R8;
479 #if SIZEOF_REGISTER == 8
480 /* FIXME: Need to add many more cases */
481 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
484 int dr = alloc_preg (cfg);
485 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
486 (ins)->sreg2 = widen->dreg;
491 #define ADD_BINOP(op) do { \
492 MONO_INST_NEW (cfg, ins, (op)); \
494 ins->sreg1 = sp [0]->dreg; \
495 ins->sreg2 = sp [1]->dreg; \
496 type_from_op (cfg, ins, sp [0], sp [1]); \
498 /* Have to insert a widening op */ \
499 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
500 ins->dreg = alloc_dreg ((cfg), (ins)->type); \
501 MONO_ADD_INS ((cfg)->cbb, (ins)); \
502 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
505 #define ADD_UNOP(op) do { \
506 MONO_INST_NEW (cfg, ins, (op)); \
508 ins->sreg1 = sp [0]->dreg; \
509 type_from_op (cfg, ins, sp [0], NULL); \
511 (ins)->dreg = alloc_dreg ((cfg), (ins)->type); \
512 MONO_ADD_INS ((cfg)->cbb, (ins)); \
513 *sp++ = mono_decompose_opcode (cfg, ins); \
516 #define ADD_BINCOND(next_block) do { \
519 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
520 cmp->sreg1 = sp [0]->dreg; \
521 cmp->sreg2 = sp [1]->dreg; \
522 type_from_op (cfg, cmp, sp [0], sp [1]); \
524 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
525 type_from_op (cfg, ins, sp [0], sp [1]); \
526 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
527 GET_BBLOCK (cfg, tblock, target); \
528 link_bblock (cfg, cfg->cbb, tblock); \
529 ins->inst_true_bb = tblock; \
530 if ((next_block)) { \
531 link_bblock (cfg, cfg->cbb, (next_block)); \
532 ins->inst_false_bb = (next_block); \
533 start_new_bblock = 1; \
535 GET_BBLOCK (cfg, tblock, ip); \
536 link_bblock (cfg, cfg->cbb, tblock); \
537 ins->inst_false_bb = tblock; \
538 start_new_bblock = 2; \
540 if (sp != stack_start) { \
541 handle_stack_args (cfg, stack_start, sp - stack_start); \
542 CHECK_UNVERIFIABLE (cfg); \
544 MONO_ADD_INS (cfg->cbb, cmp); \
545 MONO_ADD_INS (cfg->cbb, ins); \
549 * link_bblock: Links two basic blocks
551 * links two basic blocks in the control flow graph, the 'from'
552 * argument is the starting block and the 'to' argument is the block
553 * the control flow ends to after 'from'.
556 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
558 MonoBasicBlock **newa;
562 if (from->cil_code) {
564 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
566 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
569 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
571 printf ("edge from entry to exit\n");
576 for (i = 0; i < from->out_count; ++i) {
577 if (to == from->out_bb [i]) {
583 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
584 for (i = 0; i < from->out_count; ++i) {
585 newa [i] = from->out_bb [i];
593 for (i = 0; i < to->in_count; ++i) {
594 if (from == to->in_bb [i]) {
600 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
601 for (i = 0; i < to->in_count; ++i) {
602 newa [i] = to->in_bb [i];
611 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
613 link_bblock (cfg, from, to);
617 * mono_find_block_region:
619 * We mark each basic block with a region ID. We use that to avoid BB
620 * optimizations when blocks are in different regions.
623 * A region token that encodes where this region is, and information
624 * about the clause owner for this block.
626 * The region encodes the try/catch/filter clause that owns this block
627 * as well as the type. -1 is a special value that represents a block
628 * that is in none of try/catch/filter.
631 mono_find_block_region (MonoCompile *cfg, int offset)
633 MonoMethodHeader *header = cfg->header;
634 MonoExceptionClause *clause;
637 for (i = 0; i < header->num_clauses; ++i) {
638 clause = &header->clauses [i];
639 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
640 (offset < (clause->handler_offset)))
641 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
643 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
644 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
645 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
646 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
647 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
649 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
652 for (i = 0; i < header->num_clauses; ++i) {
653 clause = &header->clauses [i];
655 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
656 return ((i + 1) << 8) | clause->flags;
663 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
665 MonoMethodHeader *header = cfg->header;
666 MonoExceptionClause *clause;
670 for (i = 0; i < header->num_clauses; ++i) {
671 clause = &header->clauses [i];
672 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
673 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
674 if (clause->flags == type)
675 res = g_list_append (res, clause);
682 mono_create_spvar_for_region (MonoCompile *cfg, int region)
686 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
690 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
691 /* prevent it from being register allocated */
692 var->flags |= MONO_INST_VOLATILE;
694 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
698 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
700 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
704 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
708 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
712 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
713 /* prevent it from being register allocated */
714 var->flags |= MONO_INST_VOLATILE;
716 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
722 * Returns the type used in the eval stack when @type is loaded.
723 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
726 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
730 type = mini_get_underlying_type (type);
731 inst->klass = klass = mono_class_from_mono_type (type);
733 inst->type = STACK_MP;
738 switch (type->type) {
740 inst->type = STACK_INV;
748 inst->type = STACK_I4;
753 case MONO_TYPE_FNPTR:
754 inst->type = STACK_PTR;
756 case MONO_TYPE_CLASS:
757 case MONO_TYPE_STRING:
758 case MONO_TYPE_OBJECT:
759 case MONO_TYPE_SZARRAY:
760 case MONO_TYPE_ARRAY:
761 inst->type = STACK_OBJ;
765 inst->type = STACK_I8;
768 inst->type = cfg->r4_stack_type;
771 inst->type = STACK_R8;
773 case MONO_TYPE_VALUETYPE:
774 if (type->data.klass->enumtype) {
775 type = mono_class_enum_basetype (type->data.klass);
779 inst->type = STACK_VTYPE;
782 case MONO_TYPE_TYPEDBYREF:
783 inst->klass = mono_defaults.typed_reference_class;
784 inst->type = STACK_VTYPE;
786 case MONO_TYPE_GENERICINST:
787 type = &type->data.generic_class->container_class->byval_arg;
791 g_assert (cfg->gshared);
792 if (mini_is_gsharedvt_type (type)) {
793 g_assert (cfg->gsharedvt);
794 inst->type = STACK_VTYPE;
796 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
800 g_error ("unknown type 0x%02x in eval stack type", type->type);
805 * The following tables are used to quickly validate the IL code in type_from_op ().
808 bin_num_table [STACK_MAX] [STACK_MAX] = {
809 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
810 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
811 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
812 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
813 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
814 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
815 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
816 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
817 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
822 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
825 /* reduce the size of this table */
827 bin_int_table [STACK_MAX] [STACK_MAX] = {
828 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
829 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
830 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
831 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
832 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
833 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
834 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
835 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
839 bin_comp_table [STACK_MAX] [STACK_MAX] = {
840 /* Inv i L p F & O vt r4 */
842 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
843 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
844 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
845 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
846 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
847 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
848 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
849 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
852 /* reduce the size of this table */
854 shift_table [STACK_MAX] [STACK_MAX] = {
855 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
859 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
860 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
861 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
862 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
866 * Tables to map from the non-specific opcode to the matching
867 * type-specific opcode.
869 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
871 binops_op_map [STACK_MAX] = {
872 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
875 /* handles from CEE_NEG to CEE_CONV_U8 */
877 unops_op_map [STACK_MAX] = {
878 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
881 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
883 ovfops_op_map [STACK_MAX] = {
884 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
887 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
889 ovf2ops_op_map [STACK_MAX] = {
890 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
893 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
895 ovf3ops_op_map [STACK_MAX] = {
896 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
899 /* handles from CEE_BEQ to CEE_BLT_UN */
901 beqops_op_map [STACK_MAX] = {
902 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
905 /* handles from CEE_CEQ to CEE_CLT_UN */
907 ceqops_op_map [STACK_MAX] = {
908 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
912 * Sets ins->type (the type on the eval stack) according to the
913 * type of the opcode and the arguments to it.
914 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
916 * FIXME: this function sets ins->type unconditionally in some cases, but
917 * it should set it to invalid for some types (a conv.x on an object)
920 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
922 switch (ins->opcode) {
929 /* FIXME: check unverifiable args for STACK_MP */
930 ins->type = bin_num_table [src1->type] [src2->type];
931 ins->opcode += binops_op_map [ins->type];
938 ins->type = bin_int_table [src1->type] [src2->type];
939 ins->opcode += binops_op_map [ins->type];
944 ins->type = shift_table [src1->type] [src2->type];
945 ins->opcode += binops_op_map [ins->type];
950 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
951 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
952 ins->opcode = OP_LCOMPARE;
953 else if (src1->type == STACK_R4)
954 ins->opcode = OP_RCOMPARE;
955 else if (src1->type == STACK_R8)
956 ins->opcode = OP_FCOMPARE;
958 ins->opcode = OP_ICOMPARE;
960 case OP_ICOMPARE_IMM:
961 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
962 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
963 ins->opcode = OP_LCOMPARE_IMM;
975 ins->opcode += beqops_op_map [src1->type];
978 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
979 ins->opcode += ceqops_op_map [src1->type];
985 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
986 ins->opcode += ceqops_op_map [src1->type];
990 ins->type = neg_table [src1->type];
991 ins->opcode += unops_op_map [ins->type];
994 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
995 ins->type = src1->type;
997 ins->type = STACK_INV;
998 ins->opcode += unops_op_map [ins->type];
1004 ins->type = STACK_I4;
1005 ins->opcode += unops_op_map [src1->type];
1008 ins->type = STACK_R8;
1009 switch (src1->type) {
1012 ins->opcode = OP_ICONV_TO_R_UN;
1015 ins->opcode = OP_LCONV_TO_R_UN;
1019 case CEE_CONV_OVF_I1:
1020 case CEE_CONV_OVF_U1:
1021 case CEE_CONV_OVF_I2:
1022 case CEE_CONV_OVF_U2:
1023 case CEE_CONV_OVF_I4:
1024 case CEE_CONV_OVF_U4:
1025 ins->type = STACK_I4;
1026 ins->opcode += ovf3ops_op_map [src1->type];
1028 case CEE_CONV_OVF_I_UN:
1029 case CEE_CONV_OVF_U_UN:
1030 ins->type = STACK_PTR;
1031 ins->opcode += ovf2ops_op_map [src1->type];
1033 case CEE_CONV_OVF_I1_UN:
1034 case CEE_CONV_OVF_I2_UN:
1035 case CEE_CONV_OVF_I4_UN:
1036 case CEE_CONV_OVF_U1_UN:
1037 case CEE_CONV_OVF_U2_UN:
1038 case CEE_CONV_OVF_U4_UN:
1039 ins->type = STACK_I4;
1040 ins->opcode += ovf2ops_op_map [src1->type];
1043 ins->type = STACK_PTR;
1044 switch (src1->type) {
1046 ins->opcode = OP_ICONV_TO_U;
1050 #if SIZEOF_VOID_P == 8
1051 ins->opcode = OP_LCONV_TO_U;
1053 ins->opcode = OP_MOVE;
1057 ins->opcode = OP_LCONV_TO_U;
1060 ins->opcode = OP_FCONV_TO_U;
1066 ins->type = STACK_I8;
1067 ins->opcode += unops_op_map [src1->type];
1069 case CEE_CONV_OVF_I8:
1070 case CEE_CONV_OVF_U8:
1071 ins->type = STACK_I8;
1072 ins->opcode += ovf3ops_op_map [src1->type];
1074 case CEE_CONV_OVF_U8_UN:
1075 case CEE_CONV_OVF_I8_UN:
1076 ins->type = STACK_I8;
1077 ins->opcode += ovf2ops_op_map [src1->type];
1080 ins->type = cfg->r4_stack_type;
1081 ins->opcode += unops_op_map [src1->type];
1084 ins->type = STACK_R8;
1085 ins->opcode += unops_op_map [src1->type];
1088 ins->type = STACK_R8;
1092 ins->type = STACK_I4;
1093 ins->opcode += ovfops_op_map [src1->type];
1096 case CEE_CONV_OVF_I:
1097 case CEE_CONV_OVF_U:
1098 ins->type = STACK_PTR;
1099 ins->opcode += ovfops_op_map [src1->type];
1102 case CEE_ADD_OVF_UN:
1104 case CEE_MUL_OVF_UN:
1106 case CEE_SUB_OVF_UN:
1107 ins->type = bin_num_table [src1->type] [src2->type];
1108 ins->opcode += ovfops_op_map [src1->type];
1109 if (ins->type == STACK_R8)
1110 ins->type = STACK_INV;
1112 case OP_LOAD_MEMBASE:
1113 ins->type = STACK_PTR;
1115 case OP_LOADI1_MEMBASE:
1116 case OP_LOADU1_MEMBASE:
1117 case OP_LOADI2_MEMBASE:
1118 case OP_LOADU2_MEMBASE:
1119 case OP_LOADI4_MEMBASE:
1120 case OP_LOADU4_MEMBASE:
1121 ins->type = STACK_PTR;
1123 case OP_LOADI8_MEMBASE:
1124 ins->type = STACK_I8;
1126 case OP_LOADR4_MEMBASE:
1127 ins->type = cfg->r4_stack_type;
1129 case OP_LOADR8_MEMBASE:
1130 ins->type = STACK_R8;
1133 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1137 if (ins->type == STACK_MP)
1138 ins->klass = mono_defaults.object_class;
1143 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1149 param_table [STACK_MAX] [STACK_MAX] = {
1154 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1159 switch (args->type) {
1169 for (i = 0; i < sig->param_count; ++i) {
1170 switch (args [i].type) {
1174 if (!sig->params [i]->byref)
1178 if (sig->params [i]->byref)
1180 switch (sig->params [i]->type) {
1181 case MONO_TYPE_CLASS:
1182 case MONO_TYPE_STRING:
1183 case MONO_TYPE_OBJECT:
1184 case MONO_TYPE_SZARRAY:
1185 case MONO_TYPE_ARRAY:
1192 if (sig->params [i]->byref)
1194 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1203 /*if (!param_table [args [i].type] [sig->params [i]->type])
1211 * When we need a pointer to the current domain many times in a method, we
1212 * call mono_domain_get() once and we store the result in a local variable.
1213 * This function returns the variable that represents the MonoDomain*.
1215 inline static MonoInst *
1216 mono_get_domainvar (MonoCompile *cfg)
1218 if (!cfg->domainvar)
1219 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1220 return cfg->domainvar;
1224 * The got_var contains the address of the Global Offset Table when AOT
1228 mono_get_got_var (MonoCompile *cfg)
1230 #ifdef MONO_ARCH_NEED_GOT_VAR
1231 if (!cfg->compile_aot)
1233 if (!cfg->got_var) {
1234 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1236 return cfg->got_var;
1243 mono_get_vtable_var (MonoCompile *cfg)
1245 g_assert (cfg->gshared);
1247 if (!cfg->rgctx_var) {
1248 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1249 /* force the var to be stack allocated */
1250 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1253 return cfg->rgctx_var;
1257 type_from_stack_type (MonoInst *ins) {
1258 switch (ins->type) {
1259 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1260 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1261 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1262 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1263 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1265 return &ins->klass->this_arg;
1266 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1267 case STACK_VTYPE: return &ins->klass->byval_arg;
1269 g_error ("stack type %d to monotype not handled\n", ins->type);
1274 static G_GNUC_UNUSED int
1275 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1277 t = mono_type_get_underlying_type (t);
1289 case MONO_TYPE_FNPTR:
1291 case MONO_TYPE_CLASS:
1292 case MONO_TYPE_STRING:
1293 case MONO_TYPE_OBJECT:
1294 case MONO_TYPE_SZARRAY:
1295 case MONO_TYPE_ARRAY:
1301 return cfg->r4_stack_type;
1304 case MONO_TYPE_VALUETYPE:
1305 case MONO_TYPE_TYPEDBYREF:
1307 case MONO_TYPE_GENERICINST:
1308 if (mono_type_generic_inst_is_valuetype (t))
1314 g_assert_not_reached ();
1321 array_access_to_klass (int opcode)
1325 return mono_defaults.byte_class;
1327 return mono_defaults.uint16_class;
1330 return mono_defaults.int_class;
1333 return mono_defaults.sbyte_class;
1336 return mono_defaults.int16_class;
1339 return mono_defaults.int32_class;
1341 return mono_defaults.uint32_class;
1344 return mono_defaults.int64_class;
1347 return mono_defaults.single_class;
1350 return mono_defaults.double_class;
1351 case CEE_LDELEM_REF:
1352 case CEE_STELEM_REF:
1353 return mono_defaults.object_class;
1355 g_assert_not_reached ();
1361 * We try to share variables when possible
1364 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1369 /* inlining can result in deeper stacks */
1370 if (slot >= cfg->header->max_stack)
1371 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1373 pos = ins->type - 1 + slot * STACK_MAX;
1375 switch (ins->type) {
1382 if ((vnum = cfg->intvars [pos]))
1383 return cfg->varinfo [vnum];
1384 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1385 cfg->intvars [pos] = res->inst_c0;
1388 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1394 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1397 * Don't use this if a generic_context is set, since that means AOT can't
1398 * look up the method using just the image+token.
1399 * table == 0 means this is a reference made from a wrapper.
1401 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1402 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1403 jump_info_token->image = image;
1404 jump_info_token->token = token;
1405 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1410 * This function is called to handle items that are left on the evaluation stack
1411 * at basic block boundaries. What happens is that we save the values to local variables
1412 * and we reload them later when first entering the target basic block (with the
1413 * handle_loaded_temps () function).
1414 * A single joint point will use the same variables (stored in the array bb->out_stack or
1415 * bb->in_stack, if the basic block is before or after the joint point).
1417 * This function needs to be called _before_ emitting the last instruction of
1418 * the bb (i.e. before emitting a branch).
1419 * If the stack merge fails at a join point, cfg->unverifiable is set.
1422 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1425 MonoBasicBlock *bb = cfg->cbb;
1426 MonoBasicBlock *outb;
1427 MonoInst *inst, **locals;
1432 if (cfg->verbose_level > 3)
1433 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1434 if (!bb->out_scount) {
1435 bb->out_scount = count;
1436 //printf ("bblock %d has out:", bb->block_num);
1438 for (i = 0; i < bb->out_count; ++i) {
1439 outb = bb->out_bb [i];
1440 /* exception handlers are linked, but they should not be considered for stack args */
1441 if (outb->flags & BB_EXCEPTION_HANDLER)
1443 //printf (" %d", outb->block_num);
1444 if (outb->in_stack) {
1446 bb->out_stack = outb->in_stack;
1452 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1453 for (i = 0; i < count; ++i) {
1455 * try to reuse temps already allocated for this purpouse, if they occupy the same
1456 * stack slot and if they are of the same type.
1457 * This won't cause conflicts since if 'local' is used to
1458 * store one of the values in the in_stack of a bblock, then
1459 * the same variable will be used for the same outgoing stack
1461 * This doesn't work when inlining methods, since the bblocks
1462 * in the inlined methods do not inherit their in_stack from
1463 * the bblock they are inlined to. See bug #58863 for an
1466 if (cfg->inlined_method)
1467 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1469 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1474 for (i = 0; i < bb->out_count; ++i) {
1475 outb = bb->out_bb [i];
1476 /* exception handlers are linked, but they should not be considered for stack args */
1477 if (outb->flags & BB_EXCEPTION_HANDLER)
1479 if (outb->in_scount) {
1480 if (outb->in_scount != bb->out_scount) {
1481 cfg->unverifiable = TRUE;
1484 continue; /* check they are the same locals */
1486 outb->in_scount = count;
1487 outb->in_stack = bb->out_stack;
1490 locals = bb->out_stack;
1492 for (i = 0; i < count; ++i) {
1493 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1494 inst->cil_code = sp [i]->cil_code;
1495 sp [i] = locals [i];
1496 if (cfg->verbose_level > 3)
1497 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1501 * It is possible that the out bblocks already have in_stack assigned, and
1502 * the in_stacks differ. In this case, we will store to all the different
1509 /* Find a bblock which has a different in_stack */
1511 while (bindex < bb->out_count) {
1512 outb = bb->out_bb [bindex];
1513 /* exception handlers are linked, but they should not be considered for stack args */
1514 if (outb->flags & BB_EXCEPTION_HANDLER) {
1518 if (outb->in_stack != locals) {
1519 for (i = 0; i < count; ++i) {
1520 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1521 inst->cil_code = sp [i]->cil_code;
1522 sp [i] = locals [i];
1523 if (cfg->verbose_level > 3)
1524 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1526 locals = outb->in_stack;
1536 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1538 int ibitmap_reg = alloc_preg (cfg);
1539 #ifdef COMPRESSED_INTERFACE_BITMAP
1541 MonoInst *res, *ins;
1542 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1543 MONO_ADD_INS (cfg->cbb, ins);
1545 if (cfg->compile_aot)
1546 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_IID, klass);
1548 EMIT_NEW_ICONST (cfg, args [1], klass->interface_id);
1549 res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1550 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1552 int ibitmap_byte_reg = alloc_preg (cfg);
1554 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1556 if (cfg->compile_aot) {
1557 int iid_reg = alloc_preg (cfg);
1558 int shifted_iid_reg = alloc_preg (cfg);
1559 int ibitmap_byte_address_reg = alloc_preg (cfg);
1560 int masked_iid_reg = alloc_preg (cfg);
1561 int iid_one_bit_reg = alloc_preg (cfg);
1562 int iid_bit_reg = alloc_preg (cfg);
1563 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1564 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1565 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1566 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1567 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1568 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1569 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1570 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1572 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1573 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1579 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1580 * stored in "klass_reg" implements the interface "klass".
1583 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1585 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1589 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1590 * stored in "vtable_reg" implements the interface "klass".
1593 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1595 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1599 * Emit code which checks whenever the interface id of @klass is smaller than
1600 * than the value given by max_iid_reg.
1603 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1604 MonoBasicBlock *false_target)
1606 if (cfg->compile_aot) {
1607 int iid_reg = alloc_preg (cfg);
1608 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1609 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1612 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1614 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1616 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1619 /* Same as above, but obtains max_iid from a vtable */
1621 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1622 MonoBasicBlock *false_target)
1624 int max_iid_reg = alloc_preg (cfg);
1626 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1627 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1630 /* Same as above, but obtains max_iid from a klass */
1632 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1633 MonoBasicBlock *false_target)
1635 int max_iid_reg = alloc_preg (cfg);
1637 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1638 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1642 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1644 int idepth_reg = alloc_preg (cfg);
1645 int stypes_reg = alloc_preg (cfg);
1646 int stype = alloc_preg (cfg);
1648 mono_class_setup_supertypes (klass);
1650 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1651 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1652 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1653 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1655 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1656 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1658 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1659 } else if (cfg->compile_aot) {
1660 int const_reg = alloc_preg (cfg);
1661 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1662 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1664 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1666 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1670 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1672 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1676 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1678 int intf_reg = alloc_preg (cfg);
1680 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1681 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1682 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1684 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1686 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1690 * Variant of the above that takes a register to the class, not the vtable.
1693 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1695 int intf_bit_reg = alloc_preg (cfg);
1697 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1698 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1699 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1701 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1703 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1707 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1710 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1711 } else if (cfg->compile_aot) {
1712 int const_reg = alloc_preg (cfg);
1713 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1714 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1716 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1718 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1722 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1724 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1728 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1730 if (cfg->compile_aot) {
1731 int const_reg = alloc_preg (cfg);
1732 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1733 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1735 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1737 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1741 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1744 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1747 int rank_reg = alloc_preg (cfg);
1748 int eclass_reg = alloc_preg (cfg);
1750 g_assert (!klass_inst);
1751 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1752 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1753 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1754 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1755 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1756 if (klass->cast_class == mono_defaults.object_class) {
1757 int parent_reg = alloc_preg (cfg);
1758 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1759 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1760 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1761 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1762 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1763 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1764 } else if (klass->cast_class == mono_defaults.enum_class) {
1765 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1766 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1767 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1769 // Pass -1 as obj_reg to skip the check below for arrays of arrays
1770 mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1773 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1774 /* Check that the object is a vector too */
1775 int bounds_reg = alloc_preg (cfg);
1776 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1777 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1778 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1781 int idepth_reg = alloc_preg (cfg);
1782 int stypes_reg = alloc_preg (cfg);
1783 int stype = alloc_preg (cfg);
1785 mono_class_setup_supertypes (klass);
1787 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1788 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1789 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1790 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1792 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1793 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1794 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1799 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1801 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1805 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1809 g_assert (val == 0);
1814 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1817 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1820 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1823 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1825 #if SIZEOF_REGISTER == 8
1827 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1833 val_reg = alloc_preg (cfg);
1835 if (SIZEOF_REGISTER == 8)
1836 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1838 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1841 /* This could be optimized further if neccesary */
1843 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1850 #if !NO_UNALIGNED_ACCESS
1851 if (SIZEOF_REGISTER == 8) {
1853 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1858 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1866 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1871 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1876 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1883 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1890 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1891 g_assert (size < 10000);
1894 /* This could be optimized further if neccesary */
1896 cur_reg = alloc_preg (cfg);
1897 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1898 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1905 #if !NO_UNALIGNED_ACCESS
1906 if (SIZEOF_REGISTER == 8) {
1908 cur_reg = alloc_preg (cfg);
1909 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1910 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1919 cur_reg = alloc_preg (cfg);
1920 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1921 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1927 cur_reg = alloc_preg (cfg);
1928 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1929 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1935 cur_reg = alloc_preg (cfg);
1936 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1937 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1945 emit_tls_set (MonoCompile *cfg, int sreg1, int tls_key)
1949 if (cfg->compile_aot) {
1950 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1951 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1953 ins->sreg2 = c->dreg;
1954 MONO_ADD_INS (cfg->cbb, ins);
1956 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1958 ins->inst_offset = mini_get_tls_offset (tls_key);
1959 MONO_ADD_INS (cfg->cbb, ins);
1966 * Emit IR to push the current LMF onto the LMF stack.
1969 emit_push_lmf (MonoCompile *cfg)
1972 * Emit IR to push the LMF:
1973 * lmf_addr = <lmf_addr from tls>
1974 * lmf->lmf_addr = lmf_addr
1975 * lmf->prev_lmf = *lmf_addr
1978 int lmf_reg, prev_lmf_reg;
1979 MonoInst *ins, *lmf_ins;
1984 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
1985 /* Load current lmf */
1986 lmf_ins = mono_get_lmf_intrinsic (cfg);
1988 MONO_ADD_INS (cfg->cbb, lmf_ins);
1989 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1990 lmf_reg = ins->dreg;
1991 /* Save previous_lmf */
1992 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
1994 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
1997 * Store lmf_addr in a variable, so it can be allocated to a global register.
1999 if (!cfg->lmf_addr_var)
2000 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2003 ins = mono_get_jit_tls_intrinsic (cfg);
2005 int jit_tls_dreg = ins->dreg;
2007 MONO_ADD_INS (cfg->cbb, ins);
2008 lmf_reg = alloc_preg (cfg);
2009 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2011 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2014 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2016 MONO_ADD_INS (cfg->cbb, lmf_ins);
2019 MonoInst *args [16], *jit_tls_ins, *ins;
2021 /* Inline mono_get_lmf_addr () */
2022 /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2024 /* Load mono_jit_tls_id */
2025 if (cfg->compile_aot)
2026 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2028 EMIT_NEW_ICONST (cfg, args [0], mono_jit_tls_id);
2029 /* call pthread_getspecific () */
2030 jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2031 /* lmf_addr = &jit_tls->lmf */
2032 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2035 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2039 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2041 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2042 lmf_reg = ins->dreg;
2044 prev_lmf_reg = alloc_preg (cfg);
2045 /* Save previous_lmf */
2046 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2047 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2049 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2056 * Emit IR to pop the current LMF from the LMF stack.
2059 emit_pop_lmf (MonoCompile *cfg)
2061 int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2067 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2068 lmf_reg = ins->dreg;
2070 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2071 /* Load previous_lmf */
2072 prev_lmf_reg = alloc_preg (cfg);
2073 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2075 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2078 * Emit IR to pop the LMF:
2079 * *(lmf->lmf_addr) = lmf->prev_lmf
2081 /* This could be called before emit_push_lmf () */
2082 if (!cfg->lmf_addr_var)
2083 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2084 lmf_addr_reg = cfg->lmf_addr_var->dreg;
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));
2088 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2093 emit_instrumentation_call (MonoCompile *cfg, void *func)
2095 MonoInst *iargs [1];
2098 * Avoid instrumenting inlined methods since it can
2099 * distort profiling results.
2101 if (cfg->method != cfg->current_method)
2104 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2105 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2106 mono_emit_jit_icall (cfg, func, iargs);
2111 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
2114 type = mini_get_underlying_type (type);
2115 switch (type->type) {
2116 case MONO_TYPE_VOID:
2117 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2124 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2128 case MONO_TYPE_FNPTR:
2129 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2130 case MONO_TYPE_CLASS:
2131 case MONO_TYPE_STRING:
2132 case MONO_TYPE_OBJECT:
2133 case MONO_TYPE_SZARRAY:
2134 case MONO_TYPE_ARRAY:
2135 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2138 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2141 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2143 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2145 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2146 case MONO_TYPE_VALUETYPE:
2147 if (type->data.klass->enumtype) {
2148 type = mono_class_enum_basetype (type->data.klass);
2151 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2152 case MONO_TYPE_TYPEDBYREF:
2153 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2154 case MONO_TYPE_GENERICINST:
2155 type = &type->data.generic_class->container_class->byval_arg;
2158 case MONO_TYPE_MVAR:
2160 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2162 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2168 * target_type_is_incompatible:
2169 * @cfg: MonoCompile context
2171 * Check that the item @arg on the evaluation stack can be stored
2172 * in the target type (can be a local, or field, etc).
2173 * The cfg arg can be used to check if we need verification or just
2176 * Returns: non-0 value if arg can't be stored on a target.
2179 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2181 MonoType *simple_type;
2184 if (target->byref) {
2185 /* FIXME: check that the pointed to types match */
2186 if (arg->type == STACK_MP)
2187 return arg->klass != mono_class_from_mono_type (target);
2188 if (arg->type == STACK_PTR)
2193 simple_type = mini_get_underlying_type (target);
2194 switch (simple_type->type) {
2195 case MONO_TYPE_VOID:
2203 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2207 /* STACK_MP is needed when setting pinned locals */
2208 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2213 case MONO_TYPE_FNPTR:
2215 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2216 * in native int. (#688008).
2218 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2221 case MONO_TYPE_CLASS:
2222 case MONO_TYPE_STRING:
2223 case MONO_TYPE_OBJECT:
2224 case MONO_TYPE_SZARRAY:
2225 case MONO_TYPE_ARRAY:
2226 if (arg->type != STACK_OBJ)
2228 /* FIXME: check type compatibility */
2232 if (arg->type != STACK_I8)
2236 if (arg->type != cfg->r4_stack_type)
2240 if (arg->type != STACK_R8)
2243 case MONO_TYPE_VALUETYPE:
2244 if (arg->type != STACK_VTYPE)
2246 klass = mono_class_from_mono_type (simple_type);
2247 if (klass != arg->klass)
2250 case MONO_TYPE_TYPEDBYREF:
2251 if (arg->type != STACK_VTYPE)
2253 klass = mono_class_from_mono_type (simple_type);
2254 if (klass != arg->klass)
2257 case MONO_TYPE_GENERICINST:
2258 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2259 if (arg->type != STACK_VTYPE)
2261 klass = mono_class_from_mono_type (simple_type);
2262 /* The second cases is needed when doing partial sharing */
2263 if (klass != arg->klass && mono_class_from_mono_type (target) != arg->klass)
2267 if (arg->type != STACK_OBJ)
2269 /* FIXME: check type compatibility */
2273 case MONO_TYPE_MVAR:
2274 g_assert (cfg->gshared);
2275 if (mini_type_var_is_vt (simple_type)) {
2276 if (arg->type != STACK_VTYPE)
2279 if (arg->type != STACK_OBJ)
2284 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2290 * Prepare arguments for passing to a function call.
2291 * Return a non-zero value if the arguments can't be passed to the given
2293 * The type checks are not yet complete and some conversions may need
2294 * casts on 32 or 64 bit architectures.
2296 * FIXME: implement this using target_type_is_incompatible ()
2299 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2301 MonoType *simple_type;
2305 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2309 for (i = 0; i < sig->param_count; ++i) {
2310 if (sig->params [i]->byref) {
2311 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2315 simple_type = mini_get_underlying_type (sig->params [i]);
2317 switch (simple_type->type) {
2318 case MONO_TYPE_VOID:
2327 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2333 case MONO_TYPE_FNPTR:
2334 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2337 case MONO_TYPE_CLASS:
2338 case MONO_TYPE_STRING:
2339 case MONO_TYPE_OBJECT:
2340 case MONO_TYPE_SZARRAY:
2341 case MONO_TYPE_ARRAY:
2342 if (args [i]->type != STACK_OBJ)
2347 if (args [i]->type != STACK_I8)
2351 if (args [i]->type != cfg->r4_stack_type)
2355 if (args [i]->type != STACK_R8)
2358 case MONO_TYPE_VALUETYPE:
2359 if (simple_type->data.klass->enumtype) {
2360 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2363 if (args [i]->type != STACK_VTYPE)
2366 case MONO_TYPE_TYPEDBYREF:
2367 if (args [i]->type != STACK_VTYPE)
2370 case MONO_TYPE_GENERICINST:
2371 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2374 case MONO_TYPE_MVAR:
2376 if (args [i]->type != STACK_VTYPE)
2380 g_error ("unknown type 0x%02x in check_call_signature",
2388 callvirt_to_call (int opcode)
2391 case OP_CALL_MEMBASE:
2393 case OP_VOIDCALL_MEMBASE:
2395 case OP_FCALL_MEMBASE:
2397 case OP_RCALL_MEMBASE:
2399 case OP_VCALL_MEMBASE:
2401 case OP_LCALL_MEMBASE:
2404 g_assert_not_reached ();
2410 /* Either METHOD or IMT_ARG needs to be set */
2412 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2416 if (COMPILE_LLVM (cfg)) {
2417 method_reg = alloc_preg (cfg);
2420 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2421 } else if (cfg->compile_aot) {
2422 MONO_EMIT_NEW_AOTCONST (cfg, method_reg, method, MONO_PATCH_INFO_METHODCONST);
2425 MONO_INST_NEW (cfg, ins, OP_PCONST);
2426 ins->inst_p0 = method;
2427 ins->dreg = method_reg;
2428 MONO_ADD_INS (cfg->cbb, ins);
2432 call->imt_arg_reg = method_reg;
2434 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2438 method_reg = alloc_preg (cfg);
2441 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2442 } else if (cfg->compile_aot) {
2443 MONO_EMIT_NEW_AOTCONST (cfg, method_reg, method, MONO_PATCH_INFO_METHODCONST);
2446 MONO_INST_NEW (cfg, ins, OP_PCONST);
2447 ins->inst_p0 = method;
2448 ins->dreg = method_reg;
2449 MONO_ADD_INS (cfg->cbb, ins);
2452 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2455 static MonoJumpInfo *
2456 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2458 MonoJumpInfo *ji = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2462 ji->data.target = target;
2468 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2471 return mono_class_check_context_used (klass);
2477 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2480 return mono_method_check_context_used (method);
2486 * check_method_sharing:
2488 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2491 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2493 gboolean pass_vtable = FALSE;
2494 gboolean pass_mrgctx = FALSE;
2496 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2497 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
2498 gboolean sharable = FALSE;
2500 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2504 * Pass vtable iff target method might
2505 * be shared, which means that sharing
2506 * is enabled for its class and its
2507 * context is sharable (and it's not a
2510 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2514 if (mini_method_get_context (cmethod) &&
2515 mini_method_get_context (cmethod)->method_inst) {
2516 g_assert (!pass_vtable);
2518 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2521 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2526 if (out_pass_vtable)
2527 *out_pass_vtable = pass_vtable;
2528 if (out_pass_mrgctx)
2529 *out_pass_mrgctx = pass_mrgctx;
2532 inline static MonoCallInst *
2533 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2534 MonoInst **args, int calli, int virtual, int tail, int rgctx, int unbox_trampoline)
2538 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2543 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2545 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2547 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual));
2550 call->signature = sig;
2551 call->rgctx_reg = rgctx;
2552 sig_ret = mini_get_underlying_type (sig->ret);
2554 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2557 if (mini_type_is_vtype (sig_ret)) {
2558 call->vret_var = cfg->vret_addr;
2559 //g_assert_not_reached ();
2561 } else if (mini_type_is_vtype (sig_ret)) {
2562 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2565 temp->backend.is_pinvoke = sig->pinvoke;
2568 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2569 * address of return value to increase optimization opportunities.
2570 * Before vtype decomposition, the dreg of the call ins itself represents the
2571 * fact the call modifies the return value. After decomposition, the call will
2572 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2573 * will be transformed into an LDADDR.
2575 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2576 loada->dreg = alloc_preg (cfg);
2577 loada->inst_p0 = temp;
2578 /* We reference the call too since call->dreg could change during optimization */
2579 loada->inst_p1 = call;
2580 MONO_ADD_INS (cfg->cbb, loada);
2582 call->inst.dreg = temp->dreg;
2584 call->vret_var = loada;
2585 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2586 call->inst.dreg = alloc_dreg (cfg, call->inst.type);
2588 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2589 if (COMPILE_SOFT_FLOAT (cfg)) {
2591 * If the call has a float argument, we would need to do an r8->r4 conversion using
2592 * an icall, but that cannot be done during the call sequence since it would clobber
2593 * the call registers + the stack. So we do it before emitting the call.
2595 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2597 MonoInst *in = call->args [i];
2599 if (i >= sig->hasthis)
2600 t = sig->params [i - sig->hasthis];
2602 t = &mono_defaults.int_class->byval_arg;
2603 t = mono_type_get_underlying_type (t);
2605 if (!t->byref && t->type == MONO_TYPE_R4) {
2606 MonoInst *iargs [1];
2610 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2612 /* The result will be in an int vreg */
2613 call->args [i] = conv;
2619 call->need_unbox_trampoline = unbox_trampoline;
2622 if (COMPILE_LLVM (cfg))
2623 mono_llvm_emit_call (cfg, call);
2625 mono_arch_emit_call (cfg, call);
2627 mono_arch_emit_call (cfg, call);
2630 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2631 cfg->flags |= MONO_CFG_HAS_CALLS;
2637 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2639 #ifdef MONO_ARCH_RGCTX_REG
2640 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2641 cfg->uses_rgctx_reg = TRUE;
2642 call->rgctx_reg = TRUE;
2644 call->rgctx_arg_reg = rgctx_reg;
2651 inline static MonoInst*
2652 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2657 gboolean check_sp = FALSE;
2659 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2660 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2662 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2667 rgctx_reg = mono_alloc_preg (cfg);
2668 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2672 if (!cfg->stack_inbalance_var)
2673 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2675 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2676 ins->dreg = cfg->stack_inbalance_var->dreg;
2677 MONO_ADD_INS (cfg->cbb, ins);
2680 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2682 call->inst.sreg1 = addr->dreg;
2685 emit_imt_argument (cfg, call, NULL, imt_arg);
2687 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2692 sp_reg = mono_alloc_preg (cfg);
2694 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2696 MONO_ADD_INS (cfg->cbb, ins);
2698 /* Restore the stack so we don't crash when throwing the exception */
2699 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2700 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2701 MONO_ADD_INS (cfg->cbb, ins);
2703 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2704 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2708 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2710 return (MonoInst*)call;
2714 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2717 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2719 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2722 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2723 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2725 #ifndef DISABLE_REMOTING
2726 gboolean might_be_remote = FALSE;
2728 gboolean virtual = this_ins != NULL;
2729 gboolean enable_for_aot = TRUE;
2733 gboolean need_unbox_trampoline;
2736 sig = mono_method_signature (method);
2739 rgctx_reg = mono_alloc_preg (cfg);
2740 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2743 if (method->string_ctor) {
2744 /* Create the real signature */
2745 /* FIXME: Cache these */
2746 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2747 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2752 context_used = mini_method_check_context_used (cfg, method);
2754 #ifndef DISABLE_REMOTING
2755 might_be_remote = this_ins && sig->hasthis &&
2756 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2757 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2759 if (might_be_remote && context_used) {
2762 g_assert (cfg->gshared);
2764 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2766 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2770 need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
2772 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2774 #ifndef DISABLE_REMOTING
2775 if (might_be_remote)
2776 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2779 call->method = method;
2780 call->inst.flags |= MONO_INST_HAS_METHOD;
2781 call->inst.inst_left = this_ins;
2782 call->tail_call = tail;
2785 int vtable_reg, slot_reg, this_reg;
2788 this_reg = this_ins->dreg;
2790 if ((method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2791 MonoInst *dummy_use;
2793 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2795 /* Make a call to delegate->invoke_impl */
2796 call->inst.inst_basereg = this_reg;
2797 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2798 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2800 /* We must emit a dummy use here because the delegate trampoline will
2801 replace the 'this' argument with the delegate target making this activation
2802 no longer a root for the delegate.
2803 This is an issue for delegates that target collectible code such as dynamic
2804 methods of GC'able assemblies.
2806 For a test case look into #667921.
2808 FIXME: a dummy use is not the best way to do it as the local register allocator
2809 will put it on a caller save register and spil it around the call.
2810 Ideally, we would either put it on a callee save register or only do the store part.
2812 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2814 return (MonoInst*)call;
2817 if ((!cfg->compile_aot || enable_for_aot) &&
2818 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2819 (MONO_METHOD_IS_FINAL (method) &&
2820 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2821 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2823 * the method is not virtual, we just need to ensure this is not null
2824 * and then we can call the method directly.
2826 #ifndef DISABLE_REMOTING
2827 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2829 * The check above ensures method is not gshared, this is needed since
2830 * gshared methods can't have wrappers.
2832 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2836 if (!method->string_ctor)
2837 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2839 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2840 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2842 * the method is virtual, but we can statically dispatch since either
2843 * it's class or the method itself are sealed.
2844 * But first we need to ensure it's not a null reference.
2846 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2848 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2850 vtable_reg = alloc_preg (cfg);
2851 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2852 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2853 guint32 imt_slot = mono_method_get_imt_slot (method);
2854 emit_imt_argument (cfg, call, call->method, imt_arg);
2855 slot_reg = vtable_reg;
2856 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2858 slot_reg = vtable_reg;
2859 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2860 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2862 g_assert (mono_method_signature (method)->generic_param_count);
2863 emit_imt_argument (cfg, call, call->method, imt_arg);
2867 call->inst.sreg1 = slot_reg;
2868 call->inst.inst_offset = offset;
2869 call->virtual = TRUE;
2873 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2876 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2878 return (MonoInst*)call;
2882 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2884 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2888 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2895 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2898 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2900 return (MonoInst*)call;
2904 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2906 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2910 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2914 * mono_emit_abs_call:
2916 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2918 inline static MonoInst*
2919 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2920 MonoMethodSignature *sig, MonoInst **args)
2922 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2926 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2929 if (cfg->abs_patches == NULL)
2930 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2931 g_hash_table_insert (cfg->abs_patches, ji, ji);
2932 ins = mono_emit_native_call (cfg, ji, sig, args);
2933 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2938 direct_icalls_enabled (MonoCompile *cfg)
2940 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2942 if (cfg->compile_llvm)
2945 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2951 mono_emit_jit_icall_by_info (MonoCompile *cfg, MonoJitICallInfo *info, MonoInst **args)
2954 * Call the jit icall without a wrapper if possible.
2955 * The wrapper is needed for the following reasons:
2956 * - to handle exceptions thrown using mono_raise_exceptions () from the
2957 * icall function. The EH code needs the lmf frame pushed by the
2958 * wrapper to be able to unwind back to managed code.
2959 * - to be able to do stack walks for asynchronously suspended
2960 * threads when debugging.
2962 if (info->no_raise && direct_icalls_enabled (cfg)) {
2966 if (!info->wrapper_method) {
2967 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2968 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2970 mono_memory_barrier ();
2974 * Inline the wrapper method, which is basically a call to the C icall, and
2975 * an exception check.
2977 costs = inline_method (cfg, info->wrapper_method, NULL,
2978 args, NULL, cfg->real_offset, TRUE);
2979 g_assert (costs > 0);
2980 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2984 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2989 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2991 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2992 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2996 * Native code might return non register sized integers
2997 * without initializing the upper bits.
2999 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3000 case OP_LOADI1_MEMBASE:
3001 widen_op = OP_ICONV_TO_I1;
3003 case OP_LOADU1_MEMBASE:
3004 widen_op = OP_ICONV_TO_U1;
3006 case OP_LOADI2_MEMBASE:
3007 widen_op = OP_ICONV_TO_I2;
3009 case OP_LOADU2_MEMBASE:
3010 widen_op = OP_ICONV_TO_U2;
3016 if (widen_op != -1) {
3017 int dreg = alloc_preg (cfg);
3020 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3021 widen->type = ins->type;
3031 get_memcpy_method (void)
3033 static MonoMethod *memcpy_method = NULL;
3034 if (!memcpy_method) {
3035 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3037 g_error ("Old corlib found. Install a new one");
3039 return memcpy_method;
3043 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3045 MonoClassField *field;
3046 gpointer iter = NULL;
3048 while ((field = mono_class_get_fields (klass, &iter))) {
3051 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3053 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3054 if (mini_type_is_reference (mono_field_get_type (field))) {
3055 g_assert ((foffset % SIZEOF_VOID_P) == 0);
3056 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3058 MonoClass *field_class = mono_class_from_mono_type (field->type);
3059 if (field_class->has_references)
3060 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3066 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3068 int card_table_shift_bits;
3069 gpointer card_table_mask;
3071 MonoInst *dummy_use;
3072 int nursery_shift_bits;
3073 size_t nursery_size;
3074 gboolean has_card_table_wb = FALSE;
3076 if (!cfg->gen_write_barriers)
3079 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3081 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3083 #ifdef MONO_ARCH_HAVE_CARD_TABLE_WBARRIER
3084 has_card_table_wb = TRUE;
3087 if (has_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3090 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3091 wbarrier->sreg1 = ptr->dreg;
3092 wbarrier->sreg2 = value->dreg;
3093 MONO_ADD_INS (cfg->cbb, wbarrier);
3094 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3095 int offset_reg = alloc_preg (cfg);
3096 int card_reg = alloc_preg (cfg);
3099 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3100 if (card_table_mask)
3101 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3103 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3104 * IMM's larger than 32bits.
3106 if (cfg->compile_aot) {
3107 MONO_EMIT_NEW_AOTCONST (cfg, card_reg, NULL, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR);
3109 MONO_INST_NEW (cfg, ins, OP_PCONST);
3110 ins->inst_p0 = card_table;
3111 ins->dreg = card_reg;
3112 MONO_ADD_INS (cfg->cbb, ins);
3115 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3116 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3118 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3119 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3122 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3126 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3128 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3129 unsigned need_wb = 0;
3134 /*types with references can't have alignment smaller than sizeof(void*) */
3135 if (align < SIZEOF_VOID_P)
3138 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3139 if (size > 32 * SIZEOF_VOID_P)
3142 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3144 /* We don't unroll more than 5 stores to avoid code bloat. */
3145 if (size > 5 * SIZEOF_VOID_P) {
3146 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3147 size += (SIZEOF_VOID_P - 1);
3148 size &= ~(SIZEOF_VOID_P - 1);
3150 EMIT_NEW_ICONST (cfg, iargs [2], size);
3151 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3152 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3156 destreg = iargs [0]->dreg;
3157 srcreg = iargs [1]->dreg;
3160 dest_ptr_reg = alloc_preg (cfg);
3161 tmp_reg = alloc_preg (cfg);
3164 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3166 while (size >= SIZEOF_VOID_P) {
3167 MonoInst *load_inst;
3168 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3169 load_inst->dreg = tmp_reg;
3170 load_inst->inst_basereg = srcreg;
3171 load_inst->inst_offset = offset;
3172 MONO_ADD_INS (cfg->cbb, load_inst);
3174 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3177 emit_write_barrier (cfg, iargs [0], load_inst);
3179 offset += SIZEOF_VOID_P;
3180 size -= SIZEOF_VOID_P;
3183 /*tmp += sizeof (void*)*/
3184 if (size >= SIZEOF_VOID_P) {
3185 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3186 MONO_ADD_INS (cfg->cbb, iargs [0]);
3190 /* Those cannot be references since size < sizeof (void*) */
3192 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3193 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3199 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3200 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3206 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3207 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3216 * Emit code to copy a valuetype of type @klass whose address is stored in
3217 * @src->dreg to memory whose address is stored at @dest->dreg.
3220 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3222 MonoInst *iargs [4];
3225 MonoMethod *memcpy_method;
3226 MonoInst *size_ins = NULL;
3227 MonoInst *memcpy_ins = NULL;
3231 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3234 * This check breaks with spilled vars... need to handle it during verification anyway.
3235 * g_assert (klass && klass == src->klass && klass == dest->klass);
3238 if (mini_is_gsharedvt_klass (klass)) {
3240 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3241 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3245 n = mono_class_native_size (klass, &align);
3247 n = mono_class_value_size (klass, &align);
3249 /* if native is true there should be no references in the struct */
3250 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3251 /* Avoid barriers when storing to the stack */
3252 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3253 (dest->opcode == OP_LDADDR))) {
3259 context_used = mini_class_check_context_used (cfg, klass);
3261 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3262 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3264 } else if (context_used) {
3265 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3267 if (cfg->compile_aot) {
3268 EMIT_NEW_CLASSCONST (cfg, iargs [2], klass);
3270 EMIT_NEW_PCONST (cfg, iargs [2], klass);
3271 mono_class_compute_gc_descriptor (klass);
3276 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3278 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3283 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3284 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3285 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3290 iargs [2] = size_ins;
3292 EMIT_NEW_ICONST (cfg, iargs [2], n);
3294 memcpy_method = get_memcpy_method ();
3296 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3298 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3303 get_memset_method (void)
3305 static MonoMethod *memset_method = NULL;
3306 if (!memset_method) {
3307 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3309 g_error ("Old corlib found. Install a new one");
3311 return memset_method;
3315 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3317 MonoInst *iargs [3];
3320 MonoMethod *memset_method;
3321 MonoInst *size_ins = NULL;
3322 MonoInst *bzero_ins = NULL;
3323 static MonoMethod *bzero_method;
3325 /* FIXME: Optimize this for the case when dest is an LDADDR */
3326 mono_class_init (klass);
3327 if (mini_is_gsharedvt_klass (klass)) {
3328 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3329 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3331 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3332 g_assert (bzero_method);
3334 iargs [1] = size_ins;
3335 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3339 n = mono_class_value_size (klass, &align);
3341 if (n <= sizeof (gpointer) * 8) {
3342 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3345 memset_method = get_memset_method ();
3347 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3348 EMIT_NEW_ICONST (cfg, iargs [2], n);
3349 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3356 * Emit IR to return either the this pointer for instance method,
3357 * or the mrgctx for static methods.
3360 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3362 MonoInst *this_ins = NULL;
3364 g_assert (cfg->gshared);
3366 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3367 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3368 !method->klass->valuetype)
3369 EMIT_NEW_ARGLOAD (cfg, this_ins, 0);
3371 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3372 MonoInst *mrgctx_loc, *mrgctx_var;
3374 g_assert (!this_ins);
3375 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3377 mrgctx_loc = mono_get_vtable_var (cfg);
3378 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3381 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3382 MonoInst *vtable_loc, *vtable_var;
3384 g_assert (!this_ins);
3386 vtable_loc = mono_get_vtable_var (cfg);
3387 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3389 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3390 MonoInst *mrgctx_var = vtable_var;
3393 vtable_reg = alloc_preg (cfg);
3394 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3395 vtable_var->type = STACK_PTR;
3403 vtable_reg = alloc_preg (cfg);
3404 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3409 static MonoJumpInfoRgctxEntry *
3410 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3412 MonoJumpInfoRgctxEntry *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3413 res->method = method;
3414 res->in_mrgctx = in_mrgctx;
3415 res->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3416 res->data->type = patch_type;
3417 res->data->data.target = patch_data;
3418 res->info_type = info_type;
3426 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3429 static inline MonoInst*
3430 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3432 /* Inline version, not currently used */
3433 // FIXME: This can be called from mono_decompose_vtype_opts (), which can't create new bblocks
3435 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3437 MonoBasicBlock *is_null_bb, *end_bb;
3438 MonoInst *res, *ins, *call;
3441 slot = mini_get_rgctx_entry_slot (entry);
3443 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3444 index = MONO_RGCTX_SLOT_INDEX (slot);
3446 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3447 for (depth = 0; ; ++depth) {
3448 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3450 if (index < size - 1)
3455 NEW_BBLOCK (cfg, end_bb);
3456 NEW_BBLOCK (cfg, is_null_bb);
3459 rgctx_reg = rgctx->dreg;
3461 rgctx_reg = alloc_preg (cfg);
3463 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3464 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3465 NEW_BBLOCK (cfg, is_null_bb);
3467 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3468 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3471 for (i = 0; i < depth; ++i) {
3472 int array_reg = alloc_preg (cfg);
3474 /* load ptr to next array */
3475 if (mrgctx && i == 0)
3476 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3478 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3479 rgctx_reg = array_reg;
3480 /* is the ptr null? */
3481 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3482 /* if yes, jump to actual trampoline */
3483 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3487 val_reg = alloc_preg (cfg);
3488 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3489 /* is the slot null? */
3490 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3491 /* if yes, jump to actual trampoline */
3492 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3495 res_reg = alloc_preg (cfg);
3496 MONO_INST_NEW (cfg, ins, OP_MOVE);
3497 ins->dreg = res_reg;
3498 ins->sreg1 = val_reg;
3499 MONO_ADD_INS (cfg->cbb, ins);
3501 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3504 MONO_START_BB (cfg, is_null_bb);
3506 EMIT_NEW_ICONST (cfg, args [1], index);
3508 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3510 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3511 MONO_INST_NEW (cfg, ins, OP_MOVE);
3512 ins->dreg = res_reg;
3513 ins->sreg1 = call->dreg;
3514 MONO_ADD_INS (cfg->cbb, ins);
3515 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3517 MONO_START_BB (cfg, end_bb);
3521 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3526 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3527 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3529 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);
3530 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3532 return emit_rgctx_fetch (cfg, rgctx, entry);
3536 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3537 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3539 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);
3540 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3542 return emit_rgctx_fetch (cfg, rgctx, entry);
3546 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3547 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3549 MonoJumpInfoGSharedVtCall *call_info;
3550 MonoJumpInfoRgctxEntry *entry;
3553 call_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3554 call_info->sig = sig;
3555 call_info->method = cmethod;
3557 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);
3558 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3560 return emit_rgctx_fetch (cfg, rgctx, entry);
3564 * emit_get_rgctx_virt_method:
3566 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3569 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3570 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3572 MonoJumpInfoVirtMethod *info;
3573 MonoJumpInfoRgctxEntry *entry;
3576 info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3577 info->klass = klass;
3578 info->method = virt_method;
3580 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);
3581 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3583 return emit_rgctx_fetch (cfg, rgctx, entry);
3587 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3588 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3590 MonoJumpInfoRgctxEntry *entry;
3593 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);
3594 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3596 return emit_rgctx_fetch (cfg, rgctx, entry);
3600 * emit_get_rgctx_method:
3602 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3603 * normal constants, else emit a load from the rgctx.
3606 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3607 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3609 if (!context_used) {
3612 switch (rgctx_type) {
3613 case MONO_RGCTX_INFO_METHOD:
3614 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3616 case MONO_RGCTX_INFO_METHOD_RGCTX:
3617 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3620 g_assert_not_reached ();
3623 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);
3624 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3626 return emit_rgctx_fetch (cfg, rgctx, entry);
3631 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3632 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3634 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);
3635 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3637 return emit_rgctx_fetch (cfg, rgctx, entry);
3641 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3643 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3644 MonoRuntimeGenericContextInfoTemplate *template;
3649 for (i = 0; i < info->num_entries; ++i) {
3650 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3652 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3656 if (info->num_entries == info->count_entries) {
3657 MonoRuntimeGenericContextInfoTemplate *new_entries;
3658 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3660 new_entries = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3662 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3663 info->entries = new_entries;
3664 info->count_entries = new_count_entries;
3667 idx = info->num_entries;
3668 template = &info->entries [idx];
3669 template->info_type = rgctx_type;
3670 template->data = data;
3672 info->num_entries ++;
3678 * emit_get_gsharedvt_info:
3680 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3683 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3688 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3689 /* Load info->entries [idx] */
3690 dreg = alloc_preg (cfg);
3691 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3697 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3699 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3703 * On return the caller must check @klass for load errors.
3706 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3708 MonoInst *vtable_arg;
3710 gboolean use_op_generic_class_init = FALSE;
3712 context_used = mini_class_check_context_used (cfg, klass);
3715 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3716 klass, MONO_RGCTX_INFO_VTABLE);
3718 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3722 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3725 #ifdef MONO_ARCH_HAVE_OP_GENERIC_CLASS_INIT
3726 if (!COMPILE_LLVM (cfg))
3727 use_op_generic_class_init = TRUE;
3730 if (use_op_generic_class_init) {
3734 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3735 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3737 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3738 ins->sreg1 = vtable_arg->dreg;
3739 MONO_ADD_INS (cfg->cbb, ins);
3741 static int byte_offset = -1;
3742 static guint8 bitmask;
3743 int bits_reg, inited_reg;
3744 MonoBasicBlock *inited_bb;
3745 MonoInst *args [16];
3747 if (byte_offset < 0)
3748 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
3750 bits_reg = alloc_ireg (cfg);
3751 inited_reg = alloc_ireg (cfg);
3753 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, bits_reg, vtable_arg->dreg, byte_offset);
3754 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, inited_reg, bits_reg, bitmask);
3756 NEW_BBLOCK (cfg, inited_bb);
3758 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3759 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3761 args [0] = vtable_arg;
3762 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3764 MONO_START_BB (cfg, inited_bb);
3769 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3773 if (cfg->gen_seq_points && cfg->method == method) {
3774 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3776 ins->flags |= MONO_INST_NONEMPTY_STACK;
3777 MONO_ADD_INS (cfg->cbb, ins);
3782 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3784 if (mini_get_debug_options ()->better_cast_details) {
3785 int vtable_reg = alloc_preg (cfg);
3786 int klass_reg = alloc_preg (cfg);
3787 MonoBasicBlock *is_null_bb = NULL;
3789 int to_klass_reg, context_used;
3792 NEW_BBLOCK (cfg, is_null_bb);
3794 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3795 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3798 tls_get = mono_get_jit_tls_intrinsic (cfg);
3800 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3804 MONO_ADD_INS (cfg->cbb, tls_get);
3805 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3806 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3808 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3810 context_used = mini_class_check_context_used (cfg, klass);
3812 MonoInst *class_ins;
3814 class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3815 to_klass_reg = class_ins->dreg;
3817 to_klass_reg = alloc_preg (cfg);
3818 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3820 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3823 MONO_START_BB (cfg, is_null_bb);
3828 reset_cast_details (MonoCompile *cfg)
3830 /* Reset the variables holding the cast details */
3831 if (mini_get_debug_options ()->better_cast_details) {
3832 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3834 MONO_ADD_INS (cfg->cbb, tls_get);
3835 /* It is enough to reset the from field */
3836 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3841 * On return the caller must check @array_class for load errors
3844 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3846 int vtable_reg = alloc_preg (cfg);
3849 context_used = mini_class_check_context_used (cfg, array_class);
3851 save_cast_details (cfg, array_class, obj->dreg, FALSE);
3853 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3855 if (cfg->opt & MONO_OPT_SHARED) {
3856 int class_reg = alloc_preg (cfg);
3857 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3858 if (cfg->compile_aot) {
3859 int klass_reg = alloc_preg (cfg);
3860 MONO_EMIT_NEW_CLASSCONST (cfg, klass_reg, array_class);
3861 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, klass_reg);
3863 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, class_reg, array_class);
3865 } else if (context_used) {
3866 MonoInst *vtable_ins;
3868 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3869 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3871 if (cfg->compile_aot) {
3875 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3877 vt_reg = alloc_preg (cfg);
3878 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3879 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3882 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3884 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3888 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3890 reset_cast_details (cfg);
3894 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
3895 * generic code is generated.
3898 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3900 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3903 MonoInst *rgctx, *addr;
3905 /* FIXME: What if the class is shared? We might not
3906 have to get the address of the method from the
3908 addr = emit_get_rgctx_method (cfg, context_used, method,
3909 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3911 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3913 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3915 gboolean pass_vtable, pass_mrgctx;
3916 MonoInst *rgctx_arg = NULL;
3918 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3919 g_assert (!pass_mrgctx);
3922 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3925 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3928 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3933 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3937 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3938 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3939 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3940 int rank_reg = alloc_dreg (cfg ,STACK_I4);
3942 obj_reg = sp [0]->dreg;
3943 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3944 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3946 /* FIXME: generics */
3947 g_assert (klass->rank == 0);
3950 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3951 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3953 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3954 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3957 MonoInst *element_class;
3959 /* This assertion is from the unboxcast insn */
3960 g_assert (klass->rank == 0);
3962 element_class = emit_get_rgctx_klass (cfg, context_used,
3963 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3965 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3966 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3968 save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3969 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3970 reset_cast_details (cfg);
3973 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3974 MONO_ADD_INS (cfg->cbb, add);
3975 add->type = STACK_MP;
3982 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3984 MonoInst *addr, *klass_inst, *is_ref, *args[16];
3985 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3989 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3995 args [1] = klass_inst;
3998 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
4000 NEW_BBLOCK (cfg, is_ref_bb);
4001 NEW_BBLOCK (cfg, is_nullable_bb);
4002 NEW_BBLOCK (cfg, end_bb);
4003 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4004 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 1);
4005 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4007 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 2);
4008 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4010 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
4011 addr_reg = alloc_dreg (cfg, STACK_MP);
4015 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
4016 MONO_ADD_INS (cfg->cbb, addr);
4018 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4021 MONO_START_BB (cfg, is_ref_bb);
4023 /* Save the ref to a temporary */
4024 dreg = alloc_ireg (cfg);
4025 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
4026 addr->dreg = addr_reg;
4027 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
4028 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4031 MONO_START_BB (cfg, is_nullable_bb);
4034 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
4035 MonoInst *unbox_call;
4036 MonoMethodSignature *unbox_sig;
4038 unbox_sig = mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4039 unbox_sig->ret = &klass->byval_arg;
4040 unbox_sig->param_count = 1;
4041 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
4042 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
4044 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
4045 addr->dreg = addr_reg;
4048 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4051 MONO_START_BB (cfg, end_bb);
4054 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
4060 * Returns NULL and set the cfg exception on error.
4063 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
4065 MonoInst *iargs [2];
4071 MonoInst *iargs [2];
4072 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4074 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4076 if (cfg->opt & MONO_OPT_SHARED)
4077 rgctx_info = MONO_RGCTX_INFO_KLASS;
4079 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4080 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4082 if (cfg->opt & MONO_OPT_SHARED) {
4083 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4085 alloc_ftn = mono_object_new;
4088 alloc_ftn = mono_object_new_specific;
4091 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4092 if (known_instance_size) {
4093 int size = mono_class_instance_size (klass);
4094 if (size < sizeof (MonoObject))
4095 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4097 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4099 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4102 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4105 if (cfg->opt & MONO_OPT_SHARED) {
4106 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4107 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4109 alloc_ftn = mono_object_new;
4110 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !klass->generic_class) {
4111 /* This happens often in argument checking code, eg. throw new FooException... */
4112 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4113 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4114 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4116 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4117 MonoMethod *managed_alloc = NULL;
4121 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4122 cfg->exception_ptr = klass;
4126 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4128 if (managed_alloc) {
4129 int size = mono_class_instance_size (klass);
4130 if (size < sizeof (MonoObject))
4131 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4133 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4134 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4135 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4137 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4139 guint32 lw = vtable->klass->instance_size;
4140 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4141 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4142 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4145 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4149 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4153 * Returns NULL and set the cfg exception on error.
4156 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4158 MonoInst *alloc, *ins;
4160 if (mono_class_is_nullable (klass)) {
4161 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4164 /* FIXME: What if the class is shared? We might not
4165 have to get the method address from the RGCTX. */
4166 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4167 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4168 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4170 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4172 gboolean pass_vtable, pass_mrgctx;
4173 MonoInst *rgctx_arg = NULL;
4175 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4176 g_assert (!pass_mrgctx);
4179 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4182 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4185 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4189 if (mini_is_gsharedvt_klass (klass)) {
4190 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4191 MonoInst *res, *is_ref, *src_var, *addr;
4194 dreg = alloc_ireg (cfg);
4196 NEW_BBLOCK (cfg, is_ref_bb);
4197 NEW_BBLOCK (cfg, is_nullable_bb);
4198 NEW_BBLOCK (cfg, end_bb);
4199 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4200 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 1);
4201 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4203 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 2);
4204 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4207 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4210 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4211 ins->opcode = OP_STOREV_MEMBASE;
4213 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4214 res->type = STACK_OBJ;
4216 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4219 MONO_START_BB (cfg, is_ref_bb);
4221 /* val is a vtype, so has to load the value manually */
4222 src_var = get_vreg_to_inst (cfg, val->dreg);
4224 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4225 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4226 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4227 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4230 MONO_START_BB (cfg, is_nullable_bb);
4233 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4234 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4236 MonoMethodSignature *box_sig;
4239 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4240 * construct that method at JIT time, so have to do things by hand.
4242 box_sig = mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4243 box_sig->ret = &mono_defaults.object_class->byval_arg;
4244 box_sig->param_count = 1;
4245 box_sig->params [0] = &klass->byval_arg;
4246 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4247 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4248 res->type = STACK_OBJ;
4252 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4254 MONO_START_BB (cfg, end_bb);
4258 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4262 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4268 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4271 MonoGenericContainer *container;
4272 MonoGenericInst *ginst;
4274 if (klass->generic_class) {
4275 container = klass->generic_class->container_class->generic_container;
4276 ginst = klass->generic_class->context.class_inst;
4277 } else if (klass->generic_container && context_used) {
4278 container = klass->generic_container;
4279 ginst = container->context.class_inst;
4284 for (i = 0; i < container->type_argc; ++i) {
4286 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4288 type = ginst->type_argv [i];
4289 if (mini_type_is_reference (type))
4295 static GHashTable* direct_icall_type_hash;
4298 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4300 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4301 if (!direct_icalls_enabled (cfg))
4305 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4306 * Whitelist a few icalls for now.
4308 if (!direct_icall_type_hash) {
4309 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4311 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4312 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4313 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4314 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4315 mono_memory_barrier ();
4316 direct_icall_type_hash = h;
4319 if (cmethod->klass == mono_defaults.math_class)
4321 /* No locking needed */
4322 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4327 #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)
4330 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4332 MonoMethod *mono_castclass;
4335 mono_castclass = mono_marshal_get_castclass_with_cache ();
4337 save_cast_details (cfg, klass, args [0]->dreg, TRUE);
4338 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4339 reset_cast_details (cfg);
4345 get_castclass_cache_idx (MonoCompile *cfg)
4347 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4348 cfg->castclass_cache_index ++;
4349 return (cfg->method_index << 16) | cfg->castclass_cache_index;
4353 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4362 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4365 if (cfg->compile_aot) {
4366 idx = get_castclass_cache_idx (cfg);
4367 EMIT_NEW_AOTCONST (cfg, args [2], MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4369 EMIT_NEW_PCONST (cfg, args [2], mono_domain_alloc0 (cfg->domain, sizeof (gpointer)));
4372 /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4373 return emit_castclass_with_cache (cfg, klass, args);
4377 * Returns NULL and set the cfg exception on error.
4380 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, guint8 *ip, int *inline_costs)
4382 MonoBasicBlock *is_null_bb;
4383 int obj_reg = src->dreg;
4384 int vtable_reg = alloc_preg (cfg);
4386 MonoInst *klass_inst = NULL, *res;
4388 context_used = mini_class_check_context_used (cfg, klass);
4390 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
4391 res = emit_castclass_with_cache_nonshared (cfg, src, klass);
4392 (*inline_costs) += 2;
4394 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
4395 MonoMethod *mono_castclass;
4396 MonoInst *iargs [1];
4399 mono_castclass = mono_marshal_get_castclass (klass);
4402 save_cast_details (cfg, klass, src->dreg, TRUE);
4403 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
4404 iargs, ip, cfg->real_offset, TRUE);
4405 reset_cast_details (cfg);
4406 CHECK_CFG_EXCEPTION;
4407 g_assert (costs > 0);
4409 cfg->real_offset += 5;
4411 (*inline_costs) += costs;
4419 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4420 MonoInst *cache_ins;
4422 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4427 /* klass - it's the second element of the cache entry*/
4428 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4431 args [2] = cache_ins;
4433 return emit_castclass_with_cache (cfg, klass, args);
4436 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4439 NEW_BBLOCK (cfg, is_null_bb);
4441 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4442 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4444 save_cast_details (cfg, klass, obj_reg, FALSE);
4446 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4447 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4448 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4450 int klass_reg = alloc_preg (cfg);
4452 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4454 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4455 /* the remoting code is broken, access the class for now */
4456 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4457 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4459 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4460 cfg->exception_ptr = klass;
4463 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4465 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4466 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4468 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4470 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4471 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4475 MONO_START_BB (cfg, is_null_bb);
4477 reset_cast_details (cfg);
4486 * Returns NULL and set the cfg exception on error.
4489 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4492 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4493 int obj_reg = src->dreg;
4494 int vtable_reg = alloc_preg (cfg);
4495 int res_reg = alloc_ireg_ref (cfg);
4496 MonoInst *klass_inst = NULL;
4501 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4502 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4503 MonoInst *cache_ins;
4505 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4510 /* klass - it's the second element of the cache entry*/
4511 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4514 args [2] = cache_ins;
4516 return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4519 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4522 NEW_BBLOCK (cfg, is_null_bb);
4523 NEW_BBLOCK (cfg, false_bb);
4524 NEW_BBLOCK (cfg, end_bb);
4526 /* Do the assignment at the beginning, so the other assignment can be if converted */
4527 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4528 ins->type = STACK_OBJ;
4531 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4532 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4534 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4536 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4537 g_assert (!context_used);
4538 /* the is_null_bb target simply copies the input register to the output */
4539 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4541 int klass_reg = alloc_preg (cfg);
4544 int rank_reg = alloc_preg (cfg);
4545 int eclass_reg = alloc_preg (cfg);
4547 g_assert (!context_used);
4548 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4549 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4550 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4551 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4552 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4553 if (klass->cast_class == mono_defaults.object_class) {
4554 int parent_reg = alloc_preg (cfg);
4555 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4556 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4557 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4558 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4559 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4560 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4561 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4562 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4563 } else if (klass->cast_class == mono_defaults.enum_class) {
4564 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4565 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4566 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4567 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4569 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4570 /* Check that the object is a vector too */
4571 int bounds_reg = alloc_preg (cfg);
4572 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4573 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4574 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4577 /* the is_null_bb target simply copies the input register to the output */
4578 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4580 } else if (mono_class_is_nullable (klass)) {
4581 g_assert (!context_used);
4582 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4583 /* the is_null_bb target simply copies the input register to the output */
4584 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4586 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4587 g_assert (!context_used);
4588 /* the remoting code is broken, access the class for now */
4589 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4590 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4592 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4593 cfg->exception_ptr = klass;
4596 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4598 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4599 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4601 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4602 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4604 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4605 /* the is_null_bb target simply copies the input register to the output */
4606 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4611 MONO_START_BB (cfg, false_bb);
4613 MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4614 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4616 MONO_START_BB (cfg, is_null_bb);
4618 MONO_START_BB (cfg, end_bb);
4624 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4626 /* This opcode takes as input an object reference and a class, and returns:
4627 0) if the object is an instance of the class,
4628 1) if the object is not instance of the class,
4629 2) if the object is a proxy whose type cannot be determined */
4632 #ifndef DISABLE_REMOTING
4633 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4635 MonoBasicBlock *true_bb, *false_bb, *end_bb;
4637 int obj_reg = src->dreg;
4638 int dreg = alloc_ireg (cfg);
4640 #ifndef DISABLE_REMOTING
4641 int klass_reg = alloc_preg (cfg);
4644 NEW_BBLOCK (cfg, true_bb);
4645 NEW_BBLOCK (cfg, false_bb);
4646 NEW_BBLOCK (cfg, end_bb);
4647 #ifndef DISABLE_REMOTING
4648 NEW_BBLOCK (cfg, false2_bb);
4649 NEW_BBLOCK (cfg, no_proxy_bb);
4652 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4653 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4655 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4656 #ifndef DISABLE_REMOTING
4657 NEW_BBLOCK (cfg, interface_fail_bb);
4660 tmp_reg = alloc_preg (cfg);
4661 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4662 #ifndef DISABLE_REMOTING
4663 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4664 MONO_START_BB (cfg, interface_fail_bb);
4665 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4667 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4669 tmp_reg = alloc_preg (cfg);
4670 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4671 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4672 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
4674 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4677 #ifndef DISABLE_REMOTING
4678 tmp_reg = alloc_preg (cfg);
4679 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4680 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4682 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4683 tmp_reg = alloc_preg (cfg);
4684 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4685 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4687 tmp_reg = alloc_preg (cfg);
4688 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4689 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4690 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4692 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4693 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4695 MONO_START_BB (cfg, no_proxy_bb);
4697 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4699 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4703 MONO_START_BB (cfg, false_bb);
4705 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4706 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4708 #ifndef DISABLE_REMOTING
4709 MONO_START_BB (cfg, false2_bb);
4711 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4712 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4715 MONO_START_BB (cfg, true_bb);
4717 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4719 MONO_START_BB (cfg, end_bb);
4722 MONO_INST_NEW (cfg, ins, OP_ICONST);
4724 ins->type = STACK_I4;
4730 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4732 /* This opcode takes as input an object reference and a class, and returns:
4733 0) if the object is an instance of the class,
4734 1) if the object is a proxy whose type cannot be determined
4735 an InvalidCastException exception is thrown otherwhise*/
4738 #ifndef DISABLE_REMOTING
4739 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4741 MonoBasicBlock *ok_result_bb;
4743 int obj_reg = src->dreg;
4744 int dreg = alloc_ireg (cfg);
4745 int tmp_reg = alloc_preg (cfg);
4747 #ifndef DISABLE_REMOTING
4748 int klass_reg = alloc_preg (cfg);
4749 NEW_BBLOCK (cfg, end_bb);
4752 NEW_BBLOCK (cfg, ok_result_bb);
4754 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4755 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4757 save_cast_details (cfg, klass, obj_reg, FALSE);
4759 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4760 #ifndef DISABLE_REMOTING
4761 NEW_BBLOCK (cfg, interface_fail_bb);
4763 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4764 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4765 MONO_START_BB (cfg, interface_fail_bb);
4766 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4768 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4770 tmp_reg = alloc_preg (cfg);
4771 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4772 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4773 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4775 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4776 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4778 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4779 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
4780 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4783 #ifndef DISABLE_REMOTING
4784 NEW_BBLOCK (cfg, no_proxy_bb);
4786 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4787 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4788 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4790 tmp_reg = alloc_preg (cfg);
4791 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4792 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4794 tmp_reg = alloc_preg (cfg);
4795 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4796 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4797 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4799 NEW_BBLOCK (cfg, fail_1_bb);
4801 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4803 MONO_START_BB (cfg, fail_1_bb);
4805 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4806 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4808 MONO_START_BB (cfg, no_proxy_bb);
4810 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4812 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4816 MONO_START_BB (cfg, ok_result_bb);
4818 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4820 #ifndef DISABLE_REMOTING
4821 MONO_START_BB (cfg, end_bb);
4825 MONO_INST_NEW (cfg, ins, OP_ICONST);
4827 ins->type = STACK_I4;
4832 static G_GNUC_UNUSED MonoInst*
4833 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4835 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4836 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4839 switch (enum_type->type) {
4842 #if SIZEOF_REGISTER == 8
4854 MonoInst *load, *and, *cmp, *ceq;
4855 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4856 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4857 int dest_reg = alloc_ireg (cfg);
4859 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4860 EMIT_NEW_BIALU (cfg, and, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4861 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4862 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4864 ceq->type = STACK_I4;
4867 load = mono_decompose_opcode (cfg, load);
4868 and = mono_decompose_opcode (cfg, and);
4869 cmp = mono_decompose_opcode (cfg, cmp);
4870 ceq = mono_decompose_opcode (cfg, ceq);
4878 * Returns NULL and set the cfg exception on error.
4880 static G_GNUC_UNUSED MonoInst*
4881 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual)
4885 gpointer trampoline;
4886 MonoInst *obj, *method_ins, *tramp_ins;
4891 MonoMethod *invoke = mono_get_delegate_invoke (klass);
4894 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
4898 obj = handle_alloc (cfg, klass, FALSE, 0);
4902 /* Inline the contents of mono_delegate_ctor */
4904 /* Set target field */
4905 /* Optimize away setting of NULL target */
4906 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
4907 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4908 if (cfg->gen_write_barriers) {
4909 dreg = alloc_preg (cfg);
4910 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4911 emit_write_barrier (cfg, ptr, target);
4915 /* Set method field */
4916 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4917 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4920 * To avoid looking up the compiled code belonging to the target method
4921 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4922 * store it, and we fill it after the method has been compiled.
4924 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4925 MonoInst *code_slot_ins;
4928 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4930 domain = mono_domain_get ();
4931 mono_domain_lock (domain);
4932 if (!domain_jit_info (domain)->method_code_hash)
4933 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4934 code_slot = g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4936 code_slot = mono_domain_alloc0 (domain, sizeof (gpointer));
4937 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4939 mono_domain_unlock (domain);
4941 if (cfg->compile_aot)
4942 EMIT_NEW_AOTCONST (cfg, code_slot_ins, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4944 EMIT_NEW_PCONST (cfg, code_slot_ins, code_slot);
4946 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
4949 if (cfg->compile_aot) {
4950 MonoDelegateClassMethodPair *del_tramp;
4952 del_tramp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4953 del_tramp->klass = klass;
4954 del_tramp->method = context_used ? NULL : method;
4955 del_tramp->virtual = virtual;
4956 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4959 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4961 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4962 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4965 /* Set invoke_impl field */
4967 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4969 dreg = alloc_preg (cfg);
4970 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4971 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4973 dreg = alloc_preg (cfg);
4974 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4975 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4978 dreg = alloc_preg (cfg);
4979 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual ? 1 : 0);
4980 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4982 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4988 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4990 MonoJitICallInfo *info;
4992 /* Need to register the icall so it gets an icall wrapper */
4993 info = mono_get_array_new_va_icall (rank);
4995 cfg->flags |= MONO_CFG_HAS_VARARGS;
4997 /* mono_array_new_va () needs a vararg calling convention */
4998 cfg->disable_llvm = TRUE;
5000 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
5001 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
5005 * handle_constrained_gsharedvt_call:
5007 * Handle constrained calls where the receiver is a gsharedvt type.
5008 * Return the instruction representing the call. Set the cfg exception on failure.
5011 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
5012 gboolean *ref_emit_widen)
5014 MonoInst *ins = NULL;
5015 gboolean emit_widen = *ref_emit_widen;
5018 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
5019 * 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
5020 * pack the arguments into an array, and do the rest of the work in in an icall.
5022 if (((cmethod->klass == mono_defaults.object_class) || (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
5023 (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)) &&
5024 (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]))))) {
5025 MonoInst *args [16];
5028 * This case handles calls to
5029 * - object:ToString()/Equals()/GetHashCode(),
5030 * - System.IComparable<T>:CompareTo()
5031 * - System.IEquatable<T>:Equals ()
5032 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
5036 if (mono_method_check_context_used (cmethod))
5037 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
5039 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
5040 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
5042 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
5043 if (fsig->hasthis && fsig->param_count) {
5044 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
5045 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
5046 ins->dreg = alloc_preg (cfg);
5047 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
5048 MONO_ADD_INS (cfg->cbb, ins);
5051 if (mini_is_gsharedvt_type (fsig->params [0])) {
5054 args [3] = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
5056 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
5057 addr_reg = ins->dreg;
5058 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
5060 EMIT_NEW_ICONST (cfg, args [3], 0);
5061 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
5064 EMIT_NEW_ICONST (cfg, args [3], 0);
5065 EMIT_NEW_ICONST (cfg, args [4], 0);
5067 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
5070 if (mini_is_gsharedvt_type (fsig->ret)) {
5071 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
5072 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
5076 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
5077 MONO_ADD_INS (cfg->cbb, add);
5079 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
5080 MONO_ADD_INS (cfg->cbb, ins);
5081 /* ins represents the call result */
5084 GSHAREDVT_FAILURE (CEE_CALLVIRT);
5087 *ref_emit_widen = emit_widen;
5096 mono_emit_load_got_addr (MonoCompile *cfg)
5098 MonoInst *getaddr, *dummy_use;
5100 if (!cfg->got_var || cfg->got_var_allocated)
5103 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
5104 getaddr->cil_code = cfg->header->code;
5105 getaddr->dreg = cfg->got_var->dreg;
5107 /* Add it to the start of the first bblock */
5108 if (cfg->bb_entry->code) {
5109 getaddr->next = cfg->bb_entry->code;
5110 cfg->bb_entry->code = getaddr;
5113 MONO_ADD_INS (cfg->bb_entry, getaddr);
5115 cfg->got_var_allocated = TRUE;
5118 * Add a dummy use to keep the got_var alive, since real uses might
5119 * only be generated by the back ends.
5120 * Add it to end_bblock, so the variable's lifetime covers the whole
5122 * It would be better to make the usage of the got var explicit in all
5123 * cases when the backend needs it (i.e. calls, throw etc.), so this
5124 * wouldn't be needed.
5126 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5127 MONO_ADD_INS (cfg->bb_exit, dummy_use);
5130 static int inline_limit;
5131 static gboolean inline_limit_inited;
5134 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5136 MonoMethodHeaderSummary header;
5138 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5139 MonoMethodSignature *sig = mono_method_signature (method);
5143 if (cfg->disable_inline)
5148 if (cfg->inline_depth > 10)
5151 #ifdef MONO_ARCH_HAVE_LMF_OPS
5152 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5153 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
5154 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
5159 if (!mono_method_get_header_summary (method, &header))
5162 /*runtime, icall and pinvoke are checked by summary call*/
5163 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5164 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5165 (mono_class_is_marshalbyref (method->klass)) ||
5169 /* also consider num_locals? */
5170 /* Do the size check early to avoid creating vtables */
5171 if (!inline_limit_inited) {
5172 if (g_getenv ("MONO_INLINELIMIT"))
5173 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5175 inline_limit = INLINE_LENGTH_LIMIT;
5176 inline_limit_inited = TRUE;
5178 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5182 * if we can initialize the class of the method right away, we do,
5183 * otherwise we don't allow inlining if the class needs initialization,
5184 * since it would mean inserting a call to mono_runtime_class_init()
5185 * inside the inlined code
5187 if (!(cfg->opt & MONO_OPT_SHARED)) {
5188 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5189 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5190 vtable = mono_class_vtable (cfg->domain, method->klass);
5193 if (!cfg->compile_aot)
5194 mono_runtime_class_init (vtable);
5195 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5196 if (cfg->run_cctors && method->klass->has_cctor) {
5197 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5198 if (!method->klass->runtime_info)
5199 /* No vtable created yet */
5201 vtable = mono_class_vtable (cfg->domain, method->klass);
5204 /* This makes so that inline cannot trigger */
5205 /* .cctors: too many apps depend on them */
5206 /* running with a specific order... */
5207 if (! vtable->initialized)
5209 mono_runtime_class_init (vtable);
5211 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5212 if (!method->klass->runtime_info)
5213 /* No vtable created yet */
5215 vtable = mono_class_vtable (cfg->domain, method->klass);
5218 if (!vtable->initialized)
5223 * If we're compiling for shared code
5224 * the cctor will need to be run at aot method load time, for example,
5225 * or at the end of the compilation of the inlining method.
5227 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5231 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5232 if (mono_arch_is_soft_float ()) {
5234 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5236 for (i = 0; i < sig->param_count; ++i)
5237 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5242 if (g_list_find (cfg->dont_inline, method))
5249 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5251 if (!cfg->compile_aot) {
5253 if (vtable->initialized)
5257 if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5258 if (cfg->method == method)
5262 if (!mono_class_needs_cctor_run (klass, method))
5265 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5266 /* The initialization is already done before the method is called */
5273 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5277 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5280 if (mini_is_gsharedvt_variable_klass (klass)) {
5283 mono_class_init (klass);
5284 size = mono_class_array_element_size (klass);
5287 mult_reg = alloc_preg (cfg);
5288 array_reg = arr->dreg;
5289 index_reg = index->dreg;
5291 #if SIZEOF_REGISTER == 8
5292 /* The array reg is 64 bits but the index reg is only 32 */
5293 if (COMPILE_LLVM (cfg)) {
5295 index2_reg = index_reg;
5297 index2_reg = alloc_preg (cfg);
5298 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5301 if (index->type == STACK_I8) {
5302 index2_reg = alloc_preg (cfg);
5303 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5305 index2_reg = index_reg;
5310 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5312 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5313 if (size == 1 || size == 2 || size == 4 || size == 8) {
5314 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5316 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5317 ins->klass = mono_class_get_element_class (klass);
5318 ins->type = STACK_MP;
5324 add_reg = alloc_ireg_mp (cfg);
5327 MonoInst *rgctx_ins;
5330 g_assert (cfg->gshared);
5331 context_used = mini_class_check_context_used (cfg, klass);
5332 g_assert (context_used);
5333 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5334 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5336 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5338 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5339 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5340 ins->klass = mono_class_get_element_class (klass);
5341 ins->type = STACK_MP;
5342 MONO_ADD_INS (cfg->cbb, ins);
5347 #ifndef MONO_ARCH_EMULATE_MUL_DIV
5349 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5351 int bounds_reg = alloc_preg (cfg);
5352 int add_reg = alloc_ireg_mp (cfg);
5353 int mult_reg = alloc_preg (cfg);
5354 int mult2_reg = alloc_preg (cfg);
5355 int low1_reg = alloc_preg (cfg);
5356 int low2_reg = alloc_preg (cfg);
5357 int high1_reg = alloc_preg (cfg);
5358 int high2_reg = alloc_preg (cfg);
5359 int realidx1_reg = alloc_preg (cfg);
5360 int realidx2_reg = alloc_preg (cfg);
5361 int sum_reg = alloc_preg (cfg);
5362 int index1, index2, tmpreg;
5366 mono_class_init (klass);
5367 size = mono_class_array_element_size (klass);
5369 index1 = index_ins1->dreg;
5370 index2 = index_ins2->dreg;
5372 #if SIZEOF_REGISTER == 8
5373 /* The array reg is 64 bits but the index reg is only 32 */
5374 if (COMPILE_LLVM (cfg)) {
5377 tmpreg = alloc_preg (cfg);
5378 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5380 tmpreg = alloc_preg (cfg);
5381 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5385 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5389 /* range checking */
5390 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
5391 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5393 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
5394 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5395 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5396 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
5397 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5398 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5399 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5401 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
5402 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5403 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5404 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
5405 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5406 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5407 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5409 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5410 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5411 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5412 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5413 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5415 ins->type = STACK_MP;
5417 MONO_ADD_INS (cfg->cbb, ins);
5424 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5428 MonoMethod *addr_method;
5430 MonoClass *eclass = cmethod->klass->element_class;
5432 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5435 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5437 #ifndef MONO_ARCH_EMULATE_MUL_DIV
5438 /* emit_ldelema_2 depends on OP_LMUL */
5439 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5440 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5444 if (mini_is_gsharedvt_variable_klass (eclass))
5447 element_size = mono_class_array_element_size (eclass);
5448 addr_method = mono_marshal_get_array_address (rank, element_size);
5449 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5454 static MonoBreakPolicy
5455 always_insert_breakpoint (MonoMethod *method)
5457 return MONO_BREAK_POLICY_ALWAYS;
5460 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5463 * mono_set_break_policy:
5464 * policy_callback: the new callback function
5466 * Allow embedders to decide wherther to actually obey breakpoint instructions
5467 * (both break IL instructions and Debugger.Break () method calls), for example
5468 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5469 * untrusted or semi-trusted code.
5471 * @policy_callback will be called every time a break point instruction needs to
5472 * be inserted with the method argument being the method that calls Debugger.Break()
5473 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5474 * if it wants the breakpoint to not be effective in the given method.
5475 * #MONO_BREAK_POLICY_ALWAYS is the default.
5478 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5480 if (policy_callback)
5481 break_policy_func = policy_callback;
5483 break_policy_func = always_insert_breakpoint;
5487 should_insert_brekpoint (MonoMethod *method) {
5488 switch (break_policy_func (method)) {
5489 case MONO_BREAK_POLICY_ALWAYS:
5491 case MONO_BREAK_POLICY_NEVER:
5493 case MONO_BREAK_POLICY_ON_DBG:
5494 g_warning ("mdb no longer supported");
5497 g_warning ("Incorrect value returned from break policy callback");
5502 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5504 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5506 MonoInst *addr, *store, *load;
5507 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5509 /* the bounds check is already done by the callers */
5510 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5512 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5513 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5514 if (mini_type_is_reference (fsig->params [2]))
5515 emit_write_barrier (cfg, addr, load);
5517 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5518 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5525 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5527 return mini_type_is_reference (&klass->byval_arg);
5531 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5533 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5534 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5535 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5536 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5537 MonoInst *iargs [3];
5540 mono_class_setup_vtable (obj_array);
5541 g_assert (helper->slot);
5543 if (sp [0]->type != STACK_OBJ)
5545 if (sp [2]->type != STACK_OBJ)
5552 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5556 if (mini_is_gsharedvt_variable_klass (klass)) {
5559 // FIXME-VT: OP_ICONST optimization
5560 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5561 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5562 ins->opcode = OP_STOREV_MEMBASE;
5563 } else if (sp [1]->opcode == OP_ICONST) {
5564 int array_reg = sp [0]->dreg;
5565 int index_reg = sp [1]->dreg;
5566 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5569 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5570 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5572 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5573 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5574 if (generic_class_is_reference_type (cfg, klass))
5575 emit_write_barrier (cfg, addr, sp [2]);
5582 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5587 eklass = mono_class_from_mono_type (fsig->params [2]);
5589 eklass = mono_class_from_mono_type (fsig->ret);
5592 return emit_array_store (cfg, eklass, args, FALSE);
5594 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5595 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5601 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5605 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
5607 //Only allow for valuetypes
5608 if (!param_klass->valuetype || !return_klass->valuetype)
5612 if (param_klass->has_references || return_klass->has_references)
5615 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5616 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5617 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)))
5620 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5621 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8)
5624 //And have the same size
5625 if (mono_class_value_size (param_klass, &align) != mono_class_value_size (return_klass, &align))
5631 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5633 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5634 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5636 //Valuetypes that are semantically equivalent
5637 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5640 //Arrays of valuetypes that are semantically equivalent
5641 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5648 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5650 #ifdef MONO_ARCH_SIMD_INTRINSICS
5651 MonoInst *ins = NULL;
5653 if (cfg->opt & MONO_OPT_SIMD) {
5654 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5660 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5664 emit_memory_barrier (MonoCompile *cfg, int kind)
5666 MonoInst *ins = NULL;
5667 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5668 MONO_ADD_INS (cfg->cbb, ins);
5669 ins->backend.memory_barrier_kind = kind;
5675 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5677 MonoInst *ins = NULL;
5680 /* The LLVM backend supports these intrinsics */
5681 if (cmethod->klass == mono_defaults.math_class) {
5682 if (strcmp (cmethod->name, "Sin") == 0) {
5684 } else if (strcmp (cmethod->name, "Cos") == 0) {
5686 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5688 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5692 if (opcode && fsig->param_count == 1) {
5693 MONO_INST_NEW (cfg, ins, opcode);
5694 ins->type = STACK_R8;
5695 ins->dreg = mono_alloc_freg (cfg);
5696 ins->sreg1 = args [0]->dreg;
5697 MONO_ADD_INS (cfg->cbb, ins);
5701 if (cfg->opt & MONO_OPT_CMOV) {
5702 if (strcmp (cmethod->name, "Min") == 0) {
5703 if (fsig->params [0]->type == MONO_TYPE_I4)
5705 if (fsig->params [0]->type == MONO_TYPE_U4)
5706 opcode = OP_IMIN_UN;
5707 else if (fsig->params [0]->type == MONO_TYPE_I8)
5709 else if (fsig->params [0]->type == MONO_TYPE_U8)
5710 opcode = OP_LMIN_UN;
5711 } else if (strcmp (cmethod->name, "Max") == 0) {
5712 if (fsig->params [0]->type == MONO_TYPE_I4)
5714 if (fsig->params [0]->type == MONO_TYPE_U4)
5715 opcode = OP_IMAX_UN;
5716 else if (fsig->params [0]->type == MONO_TYPE_I8)
5718 else if (fsig->params [0]->type == MONO_TYPE_U8)
5719 opcode = OP_LMAX_UN;
5723 if (opcode && fsig->param_count == 2) {
5724 MONO_INST_NEW (cfg, ins, opcode);
5725 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5726 ins->dreg = mono_alloc_ireg (cfg);
5727 ins->sreg1 = args [0]->dreg;
5728 ins->sreg2 = args [1]->dreg;
5729 MONO_ADD_INS (cfg->cbb, ins);
5737 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5739 if (cmethod->klass == mono_defaults.array_class) {
5740 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5741 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5742 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5743 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5744 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5745 return emit_array_unsafe_mov (cfg, fsig, args);
5752 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5754 MonoInst *ins = NULL;
5756 static MonoClass *runtime_helpers_class = NULL;
5757 if (! runtime_helpers_class)
5758 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
5759 "System.Runtime.CompilerServices", "RuntimeHelpers");
5761 if (cmethod->klass == mono_defaults.string_class) {
5762 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5763 int dreg = alloc_ireg (cfg);
5764 int index_reg = alloc_preg (cfg);
5765 int add_reg = alloc_preg (cfg);
5767 #if SIZEOF_REGISTER == 8
5768 /* The array reg is 64 bits but the index reg is only 32 */
5769 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5771 index_reg = args [1]->dreg;
5773 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5775 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5776 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5777 add_reg = ins->dreg;
5778 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5781 int mult_reg = alloc_preg (cfg);
5782 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5783 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5784 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5785 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5787 type_from_op (cfg, ins, NULL, NULL);
5789 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5790 int dreg = alloc_ireg (cfg);
5791 /* Decompose later to allow more optimizations */
5792 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5793 ins->type = STACK_I4;
5794 ins->flags |= MONO_INST_FAULT;
5795 cfg->cbb->has_array_access = TRUE;
5796 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5801 } else if (cmethod->klass == mono_defaults.object_class) {
5803 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
5804 int dreg = alloc_ireg_ref (cfg);
5805 int vt_reg = alloc_preg (cfg);
5806 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5807 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5808 type_from_op (cfg, ins, NULL, NULL);
5811 #if !defined(MONO_ARCH_EMULATE_MUL_DIV)
5812 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
5813 int dreg = alloc_ireg (cfg);
5814 int t1 = alloc_ireg (cfg);
5816 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
5817 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
5818 ins->type = STACK_I4;
5822 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
5823 MONO_INST_NEW (cfg, ins, OP_NOP);
5824 MONO_ADD_INS (cfg->cbb, ins);
5828 } else if (cmethod->klass == mono_defaults.array_class) {
5829 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5830 return emit_array_generic_access (cfg, fsig, args, FALSE);
5831 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5832 return emit_array_generic_access (cfg, fsig, args, TRUE);
5834 #ifndef MONO_BIG_ARRAYS
5836 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
5839 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
5840 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
5841 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
5842 int dreg = alloc_ireg (cfg);
5843 int bounds_reg = alloc_ireg_mp (cfg);
5844 MonoBasicBlock *end_bb, *szarray_bb;
5845 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
5847 NEW_BBLOCK (cfg, end_bb);
5848 NEW_BBLOCK (cfg, szarray_bb);
5850 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
5851 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5852 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
5853 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
5854 /* Non-szarray case */
5856 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5857 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5859 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5860 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5861 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
5862 MONO_START_BB (cfg, szarray_bb);
5865 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5866 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5868 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5869 MONO_START_BB (cfg, end_bb);
5871 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
5872 ins->type = STACK_I4;
5878 if (cmethod->name [0] != 'g')
5881 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
5882 int dreg = alloc_ireg (cfg);
5883 int vtable_reg = alloc_preg (cfg);
5884 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
5885 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5886 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5887 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5888 type_from_op (cfg, ins, NULL, NULL);
5891 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5892 int dreg = alloc_ireg (cfg);
5894 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5895 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5896 type_from_op (cfg, ins, NULL, NULL);
5901 } else if (cmethod->klass == runtime_helpers_class) {
5903 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5904 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5908 } else if (cmethod->klass == mono_defaults.thread_class) {
5909 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5910 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5911 MONO_ADD_INS (cfg->cbb, ins);
5913 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5914 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5915 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5917 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5919 if (fsig->params [0]->type == MONO_TYPE_I1)
5920 opcode = OP_LOADI1_MEMBASE;
5921 else if (fsig->params [0]->type == MONO_TYPE_U1)
5922 opcode = OP_LOADU1_MEMBASE;
5923 else if (fsig->params [0]->type == MONO_TYPE_I2)
5924 opcode = OP_LOADI2_MEMBASE;
5925 else if (fsig->params [0]->type == MONO_TYPE_U2)
5926 opcode = OP_LOADU2_MEMBASE;
5927 else if (fsig->params [0]->type == MONO_TYPE_I4)
5928 opcode = OP_LOADI4_MEMBASE;
5929 else if (fsig->params [0]->type == MONO_TYPE_U4)
5930 opcode = OP_LOADU4_MEMBASE;
5931 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5932 opcode = OP_LOADI8_MEMBASE;
5933 else if (fsig->params [0]->type == MONO_TYPE_R4)
5934 opcode = OP_LOADR4_MEMBASE;
5935 else if (fsig->params [0]->type == MONO_TYPE_R8)
5936 opcode = OP_LOADR8_MEMBASE;
5937 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5938 opcode = OP_LOAD_MEMBASE;
5941 MONO_INST_NEW (cfg, ins, opcode);
5942 ins->inst_basereg = args [0]->dreg;
5943 ins->inst_offset = 0;
5944 MONO_ADD_INS (cfg->cbb, ins);
5946 switch (fsig->params [0]->type) {
5953 ins->dreg = mono_alloc_ireg (cfg);
5954 ins->type = STACK_I4;
5958 ins->dreg = mono_alloc_lreg (cfg);
5959 ins->type = STACK_I8;
5963 ins->dreg = mono_alloc_ireg (cfg);
5964 #if SIZEOF_REGISTER == 8
5965 ins->type = STACK_I8;
5967 ins->type = STACK_I4;
5972 ins->dreg = mono_alloc_freg (cfg);
5973 ins->type = STACK_R8;
5976 g_assert (mini_type_is_reference (fsig->params [0]));
5977 ins->dreg = mono_alloc_ireg_ref (cfg);
5978 ins->type = STACK_OBJ;
5982 if (opcode == OP_LOADI8_MEMBASE)
5983 ins = mono_decompose_opcode (cfg, ins);
5985 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
5989 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5991 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5993 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5994 opcode = OP_STOREI1_MEMBASE_REG;
5995 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5996 opcode = OP_STOREI2_MEMBASE_REG;
5997 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5998 opcode = OP_STOREI4_MEMBASE_REG;
5999 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6000 opcode = OP_STOREI8_MEMBASE_REG;
6001 else if (fsig->params [0]->type == MONO_TYPE_R4)
6002 opcode = OP_STORER4_MEMBASE_REG;
6003 else if (fsig->params [0]->type == MONO_TYPE_R8)
6004 opcode = OP_STORER8_MEMBASE_REG;
6005 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6006 opcode = OP_STORE_MEMBASE_REG;
6009 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
6011 MONO_INST_NEW (cfg, ins, opcode);
6012 ins->sreg1 = args [1]->dreg;
6013 ins->inst_destbasereg = args [0]->dreg;
6014 ins->inst_offset = 0;
6015 MONO_ADD_INS (cfg->cbb, ins);
6017 if (opcode == OP_STOREI8_MEMBASE_REG)
6018 ins = mono_decompose_opcode (cfg, ins);
6023 } else if (cmethod->klass->image == mono_defaults.corlib &&
6024 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6025 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6028 #if SIZEOF_REGISTER == 8
6029 if (strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6030 if (mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6031 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6032 ins->dreg = mono_alloc_preg (cfg);
6033 ins->sreg1 = args [0]->dreg;
6034 ins->type = STACK_I8;
6035 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6036 MONO_ADD_INS (cfg->cbb, ins);
6040 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6042 /* 64 bit reads are already atomic */
6043 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6044 load_ins->dreg = mono_alloc_preg (cfg);
6045 load_ins->inst_basereg = args [0]->dreg;
6046 load_ins->inst_offset = 0;
6047 load_ins->type = STACK_I8;
6048 MONO_ADD_INS (cfg->cbb, load_ins);
6050 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6057 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6058 MonoInst *ins_iconst;
6061 if (fsig->params [0]->type == MONO_TYPE_I4) {
6062 opcode = OP_ATOMIC_ADD_I4;
6063 cfg->has_atomic_add_i4 = TRUE;
6065 #if SIZEOF_REGISTER == 8
6066 else if (fsig->params [0]->type == MONO_TYPE_I8)
6067 opcode = OP_ATOMIC_ADD_I8;
6070 if (!mono_arch_opcode_supported (opcode))
6072 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6073 ins_iconst->inst_c0 = 1;
6074 ins_iconst->dreg = mono_alloc_ireg (cfg);
6075 MONO_ADD_INS (cfg->cbb, ins_iconst);
6077 MONO_INST_NEW (cfg, ins, opcode);
6078 ins->dreg = mono_alloc_ireg (cfg);
6079 ins->inst_basereg = args [0]->dreg;
6080 ins->inst_offset = 0;
6081 ins->sreg2 = ins_iconst->dreg;
6082 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6083 MONO_ADD_INS (cfg->cbb, ins);
6085 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6086 MonoInst *ins_iconst;
6089 if (fsig->params [0]->type == MONO_TYPE_I4) {
6090 opcode = OP_ATOMIC_ADD_I4;
6091 cfg->has_atomic_add_i4 = TRUE;
6093 #if SIZEOF_REGISTER == 8
6094 else if (fsig->params [0]->type == MONO_TYPE_I8)
6095 opcode = OP_ATOMIC_ADD_I8;
6098 if (!mono_arch_opcode_supported (opcode))
6100 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6101 ins_iconst->inst_c0 = -1;
6102 ins_iconst->dreg = mono_alloc_ireg (cfg);
6103 MONO_ADD_INS (cfg->cbb, ins_iconst);
6105 MONO_INST_NEW (cfg, ins, opcode);
6106 ins->dreg = mono_alloc_ireg (cfg);
6107 ins->inst_basereg = args [0]->dreg;
6108 ins->inst_offset = 0;
6109 ins->sreg2 = ins_iconst->dreg;
6110 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6111 MONO_ADD_INS (cfg->cbb, ins);
6113 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6116 if (fsig->params [0]->type == MONO_TYPE_I4) {
6117 opcode = OP_ATOMIC_ADD_I4;
6118 cfg->has_atomic_add_i4 = TRUE;
6120 #if SIZEOF_REGISTER == 8
6121 else if (fsig->params [0]->type == MONO_TYPE_I8)
6122 opcode = OP_ATOMIC_ADD_I8;
6125 if (!mono_arch_opcode_supported (opcode))
6127 MONO_INST_NEW (cfg, ins, opcode);
6128 ins->dreg = mono_alloc_ireg (cfg);
6129 ins->inst_basereg = args [0]->dreg;
6130 ins->inst_offset = 0;
6131 ins->sreg2 = args [1]->dreg;
6132 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6133 MONO_ADD_INS (cfg->cbb, ins);
6136 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6137 MonoInst *f2i = NULL, *i2f;
6138 guint32 opcode, f2i_opcode, i2f_opcode;
6139 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6140 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6142 if (fsig->params [0]->type == MONO_TYPE_I4 ||
6143 fsig->params [0]->type == MONO_TYPE_R4) {
6144 opcode = OP_ATOMIC_EXCHANGE_I4;
6145 f2i_opcode = OP_MOVE_F_TO_I4;
6146 i2f_opcode = OP_MOVE_I4_TO_F;
6147 cfg->has_atomic_exchange_i4 = TRUE;
6149 #if SIZEOF_REGISTER == 8
6151 fsig->params [0]->type == MONO_TYPE_I8 ||
6152 fsig->params [0]->type == MONO_TYPE_R8 ||
6153 fsig->params [0]->type == MONO_TYPE_I) {
6154 opcode = OP_ATOMIC_EXCHANGE_I8;
6155 f2i_opcode = OP_MOVE_F_TO_I8;
6156 i2f_opcode = OP_MOVE_I8_TO_F;
6159 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6160 opcode = OP_ATOMIC_EXCHANGE_I4;
6161 cfg->has_atomic_exchange_i4 = TRUE;
6167 if (!mono_arch_opcode_supported (opcode))
6171 /* TODO: Decompose these opcodes instead of bailing here. */
6172 if (COMPILE_SOFT_FLOAT (cfg))
6175 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6176 f2i->dreg = mono_alloc_ireg (cfg);
6177 f2i->sreg1 = args [1]->dreg;
6178 if (f2i_opcode == OP_MOVE_F_TO_I4)
6179 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6180 MONO_ADD_INS (cfg->cbb, f2i);
6183 MONO_INST_NEW (cfg, ins, opcode);
6184 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6185 ins->inst_basereg = args [0]->dreg;
6186 ins->inst_offset = 0;
6187 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6188 MONO_ADD_INS (cfg->cbb, ins);
6190 switch (fsig->params [0]->type) {
6192 ins->type = STACK_I4;
6195 ins->type = STACK_I8;
6198 #if SIZEOF_REGISTER == 8
6199 ins->type = STACK_I8;
6201 ins->type = STACK_I4;
6206 ins->type = STACK_R8;
6209 g_assert (mini_type_is_reference (fsig->params [0]));
6210 ins->type = STACK_OBJ;
6215 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6216 i2f->dreg = mono_alloc_freg (cfg);
6217 i2f->sreg1 = ins->dreg;
6218 i2f->type = STACK_R8;
6219 if (i2f_opcode == OP_MOVE_I4_TO_F)
6220 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6221 MONO_ADD_INS (cfg->cbb, i2f);
6226 if (cfg->gen_write_barriers && is_ref)
6227 emit_write_barrier (cfg, args [0], args [1]);
6229 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6230 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6231 guint32 opcode, f2i_opcode, i2f_opcode;
6232 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6233 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6235 if (fsig->params [1]->type == MONO_TYPE_I4 ||
6236 fsig->params [1]->type == MONO_TYPE_R4) {
6237 opcode = OP_ATOMIC_CAS_I4;
6238 f2i_opcode = OP_MOVE_F_TO_I4;
6239 i2f_opcode = OP_MOVE_I4_TO_F;
6240 cfg->has_atomic_cas_i4 = TRUE;
6242 #if SIZEOF_REGISTER == 8
6244 fsig->params [1]->type == MONO_TYPE_I8 ||
6245 fsig->params [1]->type == MONO_TYPE_R8 ||
6246 fsig->params [1]->type == MONO_TYPE_I) {
6247 opcode = OP_ATOMIC_CAS_I8;
6248 f2i_opcode = OP_MOVE_F_TO_I8;
6249 i2f_opcode = OP_MOVE_I8_TO_F;
6252 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6253 opcode = OP_ATOMIC_CAS_I4;
6254 cfg->has_atomic_cas_i4 = TRUE;
6260 if (!mono_arch_opcode_supported (opcode))
6264 /* TODO: Decompose these opcodes instead of bailing here. */
6265 if (COMPILE_SOFT_FLOAT (cfg))
6268 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6269 f2i_new->dreg = mono_alloc_ireg (cfg);
6270 f2i_new->sreg1 = args [1]->dreg;
6271 if (f2i_opcode == OP_MOVE_F_TO_I4)
6272 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6273 MONO_ADD_INS (cfg->cbb, f2i_new);
6275 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6276 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6277 f2i_cmp->sreg1 = args [2]->dreg;
6278 if (f2i_opcode == OP_MOVE_F_TO_I4)
6279 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6280 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6283 MONO_INST_NEW (cfg, ins, opcode);
6284 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6285 ins->sreg1 = args [0]->dreg;
6286 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6287 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6288 MONO_ADD_INS (cfg->cbb, ins);
6290 switch (fsig->params [1]->type) {
6292 ins->type = STACK_I4;
6295 ins->type = STACK_I8;
6298 #if SIZEOF_REGISTER == 8
6299 ins->type = STACK_I8;
6301 ins->type = STACK_I4;
6305 ins->type = cfg->r4_stack_type;
6308 ins->type = STACK_R8;
6311 g_assert (mini_type_is_reference (fsig->params [1]));
6312 ins->type = STACK_OBJ;
6317 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6318 i2f->dreg = mono_alloc_freg (cfg);
6319 i2f->sreg1 = ins->dreg;
6320 i2f->type = STACK_R8;
6321 if (i2f_opcode == OP_MOVE_I4_TO_F)
6322 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6323 MONO_ADD_INS (cfg->cbb, i2f);
6328 if (cfg->gen_write_barriers && is_ref)
6329 emit_write_barrier (cfg, args [0], args [1]);
6331 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6332 fsig->params [1]->type == MONO_TYPE_I4) {
6333 MonoInst *cmp, *ceq;
6335 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6338 /* int32 r = CAS (location, value, comparand); */
6339 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6340 ins->dreg = alloc_ireg (cfg);
6341 ins->sreg1 = args [0]->dreg;
6342 ins->sreg2 = args [1]->dreg;
6343 ins->sreg3 = args [2]->dreg;
6344 ins->type = STACK_I4;
6345 MONO_ADD_INS (cfg->cbb, ins);
6347 /* bool result = r == comparand; */
6348 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6349 cmp->sreg1 = ins->dreg;
6350 cmp->sreg2 = args [2]->dreg;
6351 cmp->type = STACK_I4;
6352 MONO_ADD_INS (cfg->cbb, cmp);
6354 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6355 ceq->dreg = alloc_ireg (cfg);
6356 ceq->type = STACK_I4;
6357 MONO_ADD_INS (cfg->cbb, ceq);
6359 /* *success = result; */
6360 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6362 cfg->has_atomic_cas_i4 = TRUE;
6364 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6365 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6369 } else if (cmethod->klass->image == mono_defaults.corlib &&
6370 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6371 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6374 if (!strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6376 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6377 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6379 if (fsig->params [0]->type == MONO_TYPE_I1)
6380 opcode = OP_ATOMIC_LOAD_I1;
6381 else if (fsig->params [0]->type == MONO_TYPE_U1 || fsig->params [0]->type == MONO_TYPE_BOOLEAN)
6382 opcode = OP_ATOMIC_LOAD_U1;
6383 else if (fsig->params [0]->type == MONO_TYPE_I2)
6384 opcode = OP_ATOMIC_LOAD_I2;
6385 else if (fsig->params [0]->type == MONO_TYPE_U2)
6386 opcode = OP_ATOMIC_LOAD_U2;
6387 else if (fsig->params [0]->type == MONO_TYPE_I4)
6388 opcode = OP_ATOMIC_LOAD_I4;
6389 else if (fsig->params [0]->type == MONO_TYPE_U4)
6390 opcode = OP_ATOMIC_LOAD_U4;
6391 else if (fsig->params [0]->type == MONO_TYPE_R4)
6392 opcode = OP_ATOMIC_LOAD_R4;
6393 else if (fsig->params [0]->type == MONO_TYPE_R8)
6394 opcode = OP_ATOMIC_LOAD_R8;
6395 #if SIZEOF_REGISTER == 8
6396 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_I)
6397 opcode = OP_ATOMIC_LOAD_I8;
6398 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U8 || fsig->params [0]->type == MONO_TYPE_U)
6399 opcode = OP_ATOMIC_LOAD_U8;
6401 else if (fsig->params [0]->type == MONO_TYPE_I)
6402 opcode = OP_ATOMIC_LOAD_I4;
6403 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U)
6404 opcode = OP_ATOMIC_LOAD_U4;
6408 if (!mono_arch_opcode_supported (opcode))
6411 MONO_INST_NEW (cfg, ins, opcode);
6412 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6413 ins->sreg1 = args [0]->dreg;
6414 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6415 MONO_ADD_INS (cfg->cbb, ins);
6417 switch (fsig->params [0]->type) {
6418 case MONO_TYPE_BOOLEAN:
6425 ins->type = STACK_I4;
6429 ins->type = STACK_I8;
6433 #if SIZEOF_REGISTER == 8
6434 ins->type = STACK_I8;
6436 ins->type = STACK_I4;
6440 ins->type = cfg->r4_stack_type;
6443 ins->type = STACK_R8;
6446 g_assert (mini_type_is_reference (fsig->params [0]));
6447 ins->type = STACK_OBJ;
6453 if (!strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6455 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6457 if (fsig->params [0]->type == MONO_TYPE_I1)
6458 opcode = OP_ATOMIC_STORE_I1;
6459 else if (fsig->params [0]->type == MONO_TYPE_U1 || fsig->params [0]->type == MONO_TYPE_BOOLEAN)
6460 opcode = OP_ATOMIC_STORE_U1;
6461 else if (fsig->params [0]->type == MONO_TYPE_I2)
6462 opcode = OP_ATOMIC_STORE_I2;
6463 else if (fsig->params [0]->type == MONO_TYPE_U2)
6464 opcode = OP_ATOMIC_STORE_U2;
6465 else if (fsig->params [0]->type == MONO_TYPE_I4)
6466 opcode = OP_ATOMIC_STORE_I4;
6467 else if (fsig->params [0]->type == MONO_TYPE_U4)
6468 opcode = OP_ATOMIC_STORE_U4;
6469 else if (fsig->params [0]->type == MONO_TYPE_R4)
6470 opcode = OP_ATOMIC_STORE_R4;
6471 else if (fsig->params [0]->type == MONO_TYPE_R8)
6472 opcode = OP_ATOMIC_STORE_R8;
6473 #if SIZEOF_REGISTER == 8
6474 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_I)
6475 opcode = OP_ATOMIC_STORE_I8;
6476 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U8 || fsig->params [0]->type == MONO_TYPE_U)
6477 opcode = OP_ATOMIC_STORE_U8;
6479 else if (fsig->params [0]->type == MONO_TYPE_I)
6480 opcode = OP_ATOMIC_STORE_I4;
6481 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U)
6482 opcode = OP_ATOMIC_STORE_U4;
6486 if (!mono_arch_opcode_supported (opcode))
6489 MONO_INST_NEW (cfg, ins, opcode);
6490 ins->dreg = args [0]->dreg;
6491 ins->sreg1 = args [1]->dreg;
6492 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6493 MONO_ADD_INS (cfg->cbb, ins);
6495 if (cfg->gen_write_barriers && is_ref)
6496 emit_write_barrier (cfg, args [0], args [1]);
6502 } else if (cmethod->klass->image == mono_defaults.corlib &&
6503 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6504 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6505 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6506 if (should_insert_brekpoint (cfg->method)) {
6507 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6509 MONO_INST_NEW (cfg, ins, OP_NOP);
6510 MONO_ADD_INS (cfg->cbb, ins);
6514 } else if (cmethod->klass->image == mono_defaults.corlib &&
6515 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6516 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6517 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6519 EMIT_NEW_ICONST (cfg, ins, 1);
6521 EMIT_NEW_ICONST (cfg, ins, 0);
6524 } else if (cmethod->klass == mono_defaults.math_class) {
6526 * There is general branchless code for Min/Max, but it does not work for
6528 * http://everything2.com/?node_id=1051618
6530 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6531 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6532 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6533 !strcmp (cmethod->klass->name, "Selector")) ||
6534 (!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") &&
6535 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6536 !strcmp (cmethod->klass->name, "Selector"))
6538 #ifdef MONO_ARCH_HAVE_OBJC_GET_SELECTOR
6539 if (!strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6540 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6543 MonoJumpInfoToken *ji;
6546 cfg->disable_llvm = TRUE;
6548 if (args [0]->opcode == OP_GOT_ENTRY) {
6549 pi = args [0]->inst_p1;
6550 g_assert (pi->opcode == OP_PATCH_INFO);
6551 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6554 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6555 ji = args [0]->inst_p0;
6558 NULLIFY_INS (args [0]);
6561 s = mono_ldstr (cfg->domain, ji->image, mono_metadata_token_index (ji->token));
6562 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6563 ins->dreg = mono_alloc_ireg (cfg);
6565 ins->inst_p0 = mono_string_to_utf8 (s);
6566 MONO_ADD_INS (cfg->cbb, ins);
6572 #ifdef MONO_ARCH_SIMD_INTRINSICS
6573 if (cfg->opt & MONO_OPT_SIMD) {
6574 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6580 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6584 if (COMPILE_LLVM (cfg)) {
6585 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6590 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6594 * This entry point could be used later for arbitrary method
6597 inline static MonoInst*
6598 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6599 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6601 if (method->klass == mono_defaults.string_class) {
6602 /* managed string allocation support */
6603 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6604 MonoInst *iargs [2];
6605 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6606 MonoMethod *managed_alloc = NULL;
6608 g_assert (vtable); /*Should not fail since it System.String*/
6609 #ifndef MONO_CROSS_COMPILE
6610 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6614 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6615 iargs [1] = args [0];
6616 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6623 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6625 MonoInst *store, *temp;
6628 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6629 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6632 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6633 * would be different than the MonoInst's used to represent arguments, and
6634 * the ldelema implementation can't deal with that.
6635 * Solution: When ldelema is used on an inline argument, create a var for
6636 * it, emit ldelema on that var, and emit the saving code below in
6637 * inline_method () if needed.
6639 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6640 cfg->args [i] = temp;
6641 /* This uses cfg->args [i] which is set by the preceeding line */
6642 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6643 store->cil_code = sp [0]->cil_code;
6648 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6649 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6651 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6653 check_inline_called_method_name_limit (MonoMethod *called_method)
6656 static const char *limit = NULL;
6658 if (limit == NULL) {
6659 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6661 if (limit_string != NULL)
6662 limit = limit_string;
6667 if (limit [0] != '\0') {
6668 char *called_method_name = mono_method_full_name (called_method, TRUE);
6670 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6671 g_free (called_method_name);
6673 //return (strncmp_result <= 0);
6674 return (strncmp_result == 0);
6681 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6683 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6686 static const char *limit = NULL;
6688 if (limit == NULL) {
6689 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6690 if (limit_string != NULL) {
6691 limit = limit_string;
6697 if (limit [0] != '\0') {
6698 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6700 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6701 g_free (caller_method_name);
6703 //return (strncmp_result <= 0);
6704 return (strncmp_result == 0);
6712 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6714 static double r8_0 = 0.0;
6715 static float r4_0 = 0.0;
6719 rtype = mini_get_underlying_type (rtype);
6723 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6724 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6725 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6726 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6727 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6728 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6729 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6730 ins->type = STACK_R4;
6731 ins->inst_p0 = (void*)&r4_0;
6733 MONO_ADD_INS (cfg->cbb, ins);
6734 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6735 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6736 ins->type = STACK_R8;
6737 ins->inst_p0 = (void*)&r8_0;
6739 MONO_ADD_INS (cfg->cbb, ins);
6740 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6741 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6742 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6743 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6744 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6746 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6751 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6755 rtype = mini_get_underlying_type (rtype);
6759 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6760 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6761 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6762 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6763 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6764 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6765 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6766 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6767 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6768 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6769 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6770 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6771 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6772 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6774 emit_init_rvar (cfg, dreg, rtype);
6778 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6780 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6782 MonoInst *var = cfg->locals [local];
6783 if (COMPILE_SOFT_FLOAT (cfg)) {
6785 int reg = alloc_dreg (cfg, var->type);
6786 emit_init_rvar (cfg, reg, type);
6787 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6790 emit_init_rvar (cfg, var->dreg, type);
6792 emit_dummy_init_rvar (cfg, var->dreg, type);
6799 * Return the cost of inlining CMETHOD.
6802 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6803 guchar *ip, guint real_offset, gboolean inline_always)
6805 MonoInst *ins, *rvar = NULL;
6806 MonoMethodHeader *cheader;
6807 MonoBasicBlock *ebblock, *sbblock;
6809 MonoMethod *prev_inlined_method;
6810 MonoInst **prev_locals, **prev_args;
6811 MonoType **prev_arg_types;
6812 guint prev_real_offset;
6813 GHashTable *prev_cbb_hash;
6814 MonoBasicBlock **prev_cil_offset_to_bb;
6815 MonoBasicBlock *prev_cbb;
6816 unsigned char* prev_cil_start;
6817 guint32 prev_cil_offset_to_bb_len;
6818 MonoMethod *prev_current_method;
6819 MonoGenericContext *prev_generic_context;
6820 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual = FALSE;
6822 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6824 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6825 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6828 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6829 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6834 fsig = mono_method_signature (cmethod);
6836 if (cfg->verbose_level > 2)
6837 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6839 if (!cmethod->inline_info) {
6840 cfg->stat_inlineable_methods++;
6841 cmethod->inline_info = 1;
6844 /* allocate local variables */
6845 cheader = mono_method_get_header (cmethod);
6847 if (cheader == NULL || mono_loader_get_last_error ()) {
6848 MonoLoaderError *error = mono_loader_get_last_error ();
6851 mono_metadata_free_mh (cheader);
6852 if (inline_always && error)
6853 mono_cfg_set_exception (cfg, error->exception_type);
6855 mono_loader_clear_error ();
6859 /*Must verify before creating locals as it can cause the JIT to assert.*/
6860 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6861 mono_metadata_free_mh (cheader);
6865 /* allocate space to store the return value */
6866 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6867 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6870 prev_locals = cfg->locals;
6871 cfg->locals = mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6872 for (i = 0; i < cheader->num_locals; ++i)
6873 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6875 /* allocate start and end blocks */
6876 /* This is needed so if the inline is aborted, we can clean up */
6877 NEW_BBLOCK (cfg, sbblock);
6878 sbblock->real_offset = real_offset;
6880 NEW_BBLOCK (cfg, ebblock);
6881 ebblock->block_num = cfg->num_bblocks++;
6882 ebblock->real_offset = real_offset;
6884 prev_args = cfg->args;
6885 prev_arg_types = cfg->arg_types;
6886 prev_inlined_method = cfg->inlined_method;
6887 cfg->inlined_method = cmethod;
6888 cfg->ret_var_set = FALSE;
6889 cfg->inline_depth ++;
6890 prev_real_offset = cfg->real_offset;
6891 prev_cbb_hash = cfg->cbb_hash;
6892 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6893 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6894 prev_cil_start = cfg->cil_start;
6895 prev_cbb = cfg->cbb;
6896 prev_current_method = cfg->current_method;
6897 prev_generic_context = cfg->generic_context;
6898 prev_ret_var_set = cfg->ret_var_set;
6899 prev_disable_inline = cfg->disable_inline;
6901 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6904 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual);
6906 ret_var_set = cfg->ret_var_set;
6908 cfg->inlined_method = prev_inlined_method;
6909 cfg->real_offset = prev_real_offset;
6910 cfg->cbb_hash = prev_cbb_hash;
6911 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6912 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6913 cfg->cil_start = prev_cil_start;
6914 cfg->locals = prev_locals;
6915 cfg->args = prev_args;
6916 cfg->arg_types = prev_arg_types;
6917 cfg->current_method = prev_current_method;
6918 cfg->generic_context = prev_generic_context;
6919 cfg->ret_var_set = prev_ret_var_set;
6920 cfg->disable_inline = prev_disable_inline;
6921 cfg->inline_depth --;
6923 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6924 if (cfg->verbose_level > 2)
6925 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6927 cfg->stat_inlined_methods++;
6929 /* always add some code to avoid block split failures */
6930 MONO_INST_NEW (cfg, ins, OP_NOP);
6931 MONO_ADD_INS (prev_cbb, ins);
6933 prev_cbb->next_bb = sbblock;
6934 link_bblock (cfg, prev_cbb, sbblock);
6937 * Get rid of the begin and end bblocks if possible to aid local
6940 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6942 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6943 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6945 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6946 MonoBasicBlock *prev = ebblock->in_bb [0];
6947 mono_merge_basic_blocks (cfg, prev, ebblock);
6949 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6950 mono_merge_basic_blocks (cfg, prev_cbb, prev);
6951 cfg->cbb = prev_cbb;
6955 * Its possible that the rvar is set in some prev bblock, but not in others.
6961 for (i = 0; i < ebblock->in_count; ++i) {
6962 bb = ebblock->in_bb [i];
6964 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6967 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6977 * If the inlined method contains only a throw, then the ret var is not
6978 * set, so set it to a dummy value.
6981 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6983 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6986 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6989 if (cfg->verbose_level > 2)
6990 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6991 cfg->exception_type = MONO_EXCEPTION_NONE;
6992 mono_loader_clear_error ();
6994 /* This gets rid of the newly added bblocks */
6995 cfg->cbb = prev_cbb;
6997 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7002 * Some of these comments may well be out-of-date.
7003 * Design decisions: we do a single pass over the IL code (and we do bblock
7004 * splitting/merging in the few cases when it's required: a back jump to an IL
7005 * address that was not already seen as bblock starting point).
7006 * Code is validated as we go (full verification is still better left to metadata/verify.c).
7007 * Complex operations are decomposed in simpler ones right away. We need to let the
7008 * arch-specific code peek and poke inside this process somehow (except when the
7009 * optimizations can take advantage of the full semantic info of coarse opcodes).
7010 * All the opcodes of the form opcode.s are 'normalized' to opcode.
7011 * MonoInst->opcode initially is the IL opcode or some simplification of that
7012 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
7013 * opcode with value bigger than OP_LAST.
7014 * At this point the IR can be handed over to an interpreter, a dumb code generator
7015 * or to the optimizing code generator that will translate it to SSA form.
7017 * Profiling directed optimizations.
7018 * We may compile by default with few or no optimizations and instrument the code
7019 * or the user may indicate what methods to optimize the most either in a config file
7020 * or through repeated runs where the compiler applies offline the optimizations to
7021 * each method and then decides if it was worth it.
7024 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7025 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7026 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7027 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7028 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7029 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7030 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7031 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) TYPE_LOAD_ERROR ((klass))
7033 /* offset from br.s -> br like opcodes */
7034 #define BIG_BRANCH_OFFSET 13
7037 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7039 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7041 return b == NULL || b == bb;
7045 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7047 unsigned char *ip = start;
7048 unsigned char *target;
7051 MonoBasicBlock *bblock;
7052 const MonoOpcode *opcode;
7055 cli_addr = ip - start;
7056 i = mono_opcode_value ((const guint8 **)&ip, end);
7059 opcode = &mono_opcodes [i];
7060 switch (opcode->argument) {
7061 case MonoInlineNone:
7064 case MonoInlineString:
7065 case MonoInlineType:
7066 case MonoInlineField:
7067 case MonoInlineMethod:
7070 case MonoShortInlineR:
7077 case MonoShortInlineVar:
7078 case MonoShortInlineI:
7081 case MonoShortInlineBrTarget:
7082 target = start + cli_addr + 2 + (signed char)ip [1];
7083 GET_BBLOCK (cfg, bblock, target);
7086 GET_BBLOCK (cfg, bblock, ip);
7088 case MonoInlineBrTarget:
7089 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7090 GET_BBLOCK (cfg, bblock, target);
7093 GET_BBLOCK (cfg, bblock, ip);
7095 case MonoInlineSwitch: {
7096 guint32 n = read32 (ip + 1);
7099 cli_addr += 5 + 4 * n;
7100 target = start + cli_addr;
7101 GET_BBLOCK (cfg, bblock, target);
7103 for (j = 0; j < n; ++j) {
7104 target = start + cli_addr + (gint32)read32 (ip);
7105 GET_BBLOCK (cfg, bblock, target);
7115 g_assert_not_reached ();
7118 if (i == CEE_THROW) {
7119 unsigned char *bb_start = ip - 1;
7121 /* Find the start of the bblock containing the throw */
7123 while ((bb_start >= start) && !bblock) {
7124 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7128 bblock->out_of_line = 1;
7138 static inline MonoMethod *
7139 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7143 if (m->wrapper_type != MONO_WRAPPER_NONE) {
7144 method = mono_method_get_wrapper_data (m, token);
7147 method = mono_class_inflate_generic_method_checked (method, context, &error);
7148 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
7151 method = mono_get_method_full (m->klass->image, token, klass, context);
7157 static inline MonoMethod *
7158 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7160 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
7162 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg))
7168 static inline MonoClass*
7169 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7174 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7175 klass = mono_method_get_wrapper_data (method, token);
7177 klass = mono_class_inflate_generic_class (klass, context);
7179 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7180 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7183 mono_class_init (klass);
7187 static inline MonoMethodSignature*
7188 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
7190 MonoMethodSignature *fsig;
7192 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7195 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7197 fsig = mono_inflate_generic_signature (fsig, context, &error);
7199 g_assert (mono_error_ok (&error));
7202 fsig = mono_metadata_parse_signature (method->klass->image, token);
7208 throw_exception (void)
7210 static MonoMethod *method = NULL;
7213 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7214 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7221 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7223 MonoMethod *thrower = throw_exception ();
7226 EMIT_NEW_PCONST (cfg, args [0], ex);
7227 mono_emit_method_call (cfg, thrower, args, NULL);
7231 * Return the original method is a wrapper is specified. We can only access
7232 * the custom attributes from the original method.
7235 get_original_method (MonoMethod *method)
7237 if (method->wrapper_type == MONO_WRAPPER_NONE)
7240 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7241 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7244 /* in other cases we need to find the original method */
7245 return mono_marshal_method_from_wrapper (method);
7249 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7251 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7252 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7254 emit_throw_exception (cfg, ex);
7258 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7260 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7261 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7263 emit_throw_exception (cfg, ex);
7267 * Check that the IL instructions at ip are the array initialization
7268 * sequence and return the pointer to the data and the size.
7271 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7274 * newarr[System.Int32]
7276 * ldtoken field valuetype ...
7277 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7279 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7281 guint32 token = read32 (ip + 7);
7282 guint32 field_token = read32 (ip + 2);
7283 guint32 field_index = field_token & 0xffffff;
7285 const char *data_ptr;
7287 MonoMethod *cmethod;
7288 MonoClass *dummy_class;
7289 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7293 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7297 *out_field_token = field_token;
7299 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7302 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7304 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7305 case MONO_TYPE_BOOLEAN:
7309 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7310 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7311 case MONO_TYPE_CHAR:
7328 if (size > mono_type_size (field->type, &dummy_align))
7331 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7332 if (!image_is_dynamic (method->klass->image)) {
7333 field_index = read32 (ip + 2) & 0xffffff;
7334 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7335 data_ptr = mono_image_rva_map (method->klass->image, rva);
7336 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7337 /* for aot code we do the lookup on load */
7338 if (aot && data_ptr)
7339 return GUINT_TO_POINTER (rva);
7341 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
7343 data_ptr = mono_field_get_data (field);
7351 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7353 char *method_fname = mono_method_full_name (method, TRUE);
7355 MonoMethodHeader *header = mono_method_get_header (method);
7357 if (header->code_size == 0)
7358 method_code = g_strdup ("method body is empty.");
7360 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7361 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
7362 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
7363 g_free (method_fname);
7364 g_free (method_code);
7365 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7369 set_exception_object (MonoCompile *cfg, MonoException *exception)
7371 mono_cfg_set_exception (cfg, MONO_EXCEPTION_OBJECT_SUPPLIED);
7372 MONO_GC_REGISTER_ROOT_SINGLE (cfg->exception_ptr, MONO_ROOT_SOURCE_JIT, "jit exception");
7373 cfg->exception_ptr = exception;
7377 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7380 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7381 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
7382 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7383 /* Optimize reg-reg moves away */
7385 * Can't optimize other opcodes, since sp[0] might point to
7386 * the last ins of a decomposed opcode.
7388 sp [0]->dreg = (cfg)->locals [n]->dreg;
7390 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7395 * ldloca inhibits many optimizations so try to get rid of it in common
7398 static inline unsigned char *
7399 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7409 local = read16 (ip + 2);
7413 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7414 /* From the INITOBJ case */
7415 token = read32 (ip + 2);
7416 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7417 CHECK_TYPELOAD (klass);
7418 type = mini_get_underlying_type (&klass->byval_arg);
7419 emit_init_local (cfg, local, type, TRUE);
7427 is_exception_class (MonoClass *klass)
7430 if (klass == mono_defaults.exception_class)
7432 klass = klass->parent;
7438 * is_jit_optimizer_disabled:
7440 * Determine whenever M's assembly has a DebuggableAttribute with the
7441 * IsJITOptimizerDisabled flag set.
7444 is_jit_optimizer_disabled (MonoMethod *m)
7446 MonoAssembly *ass = m->klass->image->assembly;
7447 MonoCustomAttrInfo* attrs;
7448 static MonoClass *klass;
7450 gboolean val = FALSE;
7453 if (ass->jit_optimizer_disabled_inited)
7454 return ass->jit_optimizer_disabled;
7457 klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggableAttribute");
7460 ass->jit_optimizer_disabled = FALSE;
7461 mono_memory_barrier ();
7462 ass->jit_optimizer_disabled_inited = TRUE;
7466 attrs = mono_custom_attrs_from_assembly (ass);
7468 for (i = 0; i < attrs->num_attrs; ++i) {
7469 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7471 MonoMethodSignature *sig;
7473 if (!attr->ctor || attr->ctor->klass != klass)
7475 /* Decode the attribute. See reflection.c */
7476 p = (const char*)attr->data;
7477 g_assert (read16 (p) == 0x0001);
7480 // FIXME: Support named parameters
7481 sig = mono_method_signature (attr->ctor);
7482 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7484 /* Two boolean arguments */
7488 mono_custom_attrs_free (attrs);
7491 ass->jit_optimizer_disabled = val;
7492 mono_memory_barrier ();
7493 ass->jit_optimizer_disabled_inited = TRUE;
7499 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7501 gboolean supported_tail_call;
7504 #ifdef MONO_ARCH_HAVE_OP_TAIL_CALL
7505 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7507 supported_tail_call = mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)) && !MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->ret);
7510 for (i = 0; i < fsig->param_count; ++i) {
7511 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7512 /* These can point to the current method's stack */
7513 supported_tail_call = FALSE;
7515 if (fsig->hasthis && cmethod->klass->valuetype)
7516 /* this might point to the current method's stack */
7517 supported_tail_call = FALSE;
7518 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7519 supported_tail_call = FALSE;
7520 if (cfg->method->save_lmf)
7521 supported_tail_call = FALSE;
7522 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7523 supported_tail_call = FALSE;
7524 if (call_opcode != CEE_CALL)
7525 supported_tail_call = FALSE;
7527 /* Debugging support */
7529 if (supported_tail_call) {
7530 if (!mono_debug_count ())
7531 supported_tail_call = FALSE;
7535 return supported_tail_call;
7541 * Handle calls made to ctors from NEWOBJ opcodes.
7544 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7545 MonoInst **sp, guint8 *ip, int *inline_costs)
7547 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7549 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7550 mono_method_is_generic_sharable (cmethod, TRUE)) {
7551 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7552 mono_class_vtable (cfg->domain, cmethod->klass);
7553 CHECK_TYPELOAD (cmethod->klass);
7555 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7556 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7559 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
7560 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7562 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7564 CHECK_TYPELOAD (cmethod->klass);
7565 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7570 /* Avoid virtual calls to ctors if possible */
7571 if (mono_class_is_marshalbyref (cmethod->klass))
7572 callvirt_this_arg = sp [0];
7574 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7575 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7576 CHECK_CFG_EXCEPTION;
7577 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7578 mono_method_check_inlining (cfg, cmethod) &&
7579 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7582 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7583 cfg->real_offset += 5;
7585 *inline_costs += costs - 5;
7587 INLINE_FAILURE ("inline failure");
7588 // FIXME-VT: Clean this up
7589 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7590 GSHAREDVT_FAILURE(*ip);
7591 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7593 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7596 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7597 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7598 } else if (context_used &&
7599 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7600 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7601 MonoInst *cmethod_addr;
7603 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7605 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7606 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7608 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7610 INLINE_FAILURE ("ctor call");
7611 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7612 callvirt_this_arg, NULL, vtable_arg);
7619 * mono_method_to_ir:
7621 * Translate the .net IL into linear IR.
7624 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
7625 MonoInst *return_var, MonoInst **inline_args,
7626 guint inline_offset, gboolean is_virtual_call)
7629 MonoInst *ins, **sp, **stack_start;
7630 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
7631 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7632 MonoMethod *cmethod, *method_definition;
7633 MonoInst **arg_array;
7634 MonoMethodHeader *header;
7636 guint32 token, ins_flag;
7638 MonoClass *constrained_class = NULL;
7639 unsigned char *ip, *end, *target, *err_pos;
7640 MonoMethodSignature *sig;
7641 MonoGenericContext *generic_context = NULL;
7642 MonoGenericContainer *generic_container = NULL;
7643 MonoType **param_types;
7644 int i, n, start_new_bblock, dreg;
7645 int num_calls = 0, inline_costs = 0;
7646 int breakpoint_id = 0;
7648 GSList *class_inits = NULL;
7649 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7651 gboolean init_locals, seq_points, skip_dead_blocks;
7652 gboolean sym_seq_points = FALSE;
7653 MonoDebugMethodInfo *minfo;
7654 MonoBitSet *seq_point_locs = NULL;
7655 MonoBitSet *seq_point_set_locs = NULL;
7657 cfg->disable_inline = is_jit_optimizer_disabled (method);
7659 /* serialization and xdomain stuff may need access to private fields and methods */
7660 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7661 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7662 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7663 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7664 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7665 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7667 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7668 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7669 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7670 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7671 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7673 image = method->klass->image;
7674 header = mono_method_get_header (method);
7676 MonoLoaderError *error;
7678 if ((error = mono_loader_get_last_error ())) {
7679 mono_cfg_set_exception (cfg, error->exception_type);
7681 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
7682 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
7684 goto exception_exit;
7686 generic_container = mono_method_get_generic_container (method);
7687 sig = mono_method_signature (method);
7688 num_args = sig->hasthis + sig->param_count;
7689 ip = (unsigned char*)header->code;
7690 cfg->cil_start = ip;
7691 end = ip + header->code_size;
7692 cfg->stat_cil_code_size += header->code_size;
7694 seq_points = cfg->gen_seq_points && cfg->method == method;
7696 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7697 /* We could hit a seq point before attaching to the JIT (#8338) */
7701 if (cfg->gen_sdb_seq_points && cfg->method == method) {
7702 minfo = mono_debug_lookup_method (method);
7704 MonoSymSeqPoint *sps;
7705 int i, n_il_offsets;
7707 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7708 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7709 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);
7710 sym_seq_points = TRUE;
7711 for (i = 0; i < n_il_offsets; ++i) {
7712 if (sps [i].il_offset < header->code_size)
7713 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7716 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7717 /* Methods without line number info like auto-generated property accessors */
7718 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7719 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);
7720 sym_seq_points = TRUE;
7725 * Methods without init_locals set could cause asserts in various passes
7726 * (#497220). To work around this, we emit dummy initialization opcodes
7727 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7728 * on some platforms.
7730 if ((cfg->opt & MONO_OPT_UNSAFE) && ARCH_HAVE_DUMMY_INIT)
7731 init_locals = header->init_locals;
7735 method_definition = method;
7736 while (method_definition->is_inflated) {
7737 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7738 method_definition = imethod->declaring;
7741 /* SkipVerification is not allowed if core-clr is enabled */
7742 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7744 dont_verify_stloc = TRUE;
7747 if (sig->is_inflated)
7748 generic_context = mono_method_get_context (method);
7749 else if (generic_container)
7750 generic_context = &generic_container->context;
7751 cfg->generic_context = generic_context;
7754 g_assert (!sig->has_type_parameters);
7756 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7757 g_assert (method->is_inflated);
7758 g_assert (mono_method_get_context (method)->method_inst);
7760 if (method->is_inflated && mono_method_get_context (method)->method_inst)
7761 g_assert (sig->generic_param_count);
7763 if (cfg->method == method) {
7764 cfg->real_offset = 0;
7766 cfg->real_offset = inline_offset;
7769 cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7770 cfg->cil_offset_to_bb_len = header->code_size;
7772 cfg->current_method = method;
7774 if (cfg->verbose_level > 2)
7775 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7777 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7779 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7780 for (n = 0; n < sig->param_count; ++n)
7781 param_types [n + sig->hasthis] = sig->params [n];
7782 cfg->arg_types = param_types;
7784 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7785 if (cfg->method == method) {
7787 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7788 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7791 NEW_BBLOCK (cfg, start_bblock);
7792 cfg->bb_entry = start_bblock;
7793 start_bblock->cil_code = NULL;
7794 start_bblock->cil_length = 0;
7797 NEW_BBLOCK (cfg, end_bblock);
7798 cfg->bb_exit = end_bblock;
7799 end_bblock->cil_code = NULL;
7800 end_bblock->cil_length = 0;
7801 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7802 g_assert (cfg->num_bblocks == 2);
7804 arg_array = cfg->args;
7806 if (header->num_clauses) {
7807 cfg->spvars = g_hash_table_new (NULL, NULL);
7808 cfg->exvars = g_hash_table_new (NULL, NULL);
7810 /* handle exception clauses */
7811 for (i = 0; i < header->num_clauses; ++i) {
7812 MonoBasicBlock *try_bb;
7813 MonoExceptionClause *clause = &header->clauses [i];
7814 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7815 try_bb->real_offset = clause->try_offset;
7816 try_bb->try_start = TRUE;
7817 try_bb->region = ((i + 1) << 8) | clause->flags;
7818 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7819 tblock->real_offset = clause->handler_offset;
7820 tblock->flags |= BB_EXCEPTION_HANDLER;
7823 * Linking the try block with the EH block hinders inlining as we won't be able to
7824 * merge the bblocks from inlining and produce an artificial hole for no good reason.
7826 if (COMPILE_LLVM (cfg))
7827 link_bblock (cfg, try_bb, tblock);
7829 if (*(ip + clause->handler_offset) == CEE_POP)
7830 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7832 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7833 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7834 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7835 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7836 MONO_ADD_INS (tblock, ins);
7838 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7839 /* finally clauses already have a seq point */
7840 /* seq points for filter clauses are emitted below */
7841 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7842 MONO_ADD_INS (tblock, ins);
7845 /* todo: is a fault block unsafe to optimize? */
7846 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7847 tblock->flags |= BB_EXCEPTION_UNSAFE;
7850 /*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);
7852 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7854 /* catch and filter blocks get the exception object on the stack */
7855 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7856 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7858 /* mostly like handle_stack_args (), but just sets the input args */
7859 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7860 tblock->in_scount = 1;
7861 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7862 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7866 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7867 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7868 if (!cfg->compile_llvm) {
7869 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7870 ins->dreg = tblock->in_stack [0]->dreg;
7871 MONO_ADD_INS (tblock, ins);
7874 MonoInst *dummy_use;
7877 * Add a dummy use for the exvar so its liveness info will be
7880 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7883 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7884 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7885 MONO_ADD_INS (tblock, ins);
7888 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7889 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7890 tblock->flags |= BB_EXCEPTION_HANDLER;
7891 tblock->real_offset = clause->data.filter_offset;
7892 tblock->in_scount = 1;
7893 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7894 /* The filter block shares the exvar with the handler block */
7895 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7896 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7897 MONO_ADD_INS (tblock, ins);
7901 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7902 clause->data.catch_class &&
7904 mono_class_check_context_used (clause->data.catch_class)) {
7906 * In shared generic code with catch
7907 * clauses containing type variables
7908 * the exception handling code has to
7909 * be able to get to the rgctx.
7910 * Therefore we have to make sure that
7911 * the vtable/mrgctx argument (for
7912 * static or generic methods) or the
7913 * "this" argument (for non-static
7914 * methods) are live.
7916 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7917 mini_method_get_context (method)->method_inst ||
7918 method->klass->valuetype) {
7919 mono_get_vtable_var (cfg);
7921 MonoInst *dummy_use;
7923 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7928 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7929 cfg->cbb = start_bblock;
7930 cfg->args = arg_array;
7931 mono_save_args (cfg, sig, inline_args);
7934 /* FIRST CODE BLOCK */
7935 NEW_BBLOCK (cfg, tblock);
7936 tblock->cil_code = ip;
7940 ADD_BBLOCK (cfg, tblock);
7942 if (cfg->method == method) {
7943 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7944 if (breakpoint_id) {
7945 MONO_INST_NEW (cfg, ins, OP_BREAK);
7946 MONO_ADD_INS (cfg->cbb, ins);
7950 /* we use a separate basic block for the initialization code */
7951 NEW_BBLOCK (cfg, init_localsbb);
7952 cfg->bb_init = init_localsbb;
7953 init_localsbb->real_offset = cfg->real_offset;
7954 start_bblock->next_bb = init_localsbb;
7955 init_localsbb->next_bb = cfg->cbb;
7956 link_bblock (cfg, start_bblock, init_localsbb);
7957 link_bblock (cfg, init_localsbb, cfg->cbb);
7959 cfg->cbb = init_localsbb;
7961 if (cfg->gsharedvt && cfg->method == method) {
7962 MonoGSharedVtMethodInfo *info;
7963 MonoInst *var, *locals_var;
7966 info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7967 info->method = cfg->method;
7968 info->count_entries = 16;
7969 info->entries = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7970 cfg->gsharedvt_info = info;
7972 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7973 /* prevent it from being register allocated */
7974 //var->flags |= MONO_INST_VOLATILE;
7975 cfg->gsharedvt_info_var = var;
7977 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7978 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7980 /* Allocate locals */
7981 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7982 /* prevent it from being register allocated */
7983 //locals_var->flags |= MONO_INST_VOLATILE;
7984 cfg->gsharedvt_locals_var = locals_var;
7986 dreg = alloc_ireg (cfg);
7987 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7989 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7990 ins->dreg = locals_var->dreg;
7992 MONO_ADD_INS (cfg->cbb, ins);
7993 cfg->gsharedvt_locals_var_ins = ins;
7995 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7998 ins->flags |= MONO_INST_INIT;
8002 if (mono_security_core_clr_enabled ()) {
8003 /* check if this is native code, e.g. an icall or a p/invoke */
8004 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8005 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8007 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8008 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8010 /* if this ia a native call then it can only be JITted from platform code */
8011 if ((icall || pinvk) && method->klass && method->klass->image) {
8012 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8013 MonoException *ex = icall ? mono_get_exception_security () :
8014 mono_get_exception_method_access ();
8015 emit_throw_exception (cfg, ex);
8022 CHECK_CFG_EXCEPTION;
8024 if (header->code_size == 0)
8027 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8032 if (cfg->method == method)
8033 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8035 for (n = 0; n < header->num_locals; ++n) {
8036 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8041 /* We force the vtable variable here for all shared methods
8042 for the possibility that they might show up in a stack
8043 trace where their exact instantiation is needed. */
8044 if (cfg->gshared && method == cfg->method) {
8045 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8046 mini_method_get_context (method)->method_inst ||
8047 method->klass->valuetype) {
8048 mono_get_vtable_var (cfg);
8050 /* FIXME: Is there a better way to do this?
8051 We need the variable live for the duration
8052 of the whole method. */
8053 cfg->args [0]->flags |= MONO_INST_VOLATILE;
8057 /* add a check for this != NULL to inlined methods */
8058 if (is_virtual_call) {
8061 NEW_ARGLOAD (cfg, arg_ins, 0);
8062 MONO_ADD_INS (cfg->cbb, arg_ins);
8063 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8066 skip_dead_blocks = !dont_verify;
8067 if (skip_dead_blocks) {
8068 original_bb = bb = mono_basic_block_split (method, &cfg->error);
8073 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8074 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8077 start_new_bblock = 0;
8079 if (cfg->method == method)
8080 cfg->real_offset = ip - header->code;
8082 cfg->real_offset = inline_offset;
8087 if (start_new_bblock) {
8088 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8089 if (start_new_bblock == 2) {
8090 g_assert (ip == tblock->cil_code);
8092 GET_BBLOCK (cfg, tblock, ip);
8094 cfg->cbb->next_bb = tblock;
8096 start_new_bblock = 0;
8097 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8098 if (cfg->verbose_level > 3)
8099 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8100 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8104 g_slist_free (class_inits);
8107 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8108 link_bblock (cfg, cfg->cbb, tblock);
8109 if (sp != stack_start) {
8110 handle_stack_args (cfg, stack_start, sp - stack_start);
8112 CHECK_UNVERIFIABLE (cfg);
8114 cfg->cbb->next_bb = tblock;
8116 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8117 if (cfg->verbose_level > 3)
8118 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8119 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8122 g_slist_free (class_inits);
8127 if (skip_dead_blocks) {
8128 int ip_offset = ip - header->code;
8130 if (ip_offset == bb->end)
8134 int op_size = mono_opcode_size (ip, end);
8135 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8137 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8139 if (ip_offset + op_size == bb->end) {
8140 MONO_INST_NEW (cfg, ins, OP_NOP);
8141 MONO_ADD_INS (cfg->cbb, ins);
8142 start_new_bblock = 1;
8150 * Sequence points are points where the debugger can place a breakpoint.
8151 * Currently, we generate these automatically at points where the IL
8154 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8156 * Make methods interruptable at the beginning, and at the targets of
8157 * backward branches.
8158 * Also, do this at the start of every bblock in methods with clauses too,
8159 * to be able to handle instructions with inprecise control flow like
8161 * Backward branches are handled at the end of method-to-ir ().
8163 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8164 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8166 /* Avoid sequence points on empty IL like .volatile */
8167 // FIXME: Enable this
8168 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8169 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8170 if ((sp != stack_start) && !sym_seq_point)
8171 ins->flags |= MONO_INST_NONEMPTY_STACK;
8172 MONO_ADD_INS (cfg->cbb, ins);
8175 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8178 cfg->cbb->real_offset = cfg->real_offset;
8180 if ((cfg->method == method) && cfg->coverage_info) {
8181 guint32 cil_offset = ip - header->code;
8182 cfg->coverage_info->data [cil_offset].cil_code = ip;
8184 /* TODO: Use an increment here */
8185 #if defined(TARGET_X86)
8186 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8187 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8189 MONO_ADD_INS (cfg->cbb, ins);
8191 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8192 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8196 if (cfg->verbose_level > 3)
8197 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8201 if (seq_points && !sym_seq_points && sp != stack_start) {
8203 * The C# compiler uses these nops to notify the JIT that it should
8204 * insert seq points.
8206 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8207 MONO_ADD_INS (cfg->cbb, ins);
8209 if (cfg->keep_cil_nops)
8210 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8212 MONO_INST_NEW (cfg, ins, OP_NOP);
8214 MONO_ADD_INS (cfg->cbb, ins);
8217 if (should_insert_brekpoint (cfg->method)) {
8218 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8220 MONO_INST_NEW (cfg, ins, OP_NOP);
8223 MONO_ADD_INS (cfg->cbb, ins);
8229 CHECK_STACK_OVF (1);
8230 n = (*ip)-CEE_LDARG_0;
8232 EMIT_NEW_ARGLOAD (cfg, ins, n);
8240 CHECK_STACK_OVF (1);
8241 n = (*ip)-CEE_LDLOC_0;
8243 EMIT_NEW_LOCLOAD (cfg, ins, n);
8252 n = (*ip)-CEE_STLOC_0;
8255 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8257 emit_stloc_ir (cfg, sp, header, n);
8264 CHECK_STACK_OVF (1);
8267 EMIT_NEW_ARGLOAD (cfg, ins, n);
8273 CHECK_STACK_OVF (1);
8276 NEW_ARGLOADA (cfg, ins, n);
8277 MONO_ADD_INS (cfg->cbb, ins);
8287 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8289 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8294 CHECK_STACK_OVF (1);
8297 EMIT_NEW_LOCLOAD (cfg, ins, n);
8301 case CEE_LDLOCA_S: {
8302 unsigned char *tmp_ip;
8304 CHECK_STACK_OVF (1);
8305 CHECK_LOCAL (ip [1]);
8307 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8313 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8322 CHECK_LOCAL (ip [1]);
8323 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8325 emit_stloc_ir (cfg, sp, header, ip [1]);
8330 CHECK_STACK_OVF (1);
8331 EMIT_NEW_PCONST (cfg, ins, NULL);
8332 ins->type = STACK_OBJ;
8337 CHECK_STACK_OVF (1);
8338 EMIT_NEW_ICONST (cfg, ins, -1);
8351 CHECK_STACK_OVF (1);
8352 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8358 CHECK_STACK_OVF (1);
8360 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8366 CHECK_STACK_OVF (1);
8367 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8373 CHECK_STACK_OVF (1);
8374 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8375 ins->type = STACK_I8;
8376 ins->dreg = alloc_dreg (cfg, STACK_I8);
8378 ins->inst_l = (gint64)read64 (ip);
8379 MONO_ADD_INS (cfg->cbb, ins);
8385 gboolean use_aotconst = FALSE;
8387 #ifdef TARGET_POWERPC
8388 /* FIXME: Clean this up */
8389 if (cfg->compile_aot)
8390 use_aotconst = TRUE;
8393 /* FIXME: we should really allocate this only late in the compilation process */
8394 f = mono_domain_alloc (cfg->domain, sizeof (float));
8396 CHECK_STACK_OVF (1);
8402 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8404 dreg = alloc_freg (cfg);
8405 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8406 ins->type = cfg->r4_stack_type;
8408 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8409 ins->type = cfg->r4_stack_type;
8410 ins->dreg = alloc_dreg (cfg, STACK_R8);
8412 MONO_ADD_INS (cfg->cbb, ins);
8422 gboolean use_aotconst = FALSE;
8424 #ifdef TARGET_POWERPC
8425 /* FIXME: Clean this up */
8426 if (cfg->compile_aot)
8427 use_aotconst = TRUE;
8430 /* FIXME: we should really allocate this only late in the compilation process */
8431 d = mono_domain_alloc (cfg->domain, sizeof (double));
8433 CHECK_STACK_OVF (1);
8439 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8441 dreg = alloc_freg (cfg);
8442 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8443 ins->type = STACK_R8;
8445 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8446 ins->type = STACK_R8;
8447 ins->dreg = alloc_dreg (cfg, STACK_R8);
8449 MONO_ADD_INS (cfg->cbb, ins);
8458 MonoInst *temp, *store;
8460 CHECK_STACK_OVF (1);
8464 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8465 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8467 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8470 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8483 if (sp [0]->type == STACK_R8)
8484 /* we need to pop the value from the x86 FP stack */
8485 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8491 INLINE_FAILURE ("jmp");
8492 GSHAREDVT_FAILURE (*ip);
8495 if (stack_start != sp)
8497 token = read32 (ip + 1);
8498 /* FIXME: check the signature matches */
8499 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8501 if (!cmethod || mono_loader_get_last_error ())
8504 if (cfg->gshared && mono_method_check_context_used (cmethod))
8505 GENERIC_SHARING_FAILURE (CEE_JMP);
8507 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8509 if (ARCH_HAVE_OP_TAIL_CALL) {
8510 MonoMethodSignature *fsig = mono_method_signature (cmethod);
8513 /* Handle tail calls similarly to calls */
8514 n = fsig->param_count + fsig->hasthis;
8518 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8519 call->method = cmethod;
8520 call->tail_call = TRUE;
8521 call->signature = mono_method_signature (cmethod);
8522 call->args = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8523 call->inst.inst_p0 = cmethod;
8524 for (i = 0; i < n; ++i)
8525 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8527 mono_arch_emit_call (cfg, call);
8528 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8529 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8531 for (i = 0; i < num_args; ++i)
8532 /* Prevent arguments from being optimized away */
8533 arg_array [i]->flags |= MONO_INST_VOLATILE;
8535 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8536 ins = (MonoInst*)call;
8537 ins->inst_p0 = cmethod;
8538 MONO_ADD_INS (cfg->cbb, ins);
8542 start_new_bblock = 1;
8547 MonoMethodSignature *fsig;
8550 token = read32 (ip + 1);
8554 //GSHAREDVT_FAILURE (*ip);
8559 fsig = mini_get_signature (method, token, generic_context);
8561 if (method->dynamic && fsig->pinvoke) {
8565 * This is a call through a function pointer using a pinvoke
8566 * signature. Have to create a wrapper and call that instead.
8567 * FIXME: This is very slow, need to create a wrapper at JIT time
8568 * instead based on the signature.
8570 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8571 EMIT_NEW_PCONST (cfg, args [1], fsig);
8573 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8576 n = fsig->param_count + fsig->hasthis;
8580 //g_assert (!virtual || fsig->hasthis);
8584 inline_costs += 10 * num_calls++;
8587 * Making generic calls out of gsharedvt methods.
8588 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8589 * patching gshared method addresses into a gsharedvt method.
8591 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8593 * We pass the address to the gsharedvt trampoline in the rgctx reg
8595 MonoInst *callee = addr;
8597 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8599 GSHAREDVT_FAILURE (*ip);
8601 addr = emit_get_rgctx_sig (cfg, context_used,
8602 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8603 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8607 /* Prevent inlining of methods with indirect calls */
8608 INLINE_FAILURE ("indirect call");
8610 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8615 * Instead of emitting an indirect call, emit a direct call
8616 * with the contents of the aotconst as the patch info.
8618 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8619 info_type = addr->inst_c1;
8620 info_data = addr->inst_p0;
8622 info_type = addr->inst_right->inst_c1;
8623 info_data = addr->inst_right->inst_left;
8626 if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8627 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8632 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8636 /* End of call, INS should contain the result of the call, if any */
8638 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8640 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8643 CHECK_CFG_EXCEPTION;
8647 constrained_class = NULL;
8651 case CEE_CALLVIRT: {
8652 MonoInst *addr = NULL;
8653 MonoMethodSignature *fsig = NULL;
8655 int virtual = *ip == CEE_CALLVIRT;
8656 gboolean pass_imt_from_rgctx = FALSE;
8657 MonoInst *imt_arg = NULL;
8658 MonoInst *keep_this_alive = NULL;
8659 gboolean pass_vtable = FALSE;
8660 gboolean pass_mrgctx = FALSE;
8661 MonoInst *vtable_arg = NULL;
8662 gboolean check_this = FALSE;
8663 gboolean supported_tail_call = FALSE;
8664 gboolean tail_call = FALSE;
8665 gboolean need_seq_point = FALSE;
8666 guint32 call_opcode = *ip;
8667 gboolean emit_widen = TRUE;
8668 gboolean push_res = TRUE;
8669 gboolean skip_ret = FALSE;
8670 gboolean delegate_invoke = FALSE;
8671 gboolean direct_icall = FALSE;
8672 gboolean constrained_partial_call = FALSE;
8673 MonoMethod *cil_method;
8676 token = read32 (ip + 1);
8680 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8681 cil_method = cmethod;
8683 if (constrained_class) {
8684 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8685 if (!mini_is_gsharedvt_klass (constrained_class)) {
8686 g_assert (!cmethod->klass->valuetype);
8687 if (!mini_type_is_reference (&constrained_class->byval_arg))
8688 constrained_partial_call = TRUE;
8692 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8693 if (cfg->verbose_level > 2)
8694 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8695 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8696 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8698 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8702 if (cfg->verbose_level > 2)
8703 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8705 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8707 * This is needed since get_method_constrained can't find
8708 * the method in klass representing a type var.
8709 * The type var is guaranteed to be a reference type in this
8712 if (!mini_is_gsharedvt_klass (constrained_class))
8713 g_assert (!cmethod->klass->valuetype);
8715 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8721 if (!cmethod || mono_loader_get_last_error ())
8723 if (!dont_verify && !cfg->skip_visibility) {
8724 MonoMethod *target_method = cil_method;
8725 if (method->is_inflated) {
8726 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
8728 if (!mono_method_can_access_method (method_definition, target_method) &&
8729 !mono_method_can_access_method (method, cil_method))
8730 METHOD_ACCESS_FAILURE (method, cil_method);
8733 if (mono_security_core_clr_enabled ())
8734 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8736 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8737 /* MS.NET seems to silently convert this to a callvirt */
8742 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8743 * converts to a callvirt.
8745 * tests/bug-515884.il is an example of this behavior
8747 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8748 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8749 if (!virtual && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8753 if (!cmethod->klass->inited)
8754 if (!mono_class_init (cmethod->klass))
8755 TYPE_LOAD_ERROR (cmethod->klass);
8757 fsig = mono_method_signature (cmethod);
8760 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8761 mini_class_is_system_array (cmethod->klass)) {
8762 array_rank = cmethod->klass->rank;
8763 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8764 direct_icall = TRUE;
8765 } else if (fsig->pinvoke) {
8766 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8767 fsig = mono_method_signature (wrapper);
8768 } else if (constrained_class) {
8770 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8774 /* See code below */
8775 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8776 MonoBasicBlock *tbb;
8778 GET_BBLOCK (cfg, tbb, ip + 5);
8779 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8781 * We want to extend the try block to cover the call, but we can't do it if the
8782 * call is made directly since its followed by an exception check.
8784 direct_icall = FALSE;
8788 mono_save_token_info (cfg, image, token, cil_method);
8790 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8791 need_seq_point = TRUE;
8793 /* Don't support calls made using type arguments for now */
8795 if (cfg->gsharedvt) {
8796 if (mini_is_gsharedvt_signature (fsig))
8797 GSHAREDVT_FAILURE (*ip);
8801 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8802 g_assert_not_reached ();
8804 n = fsig->param_count + fsig->hasthis;
8806 if (!cfg->gshared && cmethod->klass->generic_container)
8810 g_assert (!mono_method_check_context_used (cmethod));
8814 //g_assert (!virtual || fsig->hasthis);
8818 if (constrained_class) {
8819 if (mini_is_gsharedvt_klass (constrained_class)) {
8820 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8821 /* The 'Own method' case below */
8822 } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
8823 /* 'The type parameter is instantiated as a reference type' case below. */
8825 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8826 CHECK_CFG_EXCEPTION;
8833 * We have the `constrained.' prefix opcode.
8835 if (constrained_partial_call) {
8836 gboolean need_box = TRUE;
8839 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8840 * called method is not known at compile time either. The called method could end up being
8841 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8842 * to box the receiver.
8843 * A simple solution would be to box always and make a normal virtual call, but that would
8844 * be bad performance wise.
8846 if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE && cmethod->klass->generic_class) {
8848 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8853 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)) {
8854 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8855 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8856 ins->klass = constrained_class;
8857 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8858 CHECK_CFG_EXCEPTION;
8859 } else if (need_box) {
8861 MonoBasicBlock *is_ref_bb, *end_bb;
8862 MonoInst *nonbox_call;
8865 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8867 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8868 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8870 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8872 NEW_BBLOCK (cfg, is_ref_bb);
8873 NEW_BBLOCK (cfg, end_bb);
8875 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);
8876 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, 1);
8877 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8880 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8882 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8885 MONO_START_BB (cfg, is_ref_bb);
8886 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8887 ins->klass = constrained_class;
8888 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8889 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8891 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8893 MONO_START_BB (cfg, end_bb);
8896 nonbox_call->dreg = ins->dreg;
8899 g_assert (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
8900 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8901 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8904 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8906 * The type parameter is instantiated as a valuetype,
8907 * but that type doesn't override the method we're
8908 * calling, so we need to box `this'.
8910 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8911 ins->klass = constrained_class;
8912 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8913 CHECK_CFG_EXCEPTION;
8914 } else if (!constrained_class->valuetype) {
8915 int dreg = alloc_ireg_ref (cfg);
8918 * The type parameter is instantiated as a reference
8919 * type. We have a managed pointer on the stack, so
8920 * we need to dereference it here.
8922 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8923 ins->type = STACK_OBJ;
8926 if (cmethod->klass->valuetype) {
8929 /* Interface method */
8932 mono_class_setup_vtable (constrained_class);
8933 CHECK_TYPELOAD (constrained_class);
8934 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8936 TYPE_LOAD_ERROR (constrained_class);
8937 slot = mono_method_get_vtable_slot (cmethod);
8939 TYPE_LOAD_ERROR (cmethod->klass);
8940 cmethod = constrained_class->vtable [ioffset + slot];
8942 if (cmethod->klass == mono_defaults.enum_class) {
8943 /* Enum implements some interfaces, so treat this as the first case */
8944 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8945 ins->klass = constrained_class;
8946 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8947 CHECK_CFG_EXCEPTION;
8952 constrained_class = NULL;
8955 if (check_call_signature (cfg, fsig, sp))
8958 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8959 delegate_invoke = TRUE;
8961 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8962 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8963 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8971 * If the callee is a shared method, then its static cctor
8972 * might not get called after the call was patched.
8974 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)) {
8975 emit_class_init (cfg, cmethod->klass);
8976 CHECK_TYPELOAD (cmethod->klass);
8979 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8982 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8984 context_used = mini_method_check_context_used (cfg, cmethod);
8986 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
8987 /* Generic method interface
8988 calls are resolved via a
8989 helper function and don't
8991 if (!cmethod_context || !cmethod_context->method_inst)
8992 pass_imt_from_rgctx = TRUE;
8996 * If a shared method calls another
8997 * shared method then the caller must
8998 * have a generic sharing context
8999 * because the magic trampoline
9000 * requires it. FIXME: We shouldn't
9001 * have to force the vtable/mrgctx
9002 * variable here. Instead there
9003 * should be a flag in the cfg to
9004 * request a generic sharing context.
9007 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9008 mono_get_vtable_var (cfg);
9013 vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9015 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9017 CHECK_TYPELOAD (cmethod->klass);
9018 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9023 g_assert (!vtable_arg);
9025 if (!cfg->compile_aot) {
9027 * emit_get_rgctx_method () calls mono_class_vtable () so check
9028 * for type load errors before.
9030 mono_class_setup_vtable (cmethod->klass);
9031 CHECK_TYPELOAD (cmethod->klass);
9034 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9036 /* !marshalbyref is needed to properly handle generic methods + remoting */
9037 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9038 MONO_METHOD_IS_FINAL (cmethod)) &&
9039 !mono_class_is_marshalbyref (cmethod->klass)) {
9046 if (pass_imt_from_rgctx) {
9047 g_assert (!pass_vtable);
9049 imt_arg = emit_get_rgctx_method (cfg, context_used,
9050 cmethod, MONO_RGCTX_INFO_METHOD);
9054 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9056 /* Calling virtual generic methods */
9057 if (virtual && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9058 !(MONO_METHOD_IS_FINAL (cmethod) &&
9059 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9060 fsig->generic_param_count &&
9061 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))) {
9062 MonoInst *this_temp, *this_arg_temp, *store;
9063 MonoInst *iargs [4];
9064 gboolean use_imt = FALSE;
9066 g_assert (fsig->is_inflated);
9068 /* Prevent inlining of methods that contain indirect calls */
9069 INLINE_FAILURE ("virtual generic call");
9071 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9072 GSHAREDVT_FAILURE (*ip);
9074 #if MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK && defined(MONO_ARCH_GSHARED_SUPPORTED)
9075 if (cmethod->wrapper_type == MONO_WRAPPER_NONE)
9080 g_assert (!imt_arg);
9082 g_assert (cmethod->is_inflated);
9083 imt_arg = emit_get_rgctx_method (cfg, context_used,
9084 cmethod, MONO_RGCTX_INFO_METHOD);
9085 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9087 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9088 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9089 MONO_ADD_INS (cfg->cbb, store);
9091 /* FIXME: This should be a managed pointer */
9092 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9094 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9095 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9096 cmethod, MONO_RGCTX_INFO_METHOD);
9097 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9098 addr = mono_emit_jit_icall (cfg,
9099 mono_helper_compile_generic_method, iargs);
9101 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9103 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9110 * Implement a workaround for the inherent races involved in locking:
9116 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9117 * try block, the Exit () won't be executed, see:
9118 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9119 * To work around this, we extend such try blocks to include the last x bytes
9120 * of the Monitor.Enter () call.
9122 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9123 MonoBasicBlock *tbb;
9125 GET_BBLOCK (cfg, tbb, ip + 5);
9127 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9128 * from Monitor.Enter like ArgumentNullException.
9130 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9131 /* Mark this bblock as needing to be extended */
9132 tbb->extend_try_block = TRUE;
9136 /* Conversion to a JIT intrinsic */
9137 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9138 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9139 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9146 if ((cfg->opt & MONO_OPT_INLINE) &&
9147 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9148 mono_method_check_inlining (cfg, cmethod)) {
9150 gboolean always = FALSE;
9152 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9153 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9154 /* Prevent inlining of methods that call wrappers */
9155 INLINE_FAILURE ("wrapper call");
9156 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9160 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9162 cfg->real_offset += 5;
9164 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9165 /* *sp is already set by inline_method */
9170 inline_costs += costs;
9176 /* Tail recursion elimination */
9177 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9178 gboolean has_vtargs = FALSE;
9181 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9182 INLINE_FAILURE ("tail call");
9184 /* keep it simple */
9185 for (i = fsig->param_count - 1; i >= 0; i--) {
9186 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9191 for (i = 0; i < n; ++i)
9192 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9193 MONO_INST_NEW (cfg, ins, OP_BR);
9194 MONO_ADD_INS (cfg->cbb, ins);
9195 tblock = start_bblock->out_bb [0];
9196 link_bblock (cfg, cfg->cbb, tblock);
9197 ins->inst_target_bb = tblock;
9198 start_new_bblock = 1;
9200 /* skip the CEE_RET, too */
9201 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9208 inline_costs += 10 * num_calls++;
9211 * Making generic calls out of gsharedvt methods.
9212 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9213 * patching gshared method addresses into a gsharedvt method.
9215 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9216 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY)) {
9217 MonoRgctxInfoType info_type;
9220 //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9221 //GSHAREDVT_FAILURE (*ip);
9222 // disable for possible remoting calls
9223 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9224 GSHAREDVT_FAILURE (*ip);
9225 if (fsig->generic_param_count) {
9226 /* virtual generic call */
9227 g_assert (!imt_arg);
9228 /* Same as the virtual generic case above */
9229 imt_arg = emit_get_rgctx_method (cfg, context_used,
9230 cmethod, MONO_RGCTX_INFO_METHOD);
9231 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9233 } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9234 /* This can happen when we call a fully instantiated iface method */
9235 imt_arg = emit_get_rgctx_method (cfg, context_used,
9236 cmethod, MONO_RGCTX_INFO_METHOD);
9241 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9242 keep_this_alive = sp [0];
9244 if (virtual && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9245 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9247 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9248 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9250 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9254 /* Generic sharing */
9257 * Use this if the callee is gsharedvt sharable too, since
9258 * at runtime we might find an instantiation so the call cannot
9259 * be patched (the 'no_patch' code path in mini-trampolines.c).
9261 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9262 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9263 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9264 (!virtual || MONO_METHOD_IS_FINAL (cmethod) ||
9265 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9266 INLINE_FAILURE ("gshared");
9268 g_assert (cfg->gshared && cmethod);
9272 * We are compiling a call to a
9273 * generic method from shared code,
9274 * which means that we have to look up
9275 * the method in the rgctx and do an
9279 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9281 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9282 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9286 /* Direct calls to icalls */
9288 MonoMethod *wrapper;
9291 /* Inline the wrapper */
9292 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9294 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9295 g_assert (costs > 0);
9296 cfg->real_offset += 5;
9298 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9299 /* *sp is already set by inline_method */
9304 inline_costs += costs;
9313 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9314 MonoInst *val = sp [fsig->param_count];
9316 if (val->type == STACK_OBJ) {
9317 MonoInst *iargs [2];
9322 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9325 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9326 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9327 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9328 emit_write_barrier (cfg, addr, val);
9329 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9330 GSHAREDVT_FAILURE (*ip);
9331 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9332 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9334 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9335 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9336 if (!cmethod->klass->element_class->valuetype && !readonly)
9337 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9338 CHECK_TYPELOAD (cmethod->klass);
9341 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9344 g_assert_not_reached ();
9351 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL);
9355 /* Tail prefix / tail call optimization */
9357 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9358 /* FIXME: runtime generic context pointer for jumps? */
9359 /* FIXME: handle this for generic sharing eventually */
9360 if ((ins_flag & MONO_INST_TAILCALL) &&
9361 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9362 supported_tail_call = TRUE;
9364 if (supported_tail_call) {
9367 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9368 INLINE_FAILURE ("tail call");
9370 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9372 if (ARCH_HAVE_OP_TAIL_CALL) {
9373 /* Handle tail calls similarly to normal calls */
9376 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9378 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9379 call->tail_call = TRUE;
9380 call->method = cmethod;
9381 call->signature = mono_method_signature (cmethod);
9384 * We implement tail calls by storing the actual arguments into the
9385 * argument variables, then emitting a CEE_JMP.
9387 for (i = 0; i < n; ++i) {
9388 /* Prevent argument from being register allocated */
9389 arg_array [i]->flags |= MONO_INST_VOLATILE;
9390 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9392 ins = (MonoInst*)call;
9393 ins->inst_p0 = cmethod;
9394 ins->inst_p1 = arg_array [0];
9395 MONO_ADD_INS (cfg->cbb, ins);
9396 link_bblock (cfg, cfg->cbb, end_bblock);
9397 start_new_bblock = 1;
9399 // FIXME: Eliminate unreachable epilogs
9402 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9403 * only reachable from this call.
9405 GET_BBLOCK (cfg, tblock, ip + 5);
9406 if (tblock == cfg->cbb || tblock->in_count == 0)
9415 * Synchronized wrappers.
9416 * Its hard to determine where to replace a method with its synchronized
9417 * wrapper without causing an infinite recursion. The current solution is
9418 * to add the synchronized wrapper in the trampolines, and to
9419 * change the called method to a dummy wrapper, and resolve that wrapper
9420 * to the real method in mono_jit_compile_method ().
9422 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9423 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9424 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9425 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9429 INLINE_FAILURE ("call");
9430 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual ? sp [0] : NULL,
9431 imt_arg, vtable_arg);
9434 link_bblock (cfg, cfg->cbb, end_bblock);
9435 start_new_bblock = 1;
9437 // FIXME: Eliminate unreachable epilogs
9440 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9441 * only reachable from this call.
9443 GET_BBLOCK (cfg, tblock, ip + 5);
9444 if (tblock == cfg->cbb || tblock->in_count == 0)
9451 /* End of call, INS should contain the result of the call, if any */
9453 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9456 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9461 if (keep_this_alive) {
9462 MonoInst *dummy_use;
9464 /* See mono_emit_method_call_full () */
9465 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9468 CHECK_CFG_EXCEPTION;
9472 g_assert (*ip == CEE_RET);
9476 constrained_class = NULL;
9478 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9482 if (cfg->method != method) {
9483 /* return from inlined method */
9485 * If in_count == 0, that means the ret is unreachable due to
9486 * being preceeded by a throw. In that case, inline_method () will
9487 * handle setting the return value
9488 * (test case: test_0_inline_throw ()).
9490 if (return_var && cfg->cbb->in_count) {
9491 MonoType *ret_type = mono_method_signature (method)->ret;
9497 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9500 //g_assert (returnvar != -1);
9501 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9502 cfg->ret_var_set = TRUE;
9505 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9507 if (cfg->lmf_var && cfg->cbb->in_count)
9511 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9513 if (seq_points && !sym_seq_points) {
9515 * Place a seq point here too even through the IL stack is not
9516 * empty, so a step over on
9519 * will work correctly.
9521 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9522 MONO_ADD_INS (cfg->cbb, ins);
9525 g_assert (!return_var);
9529 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9532 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
9535 if (!cfg->vret_addr) {
9538 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, (*sp));
9540 EMIT_NEW_RETLOADA (cfg, ret_addr);
9542 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, (*sp)->dreg);
9543 ins->klass = mono_class_from_mono_type (ret_type);
9546 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
9547 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
9548 MonoInst *iargs [1];
9552 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
9553 mono_arch_emit_setret (cfg, method, conv);
9555 mono_arch_emit_setret (cfg, method, *sp);
9558 mono_arch_emit_setret (cfg, method, *sp);
9563 if (sp != stack_start)
9565 MONO_INST_NEW (cfg, ins, OP_BR);
9567 ins->inst_target_bb = end_bblock;
9568 MONO_ADD_INS (cfg->cbb, ins);
9569 link_bblock (cfg, cfg->cbb, end_bblock);
9570 start_new_bblock = 1;
9574 MONO_INST_NEW (cfg, ins, OP_BR);
9576 target = ip + 1 + (signed char)(*ip);
9578 GET_BBLOCK (cfg, tblock, target);
9579 link_bblock (cfg, cfg->cbb, tblock);
9580 ins->inst_target_bb = tblock;
9581 if (sp != stack_start) {
9582 handle_stack_args (cfg, stack_start, sp - stack_start);
9584 CHECK_UNVERIFIABLE (cfg);
9586 MONO_ADD_INS (cfg->cbb, ins);
9587 start_new_bblock = 1;
9588 inline_costs += BRANCH_COST;
9602 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9604 target = ip + 1 + *(signed char*)ip;
9610 inline_costs += BRANCH_COST;
9614 MONO_INST_NEW (cfg, ins, OP_BR);
9617 target = ip + 4 + (gint32)read32(ip);
9619 GET_BBLOCK (cfg, tblock, target);
9620 link_bblock (cfg, cfg->cbb, tblock);
9621 ins->inst_target_bb = tblock;
9622 if (sp != stack_start) {
9623 handle_stack_args (cfg, stack_start, sp - stack_start);
9625 CHECK_UNVERIFIABLE (cfg);
9628 MONO_ADD_INS (cfg->cbb, ins);
9630 start_new_bblock = 1;
9631 inline_costs += BRANCH_COST;
9638 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9639 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9640 guint32 opsize = is_short ? 1 : 4;
9642 CHECK_OPSIZE (opsize);
9644 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9647 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9652 GET_BBLOCK (cfg, tblock, target);
9653 link_bblock (cfg, cfg->cbb, tblock);
9654 GET_BBLOCK (cfg, tblock, ip);
9655 link_bblock (cfg, cfg->cbb, tblock);
9657 if (sp != stack_start) {
9658 handle_stack_args (cfg, stack_start, sp - stack_start);
9659 CHECK_UNVERIFIABLE (cfg);
9662 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9663 cmp->sreg1 = sp [0]->dreg;
9664 type_from_op (cfg, cmp, sp [0], NULL);
9667 #if SIZEOF_REGISTER == 4
9668 if (cmp->opcode == OP_LCOMPARE_IMM) {
9669 /* Convert it to OP_LCOMPARE */
9670 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9671 ins->type = STACK_I8;
9672 ins->dreg = alloc_dreg (cfg, STACK_I8);
9674 MONO_ADD_INS (cfg->cbb, ins);
9675 cmp->opcode = OP_LCOMPARE;
9676 cmp->sreg2 = ins->dreg;
9679 MONO_ADD_INS (cfg->cbb, cmp);
9681 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9682 type_from_op (cfg, ins, sp [0], NULL);
9683 MONO_ADD_INS (cfg->cbb, ins);
9684 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9685 GET_BBLOCK (cfg, tblock, target);
9686 ins->inst_true_bb = tblock;
9687 GET_BBLOCK (cfg, tblock, ip);
9688 ins->inst_false_bb = tblock;
9689 start_new_bblock = 2;
9692 inline_costs += BRANCH_COST;
9707 MONO_INST_NEW (cfg, ins, *ip);
9709 target = ip + 4 + (gint32)read32(ip);
9715 inline_costs += BRANCH_COST;
9719 MonoBasicBlock **targets;
9720 MonoBasicBlock *default_bblock;
9721 MonoJumpInfoBBTable *table;
9722 int offset_reg = alloc_preg (cfg);
9723 int target_reg = alloc_preg (cfg);
9724 int table_reg = alloc_preg (cfg);
9725 int sum_reg = alloc_preg (cfg);
9726 gboolean use_op_switch;
9730 n = read32 (ip + 1);
9733 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
9737 CHECK_OPSIZE (n * sizeof (guint32));
9738 target = ip + n * sizeof (guint32);
9740 GET_BBLOCK (cfg, default_bblock, target);
9741 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9743 targets = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9744 for (i = 0; i < n; ++i) {
9745 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9746 targets [i] = tblock;
9747 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9751 if (sp != stack_start) {
9753 * Link the current bb with the targets as well, so handle_stack_args
9754 * will set their in_stack correctly.
9756 link_bblock (cfg, cfg->cbb, default_bblock);
9757 for (i = 0; i < n; ++i)
9758 link_bblock (cfg, cfg->cbb, targets [i]);
9760 handle_stack_args (cfg, stack_start, sp - stack_start);
9762 CHECK_UNVERIFIABLE (cfg);
9765 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9766 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9768 for (i = 0; i < n; ++i)
9769 link_bblock (cfg, cfg->cbb, targets [i]);
9771 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9772 table->table = targets;
9773 table->table_size = n;
9775 use_op_switch = FALSE;
9777 /* ARM implements SWITCH statements differently */
9778 /* FIXME: Make it use the generic implementation */
9779 if (!cfg->compile_aot)
9780 use_op_switch = TRUE;
9783 if (COMPILE_LLVM (cfg))
9784 use_op_switch = TRUE;
9786 cfg->cbb->has_jump_table = 1;
9788 if (use_op_switch) {
9789 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9790 ins->sreg1 = src1->dreg;
9791 ins->inst_p0 = table;
9792 ins->inst_many_bb = targets;
9793 ins->klass = GUINT_TO_POINTER (n);
9794 MONO_ADD_INS (cfg->cbb, ins);
9796 if (sizeof (gpointer) == 8)
9797 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9799 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9801 #if SIZEOF_REGISTER == 8
9802 /* The upper word might not be zero, and we add it to a 64 bit address later */
9803 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9806 if (cfg->compile_aot) {
9807 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9809 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9810 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9811 ins->inst_p0 = table;
9812 ins->dreg = table_reg;
9813 MONO_ADD_INS (cfg->cbb, ins);
9816 /* FIXME: Use load_memindex */
9817 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9818 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9819 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9821 start_new_bblock = 1;
9822 inline_costs += (BRANCH_COST * 2);
9842 dreg = alloc_freg (cfg);
9845 dreg = alloc_lreg (cfg);
9848 dreg = alloc_ireg_ref (cfg);
9851 dreg = alloc_preg (cfg);
9854 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
9855 ins->type = ldind_type [*ip - CEE_LDIND_I1];
9856 if (*ip == CEE_LDIND_R4)
9857 ins->type = cfg->r4_stack_type;
9858 ins->flags |= ins_flag;
9859 MONO_ADD_INS (cfg->cbb, ins);
9861 if (ins_flag & MONO_INST_VOLATILE) {
9862 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9863 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9879 if (ins_flag & MONO_INST_VOLATILE) {
9880 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9881 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9884 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9885 ins->flags |= ins_flag;
9888 MONO_ADD_INS (cfg->cbb, ins);
9890 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)))
9891 emit_write_barrier (cfg, sp [0], sp [1]);
9900 MONO_INST_NEW (cfg, ins, (*ip));
9902 ins->sreg1 = sp [0]->dreg;
9903 ins->sreg2 = sp [1]->dreg;
9904 type_from_op (cfg, ins, sp [0], sp [1]);
9906 ins->dreg = alloc_dreg ((cfg), (ins)->type);
9908 /* Use the immediate opcodes if possible */
9909 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9910 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9911 if (imm_opcode != -1) {
9912 ins->opcode = imm_opcode;
9913 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9916 NULLIFY_INS (sp [1]);
9920 MONO_ADD_INS ((cfg)->cbb, (ins));
9922 *sp++ = mono_decompose_opcode (cfg, ins);
9939 MONO_INST_NEW (cfg, ins, (*ip));
9941 ins->sreg1 = sp [0]->dreg;
9942 ins->sreg2 = sp [1]->dreg;
9943 type_from_op (cfg, ins, sp [0], sp [1]);
9945 add_widen_op (cfg, ins, &sp [0], &sp [1]);
9946 ins->dreg = alloc_dreg ((cfg), (ins)->type);
9948 /* FIXME: Pass opcode to is_inst_imm */
9950 /* Use the immediate opcodes if possible */
9951 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)) {
9954 imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9955 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
9956 /* Keep emulated opcodes which are optimized away later */
9957 if ((ins->opcode == OP_IREM_UN || ins->opcode == OP_IDIV_UN_IMM) && (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP)) && sp [1]->opcode == OP_ICONST && mono_is_power_of_two (sp [1]->inst_c0) >= 0) {
9958 imm_opcode = mono_op_to_op_imm (ins->opcode);
9961 if (imm_opcode != -1) {
9962 ins->opcode = imm_opcode;
9963 if (sp [1]->opcode == OP_I8CONST) {
9964 #if SIZEOF_REGISTER == 8
9965 ins->inst_imm = sp [1]->inst_l;
9967 ins->inst_ls_word = sp [1]->inst_ls_word;
9968 ins->inst_ms_word = sp [1]->inst_ms_word;
9972 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9975 /* Might be followed by an instruction added by add_widen_op */
9976 if (sp [1]->next == NULL)
9977 NULLIFY_INS (sp [1]);
9980 MONO_ADD_INS ((cfg)->cbb, (ins));
9982 *sp++ = mono_decompose_opcode (cfg, ins);
9995 case CEE_CONV_OVF_I8:
9996 case CEE_CONV_OVF_U8:
10000 /* Special case this earlier so we have long constants in the IR */
10001 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10002 int data = sp [-1]->inst_c0;
10003 sp [-1]->opcode = OP_I8CONST;
10004 sp [-1]->type = STACK_I8;
10005 #if SIZEOF_REGISTER == 8
10006 if ((*ip) == CEE_CONV_U8)
10007 sp [-1]->inst_c0 = (guint32)data;
10009 sp [-1]->inst_c0 = data;
10011 sp [-1]->inst_ls_word = data;
10012 if ((*ip) == CEE_CONV_U8)
10013 sp [-1]->inst_ms_word = 0;
10015 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10017 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10024 case CEE_CONV_OVF_I4:
10025 case CEE_CONV_OVF_I1:
10026 case CEE_CONV_OVF_I2:
10027 case CEE_CONV_OVF_I:
10028 case CEE_CONV_OVF_U:
10031 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10032 ADD_UNOP (CEE_CONV_OVF_I8);
10039 case CEE_CONV_OVF_U1:
10040 case CEE_CONV_OVF_U2:
10041 case CEE_CONV_OVF_U4:
10044 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10045 ADD_UNOP (CEE_CONV_OVF_U8);
10052 case CEE_CONV_OVF_I1_UN:
10053 case CEE_CONV_OVF_I2_UN:
10054 case CEE_CONV_OVF_I4_UN:
10055 case CEE_CONV_OVF_I8_UN:
10056 case CEE_CONV_OVF_U1_UN:
10057 case CEE_CONV_OVF_U2_UN:
10058 case CEE_CONV_OVF_U4_UN:
10059 case CEE_CONV_OVF_U8_UN:
10060 case CEE_CONV_OVF_I_UN:
10061 case CEE_CONV_OVF_U_UN:
10068 CHECK_CFG_EXCEPTION;
10072 case CEE_ADD_OVF_UN:
10074 case CEE_MUL_OVF_UN:
10076 case CEE_SUB_OVF_UN:
10082 GSHAREDVT_FAILURE (*ip);
10085 token = read32 (ip + 1);
10086 klass = mini_get_class (method, token, generic_context);
10087 CHECK_TYPELOAD (klass);
10089 if (generic_class_is_reference_type (cfg, klass)) {
10090 MonoInst *store, *load;
10091 int dreg = alloc_ireg_ref (cfg);
10093 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10094 load->flags |= ins_flag;
10095 MONO_ADD_INS (cfg->cbb, load);
10097 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10098 store->flags |= ins_flag;
10099 MONO_ADD_INS (cfg->cbb, store);
10101 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10102 emit_write_barrier (cfg, sp [0], sp [1]);
10104 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10110 int loc_index = -1;
10116 token = read32 (ip + 1);
10117 klass = mini_get_class (method, token, generic_context);
10118 CHECK_TYPELOAD (klass);
10120 /* Optimize the common ldobj+stloc combination */
10123 loc_index = ip [6];
10130 loc_index = ip [5] - CEE_STLOC_0;
10137 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10138 CHECK_LOCAL (loc_index);
10140 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10141 ins->dreg = cfg->locals [loc_index]->dreg;
10142 ins->flags |= ins_flag;
10145 if (ins_flag & MONO_INST_VOLATILE) {
10146 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10147 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10153 /* Optimize the ldobj+stobj combination */
10154 /* The reference case ends up being a load+store anyway */
10155 /* Skip this if the operation is volatile. */
10156 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)) {
10161 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10168 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10169 ins->flags |= ins_flag;
10172 if (ins_flag & MONO_INST_VOLATILE) {
10173 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10174 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10183 CHECK_STACK_OVF (1);
10185 n = read32 (ip + 1);
10187 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10188 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10189 ins->type = STACK_OBJ;
10192 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10193 MonoInst *iargs [1];
10194 char *str = mono_method_get_wrapper_data (method, n);
10196 if (cfg->compile_aot)
10197 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10199 EMIT_NEW_PCONST (cfg, iargs [0], str);
10200 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10202 if (cfg->opt & MONO_OPT_SHARED) {
10203 MonoInst *iargs [3];
10205 if (cfg->compile_aot) {
10206 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10208 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10209 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10210 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10211 *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
10212 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10214 if (cfg->cbb->out_of_line) {
10215 MonoInst *iargs [2];
10217 if (image == mono_defaults.corlib) {
10219 * Avoid relocations in AOT and save some space by using a
10220 * version of helper_ldstr specialized to mscorlib.
10222 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10223 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10225 /* Avoid creating the string object */
10226 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10227 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10228 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10232 if (cfg->compile_aot) {
10233 NEW_LDSTRCONST (cfg, ins, image, n);
10235 MONO_ADD_INS (cfg->cbb, ins);
10238 NEW_PCONST (cfg, ins, NULL);
10239 ins->type = STACK_OBJ;
10240 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10242 OUT_OF_MEMORY_FAILURE;
10245 MONO_ADD_INS (cfg->cbb, ins);
10254 MonoInst *iargs [2];
10255 MonoMethodSignature *fsig;
10258 MonoInst *vtable_arg = NULL;
10261 token = read32 (ip + 1);
10262 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10263 if (!cmethod || mono_loader_get_last_error ())
10265 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10268 mono_save_token_info (cfg, image, token, cmethod);
10270 if (!mono_class_init (cmethod->klass))
10271 TYPE_LOAD_ERROR (cmethod->klass);
10273 context_used = mini_method_check_context_used (cfg, cmethod);
10275 if (mono_security_core_clr_enabled ())
10276 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10278 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)) {
10279 emit_class_init (cfg, cmethod->klass);
10280 CHECK_TYPELOAD (cmethod->klass);
10284 if (cfg->gsharedvt) {
10285 if (mini_is_gsharedvt_variable_signature (sig))
10286 GSHAREDVT_FAILURE (*ip);
10290 n = fsig->param_count;
10294 * Generate smaller code for the common newobj <exception> instruction in
10295 * argument checking code.
10297 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10298 is_exception_class (cmethod->klass) && n <= 2 &&
10299 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10300 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10301 MonoInst *iargs [3];
10305 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10308 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10311 iargs [1] = sp [0];
10312 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10315 iargs [1] = sp [0];
10316 iargs [2] = sp [1];
10317 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10320 g_assert_not_reached ();
10328 /* move the args to allow room for 'this' in the first position */
10334 /* check_call_signature () requires sp[0] to be set */
10335 this_ins.type = STACK_OBJ;
10336 sp [0] = &this_ins;
10337 if (check_call_signature (cfg, fsig, sp))
10342 if (mini_class_is_system_array (cmethod->klass)) {
10343 *sp = emit_get_rgctx_method (cfg, context_used,
10344 cmethod, MONO_RGCTX_INFO_METHOD);
10346 /* Avoid varargs in the common case */
10347 if (fsig->param_count == 1)
10348 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10349 else if (fsig->param_count == 2)
10350 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10351 else if (fsig->param_count == 3)
10352 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10353 else if (fsig->param_count == 4)
10354 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10356 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10357 } else if (cmethod->string_ctor) {
10358 g_assert (!context_used);
10359 g_assert (!vtable_arg);
10360 /* we simply pass a null pointer */
10361 EMIT_NEW_PCONST (cfg, *sp, NULL);
10362 /* now call the string ctor */
10363 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10365 if (cmethod->klass->valuetype) {
10366 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10367 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10368 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10373 * The code generated by mini_emit_virtual_call () expects
10374 * iargs [0] to be a boxed instance, but luckily the vcall
10375 * will be transformed into a normal call there.
10377 } else if (context_used) {
10378 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10381 MonoVTable *vtable = NULL;
10383 if (!cfg->compile_aot)
10384 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10385 CHECK_TYPELOAD (cmethod->klass);
10388 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10389 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10390 * As a workaround, we call class cctors before allocating objects.
10392 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10393 emit_class_init (cfg, cmethod->klass);
10394 if (cfg->verbose_level > 2)
10395 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10396 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10399 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10402 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10405 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10407 /* Now call the actual ctor */
10408 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10409 CHECK_CFG_EXCEPTION;
10412 if (alloc == NULL) {
10414 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10415 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10423 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10424 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10427 case CEE_CASTCLASS:
10431 token = read32 (ip + 1);
10432 klass = mini_get_class (method, token, generic_context);
10433 CHECK_TYPELOAD (klass);
10434 if (sp [0]->type != STACK_OBJ)
10437 ins = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10438 CHECK_CFG_EXCEPTION;
10447 token = read32 (ip + 1);
10448 klass = mini_get_class (method, token, generic_context);
10449 CHECK_TYPELOAD (klass);
10450 if (sp [0]->type != STACK_OBJ)
10453 context_used = mini_class_check_context_used (cfg, klass);
10455 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10456 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10457 MonoInst *args [3];
10464 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10467 if (cfg->compile_aot) {
10468 idx = get_castclass_cache_idx (cfg);
10469 EMIT_NEW_AOTCONST (cfg, args [2], MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
10471 EMIT_NEW_PCONST (cfg, args [2], mono_domain_alloc0 (cfg->domain, sizeof (gpointer)));
10474 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
10477 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
10478 MonoMethod *mono_isinst;
10479 MonoInst *iargs [1];
10482 mono_isinst = mono_marshal_get_isinst (klass);
10483 iargs [0] = sp [0];
10485 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst),
10486 iargs, ip, cfg->real_offset, TRUE);
10487 CHECK_CFG_EXCEPTION;
10488 g_assert (costs > 0);
10491 cfg->real_offset += 5;
10495 inline_costs += costs;
10498 ins = handle_isinst (cfg, klass, *sp, context_used);
10499 CHECK_CFG_EXCEPTION;
10505 case CEE_UNBOX_ANY: {
10506 MonoInst *res, *addr;
10511 token = read32 (ip + 1);
10512 klass = mini_get_class (method, token, generic_context);
10513 CHECK_TYPELOAD (klass);
10515 mono_save_token_info (cfg, image, token, klass);
10517 context_used = mini_class_check_context_used (cfg, klass);
10519 if (mini_is_gsharedvt_klass (klass)) {
10520 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10522 } else if (generic_class_is_reference_type (cfg, klass)) {
10523 res = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10524 CHECK_CFG_EXCEPTION;
10525 } else if (mono_class_is_nullable (klass)) {
10526 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10528 addr = handle_unbox (cfg, klass, sp, context_used);
10530 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10541 MonoClass *enum_class;
10542 MonoMethod *has_flag;
10548 token = read32 (ip + 1);
10549 klass = mini_get_class (method, token, generic_context);
10550 CHECK_TYPELOAD (klass);
10552 mono_save_token_info (cfg, image, token, klass);
10554 context_used = mini_class_check_context_used (cfg, klass);
10556 if (generic_class_is_reference_type (cfg, klass)) {
10562 if (klass == mono_defaults.void_class)
10564 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10566 /* frequent check in generic code: box (struct), brtrue */
10571 * <push int/long ptr>
10574 * constrained. MyFlags
10575 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10577 * If we find this sequence and the operand types on box and constrained
10578 * are equal, we can emit a specialized instruction sequence instead of
10579 * the very slow HasFlag () call.
10581 if ((cfg->opt & MONO_OPT_INTRINS) &&
10582 /* Cheap checks first. */
10583 ip + 5 + 6 + 5 < end &&
10584 ip [5] == CEE_PREFIX1 &&
10585 ip [6] == CEE_CONSTRAINED_ &&
10586 ip [11] == CEE_CALLVIRT &&
10587 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10588 mono_class_is_enum (klass) &&
10589 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10590 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10591 has_flag->klass == mono_defaults.enum_class &&
10592 !strcmp (has_flag->name, "HasFlag") &&
10593 has_flag->signature->hasthis &&
10594 has_flag->signature->param_count == 1) {
10595 CHECK_TYPELOAD (enum_class);
10597 if (enum_class == klass) {
10598 MonoInst *enum_this, *enum_flag;
10603 enum_this = sp [0];
10604 enum_flag = sp [1];
10606 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10611 // FIXME: LLVM can't handle the inconsistent bb linking
10612 if (!mono_class_is_nullable (klass) &&
10613 !mini_is_gsharedvt_klass (klass) &&
10614 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10615 (ip [5] == CEE_BRTRUE ||
10616 ip [5] == CEE_BRTRUE_S ||
10617 ip [5] == CEE_BRFALSE ||
10618 ip [5] == CEE_BRFALSE_S)) {
10619 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10621 MonoBasicBlock *true_bb, *false_bb;
10625 if (cfg->verbose_level > 3) {
10626 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10627 printf ("<box+brtrue opt>\n");
10632 case CEE_BRFALSE_S:
10635 target = ip + 1 + (signed char)(*ip);
10642 target = ip + 4 + (gint)(read32 (ip));
10646 g_assert_not_reached ();
10650 * We need to link both bblocks, since it is needed for handling stack
10651 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10652 * Branching to only one of them would lead to inconsistencies, so
10653 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10655 GET_BBLOCK (cfg, true_bb, target);
10656 GET_BBLOCK (cfg, false_bb, ip);
10658 mono_link_bblock (cfg, cfg->cbb, true_bb);
10659 mono_link_bblock (cfg, cfg->cbb, false_bb);
10661 if (sp != stack_start) {
10662 handle_stack_args (cfg, stack_start, sp - stack_start);
10664 CHECK_UNVERIFIABLE (cfg);
10667 if (COMPILE_LLVM (cfg)) {
10668 dreg = alloc_ireg (cfg);
10669 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10670 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10672 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10674 /* The JIT can't eliminate the iconst+compare */
10675 MONO_INST_NEW (cfg, ins, OP_BR);
10676 ins->inst_target_bb = is_true ? true_bb : false_bb;
10677 MONO_ADD_INS (cfg->cbb, ins);
10680 start_new_bblock = 1;
10684 *sp++ = handle_box (cfg, val, klass, context_used);
10686 CHECK_CFG_EXCEPTION;
10695 token = read32 (ip + 1);
10696 klass = mini_get_class (method, token, generic_context);
10697 CHECK_TYPELOAD (klass);
10699 mono_save_token_info (cfg, image, token, klass);
10701 context_used = mini_class_check_context_used (cfg, klass);
10703 if (mono_class_is_nullable (klass)) {
10706 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10707 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10711 ins = handle_unbox (cfg, klass, sp, context_used);
10724 MonoClassField *field;
10725 #ifndef DISABLE_REMOTING
10729 gboolean is_instance;
10731 gpointer addr = NULL;
10732 gboolean is_special_static;
10734 MonoInst *store_val = NULL;
10735 MonoInst *thread_ins;
10738 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10740 if (op == CEE_STFLD) {
10743 store_val = sp [1];
10748 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10750 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10753 if (op == CEE_STSFLD) {
10756 store_val = sp [0];
10761 token = read32 (ip + 1);
10762 if (method->wrapper_type != MONO_WRAPPER_NONE) {
10763 field = mono_method_get_wrapper_data (method, token);
10764 klass = field->parent;
10767 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10770 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10771 FIELD_ACCESS_FAILURE (method, field);
10772 mono_class_init (klass);
10774 /* if the class is Critical then transparent code cannot access it's fields */
10775 if (!is_instance && mono_security_core_clr_enabled ())
10776 ensure_method_is_allowed_to_access_field (cfg, method, field);
10778 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10779 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
10780 if (mono_security_core_clr_enabled ())
10781 ensure_method_is_allowed_to_access_field (cfg, method, field);
10784 ftype = mono_field_get_type (field);
10787 * LDFLD etc. is usable on static fields as well, so convert those cases to
10790 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10802 g_assert_not_reached ();
10804 is_instance = FALSE;
10807 context_used = mini_class_check_context_used (cfg, klass);
10809 /* INSTANCE CASE */
10811 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10812 if (op == CEE_STFLD) {
10813 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10815 #ifndef DISABLE_REMOTING
10816 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10817 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
10818 MonoInst *iargs [5];
10820 GSHAREDVT_FAILURE (op);
10822 iargs [0] = sp [0];
10823 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10824 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10825 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
10827 iargs [4] = sp [1];
10829 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10830 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
10831 iargs, ip, cfg->real_offset, TRUE);
10832 CHECK_CFG_EXCEPTION;
10833 g_assert (costs > 0);
10835 cfg->real_offset += 5;
10837 inline_costs += costs;
10839 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10846 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10848 if (mini_is_gsharedvt_klass (klass)) {
10849 MonoInst *offset_ins;
10851 context_used = mini_class_check_context_used (cfg, klass);
10853 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10854 dreg = alloc_ireg_mp (cfg);
10855 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10856 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
10857 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10859 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10861 if (sp [0]->opcode != OP_LDADDR)
10862 store->flags |= MONO_INST_FAULT;
10864 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)) {
10865 /* insert call to write barrier */
10869 dreg = alloc_ireg_mp (cfg);
10870 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10871 emit_write_barrier (cfg, ptr, sp [1]);
10874 store->flags |= ins_flag;
10881 #ifndef DISABLE_REMOTING
10882 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10883 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
10884 MonoInst *iargs [4];
10886 GSHAREDVT_FAILURE (op);
10888 iargs [0] = sp [0];
10889 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10890 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10891 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10892 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10893 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
10894 iargs, ip, cfg->real_offset, TRUE);
10895 CHECK_CFG_EXCEPTION;
10896 g_assert (costs > 0);
10898 cfg->real_offset += 5;
10902 inline_costs += costs;
10904 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10910 if (sp [0]->type == STACK_VTYPE) {
10913 /* Have to compute the address of the variable */
10915 var = get_vreg_to_inst (cfg, sp [0]->dreg);
10917 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10919 g_assert (var->klass == klass);
10921 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10925 if (op == CEE_LDFLDA) {
10926 if (sp [0]->type == STACK_OBJ) {
10927 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10928 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10931 dreg = alloc_ireg_mp (cfg);
10933 if (mini_is_gsharedvt_klass (klass)) {
10934 MonoInst *offset_ins;
10936 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10937 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10939 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10941 ins->klass = mono_class_from_mono_type (field->type);
10942 ins->type = STACK_MP;
10947 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10949 if (mini_is_gsharedvt_klass (klass)) {
10950 MonoInst *offset_ins;
10952 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10953 dreg = alloc_ireg_mp (cfg);
10954 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10955 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
10957 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
10959 load->flags |= ins_flag;
10960 if (sp [0]->opcode != OP_LDADDR)
10961 load->flags |= MONO_INST_FAULT;
10973 context_used = mini_class_check_context_used (cfg, klass);
10975 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL)
10978 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10979 * to be called here.
10981 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10982 mono_class_vtable (cfg->domain, klass);
10983 CHECK_TYPELOAD (klass);
10985 mono_domain_lock (cfg->domain);
10986 if (cfg->domain->special_static_fields)
10987 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10988 mono_domain_unlock (cfg->domain);
10990 is_special_static = mono_class_field_is_special_static (field);
10992 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10993 thread_ins = mono_get_thread_intrinsic (cfg);
10997 /* Generate IR to compute the field address */
10998 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11000 * Fast access to TLS data
11001 * Inline version of get_thread_static_data () in
11005 int idx, static_data_reg, array_reg, dreg;
11007 GSHAREDVT_FAILURE (op);
11009 MONO_ADD_INS (cfg->cbb, thread_ins);
11010 static_data_reg = alloc_ireg (cfg);
11011 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11013 if (cfg->compile_aot) {
11014 int offset_reg, offset2_reg, idx_reg;
11016 /* For TLS variables, this will return the TLS offset */
11017 EMIT_NEW_SFLDACONST (cfg, ins, field);
11018 offset_reg = ins->dreg;
11019 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11020 idx_reg = alloc_ireg (cfg);
11021 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11022 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11023 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11024 array_reg = alloc_ireg (cfg);
11025 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11026 offset2_reg = alloc_ireg (cfg);
11027 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11028 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11029 dreg = alloc_ireg (cfg);
11030 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11032 offset = (gsize)addr & 0x7fffffff;
11033 idx = offset & 0x3f;
11035 array_reg = alloc_ireg (cfg);
11036 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11037 dreg = alloc_ireg (cfg);
11038 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11040 } else if ((cfg->opt & MONO_OPT_SHARED) ||
11041 (cfg->compile_aot && is_special_static) ||
11042 (context_used && is_special_static)) {
11043 MonoInst *iargs [2];
11045 g_assert (field->parent);
11046 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11047 if (context_used) {
11048 iargs [1] = emit_get_rgctx_field (cfg, context_used,
11049 field, MONO_RGCTX_INFO_CLASS_FIELD);
11051 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11053 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11054 } else if (context_used) {
11055 MonoInst *static_data;
11058 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11059 method->klass->name_space, method->klass->name, method->name,
11060 depth, field->offset);
11063 if (mono_class_needs_cctor_run (klass, method))
11064 emit_class_init (cfg, klass);
11067 * The pointer we're computing here is
11069 * super_info.static_data + field->offset
11071 static_data = emit_get_rgctx_klass (cfg, context_used,
11072 klass, MONO_RGCTX_INFO_STATIC_DATA);
11074 if (mini_is_gsharedvt_klass (klass)) {
11075 MonoInst *offset_ins;
11077 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11078 dreg = alloc_ireg_mp (cfg);
11079 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11080 } else if (field->offset == 0) {
11083 int addr_reg = mono_alloc_preg (cfg);
11084 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11086 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11087 MonoInst *iargs [2];
11089 g_assert (field->parent);
11090 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11091 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11092 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11094 MonoVTable *vtable = NULL;
11096 if (!cfg->compile_aot)
11097 vtable = mono_class_vtable (cfg->domain, klass);
11098 CHECK_TYPELOAD (klass);
11101 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11102 if (!(g_slist_find (class_inits, klass))) {
11103 emit_class_init (cfg, klass);
11104 if (cfg->verbose_level > 2)
11105 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11106 class_inits = g_slist_prepend (class_inits, klass);
11109 if (cfg->run_cctors) {
11111 /* This makes so that inline cannot trigger */
11112 /* .cctors: too many apps depend on them */
11113 /* running with a specific order... */
11115 if (! vtable->initialized)
11116 INLINE_FAILURE ("class init");
11117 ex = mono_runtime_class_init_full (vtable, FALSE);
11119 set_exception_object (cfg, ex);
11120 goto exception_exit;
11124 if (cfg->compile_aot)
11125 EMIT_NEW_SFLDACONST (cfg, ins, field);
11128 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11130 EMIT_NEW_PCONST (cfg, ins, addr);
11133 MonoInst *iargs [1];
11134 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11135 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11139 /* Generate IR to do the actual load/store operation */
11141 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11142 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11143 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11146 if (op == CEE_LDSFLDA) {
11147 ins->klass = mono_class_from_mono_type (ftype);
11148 ins->type = STACK_PTR;
11150 } else if (op == CEE_STSFLD) {
11153 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11154 store->flags |= ins_flag;
11156 gboolean is_const = FALSE;
11157 MonoVTable *vtable = NULL;
11158 gpointer addr = NULL;
11160 if (!context_used) {
11161 vtable = mono_class_vtable (cfg->domain, klass);
11162 CHECK_TYPELOAD (klass);
11164 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11165 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11166 int ro_type = ftype->type;
11168 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11169 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11170 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11173 GSHAREDVT_FAILURE (op);
11175 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11178 case MONO_TYPE_BOOLEAN:
11180 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11184 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11187 case MONO_TYPE_CHAR:
11189 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11193 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11198 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11202 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11207 case MONO_TYPE_PTR:
11208 case MONO_TYPE_FNPTR:
11209 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11210 type_to_eval_stack_type ((cfg), field->type, *sp);
11213 case MONO_TYPE_STRING:
11214 case MONO_TYPE_OBJECT:
11215 case MONO_TYPE_CLASS:
11216 case MONO_TYPE_SZARRAY:
11217 case MONO_TYPE_ARRAY:
11218 if (!mono_gc_is_moving ()) {
11219 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11220 type_to_eval_stack_type ((cfg), field->type, *sp);
11228 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11233 case MONO_TYPE_VALUETYPE:
11243 CHECK_STACK_OVF (1);
11245 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11246 load->flags |= ins_flag;
11252 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11253 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11254 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11265 token = read32 (ip + 1);
11266 klass = mini_get_class (method, token, generic_context);
11267 CHECK_TYPELOAD (klass);
11268 if (ins_flag & MONO_INST_VOLATILE) {
11269 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11270 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11272 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11273 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11274 ins->flags |= ins_flag;
11275 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11276 generic_class_is_reference_type (cfg, klass)) {
11277 /* insert call to write barrier */
11278 emit_write_barrier (cfg, sp [0], sp [1]);
11290 const char *data_ptr;
11292 guint32 field_token;
11298 token = read32 (ip + 1);
11300 klass = mini_get_class (method, token, generic_context);
11301 CHECK_TYPELOAD (klass);
11303 context_used = mini_class_check_context_used (cfg, klass);
11305 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11306 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11307 ins->sreg1 = sp [0]->dreg;
11308 ins->type = STACK_I4;
11309 ins->dreg = alloc_ireg (cfg);
11310 MONO_ADD_INS (cfg->cbb, ins);
11311 *sp = mono_decompose_opcode (cfg, ins);
11314 if (context_used) {
11315 MonoInst *args [3];
11316 MonoClass *array_class = mono_array_class_get (klass, 1);
11317 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11319 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11322 args [0] = emit_get_rgctx_klass (cfg, context_used,
11323 array_class, MONO_RGCTX_INFO_VTABLE);
11328 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11330 ins = mono_emit_jit_icall (cfg, mono_array_new_specific, args);
11332 if (cfg->opt & MONO_OPT_SHARED) {
11333 /* Decompose now to avoid problems with references to the domainvar */
11334 MonoInst *iargs [3];
11336 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11337 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11338 iargs [2] = sp [0];
11340 ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
11342 /* Decompose later since it is needed by abcrem */
11343 MonoClass *array_type = mono_array_class_get (klass, 1);
11344 mono_class_vtable (cfg->domain, array_type);
11345 CHECK_TYPELOAD (array_type);
11347 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11348 ins->dreg = alloc_ireg_ref (cfg);
11349 ins->sreg1 = sp [0]->dreg;
11350 ins->inst_newa_class = klass;
11351 ins->type = STACK_OBJ;
11352 ins->klass = array_type;
11353 MONO_ADD_INS (cfg->cbb, ins);
11354 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11355 cfg->cbb->has_array_access = TRUE;
11357 /* Needed so mono_emit_load_get_addr () gets called */
11358 mono_get_got_var (cfg);
11368 * we inline/optimize the initialization sequence if possible.
11369 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11370 * for small sizes open code the memcpy
11371 * ensure the rva field is big enough
11373 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))) {
11374 MonoMethod *memcpy_method = get_memcpy_method ();
11375 MonoInst *iargs [3];
11376 int add_reg = alloc_ireg_mp (cfg);
11378 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11379 if (cfg->compile_aot) {
11380 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11382 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11384 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11385 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11394 if (sp [0]->type != STACK_OBJ)
11397 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11398 ins->dreg = alloc_preg (cfg);
11399 ins->sreg1 = sp [0]->dreg;
11400 ins->type = STACK_I4;
11401 /* This flag will be inherited by the decomposition */
11402 ins->flags |= MONO_INST_FAULT;
11403 MONO_ADD_INS (cfg->cbb, ins);
11404 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11405 cfg->cbb->has_array_access = TRUE;
11413 if (sp [0]->type != STACK_OBJ)
11416 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11418 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11419 CHECK_TYPELOAD (klass);
11420 /* we need to make sure that this array is exactly the type it needs
11421 * to be for correctness. the wrappers are lax with their usage
11422 * so we need to ignore them here
11424 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11425 MonoClass *array_class = mono_array_class_get (klass, 1);
11426 mini_emit_check_array_type (cfg, sp [0], array_class);
11427 CHECK_TYPELOAD (array_class);
11431 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11436 case CEE_LDELEM_I1:
11437 case CEE_LDELEM_U1:
11438 case CEE_LDELEM_I2:
11439 case CEE_LDELEM_U2:
11440 case CEE_LDELEM_I4:
11441 case CEE_LDELEM_U4:
11442 case CEE_LDELEM_I8:
11444 case CEE_LDELEM_R4:
11445 case CEE_LDELEM_R8:
11446 case CEE_LDELEM_REF: {
11452 if (*ip == CEE_LDELEM) {
11454 token = read32 (ip + 1);
11455 klass = mini_get_class (method, token, generic_context);
11456 CHECK_TYPELOAD (klass);
11457 mono_class_init (klass);
11460 klass = array_access_to_klass (*ip);
11462 if (sp [0]->type != STACK_OBJ)
11465 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11467 if (mini_is_gsharedvt_variable_klass (klass)) {
11468 // FIXME-VT: OP_ICONST optimization
11469 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11470 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11471 ins->opcode = OP_LOADV_MEMBASE;
11472 } else if (sp [1]->opcode == OP_ICONST) {
11473 int array_reg = sp [0]->dreg;
11474 int index_reg = sp [1]->dreg;
11475 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11477 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11478 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11480 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11481 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11484 if (*ip == CEE_LDELEM)
11491 case CEE_STELEM_I1:
11492 case CEE_STELEM_I2:
11493 case CEE_STELEM_I4:
11494 case CEE_STELEM_I8:
11495 case CEE_STELEM_R4:
11496 case CEE_STELEM_R8:
11497 case CEE_STELEM_REF:
11502 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11504 if (*ip == CEE_STELEM) {
11506 token = read32 (ip + 1);
11507 klass = mini_get_class (method, token, generic_context);
11508 CHECK_TYPELOAD (klass);
11509 mono_class_init (klass);
11512 klass = array_access_to_klass (*ip);
11514 if (sp [0]->type != STACK_OBJ)
11517 emit_array_store (cfg, klass, sp, TRUE);
11519 if (*ip == CEE_STELEM)
11526 case CEE_CKFINITE: {
11530 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11531 ins->sreg1 = sp [0]->dreg;
11532 ins->dreg = alloc_freg (cfg);
11533 ins->type = STACK_R8;
11534 MONO_ADD_INS (cfg->cbb, ins);
11536 *sp++ = mono_decompose_opcode (cfg, ins);
11541 case CEE_REFANYVAL: {
11542 MonoInst *src_var, *src;
11544 int klass_reg = alloc_preg (cfg);
11545 int dreg = alloc_preg (cfg);
11547 GSHAREDVT_FAILURE (*ip);
11550 MONO_INST_NEW (cfg, ins, *ip);
11553 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11554 CHECK_TYPELOAD (klass);
11556 context_used = mini_class_check_context_used (cfg, klass);
11559 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11561 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11562 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11563 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11565 if (context_used) {
11566 MonoInst *klass_ins;
11568 klass_ins = emit_get_rgctx_klass (cfg, context_used,
11569 klass, MONO_RGCTX_INFO_KLASS);
11572 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11573 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11575 mini_emit_class_check (cfg, klass_reg, klass);
11577 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11578 ins->type = STACK_MP;
11579 ins->klass = klass;
11584 case CEE_MKREFANY: {
11585 MonoInst *loc, *addr;
11587 GSHAREDVT_FAILURE (*ip);
11590 MONO_INST_NEW (cfg, ins, *ip);
11593 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11594 CHECK_TYPELOAD (klass);
11596 context_used = mini_class_check_context_used (cfg, klass);
11598 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11599 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11601 if (context_used) {
11602 MonoInst *const_ins;
11603 int type_reg = alloc_preg (cfg);
11605 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11606 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11607 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11608 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11609 } else if (cfg->compile_aot) {
11610 int const_reg = alloc_preg (cfg);
11611 int type_reg = alloc_preg (cfg);
11613 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11614 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11615 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11616 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11618 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
11619 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
11621 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11623 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11624 ins->type = STACK_VTYPE;
11625 ins->klass = mono_defaults.typed_reference_class;
11630 case CEE_LDTOKEN: {
11632 MonoClass *handle_class;
11634 CHECK_STACK_OVF (1);
11637 n = read32 (ip + 1);
11639 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11640 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11641 handle = mono_method_get_wrapper_data (method, n);
11642 handle_class = mono_method_get_wrapper_data (method, n + 1);
11643 if (handle_class == mono_defaults.typehandle_class)
11644 handle = &((MonoClass*)handle)->byval_arg;
11647 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11652 mono_class_init (handle_class);
11653 if (cfg->gshared) {
11654 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11655 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11656 /* This case handles ldtoken
11657 of an open type, like for
11660 } else if (handle_class == mono_defaults.typehandle_class) {
11661 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type (handle));
11662 } else if (handle_class == mono_defaults.fieldhandle_class)
11663 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11664 else if (handle_class == mono_defaults.methodhandle_class)
11665 context_used = mini_method_check_context_used (cfg, handle);
11667 g_assert_not_reached ();
11670 if ((cfg->opt & MONO_OPT_SHARED) &&
11671 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11672 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11673 MonoInst *addr, *vtvar, *iargs [3];
11674 int method_context_used;
11676 method_context_used = mini_method_check_context_used (cfg, method);
11678 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11680 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11681 EMIT_NEW_ICONST (cfg, iargs [1], n);
11682 if (method_context_used) {
11683 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11684 method, MONO_RGCTX_INFO_METHOD);
11685 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11687 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11688 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11690 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11692 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11694 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11696 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11697 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
11698 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11699 (cmethod->klass == mono_defaults.systemtype_class) &&
11700 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11701 MonoClass *tclass = mono_class_from_mono_type (handle);
11703 mono_class_init (tclass);
11704 if (context_used) {
11705 ins = emit_get_rgctx_klass (cfg, context_used,
11706 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11707 } else if (cfg->compile_aot) {
11708 if (method->wrapper_type) {
11709 mono_error_init (&error); //got to do it since there are multiple conditionals below
11710 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11711 /* Special case for static synchronized wrappers */
11712 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11714 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11715 /* FIXME: n is not a normal token */
11717 EMIT_NEW_PCONST (cfg, ins, NULL);
11720 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11723 EMIT_NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
11725 ins->type = STACK_OBJ;
11726 ins->klass = cmethod->klass;
11729 MonoInst *addr, *vtvar;
11731 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11733 if (context_used) {
11734 if (handle_class == mono_defaults.typehandle_class) {
11735 ins = emit_get_rgctx_klass (cfg, context_used,
11736 mono_class_from_mono_type (handle),
11737 MONO_RGCTX_INFO_TYPE);
11738 } else if (handle_class == mono_defaults.methodhandle_class) {
11739 ins = emit_get_rgctx_method (cfg, context_used,
11740 handle, MONO_RGCTX_INFO_METHOD);
11741 } else if (handle_class == mono_defaults.fieldhandle_class) {
11742 ins = emit_get_rgctx_field (cfg, context_used,
11743 handle, MONO_RGCTX_INFO_CLASS_FIELD);
11745 g_assert_not_reached ();
11747 } else if (cfg->compile_aot) {
11748 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11750 EMIT_NEW_PCONST (cfg, ins, handle);
11752 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11753 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11754 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11764 MONO_INST_NEW (cfg, ins, OP_THROW);
11766 ins->sreg1 = sp [0]->dreg;
11768 cfg->cbb->out_of_line = TRUE;
11769 MONO_ADD_INS (cfg->cbb, ins);
11770 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11771 MONO_ADD_INS (cfg->cbb, ins);
11774 link_bblock (cfg, cfg->cbb, end_bblock);
11775 start_new_bblock = 1;
11777 case CEE_ENDFINALLY:
11778 /* mono_save_seq_point_info () depends on this */
11779 if (sp != stack_start)
11780 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11781 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11782 MONO_ADD_INS (cfg->cbb, ins);
11784 start_new_bblock = 1;
11787 * Control will leave the method so empty the stack, otherwise
11788 * the next basic block will start with a nonempty stack.
11790 while (sp != stack_start) {
11795 case CEE_LEAVE_S: {
11798 if (*ip == CEE_LEAVE) {
11800 target = ip + 5 + (gint32)read32(ip + 1);
11803 target = ip + 2 + (signed char)(ip [1]);
11806 /* empty the stack */
11807 while (sp != stack_start) {
11812 * If this leave statement is in a catch block, check for a
11813 * pending exception, and rethrow it if necessary.
11814 * We avoid doing this in runtime invoke wrappers, since those are called
11815 * by native code which excepts the wrapper to catch all exceptions.
11817 for (i = 0; i < header->num_clauses; ++i) {
11818 MonoExceptionClause *clause = &header->clauses [i];
11821 * Use <= in the final comparison to handle clauses with multiple
11822 * leave statements, like in bug #78024.
11823 * The ordering of the exception clauses guarantees that we find the
11824 * innermost clause.
11826 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) {
11828 MonoBasicBlock *dont_throw;
11833 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11836 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11838 NEW_BBLOCK (cfg, dont_throw);
11841 * Currently, we always rethrow the abort exception, despite the
11842 * fact that this is not correct. See thread6.cs for an example.
11843 * But propagating the abort exception is more important than
11844 * getting the sematics right.
11846 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11847 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11848 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11850 MONO_START_BB (cfg, dont_throw);
11854 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11856 MonoExceptionClause *clause;
11858 for (tmp = handlers; tmp; tmp = tmp->next) {
11859 clause = tmp->data;
11860 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11862 link_bblock (cfg, cfg->cbb, tblock);
11863 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11864 ins->inst_target_bb = tblock;
11865 ins->inst_eh_block = clause;
11866 MONO_ADD_INS (cfg->cbb, ins);
11867 cfg->cbb->has_call_handler = 1;
11868 if (COMPILE_LLVM (cfg)) {
11869 MonoBasicBlock *target_bb;
11872 * Link the finally bblock with the target, since it will
11873 * conceptually branch there.
11874 * FIXME: Have to link the bblock containing the endfinally.
11876 GET_BBLOCK (cfg, target_bb, target);
11877 link_bblock (cfg, tblock, target_bb);
11880 g_list_free (handlers);
11883 MONO_INST_NEW (cfg, ins, OP_BR);
11884 MONO_ADD_INS (cfg->cbb, ins);
11885 GET_BBLOCK (cfg, tblock, target);
11886 link_bblock (cfg, cfg->cbb, tblock);
11887 ins->inst_target_bb = tblock;
11888 start_new_bblock = 1;
11890 if (*ip == CEE_LEAVE)
11899 * Mono specific opcodes
11901 case MONO_CUSTOM_PREFIX: {
11903 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11907 case CEE_MONO_ICALL: {
11909 MonoJitICallInfo *info;
11911 token = read32 (ip + 2);
11912 func = mono_method_get_wrapper_data (method, token);
11913 info = mono_find_jit_icall_by_addr (func);
11915 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11918 CHECK_STACK (info->sig->param_count);
11919 sp -= info->sig->param_count;
11921 ins = mono_emit_jit_icall (cfg, info->func, sp);
11922 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11926 inline_costs += 10 * num_calls++;
11930 case CEE_MONO_LDPTR_CARD_TABLE: {
11932 gpointer card_mask;
11933 CHECK_STACK_OVF (1);
11935 if (cfg->compile_aot)
11936 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11938 EMIT_NEW_PCONST (cfg, ins, mono_gc_get_card_table (&shift_bits, &card_mask));
11942 inline_costs += 10 * num_calls++;
11945 case CEE_MONO_LDPTR_NURSERY_START: {
11948 CHECK_STACK_OVF (1);
11950 if (cfg->compile_aot)
11951 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11953 EMIT_NEW_PCONST (cfg, ins, mono_gc_get_nursery (&shift_bits, &size));
11957 inline_costs += 10 * num_calls++;
11960 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
11961 CHECK_STACK_OVF (1);
11963 if (cfg->compile_aot)
11964 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11966 EMIT_NEW_PCONST (cfg, ins, mono_thread_interruption_request_flag ());
11970 inline_costs += 10 * num_calls++;
11973 case CEE_MONO_LDPTR: {
11976 CHECK_STACK_OVF (1);
11978 token = read32 (ip + 2);
11980 ptr = mono_method_get_wrapper_data (method, token);
11981 EMIT_NEW_PCONST (cfg, ins, ptr);
11984 inline_costs += 10 * num_calls++;
11985 /* Can't embed random pointers into AOT code */
11989 case CEE_MONO_JIT_ICALL_ADDR: {
11990 MonoJitICallInfo *callinfo;
11993 CHECK_STACK_OVF (1);
11995 token = read32 (ip + 2);
11997 ptr = mono_method_get_wrapper_data (method, token);
11998 callinfo = mono_find_jit_icall_by_addr (ptr);
11999 g_assert (callinfo);
12000 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12003 inline_costs += 10 * num_calls++;
12006 case CEE_MONO_ICALL_ADDR: {
12007 MonoMethod *cmethod;
12010 CHECK_STACK_OVF (1);
12012 token = read32 (ip + 2);
12014 cmethod = mono_method_get_wrapper_data (method, token);
12016 if (cfg->compile_aot) {
12017 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12019 ptr = mono_lookup_internal_call (cmethod);
12021 EMIT_NEW_PCONST (cfg, ins, ptr);
12027 case CEE_MONO_VTADDR: {
12028 MonoInst *src_var, *src;
12034 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12035 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12040 case CEE_MONO_NEWOBJ: {
12041 MonoInst *iargs [2];
12043 CHECK_STACK_OVF (1);
12045 token = read32 (ip + 2);
12046 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12047 mono_class_init (klass);
12048 NEW_DOMAINCONST (cfg, iargs [0]);
12049 MONO_ADD_INS (cfg->cbb, iargs [0]);
12050 NEW_CLASSCONST (cfg, iargs [1], klass);
12051 MONO_ADD_INS (cfg->cbb, iargs [1]);
12052 *sp++ = mono_emit_jit_icall (cfg, mono_object_new, iargs);
12054 inline_costs += 10 * num_calls++;
12057 case CEE_MONO_OBJADDR:
12060 MONO_INST_NEW (cfg, ins, OP_MOVE);
12061 ins->dreg = alloc_ireg_mp (cfg);
12062 ins->sreg1 = sp [0]->dreg;
12063 ins->type = STACK_MP;
12064 MONO_ADD_INS (cfg->cbb, ins);
12068 case CEE_MONO_LDNATIVEOBJ:
12070 * Similar to LDOBJ, but instead load the unmanaged
12071 * representation of the vtype to the stack.
12076 token = read32 (ip + 2);
12077 klass = mono_method_get_wrapper_data (method, token);
12078 g_assert (klass->valuetype);
12079 mono_class_init (klass);
12082 MonoInst *src, *dest, *temp;
12085 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12086 temp->backend.is_pinvoke = 1;
12087 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12088 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12090 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12091 dest->type = STACK_VTYPE;
12092 dest->klass = klass;
12098 case CEE_MONO_RETOBJ: {
12100 * Same as RET, but return the native representation of a vtype
12103 g_assert (cfg->ret);
12104 g_assert (mono_method_signature (method)->pinvoke);
12109 token = read32 (ip + 2);
12110 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12112 if (!cfg->vret_addr) {
12113 g_assert (cfg->ret_var_is_local);
12115 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12117 EMIT_NEW_RETLOADA (cfg, ins);
12119 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12121 if (sp != stack_start)
12124 MONO_INST_NEW (cfg, ins, OP_BR);
12125 ins->inst_target_bb = end_bblock;
12126 MONO_ADD_INS (cfg->cbb, ins);
12127 link_bblock (cfg, cfg->cbb, end_bblock);
12128 start_new_bblock = 1;
12132 case CEE_MONO_CISINST:
12133 case CEE_MONO_CCASTCLASS: {
12138 token = read32 (ip + 2);
12139 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12140 if (ip [1] == CEE_MONO_CISINST)
12141 ins = handle_cisinst (cfg, klass, sp [0]);
12143 ins = handle_ccastclass (cfg, klass, sp [0]);
12148 case CEE_MONO_SAVE_LMF:
12149 case CEE_MONO_RESTORE_LMF:
12150 #ifdef MONO_ARCH_HAVE_LMF_OPS
12151 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
12152 MONO_ADD_INS (cfg->cbb, ins);
12153 cfg->need_lmf_area = TRUE;
12157 case CEE_MONO_CLASSCONST:
12158 CHECK_STACK_OVF (1);
12160 token = read32 (ip + 2);
12161 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12164 inline_costs += 10 * num_calls++;
12166 case CEE_MONO_NOT_TAKEN:
12167 cfg->cbb->out_of_line = TRUE;
12170 case CEE_MONO_TLS: {
12173 CHECK_STACK_OVF (1);
12175 key = (gint32)read32 (ip + 2);
12176 g_assert (key < TLS_KEY_NUM);
12178 ins = mono_create_tls_get (cfg, key);
12180 if (cfg->compile_aot) {
12182 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12183 ins->dreg = alloc_preg (cfg);
12184 ins->type = STACK_PTR;
12186 g_assert_not_reached ();
12189 ins->type = STACK_PTR;
12190 MONO_ADD_INS (cfg->cbb, ins);
12195 case CEE_MONO_DYN_CALL: {
12196 MonoCallInst *call;
12198 /* It would be easier to call a trampoline, but that would put an
12199 * extra frame on the stack, confusing exception handling. So
12200 * implement it inline using an opcode for now.
12203 if (!cfg->dyn_call_var) {
12204 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12205 /* prevent it from being register allocated */
12206 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12209 /* Has to use a call inst since it local regalloc expects it */
12210 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12211 ins = (MonoInst*)call;
12213 ins->sreg1 = sp [0]->dreg;
12214 ins->sreg2 = sp [1]->dreg;
12215 MONO_ADD_INS (cfg->cbb, ins);
12217 cfg->param_area = MAX (cfg->param_area, MONO_ARCH_DYN_CALL_PARAM_AREA);
12220 inline_costs += 10 * num_calls++;
12224 case CEE_MONO_MEMORY_BARRIER: {
12226 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12230 case CEE_MONO_JIT_ATTACH: {
12231 MonoInst *args [16], *domain_ins;
12232 MonoInst *ad_ins, *jit_tls_ins;
12233 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12235 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12237 EMIT_NEW_PCONST (cfg, ins, NULL);
12238 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12240 ad_ins = mono_get_domain_intrinsic (cfg);
12241 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12243 if (MONO_ARCH_HAVE_TLS_GET && ad_ins && jit_tls_ins) {
12244 NEW_BBLOCK (cfg, next_bb);
12245 NEW_BBLOCK (cfg, call_bb);
12247 if (cfg->compile_aot) {
12248 /* AOT code is only used in the root domain */
12249 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12251 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12253 MONO_ADD_INS (cfg->cbb, ad_ins);
12254 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12255 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12257 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12258 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12259 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12261 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12262 MONO_START_BB (cfg, call_bb);
12265 if (cfg->compile_aot) {
12266 /* AOT code is only used in the root domain */
12267 EMIT_NEW_PCONST (cfg, args [0], NULL);
12269 EMIT_NEW_PCONST (cfg, args [0], cfg->domain);
12271 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12272 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12275 MONO_START_BB (cfg, next_bb);
12279 case CEE_MONO_JIT_DETACH: {
12280 MonoInst *args [16];
12282 /* Restore the original domain */
12283 dreg = alloc_ireg (cfg);
12284 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12285 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12290 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12296 case CEE_PREFIX1: {
12299 case CEE_ARGLIST: {
12300 /* somewhat similar to LDTOKEN */
12301 MonoInst *addr, *vtvar;
12302 CHECK_STACK_OVF (1);
12303 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12305 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12306 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12308 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12309 ins->type = STACK_VTYPE;
12310 ins->klass = mono_defaults.argumenthandle_class;
12320 MonoInst *cmp, *arg1, *arg2;
12328 * The following transforms:
12329 * CEE_CEQ into OP_CEQ
12330 * CEE_CGT into OP_CGT
12331 * CEE_CGT_UN into OP_CGT_UN
12332 * CEE_CLT into OP_CLT
12333 * CEE_CLT_UN into OP_CLT_UN
12335 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12337 MONO_INST_NEW (cfg, ins, cmp->opcode);
12338 cmp->sreg1 = arg1->dreg;
12339 cmp->sreg2 = arg2->dreg;
12340 type_from_op (cfg, cmp, arg1, arg2);
12342 add_widen_op (cfg, cmp, &arg1, &arg2);
12343 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12344 cmp->opcode = OP_LCOMPARE;
12345 else if (arg1->type == STACK_R4)
12346 cmp->opcode = OP_RCOMPARE;
12347 else if (arg1->type == STACK_R8)
12348 cmp->opcode = OP_FCOMPARE;
12350 cmp->opcode = OP_ICOMPARE;
12351 MONO_ADD_INS (cfg->cbb, cmp);
12352 ins->type = STACK_I4;
12353 ins->dreg = alloc_dreg (cfg, ins->type);
12354 type_from_op (cfg, ins, arg1, arg2);
12356 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12358 * The backends expect the fceq opcodes to do the
12361 ins->sreg1 = cmp->sreg1;
12362 ins->sreg2 = cmp->sreg2;
12365 MONO_ADD_INS (cfg->cbb, ins);
12371 MonoInst *argconst;
12372 MonoMethod *cil_method;
12374 CHECK_STACK_OVF (1);
12376 n = read32 (ip + 2);
12377 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12378 if (!cmethod || mono_loader_get_last_error ())
12380 mono_class_init (cmethod->klass);
12382 mono_save_token_info (cfg, image, n, cmethod);
12384 context_used = mini_method_check_context_used (cfg, cmethod);
12386 cil_method = cmethod;
12387 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12388 METHOD_ACCESS_FAILURE (method, cil_method);
12390 if (mono_security_core_clr_enabled ())
12391 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12394 * Optimize the common case of ldftn+delegate creation
12396 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12397 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12398 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12399 MonoInst *target_ins, *handle_ins;
12400 MonoMethod *invoke;
12401 int invoke_context_used;
12403 invoke = mono_get_delegate_invoke (ctor_method->klass);
12404 if (!invoke || !mono_method_signature (invoke))
12407 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12409 target_ins = sp [-1];
12411 if (mono_security_core_clr_enabled ())
12412 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12414 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12415 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12416 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12417 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12418 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12422 /* FIXME: SGEN support */
12423 if (invoke_context_used == 0) {
12425 if (cfg->verbose_level > 3)
12426 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));
12427 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12430 CHECK_CFG_EXCEPTION;
12440 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12441 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12445 inline_costs += 10 * num_calls++;
12448 case CEE_LDVIRTFTN: {
12449 MonoInst *args [2];
12453 n = read32 (ip + 2);
12454 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12455 if (!cmethod || mono_loader_get_last_error ())
12457 mono_class_init (cmethod->klass);
12459 context_used = mini_method_check_context_used (cfg, cmethod);
12461 if (mono_security_core_clr_enabled ())
12462 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12465 * Optimize the common case of ldvirtftn+delegate creation
12467 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)) {
12468 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12469 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12470 MonoInst *target_ins, *handle_ins;
12471 MonoMethod *invoke;
12472 int invoke_context_used;
12473 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12475 invoke = mono_get_delegate_invoke (ctor_method->klass);
12476 if (!invoke || !mono_method_signature (invoke))
12479 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12481 target_ins = sp [-1];
12483 if (mono_security_core_clr_enabled ())
12484 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12486 /* FIXME: SGEN support */
12487 if (invoke_context_used == 0) {
12489 if (cfg->verbose_level > 3)
12490 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));
12491 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12494 CHECK_CFG_EXCEPTION;
12507 args [1] = emit_get_rgctx_method (cfg, context_used,
12508 cmethod, MONO_RGCTX_INFO_METHOD);
12511 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12513 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12516 inline_costs += 10 * num_calls++;
12520 CHECK_STACK_OVF (1);
12522 n = read16 (ip + 2);
12524 EMIT_NEW_ARGLOAD (cfg, ins, n);
12529 CHECK_STACK_OVF (1);
12531 n = read16 (ip + 2);
12533 NEW_ARGLOADA (cfg, ins, n);
12534 MONO_ADD_INS (cfg->cbb, ins);
12542 n = read16 (ip + 2);
12544 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12546 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12550 CHECK_STACK_OVF (1);
12552 n = read16 (ip + 2);
12554 EMIT_NEW_LOCLOAD (cfg, ins, n);
12559 unsigned char *tmp_ip;
12560 CHECK_STACK_OVF (1);
12562 n = read16 (ip + 2);
12565 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12571 EMIT_NEW_LOCLOADA (cfg, ins, n);
12580 n = read16 (ip + 2);
12582 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12584 emit_stloc_ir (cfg, sp, header, n);
12591 if (sp != stack_start)
12593 if (cfg->method != method)
12595 * Inlining this into a loop in a parent could lead to
12596 * stack overflows which is different behavior than the
12597 * non-inlined case, thus disable inlining in this case.
12599 INLINE_FAILURE("localloc");
12601 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12602 ins->dreg = alloc_preg (cfg);
12603 ins->sreg1 = sp [0]->dreg;
12604 ins->type = STACK_PTR;
12605 MONO_ADD_INS (cfg->cbb, ins);
12607 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12609 ins->flags |= MONO_INST_INIT;
12614 case CEE_ENDFILTER: {
12615 MonoExceptionClause *clause, *nearest;
12620 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
12622 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12623 ins->sreg1 = (*sp)->dreg;
12624 MONO_ADD_INS (cfg->cbb, ins);
12625 start_new_bblock = 1;
12629 for (cc = 0; cc < header->num_clauses; ++cc) {
12630 clause = &header->clauses [cc];
12631 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12632 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12633 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12636 g_assert (nearest);
12637 if ((ip - header->code) != nearest->handler_offset)
12642 case CEE_UNALIGNED_:
12643 ins_flag |= MONO_INST_UNALIGNED;
12644 /* FIXME: record alignment? we can assume 1 for now */
12648 case CEE_VOLATILE_:
12649 ins_flag |= MONO_INST_VOLATILE;
12653 ins_flag |= MONO_INST_TAILCALL;
12654 cfg->flags |= MONO_CFG_HAS_TAIL;
12655 /* Can't inline tail calls at this time */
12656 inline_costs += 100000;
12663 token = read32 (ip + 2);
12664 klass = mini_get_class (method, token, generic_context);
12665 CHECK_TYPELOAD (klass);
12666 if (generic_class_is_reference_type (cfg, klass))
12667 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12669 mini_emit_initobj (cfg, *sp, NULL, klass);
12673 case CEE_CONSTRAINED_:
12675 token = read32 (ip + 2);
12676 constrained_class = mini_get_class (method, token, generic_context);
12677 CHECK_TYPELOAD (constrained_class);
12681 case CEE_INITBLK: {
12682 MonoInst *iargs [3];
12686 /* Skip optimized paths for volatile operations. */
12687 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)) {
12688 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
12689 } 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)) {
12690 /* emit_memset only works when val == 0 */
12691 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
12694 iargs [0] = sp [0];
12695 iargs [1] = sp [1];
12696 iargs [2] = sp [2];
12697 if (ip [1] == CEE_CPBLK) {
12699 * FIXME: It's unclear whether we should be emitting both the acquire
12700 * and release barriers for cpblk. It is technically both a load and
12701 * store operation, so it seems like that's the sensible thing to do.
12703 * FIXME: We emit full barriers on both sides of the operation for
12704 * simplicity. We should have a separate atomic memcpy method instead.
12706 MonoMethod *memcpy_method = get_memcpy_method ();
12708 if (ins_flag & MONO_INST_VOLATILE)
12709 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12711 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
12712 call->flags |= ins_flag;
12714 if (ins_flag & MONO_INST_VOLATILE)
12715 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12717 MonoMethod *memset_method = get_memset_method ();
12718 if (ins_flag & MONO_INST_VOLATILE) {
12719 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
12720 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
12722 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
12723 call->flags |= ins_flag;
12734 ins_flag |= MONO_INST_NOTYPECHECK;
12736 ins_flag |= MONO_INST_NORANGECHECK;
12737 /* we ignore the no-nullcheck for now since we
12738 * really do it explicitly only when doing callvirt->call
12742 case CEE_RETHROW: {
12744 int handler_offset = -1;
12746 for (i = 0; i < header->num_clauses; ++i) {
12747 MonoExceptionClause *clause = &header->clauses [i];
12748 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12749 handler_offset = clause->handler_offset;
12754 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12756 if (handler_offset == -1)
12759 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12760 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12761 ins->sreg1 = load->dreg;
12762 MONO_ADD_INS (cfg->cbb, ins);
12764 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12765 MONO_ADD_INS (cfg->cbb, ins);
12768 link_bblock (cfg, cfg->cbb, end_bblock);
12769 start_new_bblock = 1;
12777 CHECK_STACK_OVF (1);
12779 token = read32 (ip + 2);
12780 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12781 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12784 val = mono_type_size (type, &ialign);
12786 MonoClass *klass = mini_get_class (method, token, generic_context);
12787 CHECK_TYPELOAD (klass);
12789 val = mono_type_size (&klass->byval_arg, &ialign);
12791 if (mini_is_gsharedvt_klass (klass))
12792 GSHAREDVT_FAILURE (*ip);
12794 EMIT_NEW_ICONST (cfg, ins, val);
12799 case CEE_REFANYTYPE: {
12800 MonoInst *src_var, *src;
12802 GSHAREDVT_FAILURE (*ip);
12808 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12810 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12811 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12812 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12817 case CEE_READONLY_:
12830 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12840 g_warning ("opcode 0x%02x not handled", *ip);
12844 if (start_new_bblock != 1)
12847 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12848 if (cfg->cbb->next_bb) {
12849 /* This could already be set because of inlining, #693905 */
12850 MonoBasicBlock *bb = cfg->cbb;
12852 while (bb->next_bb)
12854 bb->next_bb = end_bblock;
12856 cfg->cbb->next_bb = end_bblock;
12859 if (cfg->method == method && cfg->domainvar) {
12861 MonoInst *get_domain;
12863 cfg->cbb = init_localsbb;
12865 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
12866 MONO_ADD_INS (cfg->cbb, get_domain);
12868 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
12870 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12871 MONO_ADD_INS (cfg->cbb, store);
12874 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12875 if (cfg->compile_aot)
12876 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12877 mono_get_got_var (cfg);
12880 if (cfg->method == method && cfg->got_var)
12881 mono_emit_load_got_addr (cfg);
12883 if (init_localsbb) {
12884 cfg->cbb = init_localsbb;
12886 for (i = 0; i < header->num_locals; ++i) {
12887 emit_init_local (cfg, i, header->locals [i], init_locals);
12891 if (cfg->init_ref_vars && cfg->method == method) {
12892 /* Emit initialization for ref vars */
12893 // FIXME: Avoid duplication initialization for IL locals.
12894 for (i = 0; i < cfg->num_varinfo; ++i) {
12895 MonoInst *ins = cfg->varinfo [i];
12897 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12898 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12902 if (cfg->lmf_var && cfg->method == method) {
12903 cfg->cbb = init_localsbb;
12904 emit_push_lmf (cfg);
12907 cfg->cbb = init_localsbb;
12908 emit_instrumentation_call (cfg, mono_profiler_method_enter);
12911 MonoBasicBlock *bb;
12914 * Make seq points at backward branch targets interruptable.
12916 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12917 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12918 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12921 /* Add a sequence point for method entry/exit events */
12922 if (seq_points && cfg->gen_sdb_seq_points) {
12923 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12924 MONO_ADD_INS (init_localsbb, ins);
12925 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12926 MONO_ADD_INS (cfg->bb_exit, ins);
12930 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12931 * the code they refer to was dead (#11880).
12933 if (sym_seq_points) {
12934 for (i = 0; i < header->code_size; ++i) {
12935 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12938 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12939 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
12946 if (cfg->method == method) {
12947 MonoBasicBlock *bb;
12948 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12949 bb->region = mono_find_block_region (cfg, bb->real_offset);
12951 mono_create_spvar_for_region (cfg, bb->region);
12952 if (cfg->verbose_level > 2)
12953 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
12957 if (inline_costs < 0) {
12960 /* Method is too large */
12961 mname = mono_method_full_name (method, TRUE);
12962 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
12963 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
12967 if ((cfg->verbose_level > 2) && (cfg->method == method))
12968 mono_print_code (cfg, "AFTER METHOD-TO-IR");
12973 g_assert (!mono_error_ok (&cfg->error));
12977 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
12981 set_exception_type_from_invalid_il (cfg, method, ip);
12985 g_slist_free (class_inits);
12986 mono_basic_block_free (original_bb);
12987 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
12988 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
12989 if (cfg->exception_type)
12992 return inline_costs;
12996 store_membase_reg_to_store_membase_imm (int opcode)
12999 case OP_STORE_MEMBASE_REG:
13000 return OP_STORE_MEMBASE_IMM;
13001 case OP_STOREI1_MEMBASE_REG:
13002 return OP_STOREI1_MEMBASE_IMM;
13003 case OP_STOREI2_MEMBASE_REG:
13004 return OP_STOREI2_MEMBASE_IMM;
13005 case OP_STOREI4_MEMBASE_REG:
13006 return OP_STOREI4_MEMBASE_IMM;
13007 case OP_STOREI8_MEMBASE_REG:
13008 return OP_STOREI8_MEMBASE_IMM;
13010 g_assert_not_reached ();
13017 mono_op_to_op_imm (int opcode)
13021 return OP_IADD_IMM;
13023 return OP_ISUB_IMM;
13025 return OP_IDIV_IMM;
13027 return OP_IDIV_UN_IMM;
13029 return OP_IREM_IMM;
13031 return OP_IREM_UN_IMM;
13033 return OP_IMUL_IMM;
13035 return OP_IAND_IMM;
13039 return OP_IXOR_IMM;
13041 return OP_ISHL_IMM;
13043 return OP_ISHR_IMM;
13045 return OP_ISHR_UN_IMM;
13048 return OP_LADD_IMM;
13050 return OP_LSUB_IMM;
13052 return OP_LAND_IMM;
13056 return OP_LXOR_IMM;
13058 return OP_LSHL_IMM;
13060 return OP_LSHR_IMM;
13062 return OP_LSHR_UN_IMM;
13063 #if SIZEOF_REGISTER == 8
13065 return OP_LREM_IMM;
13069 return OP_COMPARE_IMM;
13071 return OP_ICOMPARE_IMM;
13073 return OP_LCOMPARE_IMM;
13075 case OP_STORE_MEMBASE_REG:
13076 return OP_STORE_MEMBASE_IMM;
13077 case OP_STOREI1_MEMBASE_REG:
13078 return OP_STOREI1_MEMBASE_IMM;
13079 case OP_STOREI2_MEMBASE_REG:
13080 return OP_STOREI2_MEMBASE_IMM;
13081 case OP_STOREI4_MEMBASE_REG:
13082 return OP_STOREI4_MEMBASE_IMM;
13084 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13086 return OP_X86_PUSH_IMM;
13087 case OP_X86_COMPARE_MEMBASE_REG:
13088 return OP_X86_COMPARE_MEMBASE_IMM;
13090 #if defined(TARGET_AMD64)
13091 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13092 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13094 case OP_VOIDCALL_REG:
13095 return OP_VOIDCALL;
13103 return OP_LOCALLOC_IMM;
13110 ldind_to_load_membase (int opcode)
13114 return OP_LOADI1_MEMBASE;
13116 return OP_LOADU1_MEMBASE;
13118 return OP_LOADI2_MEMBASE;
13120 return OP_LOADU2_MEMBASE;
13122 return OP_LOADI4_MEMBASE;
13124 return OP_LOADU4_MEMBASE;
13126 return OP_LOAD_MEMBASE;
13127 case CEE_LDIND_REF:
13128 return OP_LOAD_MEMBASE;
13130 return OP_LOADI8_MEMBASE;
13132 return OP_LOADR4_MEMBASE;
13134 return OP_LOADR8_MEMBASE;
13136 g_assert_not_reached ();
13143 stind_to_store_membase (int opcode)
13147 return OP_STOREI1_MEMBASE_REG;
13149 return OP_STOREI2_MEMBASE_REG;
13151 return OP_STOREI4_MEMBASE_REG;
13153 case CEE_STIND_REF:
13154 return OP_STORE_MEMBASE_REG;
13156 return OP_STOREI8_MEMBASE_REG;
13158 return OP_STORER4_MEMBASE_REG;
13160 return OP_STORER8_MEMBASE_REG;
13162 g_assert_not_reached ();
13169 mono_load_membase_to_load_mem (int opcode)
13171 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13172 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13174 case OP_LOAD_MEMBASE:
13175 return OP_LOAD_MEM;
13176 case OP_LOADU1_MEMBASE:
13177 return OP_LOADU1_MEM;
13178 case OP_LOADU2_MEMBASE:
13179 return OP_LOADU2_MEM;
13180 case OP_LOADI4_MEMBASE:
13181 return OP_LOADI4_MEM;
13182 case OP_LOADU4_MEMBASE:
13183 return OP_LOADU4_MEM;
13184 #if SIZEOF_REGISTER == 8
13185 case OP_LOADI8_MEMBASE:
13186 return OP_LOADI8_MEM;
13195 op_to_op_dest_membase (int store_opcode, int opcode)
13197 #if defined(TARGET_X86)
13198 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13203 return OP_X86_ADD_MEMBASE_REG;
13205 return OP_X86_SUB_MEMBASE_REG;
13207 return OP_X86_AND_MEMBASE_REG;
13209 return OP_X86_OR_MEMBASE_REG;
13211 return OP_X86_XOR_MEMBASE_REG;
13214 return OP_X86_ADD_MEMBASE_IMM;
13217 return OP_X86_SUB_MEMBASE_IMM;
13220 return OP_X86_AND_MEMBASE_IMM;
13223 return OP_X86_OR_MEMBASE_IMM;
13226 return OP_X86_XOR_MEMBASE_IMM;
13232 #if defined(TARGET_AMD64)
13233 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13238 return OP_X86_ADD_MEMBASE_REG;
13240 return OP_X86_SUB_MEMBASE_REG;
13242 return OP_X86_AND_MEMBASE_REG;
13244 return OP_X86_OR_MEMBASE_REG;
13246 return OP_X86_XOR_MEMBASE_REG;
13248 return OP_X86_ADD_MEMBASE_IMM;
13250 return OP_X86_SUB_MEMBASE_IMM;
13252 return OP_X86_AND_MEMBASE_IMM;
13254 return OP_X86_OR_MEMBASE_IMM;
13256 return OP_X86_XOR_MEMBASE_IMM;
13258 return OP_AMD64_ADD_MEMBASE_REG;
13260 return OP_AMD64_SUB_MEMBASE_REG;
13262 return OP_AMD64_AND_MEMBASE_REG;
13264 return OP_AMD64_OR_MEMBASE_REG;
13266 return OP_AMD64_XOR_MEMBASE_REG;
13269 return OP_AMD64_ADD_MEMBASE_IMM;
13272 return OP_AMD64_SUB_MEMBASE_IMM;
13275 return OP_AMD64_AND_MEMBASE_IMM;
13278 return OP_AMD64_OR_MEMBASE_IMM;
13281 return OP_AMD64_XOR_MEMBASE_IMM;
13291 op_to_op_store_membase (int store_opcode, int opcode)
13293 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13296 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13297 return OP_X86_SETEQ_MEMBASE;
13299 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13300 return OP_X86_SETNE_MEMBASE;
13308 op_to_op_src1_membase (int load_opcode, int opcode)
13311 /* FIXME: This has sign extension issues */
13313 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13314 return OP_X86_COMPARE_MEMBASE8_IMM;
13317 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13322 return OP_X86_PUSH_MEMBASE;
13323 case OP_COMPARE_IMM:
13324 case OP_ICOMPARE_IMM:
13325 return OP_X86_COMPARE_MEMBASE_IMM;
13328 return OP_X86_COMPARE_MEMBASE_REG;
13332 #ifdef TARGET_AMD64
13333 /* FIXME: This has sign extension issues */
13335 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13336 return OP_X86_COMPARE_MEMBASE8_IMM;
13341 #ifdef __mono_ilp32__
13342 if (load_opcode == OP_LOADI8_MEMBASE)
13344 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13346 return OP_X86_PUSH_MEMBASE;
13348 /* FIXME: This only works for 32 bit immediates
13349 case OP_COMPARE_IMM:
13350 case OP_LCOMPARE_IMM:
13351 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13352 return OP_AMD64_COMPARE_MEMBASE_IMM;
13354 case OP_ICOMPARE_IMM:
13355 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13356 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13360 #ifdef __mono_ilp32__
13361 if (load_opcode == OP_LOAD_MEMBASE)
13362 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13363 if (load_opcode == OP_LOADI8_MEMBASE)
13365 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13367 return OP_AMD64_COMPARE_MEMBASE_REG;
13370 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13371 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13380 op_to_op_src2_membase (int load_opcode, int opcode)
13383 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13389 return OP_X86_COMPARE_REG_MEMBASE;
13391 return OP_X86_ADD_REG_MEMBASE;
13393 return OP_X86_SUB_REG_MEMBASE;
13395 return OP_X86_AND_REG_MEMBASE;
13397 return OP_X86_OR_REG_MEMBASE;
13399 return OP_X86_XOR_REG_MEMBASE;
13403 #ifdef TARGET_AMD64
13404 #ifdef __mono_ilp32__
13405 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE) ) {
13407 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)) {
13411 return OP_AMD64_ICOMPARE_REG_MEMBASE;
13413 return OP_X86_ADD_REG_MEMBASE;
13415 return OP_X86_SUB_REG_MEMBASE;
13417 return OP_X86_AND_REG_MEMBASE;
13419 return OP_X86_OR_REG_MEMBASE;
13421 return OP_X86_XOR_REG_MEMBASE;
13423 #ifdef __mono_ilp32__
13424 } else if (load_opcode == OP_LOADI8_MEMBASE) {
13426 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE)) {
13431 return OP_AMD64_COMPARE_REG_MEMBASE;
13433 return OP_AMD64_ADD_REG_MEMBASE;
13435 return OP_AMD64_SUB_REG_MEMBASE;
13437 return OP_AMD64_AND_REG_MEMBASE;
13439 return OP_AMD64_OR_REG_MEMBASE;
13441 return OP_AMD64_XOR_REG_MEMBASE;
13450 mono_op_to_op_imm_noemul (int opcode)
13453 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13459 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13466 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13471 return mono_op_to_op_imm (opcode);
13476 * mono_handle_global_vregs:
13478 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
13482 mono_handle_global_vregs (MonoCompile *cfg)
13484 gint32 *vreg_to_bb;
13485 MonoBasicBlock *bb;
13488 vreg_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13490 #ifdef MONO_ARCH_SIMD_INTRINSICS
13491 if (cfg->uses_simd_intrinsics)
13492 mono_simd_simplify_indirection (cfg);
13495 /* Find local vregs used in more than one bb */
13496 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13497 MonoInst *ins = bb->code;
13498 int block_num = bb->block_num;
13500 if (cfg->verbose_level > 2)
13501 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13504 for (; ins; ins = ins->next) {
13505 const char *spec = INS_INFO (ins->opcode);
13506 int regtype = 0, regindex;
13509 if (G_UNLIKELY (cfg->verbose_level > 2))
13510 mono_print_ins (ins);
13512 g_assert (ins->opcode >= MONO_CEE_LAST);
13514 for (regindex = 0; regindex < 4; regindex ++) {
13517 if (regindex == 0) {
13518 regtype = spec [MONO_INST_DEST];
13519 if (regtype == ' ')
13522 } else if (regindex == 1) {
13523 regtype = spec [MONO_INST_SRC1];
13524 if (regtype == ' ')
13527 } else if (regindex == 2) {
13528 regtype = spec [MONO_INST_SRC2];
13529 if (regtype == ' ')
13532 } else if (regindex == 3) {
13533 regtype = spec [MONO_INST_SRC3];
13534 if (regtype == ' ')
13539 #if SIZEOF_REGISTER == 4
13540 /* In the LLVM case, the long opcodes are not decomposed */
13541 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13543 * Since some instructions reference the original long vreg,
13544 * and some reference the two component vregs, it is quite hard
13545 * to determine when it needs to be global. So be conservative.
13547 if (!get_vreg_to_inst (cfg, vreg)) {
13548 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13550 if (cfg->verbose_level > 2)
13551 printf ("LONG VREG R%d made global.\n", vreg);
13555 * Make the component vregs volatile since the optimizations can
13556 * get confused otherwise.
13558 get_vreg_to_inst (cfg, vreg + 1)->flags |= MONO_INST_VOLATILE;
13559 get_vreg_to_inst (cfg, vreg + 2)->flags |= MONO_INST_VOLATILE;
13563 g_assert (vreg != -1);
13565 prev_bb = vreg_to_bb [vreg];
13566 if (prev_bb == 0) {
13567 /* 0 is a valid block num */
13568 vreg_to_bb [vreg] = block_num + 1;
13569 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13570 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13573 if (!get_vreg_to_inst (cfg, vreg)) {
13574 if (G_UNLIKELY (cfg->verbose_level > 2))
13575 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13579 if (vreg_is_ref (cfg, vreg))
13580 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13582 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13585 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13588 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13591 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13594 g_assert_not_reached ();
13598 /* Flag as having been used in more than one bb */
13599 vreg_to_bb [vreg] = -1;
13605 /* If a variable is used in only one bblock, convert it into a local vreg */
13606 for (i = 0; i < cfg->num_varinfo; i++) {
13607 MonoInst *var = cfg->varinfo [i];
13608 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13610 switch (var->type) {
13616 #if SIZEOF_REGISTER == 8
13619 #if !defined(TARGET_X86)
13620 /* Enabling this screws up the fp stack on x86 */
13623 if (mono_arch_is_soft_float ())
13626 /* Arguments are implicitly global */
13627 /* Putting R4 vars into registers doesn't work currently */
13628 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13629 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) {
13631 * Make that the variable's liveness interval doesn't contain a call, since
13632 * that would cause the lvreg to be spilled, making the whole optimization
13635 /* This is too slow for JIT compilation */
13637 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13639 int def_index, call_index, ins_index;
13640 gboolean spilled = FALSE;
13645 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13646 const char *spec = INS_INFO (ins->opcode);
13648 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13649 def_index = ins_index;
13651 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13652 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13653 if (call_index > def_index) {
13659 if (MONO_IS_CALL (ins))
13660 call_index = ins_index;
13670 if (G_UNLIKELY (cfg->verbose_level > 2))
13671 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13672 var->flags |= MONO_INST_IS_DEAD;
13673 cfg->vreg_to_inst [var->dreg] = NULL;
13680 * Compress the varinfo and vars tables so the liveness computation is faster and
13681 * takes up less space.
13684 for (i = 0; i < cfg->num_varinfo; ++i) {
13685 MonoInst *var = cfg->varinfo [i];
13686 if (pos < i && cfg->locals_start == i)
13687 cfg->locals_start = pos;
13688 if (!(var->flags & MONO_INST_IS_DEAD)) {
13690 cfg->varinfo [pos] = cfg->varinfo [i];
13691 cfg->varinfo [pos]->inst_c0 = pos;
13692 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13693 cfg->vars [pos].idx = pos;
13694 #if SIZEOF_REGISTER == 4
13695 if (cfg->varinfo [pos]->type == STACK_I8) {
13696 /* Modify the two component vars too */
13699 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 1);
13700 var1->inst_c0 = pos;
13701 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 2);
13702 var1->inst_c0 = pos;
13709 cfg->num_varinfo = pos;
13710 if (cfg->locals_start > cfg->num_varinfo)
13711 cfg->locals_start = cfg->num_varinfo;
13715 * mono_spill_global_vars:
13717 * Generate spill code for variables which are not allocated to registers,
13718 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13719 * code is generated which could be optimized by the local optimization passes.
13722 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13724 MonoBasicBlock *bb;
13726 int orig_next_vreg;
13727 guint32 *vreg_to_lvreg;
13729 guint32 i, lvregs_len;
13730 gboolean dest_has_lvreg = FALSE;
13731 guint32 stacktypes [128];
13732 MonoInst **live_range_start, **live_range_end;
13733 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13734 int *gsharedvt_vreg_to_idx = NULL;
13736 *need_local_opts = FALSE;
13738 memset (spec2, 0, sizeof (spec2));
13740 /* FIXME: Move this function to mini.c */
13741 stacktypes ['i'] = STACK_PTR;
13742 stacktypes ['l'] = STACK_I8;
13743 stacktypes ['f'] = STACK_R8;
13744 #ifdef MONO_ARCH_SIMD_INTRINSICS
13745 stacktypes ['x'] = STACK_VTYPE;
13748 #if SIZEOF_REGISTER == 4
13749 /* Create MonoInsts for longs */
13750 for (i = 0; i < cfg->num_varinfo; i++) {
13751 MonoInst *ins = cfg->varinfo [i];
13753 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13754 switch (ins->type) {
13759 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13762 g_assert (ins->opcode == OP_REGOFFSET);
13764 tree = get_vreg_to_inst (cfg, ins->dreg + 1);
13766 tree->opcode = OP_REGOFFSET;
13767 tree->inst_basereg = ins->inst_basereg;
13768 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13770 tree = get_vreg_to_inst (cfg, ins->dreg + 2);
13772 tree->opcode = OP_REGOFFSET;
13773 tree->inst_basereg = ins->inst_basereg;
13774 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13784 if (cfg->compute_gc_maps) {
13785 /* registers need liveness info even for !non refs */
13786 for (i = 0; i < cfg->num_varinfo; i++) {
13787 MonoInst *ins = cfg->varinfo [i];
13789 if (ins->opcode == OP_REGVAR)
13790 ins->flags |= MONO_INST_GC_TRACK;
13794 if (cfg->gsharedvt) {
13795 gsharedvt_vreg_to_idx = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13797 for (i = 0; i < cfg->num_varinfo; ++i) {
13798 MonoInst *ins = cfg->varinfo [i];
13801 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13802 if (i >= cfg->locals_start) {
13804 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13805 gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13806 ins->opcode = OP_GSHAREDVT_LOCAL;
13807 ins->inst_imm = idx;
13810 gsharedvt_vreg_to_idx [ins->dreg] = -1;
13811 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13817 /* FIXME: widening and truncation */
13820 * As an optimization, when a variable allocated to the stack is first loaded into
13821 * an lvreg, we will remember the lvreg and use it the next time instead of loading
13822 * the variable again.
13824 orig_next_vreg = cfg->next_vreg;
13825 vreg_to_lvreg = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13826 lvregs = mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
13830 * These arrays contain the first and last instructions accessing a given
13832 * Since we emit bblocks in the same order we process them here, and we
13833 * don't split live ranges, these will precisely describe the live range of
13834 * the variable, i.e. the instruction range where a valid value can be found
13835 * in the variables location.
13836 * The live range is computed using the liveness info computed by the liveness pass.
13837 * We can't use vmv->range, since that is an abstract live range, and we need
13838 * one which is instruction precise.
13839 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13841 /* FIXME: Only do this if debugging info is requested */
13842 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13843 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13844 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13845 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13847 /* Add spill loads/stores */
13848 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13851 if (cfg->verbose_level > 2)
13852 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13854 /* Clear vreg_to_lvreg array */
13855 for (i = 0; i < lvregs_len; i++)
13856 vreg_to_lvreg [lvregs [i]] = 0;
13860 MONO_BB_FOR_EACH_INS (bb, ins) {
13861 const char *spec = INS_INFO (ins->opcode);
13862 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13863 gboolean store, no_lvreg;
13864 int sregs [MONO_MAX_SRC_REGS];
13866 if (G_UNLIKELY (cfg->verbose_level > 2))
13867 mono_print_ins (ins);
13869 if (ins->opcode == OP_NOP)
13873 * We handle LDADDR here as well, since it can only be decomposed
13874 * when variable addresses are known.
13876 if (ins->opcode == OP_LDADDR) {
13877 MonoInst *var = ins->inst_p0;
13879 if (var->opcode == OP_VTARG_ADDR) {
13880 /* Happens on SPARC/S390 where vtypes are passed by reference */
13881 MonoInst *vtaddr = var->inst_left;
13882 if (vtaddr->opcode == OP_REGVAR) {
13883 ins->opcode = OP_MOVE;
13884 ins->sreg1 = vtaddr->dreg;
13886 else if (var->inst_left->opcode == OP_REGOFFSET) {
13887 ins->opcode = OP_LOAD_MEMBASE;
13888 ins->inst_basereg = vtaddr->inst_basereg;
13889 ins->inst_offset = vtaddr->inst_offset;
13892 } else if (cfg->gsharedvt && gsharedvt_vreg_to_idx [var->dreg] < 0) {
13893 /* gsharedvt arg passed by ref */
13894 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13896 ins->opcode = OP_LOAD_MEMBASE;
13897 ins->inst_basereg = var->inst_basereg;
13898 ins->inst_offset = var->inst_offset;
13899 } else if (cfg->gsharedvt && gsharedvt_vreg_to_idx [var->dreg]) {
13900 MonoInst *load, *load2, *load3;
13901 int idx = gsharedvt_vreg_to_idx [var->dreg] - 1;
13902 int reg1, reg2, reg3;
13903 MonoInst *info_var = cfg->gsharedvt_info_var;
13904 MonoInst *locals_var = cfg->gsharedvt_locals_var;
13908 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13911 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13913 g_assert (info_var);
13914 g_assert (locals_var);
13916 /* Mark the instruction used to compute the locals var as used */
13917 cfg->gsharedvt_locals_var_ins = NULL;
13919 /* Load the offset */
13920 if (info_var->opcode == OP_REGOFFSET) {
13921 reg1 = alloc_ireg (cfg);
13922 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13923 } else if (info_var->opcode == OP_REGVAR) {
13925 reg1 = info_var->dreg;
13927 g_assert_not_reached ();
13929 reg2 = alloc_ireg (cfg);
13930 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13931 /* Load the locals area address */
13932 reg3 = alloc_ireg (cfg);
13933 if (locals_var->opcode == OP_REGOFFSET) {
13934 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
13935 } else if (locals_var->opcode == OP_REGVAR) {
13936 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
13938 g_assert_not_reached ();
13940 /* Compute the address */
13941 ins->opcode = OP_PADD;
13945 mono_bblock_insert_before_ins (bb, ins, load3);
13946 mono_bblock_insert_before_ins (bb, load3, load2);
13948 mono_bblock_insert_before_ins (bb, load2, load);
13950 g_assert (var->opcode == OP_REGOFFSET);
13952 ins->opcode = OP_ADD_IMM;
13953 ins->sreg1 = var->inst_basereg;
13954 ins->inst_imm = var->inst_offset;
13957 *need_local_opts = TRUE;
13958 spec = INS_INFO (ins->opcode);
13961 if (ins->opcode < MONO_CEE_LAST) {
13962 mono_print_ins (ins);
13963 g_assert_not_reached ();
13967 * Store opcodes have destbasereg in the dreg, but in reality, it is an
13971 if (MONO_IS_STORE_MEMBASE (ins)) {
13972 tmp_reg = ins->dreg;
13973 ins->dreg = ins->sreg2;
13974 ins->sreg2 = tmp_reg;
13977 spec2 [MONO_INST_DEST] = ' ';
13978 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13979 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13980 spec2 [MONO_INST_SRC3] = ' ';
13982 } else if (MONO_IS_STORE_MEMINDEX (ins))
13983 g_assert_not_reached ();
13988 if (G_UNLIKELY (cfg->verbose_level > 2)) {
13989 printf ("\t %.3s %d", spec, ins->dreg);
13990 num_sregs = mono_inst_get_src_registers (ins, sregs);
13991 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
13992 printf (" %d", sregs [srcindex]);
13999 regtype = spec [MONO_INST_DEST];
14000 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14003 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14004 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14005 MonoInst *store_ins;
14007 MonoInst *def_ins = ins;
14008 int dreg = ins->dreg; /* The original vreg */
14010 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14012 if (var->opcode == OP_REGVAR) {
14013 ins->dreg = var->dreg;
14014 } 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)) {
14016 * Instead of emitting a load+store, use a _membase opcode.
14018 g_assert (var->opcode == OP_REGOFFSET);
14019 if (ins->opcode == OP_MOVE) {
14023 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14024 ins->inst_basereg = var->inst_basereg;
14025 ins->inst_offset = var->inst_offset;
14028 spec = INS_INFO (ins->opcode);
14032 g_assert (var->opcode == OP_REGOFFSET);
14034 prev_dreg = ins->dreg;
14036 /* Invalidate any previous lvreg for this vreg */
14037 vreg_to_lvreg [ins->dreg] = 0;
14041 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14043 store_opcode = OP_STOREI8_MEMBASE_REG;
14046 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14048 #if SIZEOF_REGISTER != 8
14049 if (regtype == 'l') {
14050 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, ins->dreg + 1);
14051 mono_bblock_insert_after_ins (bb, ins, store_ins);
14052 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, ins->dreg + 2);
14053 mono_bblock_insert_after_ins (bb, ins, store_ins);
14054 def_ins = store_ins;
14059 g_assert (store_opcode != OP_STOREV_MEMBASE);
14061 /* Try to fuse the store into the instruction itself */
14062 /* FIXME: Add more instructions */
14063 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14064 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14065 ins->inst_imm = ins->inst_c0;
14066 ins->inst_destbasereg = var->inst_basereg;
14067 ins->inst_offset = var->inst_offset;
14068 spec = INS_INFO (ins->opcode);
14069 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14070 ins->opcode = store_opcode;
14071 ins->inst_destbasereg = var->inst_basereg;
14072 ins->inst_offset = var->inst_offset;
14076 tmp_reg = ins->dreg;
14077 ins->dreg = ins->sreg2;
14078 ins->sreg2 = tmp_reg;
14081 spec2 [MONO_INST_DEST] = ' ';
14082 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14083 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14084 spec2 [MONO_INST_SRC3] = ' ';
14086 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14087 // FIXME: The backends expect the base reg to be in inst_basereg
14088 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14090 ins->inst_basereg = var->inst_basereg;
14091 ins->inst_offset = var->inst_offset;
14092 spec = INS_INFO (ins->opcode);
14094 /* printf ("INS: "); mono_print_ins (ins); */
14095 /* Create a store instruction */
14096 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14098 /* Insert it after the instruction */
14099 mono_bblock_insert_after_ins (bb, ins, store_ins);
14101 def_ins = store_ins;
14104 * We can't assign ins->dreg to var->dreg here, since the
14105 * sregs could use it. So set a flag, and do it after
14108 if ((!MONO_ARCH_USE_FPSTACK || ((store_opcode != OP_STORER8_MEMBASE_REG) && (store_opcode != OP_STORER4_MEMBASE_REG))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
14109 dest_has_lvreg = TRUE;
14114 if (def_ins && !live_range_start [dreg]) {
14115 live_range_start [dreg] = def_ins;
14116 live_range_start_bb [dreg] = bb;
14119 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14122 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14123 tmp->inst_c1 = dreg;
14124 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14131 num_sregs = mono_inst_get_src_registers (ins, sregs);
14132 for (srcindex = 0; srcindex < 3; ++srcindex) {
14133 regtype = spec [MONO_INST_SRC1 + srcindex];
14134 sreg = sregs [srcindex];
14136 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14137 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14138 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14139 MonoInst *use_ins = ins;
14140 MonoInst *load_ins;
14141 guint32 load_opcode;
14143 if (var->opcode == OP_REGVAR) {
14144 sregs [srcindex] = var->dreg;
14145 //mono_inst_set_src_registers (ins, sregs);
14146 live_range_end [sreg] = use_ins;
14147 live_range_end_bb [sreg] = bb;
14149 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14152 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14153 /* var->dreg is a hreg */
14154 tmp->inst_c1 = sreg;
14155 mono_bblock_insert_after_ins (bb, ins, tmp);
14161 g_assert (var->opcode == OP_REGOFFSET);
14163 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14165 g_assert (load_opcode != OP_LOADV_MEMBASE);
14167 if (vreg_to_lvreg [sreg]) {
14168 g_assert (vreg_to_lvreg [sreg] != -1);
14170 /* The variable is already loaded to an lvreg */
14171 if (G_UNLIKELY (cfg->verbose_level > 2))
14172 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14173 sregs [srcindex] = vreg_to_lvreg [sreg];
14174 //mono_inst_set_src_registers (ins, sregs);
14178 /* Try to fuse the load into the instruction */
14179 if ((srcindex == 0) && (op_to_op_src1_membase (load_opcode, ins->opcode) != -1)) {
14180 ins->opcode = op_to_op_src1_membase (load_opcode, ins->opcode);
14181 sregs [0] = var->inst_basereg;
14182 //mono_inst_set_src_registers (ins, sregs);
14183 ins->inst_offset = var->inst_offset;
14184 } else if ((srcindex == 1) && (op_to_op_src2_membase (load_opcode, ins->opcode) != -1)) {
14185 ins->opcode = op_to_op_src2_membase (load_opcode, ins->opcode);
14186 sregs [1] = var->inst_basereg;
14187 //mono_inst_set_src_registers (ins, sregs);
14188 ins->inst_offset = var->inst_offset;
14190 if (MONO_IS_REAL_MOVE (ins)) {
14191 ins->opcode = OP_NOP;
14194 //printf ("%d ", srcindex); mono_print_ins (ins);
14196 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14198 if ((!MONO_ARCH_USE_FPSTACK || ((load_opcode != OP_LOADR8_MEMBASE) && (load_opcode != OP_LOADR4_MEMBASE))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && !no_lvreg) {
14199 if (var->dreg == prev_dreg) {
14201 * sreg refers to the value loaded by the load
14202 * emitted below, but we need to use ins->dreg
14203 * since it refers to the store emitted earlier.
14207 g_assert (sreg != -1);
14208 vreg_to_lvreg [var->dreg] = sreg;
14209 g_assert (lvregs_len < 1024);
14210 lvregs [lvregs_len ++] = var->dreg;
14214 sregs [srcindex] = sreg;
14215 //mono_inst_set_src_registers (ins, sregs);
14217 #if SIZEOF_REGISTER != 8
14218 if (regtype == 'l') {
14219 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 2, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14220 mono_bblock_insert_before_ins (bb, ins, load_ins);
14221 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 1, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14222 mono_bblock_insert_before_ins (bb, ins, load_ins);
14223 use_ins = load_ins;
14228 #if SIZEOF_REGISTER == 4
14229 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14231 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14232 mono_bblock_insert_before_ins (bb, ins, load_ins);
14233 use_ins = load_ins;
14237 if (var->dreg < orig_next_vreg) {
14238 live_range_end [var->dreg] = use_ins;
14239 live_range_end_bb [var->dreg] = bb;
14242 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14245 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14246 tmp->inst_c1 = var->dreg;
14247 mono_bblock_insert_after_ins (bb, ins, tmp);
14251 mono_inst_set_src_registers (ins, sregs);
14253 if (dest_has_lvreg) {
14254 g_assert (ins->dreg != -1);
14255 vreg_to_lvreg [prev_dreg] = ins->dreg;
14256 g_assert (lvregs_len < 1024);
14257 lvregs [lvregs_len ++] = prev_dreg;
14258 dest_has_lvreg = FALSE;
14262 tmp_reg = ins->dreg;
14263 ins->dreg = ins->sreg2;
14264 ins->sreg2 = tmp_reg;
14267 if (MONO_IS_CALL (ins)) {
14268 /* Clear vreg_to_lvreg array */
14269 for (i = 0; i < lvregs_len; i++)
14270 vreg_to_lvreg [lvregs [i]] = 0;
14272 } else if (ins->opcode == OP_NOP) {
14274 MONO_INST_NULLIFY_SREGS (ins);
14277 if (cfg->verbose_level > 2)
14278 mono_print_ins_index (1, ins);
14281 /* Extend the live range based on the liveness info */
14282 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14283 for (i = 0; i < cfg->num_varinfo; i ++) {
14284 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14286 if (vreg_is_volatile (cfg, vi->vreg))
14287 /* The liveness info is incomplete */
14290 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14291 /* Live from at least the first ins of this bb */
14292 live_range_start [vi->vreg] = bb->code;
14293 live_range_start_bb [vi->vreg] = bb;
14296 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14297 /* Live at least until the last ins of this bb */
14298 live_range_end [vi->vreg] = bb->last_ins;
14299 live_range_end_bb [vi->vreg] = bb;
14305 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
14307 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14308 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14310 if (cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14311 for (i = 0; i < cfg->num_varinfo; ++i) {
14312 int vreg = MONO_VARINFO (cfg, i)->vreg;
14315 if (live_range_start [vreg]) {
14316 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14318 ins->inst_c1 = vreg;
14319 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14321 if (live_range_end [vreg]) {
14322 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14324 ins->inst_c1 = vreg;
14325 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14326 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14328 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14334 if (cfg->gsharedvt_locals_var_ins) {
14335 /* Nullify if unused */
14336 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14337 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14340 g_free (live_range_start);
14341 g_free (live_range_end);
14342 g_free (live_range_start_bb);
14343 g_free (live_range_end_bb);
14348 * - use 'iadd' instead of 'int_add'
14349 * - handling ovf opcodes: decompose in method_to_ir.
14350 * - unify iregs/fregs
14351 * -> partly done, the missing parts are:
14352 * - a more complete unification would involve unifying the hregs as well, so
14353 * code wouldn't need if (fp) all over the place. but that would mean the hregs
14354 * would no longer map to the machine hregs, so the code generators would need to
14355 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14356 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14357 * fp/non-fp branches speeds it up by about 15%.
14358 * - use sext/zext opcodes instead of shifts
14360 * - get rid of TEMPLOADs if possible and use vregs instead
14361 * - clean up usage of OP_P/OP_ opcodes
14362 * - cleanup usage of DUMMY_USE
14363 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
14365 * - set the stack type and allocate a dreg in the EMIT_NEW macros
14366 * - get rid of all the <foo>2 stuff when the new JIT is ready.
14367 * - make sure handle_stack_args () is called before the branch is emitted
14368 * - when the new IR is done, get rid of all unused stuff
14369 * - COMPARE/BEQ as separate instructions or unify them ?
14370 * - keeping them separate allows specialized compare instructions like
14371 * compare_imm, compare_membase
14372 * - most back ends unify fp compare+branch, fp compare+ceq
14373 * - integrate mono_save_args into inline_method
14374 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14375 * - handle long shift opts on 32 bit platforms somehow: they require
14376 * 3 sregs (2 for arg1 and 1 for arg2)
14377 * - make byref a 'normal' type.
14378 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14379 * variable if needed.
14380 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14381 * like inline_method.
14382 * - remove inlining restrictions
14383 * - fix LNEG and enable cfold of INEG
14384 * - generalize x86 optimizations like ldelema as a peephole optimization
14385 * - add store_mem_imm for amd64
14386 * - optimize the loading of the interruption flag in the managed->native wrappers
14387 * - avoid special handling of OP_NOP in passes
14388 * - move code inserting instructions into one function/macro.
14389 * - try a coalescing phase after liveness analysis
14390 * - add float -> vreg conversion + local optimizations on !x86
14391 * - figure out how to handle decomposed branches during optimizations, ie.
14392 * compare+branch, op_jump_table+op_br etc.
14393 * - promote RuntimeXHandles to vregs
14394 * - vtype cleanups:
14395 * - add a NEW_VARLOADA_VREG macro
14396 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
14397 * accessing vtype fields.
14398 * - get rid of I8CONST on 64 bit platforms
14399 * - dealing with the increase in code size due to branches created during opcode
14401 * - use extended basic blocks
14402 * - all parts of the JIT
14403 * - handle_global_vregs () && local regalloc
14404 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14405 * - sources of increase in code size:
14408 * - isinst and castclass
14409 * - lvregs not allocated to global registers even if used multiple times
14410 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14412 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14413 * - add all micro optimizations from the old JIT
14414 * - put tree optimizations into the deadce pass
14415 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14416 * specific function.
14417 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14418 * fcompare + branchCC.
14419 * - create a helper function for allocating a stack slot, taking into account
14420 * MONO_CFG_HAS_SPILLUP.
14422 * - merge the ia64 switch changes.
14423 * - optimize mono_regstate2_alloc_int/float.
14424 * - fix the pessimistic handling of variables accessed in exception handler blocks.
14425 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14426 * parts of the tree could be separated by other instructions, killing the tree
14427 * arguments, or stores killing loads etc. Also, should we fold loads into other
14428 * instructions if the result of the load is used multiple times ?
14429 * - make the REM_IMM optimization in mini-x86.c arch-independent.
14430 * - LAST MERGE: 108395.
14431 * - when returning vtypes in registers, generate IR and append it to the end of the
14432 * last bb instead of doing it in the epilog.
14433 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14441 - When to decompose opcodes:
14442 - earlier: this makes some optimizations hard to implement, since the low level IR
14443 no longer contains the neccessary information. But it is easier to do.
14444 - later: harder to implement, enables more optimizations.
14445 - Branches inside bblocks:
14446 - created when decomposing complex opcodes.
14447 - branches to another bblock: harmless, but not tracked by the branch
14448 optimizations, so need to branch to a label at the start of the bblock.
14449 - branches to inside the same bblock: very problematic, trips up the local
14450 reg allocator. Can be fixed by spitting the current bblock, but that is a
14451 complex operation, since some local vregs can become global vregs etc.
14452 - Local/global vregs:
14453 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14454 local register allocator.
14455 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14456 structure, created by mono_create_var (). Assigned to hregs or the stack by
14457 the global register allocator.
14458 - When to do optimizations like alu->alu_imm:
14459 - earlier -> saves work later on since the IR will be smaller/simpler
14460 - later -> can work on more instructions
14461 - Handling of valuetypes:
14462 - When a vtype is pushed on the stack, a new temporary is created, an
14463 instruction computing its address (LDADDR) is emitted and pushed on
14464 the stack. Need to optimize cases when the vtype is used immediately as in
14465 argument passing, stloc etc.
14466 - Instead of the to_end stuff in the old JIT, simply call the function handling
14467 the values on the stack before emitting the last instruction of the bb.
14470 #endif /* DISABLE_JIT */