3 * Convert CIL to the JIT internal representation
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2002 Ximian, Inc.
10 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/utils/mono-compiler.h>
31 #ifdef HAVE_SYS_TIME_H
39 #include <mono/utils/memcheck.h>
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/assembly.h>
42 #include <mono/metadata/attrdefs.h>
43 #include <mono/metadata/loader.h>
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/metadata/class.h>
46 #include <mono/metadata/object.h>
47 #include <mono/metadata/exception.h>
48 #include <mono/metadata/opcodes.h>
49 #include <mono/metadata/mono-endian.h>
50 #include <mono/metadata/tokentype.h>
51 #include <mono/metadata/tabledefs.h>
52 #include <mono/metadata/marshal.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/debug-internals.h>
55 #include <mono/metadata/gc-internals.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/profiler-private.h>
60 #include <mono/metadata/profiler.h>
61 #include <mono/metadata/monitor.h>
62 #include <mono/utils/mono-memory-model.h>
63 #include <mono/utils/mono-error-internals.h>
64 #include <mono/metadata/mono-basic-block.h>
65 #include <mono/metadata/reflection-internals.h>
66 #include <mono/utils/mono-threads-coop.h>
72 #include "jit-icalls.h"
74 #include "debugger-agent.h"
75 #include "seq-points.h"
76 #include "aot-compiler.h"
77 #include "mini-llvm.h"
79 #define BRANCH_COST 10
80 #define INLINE_LENGTH_LIMIT 20
82 /* These have 'cfg' as an implicit argument */
83 #define INLINE_FAILURE(msg) do { \
84 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
85 inline_failure (cfg, msg); \
86 goto exception_exit; \
89 #define CHECK_CFG_EXCEPTION do {\
90 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
91 goto exception_exit; \
93 #define FIELD_ACCESS_FAILURE(method, field) do { \
94 field_access_failure ((cfg), (method), (field)); \
95 goto exception_exit; \
97 #define GENERIC_SHARING_FAILURE(opcode) do { \
99 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
100 goto exception_exit; \
103 #define GSHAREDVT_FAILURE(opcode) do { \
104 if (cfg->gsharedvt) { \
105 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
106 goto exception_exit; \
109 #define OUT_OF_MEMORY_FAILURE do { \
110 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
111 mono_error_set_out_of_memory (&cfg->error, ""); \
112 goto exception_exit; \
114 #define DISABLE_AOT(cfg) do { \
115 if ((cfg)->verbose_level >= 2) \
116 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
117 (cfg)->disable_aot = TRUE; \
119 #define LOAD_ERROR do { \
120 break_on_unverified (); \
121 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
122 goto exception_exit; \
125 #define TYPE_LOAD_ERROR(klass) do { \
126 cfg->exception_ptr = klass; \
130 #define CHECK_CFG_ERROR do {\
131 if (!mono_error_ok (&cfg->error)) { \
132 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
133 goto mono_error_exit; \
137 /* Determine whenever 'ins' represents a load of the 'this' argument */
138 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
140 static int ldind_to_load_membase (int opcode);
141 static int stind_to_store_membase (int opcode);
143 int mono_op_to_op_imm (int opcode);
144 int mono_op_to_op_imm_noemul (int opcode);
146 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
147 guchar *ip, guint real_offset, gboolean inline_always);
149 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
151 /* helper methods signatures */
152 static MonoMethodSignature *helper_sig_domain_get;
153 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
154 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
155 static MonoMethodSignature *helper_sig_jit_thread_attach;
156 static MonoMethodSignature *helper_sig_get_tls_tramp;
157 static MonoMethodSignature *helper_sig_set_tls_tramp;
159 /* type loading helpers */
160 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, "System.Runtime.CompilerServices", "RuntimeHelpers")
161 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
164 * Instruction metadata
172 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
173 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
179 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
184 /* keep in sync with the enum in mini.h */
187 #include "mini-ops.h"
192 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
193 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
195 * This should contain the index of the last sreg + 1. This is not the same
196 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
198 const gint8 ins_sreg_counts[] = {
199 #include "mini-ops.h"
205 mono_alloc_ireg (MonoCompile *cfg)
207 return alloc_ireg (cfg);
211 mono_alloc_lreg (MonoCompile *cfg)
213 return alloc_lreg (cfg);
217 mono_alloc_freg (MonoCompile *cfg)
219 return alloc_freg (cfg);
223 mono_alloc_preg (MonoCompile *cfg)
225 return alloc_preg (cfg);
229 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
231 return alloc_dreg (cfg, stack_type);
235 * mono_alloc_ireg_ref:
237 * Allocate an IREG, and mark it as holding a GC ref.
240 mono_alloc_ireg_ref (MonoCompile *cfg)
242 return alloc_ireg_ref (cfg);
246 * mono_alloc_ireg_mp:
248 * Allocate an IREG, and mark it as holding a managed pointer.
251 mono_alloc_ireg_mp (MonoCompile *cfg)
253 return alloc_ireg_mp (cfg);
257 * mono_alloc_ireg_copy:
259 * Allocate an IREG with the same GC type as VREG.
262 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
264 if (vreg_is_ref (cfg, vreg))
265 return alloc_ireg_ref (cfg);
266 else if (vreg_is_mp (cfg, vreg))
267 return alloc_ireg_mp (cfg);
269 return alloc_ireg (cfg);
273 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
278 type = mini_get_underlying_type (type);
280 switch (type->type) {
293 case MONO_TYPE_FNPTR:
295 case MONO_TYPE_CLASS:
296 case MONO_TYPE_STRING:
297 case MONO_TYPE_OBJECT:
298 case MONO_TYPE_SZARRAY:
299 case MONO_TYPE_ARRAY:
303 #if SIZEOF_REGISTER == 8
309 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
312 case MONO_TYPE_VALUETYPE:
313 if (type->data.klass->enumtype) {
314 type = mono_class_enum_basetype (type->data.klass);
317 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
320 case MONO_TYPE_TYPEDBYREF:
322 case MONO_TYPE_GENERICINST:
323 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
325 type = &type->data.generic_class->container_class->byval_arg;
329 g_assert (cfg->gshared);
330 if (mini_type_var_is_vt (type))
333 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
335 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
341 mono_print_bb (MonoBasicBlock *bb, const char *msg)
345 GString *str = g_string_new ("");
347 g_string_append_printf (str, "%s %d: [IN: ", msg, bb->block_num);
348 for (i = 0; i < bb->in_count; ++i)
349 g_string_append_printf (str, " BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
350 g_string_append_printf (str, ", OUT: ");
351 for (i = 0; i < bb->out_count; ++i)
352 g_string_append_printf (str, " BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
353 g_string_append_printf (str, " ]\n");
355 g_print ("%s", str->str);
356 g_string_free (str, TRUE);
358 for (tree = bb->code; tree; tree = tree->next)
359 mono_print_ins_index (-1, tree);
363 mono_create_helper_signatures (void)
365 helper_sig_domain_get = mono_create_icall_signature ("ptr");
366 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
367 helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
368 helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
369 helper_sig_get_tls_tramp = mono_create_icall_signature ("ptr");
370 helper_sig_set_tls_tramp = mono_create_icall_signature ("void ptr");
373 static MONO_NEVER_INLINE void
374 break_on_unverified (void)
376 if (mini_get_debug_options ()->break_on_unverified)
380 static MONO_NEVER_INLINE void
381 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
383 char *method_fname = mono_method_full_name (method, TRUE);
384 char *field_fname = mono_field_full_name (field);
385 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
386 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
387 g_free (method_fname);
388 g_free (field_fname);
391 static MONO_NEVER_INLINE void
392 inline_failure (MonoCompile *cfg, const char *msg)
394 if (cfg->verbose_level >= 2)
395 printf ("inline failed: %s\n", msg);
396 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
399 static MONO_NEVER_INLINE void
400 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
402 if (cfg->verbose_level > 2) \
403 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);
404 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
407 static MONO_NEVER_INLINE void
408 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
410 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);
411 if (cfg->verbose_level >= 2)
412 printf ("%s\n", cfg->exception_message);
413 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
417 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
418 * foo<T> (int i) { ldarg.0; box T; }
420 #define UNVERIFIED do { \
421 if (cfg->gsharedvt) { \
422 if (cfg->verbose_level > 2) \
423 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
424 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
425 goto exception_exit; \
427 break_on_unverified (); \
431 #define GET_BBLOCK(cfg,tblock,ip) do { \
432 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
434 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
435 NEW_BBLOCK (cfg, (tblock)); \
436 (tblock)->cil_code = (ip); \
437 ADD_BBLOCK (cfg, (tblock)); \
441 #if defined(TARGET_X86) || defined(TARGET_AMD64)
442 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
443 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
444 (dest)->dreg = alloc_ireg_mp ((cfg)); \
445 (dest)->sreg1 = (sr1); \
446 (dest)->sreg2 = (sr2); \
447 (dest)->inst_imm = (imm); \
448 (dest)->backend.shift_amount = (shift); \
449 MONO_ADD_INS ((cfg)->cbb, (dest)); \
453 /* Emit conversions so both operands of a binary opcode are of the same type */
455 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
457 MonoInst *arg1 = *arg1_ref;
458 MonoInst *arg2 = *arg2_ref;
461 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
462 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
465 /* Mixing r4/r8 is allowed by the spec */
466 if (arg1->type == STACK_R4) {
467 int dreg = alloc_freg (cfg);
469 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
470 conv->type = STACK_R8;
474 if (arg2->type == STACK_R4) {
475 int dreg = alloc_freg (cfg);
477 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
478 conv->type = STACK_R8;
484 #if SIZEOF_REGISTER == 8
485 /* FIXME: Need to add many more cases */
486 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
489 int dr = alloc_preg (cfg);
490 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
491 (ins)->sreg2 = widen->dreg;
496 #define ADD_BINOP(op) do { \
497 MONO_INST_NEW (cfg, ins, (op)); \
499 ins->sreg1 = sp [0]->dreg; \
500 ins->sreg2 = sp [1]->dreg; \
501 type_from_op (cfg, ins, sp [0], sp [1]); \
503 /* Have to insert a widening op */ \
504 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
505 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
506 MONO_ADD_INS ((cfg)->cbb, (ins)); \
507 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
510 #define ADD_UNOP(op) do { \
511 MONO_INST_NEW (cfg, ins, (op)); \
513 ins->sreg1 = sp [0]->dreg; \
514 type_from_op (cfg, ins, sp [0], NULL); \
516 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
517 MONO_ADD_INS ((cfg)->cbb, (ins)); \
518 *sp++ = mono_decompose_opcode (cfg, ins); \
521 #define ADD_BINCOND(next_block) do { \
524 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
525 cmp->sreg1 = sp [0]->dreg; \
526 cmp->sreg2 = sp [1]->dreg; \
527 type_from_op (cfg, cmp, sp [0], sp [1]); \
529 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
530 type_from_op (cfg, ins, sp [0], sp [1]); \
531 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
532 GET_BBLOCK (cfg, tblock, target); \
533 link_bblock (cfg, cfg->cbb, tblock); \
534 ins->inst_true_bb = tblock; \
535 if ((next_block)) { \
536 link_bblock (cfg, cfg->cbb, (next_block)); \
537 ins->inst_false_bb = (next_block); \
538 start_new_bblock = 1; \
540 GET_BBLOCK (cfg, tblock, ip); \
541 link_bblock (cfg, cfg->cbb, tblock); \
542 ins->inst_false_bb = tblock; \
543 start_new_bblock = 2; \
545 if (sp != stack_start) { \
546 handle_stack_args (cfg, stack_start, sp - stack_start); \
547 CHECK_UNVERIFIABLE (cfg); \
549 MONO_ADD_INS (cfg->cbb, cmp); \
550 MONO_ADD_INS (cfg->cbb, ins); \
554 * link_bblock: Links two basic blocks
556 * links two basic blocks in the control flow graph, the 'from'
557 * argument is the starting block and the 'to' argument is the block
558 * the control flow ends to after 'from'.
561 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
563 MonoBasicBlock **newa;
567 if (from->cil_code) {
569 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
571 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
574 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
576 printf ("edge from entry to exit\n");
581 for (i = 0; i < from->out_count; ++i) {
582 if (to == from->out_bb [i]) {
588 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
589 for (i = 0; i < from->out_count; ++i) {
590 newa [i] = from->out_bb [i];
598 for (i = 0; i < to->in_count; ++i) {
599 if (from == to->in_bb [i]) {
605 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
606 for (i = 0; i < to->in_count; ++i) {
607 newa [i] = to->in_bb [i];
616 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
618 link_bblock (cfg, from, to);
622 * mono_find_block_region:
624 * We mark each basic block with a region ID. We use that to avoid BB
625 * optimizations when blocks are in different regions.
628 * A region token that encodes where this region is, and information
629 * about the clause owner for this block.
631 * The region encodes the try/catch/filter clause that owns this block
632 * as well as the type. -1 is a special value that represents a block
633 * that is in none of try/catch/filter.
636 mono_find_block_region (MonoCompile *cfg, int offset)
638 MonoMethodHeader *header = cfg->header;
639 MonoExceptionClause *clause;
642 for (i = 0; i < header->num_clauses; ++i) {
643 clause = &header->clauses [i];
644 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
645 (offset < (clause->handler_offset)))
646 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
648 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
649 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
650 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
651 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
652 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
654 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
657 for (i = 0; i < header->num_clauses; ++i) {
658 clause = &header->clauses [i];
660 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
661 return ((i + 1) << 8) | clause->flags;
668 ip_in_finally_clause (MonoCompile *cfg, int offset)
670 MonoMethodHeader *header = cfg->header;
671 MonoExceptionClause *clause;
674 for (i = 0; i < header->num_clauses; ++i) {
675 clause = &header->clauses [i];
676 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
679 if (MONO_OFFSET_IN_HANDLER (clause, offset))
686 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
688 MonoMethodHeader *header = cfg->header;
689 MonoExceptionClause *clause;
693 for (i = 0; i < header->num_clauses; ++i) {
694 clause = &header->clauses [i];
695 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
696 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
697 if (clause->flags == type)
698 res = g_list_append (res, clause);
705 mono_create_spvar_for_region (MonoCompile *cfg, int region)
709 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
713 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
714 /* prevent it from being register allocated */
715 var->flags |= MONO_INST_VOLATILE;
717 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
721 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
723 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
727 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
731 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
735 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
736 /* prevent it from being register allocated */
737 var->flags |= MONO_INST_VOLATILE;
739 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
745 * Returns the type used in the eval stack when @type is loaded.
746 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
749 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
753 type = mini_get_underlying_type (type);
754 inst->klass = klass = mono_class_from_mono_type (type);
756 inst->type = STACK_MP;
761 switch (type->type) {
763 inst->type = STACK_INV;
771 inst->type = STACK_I4;
776 case MONO_TYPE_FNPTR:
777 inst->type = STACK_PTR;
779 case MONO_TYPE_CLASS:
780 case MONO_TYPE_STRING:
781 case MONO_TYPE_OBJECT:
782 case MONO_TYPE_SZARRAY:
783 case MONO_TYPE_ARRAY:
784 inst->type = STACK_OBJ;
788 inst->type = STACK_I8;
791 inst->type = cfg->r4_stack_type;
794 inst->type = STACK_R8;
796 case MONO_TYPE_VALUETYPE:
797 if (type->data.klass->enumtype) {
798 type = mono_class_enum_basetype (type->data.klass);
802 inst->type = STACK_VTYPE;
805 case MONO_TYPE_TYPEDBYREF:
806 inst->klass = mono_defaults.typed_reference_class;
807 inst->type = STACK_VTYPE;
809 case MONO_TYPE_GENERICINST:
810 type = &type->data.generic_class->container_class->byval_arg;
814 g_assert (cfg->gshared);
815 if (mini_is_gsharedvt_type (type)) {
816 g_assert (cfg->gsharedvt);
817 inst->type = STACK_VTYPE;
819 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
823 g_error ("unknown type 0x%02x in eval stack type", type->type);
828 * The following tables are used to quickly validate the IL code in type_from_op ().
831 bin_num_table [STACK_MAX] [STACK_MAX] = {
832 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
833 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
834 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
835 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
836 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
837 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
838 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
839 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
840 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
845 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
848 /* reduce the size of this table */
850 bin_int_table [STACK_MAX] [STACK_MAX] = {
851 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
852 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
853 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
854 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
855 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
862 bin_comp_table [STACK_MAX] [STACK_MAX] = {
863 /* Inv i L p F & O vt r4 */
865 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
866 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
867 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
868 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
869 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
870 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
871 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
872 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
875 /* reduce the size of this table */
877 shift_table [STACK_MAX] [STACK_MAX] = {
878 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
879 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
880 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
881 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
882 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
883 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
884 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
885 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
889 * Tables to map from the non-specific opcode to the matching
890 * type-specific opcode.
892 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
894 binops_op_map [STACK_MAX] = {
895 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
898 /* handles from CEE_NEG to CEE_CONV_U8 */
900 unops_op_map [STACK_MAX] = {
901 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
904 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
906 ovfops_op_map [STACK_MAX] = {
907 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
910 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
912 ovf2ops_op_map [STACK_MAX] = {
913 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
916 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
918 ovf3ops_op_map [STACK_MAX] = {
919 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
922 /* handles from CEE_BEQ to CEE_BLT_UN */
924 beqops_op_map [STACK_MAX] = {
925 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
928 /* handles from CEE_CEQ to CEE_CLT_UN */
930 ceqops_op_map [STACK_MAX] = {
931 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
935 * Sets ins->type (the type on the eval stack) according to the
936 * type of the opcode and the arguments to it.
937 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
939 * FIXME: this function sets ins->type unconditionally in some cases, but
940 * it should set it to invalid for some types (a conv.x on an object)
943 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
945 switch (ins->opcode) {
952 /* FIXME: check unverifiable args for STACK_MP */
953 ins->type = bin_num_table [src1->type] [src2->type];
954 ins->opcode += binops_op_map [ins->type];
961 ins->type = bin_int_table [src1->type] [src2->type];
962 ins->opcode += binops_op_map [ins->type];
967 ins->type = shift_table [src1->type] [src2->type];
968 ins->opcode += binops_op_map [ins->type];
973 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
974 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
975 ins->opcode = OP_LCOMPARE;
976 else if (src1->type == STACK_R4)
977 ins->opcode = OP_RCOMPARE;
978 else if (src1->type == STACK_R8)
979 ins->opcode = OP_FCOMPARE;
981 ins->opcode = OP_ICOMPARE;
983 case OP_ICOMPARE_IMM:
984 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
985 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
986 ins->opcode = OP_LCOMPARE_IMM;
998 ins->opcode += beqops_op_map [src1->type];
1001 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1002 ins->opcode += ceqops_op_map [src1->type];
1008 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1009 ins->opcode += ceqops_op_map [src1->type];
1013 ins->type = neg_table [src1->type];
1014 ins->opcode += unops_op_map [ins->type];
1017 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1018 ins->type = src1->type;
1020 ins->type = STACK_INV;
1021 ins->opcode += unops_op_map [ins->type];
1027 ins->type = STACK_I4;
1028 ins->opcode += unops_op_map [src1->type];
1031 ins->type = STACK_R8;
1032 switch (src1->type) {
1035 ins->opcode = OP_ICONV_TO_R_UN;
1038 ins->opcode = OP_LCONV_TO_R_UN;
1042 case CEE_CONV_OVF_I1:
1043 case CEE_CONV_OVF_U1:
1044 case CEE_CONV_OVF_I2:
1045 case CEE_CONV_OVF_U2:
1046 case CEE_CONV_OVF_I4:
1047 case CEE_CONV_OVF_U4:
1048 ins->type = STACK_I4;
1049 ins->opcode += ovf3ops_op_map [src1->type];
1051 case CEE_CONV_OVF_I_UN:
1052 case CEE_CONV_OVF_U_UN:
1053 ins->type = STACK_PTR;
1054 ins->opcode += ovf2ops_op_map [src1->type];
1056 case CEE_CONV_OVF_I1_UN:
1057 case CEE_CONV_OVF_I2_UN:
1058 case CEE_CONV_OVF_I4_UN:
1059 case CEE_CONV_OVF_U1_UN:
1060 case CEE_CONV_OVF_U2_UN:
1061 case CEE_CONV_OVF_U4_UN:
1062 ins->type = STACK_I4;
1063 ins->opcode += ovf2ops_op_map [src1->type];
1066 ins->type = STACK_PTR;
1067 switch (src1->type) {
1069 ins->opcode = OP_ICONV_TO_U;
1073 #if SIZEOF_VOID_P == 8
1074 ins->opcode = OP_LCONV_TO_U;
1076 ins->opcode = OP_MOVE;
1080 ins->opcode = OP_LCONV_TO_U;
1083 ins->opcode = OP_FCONV_TO_U;
1089 ins->type = STACK_I8;
1090 ins->opcode += unops_op_map [src1->type];
1092 case CEE_CONV_OVF_I8:
1093 case CEE_CONV_OVF_U8:
1094 ins->type = STACK_I8;
1095 ins->opcode += ovf3ops_op_map [src1->type];
1097 case CEE_CONV_OVF_U8_UN:
1098 case CEE_CONV_OVF_I8_UN:
1099 ins->type = STACK_I8;
1100 ins->opcode += ovf2ops_op_map [src1->type];
1103 ins->type = cfg->r4_stack_type;
1104 ins->opcode += unops_op_map [src1->type];
1107 ins->type = STACK_R8;
1108 ins->opcode += unops_op_map [src1->type];
1111 ins->type = STACK_R8;
1115 ins->type = STACK_I4;
1116 ins->opcode += ovfops_op_map [src1->type];
1119 case CEE_CONV_OVF_I:
1120 case CEE_CONV_OVF_U:
1121 ins->type = STACK_PTR;
1122 ins->opcode += ovfops_op_map [src1->type];
1125 case CEE_ADD_OVF_UN:
1127 case CEE_MUL_OVF_UN:
1129 case CEE_SUB_OVF_UN:
1130 ins->type = bin_num_table [src1->type] [src2->type];
1131 ins->opcode += ovfops_op_map [src1->type];
1132 if (ins->type == STACK_R8)
1133 ins->type = STACK_INV;
1135 case OP_LOAD_MEMBASE:
1136 ins->type = STACK_PTR;
1138 case OP_LOADI1_MEMBASE:
1139 case OP_LOADU1_MEMBASE:
1140 case OP_LOADI2_MEMBASE:
1141 case OP_LOADU2_MEMBASE:
1142 case OP_LOADI4_MEMBASE:
1143 case OP_LOADU4_MEMBASE:
1144 ins->type = STACK_PTR;
1146 case OP_LOADI8_MEMBASE:
1147 ins->type = STACK_I8;
1149 case OP_LOADR4_MEMBASE:
1150 ins->type = cfg->r4_stack_type;
1152 case OP_LOADR8_MEMBASE:
1153 ins->type = STACK_R8;
1156 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1160 if (ins->type == STACK_MP)
1161 ins->klass = mono_defaults.object_class;
1165 ldind_to_type (int op)
1168 case CEE_LDIND_I1: return mono_defaults.sbyte_class;
1169 case CEE_LDIND_U1: return mono_defaults.byte_class;
1170 case CEE_LDIND_I2: return mono_defaults.int16_class;
1171 case CEE_LDIND_U2: return mono_defaults.uint16_class;
1172 case CEE_LDIND_I4: return mono_defaults.int32_class;
1173 case CEE_LDIND_U4: return mono_defaults.uint32_class;
1174 case CEE_LDIND_I8: return mono_defaults.int64_class;
1175 case CEE_LDIND_I: return mono_defaults.int_class;
1176 case CEE_LDIND_R4: return mono_defaults.single_class;
1177 case CEE_LDIND_R8: return mono_defaults.double_class;
1178 case CEE_LDIND_REF:return mono_defaults.object_class; //FIXME we should try to return a more specific type
1179 default: g_error ("Unknown ldind type %d", op);
1186 param_table [STACK_MAX] [STACK_MAX] = {
1191 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1196 switch (args->type) {
1206 for (i = 0; i < sig->param_count; ++i) {
1207 switch (args [i].type) {
1211 if (!sig->params [i]->byref)
1215 if (sig->params [i]->byref)
1217 switch (sig->params [i]->type) {
1218 case MONO_TYPE_CLASS:
1219 case MONO_TYPE_STRING:
1220 case MONO_TYPE_OBJECT:
1221 case MONO_TYPE_SZARRAY:
1222 case MONO_TYPE_ARRAY:
1229 if (sig->params [i]->byref)
1231 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1240 /*if (!param_table [args [i].type] [sig->params [i]->type])
1248 * When we need a pointer to the current domain many times in a method, we
1249 * call mono_domain_get() once and we store the result in a local variable.
1250 * This function returns the variable that represents the MonoDomain*.
1252 inline static MonoInst *
1253 mono_get_domainvar (MonoCompile *cfg)
1255 if (!cfg->domainvar)
1256 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1257 return cfg->domainvar;
1261 * The got_var contains the address of the Global Offset Table when AOT
1265 mono_get_got_var (MonoCompile *cfg)
1267 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1269 if (!cfg->got_var) {
1270 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1272 return cfg->got_var;
1276 mono_create_rgctx_var (MonoCompile *cfg)
1278 if (!cfg->rgctx_var) {
1279 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1280 /* force the var to be stack allocated */
1281 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1286 mono_get_vtable_var (MonoCompile *cfg)
1288 g_assert (cfg->gshared);
1290 mono_create_rgctx_var (cfg);
1292 return cfg->rgctx_var;
1296 type_from_stack_type (MonoInst *ins) {
1297 switch (ins->type) {
1298 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1299 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1300 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1301 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1302 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1304 return &ins->klass->this_arg;
1305 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1306 case STACK_VTYPE: return &ins->klass->byval_arg;
1308 g_error ("stack type %d to monotype not handled\n", ins->type);
1313 static G_GNUC_UNUSED int
1314 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1316 t = mono_type_get_underlying_type (t);
1328 case MONO_TYPE_FNPTR:
1330 case MONO_TYPE_CLASS:
1331 case MONO_TYPE_STRING:
1332 case MONO_TYPE_OBJECT:
1333 case MONO_TYPE_SZARRAY:
1334 case MONO_TYPE_ARRAY:
1340 return cfg->r4_stack_type;
1343 case MONO_TYPE_VALUETYPE:
1344 case MONO_TYPE_TYPEDBYREF:
1346 case MONO_TYPE_GENERICINST:
1347 if (mono_type_generic_inst_is_valuetype (t))
1353 g_assert_not_reached ();
1360 array_access_to_klass (int opcode)
1364 return mono_defaults.byte_class;
1366 return mono_defaults.uint16_class;
1369 return mono_defaults.int_class;
1372 return mono_defaults.sbyte_class;
1375 return mono_defaults.int16_class;
1378 return mono_defaults.int32_class;
1380 return mono_defaults.uint32_class;
1383 return mono_defaults.int64_class;
1386 return mono_defaults.single_class;
1389 return mono_defaults.double_class;
1390 case CEE_LDELEM_REF:
1391 case CEE_STELEM_REF:
1392 return mono_defaults.object_class;
1394 g_assert_not_reached ();
1400 * We try to share variables when possible
1403 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1408 /* inlining can result in deeper stacks */
1409 if (slot >= cfg->header->max_stack)
1410 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1412 pos = ins->type - 1 + slot * STACK_MAX;
1414 switch (ins->type) {
1421 if ((vnum = cfg->intvars [pos]))
1422 return cfg->varinfo [vnum];
1423 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1424 cfg->intvars [pos] = res->inst_c0;
1427 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1433 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1436 * Don't use this if a generic_context is set, since that means AOT can't
1437 * look up the method using just the image+token.
1438 * table == 0 means this is a reference made from a wrapper.
1440 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1441 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1442 jump_info_token->image = image;
1443 jump_info_token->token = token;
1444 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1449 * This function is called to handle items that are left on the evaluation stack
1450 * at basic block boundaries. What happens is that we save the values to local variables
1451 * and we reload them later when first entering the target basic block (with the
1452 * handle_loaded_temps () function).
1453 * A single joint point will use the same variables (stored in the array bb->out_stack or
1454 * bb->in_stack, if the basic block is before or after the joint point).
1456 * This function needs to be called _before_ emitting the last instruction of
1457 * the bb (i.e. before emitting a branch).
1458 * If the stack merge fails at a join point, cfg->unverifiable is set.
1461 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1464 MonoBasicBlock *bb = cfg->cbb;
1465 MonoBasicBlock *outb;
1466 MonoInst *inst, **locals;
1471 if (cfg->verbose_level > 3)
1472 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1473 if (!bb->out_scount) {
1474 bb->out_scount = count;
1475 //printf ("bblock %d has out:", bb->block_num);
1477 for (i = 0; i < bb->out_count; ++i) {
1478 outb = bb->out_bb [i];
1479 /* exception handlers are linked, but they should not be considered for stack args */
1480 if (outb->flags & BB_EXCEPTION_HANDLER)
1482 //printf (" %d", outb->block_num);
1483 if (outb->in_stack) {
1485 bb->out_stack = outb->in_stack;
1491 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1492 for (i = 0; i < count; ++i) {
1494 * try to reuse temps already allocated for this purpouse, if they occupy the same
1495 * stack slot and if they are of the same type.
1496 * This won't cause conflicts since if 'local' is used to
1497 * store one of the values in the in_stack of a bblock, then
1498 * the same variable will be used for the same outgoing stack
1500 * This doesn't work when inlining methods, since the bblocks
1501 * in the inlined methods do not inherit their in_stack from
1502 * the bblock they are inlined to. See bug #58863 for an
1505 if (cfg->inlined_method)
1506 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1508 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1513 for (i = 0; i < bb->out_count; ++i) {
1514 outb = bb->out_bb [i];
1515 /* exception handlers are linked, but they should not be considered for stack args */
1516 if (outb->flags & BB_EXCEPTION_HANDLER)
1518 if (outb->in_scount) {
1519 if (outb->in_scount != bb->out_scount) {
1520 cfg->unverifiable = TRUE;
1523 continue; /* check they are the same locals */
1525 outb->in_scount = count;
1526 outb->in_stack = bb->out_stack;
1529 locals = bb->out_stack;
1531 for (i = 0; i < count; ++i) {
1532 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1533 inst->cil_code = sp [i]->cil_code;
1534 sp [i] = locals [i];
1535 if (cfg->verbose_level > 3)
1536 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1540 * It is possible that the out bblocks already have in_stack assigned, and
1541 * the in_stacks differ. In this case, we will store to all the different
1548 /* Find a bblock which has a different in_stack */
1550 while (bindex < bb->out_count) {
1551 outb = bb->out_bb [bindex];
1552 /* exception handlers are linked, but they should not be considered for stack args */
1553 if (outb->flags & BB_EXCEPTION_HANDLER) {
1557 if (outb->in_stack != locals) {
1558 for (i = 0; i < count; ++i) {
1559 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1560 inst->cil_code = sp [i]->cil_code;
1561 sp [i] = locals [i];
1562 if (cfg->verbose_level > 3)
1563 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1565 locals = outb->in_stack;
1575 mini_emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1579 if (cfg->compile_aot) {
1580 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1586 ji.type = patch_type;
1587 ji.data.target = data;
1588 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1589 mono_error_assert_ok (&error);
1591 EMIT_NEW_PCONST (cfg, ins, target);
1597 mono_create_fast_tls_getter (MonoCompile *cfg, MonoTlsKey key)
1599 int tls_offset = mono_tls_get_tls_offset (key);
1601 if (cfg->compile_aot)
1604 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1606 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
1607 ins->dreg = mono_alloc_preg (cfg);
1608 ins->inst_offset = tls_offset;
1615 mono_create_fast_tls_setter (MonoCompile *cfg, MonoInst* value, MonoTlsKey key)
1617 int tls_offset = mono_tls_get_tls_offset (key);
1619 if (cfg->compile_aot)
1622 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1624 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1625 ins->sreg1 = value->dreg;
1626 ins->inst_offset = tls_offset;
1634 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
1636 MonoInst *fast_tls = NULL;
1638 if (!mini_get_debug_options ()->use_fallback_tls)
1639 fast_tls = mono_create_fast_tls_getter (cfg, key);
1642 MONO_ADD_INS (cfg->cbb, fast_tls);
1646 if (cfg->compile_aot) {
1649 * tls getters are critical pieces of code and we don't want to resolve them
1650 * through the standard plt/tramp mechanism since we might expose ourselves
1651 * to crashes and infinite recursions.
1653 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GET_TLS_TRAMP, (void*)key);
1654 return mini_emit_calli (cfg, helper_sig_get_tls_tramp, NULL, addr, NULL, NULL);
1656 gpointer getter = mono_tls_get_tls_getter (key, FALSE);
1657 return mono_emit_jit_icall (cfg, getter, NULL);
1662 mono_create_tls_set (MonoCompile *cfg, MonoInst *value, MonoTlsKey key)
1664 MonoInst *fast_tls = NULL;
1666 if (!mini_get_debug_options ()->use_fallback_tls)
1667 fast_tls = mono_create_fast_tls_setter (cfg, value, key);
1670 MONO_ADD_INS (cfg->cbb, fast_tls);
1674 if (cfg->compile_aot) {
1676 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_SET_TLS_TRAMP, (void*)key);
1677 return mini_emit_calli (cfg, helper_sig_set_tls_tramp, &value, addr, NULL, NULL);
1679 gpointer setter = mono_tls_get_tls_setter (key, FALSE);
1680 return mono_emit_jit_icall (cfg, setter, &value);
1687 * Emit IR to push the current LMF onto the LMF stack.
1690 emit_push_lmf (MonoCompile *cfg)
1693 * Emit IR to push the LMF:
1694 * lmf_addr = <lmf_addr from tls>
1695 * lmf->lmf_addr = lmf_addr
1696 * lmf->prev_lmf = *lmf_addr
1699 MonoInst *ins, *lmf_ins;
1704 int lmf_reg, prev_lmf_reg;
1706 * Store lmf_addr in a variable, so it can be allocated to a global register.
1708 if (!cfg->lmf_addr_var)
1709 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1712 ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
1714 int jit_tls_dreg = ins->dreg;
1716 lmf_reg = alloc_preg (cfg);
1717 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
1719 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
1722 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
1724 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1725 lmf_reg = ins->dreg;
1727 prev_lmf_reg = alloc_preg (cfg);
1728 /* Save previous_lmf */
1729 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
1730 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
1732 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
1738 * Emit IR to pop the current LMF from the LMF stack.
1741 emit_pop_lmf (MonoCompile *cfg)
1743 int lmf_reg, lmf_addr_reg;
1749 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1750 lmf_reg = ins->dreg;
1754 * Emit IR to pop the LMF:
1755 * *(lmf->lmf_addr) = lmf->prev_lmf
1757 /* This could be called before emit_push_lmf () */
1758 if (!cfg->lmf_addr_var)
1759 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1760 lmf_addr_reg = cfg->lmf_addr_var->dreg;
1762 prev_lmf_reg = alloc_preg (cfg);
1763 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1764 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
1768 emit_instrumentation_call (MonoCompile *cfg, void *func)
1770 MonoInst *iargs [1];
1773 * Avoid instrumenting inlined methods since it can
1774 * distort profiling results.
1776 if (cfg->method != cfg->current_method)
1779 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
1780 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
1781 mono_emit_jit_icall (cfg, func, iargs);
1786 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
1789 type = mini_get_underlying_type (type);
1790 switch (type->type) {
1791 case MONO_TYPE_VOID:
1792 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
1799 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1803 case MONO_TYPE_FNPTR:
1804 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1805 case MONO_TYPE_CLASS:
1806 case MONO_TYPE_STRING:
1807 case MONO_TYPE_OBJECT:
1808 case MONO_TYPE_SZARRAY:
1809 case MONO_TYPE_ARRAY:
1810 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1813 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
1816 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
1818 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1820 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1821 case MONO_TYPE_VALUETYPE:
1822 if (type->data.klass->enumtype) {
1823 type = mono_class_enum_basetype (type->data.klass);
1826 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1827 case MONO_TYPE_TYPEDBYREF:
1828 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1829 case MONO_TYPE_GENERICINST:
1830 type = &type->data.generic_class->container_class->byval_arg;
1833 case MONO_TYPE_MVAR:
1835 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1837 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1842 //XXX this ignores if t is byref
1843 #define MONO_TYPE_IS_PRIMITIVE_SCALAR(t) ((((((t)->type >= MONO_TYPE_BOOLEAN && (t)->type <= MONO_TYPE_U8) || ((t)->type >= MONO_TYPE_I && (t)->type <= MONO_TYPE_U)))))
1846 * target_type_is_incompatible:
1847 * @cfg: MonoCompile context
1849 * Check that the item @arg on the evaluation stack can be stored
1850 * in the target type (can be a local, or field, etc).
1851 * The cfg arg can be used to check if we need verification or just
1854 * Returns: non-0 value if arg can't be stored on a target.
1857 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
1859 MonoType *simple_type;
1862 if (target->byref) {
1863 /* FIXME: check that the pointed to types match */
1864 if (arg->type == STACK_MP) {
1865 /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
1866 MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
1867 MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
1869 /* if the target is native int& or same type */
1870 if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
1873 /* Both are primitive type byrefs and the source points to a larger type that the destination */
1874 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
1875 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
1879 if (arg->type == STACK_PTR)
1884 simple_type = mini_get_underlying_type (target);
1885 switch (simple_type->type) {
1886 case MONO_TYPE_VOID:
1894 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
1898 /* STACK_MP is needed when setting pinned locals */
1899 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1904 case MONO_TYPE_FNPTR:
1906 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
1907 * in native int. (#688008).
1909 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1912 case MONO_TYPE_CLASS:
1913 case MONO_TYPE_STRING:
1914 case MONO_TYPE_OBJECT:
1915 case MONO_TYPE_SZARRAY:
1916 case MONO_TYPE_ARRAY:
1917 if (arg->type != STACK_OBJ)
1919 /* FIXME: check type compatibility */
1923 if (arg->type != STACK_I8)
1927 if (arg->type != cfg->r4_stack_type)
1931 if (arg->type != STACK_R8)
1934 case MONO_TYPE_VALUETYPE:
1935 if (arg->type != STACK_VTYPE)
1937 klass = mono_class_from_mono_type (simple_type);
1938 if (klass != arg->klass)
1941 case MONO_TYPE_TYPEDBYREF:
1942 if (arg->type != STACK_VTYPE)
1944 klass = mono_class_from_mono_type (simple_type);
1945 if (klass != arg->klass)
1948 case MONO_TYPE_GENERICINST:
1949 if (mono_type_generic_inst_is_valuetype (simple_type)) {
1950 MonoClass *target_class;
1951 if (arg->type != STACK_VTYPE)
1953 klass = mono_class_from_mono_type (simple_type);
1954 target_class = mono_class_from_mono_type (target);
1955 /* The second cases is needed when doing partial sharing */
1956 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
1960 if (arg->type != STACK_OBJ)
1962 /* FIXME: check type compatibility */
1966 case MONO_TYPE_MVAR:
1967 g_assert (cfg->gshared);
1968 if (mini_type_var_is_vt (simple_type)) {
1969 if (arg->type != STACK_VTYPE)
1972 if (arg->type != STACK_OBJ)
1977 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
1983 * Prepare arguments for passing to a function call.
1984 * Return a non-zero value if the arguments can't be passed to the given
1986 * The type checks are not yet complete and some conversions may need
1987 * casts on 32 or 64 bit architectures.
1989 * FIXME: implement this using target_type_is_incompatible ()
1992 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1994 MonoType *simple_type;
1998 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2002 for (i = 0; i < sig->param_count; ++i) {
2003 if (sig->params [i]->byref) {
2004 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2008 simple_type = mini_get_underlying_type (sig->params [i]);
2010 switch (simple_type->type) {
2011 case MONO_TYPE_VOID:
2020 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2026 case MONO_TYPE_FNPTR:
2027 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2030 case MONO_TYPE_CLASS:
2031 case MONO_TYPE_STRING:
2032 case MONO_TYPE_OBJECT:
2033 case MONO_TYPE_SZARRAY:
2034 case MONO_TYPE_ARRAY:
2035 if (args [i]->type != STACK_OBJ)
2040 if (args [i]->type != STACK_I8)
2044 if (args [i]->type != cfg->r4_stack_type)
2048 if (args [i]->type != STACK_R8)
2051 case MONO_TYPE_VALUETYPE:
2052 if (simple_type->data.klass->enumtype) {
2053 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2056 if (args [i]->type != STACK_VTYPE)
2059 case MONO_TYPE_TYPEDBYREF:
2060 if (args [i]->type != STACK_VTYPE)
2063 case MONO_TYPE_GENERICINST:
2064 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2067 case MONO_TYPE_MVAR:
2069 if (args [i]->type != STACK_VTYPE)
2073 g_error ("unknown type 0x%02x in check_call_signature",
2081 callvirt_to_call (int opcode)
2084 case OP_CALL_MEMBASE:
2086 case OP_VOIDCALL_MEMBASE:
2088 case OP_FCALL_MEMBASE:
2090 case OP_RCALL_MEMBASE:
2092 case OP_VCALL_MEMBASE:
2094 case OP_LCALL_MEMBASE:
2097 g_assert_not_reached ();
2104 callvirt_to_call_reg (int opcode)
2107 case OP_CALL_MEMBASE:
2109 case OP_VOIDCALL_MEMBASE:
2110 return OP_VOIDCALL_REG;
2111 case OP_FCALL_MEMBASE:
2112 return OP_FCALL_REG;
2113 case OP_RCALL_MEMBASE:
2114 return OP_RCALL_REG;
2115 case OP_VCALL_MEMBASE:
2116 return OP_VCALL_REG;
2117 case OP_LCALL_MEMBASE:
2118 return OP_LCALL_REG;
2120 g_assert_not_reached ();
2126 /* Either METHOD or IMT_ARG needs to be set */
2128 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2132 if (COMPILE_LLVM (cfg)) {
2134 method_reg = alloc_preg (cfg);
2135 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2137 MonoInst *ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2138 method_reg = ins->dreg;
2142 call->imt_arg_reg = method_reg;
2144 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2149 method_reg = alloc_preg (cfg);
2150 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2152 MonoInst *ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2153 method_reg = ins->dreg;
2156 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2159 static MonoJumpInfo *
2160 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2162 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2166 ji->data.target = target;
2172 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2175 return mono_class_check_context_used (klass);
2181 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2184 return mono_method_check_context_used (method);
2190 * check_method_sharing:
2192 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2195 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2197 gboolean pass_vtable = FALSE;
2198 gboolean pass_mrgctx = FALSE;
2200 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2201 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2202 gboolean sharable = FALSE;
2204 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2208 * Pass vtable iff target method might
2209 * be shared, which means that sharing
2210 * is enabled for its class and its
2211 * context is sharable (and it's not a
2214 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2218 if (mini_method_get_context (cmethod) &&
2219 mini_method_get_context (cmethod)->method_inst) {
2220 g_assert (!pass_vtable);
2222 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2225 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2230 if (out_pass_vtable)
2231 *out_pass_vtable = pass_vtable;
2232 if (out_pass_mrgctx)
2233 *out_pass_mrgctx = pass_mrgctx;
2236 inline static MonoCallInst *
2237 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2238 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2242 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2250 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2252 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2254 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2257 call->signature = sig;
2258 call->rgctx_reg = rgctx;
2259 sig_ret = mini_get_underlying_type (sig->ret);
2261 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2264 if (mini_type_is_vtype (sig_ret)) {
2265 call->vret_var = cfg->vret_addr;
2266 //g_assert_not_reached ();
2268 } else if (mini_type_is_vtype (sig_ret)) {
2269 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2272 temp->backend.is_pinvoke = sig->pinvoke;
2275 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2276 * address of return value to increase optimization opportunities.
2277 * Before vtype decomposition, the dreg of the call ins itself represents the
2278 * fact the call modifies the return value. After decomposition, the call will
2279 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2280 * will be transformed into an LDADDR.
2282 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2283 loada->dreg = alloc_preg (cfg);
2284 loada->inst_p0 = temp;
2285 /* We reference the call too since call->dreg could change during optimization */
2286 loada->inst_p1 = call;
2287 MONO_ADD_INS (cfg->cbb, loada);
2289 call->inst.dreg = temp->dreg;
2291 call->vret_var = loada;
2292 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2293 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2295 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2296 if (COMPILE_SOFT_FLOAT (cfg)) {
2298 * If the call has a float argument, we would need to do an r8->r4 conversion using
2299 * an icall, but that cannot be done during the call sequence since it would clobber
2300 * the call registers + the stack. So we do it before emitting the call.
2302 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2304 MonoInst *in = call->args [i];
2306 if (i >= sig->hasthis)
2307 t = sig->params [i - sig->hasthis];
2309 t = &mono_defaults.int_class->byval_arg;
2310 t = mono_type_get_underlying_type (t);
2312 if (!t->byref && t->type == MONO_TYPE_R4) {
2313 MonoInst *iargs [1];
2317 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2319 /* The result will be in an int vreg */
2320 call->args [i] = conv;
2326 call->need_unbox_trampoline = unbox_trampoline;
2329 if (COMPILE_LLVM (cfg))
2330 mono_llvm_emit_call (cfg, call);
2332 mono_arch_emit_call (cfg, call);
2334 mono_arch_emit_call (cfg, call);
2337 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2338 cfg->flags |= MONO_CFG_HAS_CALLS;
2344 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2346 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2347 cfg->uses_rgctx_reg = TRUE;
2348 call->rgctx_reg = TRUE;
2350 call->rgctx_arg_reg = rgctx_reg;
2355 mini_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2360 gboolean check_sp = FALSE;
2362 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2363 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2365 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2370 rgctx_reg = mono_alloc_preg (cfg);
2371 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2375 if (!cfg->stack_inbalance_var)
2376 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2378 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2379 ins->dreg = cfg->stack_inbalance_var->dreg;
2380 MONO_ADD_INS (cfg->cbb, ins);
2383 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2385 call->inst.sreg1 = addr->dreg;
2388 emit_imt_argument (cfg, call, NULL, imt_arg);
2390 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2395 sp_reg = mono_alloc_preg (cfg);
2397 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2399 MONO_ADD_INS (cfg->cbb, ins);
2401 /* Restore the stack so we don't crash when throwing the exception */
2402 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2403 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2404 MONO_ADD_INS (cfg->cbb, ins);
2406 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2407 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2411 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2413 return (MonoInst*)call;
2417 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2420 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2421 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2423 #ifndef DISABLE_REMOTING
2424 gboolean might_be_remote = FALSE;
2426 gboolean virtual_ = this_ins != NULL;
2427 gboolean enable_for_aot = TRUE;
2430 MonoInst *call_target = NULL;
2432 gboolean need_unbox_trampoline;
2435 sig = mono_method_signature (method);
2437 if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2438 g_assert_not_reached ();
2441 rgctx_reg = mono_alloc_preg (cfg);
2442 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2445 if (method->string_ctor) {
2446 /* Create the real signature */
2447 /* FIXME: Cache these */
2448 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2449 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2454 context_used = mini_method_check_context_used (cfg, method);
2456 #ifndef DISABLE_REMOTING
2457 might_be_remote = this_ins && sig->hasthis &&
2458 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2459 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2461 if (might_be_remote && context_used) {
2464 g_assert (cfg->gshared);
2466 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2468 return mini_emit_calli (cfg, sig, args, addr, NULL, NULL);
2472 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2473 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2475 need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2477 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2479 #ifndef DISABLE_REMOTING
2480 if (might_be_remote)
2481 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2484 call->method = method;
2485 call->inst.flags |= MONO_INST_HAS_METHOD;
2486 call->inst.inst_left = this_ins;
2487 call->tail_call = tail;
2490 int vtable_reg, slot_reg, this_reg;
2493 this_reg = this_ins->dreg;
2495 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2496 MonoInst *dummy_use;
2498 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2500 /* Make a call to delegate->invoke_impl */
2501 call->inst.inst_basereg = this_reg;
2502 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2503 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2505 /* We must emit a dummy use here because the delegate trampoline will
2506 replace the 'this' argument with the delegate target making this activation
2507 no longer a root for the delegate.
2508 This is an issue for delegates that target collectible code such as dynamic
2509 methods of GC'able assemblies.
2511 For a test case look into #667921.
2513 FIXME: a dummy use is not the best way to do it as the local register allocator
2514 will put it on a caller save register and spil it around the call.
2515 Ideally, we would either put it on a callee save register or only do the store part.
2517 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2519 return (MonoInst*)call;
2522 if ((!cfg->compile_aot || enable_for_aot) &&
2523 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2524 (MONO_METHOD_IS_FINAL (method) &&
2525 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2526 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2528 * the method is not virtual, we just need to ensure this is not null
2529 * and then we can call the method directly.
2531 #ifndef DISABLE_REMOTING
2532 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2534 * The check above ensures method is not gshared, this is needed since
2535 * gshared methods can't have wrappers.
2537 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2541 if (!method->string_ctor)
2542 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2544 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2545 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2547 * the method is virtual, but we can statically dispatch since either
2548 * it's class or the method itself are sealed.
2549 * But first we need to ensure it's not a null reference.
2551 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2553 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2554 } else if (call_target) {
2555 vtable_reg = alloc_preg (cfg);
2556 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2558 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2559 call->inst.sreg1 = call_target->dreg;
2560 call->inst.flags &= !MONO_INST_HAS_METHOD;
2562 vtable_reg = alloc_preg (cfg);
2563 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2564 if (mono_class_is_interface (method->klass)) {
2565 guint32 imt_slot = mono_method_get_imt_slot (method);
2566 emit_imt_argument (cfg, call, call->method, imt_arg);
2567 slot_reg = vtable_reg;
2568 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2570 slot_reg = vtable_reg;
2571 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2572 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2574 g_assert (mono_method_signature (method)->generic_param_count);
2575 emit_imt_argument (cfg, call, call->method, imt_arg);
2579 call->inst.sreg1 = slot_reg;
2580 call->inst.inst_offset = offset;
2581 call->is_virtual = TRUE;
2585 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2588 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2590 return (MonoInst*)call;
2594 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2596 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2600 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2607 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2610 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2612 return (MonoInst*)call;
2616 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2618 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2622 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2626 * mono_emit_abs_call:
2628 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2630 inline static MonoInst*
2631 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2632 MonoMethodSignature *sig, MonoInst **args)
2634 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2638 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2641 if (cfg->abs_patches == NULL)
2642 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2643 g_hash_table_insert (cfg->abs_patches, ji, ji);
2644 ins = mono_emit_native_call (cfg, ji, sig, args);
2645 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2649 static MonoMethodSignature*
2650 sig_to_rgctx_sig (MonoMethodSignature *sig)
2652 // FIXME: memory allocation
2653 MonoMethodSignature *res;
2656 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2657 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2658 res->param_count = sig->param_count + 1;
2659 for (i = 0; i < sig->param_count; ++i)
2660 res->params [i] = sig->params [i];
2661 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2665 /* Make an indirect call to FSIG passing an additional argument */
2667 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
2669 MonoMethodSignature *csig;
2670 MonoInst *args_buf [16];
2672 int i, pindex, tmp_reg;
2674 /* Make a call with an rgctx/extra arg */
2675 if (fsig->param_count + 2 < 16)
2678 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
2681 args [pindex ++] = orig_args [0];
2682 for (i = 0; i < fsig->param_count; ++i)
2683 args [pindex ++] = orig_args [fsig->hasthis + i];
2684 tmp_reg = alloc_preg (cfg);
2685 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
2686 csig = sig_to_rgctx_sig (fsig);
2687 return mini_emit_calli (cfg, csig, args, call_target, NULL, NULL);
2690 /* Emit an indirect call to the function descriptor ADDR */
2692 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
2694 int addr_reg, arg_reg;
2695 MonoInst *call_target;
2697 g_assert (cfg->llvm_only);
2700 * addr points to a <addr, arg> pair, load both of them, and
2701 * make a call to addr, passing arg as an extra arg.
2703 addr_reg = alloc_preg (cfg);
2704 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
2705 arg_reg = alloc_preg (cfg);
2706 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
2708 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
2712 direct_icalls_enabled (MonoCompile *cfg)
2716 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2718 if (cfg->compile_llvm && !cfg->llvm_only)
2721 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2727 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
2730 * Call the jit icall without a wrapper if possible.
2731 * The wrapper is needed for the following reasons:
2732 * - to handle exceptions thrown using mono_raise_exceptions () from the
2733 * icall function. The EH code needs the lmf frame pushed by the
2734 * wrapper to be able to unwind back to managed code.
2735 * - to be able to do stack walks for asynchronously suspended
2736 * threads when debugging.
2738 if (info->no_raise && direct_icalls_enabled (cfg)) {
2742 if (!info->wrapper_method) {
2743 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2744 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2746 mono_memory_barrier ();
2750 * Inline the wrapper method, which is basically a call to the C icall, and
2751 * an exception check.
2753 costs = inline_method (cfg, info->wrapper_method, NULL,
2754 args, NULL, il_offset, TRUE);
2755 g_assert (costs > 0);
2756 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2760 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2765 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2767 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2768 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2772 * Native code might return non register sized integers
2773 * without initializing the upper bits.
2775 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
2776 case OP_LOADI1_MEMBASE:
2777 widen_op = OP_ICONV_TO_I1;
2779 case OP_LOADU1_MEMBASE:
2780 widen_op = OP_ICONV_TO_U1;
2782 case OP_LOADI2_MEMBASE:
2783 widen_op = OP_ICONV_TO_I2;
2785 case OP_LOADU2_MEMBASE:
2786 widen_op = OP_ICONV_TO_U2;
2792 if (widen_op != -1) {
2793 int dreg = alloc_preg (cfg);
2796 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
2797 widen->type = ins->type;
2808 emit_method_access_failure (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
2810 MonoInst *args [16];
2812 args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (caller), caller, MONO_RGCTX_INFO_METHOD);
2813 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (callee), callee, MONO_RGCTX_INFO_METHOD);
2815 mono_emit_jit_icall (cfg, mono_throw_method_access, args);
2819 mini_get_memcpy_method (void)
2821 static MonoMethod *memcpy_method = NULL;
2822 if (!memcpy_method) {
2823 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2825 g_error ("Old corlib found. Install a new one");
2827 return memcpy_method;
2831 mini_emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
2833 int card_table_shift_bits;
2834 gpointer card_table_mask;
2836 MonoInst *dummy_use;
2837 int nursery_shift_bits;
2838 size_t nursery_size;
2840 if (!cfg->gen_write_barriers)
2843 //method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1])
2845 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
2847 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
2849 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
2852 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
2853 wbarrier->sreg1 = ptr->dreg;
2854 wbarrier->sreg2 = value->dreg;
2855 MONO_ADD_INS (cfg->cbb, wbarrier);
2856 } else if (card_table) {
2857 int offset_reg = alloc_preg (cfg);
2862 * We emit a fast light weight write barrier. This always marks cards as in the concurrent
2863 * collector case, so, for the serial collector, it might slightly slow down nursery
2864 * collections. We also expect that the host system and the target system have the same card
2865 * table configuration, which is the case if they have the same pointer size.
2868 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
2869 if (card_table_mask)
2870 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
2872 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
2873 * IMM's larger than 32bits.
2875 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
2876 card_reg = ins->dreg;
2878 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
2879 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
2881 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
2882 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
2885 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
2889 mini_get_memset_method (void)
2891 static MonoMethod *memset_method = NULL;
2892 if (!memset_method) {
2893 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2895 g_error ("Old corlib found. Install a new one");
2897 return memset_method;
2901 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
2903 MonoInst *iargs [3];
2906 MonoMethod *memset_method;
2907 MonoInst *size_ins = NULL;
2908 MonoInst *bzero_ins = NULL;
2909 static MonoMethod *bzero_method;
2911 /* FIXME: Optimize this for the case when dest is an LDADDR */
2912 mono_class_init (klass);
2913 if (mini_is_gsharedvt_klass (klass)) {
2914 size_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
2915 bzero_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
2917 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
2918 g_assert (bzero_method);
2920 iargs [1] = size_ins;
2921 mini_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
2925 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
2927 n = mono_class_value_size (klass, &align);
2929 if (n <= sizeof (gpointer) * 8) {
2930 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
2933 memset_method = mini_get_memset_method ();
2935 EMIT_NEW_ICONST (cfg, iargs [1], 0);
2936 EMIT_NEW_ICONST (cfg, iargs [2], n);
2937 mono_emit_method_call (cfg, memset_method, iargs, NULL);
2944 * Emit IR to return either the this pointer for instance method,
2945 * or the mrgctx for static methods.
2948 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
2950 MonoInst *this_ins = NULL;
2952 g_assert (cfg->gshared);
2954 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
2955 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
2956 !method->klass->valuetype)
2957 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
2959 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
2960 MonoInst *mrgctx_loc, *mrgctx_var;
2962 g_assert (!this_ins);
2963 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
2965 mrgctx_loc = mono_get_vtable_var (cfg);
2966 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
2969 } else if (MONO_CLASS_IS_INTERFACE (cfg->method->klass)) {
2970 MonoInst *mrgctx_loc, *mrgctx_var;
2972 /* Default interface methods need an mrgctx since the vtabke at runtime points at an implementing class */
2973 mrgctx_loc = mono_get_vtable_var (cfg);
2974 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
2976 g_assert (mono_method_needs_static_rgctx_invoke (cfg->method, TRUE));
2979 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
2980 MonoInst *vtable_loc, *vtable_var;
2982 g_assert (!this_ins);
2984 vtable_loc = mono_get_vtable_var (cfg);
2985 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
2987 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
2988 MonoInst *mrgctx_var = vtable_var;
2991 vtable_reg = alloc_preg (cfg);
2992 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
2993 vtable_var->type = STACK_PTR;
3001 vtable_reg = alloc_preg (cfg);
3002 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3007 static MonoJumpInfoRgctxEntry *
3008 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3010 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3011 res->method = method;
3012 res->in_mrgctx = in_mrgctx;
3013 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3014 res->data->type = patch_type;
3015 res->data->data.target = patch_data;
3016 res->info_type = info_type;
3021 static inline MonoInst*
3022 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3024 MonoInst *args [16];
3027 // FIXME: No fastpath since the slot is not a compile time constant
3029 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3030 if (entry->in_mrgctx)
3031 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3033 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3037 * FIXME: This can be called during decompose, which is a problem since it creates
3039 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3041 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3043 MonoBasicBlock *is_null_bb, *end_bb;
3044 MonoInst *res, *ins, *call;
3047 slot = mini_get_rgctx_entry_slot (entry);
3049 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3050 index = MONO_RGCTX_SLOT_INDEX (slot);
3052 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3053 for (depth = 0; ; ++depth) {
3054 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3056 if (index < size - 1)
3061 NEW_BBLOCK (cfg, end_bb);
3062 NEW_BBLOCK (cfg, is_null_bb);
3065 rgctx_reg = rgctx->dreg;
3067 rgctx_reg = alloc_preg (cfg);
3069 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3070 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3071 NEW_BBLOCK (cfg, is_null_bb);
3073 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3074 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3077 for (i = 0; i < depth; ++i) {
3078 int array_reg = alloc_preg (cfg);
3080 /* load ptr to next array */
3081 if (mrgctx && i == 0)
3082 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3084 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3085 rgctx_reg = array_reg;
3086 /* is the ptr null? */
3087 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3088 /* if yes, jump to actual trampoline */
3089 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3093 val_reg = alloc_preg (cfg);
3094 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3095 /* is the slot null? */
3096 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3097 /* if yes, jump to actual trampoline */
3098 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3101 res_reg = alloc_preg (cfg);
3102 MONO_INST_NEW (cfg, ins, OP_MOVE);
3103 ins->dreg = res_reg;
3104 ins->sreg1 = val_reg;
3105 MONO_ADD_INS (cfg->cbb, ins);
3107 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3110 MONO_START_BB (cfg, is_null_bb);
3112 EMIT_NEW_ICONST (cfg, args [1], index);
3114 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3116 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3117 MONO_INST_NEW (cfg, ins, OP_MOVE);
3118 ins->dreg = res_reg;
3119 ins->sreg1 = call->dreg;
3120 MONO_ADD_INS (cfg->cbb, ins);
3121 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3123 MONO_START_BB (cfg, end_bb);
3132 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3135 static inline MonoInst*
3136 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3139 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3141 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3145 mini_emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3146 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3148 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3149 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3151 return emit_rgctx_fetch (cfg, rgctx, entry);
3155 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3156 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3158 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3159 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3161 return emit_rgctx_fetch (cfg, rgctx, entry);
3165 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3166 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3168 MonoJumpInfoGSharedVtCall *call_info;
3169 MonoJumpInfoRgctxEntry *entry;
3172 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3173 call_info->sig = sig;
3174 call_info->method = cmethod;
3176 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3177 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3179 return emit_rgctx_fetch (cfg, rgctx, entry);
3183 * emit_get_rgctx_virt_method:
3185 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3188 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3189 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3191 MonoJumpInfoVirtMethod *info;
3192 MonoJumpInfoRgctxEntry *entry;
3195 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3196 info->klass = klass;
3197 info->method = virt_method;
3199 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_VIRT_METHOD, info, rgctx_type);
3200 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3202 return emit_rgctx_fetch (cfg, rgctx, entry);
3206 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3207 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3209 MonoJumpInfoRgctxEntry *entry;
3212 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3213 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3215 return emit_rgctx_fetch (cfg, rgctx, entry);
3219 * emit_get_rgctx_method:
3221 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3222 * normal constants, else emit a load from the rgctx.
3225 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3226 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3228 if (!context_used) {
3231 switch (rgctx_type) {
3232 case MONO_RGCTX_INFO_METHOD:
3233 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3235 case MONO_RGCTX_INFO_METHOD_RGCTX:
3236 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3239 g_assert_not_reached ();
3242 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3243 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3245 return emit_rgctx_fetch (cfg, rgctx, entry);
3250 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3251 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3253 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3254 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3256 return emit_rgctx_fetch (cfg, rgctx, entry);
3260 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3262 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3263 MonoRuntimeGenericContextInfoTemplate *template_;
3268 for (i = 0; i < info->num_entries; ++i) {
3269 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3271 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3275 if (info->num_entries == info->count_entries) {
3276 MonoRuntimeGenericContextInfoTemplate *new_entries;
3277 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3279 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3281 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3282 info->entries = new_entries;
3283 info->count_entries = new_count_entries;
3286 idx = info->num_entries;
3287 template_ = &info->entries [idx];
3288 template_->info_type = rgctx_type;
3289 template_->data = data;
3291 info->num_entries ++;
3297 * emit_get_gsharedvt_info:
3299 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3302 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3307 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3308 /* Load info->entries [idx] */
3309 dreg = alloc_preg (cfg);
3310 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3316 mini_emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3318 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3322 * On return the caller must check @klass for load errors.
3325 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3327 MonoInst *vtable_arg;
3330 context_used = mini_class_check_context_used (cfg, klass);
3333 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
3334 klass, MONO_RGCTX_INFO_VTABLE);
3336 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3340 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3343 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3347 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3348 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3350 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3351 ins->sreg1 = vtable_arg->dreg;
3352 MONO_ADD_INS (cfg->cbb, ins);
3355 MonoBasicBlock *inited_bb;
3356 MonoInst *args [16];
3358 inited_reg = alloc_ireg (cfg);
3360 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, inited_reg, vtable_arg->dreg, MONO_STRUCT_OFFSET (MonoVTable, initialized));
3362 NEW_BBLOCK (cfg, inited_bb);
3364 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3365 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3367 args [0] = vtable_arg;
3368 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3370 MONO_START_BB (cfg, inited_bb);
3375 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3379 if (cfg->gen_seq_points && cfg->method == method) {
3380 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3382 ins->flags |= MONO_INST_NONEMPTY_STACK;
3383 MONO_ADD_INS (cfg->cbb, ins);
3388 mini_save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3390 if (mini_get_debug_options ()->better_cast_details) {
3391 int vtable_reg = alloc_preg (cfg);
3392 int klass_reg = alloc_preg (cfg);
3393 MonoBasicBlock *is_null_bb = NULL;
3395 int to_klass_reg, context_used;
3398 NEW_BBLOCK (cfg, is_null_bb);
3400 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3401 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3404 tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3406 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3410 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3411 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3413 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3415 context_used = mini_class_check_context_used (cfg, klass);
3417 MonoInst *class_ins;
3419 class_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3420 to_klass_reg = class_ins->dreg;
3422 to_klass_reg = alloc_preg (cfg);
3423 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3425 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3428 MONO_START_BB (cfg, is_null_bb);
3433 mini_reset_cast_details (MonoCompile *cfg)
3435 /* Reset the variables holding the cast details */
3436 if (mini_get_debug_options ()->better_cast_details) {
3437 MonoInst *tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3438 /* It is enough to reset the from field */
3439 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3444 * On return the caller must check @array_class for load errors
3447 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3449 int vtable_reg = alloc_preg (cfg);
3452 context_used = mini_class_check_context_used (cfg, array_class);
3454 mini_save_cast_details (cfg, array_class, obj->dreg, FALSE);
3456 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3458 if (cfg->opt & MONO_OPT_SHARED) {
3459 int class_reg = alloc_preg (cfg);
3462 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3463 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3464 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3465 } else if (context_used) {
3466 MonoInst *vtable_ins;
3468 vtable_ins = mini_emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3469 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3471 if (cfg->compile_aot) {
3475 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3477 vt_reg = alloc_preg (cfg);
3478 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3479 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3482 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3484 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3488 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3490 mini_reset_cast_details (cfg);
3494 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
3495 * generic code is generated.
3498 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3500 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3503 MonoInst *rgctx, *addr;
3505 /* FIXME: What if the class is shared? We might not
3506 have to get the address of the method from the
3508 addr = emit_get_rgctx_method (cfg, context_used, method,
3509 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3510 if (cfg->llvm_only) {
3511 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
3512 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3514 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3516 return mini_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3519 gboolean pass_vtable, pass_mrgctx;
3520 MonoInst *rgctx_arg = NULL;
3522 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3523 g_assert (!pass_mrgctx);
3526 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3529 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3532 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3537 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3541 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3542 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3543 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3544 int rank_reg = alloc_dreg (cfg ,STACK_I4);
3546 obj_reg = sp [0]->dreg;
3547 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3548 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3550 /* FIXME: generics */
3551 g_assert (klass->rank == 0);
3554 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3555 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3557 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3558 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3561 MonoInst *element_class;
3563 /* This assertion is from the unboxcast insn */
3564 g_assert (klass->rank == 0);
3566 element_class = mini_emit_get_rgctx_klass (cfg, context_used,
3567 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3569 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3570 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3572 mini_save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3573 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3574 mini_reset_cast_details (cfg);
3577 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3578 MONO_ADD_INS (cfg->cbb, add);
3579 add->type = STACK_MP;
3586 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3588 MonoInst *addr, *klass_inst, *is_ref, *args[16];
3589 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3593 klass_inst = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3599 args [1] = klass_inst;
3602 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3604 NEW_BBLOCK (cfg, is_ref_bb);
3605 NEW_BBLOCK (cfg, is_nullable_bb);
3606 NEW_BBLOCK (cfg, end_bb);
3607 is_ref = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3608 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3609 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3611 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3612 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3614 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3615 addr_reg = alloc_dreg (cfg, STACK_MP);
3619 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3620 MONO_ADD_INS (cfg->cbb, addr);
3622 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3625 MONO_START_BB (cfg, is_ref_bb);
3627 /* Save the ref to a temporary */
3628 dreg = alloc_ireg (cfg);
3629 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3630 addr->dreg = addr_reg;
3631 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3632 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3635 MONO_START_BB (cfg, is_nullable_bb);
3638 MonoInst *addr = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3639 MonoInst *unbox_call;
3640 MonoMethodSignature *unbox_sig;
3642 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3643 unbox_sig->ret = &klass->byval_arg;
3644 unbox_sig->param_count = 1;
3645 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3648 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
3650 unbox_call = mini_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3652 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3653 addr->dreg = addr_reg;
3656 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3659 MONO_START_BB (cfg, end_bb);
3662 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3668 * Returns NULL and set the cfg exception on error.
3671 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3673 MonoInst *iargs [2];
3678 MonoRgctxInfoType rgctx_info;
3679 MonoInst *iargs [2];
3680 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
3682 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
3684 if (cfg->opt & MONO_OPT_SHARED)
3685 rgctx_info = MONO_RGCTX_INFO_KLASS;
3687 rgctx_info = MONO_RGCTX_INFO_VTABLE;
3688 data = mini_emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
3690 if (cfg->opt & MONO_OPT_SHARED) {
3691 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3693 alloc_ftn = ves_icall_object_new;
3696 alloc_ftn = ves_icall_object_new_specific;
3699 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
3700 if (known_instance_size) {
3701 int size = mono_class_instance_size (klass);
3702 if (size < sizeof (MonoObject))
3703 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
3705 EMIT_NEW_ICONST (cfg, iargs [1], size);
3707 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
3710 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
3713 if (cfg->opt & MONO_OPT_SHARED) {
3714 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3715 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
3717 alloc_ftn = ves_icall_object_new;
3718 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
3719 /* This happens often in argument checking code, eg. throw new FooException... */
3720 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
3721 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3722 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
3724 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3725 MonoMethod *managed_alloc = NULL;
3729 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
3730 cfg->exception_ptr = klass;
3734 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
3736 if (managed_alloc) {
3737 int size = mono_class_instance_size (klass);
3738 if (size < sizeof (MonoObject))
3739 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
3741 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
3742 EMIT_NEW_ICONST (cfg, iargs [1], size);
3743 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
3745 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
3747 guint32 lw = vtable->klass->instance_size;
3748 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
3749 EMIT_NEW_ICONST (cfg, iargs [0], lw);
3750 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
3753 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
3757 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
3761 * Returns NULL and set the cfg exception on error.
3764 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
3766 MonoInst *alloc, *ins;
3768 if (mono_class_is_nullable (klass)) {
3769 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
3772 if (cfg->llvm_only && cfg->gsharedvt) {
3773 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
3774 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3775 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3777 /* FIXME: What if the class is shared? We might not
3778 have to get the method address from the RGCTX. */
3779 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
3780 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3781 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3783 return mini_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3786 gboolean pass_vtable, pass_mrgctx;
3787 MonoInst *rgctx_arg = NULL;
3789 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3790 g_assert (!pass_mrgctx);
3793 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3796 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3799 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3803 if (mini_is_gsharedvt_klass (klass)) {
3804 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3805 MonoInst *res, *is_ref, *src_var, *addr;
3808 dreg = alloc_ireg (cfg);
3810 NEW_BBLOCK (cfg, is_ref_bb);
3811 NEW_BBLOCK (cfg, is_nullable_bb);
3812 NEW_BBLOCK (cfg, end_bb);
3813 is_ref = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3814 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3815 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3817 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3818 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3821 alloc = handle_alloc (cfg, klass, TRUE, context_used);
3824 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
3825 ins->opcode = OP_STOREV_MEMBASE;
3827 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
3828 res->type = STACK_OBJ;
3830 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3833 MONO_START_BB (cfg, is_ref_bb);
3835 /* val is a vtype, so has to load the value manually */
3836 src_var = get_vreg_to_inst (cfg, val->dreg);
3838 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
3839 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
3840 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
3841 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3844 MONO_START_BB (cfg, is_nullable_bb);
3847 MonoInst *addr = mini_emit_get_gsharedvt_info_klass (cfg, klass,
3848 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
3850 MonoMethodSignature *box_sig;
3853 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
3854 * construct that method at JIT time, so have to do things by hand.
3856 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3857 box_sig->ret = &mono_defaults.object_class->byval_arg;
3858 box_sig->param_count = 1;
3859 box_sig->params [0] = &klass->byval_arg;
3862 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
3864 box_call = mini_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
3865 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
3866 res->type = STACK_OBJ;
3870 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3872 MONO_START_BB (cfg, end_bb);
3876 alloc = handle_alloc (cfg, klass, TRUE, context_used);
3880 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
3885 static GHashTable* direct_icall_type_hash;
3888 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
3890 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3891 if (!direct_icalls_enabled (cfg))
3895 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
3896 * Whitelist a few icalls for now.
3898 if (!direct_icall_type_hash) {
3899 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
3901 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
3902 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
3903 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
3904 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
3905 mono_memory_barrier ();
3906 direct_icall_type_hash = h;
3909 if (cmethod->klass == mono_defaults.math_class)
3911 /* No locking needed */
3912 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
3918 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
3920 if (cmethod->klass == mono_defaults.systemtype_class) {
3921 if (!strcmp (cmethod->name, "GetType"))
3927 static G_GNUC_UNUSED MonoInst*
3928 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
3930 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
3931 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
3934 switch (enum_type->type) {
3937 #if SIZEOF_REGISTER == 8
3949 MonoInst *load, *and_, *cmp, *ceq;
3950 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
3951 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
3952 int dest_reg = alloc_ireg (cfg);
3954 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
3955 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
3956 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
3957 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
3959 ceq->type = STACK_I4;
3962 load = mono_decompose_opcode (cfg, load);
3963 and_ = mono_decompose_opcode (cfg, and_);
3964 cmp = mono_decompose_opcode (cfg, cmp);
3965 ceq = mono_decompose_opcode (cfg, ceq);
3973 * Returns NULL and set the cfg exception on error.
3975 static G_GNUC_UNUSED MonoInst*
3976 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
3980 gpointer trampoline;
3981 MonoInst *obj, *method_ins, *tramp_ins;
3985 if (virtual_ && !cfg->llvm_only) {
3986 MonoMethod *invoke = mono_get_delegate_invoke (klass);
3989 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
3993 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
3997 /* Inline the contents of mono_delegate_ctor */
3999 /* Set target field */
4000 /* Optimize away setting of NULL target */
4001 if (!MONO_INS_IS_PCONST_NULL (target)) {
4002 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4003 if (cfg->gen_write_barriers) {
4004 dreg = alloc_preg (cfg);
4005 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4006 mini_emit_write_barrier (cfg, ptr, target);
4010 /* Set method field */
4011 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4012 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4015 * To avoid looking up the compiled code belonging to the target method
4016 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4017 * store it, and we fill it after the method has been compiled.
4019 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4020 MonoInst *code_slot_ins;
4023 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4025 domain = mono_domain_get ();
4026 mono_domain_lock (domain);
4027 if (!domain_jit_info (domain)->method_code_hash)
4028 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4029 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4031 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
4032 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4034 mono_domain_unlock (domain);
4036 code_slot_ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4038 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
4041 if (cfg->llvm_only) {
4042 MonoInst *args [16];
4047 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4048 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
4051 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
4057 if (cfg->compile_aot) {
4058 MonoDelegateClassMethodPair *del_tramp;
4060 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4061 del_tramp->klass = klass;
4062 del_tramp->method = context_used ? NULL : method;
4063 del_tramp->is_virtual = virtual_;
4064 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4067 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4069 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4070 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4073 /* Set invoke_impl field */
4075 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4077 dreg = alloc_preg (cfg);
4078 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4079 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4081 dreg = alloc_preg (cfg);
4082 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4083 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4086 dreg = alloc_preg (cfg);
4087 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
4088 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4090 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4096 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4098 MonoJitICallInfo *info;
4100 /* Need to register the icall so it gets an icall wrapper */
4101 info = mono_get_array_new_va_icall (rank);
4103 cfg->flags |= MONO_CFG_HAS_VARARGS;
4105 /* mono_array_new_va () needs a vararg calling convention */
4106 cfg->exception_message = g_strdup ("array-new");
4107 cfg->disable_llvm = TRUE;
4109 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4110 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4114 * handle_constrained_gsharedvt_call:
4116 * Handle constrained calls where the receiver is a gsharedvt type.
4117 * Return the instruction representing the call. Set the cfg exception on failure.
4120 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4121 gboolean *ref_emit_widen)
4123 MonoInst *ins = NULL;
4124 gboolean emit_widen = *ref_emit_widen;
4127 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4128 * 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
4129 * pack the arguments into an array, and do the rest of the work in in an icall.
4131 if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4132 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret)) || mini_is_gsharedvt_type (fsig->ret)) &&
4133 (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]))))) {
4134 MonoInst *args [16];
4137 * This case handles calls to
4138 * - object:ToString()/Equals()/GetHashCode(),
4139 * - System.IComparable<T>:CompareTo()
4140 * - System.IEquatable<T>:Equals ()
4141 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4145 if (mono_method_check_context_used (cmethod))
4146 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4148 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4149 args [2] = mini_emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4151 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4152 if (fsig->hasthis && fsig->param_count) {
4153 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4154 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4155 ins->dreg = alloc_preg (cfg);
4156 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4157 MONO_ADD_INS (cfg->cbb, ins);
4160 if (mini_is_gsharedvt_type (fsig->params [0])) {
4161 int addr_reg, deref_arg_reg;
4163 ins = mini_emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4164 deref_arg_reg = alloc_preg (cfg);
4165 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
4166 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
4168 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4169 addr_reg = ins->dreg;
4170 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4172 EMIT_NEW_ICONST (cfg, args [3], 0);
4173 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4176 EMIT_NEW_ICONST (cfg, args [3], 0);
4177 EMIT_NEW_ICONST (cfg, args [4], 0);
4179 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4182 if (mini_is_gsharedvt_type (fsig->ret)) {
4183 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
4184 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret))) {
4188 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4189 MONO_ADD_INS (cfg->cbb, add);
4191 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4192 MONO_ADD_INS (cfg->cbb, ins);
4193 /* ins represents the call result */
4196 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4199 *ref_emit_widen = emit_widen;
4208 mono_emit_load_got_addr (MonoCompile *cfg)
4210 MonoInst *getaddr, *dummy_use;
4212 if (!cfg->got_var || cfg->got_var_allocated)
4215 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4216 getaddr->cil_code = cfg->header->code;
4217 getaddr->dreg = cfg->got_var->dreg;
4219 /* Add it to the start of the first bblock */
4220 if (cfg->bb_entry->code) {
4221 getaddr->next = cfg->bb_entry->code;
4222 cfg->bb_entry->code = getaddr;
4225 MONO_ADD_INS (cfg->bb_entry, getaddr);
4227 cfg->got_var_allocated = TRUE;
4230 * Add a dummy use to keep the got_var alive, since real uses might
4231 * only be generated by the back ends.
4232 * Add it to end_bblock, so the variable's lifetime covers the whole
4234 * It would be better to make the usage of the got var explicit in all
4235 * cases when the backend needs it (i.e. calls, throw etc.), so this
4236 * wouldn't be needed.
4238 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
4239 MONO_ADD_INS (cfg->bb_exit, dummy_use);
4242 static int inline_limit;
4243 static gboolean inline_limit_inited;
4246 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
4248 MonoMethodHeaderSummary header;
4250 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4251 MonoMethodSignature *sig = mono_method_signature (method);
4255 if (cfg->disable_inline)
4260 if (cfg->inline_depth > 10)
4263 if (!mono_method_get_header_summary (method, &header))
4266 /*runtime, icall and pinvoke are checked by summary call*/
4267 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
4268 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
4269 (mono_class_is_marshalbyref (method->klass)) ||
4273 /* also consider num_locals? */
4274 /* Do the size check early to avoid creating vtables */
4275 if (!inline_limit_inited) {
4277 if ((inlinelimit = g_getenv ("MONO_INLINELIMIT"))) {
4278 inline_limit = atoi (inlinelimit);
4279 g_free (inlinelimit);
4281 inline_limit = INLINE_LENGTH_LIMIT;
4282 inline_limit_inited = TRUE;
4284 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
4288 * if we can initialize the class of the method right away, we do,
4289 * otherwise we don't allow inlining if the class needs initialization,
4290 * since it would mean inserting a call to mono_runtime_class_init()
4291 * inside the inlined code
4293 if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
4296 if (!(cfg->opt & MONO_OPT_SHARED)) {
4297 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
4298 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
4299 if (method->klass->has_cctor) {
4300 vtable = mono_class_vtable (cfg->domain, method->klass);
4303 if (!cfg->compile_aot) {
4305 if (!mono_runtime_class_init_full (vtable, &error)) {
4306 mono_error_cleanup (&error);
4311 } else if (mono_class_is_before_field_init (method->klass)) {
4312 if (cfg->run_cctors && method->klass->has_cctor) {
4313 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
4314 if (!method->klass->runtime_info)
4315 /* No vtable created yet */
4317 vtable = mono_class_vtable (cfg->domain, method->klass);
4320 /* This makes so that inline cannot trigger */
4321 /* .cctors: too many apps depend on them */
4322 /* running with a specific order... */
4323 if (! vtable->initialized)
4326 if (!mono_runtime_class_init_full (vtable, &error)) {
4327 mono_error_cleanup (&error);
4331 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
4332 if (!method->klass->runtime_info)
4333 /* No vtable created yet */
4335 vtable = mono_class_vtable (cfg->domain, method->klass);
4338 if (!vtable->initialized)
4343 * If we're compiling for shared code
4344 * the cctor will need to be run at aot method load time, for example,
4345 * or at the end of the compilation of the inlining method.
4347 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
4351 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4352 if (mono_arch_is_soft_float ()) {
4354 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
4356 for (i = 0; i < sig->param_count; ++i)
4357 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
4362 if (g_list_find (cfg->dont_inline, method))
4369 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
4371 if (!cfg->compile_aot) {
4373 if (vtable->initialized)
4377 if (mono_class_is_before_field_init (klass)) {
4378 if (cfg->method == method)
4382 if (!mono_class_needs_cctor_run (klass, method))
4385 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
4386 /* The initialization is already done before the method is called */
4393 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
4397 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
4400 if (mini_is_gsharedvt_variable_klass (klass)) {
4403 mono_class_init (klass);
4404 size = mono_class_array_element_size (klass);
4407 mult_reg = alloc_preg (cfg);
4408 array_reg = arr->dreg;
4409 index_reg = index->dreg;
4411 #if SIZEOF_REGISTER == 8
4412 /* The array reg is 64 bits but the index reg is only 32 */
4413 if (COMPILE_LLVM (cfg)) {
4415 * abcrem can't handle the OP_SEXT_I4, so add this after abcrem,
4416 * during OP_BOUNDS_CHECK decomposition, and in the implementation
4417 * of OP_X86_LEA for llvm.
4419 index2_reg = index_reg;
4421 index2_reg = alloc_preg (cfg);
4422 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
4425 if (index->type == STACK_I8) {
4426 index2_reg = alloc_preg (cfg);
4427 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
4429 index2_reg = index_reg;
4434 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
4436 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4437 if (size == 1 || size == 2 || size == 4 || size == 8) {
4438 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
4440 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
4441 ins->klass = mono_class_get_element_class (klass);
4442 ins->type = STACK_MP;
4448 add_reg = alloc_ireg_mp (cfg);
4451 MonoInst *rgctx_ins;
4454 g_assert (cfg->gshared);
4455 context_used = mini_class_check_context_used (cfg, klass);
4456 g_assert (context_used);
4457 rgctx_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
4458 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
4460 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
4462 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
4463 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4464 ins->klass = mono_class_get_element_class (klass);
4465 ins->type = STACK_MP;
4466 MONO_ADD_INS (cfg->cbb, ins);
4472 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
4474 int bounds_reg = alloc_preg (cfg);
4475 int add_reg = alloc_ireg_mp (cfg);
4476 int mult_reg = alloc_preg (cfg);
4477 int mult2_reg = alloc_preg (cfg);
4478 int low1_reg = alloc_preg (cfg);
4479 int low2_reg = alloc_preg (cfg);
4480 int high1_reg = alloc_preg (cfg);
4481 int high2_reg = alloc_preg (cfg);
4482 int realidx1_reg = alloc_preg (cfg);
4483 int realidx2_reg = alloc_preg (cfg);
4484 int sum_reg = alloc_preg (cfg);
4485 int index1, index2, tmpreg;
4489 mono_class_init (klass);
4490 size = mono_class_array_element_size (klass);
4492 index1 = index_ins1->dreg;
4493 index2 = index_ins2->dreg;
4495 #if SIZEOF_REGISTER == 8
4496 /* The array reg is 64 bits but the index reg is only 32 */
4497 if (COMPILE_LLVM (cfg)) {
4500 tmpreg = alloc_preg (cfg);
4501 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
4503 tmpreg = alloc_preg (cfg);
4504 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
4508 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
4512 /* range checking */
4513 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
4514 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4516 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
4517 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4518 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
4519 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
4520 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4521 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
4522 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4524 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
4525 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4526 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
4527 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
4528 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4529 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
4530 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4532 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
4533 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
4534 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
4535 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
4536 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4538 ins->type = STACK_MP;
4540 MONO_ADD_INS (cfg->cbb, ins);
4546 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
4550 MonoMethod *addr_method;
4552 MonoClass *eclass = cmethod->klass->element_class;
4554 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
4557 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
4559 /* emit_ldelema_2 depends on OP_LMUL */
4560 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
4561 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
4564 if (mini_is_gsharedvt_variable_klass (eclass))
4567 element_size = mono_class_array_element_size (eclass);
4568 addr_method = mono_marshal_get_array_address (rank, element_size);
4569 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
4574 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
4576 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4578 MonoInst *addr, *store, *load;
4579 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
4581 /* the bounds check is already done by the callers */
4582 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4584 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
4585 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
4586 if (mini_type_is_reference (&eklass->byval_arg))
4587 mini_emit_write_barrier (cfg, addr, load);
4589 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
4590 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
4597 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4599 return mini_type_is_reference (&klass->byval_arg);
4603 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
4605 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
4606 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
4607 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
4608 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
4609 MonoInst *iargs [3];
4612 mono_class_setup_vtable (obj_array);
4613 g_assert (helper->slot);
4615 if (sp [0]->type != STACK_OBJ)
4617 if (sp [2]->type != STACK_OBJ)
4624 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
4628 if (mini_is_gsharedvt_variable_klass (klass)) {
4631 // FIXME-VT: OP_ICONST optimization
4632 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
4633 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4634 ins->opcode = OP_STOREV_MEMBASE;
4635 } else if (sp [1]->opcode == OP_ICONST) {
4636 int array_reg = sp [0]->dreg;
4637 int index_reg = sp [1]->dreg;
4638 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
4640 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
4641 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
4644 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
4645 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
4647 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
4648 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4649 if (generic_class_is_reference_type (cfg, klass))
4650 mini_emit_write_barrier (cfg, addr, sp [2]);
4657 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4662 eklass = mono_class_from_mono_type (fsig->params [2]);
4664 eklass = mono_class_from_mono_type (fsig->ret);
4667 return emit_array_store (cfg, eklass, args, FALSE);
4669 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4670 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
4676 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
4679 int param_size, return_size;
4681 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
4682 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
4684 if (cfg->verbose_level > 3)
4685 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
4687 //Don't allow mixing reference types with value types
4688 if (param_klass->valuetype != return_klass->valuetype) {
4689 if (cfg->verbose_level > 3)
4690 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
4694 if (!param_klass->valuetype) {
4695 if (cfg->verbose_level > 3)
4696 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
4701 if (param_klass->has_references || return_klass->has_references)
4704 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
4705 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
4706 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
4707 if (cfg->verbose_level > 3)
4708 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
4712 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
4713 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
4714 if (cfg->verbose_level > 3)
4715 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
4719 param_size = mono_class_value_size (param_klass, &align);
4720 return_size = mono_class_value_size (return_klass, &align);
4722 //We can do it if sizes match
4723 if (param_size == return_size) {
4724 if (cfg->verbose_level > 3)
4725 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
4729 //No simple way to handle struct if sizes don't match
4730 if (MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg)) {
4731 if (cfg->verbose_level > 3)
4732 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
4737 * Same reg size category.
4738 * A quick note on why we don't require widening here.
4739 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
4741 * Since the source value comes from a function argument, the JIT will already have
4742 * the value in a VREG and performed any widening needed before (say, when loading from a field).
4744 if (param_size <= 4 && return_size <= 4) {
4745 if (cfg->verbose_level > 3)
4746 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
4754 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
4756 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
4757 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
4759 if (mini_is_gsharedvt_variable_type (fsig->ret))
4762 //Valuetypes that are semantically equivalent or numbers than can be widened to
4763 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
4766 //Arrays of valuetypes that are semantically equivalent
4767 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
4774 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4776 #ifdef MONO_ARCH_SIMD_INTRINSICS
4777 MonoInst *ins = NULL;
4779 if (cfg->opt & MONO_OPT_SIMD) {
4780 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
4786 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
4790 mini_emit_memory_barrier (MonoCompile *cfg, int kind)
4792 MonoInst *ins = NULL;
4793 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4794 MONO_ADD_INS (cfg->cbb, ins);
4795 ins->backend.memory_barrier_kind = kind;
4801 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4803 MonoInst *ins = NULL;
4806 /* The LLVM backend supports these intrinsics */
4807 if (cmethod->klass == mono_defaults.math_class) {
4808 if (strcmp (cmethod->name, "Sin") == 0) {
4810 } else if (strcmp (cmethod->name, "Cos") == 0) {
4812 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
4814 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
4818 if (opcode && fsig->param_count == 1) {
4819 MONO_INST_NEW (cfg, ins, opcode);
4820 ins->type = STACK_R8;
4821 ins->dreg = mono_alloc_dreg (cfg, ins->type);
4822 ins->sreg1 = args [0]->dreg;
4823 MONO_ADD_INS (cfg->cbb, ins);
4827 if (cfg->opt & MONO_OPT_CMOV) {
4828 if (strcmp (cmethod->name, "Min") == 0) {
4829 if (fsig->params [0]->type == MONO_TYPE_I4)
4831 if (fsig->params [0]->type == MONO_TYPE_U4)
4832 opcode = OP_IMIN_UN;
4833 else if (fsig->params [0]->type == MONO_TYPE_I8)
4835 else if (fsig->params [0]->type == MONO_TYPE_U8)
4836 opcode = OP_LMIN_UN;
4837 } else if (strcmp (cmethod->name, "Max") == 0) {
4838 if (fsig->params [0]->type == MONO_TYPE_I4)
4840 if (fsig->params [0]->type == MONO_TYPE_U4)
4841 opcode = OP_IMAX_UN;
4842 else if (fsig->params [0]->type == MONO_TYPE_I8)
4844 else if (fsig->params [0]->type == MONO_TYPE_U8)
4845 opcode = OP_LMAX_UN;
4849 if (opcode && fsig->param_count == 2) {
4850 MONO_INST_NEW (cfg, ins, opcode);
4851 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
4852 ins->dreg = mono_alloc_dreg (cfg, ins->type);
4853 ins->sreg1 = args [0]->dreg;
4854 ins->sreg2 = args [1]->dreg;
4855 MONO_ADD_INS (cfg->cbb, ins);
4863 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4865 if (cmethod->klass == mono_defaults.array_class) {
4866 if (strcmp (cmethod->name, "UnsafeStore") == 0)
4867 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
4868 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
4869 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
4870 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
4871 return emit_array_unsafe_mov (cfg, fsig, args);
4879 mono_type_is_native_blittable (MonoType *t)
4881 if (MONO_TYPE_IS_REFERENCE (t))
4884 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (t))
4887 MonoClass *klass = mono_class_from_mono_type (t);
4889 //MonoClass::blitable depends on mono_class_setup_fields being done.
4890 mono_class_setup_fields (klass);
4891 if (!klass->blittable)
4894 // If the native marshal size is different we can't convert PtrToStructure to a type load
4895 if (mono_class_native_size (klass, NULL) != mono_class_value_size (klass, NULL))
4903 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4905 MonoInst *ins = NULL;
4906 MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
4908 if (cmethod->klass == mono_defaults.string_class) {
4909 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
4910 int dreg = alloc_ireg (cfg);
4911 int index_reg = alloc_preg (cfg);
4912 int add_reg = alloc_preg (cfg);
4914 #if SIZEOF_REGISTER == 8
4915 if (COMPILE_LLVM (cfg)) {
4916 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
4918 /* The array reg is 64 bits but the index reg is only 32 */
4919 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
4922 index_reg = args [1]->dreg;
4924 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
4926 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4927 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
4928 add_reg = ins->dreg;
4929 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
4932 int mult_reg = alloc_preg (cfg);
4933 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
4934 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
4935 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
4936 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
4938 type_from_op (cfg, ins, NULL, NULL);
4940 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
4941 int dreg = alloc_ireg (cfg);
4942 /* Decompose later to allow more optimizations */
4943 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
4944 ins->type = STACK_I4;
4945 ins->flags |= MONO_INST_FAULT;
4946 cfg->cbb->has_array_access = TRUE;
4947 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
4952 } else if (cmethod->klass == mono_defaults.object_class) {
4953 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
4954 int dreg = alloc_ireg_ref (cfg);
4955 int vt_reg = alloc_preg (cfg);
4956 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4957 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
4958 type_from_op (cfg, ins, NULL, NULL);
4961 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
4962 int dreg = alloc_ireg (cfg);
4963 int t1 = alloc_ireg (cfg);
4965 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
4966 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
4967 ins->type = STACK_I4;
4970 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
4971 MONO_INST_NEW (cfg, ins, OP_NOP);
4972 MONO_ADD_INS (cfg->cbb, ins);
4976 } else if (cmethod->klass == mono_defaults.array_class) {
4977 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
4978 return emit_array_generic_access (cfg, fsig, args, FALSE);
4979 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
4980 return emit_array_generic_access (cfg, fsig, args, TRUE);
4982 #ifndef MONO_BIG_ARRAYS
4984 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
4987 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
4988 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
4989 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
4990 int dreg = alloc_ireg (cfg);
4991 int bounds_reg = alloc_ireg_mp (cfg);
4992 MonoBasicBlock *end_bb, *szarray_bb;
4993 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
4995 NEW_BBLOCK (cfg, end_bb);
4996 NEW_BBLOCK (cfg, szarray_bb);
4998 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
4999 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5000 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
5001 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
5002 /* Non-szarray case */
5004 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5005 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5007 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5008 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5009 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
5010 MONO_START_BB (cfg, szarray_bb);
5013 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5014 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5016 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5017 MONO_START_BB (cfg, end_bb);
5019 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
5020 ins->type = STACK_I4;
5026 if (cmethod->name [0] != 'g')
5029 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
5030 int dreg = alloc_ireg (cfg);
5031 int vtable_reg = alloc_preg (cfg);
5032 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
5033 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5034 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5035 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5036 type_from_op (cfg, ins, NULL, NULL);
5039 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5040 int dreg = alloc_ireg (cfg);
5042 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5043 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5044 type_from_op (cfg, ins, NULL, NULL);
5049 } else if (cmethod->klass == runtime_helpers_class) {
5050 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5051 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5053 } else if (strcmp (cmethod->name, "IsReferenceOrContainsReferences") == 0 && fsig->param_count == 0) {
5054 MonoGenericContext *ctx = mono_method_get_context (cmethod);
5056 g_assert (ctx->method_inst);
5057 g_assert (ctx->method_inst->type_argc == 1);
5058 MonoType *t = mini_get_underlying_type (ctx->method_inst->type_argv [0]);
5059 MonoClass *klass = mono_class_from_mono_type (t);
5063 mono_class_init (klass);
5064 if (MONO_TYPE_IS_REFERENCE (t))
5065 EMIT_NEW_ICONST (cfg, ins, 1);
5066 else if (MONO_TYPE_IS_PRIMITIVE (t))
5067 EMIT_NEW_ICONST (cfg, ins, 0);
5068 else if (cfg->gshared && (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR) && !mini_type_var_is_vt (t))
5069 EMIT_NEW_ICONST (cfg, ins, 1);
5070 else if (!cfg->gshared || !mini_class_check_context_used (cfg, klass))
5071 EMIT_NEW_ICONST (cfg, ins, klass->has_references ? 1 : 0);
5073 g_assert (cfg->gshared);
5075 int context_used = mini_class_check_context_used (cfg, klass);
5077 /* This returns 1 or 2 */
5078 MonoInst *info = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CLASS_IS_REF_OR_CONTAINS_REFS);
5079 int dreg = alloc_ireg (cfg);
5080 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ISUB_IMM, dreg, info->dreg, 1);
5086 } else if (cmethod->klass == mono_defaults.monitor_class) {
5087 gboolean is_enter = FALSE;
5088 gboolean is_v4 = FALSE;
5090 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
5094 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
5099 * To make async stack traces work, icalls which can block should have a wrapper.
5100 * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
5102 MonoBasicBlock *end_bb;
5104 NEW_BBLOCK (cfg, end_bb);
5106 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
5107 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
5108 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
5109 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_internal : (gpointer)mono_monitor_enter_internal, args);
5110 MONO_START_BB (cfg, end_bb);
5113 } else if (cmethod->klass == mono_defaults.thread_class) {
5114 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5115 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5116 MONO_ADD_INS (cfg->cbb, ins);
5118 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5119 return mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5120 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5122 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5124 if (fsig->params [0]->type == MONO_TYPE_I1)
5125 opcode = OP_LOADI1_MEMBASE;
5126 else if (fsig->params [0]->type == MONO_TYPE_U1)
5127 opcode = OP_LOADU1_MEMBASE;
5128 else if (fsig->params [0]->type == MONO_TYPE_I2)
5129 opcode = OP_LOADI2_MEMBASE;
5130 else if (fsig->params [0]->type == MONO_TYPE_U2)
5131 opcode = OP_LOADU2_MEMBASE;
5132 else if (fsig->params [0]->type == MONO_TYPE_I4)
5133 opcode = OP_LOADI4_MEMBASE;
5134 else if (fsig->params [0]->type == MONO_TYPE_U4)
5135 opcode = OP_LOADU4_MEMBASE;
5136 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5137 opcode = OP_LOADI8_MEMBASE;
5138 else if (fsig->params [0]->type == MONO_TYPE_R4)
5139 opcode = OP_LOADR4_MEMBASE;
5140 else if (fsig->params [0]->type == MONO_TYPE_R8)
5141 opcode = OP_LOADR8_MEMBASE;
5142 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5143 opcode = OP_LOAD_MEMBASE;
5146 MONO_INST_NEW (cfg, ins, opcode);
5147 ins->inst_basereg = args [0]->dreg;
5148 ins->inst_offset = 0;
5149 MONO_ADD_INS (cfg->cbb, ins);
5151 switch (fsig->params [0]->type) {
5158 ins->dreg = mono_alloc_ireg (cfg);
5159 ins->type = STACK_I4;
5163 ins->dreg = mono_alloc_lreg (cfg);
5164 ins->type = STACK_I8;
5168 ins->dreg = mono_alloc_ireg (cfg);
5169 #if SIZEOF_REGISTER == 8
5170 ins->type = STACK_I8;
5172 ins->type = STACK_I4;
5177 ins->dreg = mono_alloc_freg (cfg);
5178 ins->type = STACK_R8;
5181 g_assert (mini_type_is_reference (fsig->params [0]));
5182 ins->dreg = mono_alloc_ireg_ref (cfg);
5183 ins->type = STACK_OBJ;
5187 if (opcode == OP_LOADI8_MEMBASE)
5188 ins = mono_decompose_opcode (cfg, ins);
5190 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5194 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5196 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5198 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5199 opcode = OP_STOREI1_MEMBASE_REG;
5200 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5201 opcode = OP_STOREI2_MEMBASE_REG;
5202 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5203 opcode = OP_STOREI4_MEMBASE_REG;
5204 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5205 opcode = OP_STOREI8_MEMBASE_REG;
5206 else if (fsig->params [0]->type == MONO_TYPE_R4)
5207 opcode = OP_STORER4_MEMBASE_REG;
5208 else if (fsig->params [0]->type == MONO_TYPE_R8)
5209 opcode = OP_STORER8_MEMBASE_REG;
5210 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5211 opcode = OP_STORE_MEMBASE_REG;
5214 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5216 MONO_INST_NEW (cfg, ins, opcode);
5217 ins->sreg1 = args [1]->dreg;
5218 ins->inst_destbasereg = args [0]->dreg;
5219 ins->inst_offset = 0;
5220 MONO_ADD_INS (cfg->cbb, ins);
5222 if (opcode == OP_STOREI8_MEMBASE_REG)
5223 ins = mono_decompose_opcode (cfg, ins);
5228 } else if (cmethod->klass->image == mono_defaults.corlib &&
5229 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5230 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5233 #if SIZEOF_REGISTER == 8
5234 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5235 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5236 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5237 ins->dreg = mono_alloc_preg (cfg);
5238 ins->sreg1 = args [0]->dreg;
5239 ins->type = STACK_I8;
5240 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5241 MONO_ADD_INS (cfg->cbb, ins);
5245 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5247 /* 64 bit reads are already atomic */
5248 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5249 load_ins->dreg = mono_alloc_preg (cfg);
5250 load_ins->inst_basereg = args [0]->dreg;
5251 load_ins->inst_offset = 0;
5252 load_ins->type = STACK_I8;
5253 MONO_ADD_INS (cfg->cbb, load_ins);
5255 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5262 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
5263 MonoInst *ins_iconst;
5266 if (fsig->params [0]->type == MONO_TYPE_I4) {
5267 opcode = OP_ATOMIC_ADD_I4;
5268 cfg->has_atomic_add_i4 = TRUE;
5270 #if SIZEOF_REGISTER == 8
5271 else if (fsig->params [0]->type == MONO_TYPE_I8)
5272 opcode = OP_ATOMIC_ADD_I8;
5275 if (!mono_arch_opcode_supported (opcode))
5277 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5278 ins_iconst->inst_c0 = 1;
5279 ins_iconst->dreg = mono_alloc_ireg (cfg);
5280 MONO_ADD_INS (cfg->cbb, ins_iconst);
5282 MONO_INST_NEW (cfg, ins, opcode);
5283 ins->dreg = mono_alloc_ireg (cfg);
5284 ins->inst_basereg = args [0]->dreg;
5285 ins->inst_offset = 0;
5286 ins->sreg2 = ins_iconst->dreg;
5287 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5288 MONO_ADD_INS (cfg->cbb, ins);
5290 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
5291 MonoInst *ins_iconst;
5294 if (fsig->params [0]->type == MONO_TYPE_I4) {
5295 opcode = OP_ATOMIC_ADD_I4;
5296 cfg->has_atomic_add_i4 = TRUE;
5298 #if SIZEOF_REGISTER == 8
5299 else if (fsig->params [0]->type == MONO_TYPE_I8)
5300 opcode = OP_ATOMIC_ADD_I8;
5303 if (!mono_arch_opcode_supported (opcode))
5305 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5306 ins_iconst->inst_c0 = -1;
5307 ins_iconst->dreg = mono_alloc_ireg (cfg);
5308 MONO_ADD_INS (cfg->cbb, ins_iconst);
5310 MONO_INST_NEW (cfg, ins, opcode);
5311 ins->dreg = mono_alloc_ireg (cfg);
5312 ins->inst_basereg = args [0]->dreg;
5313 ins->inst_offset = 0;
5314 ins->sreg2 = ins_iconst->dreg;
5315 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5316 MONO_ADD_INS (cfg->cbb, ins);
5318 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
5321 if (fsig->params [0]->type == MONO_TYPE_I4) {
5322 opcode = OP_ATOMIC_ADD_I4;
5323 cfg->has_atomic_add_i4 = TRUE;
5325 #if SIZEOF_REGISTER == 8
5326 else if (fsig->params [0]->type == MONO_TYPE_I8)
5327 opcode = OP_ATOMIC_ADD_I8;
5330 if (!mono_arch_opcode_supported (opcode))
5332 MONO_INST_NEW (cfg, ins, opcode);
5333 ins->dreg = mono_alloc_ireg (cfg);
5334 ins->inst_basereg = args [0]->dreg;
5335 ins->inst_offset = 0;
5336 ins->sreg2 = args [1]->dreg;
5337 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5338 MONO_ADD_INS (cfg->cbb, ins);
5341 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
5342 MonoInst *f2i = NULL, *i2f;
5343 guint32 opcode, f2i_opcode, i2f_opcode;
5344 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5345 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
5347 if (fsig->params [0]->type == MONO_TYPE_I4 ||
5348 fsig->params [0]->type == MONO_TYPE_R4) {
5349 opcode = OP_ATOMIC_EXCHANGE_I4;
5350 f2i_opcode = OP_MOVE_F_TO_I4;
5351 i2f_opcode = OP_MOVE_I4_TO_F;
5352 cfg->has_atomic_exchange_i4 = TRUE;
5354 #if SIZEOF_REGISTER == 8
5356 fsig->params [0]->type == MONO_TYPE_I8 ||
5357 fsig->params [0]->type == MONO_TYPE_R8 ||
5358 fsig->params [0]->type == MONO_TYPE_I) {
5359 opcode = OP_ATOMIC_EXCHANGE_I8;
5360 f2i_opcode = OP_MOVE_F_TO_I8;
5361 i2f_opcode = OP_MOVE_I8_TO_F;
5364 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
5365 opcode = OP_ATOMIC_EXCHANGE_I4;
5366 cfg->has_atomic_exchange_i4 = TRUE;
5372 if (!mono_arch_opcode_supported (opcode))
5376 /* TODO: Decompose these opcodes instead of bailing here. */
5377 if (COMPILE_SOFT_FLOAT (cfg))
5380 MONO_INST_NEW (cfg, f2i, f2i_opcode);
5381 f2i->dreg = mono_alloc_ireg (cfg);
5382 f2i->sreg1 = args [1]->dreg;
5383 if (f2i_opcode == OP_MOVE_F_TO_I4)
5384 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5385 MONO_ADD_INS (cfg->cbb, f2i);
5388 MONO_INST_NEW (cfg, ins, opcode);
5389 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
5390 ins->inst_basereg = args [0]->dreg;
5391 ins->inst_offset = 0;
5392 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
5393 MONO_ADD_INS (cfg->cbb, ins);
5395 switch (fsig->params [0]->type) {
5397 ins->type = STACK_I4;
5400 ins->type = STACK_I8;
5403 #if SIZEOF_REGISTER == 8
5404 ins->type = STACK_I8;
5406 ins->type = STACK_I4;
5411 ins->type = STACK_R8;
5414 g_assert (mini_type_is_reference (fsig->params [0]));
5415 ins->type = STACK_OBJ;
5420 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5421 i2f->dreg = mono_alloc_freg (cfg);
5422 i2f->sreg1 = ins->dreg;
5423 i2f->type = STACK_R8;
5424 if (i2f_opcode == OP_MOVE_I4_TO_F)
5425 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5426 MONO_ADD_INS (cfg->cbb, i2f);
5431 if (cfg->gen_write_barriers && is_ref)
5432 mini_emit_write_barrier (cfg, args [0], args [1]);
5434 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
5435 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
5436 guint32 opcode, f2i_opcode, i2f_opcode;
5437 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
5438 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
5440 if (fsig->params [1]->type == MONO_TYPE_I4 ||
5441 fsig->params [1]->type == MONO_TYPE_R4) {
5442 opcode = OP_ATOMIC_CAS_I4;
5443 f2i_opcode = OP_MOVE_F_TO_I4;
5444 i2f_opcode = OP_MOVE_I4_TO_F;
5445 cfg->has_atomic_cas_i4 = TRUE;
5447 #if SIZEOF_REGISTER == 8
5449 fsig->params [1]->type == MONO_TYPE_I8 ||
5450 fsig->params [1]->type == MONO_TYPE_R8 ||
5451 fsig->params [1]->type == MONO_TYPE_I) {
5452 opcode = OP_ATOMIC_CAS_I8;
5453 f2i_opcode = OP_MOVE_F_TO_I8;
5454 i2f_opcode = OP_MOVE_I8_TO_F;
5457 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
5458 opcode = OP_ATOMIC_CAS_I4;
5459 cfg->has_atomic_cas_i4 = TRUE;
5465 if (!mono_arch_opcode_supported (opcode))
5469 /* TODO: Decompose these opcodes instead of bailing here. */
5470 if (COMPILE_SOFT_FLOAT (cfg))
5473 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
5474 f2i_new->dreg = mono_alloc_ireg (cfg);
5475 f2i_new->sreg1 = args [1]->dreg;
5476 if (f2i_opcode == OP_MOVE_F_TO_I4)
5477 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5478 MONO_ADD_INS (cfg->cbb, f2i_new);
5480 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
5481 f2i_cmp->dreg = mono_alloc_ireg (cfg);
5482 f2i_cmp->sreg1 = args [2]->dreg;
5483 if (f2i_opcode == OP_MOVE_F_TO_I4)
5484 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5485 MONO_ADD_INS (cfg->cbb, f2i_cmp);
5488 MONO_INST_NEW (cfg, ins, opcode);
5489 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
5490 ins->sreg1 = args [0]->dreg;
5491 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
5492 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
5493 MONO_ADD_INS (cfg->cbb, ins);
5495 switch (fsig->params [1]->type) {
5497 ins->type = STACK_I4;
5500 ins->type = STACK_I8;
5503 #if SIZEOF_REGISTER == 8
5504 ins->type = STACK_I8;
5506 ins->type = STACK_I4;
5510 ins->type = cfg->r4_stack_type;
5513 ins->type = STACK_R8;
5516 g_assert (mini_type_is_reference (fsig->params [1]));
5517 ins->type = STACK_OBJ;
5522 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5523 i2f->dreg = mono_alloc_freg (cfg);
5524 i2f->sreg1 = ins->dreg;
5525 i2f->type = STACK_R8;
5526 if (i2f_opcode == OP_MOVE_I4_TO_F)
5527 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5528 MONO_ADD_INS (cfg->cbb, i2f);
5533 if (cfg->gen_write_barriers && is_ref)
5534 mini_emit_write_barrier (cfg, args [0], args [1]);
5536 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
5537 fsig->params [1]->type == MONO_TYPE_I4) {
5538 MonoInst *cmp, *ceq;
5540 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
5543 /* int32 r = CAS (location, value, comparand); */
5544 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
5545 ins->dreg = alloc_ireg (cfg);
5546 ins->sreg1 = args [0]->dreg;
5547 ins->sreg2 = args [1]->dreg;
5548 ins->sreg3 = args [2]->dreg;
5549 ins->type = STACK_I4;
5550 MONO_ADD_INS (cfg->cbb, ins);
5552 /* bool result = r == comparand; */
5553 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
5554 cmp->sreg1 = ins->dreg;
5555 cmp->sreg2 = args [2]->dreg;
5556 cmp->type = STACK_I4;
5557 MONO_ADD_INS (cfg->cbb, cmp);
5559 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
5560 ceq->dreg = alloc_ireg (cfg);
5561 ceq->type = STACK_I4;
5562 MONO_ADD_INS (cfg->cbb, ceq);
5564 /* *success = result; */
5565 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
5567 cfg->has_atomic_cas_i4 = TRUE;
5569 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
5570 ins = mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5574 } else if (cmethod->klass->image == mono_defaults.corlib &&
5575 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5576 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
5579 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
5581 MonoType *t = fsig->params [0];
5583 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
5585 g_assert (t->byref);
5586 /* t is a byref type, so the reference check is more complicated */
5587 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5588 if (t->type == MONO_TYPE_I1)
5589 opcode = OP_ATOMIC_LOAD_I1;
5590 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5591 opcode = OP_ATOMIC_LOAD_U1;
5592 else if (t->type == MONO_TYPE_I2)
5593 opcode = OP_ATOMIC_LOAD_I2;
5594 else if (t->type == MONO_TYPE_U2)
5595 opcode = OP_ATOMIC_LOAD_U2;
5596 else if (t->type == MONO_TYPE_I4)
5597 opcode = OP_ATOMIC_LOAD_I4;
5598 else if (t->type == MONO_TYPE_U4)
5599 opcode = OP_ATOMIC_LOAD_U4;
5600 else if (t->type == MONO_TYPE_R4)
5601 opcode = OP_ATOMIC_LOAD_R4;
5602 else if (t->type == MONO_TYPE_R8)
5603 opcode = OP_ATOMIC_LOAD_R8;
5604 #if SIZEOF_REGISTER == 8
5605 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5606 opcode = OP_ATOMIC_LOAD_I8;
5607 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5608 opcode = OP_ATOMIC_LOAD_U8;
5610 else if (t->type == MONO_TYPE_I)
5611 opcode = OP_ATOMIC_LOAD_I4;
5612 else if (is_ref || t->type == MONO_TYPE_U)
5613 opcode = OP_ATOMIC_LOAD_U4;
5617 if (!mono_arch_opcode_supported (opcode))
5620 MONO_INST_NEW (cfg, ins, opcode);
5621 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
5622 ins->sreg1 = args [0]->dreg;
5623 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
5624 MONO_ADD_INS (cfg->cbb, ins);
5627 case MONO_TYPE_BOOLEAN:
5634 ins->type = STACK_I4;
5638 ins->type = STACK_I8;
5642 #if SIZEOF_REGISTER == 8
5643 ins->type = STACK_I8;
5645 ins->type = STACK_I4;
5649 ins->type = cfg->r4_stack_type;
5652 ins->type = STACK_R8;
5656 ins->type = STACK_OBJ;
5662 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
5664 MonoType *t = fsig->params [0];
5667 g_assert (t->byref);
5668 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5669 if (t->type == MONO_TYPE_I1)
5670 opcode = OP_ATOMIC_STORE_I1;
5671 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5672 opcode = OP_ATOMIC_STORE_U1;
5673 else if (t->type == MONO_TYPE_I2)
5674 opcode = OP_ATOMIC_STORE_I2;
5675 else if (t->type == MONO_TYPE_U2)
5676 opcode = OP_ATOMIC_STORE_U2;
5677 else if (t->type == MONO_TYPE_I4)
5678 opcode = OP_ATOMIC_STORE_I4;
5679 else if (t->type == MONO_TYPE_U4)
5680 opcode = OP_ATOMIC_STORE_U4;
5681 else if (t->type == MONO_TYPE_R4)
5682 opcode = OP_ATOMIC_STORE_R4;
5683 else if (t->type == MONO_TYPE_R8)
5684 opcode = OP_ATOMIC_STORE_R8;
5685 #if SIZEOF_REGISTER == 8
5686 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5687 opcode = OP_ATOMIC_STORE_I8;
5688 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5689 opcode = OP_ATOMIC_STORE_U8;
5691 else if (t->type == MONO_TYPE_I)
5692 opcode = OP_ATOMIC_STORE_I4;
5693 else if (is_ref || t->type == MONO_TYPE_U)
5694 opcode = OP_ATOMIC_STORE_U4;
5698 if (!mono_arch_opcode_supported (opcode))
5701 MONO_INST_NEW (cfg, ins, opcode);
5702 ins->dreg = args [0]->dreg;
5703 ins->sreg1 = args [1]->dreg;
5704 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
5705 MONO_ADD_INS (cfg->cbb, ins);
5707 if (cfg->gen_write_barriers && is_ref)
5708 mini_emit_write_barrier (cfg, args [0], args [1]);
5714 } else if (cmethod->klass->image == mono_defaults.corlib &&
5715 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
5716 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
5717 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
5718 if (mini_should_insert_breakpoint (cfg->method)) {
5719 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
5721 MONO_INST_NEW (cfg, ins, OP_NOP);
5722 MONO_ADD_INS (cfg->cbb, ins);
5726 } else if (cmethod->klass->image == mono_defaults.corlib &&
5727 (strcmp (cmethod->klass->name_space, "System") == 0) &&
5728 (strcmp (cmethod->klass->name, "Environment") == 0)) {
5729 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
5731 EMIT_NEW_ICONST (cfg, ins, 1);
5733 EMIT_NEW_ICONST (cfg, ins, 0);
5736 } else if (cmethod->klass->image == mono_defaults.corlib &&
5737 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
5738 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
5739 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
5740 /* No stack walks are currently available, so implement this as an intrinsic */
5741 MonoInst *assembly_ins;
5743 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
5744 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
5747 } else if (cmethod->klass->image == mono_defaults.corlib &&
5748 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
5749 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
5750 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
5751 /* No stack walks are currently available, so implement this as an intrinsic */
5752 MonoInst *method_ins;
5753 MonoMethod *declaring = cfg->method;
5755 /* This returns the declaring generic method */
5756 if (declaring->is_inflated)
5757 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
5758 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
5759 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
5760 cfg->no_inline = TRUE;
5761 if (cfg->method != cfg->current_method)
5762 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
5765 } else if (cmethod->klass == mono_defaults.math_class) {
5767 * There is general branchless code for Min/Max, but it does not work for
5769 * http://everything2.com/?node_id=1051618
5771 } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
5772 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
5773 MONO_INST_NEW (cfg, ins, OP_PCEQ);
5774 ins->dreg = alloc_preg (cfg);
5775 ins->type = STACK_I4;
5776 MONO_ADD_INS (cfg->cbb, ins);
5778 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
5779 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
5780 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
5781 !strcmp (cmethod->klass->name, "Selector")) ||
5782 ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
5783 !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
5784 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
5785 !strcmp (cmethod->klass->name, "Selector"))
5787 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
5788 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
5789 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
5792 MonoJumpInfoToken *ji;
5795 if (args [0]->opcode == OP_GOT_ENTRY) {
5796 pi = (MonoInst *)args [0]->inst_p1;
5797 g_assert (pi->opcode == OP_PATCH_INFO);
5798 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
5799 ji = (MonoJumpInfoToken *)pi->inst_p0;
5801 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
5802 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
5805 NULLIFY_INS (args [0]);
5807 s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
5808 return_val_if_nok (&cfg->error, NULL);
5810 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
5811 ins->dreg = mono_alloc_ireg (cfg);
5814 MONO_ADD_INS (cfg->cbb, ins);
5817 } else if (cmethod->klass->image == mono_defaults.corlib &&
5818 (strcmp (cmethod->klass->name_space, "System.Runtime.InteropServices") == 0) &&
5819 (strcmp (cmethod->klass->name, "Marshal") == 0)) {
5820 //Convert Marshal.PtrToStructure<T> of blittable T to direct loads
5821 if (strcmp (cmethod->name, "PtrToStructure") == 0 &&
5822 cmethod->is_inflated &&
5823 fsig->param_count == 1 &&
5824 !mini_method_check_context_used (cfg, cmethod)) {
5826 MonoGenericContext *method_context = mono_method_get_context (cmethod);
5827 MonoType *arg0 = method_context->method_inst->type_argv [0];
5828 if (mono_type_is_native_blittable (arg0))
5829 return mini_emit_memory_load (cfg, arg0, args [0], 0, 0);
5833 #ifdef MONO_ARCH_SIMD_INTRINSICS
5834 if (cfg->opt & MONO_OPT_SIMD) {
5835 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5841 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5845 if (COMPILE_LLVM (cfg)) {
5846 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
5851 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
5855 * This entry point could be used later for arbitrary method
5858 inline static MonoInst*
5859 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
5860 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
5862 if (method->klass == mono_defaults.string_class) {
5863 /* managed string allocation support */
5864 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
5865 MonoInst *iargs [2];
5866 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
5867 MonoMethod *managed_alloc = NULL;
5869 g_assert (vtable); /*Should not fail since it System.String*/
5870 #ifndef MONO_CROSS_COMPILE
5871 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
5875 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
5876 iargs [1] = args [0];
5877 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
5884 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
5886 MonoInst *store, *temp;
5889 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5890 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
5893 * FIXME: We should use *args++ = sp [0], but that would mean the arg
5894 * would be different than the MonoInst's used to represent arguments, and
5895 * the ldelema implementation can't deal with that.
5896 * Solution: When ldelema is used on an inline argument, create a var for
5897 * it, emit ldelema on that var, and emit the saving code below in
5898 * inline_method () if needed.
5900 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
5901 cfg->args [i] = temp;
5902 /* This uses cfg->args [i] which is set by the preceeding line */
5903 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
5904 store->cil_code = sp [0]->cil_code;
5909 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
5910 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
5912 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
5914 check_inline_called_method_name_limit (MonoMethod *called_method)
5917 static const char *limit = NULL;
5919 if (limit == NULL) {
5920 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
5922 if (limit_string != NULL)
5923 limit = limit_string;
5928 if (limit [0] != '\0') {
5929 char *called_method_name = mono_method_full_name (called_method, TRUE);
5931 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
5932 g_free (called_method_name);
5934 //return (strncmp_result <= 0);
5935 return (strncmp_result == 0);
5942 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
5944 check_inline_caller_method_name_limit (MonoMethod *caller_method)
5947 static const char *limit = NULL;
5949 if (limit == NULL) {
5950 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
5951 if (limit_string != NULL) {
5952 limit = limit_string;
5958 if (limit [0] != '\0') {
5959 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
5961 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
5962 g_free (caller_method_name);
5964 //return (strncmp_result <= 0);
5965 return (strncmp_result == 0);
5973 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
5975 static double r8_0 = 0.0;
5976 static float r4_0 = 0.0;
5980 rtype = mini_get_underlying_type (rtype);
5984 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
5985 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5986 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5987 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5988 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
5989 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
5990 MONO_INST_NEW (cfg, ins, OP_R4CONST);
5991 ins->type = STACK_R4;
5992 ins->inst_p0 = (void*)&r4_0;
5994 MONO_ADD_INS (cfg->cbb, ins);
5995 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5996 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5997 ins->type = STACK_R8;
5998 ins->inst_p0 = (void*)&r8_0;
6000 MONO_ADD_INS (cfg->cbb, ins);
6001 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6002 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6003 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6004 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6005 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6007 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6012 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6016 rtype = mini_get_underlying_type (rtype);
6020 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6021 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6022 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6023 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6024 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6025 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6026 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6027 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6028 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6029 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6030 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6031 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6032 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6033 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6035 emit_init_rvar (cfg, dreg, rtype);
6039 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6041 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6043 MonoInst *var = cfg->locals [local];
6044 if (COMPILE_SOFT_FLOAT (cfg)) {
6046 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
6047 emit_init_rvar (cfg, reg, type);
6048 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6051 emit_init_rvar (cfg, var->dreg, type);
6053 emit_dummy_init_rvar (cfg, var->dreg, type);
6058 mini_inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, guchar *ip, guint real_offset, gboolean inline_always)
6060 return inline_method (cfg, cmethod, fsig, sp, ip, real_offset, inline_always);
6066 * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
6069 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6070 guchar *ip, guint real_offset, gboolean inline_always)
6073 MonoInst *ins, *rvar = NULL;
6074 MonoMethodHeader *cheader;
6075 MonoBasicBlock *ebblock, *sbblock;
6077 MonoMethod *prev_inlined_method;
6078 MonoInst **prev_locals, **prev_args;
6079 MonoType **prev_arg_types;
6080 guint prev_real_offset;
6081 GHashTable *prev_cbb_hash;
6082 MonoBasicBlock **prev_cil_offset_to_bb;
6083 MonoBasicBlock *prev_cbb;
6084 const unsigned char *prev_ip;
6085 unsigned char *prev_cil_start;
6086 guint32 prev_cil_offset_to_bb_len;
6087 MonoMethod *prev_current_method;
6088 MonoGenericContext *prev_generic_context;
6089 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
6091 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6093 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6094 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6097 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6098 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6103 fsig = mono_method_signature (cmethod);
6105 if (cfg->verbose_level > 2)
6106 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6108 if (!cmethod->inline_info) {
6109 cfg->stat_inlineable_methods++;
6110 cmethod->inline_info = 1;
6113 /* allocate local variables */
6114 cheader = mono_method_get_header_checked (cmethod, &error);
6116 if (inline_always) {
6117 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
6118 mono_error_move (&cfg->error, &error);
6120 mono_error_cleanup (&error);
6125 /*Must verify before creating locals as it can cause the JIT to assert.*/
6126 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6127 mono_metadata_free_mh (cheader);
6131 /* allocate space to store the return value */
6132 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6133 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6136 prev_locals = cfg->locals;
6137 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6138 for (i = 0; i < cheader->num_locals; ++i)
6139 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6141 /* allocate start and end blocks */
6142 /* This is needed so if the inline is aborted, we can clean up */
6143 NEW_BBLOCK (cfg, sbblock);
6144 sbblock->real_offset = real_offset;
6146 NEW_BBLOCK (cfg, ebblock);
6147 ebblock->block_num = cfg->num_bblocks++;
6148 ebblock->real_offset = real_offset;
6150 prev_args = cfg->args;
6151 prev_arg_types = cfg->arg_types;
6152 prev_inlined_method = cfg->inlined_method;
6153 cfg->inlined_method = cmethod;
6154 cfg->ret_var_set = FALSE;
6155 cfg->inline_depth ++;
6156 prev_real_offset = cfg->real_offset;
6157 prev_cbb_hash = cfg->cbb_hash;
6158 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6159 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6160 prev_cil_start = cfg->cil_start;
6162 prev_cbb = cfg->cbb;
6163 prev_current_method = cfg->current_method;
6164 prev_generic_context = cfg->generic_context;
6165 prev_ret_var_set = cfg->ret_var_set;
6166 prev_disable_inline = cfg->disable_inline;
6168 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6171 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
6173 ret_var_set = cfg->ret_var_set;
6175 cfg->inlined_method = prev_inlined_method;
6176 cfg->real_offset = prev_real_offset;
6177 cfg->cbb_hash = prev_cbb_hash;
6178 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6179 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6180 cfg->cil_start = prev_cil_start;
6182 cfg->locals = prev_locals;
6183 cfg->args = prev_args;
6184 cfg->arg_types = prev_arg_types;
6185 cfg->current_method = prev_current_method;
6186 cfg->generic_context = prev_generic_context;
6187 cfg->ret_var_set = prev_ret_var_set;
6188 cfg->disable_inline = prev_disable_inline;
6189 cfg->inline_depth --;
6191 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6192 if (cfg->verbose_level > 2)
6193 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6195 cfg->stat_inlined_methods++;
6197 /* always add some code to avoid block split failures */
6198 MONO_INST_NEW (cfg, ins, OP_NOP);
6199 MONO_ADD_INS (prev_cbb, ins);
6201 prev_cbb->next_bb = sbblock;
6202 link_bblock (cfg, prev_cbb, sbblock);
6205 * Get rid of the begin and end bblocks if possible to aid local
6208 if (prev_cbb->out_count == 1)
6209 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6211 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6212 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6214 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6215 MonoBasicBlock *prev = ebblock->in_bb [0];
6217 if (prev->next_bb == ebblock) {
6218 mono_merge_basic_blocks (cfg, prev, ebblock);
6220 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6221 mono_merge_basic_blocks (cfg, prev_cbb, prev);
6222 cfg->cbb = prev_cbb;
6225 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
6230 * Its possible that the rvar is set in some prev bblock, but not in others.
6236 for (i = 0; i < ebblock->in_count; ++i) {
6237 bb = ebblock->in_bb [i];
6239 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6242 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6252 * If the inlined method contains only a throw, then the ret var is not
6253 * set, so set it to a dummy value.
6256 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6258 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6261 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6264 if (cfg->verbose_level > 2)
6265 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6266 cfg->exception_type = MONO_EXCEPTION_NONE;
6268 /* This gets rid of the newly added bblocks */
6269 cfg->cbb = prev_cbb;
6271 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6276 * Some of these comments may well be out-of-date.
6277 * Design decisions: we do a single pass over the IL code (and we do bblock
6278 * splitting/merging in the few cases when it's required: a back jump to an IL
6279 * address that was not already seen as bblock starting point).
6280 * Code is validated as we go (full verification is still better left to metadata/verify.c).
6281 * Complex operations are decomposed in simpler ones right away. We need to let the
6282 * arch-specific code peek and poke inside this process somehow (except when the
6283 * optimizations can take advantage of the full semantic info of coarse opcodes).
6284 * All the opcodes of the form opcode.s are 'normalized' to opcode.
6285 * MonoInst->opcode initially is the IL opcode or some simplification of that
6286 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
6287 * opcode with value bigger than OP_LAST.
6288 * At this point the IR can be handed over to an interpreter, a dumb code generator
6289 * or to the optimizing code generator that will translate it to SSA form.
6291 * Profiling directed optimizations.
6292 * We may compile by default with few or no optimizations and instrument the code
6293 * or the user may indicate what methods to optimize the most either in a config file
6294 * or through repeated runs where the compiler applies offline the optimizations to
6295 * each method and then decides if it was worth it.
6298 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6299 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6300 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6301 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6302 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6303 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6304 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6305 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
6307 /* offset from br.s -> br like opcodes */
6308 #define BIG_BRANCH_OFFSET 13
6311 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6313 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6315 return b == NULL || b == bb;
6319 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6321 unsigned char *ip = start;
6322 unsigned char *target;
6325 MonoBasicBlock *bblock;
6326 const MonoOpcode *opcode;
6329 cli_addr = ip - start;
6330 i = mono_opcode_value ((const guint8 **)&ip, end);
6333 opcode = &mono_opcodes [i];
6334 switch (opcode->argument) {
6335 case MonoInlineNone:
6338 case MonoInlineString:
6339 case MonoInlineType:
6340 case MonoInlineField:
6341 case MonoInlineMethod:
6344 case MonoShortInlineR:
6351 case MonoShortInlineVar:
6352 case MonoShortInlineI:
6355 case MonoShortInlineBrTarget:
6356 target = start + cli_addr + 2 + (signed char)ip [1];
6357 GET_BBLOCK (cfg, bblock, target);
6360 GET_BBLOCK (cfg, bblock, ip);
6362 case MonoInlineBrTarget:
6363 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
6364 GET_BBLOCK (cfg, bblock, target);
6367 GET_BBLOCK (cfg, bblock, ip);
6369 case MonoInlineSwitch: {
6370 guint32 n = read32 (ip + 1);
6373 cli_addr += 5 + 4 * n;
6374 target = start + cli_addr;
6375 GET_BBLOCK (cfg, bblock, target);
6377 for (j = 0; j < n; ++j) {
6378 target = start + cli_addr + (gint32)read32 (ip);
6379 GET_BBLOCK (cfg, bblock, target);
6389 g_assert_not_reached ();
6392 if (i == CEE_THROW) {
6393 unsigned char *bb_start = ip - 1;
6395 /* Find the start of the bblock containing the throw */
6397 while ((bb_start >= start) && !bblock) {
6398 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
6402 bblock->out_of_line = 1;
6412 static inline MonoMethod *
6413 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
6419 if (m->wrapper_type != MONO_WRAPPER_NONE) {
6420 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
6422 method = mono_class_inflate_generic_method_checked (method, context, error);
6425 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
6431 static inline MonoMethod *
6432 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
6435 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
6437 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
6438 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
6442 if (!method && !cfg)
6443 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6448 static inline MonoMethodSignature*
6449 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
6451 MonoMethodSignature *fsig;
6454 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6455 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6457 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
6458 return_val_if_nok (error, NULL);
6461 fsig = mono_inflate_generic_signature(fsig, context, error);
6467 throw_exception (void)
6469 static MonoMethod *method = NULL;
6472 MonoSecurityManager *secman = mono_security_manager_get_methods ();
6473 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
6480 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
6482 MonoMethod *thrower = throw_exception ();
6485 EMIT_NEW_PCONST (cfg, args [0], ex);
6486 mono_emit_method_call (cfg, thrower, args, NULL);
6490 * Return the original method is a wrapper is specified. We can only access
6491 * the custom attributes from the original method.
6494 get_original_method (MonoMethod *method)
6496 if (method->wrapper_type == MONO_WRAPPER_NONE)
6499 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
6500 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
6503 /* in other cases we need to find the original method */
6504 return mono_marshal_method_from_wrapper (method);
6508 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
6510 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6511 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
6513 emit_throw_exception (cfg, ex);
6517 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
6519 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6520 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
6522 emit_throw_exception (cfg, ex);
6526 * Check that the IL instructions at ip are the array initialization
6527 * sequence and return the pointer to the data and the size.
6530 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
6533 * newarr[System.Int32]
6535 * ldtoken field valuetype ...
6536 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
6538 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
6540 guint32 token = read32 (ip + 7);
6541 guint32 field_token = read32 (ip + 2);
6542 guint32 field_index = field_token & 0xffffff;
6544 const char *data_ptr;
6546 MonoMethod *cmethod;
6547 MonoClass *dummy_class;
6548 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
6552 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6556 *out_field_token = field_token;
6558 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
6561 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
6563 switch (mini_get_underlying_type (&klass->byval_arg)->type) {
6567 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
6568 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
6585 if (size > mono_type_size (field->type, &dummy_align))
6588 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
6589 if (!image_is_dynamic (method->klass->image)) {
6590 field_index = read32 (ip + 2) & 0xffffff;
6591 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
6592 data_ptr = mono_image_rva_map (method->klass->image, rva);
6593 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
6594 /* for aot code we do the lookup on load */
6595 if (aot && data_ptr)
6596 return (const char *)GUINT_TO_POINTER (rva);
6598 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
6600 data_ptr = mono_field_get_data (field);
6608 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
6611 char *method_fname = mono_method_full_name (method, TRUE);
6613 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
6616 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
6617 mono_error_cleanup (&error);
6618 } else if (header->code_size == 0)
6619 method_code = g_strdup ("method body is empty.");
6621 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
6622 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
6623 g_free (method_fname);
6624 g_free (method_code);
6625 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
6629 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
6632 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
6633 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
6634 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
6635 /* Optimize reg-reg moves away */
6637 * Can't optimize other opcodes, since sp[0] might point to
6638 * the last ins of a decomposed opcode.
6640 sp [0]->dreg = (cfg)->locals [n]->dreg;
6642 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
6647 * ldloca inhibits many optimizations so try to get rid of it in common
6650 static inline unsigned char *
6651 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
6661 local = read16 (ip + 2);
6665 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
6666 /* From the INITOBJ case */
6667 token = read32 (ip + 2);
6668 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
6669 CHECK_TYPELOAD (klass);
6670 type = mini_get_underlying_type (&klass->byval_arg);
6671 emit_init_local (cfg, local, type, TRUE);
6679 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
6681 MonoInst *icall_args [16];
6682 MonoInst *call_target, *ins, *vtable_ins;
6683 int arg_reg, this_reg, vtable_reg;
6684 gboolean is_iface = mono_class_is_interface (cmethod->klass);
6685 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
6686 gboolean variant_iface = FALSE;
6689 gboolean special_array_interface = cmethod->klass->is_array_special_interface;
6692 * In llvm-only mode, vtables contain function descriptors instead of
6693 * method addresses/trampolines.
6695 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
6698 slot = mono_method_get_imt_slot (cmethod);
6700 slot = mono_method_get_vtable_index (cmethod);
6702 this_reg = sp [0]->dreg;
6704 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
6705 variant_iface = TRUE;
6707 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
6709 * The simplest case, a normal virtual call.
6711 int slot_reg = alloc_preg (cfg);
6712 int addr_reg = alloc_preg (cfg);
6713 int arg_reg = alloc_preg (cfg);
6714 MonoBasicBlock *non_null_bb;
6716 vtable_reg = alloc_preg (cfg);
6717 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6718 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
6720 /* Load the vtable slot, which contains a function descriptor. */
6721 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6723 NEW_BBLOCK (cfg, non_null_bb);
6725 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
6726 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
6727 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
6730 // FIXME: Make the wrapper use the preserveall cconv
6731 // FIXME: Use one icall per slot for small slot numbers ?
6732 icall_args [0] = vtable_ins;
6733 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6734 /* Make the icall return the vtable slot value to save some code space */
6735 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
6736 ins->dreg = slot_reg;
6737 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
6740 MONO_START_BB (cfg, non_null_bb);
6741 /* Load the address + arg from the vtable slot */
6742 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6743 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
6745 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
6748 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt && !special_array_interface) {
6750 * A simple interface call
6752 * We make a call through an imt slot to obtain the function descriptor we need to call.
6753 * The imt slot contains a function descriptor for a runtime function + arg.
6755 int slot_reg = alloc_preg (cfg);
6756 int addr_reg = alloc_preg (cfg);
6757 int arg_reg = alloc_preg (cfg);
6758 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
6760 vtable_reg = alloc_preg (cfg);
6761 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6762 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
6765 * The slot is already initialized when the vtable is created so there is no need
6769 /* Load the imt slot, which contains a function descriptor. */
6770 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6772 /* Load the address + arg of the imt thunk from the imt slot */
6773 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6774 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
6776 * IMT thunks in llvm-only mode are C functions which take an info argument
6777 * plus the imt method and return the ftndesc to call.
6779 icall_args [0] = thunk_arg_ins;
6780 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
6781 cmethod, MONO_RGCTX_INFO_METHOD);
6782 ftndesc_ins = mini_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
6784 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
6787 if ((fsig->generic_param_count || variant_iface || special_array_interface) && !is_gsharedvt) {
6789 * This is similar to the interface case, the vtable slot points to an imt thunk which is
6790 * dynamically extended as more instantiations are discovered.
6791 * This handles generic virtual methods both on classes and interfaces.
6793 int slot_reg = alloc_preg (cfg);
6794 int addr_reg = alloc_preg (cfg);
6795 int arg_reg = alloc_preg (cfg);
6796 int ftndesc_reg = alloc_preg (cfg);
6797 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
6798 MonoBasicBlock *slowpath_bb, *end_bb;
6800 NEW_BBLOCK (cfg, slowpath_bb);
6801 NEW_BBLOCK (cfg, end_bb);
6803 vtable_reg = alloc_preg (cfg);
6804 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6806 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
6808 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
6810 /* Load the slot, which contains a function descriptor. */
6811 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6813 /* These slots are not initialized, so fall back to the slow path until they are initialized */
6814 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
6815 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
6816 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
6819 /* Same as with iface calls */
6820 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6821 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
6822 icall_args [0] = thunk_arg_ins;
6823 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
6824 cmethod, MONO_RGCTX_INFO_METHOD);
6825 ftndesc_ins = mini_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
6826 ftndesc_ins->dreg = ftndesc_reg;
6828 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
6829 * they don't know about yet. Fall back to the slowpath in that case.
6831 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
6832 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
6834 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6837 MONO_START_BB (cfg, slowpath_bb);
6838 icall_args [0] = vtable_ins;
6839 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6840 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
6841 cmethod, MONO_RGCTX_INFO_METHOD);
6843 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
6845 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
6846 ftndesc_ins->dreg = ftndesc_reg;
6847 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6850 MONO_START_BB (cfg, end_bb);
6851 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
6855 * Non-optimized cases
6857 icall_args [0] = sp [0];
6858 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6860 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
6861 cmethod, MONO_RGCTX_INFO_METHOD);
6863 arg_reg = alloc_preg (cfg);
6864 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
6865 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
6867 g_assert (is_gsharedvt);
6869 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
6871 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
6874 * Pass the extra argument even if the callee doesn't receive it, most
6875 * calling conventions allow this.
6877 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
6881 is_exception_class (MonoClass *klass)
6884 if (klass == mono_defaults.exception_class)
6886 klass = klass->parent;
6892 * is_jit_optimizer_disabled:
6894 * Determine whenever M's assembly has a DebuggableAttribute with the
6895 * IsJITOptimizerDisabled flag set.
6898 is_jit_optimizer_disabled (MonoMethod *m)
6901 MonoAssembly *ass = m->klass->image->assembly;
6902 MonoCustomAttrInfo* attrs;
6905 gboolean val = FALSE;
6908 if (ass->jit_optimizer_disabled_inited)
6909 return ass->jit_optimizer_disabled;
6911 klass = mono_class_try_get_debuggable_attribute_class ();
6915 ass->jit_optimizer_disabled = FALSE;
6916 mono_memory_barrier ();
6917 ass->jit_optimizer_disabled_inited = TRUE;
6921 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
6922 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6924 for (i = 0; i < attrs->num_attrs; ++i) {
6925 MonoCustomAttrEntry *attr = &attrs->attrs [i];
6927 MonoMethodSignature *sig;
6929 if (!attr->ctor || attr->ctor->klass != klass)
6931 /* Decode the attribute. See reflection.c */
6932 p = (const char*)attr->data;
6933 g_assert (read16 (p) == 0x0001);
6936 // FIXME: Support named parameters
6937 sig = mono_method_signature (attr->ctor);
6938 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
6940 /* Two boolean arguments */
6944 mono_custom_attrs_free (attrs);
6947 ass->jit_optimizer_disabled = val;
6948 mono_memory_barrier ();
6949 ass->jit_optimizer_disabled_inited = TRUE;
6955 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
6957 gboolean supported_tail_call;
6960 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
6962 for (i = 0; i < fsig->param_count; ++i) {
6963 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
6964 /* These can point to the current method's stack */
6965 supported_tail_call = FALSE;
6967 if (fsig->hasthis && cmethod->klass->valuetype)
6968 /* this might point to the current method's stack */
6969 supported_tail_call = FALSE;
6970 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
6971 supported_tail_call = FALSE;
6972 if (cfg->method->save_lmf)
6973 supported_tail_call = FALSE;
6974 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
6975 supported_tail_call = FALSE;
6976 if (call_opcode != CEE_CALL)
6977 supported_tail_call = FALSE;
6979 /* Debugging support */
6981 if (supported_tail_call) {
6982 if (!mono_debug_count ())
6983 supported_tail_call = FALSE;
6987 return supported_tail_call;
6993 * Handle calls made to ctors from NEWOBJ opcodes.
6996 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
6997 MonoInst **sp, guint8 *ip, int *inline_costs)
6999 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7001 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7002 mono_method_is_generic_sharable (cmethod, TRUE)) {
7003 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7004 mono_class_vtable (cfg->domain, cmethod->klass);
7005 CHECK_TYPELOAD (cmethod->klass);
7007 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7008 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7011 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
7012 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7014 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7016 CHECK_TYPELOAD (cmethod->klass);
7017 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7022 /* Avoid virtual calls to ctors if possible */
7023 if (mono_class_is_marshalbyref (cmethod->klass))
7024 callvirt_this_arg = sp [0];
7026 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7027 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7028 CHECK_CFG_EXCEPTION;
7029 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7030 mono_method_check_inlining (cfg, cmethod) &&
7031 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7034 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7035 cfg->real_offset += 5;
7037 *inline_costs += costs - 5;
7039 INLINE_FAILURE ("inline failure");
7040 // FIXME-VT: Clean this up
7041 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7042 GSHAREDVT_FAILURE(*ip);
7043 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7045 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7048 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7050 if (cfg->llvm_only) {
7051 // FIXME: Avoid initializing vtable_arg
7052 emit_llvmonly_calli (cfg, fsig, sp, addr);
7054 mini_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7056 } else if (context_used &&
7057 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7058 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7059 MonoInst *cmethod_addr;
7061 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7063 if (cfg->llvm_only) {
7064 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
7065 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7066 emit_llvmonly_calli (cfg, fsig, sp, addr);
7068 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7069 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7071 mini_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7074 INLINE_FAILURE ("ctor call");
7075 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7076 callvirt_this_arg, NULL, vtable_arg);
7083 emit_setret (MonoCompile *cfg, MonoInst *val)
7085 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
7088 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
7091 if (!cfg->vret_addr) {
7092 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
7094 EMIT_NEW_RETLOADA (cfg, ret_addr);
7096 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
7097 ins->klass = mono_class_from_mono_type (ret_type);
7100 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
7101 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
7102 MonoInst *iargs [1];
7106 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
7107 mono_arch_emit_setret (cfg, cfg->method, conv);
7109 mono_arch_emit_setret (cfg, cfg->method, val);
7112 mono_arch_emit_setret (cfg, cfg->method, val);
7118 * mono_method_to_ir:
7120 * Translate the .net IL into linear IR.
7122 * @start_bblock: if not NULL, the starting basic block, used during inlining.
7123 * @end_bblock: if not NULL, the ending basic block, used during inlining.
7124 * @return_var: if not NULL, the place where the return value is stored, used during inlining.
7125 * @inline_args: if not NULL, contains the arguments to the inline call
7126 * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
7127 * @is_virtual_call: whether this method is being called as a result of a call to callvirt
7129 * This method is used to turn ECMA IL into Mono's internal Linear IR
7130 * reprensetation. It is used both for entire methods, as well as
7131 * inlining existing methods. In the former case, the @start_bblock,
7132 * @end_bblock, @return_var, @inline_args are all set to NULL, and the
7133 * inline_offset is set to zero.
7135 * Returns: the inline cost, or -1 if there was an error processing this method.
7138 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
7139 MonoInst *return_var, MonoInst **inline_args,
7140 guint inline_offset, gboolean is_virtual_call)
7143 MonoInst *ins, **sp, **stack_start;
7144 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
7145 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7146 MonoMethod *cmethod, *method_definition;
7147 MonoInst **arg_array;
7148 MonoMethodHeader *header;
7150 guint32 token, ins_flag;
7152 MonoClass *constrained_class = NULL;
7153 unsigned char *ip, *end, *target, *err_pos;
7154 MonoMethodSignature *sig;
7155 MonoGenericContext *generic_context = NULL;
7156 MonoGenericContainer *generic_container = NULL;
7157 MonoType **param_types;
7158 int i, n, start_new_bblock, dreg;
7159 int num_calls = 0, inline_costs = 0;
7160 int breakpoint_id = 0;
7162 GSList *class_inits = NULL;
7163 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7165 gboolean init_locals, seq_points, skip_dead_blocks;
7166 gboolean sym_seq_points = FALSE;
7167 MonoDebugMethodInfo *minfo;
7168 MonoBitSet *seq_point_locs = NULL;
7169 MonoBitSet *seq_point_set_locs = NULL;
7171 cfg->disable_inline = is_jit_optimizer_disabled (method);
7173 /* serialization and xdomain stuff may need access to private fields and methods */
7174 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7175 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7176 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7177 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7178 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7179 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7181 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7182 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7183 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7184 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7185 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7187 image = method->klass->image;
7188 header = mono_method_get_header_checked (method, &cfg->error);
7190 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7191 goto exception_exit;
7193 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7196 generic_container = mono_method_get_generic_container (method);
7197 sig = mono_method_signature (method);
7198 num_args = sig->hasthis + sig->param_count;
7199 ip = (unsigned char*)header->code;
7200 cfg->cil_start = ip;
7201 end = ip + header->code_size;
7202 cfg->stat_cil_code_size += header->code_size;
7204 seq_points = cfg->gen_seq_points && cfg->method == method;
7206 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7207 /* We could hit a seq point before attaching to the JIT (#8338) */
7211 if (cfg->gen_sdb_seq_points && cfg->method == method) {
7212 minfo = mono_debug_lookup_method (method);
7214 MonoSymSeqPoint *sps;
7215 int i, n_il_offsets;
7217 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7218 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7219 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);
7220 sym_seq_points = TRUE;
7221 for (i = 0; i < n_il_offsets; ++i) {
7222 if (sps [i].il_offset < header->code_size)
7223 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7227 MonoDebugMethodAsyncInfo* asyncMethod = mono_debug_lookup_method_async_debug_info (method);
7229 for (i = 0; asyncMethod != NULL && i < asyncMethod->num_awaits; i++)
7231 mono_bitset_set_fast (seq_point_locs, asyncMethod->resume_offsets[i]);
7232 mono_bitset_set_fast (seq_point_locs, asyncMethod->yield_offsets[i]);
7234 mono_debug_free_method_async_debug_info (asyncMethod);
7236 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7237 /* Methods without line number info like auto-generated property accessors */
7238 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7239 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);
7240 sym_seq_points = TRUE;
7245 * Methods without init_locals set could cause asserts in various passes
7246 * (#497220). To work around this, we emit dummy initialization opcodes
7247 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7248 * on some platforms.
7250 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
7251 init_locals = header->init_locals;
7255 method_definition = method;
7256 while (method_definition->is_inflated) {
7257 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7258 method_definition = imethod->declaring;
7261 /* SkipVerification is not allowed if core-clr is enabled */
7262 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7264 dont_verify_stloc = TRUE;
7267 if (sig->is_inflated)
7268 generic_context = mono_method_get_context (method);
7269 else if (generic_container)
7270 generic_context = &generic_container->context;
7271 cfg->generic_context = generic_context;
7274 g_assert (!sig->has_type_parameters);
7276 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7277 g_assert (method->is_inflated);
7278 g_assert (mono_method_get_context (method)->method_inst);
7280 if (method->is_inflated && mono_method_get_context (method)->method_inst)
7281 g_assert (sig->generic_param_count);
7283 if (cfg->method == method) {
7284 cfg->real_offset = 0;
7286 cfg->real_offset = inline_offset;
7289 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7290 cfg->cil_offset_to_bb_len = header->code_size;
7292 cfg->current_method = method;
7294 if (cfg->verbose_level > 2)
7295 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7297 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7299 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7300 for (n = 0; n < sig->param_count; ++n)
7301 param_types [n + sig->hasthis] = sig->params [n];
7302 cfg->arg_types = param_types;
7304 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7305 if (cfg->method == method) {
7307 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7308 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7311 NEW_BBLOCK (cfg, start_bblock);
7312 cfg->bb_entry = start_bblock;
7313 start_bblock->cil_code = NULL;
7314 start_bblock->cil_length = 0;
7317 NEW_BBLOCK (cfg, end_bblock);
7318 cfg->bb_exit = end_bblock;
7319 end_bblock->cil_code = NULL;
7320 end_bblock->cil_length = 0;
7321 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7322 g_assert (cfg->num_bblocks == 2);
7324 arg_array = cfg->args;
7326 if (header->num_clauses) {
7327 cfg->spvars = g_hash_table_new (NULL, NULL);
7328 cfg->exvars = g_hash_table_new (NULL, NULL);
7330 /* handle exception clauses */
7331 for (i = 0; i < header->num_clauses; ++i) {
7332 MonoBasicBlock *try_bb;
7333 MonoExceptionClause *clause = &header->clauses [i];
7334 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7336 try_bb->real_offset = clause->try_offset;
7337 try_bb->try_start = TRUE;
7338 try_bb->region = ((i + 1) << 8) | clause->flags;
7339 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7340 tblock->real_offset = clause->handler_offset;
7341 tblock->flags |= BB_EXCEPTION_HANDLER;
7344 * Linking the try block with the EH block hinders inlining as we won't be able to
7345 * merge the bblocks from inlining and produce an artificial hole for no good reason.
7347 if (COMPILE_LLVM (cfg))
7348 link_bblock (cfg, try_bb, tblock);
7350 if (*(ip + clause->handler_offset) == CEE_POP)
7351 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7353 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7354 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7355 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7356 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7357 MONO_ADD_INS (tblock, ins);
7359 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7360 /* finally clauses already have a seq point */
7361 /* seq points for filter clauses are emitted below */
7362 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7363 MONO_ADD_INS (tblock, ins);
7366 /* todo: is a fault block unsafe to optimize? */
7367 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7368 tblock->flags |= BB_EXCEPTION_UNSAFE;
7371 /*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);
7373 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7375 /* catch and filter blocks get the exception object on the stack */
7376 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7377 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7379 /* mostly like handle_stack_args (), but just sets the input args */
7380 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7381 tblock->in_scount = 1;
7382 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7383 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7387 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7388 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7389 if (!cfg->compile_llvm) {
7390 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7391 ins->dreg = tblock->in_stack [0]->dreg;
7392 MONO_ADD_INS (tblock, ins);
7395 MonoInst *dummy_use;
7398 * Add a dummy use for the exvar so its liveness info will be
7401 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7404 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7405 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7406 MONO_ADD_INS (tblock, ins);
7409 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7410 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7411 tblock->flags |= BB_EXCEPTION_HANDLER;
7412 tblock->real_offset = clause->data.filter_offset;
7413 tblock->in_scount = 1;
7414 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7415 /* The filter block shares the exvar with the handler block */
7416 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7417 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7418 MONO_ADD_INS (tblock, ins);
7422 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7423 clause->data.catch_class &&
7425 mono_class_check_context_used (clause->data.catch_class)) {
7427 * In shared generic code with catch
7428 * clauses containing type variables
7429 * the exception handling code has to
7430 * be able to get to the rgctx.
7431 * Therefore we have to make sure that
7432 * the vtable/mrgctx argument (for
7433 * static or generic methods) or the
7434 * "this" argument (for non-static
7435 * methods) are live.
7437 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7438 mini_method_get_context (method)->method_inst ||
7439 method->klass->valuetype) {
7440 mono_get_vtable_var (cfg);
7442 MonoInst *dummy_use;
7444 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7449 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7450 cfg->cbb = start_bblock;
7451 cfg->args = arg_array;
7452 mono_save_args (cfg, sig, inline_args);
7455 /* FIRST CODE BLOCK */
7456 NEW_BBLOCK (cfg, tblock);
7457 tblock->cil_code = ip;
7461 ADD_BBLOCK (cfg, tblock);
7463 if (cfg->method == method) {
7464 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7465 if (breakpoint_id) {
7466 MONO_INST_NEW (cfg, ins, OP_BREAK);
7467 MONO_ADD_INS (cfg->cbb, ins);
7471 /* we use a separate basic block for the initialization code */
7472 NEW_BBLOCK (cfg, init_localsbb);
7473 if (cfg->method == method)
7474 cfg->bb_init = init_localsbb;
7475 init_localsbb->real_offset = cfg->real_offset;
7476 start_bblock->next_bb = init_localsbb;
7477 init_localsbb->next_bb = cfg->cbb;
7478 link_bblock (cfg, start_bblock, init_localsbb);
7479 link_bblock (cfg, init_localsbb, cfg->cbb);
7481 cfg->cbb = init_localsbb;
7483 if (cfg->gsharedvt && cfg->method == method) {
7484 MonoGSharedVtMethodInfo *info;
7485 MonoInst *var, *locals_var;
7488 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7489 info->method = cfg->method;
7490 info->count_entries = 16;
7491 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7492 cfg->gsharedvt_info = info;
7494 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7495 /* prevent it from being register allocated */
7496 //var->flags |= MONO_INST_VOLATILE;
7497 cfg->gsharedvt_info_var = var;
7499 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7500 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7502 /* Allocate locals */
7503 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7504 /* prevent it from being register allocated */
7505 //locals_var->flags |= MONO_INST_VOLATILE;
7506 cfg->gsharedvt_locals_var = locals_var;
7508 dreg = alloc_ireg (cfg);
7509 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7511 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7512 ins->dreg = locals_var->dreg;
7514 MONO_ADD_INS (cfg->cbb, ins);
7515 cfg->gsharedvt_locals_var_ins = ins;
7517 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7520 ins->flags |= MONO_INST_INIT;
7524 if (mono_security_core_clr_enabled ()) {
7525 /* check if this is native code, e.g. an icall or a p/invoke */
7526 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
7527 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
7529 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
7530 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
7532 /* if this ia a native call then it can only be JITted from platform code */
7533 if ((icall || pinvk) && method->klass && method->klass->image) {
7534 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
7535 MonoException *ex = icall ? mono_get_exception_security () :
7536 mono_get_exception_method_access ();
7537 emit_throw_exception (cfg, ex);
7544 CHECK_CFG_EXCEPTION;
7546 if (header->code_size == 0)
7549 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
7554 if (cfg->method == method)
7555 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
7557 for (n = 0; n < header->num_locals; ++n) {
7558 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
7563 /* We force the vtable variable here for all shared methods
7564 for the possibility that they might show up in a stack
7565 trace where their exact instantiation is needed. */
7566 if (cfg->gshared && method == cfg->method) {
7567 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7568 mini_method_get_context (method)->method_inst ||
7569 method->klass->valuetype) {
7570 mono_get_vtable_var (cfg);
7572 /* FIXME: Is there a better way to do this?
7573 We need the variable live for the duration
7574 of the whole method. */
7575 cfg->args [0]->flags |= MONO_INST_VOLATILE;
7579 /* add a check for this != NULL to inlined methods */
7580 if (is_virtual_call) {
7583 NEW_ARGLOAD (cfg, arg_ins, 0);
7584 MONO_ADD_INS (cfg->cbb, arg_ins);
7585 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
7588 skip_dead_blocks = !dont_verify;
7589 if (skip_dead_blocks) {
7590 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
7595 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
7596 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
7599 start_new_bblock = 0;
7601 if (cfg->method == method)
7602 cfg->real_offset = ip - header->code;
7604 cfg->real_offset = inline_offset;
7609 if (start_new_bblock) {
7610 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
7611 if (start_new_bblock == 2) {
7612 g_assert (ip == tblock->cil_code);
7614 GET_BBLOCK (cfg, tblock, ip);
7616 cfg->cbb->next_bb = tblock;
7618 start_new_bblock = 0;
7619 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7620 if (cfg->verbose_level > 3)
7621 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7622 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7626 g_slist_free (class_inits);
7629 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
7630 link_bblock (cfg, cfg->cbb, tblock);
7631 if (sp != stack_start) {
7632 handle_stack_args (cfg, stack_start, sp - stack_start);
7634 CHECK_UNVERIFIABLE (cfg);
7636 cfg->cbb->next_bb = tblock;
7638 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7639 if (cfg->verbose_level > 3)
7640 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7641 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7644 g_slist_free (class_inits);
7649 if (skip_dead_blocks) {
7650 int ip_offset = ip - header->code;
7652 if (ip_offset == bb->end)
7656 int op_size = mono_opcode_size (ip, end);
7657 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
7659 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
7661 if (ip_offset + op_size == bb->end) {
7662 MONO_INST_NEW (cfg, ins, OP_NOP);
7663 MONO_ADD_INS (cfg->cbb, ins);
7664 start_new_bblock = 1;
7672 * Sequence points are points where the debugger can place a breakpoint.
7673 * Currently, we generate these automatically at points where the IL
7676 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
7678 * Make methods interruptable at the beginning, and at the targets of
7679 * backward branches.
7680 * Also, do this at the start of every bblock in methods with clauses too,
7681 * to be able to handle instructions with inprecise control flow like
7683 * Backward branches are handled at the end of method-to-ir ().
7685 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
7686 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
7688 /* Avoid sequence points on empty IL like .volatile */
7689 // FIXME: Enable this
7690 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
7691 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
7692 if ((sp != stack_start) && !sym_seq_point)
7693 ins->flags |= MONO_INST_NONEMPTY_STACK;
7694 MONO_ADD_INS (cfg->cbb, ins);
7697 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
7700 cfg->cbb->real_offset = cfg->real_offset;
7702 if ((cfg->method == method) && cfg->coverage_info) {
7703 guint32 cil_offset = ip - header->code;
7704 gpointer counter = &cfg->coverage_info->data [cil_offset].count;
7705 cfg->coverage_info->data [cil_offset].cil_code = ip;
7707 if (mono_arch_opcode_supported (OP_ATOMIC_ADD_I4)) {
7708 MonoInst *one_ins, *load_ins;
7710 EMIT_NEW_PCONST (cfg, load_ins, counter);
7711 EMIT_NEW_ICONST (cfg, one_ins, 1);
7712 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
7713 ins->dreg = mono_alloc_ireg (cfg);
7714 ins->inst_basereg = load_ins->dreg;
7715 ins->inst_offset = 0;
7716 ins->sreg2 = one_ins->dreg;
7717 ins->type = STACK_I4;
7718 MONO_ADD_INS (cfg->cbb, ins);
7720 EMIT_NEW_PCONST (cfg, ins, counter);
7721 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
7725 if (cfg->verbose_level > 3)
7726 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
7730 if (seq_points && !sym_seq_points && sp != stack_start) {
7732 * The C# compiler uses these nops to notify the JIT that it should
7733 * insert seq points.
7735 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
7736 MONO_ADD_INS (cfg->cbb, ins);
7738 if (cfg->keep_cil_nops)
7739 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
7741 MONO_INST_NEW (cfg, ins, OP_NOP);
7743 MONO_ADD_INS (cfg->cbb, ins);
7746 if (mini_should_insert_breakpoint (cfg->method)) {
7747 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
7749 MONO_INST_NEW (cfg, ins, OP_NOP);
7752 MONO_ADD_INS (cfg->cbb, ins);
7758 CHECK_STACK_OVF (1);
7759 n = (*ip)-CEE_LDARG_0;
7761 EMIT_NEW_ARGLOAD (cfg, ins, n);
7769 CHECK_STACK_OVF (1);
7770 n = (*ip)-CEE_LDLOC_0;
7772 EMIT_NEW_LOCLOAD (cfg, ins, n);
7781 n = (*ip)-CEE_STLOC_0;
7784 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
7786 emit_stloc_ir (cfg, sp, header, n);
7793 CHECK_STACK_OVF (1);
7796 EMIT_NEW_ARGLOAD (cfg, ins, n);
7802 CHECK_STACK_OVF (1);
7805 NEW_ARGLOADA (cfg, ins, n);
7806 MONO_ADD_INS (cfg->cbb, ins);
7816 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
7818 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
7823 CHECK_STACK_OVF (1);
7826 EMIT_NEW_LOCLOAD (cfg, ins, n);
7830 case CEE_LDLOCA_S: {
7831 unsigned char *tmp_ip;
7833 CHECK_STACK_OVF (1);
7834 CHECK_LOCAL (ip [1]);
7836 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
7842 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
7851 CHECK_LOCAL (ip [1]);
7852 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
7854 emit_stloc_ir (cfg, sp, header, ip [1]);
7859 CHECK_STACK_OVF (1);
7860 EMIT_NEW_PCONST (cfg, ins, NULL);
7861 ins->type = STACK_OBJ;
7866 CHECK_STACK_OVF (1);
7867 EMIT_NEW_ICONST (cfg, ins, -1);
7880 CHECK_STACK_OVF (1);
7881 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
7887 CHECK_STACK_OVF (1);
7889 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
7895 CHECK_STACK_OVF (1);
7896 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
7902 CHECK_STACK_OVF (1);
7903 MONO_INST_NEW (cfg, ins, OP_I8CONST);
7904 ins->type = STACK_I8;
7905 ins->dreg = alloc_dreg (cfg, STACK_I8);
7907 ins->inst_l = (gint64)read64 (ip);
7908 MONO_ADD_INS (cfg->cbb, ins);
7914 gboolean use_aotconst = FALSE;
7916 #ifdef TARGET_POWERPC
7917 /* FIXME: Clean this up */
7918 if (cfg->compile_aot)
7919 use_aotconst = TRUE;
7922 /* FIXME: we should really allocate this only late in the compilation process */
7923 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
7925 CHECK_STACK_OVF (1);
7931 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
7933 dreg = alloc_freg (cfg);
7934 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
7935 ins->type = cfg->r4_stack_type;
7937 MONO_INST_NEW (cfg, ins, OP_R4CONST);
7938 ins->type = cfg->r4_stack_type;
7939 ins->dreg = alloc_dreg (cfg, STACK_R8);
7941 MONO_ADD_INS (cfg->cbb, ins);
7951 gboolean use_aotconst = FALSE;
7953 #ifdef TARGET_POWERPC
7954 /* FIXME: Clean this up */
7955 if (cfg->compile_aot)
7956 use_aotconst = TRUE;
7959 /* FIXME: we should really allocate this only late in the compilation process */
7960 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
7962 CHECK_STACK_OVF (1);
7968 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
7970 dreg = alloc_freg (cfg);
7971 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
7972 ins->type = STACK_R8;
7974 MONO_INST_NEW (cfg, ins, OP_R8CONST);
7975 ins->type = STACK_R8;
7976 ins->dreg = alloc_dreg (cfg, STACK_R8);
7978 MONO_ADD_INS (cfg->cbb, ins);
7987 MonoInst *temp, *store;
7989 CHECK_STACK_OVF (1);
7993 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
7994 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
7996 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
7999 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8012 if (sp [0]->type == STACK_R8)
8013 /* we need to pop the value from the x86 FP stack */
8014 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8019 MonoMethodSignature *fsig;
8022 INLINE_FAILURE ("jmp");
8023 GSHAREDVT_FAILURE (*ip);
8026 if (stack_start != sp)
8028 token = read32 (ip + 1);
8029 /* FIXME: check the signature matches */
8030 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8033 if (cfg->gshared && mono_method_check_context_used (cmethod))
8034 GENERIC_SHARING_FAILURE (CEE_JMP);
8036 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8038 fsig = mono_method_signature (cmethod);
8039 n = fsig->param_count + fsig->hasthis;
8040 if (cfg->llvm_only) {
8043 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8044 for (i = 0; i < n; ++i)
8045 EMIT_NEW_ARGLOAD (cfg, args [i], i);
8046 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
8048 * The code in mono-basic-block.c treats the rest of the code as dead, but we
8049 * have to emit a normal return since llvm expects it.
8052 emit_setret (cfg, ins);
8053 MONO_INST_NEW (cfg, ins, OP_BR);
8054 ins->inst_target_bb = end_bblock;
8055 MONO_ADD_INS (cfg->cbb, ins);
8056 link_bblock (cfg, cfg->cbb, end_bblock);
8059 } else if (cfg->backend->have_op_tail_call) {
8060 /* Handle tail calls similarly to calls */
8063 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8064 call->method = cmethod;
8065 call->tail_call = TRUE;
8066 call->signature = mono_method_signature (cmethod);
8067 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8068 call->inst.inst_p0 = cmethod;
8069 for (i = 0; i < n; ++i)
8070 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8072 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
8073 call->vret_var = cfg->vret_addr;
8075 mono_arch_emit_call (cfg, call);
8076 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8077 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8079 for (i = 0; i < num_args; ++i)
8080 /* Prevent arguments from being optimized away */
8081 arg_array [i]->flags |= MONO_INST_VOLATILE;
8083 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8084 ins = (MonoInst*)call;
8085 ins->inst_p0 = cmethod;
8086 MONO_ADD_INS (cfg->cbb, ins);
8090 start_new_bblock = 1;
8095 MonoMethodSignature *fsig;
8098 token = read32 (ip + 1);
8102 //GSHAREDVT_FAILURE (*ip);
8107 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
8110 if (method->dynamic && fsig->pinvoke) {
8114 * This is a call through a function pointer using a pinvoke
8115 * signature. Have to create a wrapper and call that instead.
8116 * FIXME: This is very slow, need to create a wrapper at JIT time
8117 * instead based on the signature.
8119 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8120 EMIT_NEW_PCONST (cfg, args [1], fsig);
8122 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8125 n = fsig->param_count + fsig->hasthis;
8129 //g_assert (!virtual_ || fsig->hasthis);
8133 inline_costs += 10 * num_calls++;
8136 * Making generic calls out of gsharedvt methods.
8137 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8138 * patching gshared method addresses into a gsharedvt method.
8140 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8142 * We pass the address to the gsharedvt trampoline in the rgctx reg
8144 MonoInst *callee = addr;
8146 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8148 GSHAREDVT_FAILURE (*ip);
8152 GSHAREDVT_FAILURE (*ip);
8154 addr = emit_get_rgctx_sig (cfg, context_used,
8155 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8156 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8160 /* Prevent inlining of methods with indirect calls */
8161 INLINE_FAILURE ("indirect call");
8163 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8164 MonoJumpInfoType info_type;
8168 * Instead of emitting an indirect call, emit a direct call
8169 * with the contents of the aotconst as the patch info.
8171 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8172 info_type = (MonoJumpInfoType)addr->inst_c1;
8173 info_data = addr->inst_p0;
8175 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
8176 info_data = addr->inst_right->inst_left;
8179 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
8180 ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
8183 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8184 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8189 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8193 /* End of call, INS should contain the result of the call, if any */
8195 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8197 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8200 CHECK_CFG_EXCEPTION;
8204 constrained_class = NULL;
8208 case CEE_CALLVIRT: {
8209 MonoInst *addr = NULL;
8210 MonoMethodSignature *fsig = NULL;
8212 int virtual_ = *ip == CEE_CALLVIRT;
8213 gboolean pass_imt_from_rgctx = FALSE;
8214 MonoInst *imt_arg = NULL;
8215 MonoInst *keep_this_alive = NULL;
8216 gboolean pass_vtable = FALSE;
8217 gboolean pass_mrgctx = FALSE;
8218 MonoInst *vtable_arg = NULL;
8219 gboolean check_this = FALSE;
8220 gboolean supported_tail_call = FALSE;
8221 gboolean tail_call = FALSE;
8222 gboolean need_seq_point = FALSE;
8223 guint32 call_opcode = *ip;
8224 gboolean emit_widen = TRUE;
8225 gboolean push_res = TRUE;
8226 gboolean skip_ret = FALSE;
8227 gboolean delegate_invoke = FALSE;
8228 gboolean direct_icall = FALSE;
8229 gboolean constrained_partial_call = FALSE;
8230 MonoMethod *cil_method;
8233 token = read32 (ip + 1);
8237 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8240 cil_method = cmethod;
8242 if (constrained_class) {
8243 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8244 if (!mini_is_gsharedvt_klass (constrained_class)) {
8245 g_assert (!cmethod->klass->valuetype);
8246 if (!mini_type_is_reference (&constrained_class->byval_arg))
8247 constrained_partial_call = TRUE;
8251 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8252 if (cfg->verbose_level > 2)
8253 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8254 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8255 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8257 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8261 if (cfg->verbose_level > 2)
8262 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8264 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8266 * This is needed since get_method_constrained can't find
8267 * the method in klass representing a type var.
8268 * The type var is guaranteed to be a reference type in this
8271 if (!mini_is_gsharedvt_klass (constrained_class))
8272 g_assert (!cmethod->klass->valuetype);
8274 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8279 if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
8280 /* Use the corresponding method from the base type to avoid boxing */
8281 MonoType *base_type = mono_class_enum_basetype (constrained_class);
8282 g_assert (base_type);
8283 constrained_class = mono_class_from_mono_type (base_type);
8284 cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
8289 if (!dont_verify && !cfg->skip_visibility) {
8290 MonoMethod *target_method = cil_method;
8291 if (method->is_inflated) {
8292 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
8295 if (!mono_method_can_access_method (method_definition, target_method) &&
8296 !mono_method_can_access_method (method, cil_method))
8297 emit_method_access_failure (cfg, method, cil_method);
8300 if (mono_security_core_clr_enabled ())
8301 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8303 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8304 /* MS.NET seems to silently convert this to a callvirt */
8309 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8310 * converts to a callvirt.
8312 * tests/bug-515884.il is an example of this behavior
8314 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8315 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8316 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8320 if (!cmethod->klass->inited)
8321 if (!mono_class_init (cmethod->klass))
8322 TYPE_LOAD_ERROR (cmethod->klass);
8324 fsig = mono_method_signature (cmethod);
8327 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8328 mini_class_is_system_array (cmethod->klass)) {
8329 array_rank = cmethod->klass->rank;
8330 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8331 direct_icall = TRUE;
8332 } else if (fsig->pinvoke) {
8333 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8334 fsig = mono_method_signature (wrapper);
8335 } else if (constrained_class) {
8337 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8341 if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
8342 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
8344 /* See code below */
8345 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8346 MonoBasicBlock *tbb;
8348 GET_BBLOCK (cfg, tbb, ip + 5);
8349 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8351 * We want to extend the try block to cover the call, but we can't do it if the
8352 * call is made directly since its followed by an exception check.
8354 direct_icall = FALSE;
8358 mono_save_token_info (cfg, image, token, cil_method);
8360 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8361 need_seq_point = TRUE;
8363 /* Don't support calls made using type arguments for now */
8365 if (cfg->gsharedvt) {
8366 if (mini_is_gsharedvt_signature (fsig))
8367 GSHAREDVT_FAILURE (*ip);
8371 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8372 g_assert_not_reached ();
8374 n = fsig->param_count + fsig->hasthis;
8376 if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
8380 g_assert (!mono_method_check_context_used (cmethod));
8384 //g_assert (!virtual_ || fsig->hasthis);
8388 if (cmethod && cmethod->klass->image == mono_defaults.corlib && !strcmp (cmethod->klass->name, "ThrowHelper"))
8389 cfg->cbb->out_of_line = TRUE;
8392 * We have the `constrained.' prefix opcode.
8394 if (constrained_class) {
8395 if (mini_is_gsharedvt_klass (constrained_class)) {
8396 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8397 /* The 'Own method' case below */
8398 } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
8399 /* 'The type parameter is instantiated as a reference type' case below. */
8401 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8402 CHECK_CFG_EXCEPTION;
8408 if (constrained_partial_call) {
8409 gboolean need_box = TRUE;
8412 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8413 * called method is not known at compile time either. The called method could end up being
8414 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8415 * to box the receiver.
8416 * A simple solution would be to box always and make a normal virtual call, but that would
8417 * be bad performance wise.
8419 if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
8421 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8426 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)) {
8427 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8428 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8429 ins->klass = constrained_class;
8430 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8431 CHECK_CFG_EXCEPTION;
8432 } else if (need_box) {
8434 MonoBasicBlock *is_ref_bb, *end_bb;
8435 MonoInst *nonbox_call;
8438 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8440 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8441 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8443 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8445 NEW_BBLOCK (cfg, is_ref_bb);
8446 NEW_BBLOCK (cfg, end_bb);
8448 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);
8449 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
8450 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8453 nonbox_call = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8455 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8458 MONO_START_BB (cfg, is_ref_bb);
8459 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8460 ins->klass = constrained_class;
8461 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8462 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8464 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8466 MONO_START_BB (cfg, end_bb);
8469 nonbox_call->dreg = ins->dreg;
8472 g_assert (mono_class_is_interface (cmethod->klass));
8473 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8474 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8477 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8479 * The type parameter is instantiated as a valuetype,
8480 * but that type doesn't override the method we're
8481 * calling, so we need to box `this'.
8483 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8484 ins->klass = constrained_class;
8485 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8486 CHECK_CFG_EXCEPTION;
8487 } else if (!constrained_class->valuetype) {
8488 int dreg = alloc_ireg_ref (cfg);
8491 * The type parameter is instantiated as a reference
8492 * type. We have a managed pointer on the stack, so
8493 * we need to dereference it here.
8495 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8496 ins->type = STACK_OBJ;
8499 if (cmethod->klass->valuetype) {
8502 /* Interface method */
8505 mono_class_setup_vtable (constrained_class);
8506 CHECK_TYPELOAD (constrained_class);
8507 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8509 TYPE_LOAD_ERROR (constrained_class);
8510 slot = mono_method_get_vtable_slot (cmethod);
8512 TYPE_LOAD_ERROR (cmethod->klass);
8513 cmethod = constrained_class->vtable [ioffset + slot];
8515 if (cmethod->klass == mono_defaults.enum_class) {
8516 /* Enum implements some interfaces, so treat this as the first case */
8517 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8518 ins->klass = constrained_class;
8519 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8520 CHECK_CFG_EXCEPTION;
8525 constrained_class = NULL;
8528 if (check_call_signature (cfg, fsig, sp))
8531 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8532 delegate_invoke = TRUE;
8534 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8535 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8536 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8544 * If the callee is a shared method, then its static cctor
8545 * might not get called after the call was patched.
8547 if (cfg->gshared && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
8548 emit_class_init (cfg, cmethod->klass);
8549 CHECK_TYPELOAD (cmethod->klass);
8552 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8555 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8557 context_used = mini_method_check_context_used (cfg, cmethod);
8559 if (context_used && mono_class_is_interface (cmethod->klass)) {
8560 /* Generic method interface
8561 calls are resolved via a
8562 helper function and don't
8564 if (!cmethod_context || !cmethod_context->method_inst)
8565 pass_imt_from_rgctx = TRUE;
8569 * If a shared method calls another
8570 * shared method then the caller must
8571 * have a generic sharing context
8572 * because the magic trampoline
8573 * requires it. FIXME: We shouldn't
8574 * have to force the vtable/mrgctx
8575 * variable here. Instead there
8576 * should be a flag in the cfg to
8577 * request a generic sharing context.
8580 ((cfg->method->flags & METHOD_ATTRIBUTE_STATIC) || cfg->method->klass->valuetype))
8581 mono_get_vtable_var (cfg);
8586 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8588 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8590 CHECK_TYPELOAD (cmethod->klass);
8591 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8596 g_assert (!vtable_arg);
8598 if (!cfg->compile_aot) {
8600 * emit_get_rgctx_method () calls mono_class_vtable () so check
8601 * for type load errors before.
8603 mono_class_setup_vtable (cmethod->klass);
8604 CHECK_TYPELOAD (cmethod->klass);
8607 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8609 /* !marshalbyref is needed to properly handle generic methods + remoting */
8610 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
8611 MONO_METHOD_IS_FINAL (cmethod)) &&
8612 !mono_class_is_marshalbyref (cmethod->klass)) {
8619 if (pass_imt_from_rgctx) {
8620 g_assert (!pass_vtable);
8622 imt_arg = emit_get_rgctx_method (cfg, context_used,
8623 cmethod, MONO_RGCTX_INFO_METHOD);
8627 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8629 /* Calling virtual generic methods */
8630 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
8631 !(MONO_METHOD_IS_FINAL (cmethod) &&
8632 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
8633 fsig->generic_param_count &&
8634 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
8636 MonoInst *this_temp, *this_arg_temp, *store;
8637 MonoInst *iargs [4];
8639 g_assert (fsig->is_inflated);
8641 /* Prevent inlining of methods that contain indirect calls */
8642 INLINE_FAILURE ("virtual generic call");
8644 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8645 GSHAREDVT_FAILURE (*ip);
8647 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
8648 g_assert (!imt_arg);
8650 g_assert (cmethod->is_inflated);
8651 imt_arg = emit_get_rgctx_method (cfg, context_used,
8652 cmethod, MONO_RGCTX_INFO_METHOD);
8653 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
8655 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
8656 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
8657 MONO_ADD_INS (cfg->cbb, store);
8659 /* FIXME: This should be a managed pointer */
8660 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8662 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
8663 iargs [1] = emit_get_rgctx_method (cfg, context_used,
8664 cmethod, MONO_RGCTX_INFO_METHOD);
8665 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
8666 addr = mono_emit_jit_icall (cfg,
8667 mono_helper_compile_generic_method, iargs);
8669 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
8671 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8678 * Implement a workaround for the inherent races involved in locking:
8684 * If a thread abort happens between the call to Monitor.Enter () and the start of the
8685 * try block, the Exit () won't be executed, see:
8686 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
8687 * To work around this, we extend such try blocks to include the last x bytes
8688 * of the Monitor.Enter () call.
8690 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8691 MonoBasicBlock *tbb;
8693 GET_BBLOCK (cfg, tbb, ip + 5);
8695 * Only extend try blocks with a finally, to avoid catching exceptions thrown
8696 * from Monitor.Enter like ArgumentNullException.
8698 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8699 /* Mark this bblock as needing to be extended */
8700 tbb->extend_try_block = TRUE;
8704 /* Conversion to a JIT intrinsic */
8705 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
8706 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8707 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8715 if ((cfg->opt & MONO_OPT_INLINE) &&
8716 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
8717 mono_method_check_inlining (cfg, cmethod)) {
8719 gboolean always = FALSE;
8721 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
8722 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
8723 /* Prevent inlining of methods that call wrappers */
8724 INLINE_FAILURE ("wrapper call");
8725 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
8729 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
8731 cfg->real_offset += 5;
8733 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8734 /* *sp is already set by inline_method */
8739 inline_costs += costs;
8745 /* Tail recursion elimination */
8746 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
8747 gboolean has_vtargs = FALSE;
8750 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
8751 INLINE_FAILURE ("tail call");
8753 /* keep it simple */
8754 for (i = fsig->param_count - 1; i >= 0; i--) {
8755 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
8760 if (need_seq_point) {
8761 emit_seq_point (cfg, method, ip, FALSE, TRUE);
8762 need_seq_point = FALSE;
8764 for (i = 0; i < n; ++i)
8765 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
8766 MONO_INST_NEW (cfg, ins, OP_BR);
8767 MONO_ADD_INS (cfg->cbb, ins);
8768 tblock = start_bblock->out_bb [0];
8769 link_bblock (cfg, cfg->cbb, tblock);
8770 ins->inst_target_bb = tblock;
8771 start_new_bblock = 1;
8773 /* skip the CEE_RET, too */
8774 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
8781 inline_costs += 10 * num_calls++;
8784 * Synchronized wrappers.
8785 * Its hard to determine where to replace a method with its synchronized
8786 * wrapper without causing an infinite recursion. The current solution is
8787 * to add the synchronized wrapper in the trampolines, and to
8788 * change the called method to a dummy wrapper, and resolve that wrapper
8789 * to the real method in mono_jit_compile_method ().
8791 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
8792 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
8793 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
8794 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
8798 * Making generic calls out of gsharedvt methods.
8799 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8800 * patching gshared method addresses into a gsharedvt method.
8802 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
8803 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
8804 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
8805 MonoRgctxInfoType info_type;
8808 //if (mono_class_is_interface (cmethod->klass))
8809 //GSHAREDVT_FAILURE (*ip);
8810 // disable for possible remoting calls
8811 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
8812 GSHAREDVT_FAILURE (*ip);
8813 if (fsig->generic_param_count) {
8814 /* virtual generic call */
8815 g_assert (!imt_arg);
8816 /* Same as the virtual generic case above */
8817 imt_arg = emit_get_rgctx_method (cfg, context_used,
8818 cmethod, MONO_RGCTX_INFO_METHOD);
8819 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
8821 } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
8822 /* This can happen when we call a fully instantiated iface method */
8823 imt_arg = emit_get_rgctx_method (cfg, context_used,
8824 cmethod, MONO_RGCTX_INFO_METHOD);
8829 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
8830 keep_this_alive = sp [0];
8832 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
8833 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
8835 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
8836 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
8838 if (cfg->llvm_only) {
8839 // FIXME: Avoid initializing vtable_arg
8840 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
8842 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
8847 /* Generic sharing */
8850 * Use this if the callee is gsharedvt sharable too, since
8851 * at runtime we might find an instantiation so the call cannot
8852 * be patched (the 'no_patch' code path in mini-trampolines.c).
8854 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
8855 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8856 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
8857 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
8858 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
8859 INLINE_FAILURE ("gshared");
8861 g_assert (cfg->gshared && cmethod);
8865 * We are compiling a call to a
8866 * generic method from shared code,
8867 * which means that we have to look up
8868 * the method in the rgctx and do an
8872 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8874 if (cfg->llvm_only) {
8875 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
8876 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
8878 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8879 // FIXME: Avoid initializing imt_arg/vtable_arg
8880 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
8882 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8883 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
8888 /* Direct calls to icalls */
8890 MonoMethod *wrapper;
8893 /* Inline the wrapper */
8894 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8896 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
8897 g_assert (costs > 0);
8898 cfg->real_offset += 5;
8900 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8901 /* *sp is already set by inline_method */
8906 inline_costs += costs;
8915 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
8916 MonoInst *val = sp [fsig->param_count];
8918 if (val->type == STACK_OBJ) {
8919 MonoInst *iargs [2];
8924 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
8927 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
8928 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
8929 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
8930 mini_emit_write_barrier (cfg, addr, val);
8931 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
8932 GSHAREDVT_FAILURE (*ip);
8933 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
8934 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
8936 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
8937 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
8938 if (!cmethod->klass->element_class->valuetype && !readonly)
8939 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
8940 CHECK_TYPELOAD (cmethod->klass);
8943 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
8946 g_assert_not_reached ();
8953 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
8957 /* Tail prefix / tail call optimization */
8959 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
8960 /* FIXME: runtime generic context pointer for jumps? */
8961 /* FIXME: handle this for generic sharing eventually */
8962 if ((ins_flag & MONO_INST_TAILCALL) &&
8963 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
8964 supported_tail_call = TRUE;
8966 if (supported_tail_call) {
8969 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
8970 INLINE_FAILURE ("tail call");
8972 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
8974 if (cfg->backend->have_op_tail_call) {
8975 /* Handle tail calls similarly to normal calls */
8978 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8980 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8981 call->tail_call = TRUE;
8982 call->method = cmethod;
8983 call->signature = mono_method_signature (cmethod);
8986 * We implement tail calls by storing the actual arguments into the
8987 * argument variables, then emitting a CEE_JMP.
8989 for (i = 0; i < n; ++i) {
8990 /* Prevent argument from being register allocated */
8991 arg_array [i]->flags |= MONO_INST_VOLATILE;
8992 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
8994 ins = (MonoInst*)call;
8995 ins->inst_p0 = cmethod;
8996 ins->inst_p1 = arg_array [0];
8997 MONO_ADD_INS (cfg->cbb, ins);
8998 link_bblock (cfg, cfg->cbb, end_bblock);
8999 start_new_bblock = 1;
9001 // FIXME: Eliminate unreachable epilogs
9004 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9005 * only reachable from this call.
9007 GET_BBLOCK (cfg, tblock, ip + 5);
9008 if (tblock == cfg->cbb || tblock->in_count == 0)
9017 * Virtual calls in llvm-only mode.
9019 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9020 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9025 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) && !(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
9026 INLINE_FAILURE ("call");
9027 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9028 imt_arg, vtable_arg);
9030 if (tail_call && !cfg->llvm_only) {
9031 link_bblock (cfg, cfg->cbb, end_bblock);
9032 start_new_bblock = 1;
9034 // FIXME: Eliminate unreachable epilogs
9037 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9038 * only reachable from this call.
9040 GET_BBLOCK (cfg, tblock, ip + 5);
9041 if (tblock == cfg->cbb || tblock->in_count == 0)
9048 /* End of call, INS should contain the result of the call, if any */
9050 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9053 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9058 if (keep_this_alive) {
9059 MonoInst *dummy_use;
9061 /* See mono_emit_method_call_full () */
9062 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9065 if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
9067 * Clang can convert these calls to tail calls which screw up the stack
9068 * walk. This happens even when the -fno-optimize-sibling-calls
9069 * option is passed to clang.
9070 * Work around this by emitting a dummy call.
9072 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
9075 CHECK_CFG_EXCEPTION;
9079 g_assert (*ip == CEE_RET);
9083 constrained_class = NULL;
9085 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9089 if (cfg->method != method) {
9090 /* return from inlined method */
9092 * If in_count == 0, that means the ret is unreachable due to
9093 * being preceeded by a throw. In that case, inline_method () will
9094 * handle setting the return value
9095 * (test case: test_0_inline_throw ()).
9097 if (return_var && cfg->cbb->in_count) {
9098 MonoType *ret_type = mono_method_signature (method)->ret;
9104 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9107 //g_assert (returnvar != -1);
9108 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9109 cfg->ret_var_set = TRUE;
9112 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9114 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
9118 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9120 if (seq_points && !sym_seq_points) {
9122 * Place a seq point here too even through the IL stack is not
9123 * empty, so a step over on
9126 * will work correctly.
9128 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9129 MONO_ADD_INS (cfg->cbb, ins);
9132 g_assert (!return_var);
9136 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9139 emit_setret (cfg, *sp);
9142 if (sp != stack_start)
9144 MONO_INST_NEW (cfg, ins, OP_BR);
9146 ins->inst_target_bb = end_bblock;
9147 MONO_ADD_INS (cfg->cbb, ins);
9148 link_bblock (cfg, cfg->cbb, end_bblock);
9149 start_new_bblock = 1;
9153 MONO_INST_NEW (cfg, ins, OP_BR);
9155 target = ip + 1 + (signed char)(*ip);
9157 GET_BBLOCK (cfg, tblock, target);
9158 link_bblock (cfg, cfg->cbb, tblock);
9159 ins->inst_target_bb = tblock;
9160 if (sp != stack_start) {
9161 handle_stack_args (cfg, stack_start, sp - stack_start);
9163 CHECK_UNVERIFIABLE (cfg);
9165 MONO_ADD_INS (cfg->cbb, ins);
9166 start_new_bblock = 1;
9167 inline_costs += BRANCH_COST;
9181 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9183 target = ip + 1 + *(signed char*)ip;
9189 inline_costs += BRANCH_COST;
9193 MONO_INST_NEW (cfg, ins, OP_BR);
9196 target = ip + 4 + (gint32)read32(ip);
9198 GET_BBLOCK (cfg, tblock, target);
9199 link_bblock (cfg, cfg->cbb, tblock);
9200 ins->inst_target_bb = tblock;
9201 if (sp != stack_start) {
9202 handle_stack_args (cfg, stack_start, sp - stack_start);
9204 CHECK_UNVERIFIABLE (cfg);
9207 MONO_ADD_INS (cfg->cbb, ins);
9209 start_new_bblock = 1;
9210 inline_costs += BRANCH_COST;
9217 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9218 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9219 guint32 opsize = is_short ? 1 : 4;
9221 CHECK_OPSIZE (opsize);
9223 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9226 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9231 GET_BBLOCK (cfg, tblock, target);
9232 link_bblock (cfg, cfg->cbb, tblock);
9233 GET_BBLOCK (cfg, tblock, ip);
9234 link_bblock (cfg, cfg->cbb, tblock);
9236 if (sp != stack_start) {
9237 handle_stack_args (cfg, stack_start, sp - stack_start);
9238 CHECK_UNVERIFIABLE (cfg);
9241 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9242 cmp->sreg1 = sp [0]->dreg;
9243 type_from_op (cfg, cmp, sp [0], NULL);
9246 #if SIZEOF_REGISTER == 4
9247 if (cmp->opcode == OP_LCOMPARE_IMM) {
9248 /* Convert it to OP_LCOMPARE */
9249 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9250 ins->type = STACK_I8;
9251 ins->dreg = alloc_dreg (cfg, STACK_I8);
9253 MONO_ADD_INS (cfg->cbb, ins);
9254 cmp->opcode = OP_LCOMPARE;
9255 cmp->sreg2 = ins->dreg;
9258 MONO_ADD_INS (cfg->cbb, cmp);
9260 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9261 type_from_op (cfg, ins, sp [0], NULL);
9262 MONO_ADD_INS (cfg->cbb, ins);
9263 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9264 GET_BBLOCK (cfg, tblock, target);
9265 ins->inst_true_bb = tblock;
9266 GET_BBLOCK (cfg, tblock, ip);
9267 ins->inst_false_bb = tblock;
9268 start_new_bblock = 2;
9271 inline_costs += BRANCH_COST;
9286 MONO_INST_NEW (cfg, ins, *ip);
9288 target = ip + 4 + (gint32)read32(ip);
9294 inline_costs += BRANCH_COST;
9298 MonoBasicBlock **targets;
9299 MonoBasicBlock *default_bblock;
9300 MonoJumpInfoBBTable *table;
9301 int offset_reg = alloc_preg (cfg);
9302 int target_reg = alloc_preg (cfg);
9303 int table_reg = alloc_preg (cfg);
9304 int sum_reg = alloc_preg (cfg);
9305 gboolean use_op_switch;
9309 n = read32 (ip + 1);
9312 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
9316 CHECK_OPSIZE (n * sizeof (guint32));
9317 target = ip + n * sizeof (guint32);
9319 GET_BBLOCK (cfg, default_bblock, target);
9320 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9322 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9323 for (i = 0; i < n; ++i) {
9324 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9325 targets [i] = tblock;
9326 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9330 if (sp != stack_start) {
9332 * Link the current bb with the targets as well, so handle_stack_args
9333 * will set their in_stack correctly.
9335 link_bblock (cfg, cfg->cbb, default_bblock);
9336 for (i = 0; i < n; ++i)
9337 link_bblock (cfg, cfg->cbb, targets [i]);
9339 handle_stack_args (cfg, stack_start, sp - stack_start);
9341 CHECK_UNVERIFIABLE (cfg);
9343 /* Undo the links */
9344 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
9345 for (i = 0; i < n; ++i)
9346 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
9349 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9350 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9352 for (i = 0; i < n; ++i)
9353 link_bblock (cfg, cfg->cbb, targets [i]);
9355 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9356 table->table = targets;
9357 table->table_size = n;
9359 use_op_switch = FALSE;
9361 /* ARM implements SWITCH statements differently */
9362 /* FIXME: Make it use the generic implementation */
9363 if (!cfg->compile_aot)
9364 use_op_switch = TRUE;
9367 if (COMPILE_LLVM (cfg))
9368 use_op_switch = TRUE;
9370 cfg->cbb->has_jump_table = 1;
9372 if (use_op_switch) {
9373 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9374 ins->sreg1 = src1->dreg;
9375 ins->inst_p0 = table;
9376 ins->inst_many_bb = targets;
9377 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
9378 MONO_ADD_INS (cfg->cbb, ins);
9380 if (sizeof (gpointer) == 8)
9381 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9383 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9385 #if SIZEOF_REGISTER == 8
9386 /* The upper word might not be zero, and we add it to a 64 bit address later */
9387 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9390 if (cfg->compile_aot) {
9391 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9393 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9394 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9395 ins->inst_p0 = table;
9396 ins->dreg = table_reg;
9397 MONO_ADD_INS (cfg->cbb, ins);
9400 /* FIXME: Use load_memindex */
9401 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9402 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9403 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9405 start_new_bblock = 1;
9406 inline_costs += (BRANCH_COST * 2);
9423 ins = mini_emit_memory_load (cfg, &ldind_to_type (*ip)->byval_arg, sp [0], 0, ins_flag);
9439 if (ins_flag & MONO_INST_VOLATILE) {
9440 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9441 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9444 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9445 ins->flags |= ins_flag;
9448 MONO_ADD_INS (cfg->cbb, ins);
9450 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
9451 mini_emit_write_barrier (cfg, sp [0], sp [1]);
9460 MONO_INST_NEW (cfg, ins, (*ip));
9462 ins->sreg1 = sp [0]->dreg;
9463 ins->sreg2 = sp [1]->dreg;
9464 type_from_op (cfg, ins, sp [0], sp [1]);
9466 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9468 /* Use the immediate opcodes if possible */
9469 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9470 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9471 if (imm_opcode != -1) {
9472 ins->opcode = imm_opcode;
9473 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9476 NULLIFY_INS (sp [1]);
9480 MONO_ADD_INS ((cfg)->cbb, (ins));
9482 *sp++ = mono_decompose_opcode (cfg, ins);
9499 MONO_INST_NEW (cfg, ins, (*ip));
9501 ins->sreg1 = sp [0]->dreg;
9502 ins->sreg2 = sp [1]->dreg;
9503 type_from_op (cfg, ins, sp [0], sp [1]);
9505 add_widen_op (cfg, ins, &sp [0], &sp [1]);
9506 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9508 /* FIXME: Pass opcode to is_inst_imm */
9510 /* Use the immediate opcodes if possible */
9511 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)) {
9512 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9513 if (imm_opcode != -1) {
9514 ins->opcode = imm_opcode;
9515 if (sp [1]->opcode == OP_I8CONST) {
9516 #if SIZEOF_REGISTER == 8
9517 ins->inst_imm = sp [1]->inst_l;
9519 ins->inst_ls_word = sp [1]->inst_ls_word;
9520 ins->inst_ms_word = sp [1]->inst_ms_word;
9524 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9527 /* Might be followed by an instruction added by add_widen_op */
9528 if (sp [1]->next == NULL)
9529 NULLIFY_INS (sp [1]);
9532 MONO_ADD_INS ((cfg)->cbb, (ins));
9534 *sp++ = mono_decompose_opcode (cfg, ins);
9547 case CEE_CONV_OVF_I8:
9548 case CEE_CONV_OVF_U8:
9552 /* Special case this earlier so we have long constants in the IR */
9553 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
9554 int data = sp [-1]->inst_c0;
9555 sp [-1]->opcode = OP_I8CONST;
9556 sp [-1]->type = STACK_I8;
9557 #if SIZEOF_REGISTER == 8
9558 if ((*ip) == CEE_CONV_U8)
9559 sp [-1]->inst_c0 = (guint32)data;
9561 sp [-1]->inst_c0 = data;
9563 sp [-1]->inst_ls_word = data;
9564 if ((*ip) == CEE_CONV_U8)
9565 sp [-1]->inst_ms_word = 0;
9567 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
9569 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
9576 case CEE_CONV_OVF_I4:
9577 case CEE_CONV_OVF_I1:
9578 case CEE_CONV_OVF_I2:
9579 case CEE_CONV_OVF_I:
9580 case CEE_CONV_OVF_U:
9583 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9584 ADD_UNOP (CEE_CONV_OVF_I8);
9591 case CEE_CONV_OVF_U1:
9592 case CEE_CONV_OVF_U2:
9593 case CEE_CONV_OVF_U4:
9596 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9597 ADD_UNOP (CEE_CONV_OVF_U8);
9604 case CEE_CONV_OVF_I1_UN:
9605 case CEE_CONV_OVF_I2_UN:
9606 case CEE_CONV_OVF_I4_UN:
9607 case CEE_CONV_OVF_I8_UN:
9608 case CEE_CONV_OVF_U1_UN:
9609 case CEE_CONV_OVF_U2_UN:
9610 case CEE_CONV_OVF_U4_UN:
9611 case CEE_CONV_OVF_U8_UN:
9612 case CEE_CONV_OVF_I_UN:
9613 case CEE_CONV_OVF_U_UN:
9620 CHECK_CFG_EXCEPTION;
9624 case CEE_ADD_OVF_UN:
9626 case CEE_MUL_OVF_UN:
9628 case CEE_SUB_OVF_UN:
9634 GSHAREDVT_FAILURE (*ip);
9637 token = read32 (ip + 1);
9638 klass = mini_get_class (method, token, generic_context);
9639 CHECK_TYPELOAD (klass);
9641 mini_emit_memory_copy (cfg, sp [0], sp [1], klass, FALSE, ins_flag);
9652 token = read32 (ip + 1);
9653 klass = mini_get_class (method, token, generic_context);
9654 CHECK_TYPELOAD (klass);
9656 /* Optimize the common ldobj+stloc combination */
9666 loc_index = ip [5] - CEE_STLOC_0;
9673 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
9674 CHECK_LOCAL (loc_index);
9676 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
9677 ins->dreg = cfg->locals [loc_index]->dreg;
9678 ins->flags |= ins_flag;
9681 if (ins_flag & MONO_INST_VOLATILE) {
9682 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9683 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9689 /* Optimize the ldobj+stobj combination */
9690 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token)) {
9695 mini_emit_memory_copy (cfg, sp [0], sp [1], klass, FALSE, ins_flag);
9702 ins = mini_emit_memory_load (cfg, &klass->byval_arg, sp [0], 0, ins_flag);
9711 CHECK_STACK_OVF (1);
9713 n = read32 (ip + 1);
9715 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
9716 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
9717 ins->type = STACK_OBJ;
9720 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
9721 MonoInst *iargs [1];
9722 char *str = (char *)mono_method_get_wrapper_data (method, n);
9724 if (cfg->compile_aot)
9725 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
9727 EMIT_NEW_PCONST (cfg, iargs [0], str);
9728 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
9730 if (cfg->opt & MONO_OPT_SHARED) {
9731 MonoInst *iargs [3];
9733 if (cfg->compile_aot) {
9734 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
9736 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
9737 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
9738 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
9739 *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
9740 mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
9743 if (cfg->cbb->out_of_line) {
9744 MonoInst *iargs [2];
9746 if (image == mono_defaults.corlib) {
9748 * Avoid relocations in AOT and save some space by using a
9749 * version of helper_ldstr specialized to mscorlib.
9751 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
9752 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
9754 /* Avoid creating the string object */
9755 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
9756 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
9757 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
9761 if (cfg->compile_aot) {
9762 NEW_LDSTRCONST (cfg, ins, image, n);
9764 MONO_ADD_INS (cfg->cbb, ins);
9767 NEW_PCONST (cfg, ins, NULL);
9768 ins->type = STACK_OBJ;
9769 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
9773 OUT_OF_MEMORY_FAILURE;
9776 MONO_ADD_INS (cfg->cbb, ins);
9785 MonoInst *iargs [2];
9786 MonoMethodSignature *fsig;
9789 MonoInst *vtable_arg = NULL;
9792 token = read32 (ip + 1);
9793 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9796 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9799 mono_save_token_info (cfg, image, token, cmethod);
9801 if (!mono_class_init (cmethod->klass))
9802 TYPE_LOAD_ERROR (cmethod->klass);
9804 context_used = mini_method_check_context_used (cfg, cmethod);
9806 if (!dont_verify && !cfg->skip_visibility) {
9807 MonoMethod *cil_method = cmethod;
9808 MonoMethod *target_method = cil_method;
9810 if (method->is_inflated) {
9811 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9815 if (!mono_method_can_access_method (method_definition, target_method) &&
9816 !mono_method_can_access_method (method, cil_method))
9817 emit_method_access_failure (cfg, method, cil_method);
9820 if (mono_security_core_clr_enabled ())
9821 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
9823 if (cfg->gshared && cmethod && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
9824 emit_class_init (cfg, cmethod->klass);
9825 CHECK_TYPELOAD (cmethod->klass);
9829 if (cfg->gsharedvt) {
9830 if (mini_is_gsharedvt_variable_signature (sig))
9831 GSHAREDVT_FAILURE (*ip);
9835 n = fsig->param_count;
9839 * Generate smaller code for the common newobj <exception> instruction in
9840 * argument checking code.
9842 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
9843 is_exception_class (cmethod->klass) && n <= 2 &&
9844 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
9845 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
9846 MonoInst *iargs [3];
9850 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
9853 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
9857 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
9862 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
9865 g_assert_not_reached ();
9873 /* move the args to allow room for 'this' in the first position */
9879 /* check_call_signature () requires sp[0] to be set */
9880 this_ins.type = STACK_OBJ;
9882 if (check_call_signature (cfg, fsig, sp))
9887 if (mini_class_is_system_array (cmethod->klass)) {
9888 *sp = emit_get_rgctx_method (cfg, context_used,
9889 cmethod, MONO_RGCTX_INFO_METHOD);
9891 /* Avoid varargs in the common case */
9892 if (fsig->param_count == 1)
9893 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
9894 else if (fsig->param_count == 2)
9895 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
9896 else if (fsig->param_count == 3)
9897 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
9898 else if (fsig->param_count == 4)
9899 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
9901 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
9902 } else if (cmethod->string_ctor) {
9903 g_assert (!context_used);
9904 g_assert (!vtable_arg);
9905 /* we simply pass a null pointer */
9906 EMIT_NEW_PCONST (cfg, *sp, NULL);
9907 /* now call the string ctor */
9908 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
9910 if (cmethod->klass->valuetype) {
9911 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
9912 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
9913 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
9918 * The code generated by mini_emit_virtual_call () expects
9919 * iargs [0] to be a boxed instance, but luckily the vcall
9920 * will be transformed into a normal call there.
9922 } else if (context_used) {
9923 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
9926 MonoVTable *vtable = NULL;
9928 if (!cfg->compile_aot)
9929 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9930 CHECK_TYPELOAD (cmethod->klass);
9933 * TypeInitializationExceptions thrown from the mono_runtime_class_init
9934 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
9935 * As a workaround, we call class cctors before allocating objects.
9937 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
9938 emit_class_init (cfg, cmethod->klass);
9939 if (cfg->verbose_level > 2)
9940 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
9941 class_inits = g_slist_prepend (class_inits, cmethod->klass);
9944 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
9947 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
9950 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
9952 /* Now call the actual ctor */
9953 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
9954 CHECK_CFG_EXCEPTION;
9957 if (alloc == NULL) {
9959 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
9960 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
9968 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
9969 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9977 token = read32 (ip + 1);
9978 klass = mini_get_class (method, token, generic_context);
9979 CHECK_TYPELOAD (klass);
9980 if (sp [0]->type != STACK_OBJ)
9983 MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
9984 ins->dreg = alloc_preg (cfg);
9985 ins->sreg1 = (*sp)->dreg;
9987 ins->type = STACK_OBJ;
9988 MONO_ADD_INS (cfg->cbb, ins);
9990 CHECK_CFG_EXCEPTION;
9994 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
9997 case CEE_UNBOX_ANY: {
9998 MonoInst *res, *addr;
10003 token = read32 (ip + 1);
10004 klass = mini_get_class (method, token, generic_context);
10005 CHECK_TYPELOAD (klass);
10007 mono_save_token_info (cfg, image, token, klass);
10009 context_used = mini_class_check_context_used (cfg, klass);
10011 if (mini_is_gsharedvt_klass (klass)) {
10012 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10014 } else if (generic_class_is_reference_type (cfg, klass)) {
10015 if (MONO_INS_IS_PCONST_NULL (*sp)) {
10016 EMIT_NEW_PCONST (cfg, res, NULL);
10017 res->type = STACK_OBJ;
10019 MONO_INST_NEW (cfg, res, OP_CASTCLASS);
10020 res->dreg = alloc_preg (cfg);
10021 res->sreg1 = (*sp)->dreg;
10022 res->klass = klass;
10023 res->type = STACK_OBJ;
10024 MONO_ADD_INS (cfg->cbb, res);
10025 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10027 } else if (mono_class_is_nullable (klass)) {
10028 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10030 addr = handle_unbox (cfg, klass, sp, context_used);
10032 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10043 MonoClass *enum_class;
10044 MonoMethod *has_flag;
10050 token = read32 (ip + 1);
10051 klass = mini_get_class (method, token, generic_context);
10052 CHECK_TYPELOAD (klass);
10054 mono_save_token_info (cfg, image, token, klass);
10056 context_used = mini_class_check_context_used (cfg, klass);
10058 if (generic_class_is_reference_type (cfg, klass)) {
10064 if (klass == mono_defaults.void_class)
10066 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10068 /* frequent check in generic code: box (struct), brtrue */
10073 * <push int/long ptr>
10076 * constrained. MyFlags
10077 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10079 * If we find this sequence and the operand types on box and constrained
10080 * are equal, we can emit a specialized instruction sequence instead of
10081 * the very slow HasFlag () call.
10083 if ((cfg->opt & MONO_OPT_INTRINS) &&
10084 /* Cheap checks first. */
10085 ip + 5 + 6 + 5 < end &&
10086 ip [5] == CEE_PREFIX1 &&
10087 ip [6] == CEE_CONSTRAINED_ &&
10088 ip [11] == CEE_CALLVIRT &&
10089 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10090 mono_class_is_enum (klass) &&
10091 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10092 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10093 has_flag->klass == mono_defaults.enum_class &&
10094 !strcmp (has_flag->name, "HasFlag") &&
10095 has_flag->signature->hasthis &&
10096 has_flag->signature->param_count == 1) {
10097 CHECK_TYPELOAD (enum_class);
10099 if (enum_class == klass) {
10100 MonoInst *enum_this, *enum_flag;
10105 enum_this = sp [0];
10106 enum_flag = sp [1];
10108 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10113 // FIXME: LLVM can't handle the inconsistent bb linking
10114 if (!mono_class_is_nullable (klass) &&
10115 !mini_is_gsharedvt_klass (klass) &&
10116 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10117 (ip [5] == CEE_BRTRUE ||
10118 ip [5] == CEE_BRTRUE_S ||
10119 ip [5] == CEE_BRFALSE ||
10120 ip [5] == CEE_BRFALSE_S)) {
10121 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10123 MonoBasicBlock *true_bb, *false_bb;
10127 if (cfg->verbose_level > 3) {
10128 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10129 printf ("<box+brtrue opt>\n");
10134 case CEE_BRFALSE_S:
10137 target = ip + 1 + (signed char)(*ip);
10144 target = ip + 4 + (gint)(read32 (ip));
10148 g_assert_not_reached ();
10152 * We need to link both bblocks, since it is needed for handling stack
10153 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10154 * Branching to only one of them would lead to inconsistencies, so
10155 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10157 GET_BBLOCK (cfg, true_bb, target);
10158 GET_BBLOCK (cfg, false_bb, ip);
10160 mono_link_bblock (cfg, cfg->cbb, true_bb);
10161 mono_link_bblock (cfg, cfg->cbb, false_bb);
10163 if (sp != stack_start) {
10164 handle_stack_args (cfg, stack_start, sp - stack_start);
10166 CHECK_UNVERIFIABLE (cfg);
10169 if (COMPILE_LLVM (cfg)) {
10170 dreg = alloc_ireg (cfg);
10171 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10172 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10174 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10176 /* The JIT can't eliminate the iconst+compare */
10177 MONO_INST_NEW (cfg, ins, OP_BR);
10178 ins->inst_target_bb = is_true ? true_bb : false_bb;
10179 MONO_ADD_INS (cfg->cbb, ins);
10182 start_new_bblock = 1;
10186 *sp++ = handle_box (cfg, val, klass, context_used);
10188 CHECK_CFG_EXCEPTION;
10197 token = read32 (ip + 1);
10198 klass = mini_get_class (method, token, generic_context);
10199 CHECK_TYPELOAD (klass);
10201 mono_save_token_info (cfg, image, token, klass);
10203 context_used = mini_class_check_context_used (cfg, klass);
10205 if (mono_class_is_nullable (klass)) {
10208 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10209 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10213 ins = handle_unbox (cfg, klass, sp, context_used);
10226 MonoClassField *field;
10227 #ifndef DISABLE_REMOTING
10231 gboolean is_instance;
10233 gpointer addr = NULL;
10234 gboolean is_special_static;
10236 MonoInst *store_val = NULL;
10237 MonoInst *thread_ins;
10240 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10242 if (op == CEE_STFLD) {
10245 store_val = sp [1];
10250 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10252 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10255 if (op == CEE_STSFLD) {
10258 store_val = sp [0];
10263 token = read32 (ip + 1);
10264 if (method->wrapper_type != MONO_WRAPPER_NONE) {
10265 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
10266 klass = field->parent;
10269 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10272 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10273 FIELD_ACCESS_FAILURE (method, field);
10274 mono_class_init (klass);
10276 /* if the class is Critical then transparent code cannot access it's fields */
10277 if (!is_instance && mono_security_core_clr_enabled ())
10278 ensure_method_is_allowed_to_access_field (cfg, method, field);
10280 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10281 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
10282 if (mono_security_core_clr_enabled ())
10283 ensure_method_is_allowed_to_access_field (cfg, method, field);
10286 ftype = mono_field_get_type (field);
10289 * LDFLD etc. is usable on static fields as well, so convert those cases to
10292 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10304 g_assert_not_reached ();
10306 is_instance = FALSE;
10309 context_used = mini_class_check_context_used (cfg, klass);
10311 /* INSTANCE CASE */
10313 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10314 if (op == CEE_STFLD) {
10315 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10317 #ifndef DISABLE_REMOTING
10318 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10319 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
10320 MonoInst *iargs [5];
10322 GSHAREDVT_FAILURE (op);
10324 iargs [0] = sp [0];
10325 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10326 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10327 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
10329 iargs [4] = sp [1];
10331 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10332 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
10333 iargs, ip, cfg->real_offset, TRUE);
10334 CHECK_CFG_EXCEPTION;
10335 g_assert (costs > 0);
10337 cfg->real_offset += 5;
10339 inline_costs += costs;
10341 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10346 MonoInst *store, *wbarrier_ptr_ins = NULL;
10348 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10350 if (ins_flag & MONO_INST_VOLATILE) {
10351 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10352 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10355 if (mini_is_gsharedvt_klass (klass)) {
10356 MonoInst *offset_ins;
10358 context_used = mini_class_check_context_used (cfg, klass);
10360 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10361 /* The value is offset by 1 */
10362 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10363 dreg = alloc_ireg_mp (cfg);
10364 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10365 wbarrier_ptr_ins = ins;
10366 /* The decomposition will call mini_emit_memory_copy () which will emit a wbarrier if needed */
10367 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10369 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10371 if (sp [0]->opcode != OP_LDADDR)
10372 store->flags |= MONO_INST_FAULT;
10374 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
10375 if (mini_is_gsharedvt_klass (klass)) {
10376 g_assert (wbarrier_ptr_ins);
10377 mini_emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
10379 /* insert call to write barrier */
10383 dreg = alloc_ireg_mp (cfg);
10384 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10385 mini_emit_write_barrier (cfg, ptr, sp [1]);
10389 store->flags |= ins_flag;
10396 #ifndef DISABLE_REMOTING
10397 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10398 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
10399 MonoInst *iargs [4];
10401 GSHAREDVT_FAILURE (op);
10403 iargs [0] = sp [0];
10404 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10405 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10406 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10407 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10408 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
10409 iargs, ip, cfg->real_offset, TRUE);
10410 CHECK_CFG_EXCEPTION;
10411 g_assert (costs > 0);
10413 cfg->real_offset += 5;
10417 inline_costs += costs;
10419 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10425 if (sp [0]->type == STACK_VTYPE) {
10428 /* Have to compute the address of the variable */
10430 var = get_vreg_to_inst (cfg, sp [0]->dreg);
10432 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10434 g_assert (var->klass == klass);
10436 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10440 if (op == CEE_LDFLDA) {
10441 if (sp [0]->type == STACK_OBJ) {
10442 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10443 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10446 dreg = alloc_ireg_mp (cfg);
10448 if (mini_is_gsharedvt_klass (klass)) {
10449 MonoInst *offset_ins;
10451 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10452 /* The value is offset by 1 */
10453 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10454 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10456 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10458 ins->klass = mono_class_from_mono_type (field->type);
10459 ins->type = STACK_MP;
10464 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10466 if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
10467 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
10476 MonoInst *field_add_inst = sp [0];
10477 if (mini_is_gsharedvt_klass (klass)) {
10478 MonoInst *offset_ins;
10480 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10481 /* The value is offset by 1 */
10482 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10483 EMIT_NEW_BIALU (cfg, field_add_inst, OP_PADD, alloc_ireg_mp (cfg), sp [0]->dreg, offset_ins->dreg);
10487 load = mini_emit_memory_load (cfg, field->type, field_add_inst, foffset, ins_flag);
10489 if (sp [0]->opcode != OP_LDADDR)
10490 load->flags |= MONO_INST_FAULT;
10502 context_used = mini_class_check_context_used (cfg, klass);
10504 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
10505 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
10509 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10510 * to be called here.
10512 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10513 mono_class_vtable (cfg->domain, klass);
10514 CHECK_TYPELOAD (klass);
10516 mono_domain_lock (cfg->domain);
10517 if (cfg->domain->special_static_fields)
10518 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10519 mono_domain_unlock (cfg->domain);
10521 is_special_static = mono_class_field_is_special_static (field);
10523 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10524 thread_ins = mono_create_tls_get (cfg, TLS_KEY_THREAD);
10528 /* Generate IR to compute the field address */
10529 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
10531 * Fast access to TLS data
10532 * Inline version of get_thread_static_data () in
10536 int idx, static_data_reg, array_reg, dreg;
10538 if (context_used && cfg->gsharedvt && mini_is_gsharedvt_klass (klass))
10539 GSHAREDVT_FAILURE (op);
10541 static_data_reg = alloc_ireg (cfg);
10542 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
10544 if (cfg->compile_aot) {
10545 int offset_reg, offset2_reg, idx_reg;
10547 /* For TLS variables, this will return the TLS offset */
10548 EMIT_NEW_SFLDACONST (cfg, ins, field);
10549 offset_reg = ins->dreg;
10550 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
10551 idx_reg = alloc_ireg (cfg);
10552 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
10553 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
10554 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
10555 array_reg = alloc_ireg (cfg);
10556 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
10557 offset2_reg = alloc_ireg (cfg);
10558 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
10559 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
10560 dreg = alloc_ireg (cfg);
10561 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
10563 offset = (gsize)addr & 0x7fffffff;
10564 idx = offset & 0x3f;
10566 array_reg = alloc_ireg (cfg);
10567 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
10568 dreg = alloc_ireg (cfg);
10569 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
10571 } else if ((cfg->opt & MONO_OPT_SHARED) ||
10572 (cfg->compile_aot && is_special_static) ||
10573 (context_used && is_special_static)) {
10574 MonoInst *iargs [2];
10576 g_assert (field->parent);
10577 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10578 if (context_used) {
10579 iargs [1] = emit_get_rgctx_field (cfg, context_used,
10580 field, MONO_RGCTX_INFO_CLASS_FIELD);
10582 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10584 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10585 } else if (context_used) {
10586 MonoInst *static_data;
10589 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
10590 method->klass->name_space, method->klass->name, method->name,
10591 depth, field->offset);
10594 if (mono_class_needs_cctor_run (klass, method))
10595 emit_class_init (cfg, klass);
10598 * The pointer we're computing here is
10600 * super_info.static_data + field->offset
10602 static_data = mini_emit_get_rgctx_klass (cfg, context_used,
10603 klass, MONO_RGCTX_INFO_STATIC_DATA);
10605 if (mini_is_gsharedvt_klass (klass)) {
10606 MonoInst *offset_ins;
10608 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10609 /* The value is offset by 1 */
10610 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10611 dreg = alloc_ireg_mp (cfg);
10612 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
10613 } else if (field->offset == 0) {
10616 int addr_reg = mono_alloc_preg (cfg);
10617 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
10619 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
10620 MonoInst *iargs [2];
10622 g_assert (field->parent);
10623 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10624 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10625 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10627 MonoVTable *vtable = NULL;
10629 if (!cfg->compile_aot)
10630 vtable = mono_class_vtable (cfg->domain, klass);
10631 CHECK_TYPELOAD (klass);
10634 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
10635 if (!(g_slist_find (class_inits, klass))) {
10636 emit_class_init (cfg, klass);
10637 if (cfg->verbose_level > 2)
10638 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
10639 class_inits = g_slist_prepend (class_inits, klass);
10642 if (cfg->run_cctors) {
10643 /* This makes so that inline cannot trigger */
10644 /* .cctors: too many apps depend on them */
10645 /* running with a specific order... */
10647 if (! vtable->initialized)
10648 INLINE_FAILURE ("class init");
10649 if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
10650 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
10651 goto exception_exit;
10655 if (cfg->compile_aot)
10656 EMIT_NEW_SFLDACONST (cfg, ins, field);
10659 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10661 EMIT_NEW_PCONST (cfg, ins, addr);
10664 MonoInst *iargs [1];
10665 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
10666 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
10670 /* Generate IR to do the actual load/store operation */
10672 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
10673 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10674 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10677 if (op == CEE_LDSFLDA) {
10678 ins->klass = mono_class_from_mono_type (ftype);
10679 ins->type = STACK_PTR;
10681 } else if (op == CEE_STSFLD) {
10684 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
10685 store->flags |= ins_flag;
10687 gboolean is_const = FALSE;
10688 MonoVTable *vtable = NULL;
10689 gpointer addr = NULL;
10691 if (!context_used) {
10692 vtable = mono_class_vtable (cfg->domain, klass);
10693 CHECK_TYPELOAD (klass);
10695 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
10696 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
10697 int ro_type = ftype->type;
10699 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10700 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
10701 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
10704 GSHAREDVT_FAILURE (op);
10706 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
10709 case MONO_TYPE_BOOLEAN:
10711 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
10715 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
10718 case MONO_TYPE_CHAR:
10720 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
10724 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
10729 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
10733 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
10738 case MONO_TYPE_PTR:
10739 case MONO_TYPE_FNPTR:
10740 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
10741 type_to_eval_stack_type ((cfg), field->type, *sp);
10744 case MONO_TYPE_STRING:
10745 case MONO_TYPE_OBJECT:
10746 case MONO_TYPE_CLASS:
10747 case MONO_TYPE_SZARRAY:
10748 case MONO_TYPE_ARRAY:
10749 if (!mono_gc_is_moving ()) {
10750 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
10751 type_to_eval_stack_type ((cfg), field->type, *sp);
10759 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
10764 case MONO_TYPE_VALUETYPE:
10774 CHECK_STACK_OVF (1);
10776 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
10777 load->flags |= ins_flag;
10783 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
10784 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10785 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10796 token = read32 (ip + 1);
10797 klass = mini_get_class (method, token, generic_context);
10798 CHECK_TYPELOAD (klass);
10800 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
10801 mini_emit_memory_store (cfg, &klass->byval_arg, sp [0], sp [1], ins_flag);
10812 const char *data_ptr;
10814 guint32 field_token;
10820 token = read32 (ip + 1);
10822 klass = mini_get_class (method, token, generic_context);
10823 CHECK_TYPELOAD (klass);
10824 if (klass->byval_arg.type == MONO_TYPE_VOID)
10827 context_used = mini_class_check_context_used (cfg, klass);
10829 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
10830 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
10831 ins->sreg1 = sp [0]->dreg;
10832 ins->type = STACK_I4;
10833 ins->dreg = alloc_ireg (cfg);
10834 MONO_ADD_INS (cfg->cbb, ins);
10835 *sp = mono_decompose_opcode (cfg, ins);
10838 if (context_used) {
10839 MonoInst *args [3];
10840 MonoClass *array_class = mono_array_class_get (klass, 1);
10841 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
10843 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
10846 args [0] = mini_emit_get_rgctx_klass (cfg, context_used,
10847 array_class, MONO_RGCTX_INFO_VTABLE);
10852 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
10854 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
10856 if (cfg->opt & MONO_OPT_SHARED) {
10857 /* Decompose now to avoid problems with references to the domainvar */
10858 MonoInst *iargs [3];
10860 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10861 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10862 iargs [2] = sp [0];
10864 ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
10866 /* Decompose later since it is needed by abcrem */
10867 MonoClass *array_type = mono_array_class_get (klass, 1);
10868 mono_class_vtable (cfg->domain, array_type);
10869 CHECK_TYPELOAD (array_type);
10871 MONO_INST_NEW (cfg, ins, OP_NEWARR);
10872 ins->dreg = alloc_ireg_ref (cfg);
10873 ins->sreg1 = sp [0]->dreg;
10874 ins->inst_newa_class = klass;
10875 ins->type = STACK_OBJ;
10876 ins->klass = array_type;
10877 MONO_ADD_INS (cfg->cbb, ins);
10878 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
10879 cfg->cbb->has_array_access = TRUE;
10881 /* Needed so mono_emit_load_get_addr () gets called */
10882 mono_get_got_var (cfg);
10892 * we inline/optimize the initialization sequence if possible.
10893 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
10894 * for small sizes open code the memcpy
10895 * ensure the rva field is big enough
10897 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))) {
10898 MonoMethod *memcpy_method = mini_get_memcpy_method ();
10899 MonoInst *iargs [3];
10900 int add_reg = alloc_ireg_mp (cfg);
10902 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
10903 if (cfg->compile_aot) {
10904 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
10906 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
10908 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
10909 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
10918 if (sp [0]->type != STACK_OBJ)
10921 MONO_INST_NEW (cfg, ins, OP_LDLEN);
10922 ins->dreg = alloc_preg (cfg);
10923 ins->sreg1 = sp [0]->dreg;
10924 ins->type = STACK_I4;
10925 /* This flag will be inherited by the decomposition */
10926 ins->flags |= MONO_INST_FAULT;
10927 MONO_ADD_INS (cfg->cbb, ins);
10928 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
10929 cfg->cbb->has_array_access = TRUE;
10937 if (sp [0]->type != STACK_OBJ)
10940 cfg->flags |= MONO_CFG_HAS_LDELEMA;
10942 klass = mini_get_class (method, read32 (ip + 1), generic_context);
10943 CHECK_TYPELOAD (klass);
10944 /* we need to make sure that this array is exactly the type it needs
10945 * to be for correctness. the wrappers are lax with their usage
10946 * so we need to ignore them here
10948 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
10949 MonoClass *array_class = mono_array_class_get (klass, 1);
10950 mini_emit_check_array_type (cfg, sp [0], array_class);
10951 CHECK_TYPELOAD (array_class);
10955 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
10960 case CEE_LDELEM_I1:
10961 case CEE_LDELEM_U1:
10962 case CEE_LDELEM_I2:
10963 case CEE_LDELEM_U2:
10964 case CEE_LDELEM_I4:
10965 case CEE_LDELEM_U4:
10966 case CEE_LDELEM_I8:
10968 case CEE_LDELEM_R4:
10969 case CEE_LDELEM_R8:
10970 case CEE_LDELEM_REF: {
10976 if (*ip == CEE_LDELEM) {
10978 token = read32 (ip + 1);
10979 klass = mini_get_class (method, token, generic_context);
10980 CHECK_TYPELOAD (klass);
10981 mono_class_init (klass);
10984 klass = array_access_to_klass (*ip);
10986 if (sp [0]->type != STACK_OBJ)
10989 cfg->flags |= MONO_CFG_HAS_LDELEMA;
10991 if (mini_is_gsharedvt_variable_klass (klass)) {
10992 // FIXME-VT: OP_ICONST optimization
10993 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
10994 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10995 ins->opcode = OP_LOADV_MEMBASE;
10996 } else if (sp [1]->opcode == OP_ICONST) {
10997 int array_reg = sp [0]->dreg;
10998 int index_reg = sp [1]->dreg;
10999 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11001 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11002 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11004 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11005 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11007 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11008 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11011 if (*ip == CEE_LDELEM)
11018 case CEE_STELEM_I1:
11019 case CEE_STELEM_I2:
11020 case CEE_STELEM_I4:
11021 case CEE_STELEM_I8:
11022 case CEE_STELEM_R4:
11023 case CEE_STELEM_R8:
11024 case CEE_STELEM_REF:
11029 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11031 if (*ip == CEE_STELEM) {
11033 token = read32 (ip + 1);
11034 klass = mini_get_class (method, token, generic_context);
11035 CHECK_TYPELOAD (klass);
11036 mono_class_init (klass);
11039 klass = array_access_to_klass (*ip);
11041 if (sp [0]->type != STACK_OBJ)
11044 emit_array_store (cfg, klass, sp, TRUE);
11046 if (*ip == CEE_STELEM)
11053 case CEE_CKFINITE: {
11057 if (cfg->llvm_only) {
11058 MonoInst *iargs [1];
11060 iargs [0] = sp [0];
11061 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
11063 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11064 ins->sreg1 = sp [0]->dreg;
11065 ins->dreg = alloc_freg (cfg);
11066 ins->type = STACK_R8;
11067 MONO_ADD_INS (cfg->cbb, ins);
11069 *sp++ = mono_decompose_opcode (cfg, ins);
11075 case CEE_REFANYVAL: {
11076 MonoInst *src_var, *src;
11078 int klass_reg = alloc_preg (cfg);
11079 int dreg = alloc_preg (cfg);
11081 GSHAREDVT_FAILURE (*ip);
11084 MONO_INST_NEW (cfg, ins, *ip);
11087 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11088 CHECK_TYPELOAD (klass);
11090 context_used = mini_class_check_context_used (cfg, klass);
11093 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11095 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11096 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11097 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11099 if (context_used) {
11100 MonoInst *klass_ins;
11102 klass_ins = mini_emit_get_rgctx_klass (cfg, context_used,
11103 klass, MONO_RGCTX_INFO_KLASS);
11106 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11107 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11109 mini_emit_class_check (cfg, klass_reg, klass);
11111 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11112 ins->type = STACK_MP;
11113 ins->klass = klass;
11118 case CEE_MKREFANY: {
11119 MonoInst *loc, *addr;
11121 GSHAREDVT_FAILURE (*ip);
11124 MONO_INST_NEW (cfg, ins, *ip);
11127 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11128 CHECK_TYPELOAD (klass);
11130 context_used = mini_class_check_context_used (cfg, klass);
11132 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11133 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11135 if (context_used) {
11136 MonoInst *const_ins;
11137 int type_reg = alloc_preg (cfg);
11139 const_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11140 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11141 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11142 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11144 int const_reg = alloc_preg (cfg);
11145 int type_reg = alloc_preg (cfg);
11147 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11148 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11149 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11150 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11152 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11154 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11155 ins->type = STACK_VTYPE;
11156 ins->klass = mono_defaults.typed_reference_class;
11161 case CEE_LDTOKEN: {
11163 MonoClass *handle_class;
11165 CHECK_STACK_OVF (1);
11168 n = read32 (ip + 1);
11170 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11171 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11172 handle = mono_method_get_wrapper_data (method, n);
11173 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
11174 if (handle_class == mono_defaults.typehandle_class)
11175 handle = &((MonoClass*)handle)->byval_arg;
11178 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11183 mono_class_init (handle_class);
11184 if (cfg->gshared) {
11185 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11186 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11187 /* This case handles ldtoken
11188 of an open type, like for
11191 } else if (handle_class == mono_defaults.typehandle_class) {
11192 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
11193 } else if (handle_class == mono_defaults.fieldhandle_class)
11194 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11195 else if (handle_class == mono_defaults.methodhandle_class)
11196 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
11198 g_assert_not_reached ();
11201 if ((cfg->opt & MONO_OPT_SHARED) &&
11202 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11203 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11204 MonoInst *addr, *vtvar, *iargs [3];
11205 int method_context_used;
11207 method_context_used = mini_method_check_context_used (cfg, method);
11209 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11211 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11212 EMIT_NEW_ICONST (cfg, iargs [1], n);
11213 if (method_context_used) {
11214 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11215 method, MONO_RGCTX_INFO_METHOD);
11216 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11218 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11219 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11221 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11223 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11225 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11227 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11228 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
11229 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11230 (cmethod->klass == mono_defaults.systemtype_class) &&
11231 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11232 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
11234 mono_class_init (tclass);
11235 if (context_used) {
11236 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11237 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11238 } else if (cfg->compile_aot) {
11239 if (method->wrapper_type) {
11240 error_init (&error); //got to do it since there are multiple conditionals below
11241 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11242 /* Special case for static synchronized wrappers */
11243 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11245 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11246 /* FIXME: n is not a normal token */
11248 EMIT_NEW_PCONST (cfg, ins, NULL);
11251 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11254 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
11256 EMIT_NEW_PCONST (cfg, ins, rt);
11258 ins->type = STACK_OBJ;
11259 ins->klass = cmethod->klass;
11262 MonoInst *addr, *vtvar;
11264 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11266 if (context_used) {
11267 if (handle_class == mono_defaults.typehandle_class) {
11268 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11269 mono_class_from_mono_type ((MonoType *)handle),
11270 MONO_RGCTX_INFO_TYPE);
11271 } else if (handle_class == mono_defaults.methodhandle_class) {
11272 ins = emit_get_rgctx_method (cfg, context_used,
11273 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
11274 } else if (handle_class == mono_defaults.fieldhandle_class) {
11275 ins = emit_get_rgctx_field (cfg, context_used,
11276 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
11278 g_assert_not_reached ();
11280 } else if (cfg->compile_aot) {
11281 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11283 EMIT_NEW_PCONST (cfg, ins, handle);
11285 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11286 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11287 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11297 if (sp [-1]->type != STACK_OBJ)
11300 MONO_INST_NEW (cfg, ins, OP_THROW);
11302 ins->sreg1 = sp [0]->dreg;
11304 cfg->cbb->out_of_line = TRUE;
11305 MONO_ADD_INS (cfg->cbb, ins);
11306 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11307 MONO_ADD_INS (cfg->cbb, ins);
11310 link_bblock (cfg, cfg->cbb, end_bblock);
11311 start_new_bblock = 1;
11312 /* This can complicate code generation for llvm since the return value might not be defined */
11313 if (COMPILE_LLVM (cfg))
11314 INLINE_FAILURE ("throw");
11316 case CEE_ENDFINALLY:
11317 if (!ip_in_finally_clause (cfg, ip - header->code))
11319 /* mono_save_seq_point_info () depends on this */
11320 if (sp != stack_start)
11321 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11322 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11323 MONO_ADD_INS (cfg->cbb, ins);
11325 start_new_bblock = 1;
11328 * Control will leave the method so empty the stack, otherwise
11329 * the next basic block will start with a nonempty stack.
11331 while (sp != stack_start) {
11336 case CEE_LEAVE_S: {
11339 if (*ip == CEE_LEAVE) {
11341 target = ip + 5 + (gint32)read32(ip + 1);
11344 target = ip + 2 + (signed char)(ip [1]);
11347 /* empty the stack */
11348 while (sp != stack_start) {
11353 * If this leave statement is in a catch block, check for a
11354 * pending exception, and rethrow it if necessary.
11355 * We avoid doing this in runtime invoke wrappers, since those are called
11356 * by native code which excepts the wrapper to catch all exceptions.
11358 for (i = 0; i < header->num_clauses; ++i) {
11359 MonoExceptionClause *clause = &header->clauses [i];
11362 * Use <= in the final comparison to handle clauses with multiple
11363 * leave statements, like in bug #78024.
11364 * The ordering of the exception clauses guarantees that we find the
11365 * innermost clause.
11367 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) {
11369 MonoBasicBlock *dont_throw;
11374 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11377 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11379 NEW_BBLOCK (cfg, dont_throw);
11382 * Currently, we always rethrow the abort exception, despite the
11383 * fact that this is not correct. See thread6.cs for an example.
11384 * But propagating the abort exception is more important than
11385 * getting the sematics right.
11387 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11388 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11389 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11391 MONO_START_BB (cfg, dont_throw);
11396 cfg->cbb->try_end = (intptr_t)(ip - header->code);
11399 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11401 MonoExceptionClause *clause;
11403 for (tmp = handlers; tmp; tmp = tmp->next) {
11404 clause = (MonoExceptionClause *)tmp->data;
11405 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11407 link_bblock (cfg, cfg->cbb, tblock);
11408 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11409 ins->inst_target_bb = tblock;
11410 ins->inst_eh_block = clause;
11411 MONO_ADD_INS (cfg->cbb, ins);
11412 cfg->cbb->has_call_handler = 1;
11413 if (COMPILE_LLVM (cfg)) {
11414 MonoBasicBlock *target_bb;
11417 * Link the finally bblock with the target, since it will
11418 * conceptually branch there.
11420 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
11421 GET_BBLOCK (cfg, target_bb, target);
11422 link_bblock (cfg, tblock, target_bb);
11425 g_list_free (handlers);
11428 MONO_INST_NEW (cfg, ins, OP_BR);
11429 MONO_ADD_INS (cfg->cbb, ins);
11430 GET_BBLOCK (cfg, tblock, target);
11431 link_bblock (cfg, cfg->cbb, tblock);
11432 ins->inst_target_bb = tblock;
11434 start_new_bblock = 1;
11436 if (*ip == CEE_LEAVE)
11445 * Mono specific opcodes
11447 case MONO_CUSTOM_PREFIX: {
11449 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11453 case CEE_MONO_ICALL: {
11455 MonoJitICallInfo *info;
11457 token = read32 (ip + 2);
11458 func = mono_method_get_wrapper_data (method, token);
11459 info = mono_find_jit_icall_by_addr (func);
11461 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11464 CHECK_STACK (info->sig->param_count);
11465 sp -= info->sig->param_count;
11467 if (cfg->compile_aot && !strcmp (info->name, "mono_threads_attach_coop")) {
11471 * This is called on unattached threads, so it cannot go through the trampoline
11472 * infrastructure. Use an indirect call through a got slot initialized at load time
11475 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL, (char*)info->name);
11476 ins = mini_emit_calli (cfg, info->sig, sp, addr, NULL, NULL);
11478 ins = mono_emit_jit_icall (cfg, info->func, sp);
11481 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11485 inline_costs += 10 * num_calls++;
11489 case CEE_MONO_LDPTR_CARD_TABLE:
11490 case CEE_MONO_LDPTR_NURSERY_START:
11491 case CEE_MONO_LDPTR_NURSERY_BITS:
11492 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
11493 CHECK_STACK_OVF (1);
11496 case CEE_MONO_LDPTR_CARD_TABLE:
11497 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11499 case CEE_MONO_LDPTR_NURSERY_START:
11500 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11502 case CEE_MONO_LDPTR_NURSERY_BITS:
11503 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
11505 case CEE_MONO_LDPTR_INT_REQ_FLAG:
11506 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11509 g_assert_not_reached ();
11515 inline_costs += 10 * num_calls++;
11518 case CEE_MONO_LDPTR: {
11521 CHECK_STACK_OVF (1);
11523 token = read32 (ip + 2);
11525 ptr = mono_method_get_wrapper_data (method, token);
11526 EMIT_NEW_PCONST (cfg, ins, ptr);
11529 inline_costs += 10 * num_calls++;
11530 /* Can't embed random pointers into AOT code */
11534 case CEE_MONO_JIT_ICALL_ADDR: {
11535 MonoJitICallInfo *callinfo;
11538 CHECK_STACK_OVF (1);
11540 token = read32 (ip + 2);
11542 ptr = mono_method_get_wrapper_data (method, token);
11543 callinfo = mono_find_jit_icall_by_addr (ptr);
11544 g_assert (callinfo);
11545 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
11548 inline_costs += 10 * num_calls++;
11551 case CEE_MONO_ICALL_ADDR: {
11552 MonoMethod *cmethod;
11555 CHECK_STACK_OVF (1);
11557 token = read32 (ip + 2);
11559 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
11561 if (cfg->compile_aot) {
11562 if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
11564 * This is generated by emit_native_wrapper () to resolve the pinvoke address
11565 * before the call, its not needed when using direct pinvoke.
11566 * This is not an optimization, but its used to avoid looking up pinvokes
11567 * on platforms which don't support dlopen ().
11569 EMIT_NEW_PCONST (cfg, ins, NULL);
11571 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
11574 ptr = mono_lookup_internal_call (cmethod);
11576 EMIT_NEW_PCONST (cfg, ins, ptr);
11582 case CEE_MONO_VTADDR: {
11583 MonoInst *src_var, *src;
11589 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11590 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
11595 case CEE_MONO_NEWOBJ: {
11596 MonoInst *iargs [2];
11598 CHECK_STACK_OVF (1);
11600 token = read32 (ip + 2);
11601 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11602 mono_class_init (klass);
11603 NEW_DOMAINCONST (cfg, iargs [0]);
11604 MONO_ADD_INS (cfg->cbb, iargs [0]);
11605 NEW_CLASSCONST (cfg, iargs [1], klass);
11606 MONO_ADD_INS (cfg->cbb, iargs [1]);
11607 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
11609 inline_costs += 10 * num_calls++;
11612 case CEE_MONO_OBJADDR:
11615 MONO_INST_NEW (cfg, ins, OP_MOVE);
11616 ins->dreg = alloc_ireg_mp (cfg);
11617 ins->sreg1 = sp [0]->dreg;
11618 ins->type = STACK_MP;
11619 MONO_ADD_INS (cfg->cbb, ins);
11623 case CEE_MONO_LDNATIVEOBJ:
11625 * Similar to LDOBJ, but instead load the unmanaged
11626 * representation of the vtype to the stack.
11631 token = read32 (ip + 2);
11632 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11633 g_assert (klass->valuetype);
11634 mono_class_init (klass);
11637 MonoInst *src, *dest, *temp;
11640 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
11641 temp->backend.is_pinvoke = 1;
11642 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
11643 mini_emit_memory_copy (cfg, dest, src, klass, TRUE, 0);
11645 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
11646 dest->type = STACK_VTYPE;
11647 dest->klass = klass;
11653 case CEE_MONO_RETOBJ: {
11655 * Same as RET, but return the native representation of a vtype
11658 g_assert (cfg->ret);
11659 g_assert (mono_method_signature (method)->pinvoke);
11664 token = read32 (ip + 2);
11665 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11667 if (!cfg->vret_addr) {
11668 g_assert (cfg->ret_var_is_local);
11670 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
11672 EMIT_NEW_RETLOADA (cfg, ins);
11674 mini_emit_memory_copy (cfg, ins, sp [0], klass, TRUE, 0);
11676 if (sp != stack_start)
11679 MONO_INST_NEW (cfg, ins, OP_BR);
11680 ins->inst_target_bb = end_bblock;
11681 MONO_ADD_INS (cfg->cbb, ins);
11682 link_bblock (cfg, cfg->cbb, end_bblock);
11683 start_new_bblock = 1;
11687 case CEE_MONO_SAVE_LMF:
11688 case CEE_MONO_RESTORE_LMF:
11691 case CEE_MONO_CLASSCONST:
11692 CHECK_STACK_OVF (1);
11694 token = read32 (ip + 2);
11695 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
11698 inline_costs += 10 * num_calls++;
11700 case CEE_MONO_NOT_TAKEN:
11701 cfg->cbb->out_of_line = TRUE;
11704 case CEE_MONO_TLS: {
11707 CHECK_STACK_OVF (1);
11709 key = (MonoTlsKey)read32 (ip + 2);
11710 g_assert (key < TLS_KEY_NUM);
11712 ins = mono_create_tls_get (cfg, key);
11714 ins->type = STACK_PTR;
11719 case CEE_MONO_DYN_CALL: {
11720 MonoCallInst *call;
11722 /* It would be easier to call a trampoline, but that would put an
11723 * extra frame on the stack, confusing exception handling. So
11724 * implement it inline using an opcode for now.
11727 if (!cfg->dyn_call_var) {
11728 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
11729 /* prevent it from being register allocated */
11730 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
11733 /* Has to use a call inst since it local regalloc expects it */
11734 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
11735 ins = (MonoInst*)call;
11737 ins->sreg1 = sp [0]->dreg;
11738 ins->sreg2 = sp [1]->dreg;
11739 MONO_ADD_INS (cfg->cbb, ins);
11741 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
11744 inline_costs += 10 * num_calls++;
11748 case CEE_MONO_MEMORY_BARRIER: {
11750 mini_emit_memory_barrier (cfg, (int)read32 (ip + 2));
11754 case CEE_MONO_ATOMIC_STORE_I4: {
11755 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
11761 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
11762 ins->dreg = sp [0]->dreg;
11763 ins->sreg1 = sp [1]->dreg;
11764 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
11765 MONO_ADD_INS (cfg->cbb, ins);
11770 case CEE_MONO_JIT_ATTACH: {
11771 MonoInst *args [16], *domain_ins;
11772 MonoInst *ad_ins, *jit_tls_ins;
11773 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
11775 g_assert (!mono_threads_is_blocking_transition_enabled ());
11777 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
11779 EMIT_NEW_PCONST (cfg, ins, NULL);
11780 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
11782 ad_ins = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
11783 jit_tls_ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
11785 if (ad_ins && jit_tls_ins) {
11786 NEW_BBLOCK (cfg, next_bb);
11787 NEW_BBLOCK (cfg, call_bb);
11789 if (cfg->compile_aot) {
11790 /* AOT code is only used in the root domain */
11791 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
11793 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
11795 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
11796 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
11798 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
11799 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
11801 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
11802 MONO_START_BB (cfg, call_bb);
11805 /* AOT code is only used in the root domain */
11806 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
11807 if (cfg->compile_aot) {
11811 * This is called on unattached threads, so it cannot go through the trampoline
11812 * infrastructure. Use an indirect call through a got slot initialized at load time
11815 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
11816 ins = mini_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
11818 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
11820 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
11823 MONO_START_BB (cfg, next_bb);
11828 case CEE_MONO_JIT_DETACH: {
11829 MonoInst *args [16];
11831 /* Restore the original domain */
11832 dreg = alloc_ireg (cfg);
11833 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
11834 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
11838 case CEE_MONO_CALLI_EXTRA_ARG: {
11840 MonoMethodSignature *fsig;
11844 * This is the same as CEE_CALLI, but passes an additional argument
11845 * to the called method in llvmonly mode.
11846 * This is only used by delegate invoke wrappers to call the
11847 * actual delegate method.
11849 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
11852 token = read32 (ip + 2);
11860 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
11863 if (cfg->llvm_only)
11864 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
11866 n = fsig->param_count + fsig->hasthis + 1;
11873 if (cfg->llvm_only) {
11875 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
11876 * cconv. This is set by mono_init_delegate ().
11878 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
11879 MonoInst *callee = addr;
11880 MonoInst *call, *localloc_ins;
11881 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
11882 int low_bit_reg = alloc_preg (cfg);
11884 NEW_BBLOCK (cfg, is_gsharedvt_bb);
11885 NEW_BBLOCK (cfg, end_bb);
11887 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
11888 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
11889 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
11891 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
11892 addr = emit_get_rgctx_sig (cfg, context_used,
11893 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
11895 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
11897 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
11898 ins->dreg = alloc_preg (cfg);
11899 ins->inst_imm = 2 * SIZEOF_VOID_P;
11900 MONO_ADD_INS (cfg->cbb, ins);
11901 localloc_ins = ins;
11902 cfg->flags |= MONO_CFG_HAS_ALLOCA;
11903 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
11904 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
11906 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
11907 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
11909 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
11910 MONO_START_BB (cfg, is_gsharedvt_bb);
11911 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
11912 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
11913 ins->dreg = call->dreg;
11915 MONO_START_BB (cfg, end_bb);
11917 /* Caller uses a normal calling conv */
11919 MonoInst *callee = addr;
11920 MonoInst *call, *localloc_ins;
11921 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
11922 int low_bit_reg = alloc_preg (cfg);
11924 NEW_BBLOCK (cfg, is_gsharedvt_bb);
11925 NEW_BBLOCK (cfg, end_bb);
11927 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
11928 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
11929 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
11931 /* Normal case: callee uses a normal cconv, no conversion is needed */
11932 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
11933 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
11934 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
11935 MONO_START_BB (cfg, is_gsharedvt_bb);
11936 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
11937 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
11938 MONO_ADD_INS (cfg->cbb, addr);
11940 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
11942 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
11943 ins->dreg = alloc_preg (cfg);
11944 ins->inst_imm = 2 * SIZEOF_VOID_P;
11945 MONO_ADD_INS (cfg->cbb, ins);
11946 localloc_ins = ins;
11947 cfg->flags |= MONO_CFG_HAS_ALLOCA;
11948 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
11949 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
11951 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
11952 ins->dreg = call->dreg;
11953 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
11955 MONO_START_BB (cfg, end_bb);
11958 /* Same as CEE_CALLI */
11959 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
11961 * We pass the address to the gsharedvt trampoline in the rgctx reg
11963 MonoInst *callee = addr;
11965 addr = emit_get_rgctx_sig (cfg, context_used,
11966 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
11967 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, callee);
11969 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
11973 if (!MONO_TYPE_IS_VOID (fsig->ret))
11974 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
11976 CHECK_CFG_EXCEPTION;
11980 constrained_class = NULL;
11983 case CEE_MONO_LDDOMAIN:
11984 CHECK_STACK_OVF (1);
11985 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
11989 case CEE_MONO_GET_LAST_ERROR:
11991 CHECK_STACK_OVF (1);
11993 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
11994 ins->dreg = alloc_dreg (cfg, STACK_I4);
11995 ins->type = STACK_I4;
11996 MONO_ADD_INS (cfg->cbb, ins);
12001 case CEE_MONO_GET_RGCTX_ARG:
12003 CHECK_STACK_OVF (1);
12005 mono_create_rgctx_var (cfg);
12007 MONO_INST_NEW (cfg, ins, OP_MOVE);
12008 ins->dreg = alloc_dreg (cfg, STACK_PTR);
12009 ins->sreg1 = cfg->rgctx_var->dreg;
12010 ins->type = STACK_PTR;
12011 MONO_ADD_INS (cfg->cbb, ins);
12017 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12023 case CEE_PREFIX1: {
12026 case CEE_ARGLIST: {
12027 /* somewhat similar to LDTOKEN */
12028 MonoInst *addr, *vtvar;
12029 CHECK_STACK_OVF (1);
12030 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12032 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12033 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12035 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12036 ins->type = STACK_VTYPE;
12037 ins->klass = mono_defaults.argumenthandle_class;
12047 MonoInst *cmp, *arg1, *arg2;
12055 * The following transforms:
12056 * CEE_CEQ into OP_CEQ
12057 * CEE_CGT into OP_CGT
12058 * CEE_CGT_UN into OP_CGT_UN
12059 * CEE_CLT into OP_CLT
12060 * CEE_CLT_UN into OP_CLT_UN
12062 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12064 MONO_INST_NEW (cfg, ins, cmp->opcode);
12065 cmp->sreg1 = arg1->dreg;
12066 cmp->sreg2 = arg2->dreg;
12067 type_from_op (cfg, cmp, arg1, arg2);
12069 add_widen_op (cfg, cmp, &arg1, &arg2);
12070 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12071 cmp->opcode = OP_LCOMPARE;
12072 else if (arg1->type == STACK_R4)
12073 cmp->opcode = OP_RCOMPARE;
12074 else if (arg1->type == STACK_R8)
12075 cmp->opcode = OP_FCOMPARE;
12077 cmp->opcode = OP_ICOMPARE;
12078 MONO_ADD_INS (cfg->cbb, cmp);
12079 ins->type = STACK_I4;
12080 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
12081 type_from_op (cfg, ins, arg1, arg2);
12083 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12085 * The backends expect the fceq opcodes to do the
12088 ins->sreg1 = cmp->sreg1;
12089 ins->sreg2 = cmp->sreg2;
12092 MONO_ADD_INS (cfg->cbb, ins);
12098 MonoInst *argconst;
12099 MonoMethod *cil_method;
12101 CHECK_STACK_OVF (1);
12103 n = read32 (ip + 2);
12104 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12107 mono_class_init (cmethod->klass);
12109 mono_save_token_info (cfg, image, n, cmethod);
12111 context_used = mini_method_check_context_used (cfg, cmethod);
12113 cil_method = cmethod;
12114 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12115 emit_method_access_failure (cfg, method, cil_method);
12117 if (mono_security_core_clr_enabled ())
12118 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12121 * Optimize the common case of ldftn+delegate creation
12123 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12124 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12125 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12126 MonoInst *target_ins, *handle_ins;
12127 MonoMethod *invoke;
12128 int invoke_context_used;
12130 invoke = mono_get_delegate_invoke (ctor_method->klass);
12131 if (!invoke || !mono_method_signature (invoke))
12134 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12136 target_ins = sp [-1];
12138 if (mono_security_core_clr_enabled ())
12139 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12141 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12142 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12143 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12144 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12145 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12149 /* FIXME: SGEN support */
12150 if (invoke_context_used == 0 || cfg->llvm_only) {
12152 if (cfg->verbose_level > 3)
12153 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));
12154 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12157 CHECK_CFG_EXCEPTION;
12167 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12168 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12172 inline_costs += 10 * num_calls++;
12175 case CEE_LDVIRTFTN: {
12176 MonoInst *args [2];
12180 n = read32 (ip + 2);
12181 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12184 mono_class_init (cmethod->klass);
12186 context_used = mini_method_check_context_used (cfg, cmethod);
12188 if (mono_security_core_clr_enabled ())
12189 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12192 * Optimize the common case of ldvirtftn+delegate creation
12194 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)) {
12195 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12196 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12197 MonoInst *target_ins, *handle_ins;
12198 MonoMethod *invoke;
12199 int invoke_context_used;
12200 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12202 invoke = mono_get_delegate_invoke (ctor_method->klass);
12203 if (!invoke || !mono_method_signature (invoke))
12206 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12208 target_ins = sp [-1];
12210 if (mono_security_core_clr_enabled ())
12211 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12213 /* FIXME: SGEN support */
12214 if (invoke_context_used == 0 || cfg->llvm_only) {
12216 if (cfg->verbose_level > 3)
12217 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));
12218 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12221 CHECK_CFG_EXCEPTION;
12234 args [1] = emit_get_rgctx_method (cfg, context_used,
12235 cmethod, MONO_RGCTX_INFO_METHOD);
12238 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12240 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12243 inline_costs += 10 * num_calls++;
12247 CHECK_STACK_OVF (1);
12249 n = read16 (ip + 2);
12251 EMIT_NEW_ARGLOAD (cfg, ins, n);
12256 CHECK_STACK_OVF (1);
12258 n = read16 (ip + 2);
12260 NEW_ARGLOADA (cfg, ins, n);
12261 MONO_ADD_INS (cfg->cbb, ins);
12269 n = read16 (ip + 2);
12271 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12273 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12277 CHECK_STACK_OVF (1);
12279 n = read16 (ip + 2);
12281 EMIT_NEW_LOCLOAD (cfg, ins, n);
12286 unsigned char *tmp_ip;
12287 CHECK_STACK_OVF (1);
12289 n = read16 (ip + 2);
12292 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12298 EMIT_NEW_LOCLOADA (cfg, ins, n);
12307 n = read16 (ip + 2);
12309 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12311 emit_stloc_ir (cfg, sp, header, n);
12315 case CEE_LOCALLOC: {
12317 MonoBasicBlock *non_zero_bb, *end_bb;
12318 int alloc_ptr = alloc_preg (cfg);
12320 if (sp != stack_start)
12322 if (cfg->method != method)
12324 * Inlining this into a loop in a parent could lead to
12325 * stack overflows which is different behavior than the
12326 * non-inlined case, thus disable inlining in this case.
12328 INLINE_FAILURE("localloc");
12330 NEW_BBLOCK (cfg, non_zero_bb);
12331 NEW_BBLOCK (cfg, end_bb);
12333 /* if size != zero */
12334 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
12335 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
12337 //size is zero, so result is NULL
12338 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
12339 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12341 MONO_START_BB (cfg, non_zero_bb);
12342 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12343 ins->dreg = alloc_ptr;
12344 ins->sreg1 = sp [0]->dreg;
12345 ins->type = STACK_PTR;
12346 MONO_ADD_INS (cfg->cbb, ins);
12348 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12350 ins->flags |= MONO_INST_INIT;
12352 MONO_START_BB (cfg, end_bb);
12353 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
12354 ins->type = STACK_PTR;
12360 case CEE_ENDFILTER: {
12361 MonoExceptionClause *clause, *nearest;
12366 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
12368 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12369 ins->sreg1 = (*sp)->dreg;
12370 MONO_ADD_INS (cfg->cbb, ins);
12371 start_new_bblock = 1;
12375 for (cc = 0; cc < header->num_clauses; ++cc) {
12376 clause = &header->clauses [cc];
12377 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12378 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12379 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12382 g_assert (nearest);
12383 if ((ip - header->code) != nearest->handler_offset)
12388 case CEE_UNALIGNED_:
12389 ins_flag |= MONO_INST_UNALIGNED;
12390 /* FIXME: record alignment? we can assume 1 for now */
12394 case CEE_VOLATILE_:
12395 ins_flag |= MONO_INST_VOLATILE;
12399 ins_flag |= MONO_INST_TAILCALL;
12400 cfg->flags |= MONO_CFG_HAS_TAIL;
12401 /* Can't inline tail calls at this time */
12402 inline_costs += 100000;
12409 token = read32 (ip + 2);
12410 klass = mini_get_class (method, token, generic_context);
12411 CHECK_TYPELOAD (klass);
12412 if (generic_class_is_reference_type (cfg, klass))
12413 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12415 mini_emit_initobj (cfg, *sp, NULL, klass);
12419 case CEE_CONSTRAINED_:
12421 token = read32 (ip + 2);
12422 constrained_class = mini_get_class (method, token, generic_context);
12423 CHECK_TYPELOAD (constrained_class);
12429 mini_emit_memory_copy_bytes (cfg, sp [0], sp [1], sp [2], ins_flag);
12437 mini_emit_memory_init_bytes (cfg, sp [0], sp [1], sp [2], ins_flag);
12445 ins_flag |= MONO_INST_NOTYPECHECK;
12447 ins_flag |= MONO_INST_NORANGECHECK;
12448 /* we ignore the no-nullcheck for now since we
12449 * really do it explicitly only when doing callvirt->call
12453 case CEE_RETHROW: {
12455 int handler_offset = -1;
12457 for (i = 0; i < header->num_clauses; ++i) {
12458 MonoExceptionClause *clause = &header->clauses [i];
12459 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12460 handler_offset = clause->handler_offset;
12465 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12467 if (handler_offset == -1)
12470 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12471 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12472 ins->sreg1 = load->dreg;
12473 MONO_ADD_INS (cfg->cbb, ins);
12475 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12476 MONO_ADD_INS (cfg->cbb, ins);
12479 link_bblock (cfg, cfg->cbb, end_bblock);
12480 start_new_bblock = 1;
12488 CHECK_STACK_OVF (1);
12490 token = read32 (ip + 2);
12491 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12492 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12495 val = mono_type_size (type, &ialign);
12497 MonoClass *klass = mini_get_class (method, token, generic_context);
12498 CHECK_TYPELOAD (klass);
12500 val = mono_type_size (&klass->byval_arg, &ialign);
12502 if (mini_is_gsharedvt_klass (klass))
12503 GSHAREDVT_FAILURE (*ip);
12505 EMIT_NEW_ICONST (cfg, ins, val);
12510 case CEE_REFANYTYPE: {
12511 MonoInst *src_var, *src;
12513 GSHAREDVT_FAILURE (*ip);
12519 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12521 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12522 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12523 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12528 case CEE_READONLY_:
12541 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12551 g_warning ("opcode 0x%02x not handled", *ip);
12555 if (start_new_bblock != 1)
12558 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12559 if (cfg->cbb->next_bb) {
12560 /* This could already be set because of inlining, #693905 */
12561 MonoBasicBlock *bb = cfg->cbb;
12563 while (bb->next_bb)
12565 bb->next_bb = end_bblock;
12567 cfg->cbb->next_bb = end_bblock;
12570 if (cfg->method == method && cfg->domainvar) {
12572 MonoInst *get_domain;
12574 cfg->cbb = init_localsbb;
12576 get_domain = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12577 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12578 MONO_ADD_INS (cfg->cbb, store);
12581 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12582 if (cfg->compile_aot)
12583 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12584 mono_get_got_var (cfg);
12587 if (cfg->method == method && cfg->got_var)
12588 mono_emit_load_got_addr (cfg);
12590 if (init_localsbb) {
12591 cfg->cbb = init_localsbb;
12593 for (i = 0; i < header->num_locals; ++i) {
12594 emit_init_local (cfg, i, header->locals [i], init_locals);
12598 if (cfg->init_ref_vars && cfg->method == method) {
12599 /* Emit initialization for ref vars */
12600 // FIXME: Avoid duplication initialization for IL locals.
12601 for (i = 0; i < cfg->num_varinfo; ++i) {
12602 MonoInst *ins = cfg->varinfo [i];
12604 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12605 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12609 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
12610 cfg->cbb = init_localsbb;
12611 emit_push_lmf (cfg);
12614 cfg->cbb = init_localsbb;
12615 emit_instrumentation_call (cfg, mono_profiler_method_enter);
12618 MonoBasicBlock *bb;
12621 * Make seq points at backward branch targets interruptable.
12623 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12624 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12625 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12628 /* Add a sequence point for method entry/exit events */
12629 if (seq_points && cfg->gen_sdb_seq_points) {
12630 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12631 MONO_ADD_INS (init_localsbb, ins);
12632 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12633 MONO_ADD_INS (cfg->bb_exit, ins);
12637 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12638 * the code they refer to was dead (#11880).
12640 if (sym_seq_points) {
12641 for (i = 0; i < header->code_size; ++i) {
12642 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12645 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12646 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
12653 if (cfg->method == method) {
12654 MonoBasicBlock *bb;
12655 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12656 if (bb == cfg->bb_init)
12659 bb->region = mono_find_block_region (cfg, bb->real_offset);
12661 mono_create_spvar_for_region (cfg, bb->region);
12662 if (cfg->verbose_level > 2)
12663 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
12666 MonoBasicBlock *bb;
12667 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
12668 for (bb = start_bblock; bb != end_bblock; bb = bb->next_bb) {
12669 bb->real_offset = inline_offset;
12673 if (inline_costs < 0) {
12676 /* Method is too large */
12677 mname = mono_method_full_name (method, TRUE);
12678 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
12682 if ((cfg->verbose_level > 2) && (cfg->method == method))
12683 mono_print_code (cfg, "AFTER METHOD-TO-IR");
12688 g_assert (!mono_error_ok (&cfg->error));
12692 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
12696 set_exception_type_from_invalid_il (cfg, method, ip);
12700 g_slist_free (class_inits);
12701 mono_basic_block_free (original_bb);
12702 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
12703 if (cfg->exception_type)
12706 return inline_costs;
12710 store_membase_reg_to_store_membase_imm (int opcode)
12713 case OP_STORE_MEMBASE_REG:
12714 return OP_STORE_MEMBASE_IMM;
12715 case OP_STOREI1_MEMBASE_REG:
12716 return OP_STOREI1_MEMBASE_IMM;
12717 case OP_STOREI2_MEMBASE_REG:
12718 return OP_STOREI2_MEMBASE_IMM;
12719 case OP_STOREI4_MEMBASE_REG:
12720 return OP_STOREI4_MEMBASE_IMM;
12721 case OP_STOREI8_MEMBASE_REG:
12722 return OP_STOREI8_MEMBASE_IMM;
12724 g_assert_not_reached ();
12731 mono_op_to_op_imm (int opcode)
12735 return OP_IADD_IMM;
12737 return OP_ISUB_IMM;
12739 return OP_IDIV_IMM;
12741 return OP_IDIV_UN_IMM;
12743 return OP_IREM_IMM;
12745 return OP_IREM_UN_IMM;
12747 return OP_IMUL_IMM;
12749 return OP_IAND_IMM;
12753 return OP_IXOR_IMM;
12755 return OP_ISHL_IMM;
12757 return OP_ISHR_IMM;
12759 return OP_ISHR_UN_IMM;
12762 return OP_LADD_IMM;
12764 return OP_LSUB_IMM;
12766 return OP_LAND_IMM;
12770 return OP_LXOR_IMM;
12772 return OP_LSHL_IMM;
12774 return OP_LSHR_IMM;
12776 return OP_LSHR_UN_IMM;
12777 #if SIZEOF_REGISTER == 8
12779 return OP_LREM_IMM;
12783 return OP_COMPARE_IMM;
12785 return OP_ICOMPARE_IMM;
12787 return OP_LCOMPARE_IMM;
12789 case OP_STORE_MEMBASE_REG:
12790 return OP_STORE_MEMBASE_IMM;
12791 case OP_STOREI1_MEMBASE_REG:
12792 return OP_STOREI1_MEMBASE_IMM;
12793 case OP_STOREI2_MEMBASE_REG:
12794 return OP_STOREI2_MEMBASE_IMM;
12795 case OP_STOREI4_MEMBASE_REG:
12796 return OP_STOREI4_MEMBASE_IMM;
12798 #if defined(TARGET_X86) || defined (TARGET_AMD64)
12800 return OP_X86_PUSH_IMM;
12801 case OP_X86_COMPARE_MEMBASE_REG:
12802 return OP_X86_COMPARE_MEMBASE_IMM;
12804 #if defined(TARGET_AMD64)
12805 case OP_AMD64_ICOMPARE_MEMBASE_REG:
12806 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
12808 case OP_VOIDCALL_REG:
12809 return OP_VOIDCALL;
12817 return OP_LOCALLOC_IMM;
12824 ldind_to_load_membase (int opcode)
12828 return OP_LOADI1_MEMBASE;
12830 return OP_LOADU1_MEMBASE;
12832 return OP_LOADI2_MEMBASE;
12834 return OP_LOADU2_MEMBASE;
12836 return OP_LOADI4_MEMBASE;
12838 return OP_LOADU4_MEMBASE;
12840 return OP_LOAD_MEMBASE;
12841 case CEE_LDIND_REF:
12842 return OP_LOAD_MEMBASE;
12844 return OP_LOADI8_MEMBASE;
12846 return OP_LOADR4_MEMBASE;
12848 return OP_LOADR8_MEMBASE;
12850 g_assert_not_reached ();
12857 stind_to_store_membase (int opcode)
12861 return OP_STOREI1_MEMBASE_REG;
12863 return OP_STOREI2_MEMBASE_REG;
12865 return OP_STOREI4_MEMBASE_REG;
12867 case CEE_STIND_REF:
12868 return OP_STORE_MEMBASE_REG;
12870 return OP_STOREI8_MEMBASE_REG;
12872 return OP_STORER4_MEMBASE_REG;
12874 return OP_STORER8_MEMBASE_REG;
12876 g_assert_not_reached ();
12883 mono_load_membase_to_load_mem (int opcode)
12885 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
12886 #if defined(TARGET_X86) || defined(TARGET_AMD64)
12888 case OP_LOAD_MEMBASE:
12889 return OP_LOAD_MEM;
12890 case OP_LOADU1_MEMBASE:
12891 return OP_LOADU1_MEM;
12892 case OP_LOADU2_MEMBASE:
12893 return OP_LOADU2_MEM;
12894 case OP_LOADI4_MEMBASE:
12895 return OP_LOADI4_MEM;
12896 case OP_LOADU4_MEMBASE:
12897 return OP_LOADU4_MEM;
12898 #if SIZEOF_REGISTER == 8
12899 case OP_LOADI8_MEMBASE:
12900 return OP_LOADI8_MEM;
12909 op_to_op_dest_membase (int store_opcode, int opcode)
12911 #if defined(TARGET_X86)
12912 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
12917 return OP_X86_ADD_MEMBASE_REG;
12919 return OP_X86_SUB_MEMBASE_REG;
12921 return OP_X86_AND_MEMBASE_REG;
12923 return OP_X86_OR_MEMBASE_REG;
12925 return OP_X86_XOR_MEMBASE_REG;
12928 return OP_X86_ADD_MEMBASE_IMM;
12931 return OP_X86_SUB_MEMBASE_IMM;
12934 return OP_X86_AND_MEMBASE_IMM;
12937 return OP_X86_OR_MEMBASE_IMM;
12940 return OP_X86_XOR_MEMBASE_IMM;
12946 #if defined(TARGET_AMD64)
12947 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
12952 return OP_X86_ADD_MEMBASE_REG;
12954 return OP_X86_SUB_MEMBASE_REG;
12956 return OP_X86_AND_MEMBASE_REG;
12958 return OP_X86_OR_MEMBASE_REG;
12960 return OP_X86_XOR_MEMBASE_REG;
12962 return OP_X86_ADD_MEMBASE_IMM;
12964 return OP_X86_SUB_MEMBASE_IMM;
12966 return OP_X86_AND_MEMBASE_IMM;
12968 return OP_X86_OR_MEMBASE_IMM;
12970 return OP_X86_XOR_MEMBASE_IMM;
12972 return OP_AMD64_ADD_MEMBASE_REG;
12974 return OP_AMD64_SUB_MEMBASE_REG;
12976 return OP_AMD64_AND_MEMBASE_REG;
12978 return OP_AMD64_OR_MEMBASE_REG;
12980 return OP_AMD64_XOR_MEMBASE_REG;
12983 return OP_AMD64_ADD_MEMBASE_IMM;
12986 return OP_AMD64_SUB_MEMBASE_IMM;
12989 return OP_AMD64_AND_MEMBASE_IMM;
12992 return OP_AMD64_OR_MEMBASE_IMM;
12995 return OP_AMD64_XOR_MEMBASE_IMM;
13005 op_to_op_store_membase (int store_opcode, int opcode)
13007 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13010 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13011 return OP_X86_SETEQ_MEMBASE;
13013 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13014 return OP_X86_SETNE_MEMBASE;
13022 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13025 /* FIXME: This has sign extension issues */
13027 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13028 return OP_X86_COMPARE_MEMBASE8_IMM;
13031 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13036 return OP_X86_PUSH_MEMBASE;
13037 case OP_COMPARE_IMM:
13038 case OP_ICOMPARE_IMM:
13039 return OP_X86_COMPARE_MEMBASE_IMM;
13042 return OP_X86_COMPARE_MEMBASE_REG;
13046 #ifdef TARGET_AMD64
13047 /* FIXME: This has sign extension issues */
13049 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13050 return OP_X86_COMPARE_MEMBASE8_IMM;
13055 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13056 return OP_X86_PUSH_MEMBASE;
13058 /* FIXME: This only works for 32 bit immediates
13059 case OP_COMPARE_IMM:
13060 case OP_LCOMPARE_IMM:
13061 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13062 return OP_AMD64_COMPARE_MEMBASE_IMM;
13064 case OP_ICOMPARE_IMM:
13065 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13066 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13070 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
13071 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13072 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13073 return OP_AMD64_COMPARE_MEMBASE_REG;
13076 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13077 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13086 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
13089 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13095 return OP_X86_COMPARE_REG_MEMBASE;
13097 return OP_X86_ADD_REG_MEMBASE;
13099 return OP_X86_SUB_REG_MEMBASE;
13101 return OP_X86_AND_REG_MEMBASE;
13103 return OP_X86_OR_REG_MEMBASE;
13105 return OP_X86_XOR_REG_MEMBASE;
13109 #ifdef TARGET_AMD64
13110 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
13113 return OP_AMD64_ICOMPARE_REG_MEMBASE;
13115 return OP_X86_ADD_REG_MEMBASE;
13117 return OP_X86_SUB_REG_MEMBASE;
13119 return OP_X86_AND_REG_MEMBASE;
13121 return OP_X86_OR_REG_MEMBASE;
13123 return OP_X86_XOR_REG_MEMBASE;
13125 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
13129 return OP_AMD64_COMPARE_REG_MEMBASE;
13131 return OP_AMD64_ADD_REG_MEMBASE;
13133 return OP_AMD64_SUB_REG_MEMBASE;
13135 return OP_AMD64_AND_REG_MEMBASE;
13137 return OP_AMD64_OR_REG_MEMBASE;
13139 return OP_AMD64_XOR_REG_MEMBASE;
13148 mono_op_to_op_imm_noemul (int opcode)
13151 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13157 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13164 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13169 return mono_op_to_op_imm (opcode);
13174 * mono_handle_global_vregs:
13176 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
13180 mono_handle_global_vregs (MonoCompile *cfg)
13182 gint32 *vreg_to_bb;
13183 MonoBasicBlock *bb;
13186 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13188 #ifdef MONO_ARCH_SIMD_INTRINSICS
13189 if (cfg->uses_simd_intrinsics)
13190 mono_simd_simplify_indirection (cfg);
13193 /* Find local vregs used in more than one bb */
13194 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13195 MonoInst *ins = bb->code;
13196 int block_num = bb->block_num;
13198 if (cfg->verbose_level > 2)
13199 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13202 for (; ins; ins = ins->next) {
13203 const char *spec = INS_INFO (ins->opcode);
13204 int regtype = 0, regindex;
13207 if (G_UNLIKELY (cfg->verbose_level > 2))
13208 mono_print_ins (ins);
13210 g_assert (ins->opcode >= MONO_CEE_LAST);
13212 for (regindex = 0; regindex < 4; regindex ++) {
13215 if (regindex == 0) {
13216 regtype = spec [MONO_INST_DEST];
13217 if (regtype == ' ')
13220 } else if (regindex == 1) {
13221 regtype = spec [MONO_INST_SRC1];
13222 if (regtype == ' ')
13225 } else if (regindex == 2) {
13226 regtype = spec [MONO_INST_SRC2];
13227 if (regtype == ' ')
13230 } else if (regindex == 3) {
13231 regtype = spec [MONO_INST_SRC3];
13232 if (regtype == ' ')
13237 #if SIZEOF_REGISTER == 4
13238 /* In the LLVM case, the long opcodes are not decomposed */
13239 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13241 * Since some instructions reference the original long vreg,
13242 * and some reference the two component vregs, it is quite hard
13243 * to determine when it needs to be global. So be conservative.
13245 if (!get_vreg_to_inst (cfg, vreg)) {
13246 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13248 if (cfg->verbose_level > 2)
13249 printf ("LONG VREG R%d made global.\n", vreg);
13253 * Make the component vregs volatile since the optimizations can
13254 * get confused otherwise.
13256 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
13257 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
13261 g_assert (vreg != -1);
13263 prev_bb = vreg_to_bb [vreg];
13264 if (prev_bb == 0) {
13265 /* 0 is a valid block num */
13266 vreg_to_bb [vreg] = block_num + 1;
13267 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13268 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13271 if (!get_vreg_to_inst (cfg, vreg)) {
13272 if (G_UNLIKELY (cfg->verbose_level > 2))
13273 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13277 if (vreg_is_ref (cfg, vreg))
13278 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13280 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13283 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13286 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13290 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13293 g_assert_not_reached ();
13297 /* Flag as having been used in more than one bb */
13298 vreg_to_bb [vreg] = -1;
13304 /* If a variable is used in only one bblock, convert it into a local vreg */
13305 for (i = 0; i < cfg->num_varinfo; i++) {
13306 MonoInst *var = cfg->varinfo [i];
13307 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13309 switch (var->type) {
13315 #if SIZEOF_REGISTER == 8
13318 #if !defined(TARGET_X86)
13319 /* Enabling this screws up the fp stack on x86 */
13322 if (mono_arch_is_soft_float ())
13326 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
13330 /* Arguments are implicitly global */
13331 /* Putting R4 vars into registers doesn't work currently */
13332 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13333 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) {
13335 * Make that the variable's liveness interval doesn't contain a call, since
13336 * that would cause the lvreg to be spilled, making the whole optimization
13339 /* This is too slow for JIT compilation */
13341 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13343 int def_index, call_index, ins_index;
13344 gboolean spilled = FALSE;
13349 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13350 const char *spec = INS_INFO (ins->opcode);
13352 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13353 def_index = ins_index;
13355 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13356 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13357 if (call_index > def_index) {
13363 if (MONO_IS_CALL (ins))
13364 call_index = ins_index;
13374 if (G_UNLIKELY (cfg->verbose_level > 2))
13375 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13376 var->flags |= MONO_INST_IS_DEAD;
13377 cfg->vreg_to_inst [var->dreg] = NULL;
13384 * Compress the varinfo and vars tables so the liveness computation is faster and
13385 * takes up less space.
13388 for (i = 0; i < cfg->num_varinfo; ++i) {
13389 MonoInst *var = cfg->varinfo [i];
13390 if (pos < i && cfg->locals_start == i)
13391 cfg->locals_start = pos;
13392 if (!(var->flags & MONO_INST_IS_DEAD)) {
13394 cfg->varinfo [pos] = cfg->varinfo [i];
13395 cfg->varinfo [pos]->inst_c0 = pos;
13396 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13397 cfg->vars [pos].idx = pos;
13398 #if SIZEOF_REGISTER == 4
13399 if (cfg->varinfo [pos]->type == STACK_I8) {
13400 /* Modify the two component vars too */
13403 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
13404 var1->inst_c0 = pos;
13405 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
13406 var1->inst_c0 = pos;
13413 cfg->num_varinfo = pos;
13414 if (cfg->locals_start > cfg->num_varinfo)
13415 cfg->locals_start = cfg->num_varinfo;
13419 * mono_allocate_gsharedvt_vars:
13421 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
13422 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
13425 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
13429 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13431 for (i = 0; i < cfg->num_varinfo; ++i) {
13432 MonoInst *ins = cfg->varinfo [i];
13435 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13436 if (i >= cfg->locals_start) {
13438 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13439 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13440 ins->opcode = OP_GSHAREDVT_LOCAL;
13441 ins->inst_imm = idx;
13444 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
13445 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13452 * mono_spill_global_vars:
13454 * Generate spill code for variables which are not allocated to registers,
13455 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13456 * code is generated which could be optimized by the local optimization passes.
13459 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13461 MonoBasicBlock *bb;
13463 int orig_next_vreg;
13464 guint32 *vreg_to_lvreg;
13466 guint32 i, lvregs_len, lvregs_size;
13467 gboolean dest_has_lvreg = FALSE;
13468 MonoStackType stacktypes [128];
13469 MonoInst **live_range_start, **live_range_end;
13470 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13472 *need_local_opts = FALSE;
13474 memset (spec2, 0, sizeof (spec2));
13476 /* FIXME: Move this function to mini.c */
13477 stacktypes ['i'] = STACK_PTR;
13478 stacktypes ['l'] = STACK_I8;
13479 stacktypes ['f'] = STACK_R8;
13480 #ifdef MONO_ARCH_SIMD_INTRINSICS
13481 stacktypes ['x'] = STACK_VTYPE;
13484 #if SIZEOF_REGISTER == 4
13485 /* Create MonoInsts for longs */
13486 for (i = 0; i < cfg->num_varinfo; i++) {
13487 MonoInst *ins = cfg->varinfo [i];
13489 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13490 switch (ins->type) {
13495 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13498 g_assert (ins->opcode == OP_REGOFFSET);
13500 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
13502 tree->opcode = OP_REGOFFSET;
13503 tree->inst_basereg = ins->inst_basereg;
13504 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13506 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
13508 tree->opcode = OP_REGOFFSET;
13509 tree->inst_basereg = ins->inst_basereg;
13510 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13520 if (cfg->compute_gc_maps) {
13521 /* registers need liveness info even for !non refs */
13522 for (i = 0; i < cfg->num_varinfo; i++) {
13523 MonoInst *ins = cfg->varinfo [i];
13525 if (ins->opcode == OP_REGVAR)
13526 ins->flags |= MONO_INST_GC_TRACK;
13530 /* FIXME: widening and truncation */
13533 * As an optimization, when a variable allocated to the stack is first loaded into
13534 * an lvreg, we will remember the lvreg and use it the next time instead of loading
13535 * the variable again.
13537 orig_next_vreg = cfg->next_vreg;
13538 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13539 lvregs_size = 1024;
13540 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * lvregs_size);
13544 * These arrays contain the first and last instructions accessing a given
13546 * Since we emit bblocks in the same order we process them here, and we
13547 * don't split live ranges, these will precisely describe the live range of
13548 * the variable, i.e. the instruction range where a valid value can be found
13549 * in the variables location.
13550 * The live range is computed using the liveness info computed by the liveness pass.
13551 * We can't use vmv->range, since that is an abstract live range, and we need
13552 * one which is instruction precise.
13553 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13555 /* FIXME: Only do this if debugging info is requested */
13556 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13557 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13558 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13559 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13561 /* Add spill loads/stores */
13562 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13565 if (cfg->verbose_level > 2)
13566 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13568 /* Clear vreg_to_lvreg array */
13569 for (i = 0; i < lvregs_len; i++)
13570 vreg_to_lvreg [lvregs [i]] = 0;
13574 MONO_BB_FOR_EACH_INS (bb, ins) {
13575 const char *spec = INS_INFO (ins->opcode);
13576 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13577 gboolean store, no_lvreg;
13578 int sregs [MONO_MAX_SRC_REGS];
13580 if (G_UNLIKELY (cfg->verbose_level > 2))
13581 mono_print_ins (ins);
13583 if (ins->opcode == OP_NOP)
13587 * We handle LDADDR here as well, since it can only be decomposed
13588 * when variable addresses are known.
13590 if (ins->opcode == OP_LDADDR) {
13591 MonoInst *var = (MonoInst *)ins->inst_p0;
13593 if (var->opcode == OP_VTARG_ADDR) {
13594 /* Happens on SPARC/S390 where vtypes are passed by reference */
13595 MonoInst *vtaddr = var->inst_left;
13596 if (vtaddr->opcode == OP_REGVAR) {
13597 ins->opcode = OP_MOVE;
13598 ins->sreg1 = vtaddr->dreg;
13600 else if (var->inst_left->opcode == OP_REGOFFSET) {
13601 ins->opcode = OP_LOAD_MEMBASE;
13602 ins->inst_basereg = vtaddr->inst_basereg;
13603 ins->inst_offset = vtaddr->inst_offset;
13606 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
13607 /* gsharedvt arg passed by ref */
13608 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13610 ins->opcode = OP_LOAD_MEMBASE;
13611 ins->inst_basereg = var->inst_basereg;
13612 ins->inst_offset = var->inst_offset;
13613 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
13614 MonoInst *load, *load2, *load3;
13615 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
13616 int reg1, reg2, reg3;
13617 MonoInst *info_var = cfg->gsharedvt_info_var;
13618 MonoInst *locals_var = cfg->gsharedvt_locals_var;
13622 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13625 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13627 g_assert (info_var);
13628 g_assert (locals_var);
13630 /* Mark the instruction used to compute the locals var as used */
13631 cfg->gsharedvt_locals_var_ins = NULL;
13633 /* Load the offset */
13634 if (info_var->opcode == OP_REGOFFSET) {
13635 reg1 = alloc_ireg (cfg);
13636 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13637 } else if (info_var->opcode == OP_REGVAR) {
13639 reg1 = info_var->dreg;
13641 g_assert_not_reached ();
13643 reg2 = alloc_ireg (cfg);
13644 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13645 /* Load the locals area address */
13646 reg3 = alloc_ireg (cfg);
13647 if (locals_var->opcode == OP_REGOFFSET) {
13648 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
13649 } else if (locals_var->opcode == OP_REGVAR) {
13650 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
13652 g_assert_not_reached ();
13654 /* Compute the address */
13655 ins->opcode = OP_PADD;
13659 mono_bblock_insert_before_ins (bb, ins, load3);
13660 mono_bblock_insert_before_ins (bb, load3, load2);
13662 mono_bblock_insert_before_ins (bb, load2, load);
13664 g_assert (var->opcode == OP_REGOFFSET);
13666 ins->opcode = OP_ADD_IMM;
13667 ins->sreg1 = var->inst_basereg;
13668 ins->inst_imm = var->inst_offset;
13671 *need_local_opts = TRUE;
13672 spec = INS_INFO (ins->opcode);
13675 if (ins->opcode < MONO_CEE_LAST) {
13676 mono_print_ins (ins);
13677 g_assert_not_reached ();
13681 * Store opcodes have destbasereg in the dreg, but in reality, it is an
13685 if (MONO_IS_STORE_MEMBASE (ins)) {
13686 tmp_reg = ins->dreg;
13687 ins->dreg = ins->sreg2;
13688 ins->sreg2 = tmp_reg;
13691 spec2 [MONO_INST_DEST] = ' ';
13692 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13693 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13694 spec2 [MONO_INST_SRC3] = ' ';
13696 } else if (MONO_IS_STORE_MEMINDEX (ins))
13697 g_assert_not_reached ();
13702 if (G_UNLIKELY (cfg->verbose_level > 2)) {
13703 printf ("\t %.3s %d", spec, ins->dreg);
13704 num_sregs = mono_inst_get_src_registers (ins, sregs);
13705 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
13706 printf (" %d", sregs [srcindex]);
13713 regtype = spec [MONO_INST_DEST];
13714 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
13717 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
13718 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
13719 MonoInst *store_ins;
13721 MonoInst *def_ins = ins;
13722 int dreg = ins->dreg; /* The original vreg */
13724 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
13726 if (var->opcode == OP_REGVAR) {
13727 ins->dreg = var->dreg;
13728 } 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)) {
13730 * Instead of emitting a load+store, use a _membase opcode.
13732 g_assert (var->opcode == OP_REGOFFSET);
13733 if (ins->opcode == OP_MOVE) {
13737 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
13738 ins->inst_basereg = var->inst_basereg;
13739 ins->inst_offset = var->inst_offset;
13742 spec = INS_INFO (ins->opcode);
13746 g_assert (var->opcode == OP_REGOFFSET);
13748 prev_dreg = ins->dreg;
13750 /* Invalidate any previous lvreg for this vreg */
13751 vreg_to_lvreg [ins->dreg] = 0;
13755 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
13757 store_opcode = OP_STOREI8_MEMBASE_REG;
13760 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
13762 #if SIZEOF_REGISTER != 8
13763 if (regtype == 'l') {
13764 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, MONO_LVREG_LS (ins->dreg));
13765 mono_bblock_insert_after_ins (bb, ins, store_ins);
13766 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, MONO_LVREG_MS (ins->dreg));
13767 mono_bblock_insert_after_ins (bb, ins, store_ins);
13768 def_ins = store_ins;
13773 g_assert (store_opcode != OP_STOREV_MEMBASE);
13775 /* Try to fuse the store into the instruction itself */
13776 /* FIXME: Add more instructions */
13777 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
13778 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
13779 ins->inst_imm = ins->inst_c0;
13780 ins->inst_destbasereg = var->inst_basereg;
13781 ins->inst_offset = var->inst_offset;
13782 spec = INS_INFO (ins->opcode);
13783 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
13784 ins->opcode = store_opcode;
13785 ins->inst_destbasereg = var->inst_basereg;
13786 ins->inst_offset = var->inst_offset;
13790 tmp_reg = ins->dreg;
13791 ins->dreg = ins->sreg2;
13792 ins->sreg2 = tmp_reg;
13795 spec2 [MONO_INST_DEST] = ' ';
13796 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13797 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13798 spec2 [MONO_INST_SRC3] = ' ';
13800 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
13801 // FIXME: The backends expect the base reg to be in inst_basereg
13802 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
13804 ins->inst_basereg = var->inst_basereg;
13805 ins->inst_offset = var->inst_offset;
13806 spec = INS_INFO (ins->opcode);
13808 /* printf ("INS: "); mono_print_ins (ins); */
13809 /* Create a store instruction */
13810 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
13812 /* Insert it after the instruction */
13813 mono_bblock_insert_after_ins (bb, ins, store_ins);
13815 def_ins = store_ins;
13818 * We can't assign ins->dreg to var->dreg here, since the
13819 * sregs could use it. So set a flag, and do it after
13822 if ((!cfg->backend->use_fpstack || ((store_opcode != OP_STORER8_MEMBASE_REG) && (store_opcode != OP_STORER4_MEMBASE_REG))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
13823 dest_has_lvreg = TRUE;
13828 if (def_ins && !live_range_start [dreg]) {
13829 live_range_start [dreg] = def_ins;
13830 live_range_start_bb [dreg] = bb;
13833 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
13836 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
13837 tmp->inst_c1 = dreg;
13838 mono_bblock_insert_after_ins (bb, def_ins, tmp);
13845 num_sregs = mono_inst_get_src_registers (ins, sregs);
13846 for (srcindex = 0; srcindex < 3; ++srcindex) {
13847 regtype = spec [MONO_INST_SRC1 + srcindex];
13848 sreg = sregs [srcindex];
13850 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
13851 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
13852 MonoInst *var = get_vreg_to_inst (cfg, sreg);
13853 MonoInst *use_ins = ins;
13854 MonoInst *load_ins;
13855 guint32 load_opcode;
13857 if (var->opcode == OP_REGVAR) {
13858 sregs [srcindex] = var->dreg;
13859 //mono_inst_set_src_registers (ins, sregs);
13860 live_range_end [sreg] = use_ins;
13861 live_range_end_bb [sreg] = bb;
13863 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
13866 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
13867 /* var->dreg is a hreg */
13868 tmp->inst_c1 = sreg;
13869 mono_bblock_insert_after_ins (bb, ins, tmp);
13875 g_assert (var->opcode == OP_REGOFFSET);
13877 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
13879 g_assert (load_opcode != OP_LOADV_MEMBASE);
13881 if (vreg_to_lvreg [sreg]) {
13882 g_assert (vreg_to_lvreg [sreg] != -1);
13884 /* The variable is already loaded to an lvreg */
13885 if (G_UNLIKELY (cfg->verbose_level > 2))
13886 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
13887 sregs [srcindex] = vreg_to_lvreg [sreg];
13888 //mono_inst_set_src_registers (ins, sregs);
13892 /* Try to fuse the load into the instruction */
13893 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
13894 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
13895 sregs [0] = var->inst_basereg;
13896 //mono_inst_set_src_registers (ins, sregs);
13897 ins->inst_offset = var->inst_offset;
13898 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
13899 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
13900 sregs [1] = var->inst_basereg;
13901 //mono_inst_set_src_registers (ins, sregs);
13902 ins->inst_offset = var->inst_offset;
13904 if (MONO_IS_REAL_MOVE (ins)) {
13905 ins->opcode = OP_NOP;
13908 //printf ("%d ", srcindex); mono_print_ins (ins);
13910 sreg = alloc_dreg (cfg, stacktypes [regtype]);
13912 if ((!cfg->backend->use_fpstack || ((load_opcode != OP_LOADR8_MEMBASE) && (load_opcode != OP_LOADR4_MEMBASE))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && !no_lvreg) {
13913 if (var->dreg == prev_dreg) {
13915 * sreg refers to the value loaded by the load
13916 * emitted below, but we need to use ins->dreg
13917 * since it refers to the store emitted earlier.
13921 g_assert (sreg != -1);
13922 vreg_to_lvreg [var->dreg] = sreg;
13923 if (lvregs_len >= lvregs_size) {
13924 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
13925 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
13926 lvregs = new_lvregs;
13929 lvregs [lvregs_len ++] = var->dreg;
13933 sregs [srcindex] = sreg;
13934 //mono_inst_set_src_registers (ins, sregs);
13936 #if SIZEOF_REGISTER != 8
13937 if (regtype == 'l') {
13938 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
13939 mono_bblock_insert_before_ins (bb, ins, load_ins);
13940 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
13941 mono_bblock_insert_before_ins (bb, ins, load_ins);
13942 use_ins = load_ins;
13947 #if SIZEOF_REGISTER == 4
13948 g_assert (load_opcode != OP_LOADI8_MEMBASE);
13950 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
13951 mono_bblock_insert_before_ins (bb, ins, load_ins);
13952 use_ins = load_ins;
13956 if (var->dreg < orig_next_vreg) {
13957 live_range_end [var->dreg] = use_ins;
13958 live_range_end_bb [var->dreg] = bb;
13961 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
13964 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
13965 tmp->inst_c1 = var->dreg;
13966 mono_bblock_insert_after_ins (bb, ins, tmp);
13970 mono_inst_set_src_registers (ins, sregs);
13972 if (dest_has_lvreg) {
13973 g_assert (ins->dreg != -1);
13974 vreg_to_lvreg [prev_dreg] = ins->dreg;
13975 if (lvregs_len >= lvregs_size) {
13976 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
13977 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
13978 lvregs = new_lvregs;
13981 lvregs [lvregs_len ++] = prev_dreg;
13982 dest_has_lvreg = FALSE;
13986 tmp_reg = ins->dreg;
13987 ins->dreg = ins->sreg2;
13988 ins->sreg2 = tmp_reg;
13991 if (MONO_IS_CALL (ins)) {
13992 /* Clear vreg_to_lvreg array */
13993 for (i = 0; i < lvregs_len; i++)
13994 vreg_to_lvreg [lvregs [i]] = 0;
13996 } else if (ins->opcode == OP_NOP) {
13998 MONO_INST_NULLIFY_SREGS (ins);
14001 if (cfg->verbose_level > 2)
14002 mono_print_ins_index (1, ins);
14005 /* Extend the live range based on the liveness info */
14006 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14007 for (i = 0; i < cfg->num_varinfo; i ++) {
14008 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14010 if (vreg_is_volatile (cfg, vi->vreg))
14011 /* The liveness info is incomplete */
14014 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14015 /* Live from at least the first ins of this bb */
14016 live_range_start [vi->vreg] = bb->code;
14017 live_range_start_bb [vi->vreg] = bb;
14020 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14021 /* Live at least until the last ins of this bb */
14022 live_range_end [vi->vreg] = bb->last_ins;
14023 live_range_end_bb [vi->vreg] = bb;
14030 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14031 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14033 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14034 for (i = 0; i < cfg->num_varinfo; ++i) {
14035 int vreg = MONO_VARINFO (cfg, i)->vreg;
14038 if (live_range_start [vreg]) {
14039 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14041 ins->inst_c1 = vreg;
14042 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14044 if (live_range_end [vreg]) {
14045 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14047 ins->inst_c1 = vreg;
14048 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14049 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14051 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14056 if (cfg->gsharedvt_locals_var_ins) {
14057 /* Nullify if unused */
14058 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14059 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14062 g_free (live_range_start);
14063 g_free (live_range_end);
14064 g_free (live_range_start_bb);
14065 g_free (live_range_end_bb);
14071 * - use 'iadd' instead of 'int_add'
14072 * - handling ovf opcodes: decompose in method_to_ir.
14073 * - unify iregs/fregs
14074 * -> partly done, the missing parts are:
14075 * - a more complete unification would involve unifying the hregs as well, so
14076 * code wouldn't need if (fp) all over the place. but that would mean the hregs
14077 * would no longer map to the machine hregs, so the code generators would need to
14078 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14079 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14080 * fp/non-fp branches speeds it up by about 15%.
14081 * - use sext/zext opcodes instead of shifts
14083 * - get rid of TEMPLOADs if possible and use vregs instead
14084 * - clean up usage of OP_P/OP_ opcodes
14085 * - cleanup usage of DUMMY_USE
14086 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
14088 * - set the stack type and allocate a dreg in the EMIT_NEW macros
14089 * - get rid of all the <foo>2 stuff when the new JIT is ready.
14090 * - make sure handle_stack_args () is called before the branch is emitted
14091 * - when the new IR is done, get rid of all unused stuff
14092 * - COMPARE/BEQ as separate instructions or unify them ?
14093 * - keeping them separate allows specialized compare instructions like
14094 * compare_imm, compare_membase
14095 * - most back ends unify fp compare+branch, fp compare+ceq
14096 * - integrate mono_save_args into inline_method
14097 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14098 * - handle long shift opts on 32 bit platforms somehow: they require
14099 * 3 sregs (2 for arg1 and 1 for arg2)
14100 * - make byref a 'normal' type.
14101 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14102 * variable if needed.
14103 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14104 * like inline_method.
14105 * - remove inlining restrictions
14106 * - fix LNEG and enable cfold of INEG
14107 * - generalize x86 optimizations like ldelema as a peephole optimization
14108 * - add store_mem_imm for amd64
14109 * - optimize the loading of the interruption flag in the managed->native wrappers
14110 * - avoid special handling of OP_NOP in passes
14111 * - move code inserting instructions into one function/macro.
14112 * - try a coalescing phase after liveness analysis
14113 * - add float -> vreg conversion + local optimizations on !x86
14114 * - figure out how to handle decomposed branches during optimizations, ie.
14115 * compare+branch, op_jump_table+op_br etc.
14116 * - promote RuntimeXHandles to vregs
14117 * - vtype cleanups:
14118 * - add a NEW_VARLOADA_VREG macro
14119 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
14120 * accessing vtype fields.
14121 * - get rid of I8CONST on 64 bit platforms
14122 * - dealing with the increase in code size due to branches created during opcode
14124 * - use extended basic blocks
14125 * - all parts of the JIT
14126 * - handle_global_vregs () && local regalloc
14127 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14128 * - sources of increase in code size:
14131 * - isinst and castclass
14132 * - lvregs not allocated to global registers even if used multiple times
14133 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14135 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14136 * - add all micro optimizations from the old JIT
14137 * - put tree optimizations into the deadce pass
14138 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14139 * specific function.
14140 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14141 * fcompare + branchCC.
14142 * - create a helper function for allocating a stack slot, taking into account
14143 * MONO_CFG_HAS_SPILLUP.
14145 * - optimize mono_regstate2_alloc_int/float.
14146 * - fix the pessimistic handling of variables accessed in exception handler blocks.
14147 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14148 * parts of the tree could be separated by other instructions, killing the tree
14149 * arguments, or stores killing loads etc. Also, should we fold loads into other
14150 * instructions if the result of the load is used multiple times ?
14151 * - make the REM_IMM optimization in mini-x86.c arch-independent.
14152 * - LAST MERGE: 108395.
14153 * - when returning vtypes in registers, generate IR and append it to the end of the
14154 * last bb instead of doing it in the epilog.
14155 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14163 - When to decompose opcodes:
14164 - earlier: this makes some optimizations hard to implement, since the low level IR
14165 no longer contains the neccessary information. But it is easier to do.
14166 - later: harder to implement, enables more optimizations.
14167 - Branches inside bblocks:
14168 - created when decomposing complex opcodes.
14169 - branches to another bblock: harmless, but not tracked by the branch
14170 optimizations, so need to branch to a label at the start of the bblock.
14171 - branches to inside the same bblock: very problematic, trips up the local
14172 reg allocator. Can be fixed by spitting the current bblock, but that is a
14173 complex operation, since some local vregs can become global vregs etc.
14174 - Local/global vregs:
14175 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14176 local register allocator.
14177 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14178 structure, created by mono_create_var (). Assigned to hregs or the stack by
14179 the global register allocator.
14180 - When to do optimizations like alu->alu_imm:
14181 - earlier -> saves work later on since the IR will be smaller/simpler
14182 - later -> can work on more instructions
14183 - Handling of valuetypes:
14184 - When a vtype is pushed on the stack, a new temporary is created, an
14185 instruction computing its address (LDADDR) is emitted and pushed on
14186 the stack. Need to optimize cases when the vtype is used immediately as in
14187 argument passing, stloc etc.
14188 - Instead of the to_end stuff in the old JIT, simply call the function handling
14189 the values on the stack before emitting the last instruction of the bb.
14192 #endif /* !DISABLE_JIT */