2 * method-to-ir.c: Convert CIL to the JIT internal representation
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <mono/utils/mono-compiler.h>
30 #ifdef HAVE_SYS_TIME_H
38 #include <mono/utils/memcheck.h>
39 #include <mono/metadata/abi-details.h>
40 #include <mono/metadata/assembly.h>
41 #include <mono/metadata/attrdefs.h>
42 #include <mono/metadata/loader.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/class.h>
45 #include <mono/metadata/object.h>
46 #include <mono/metadata/exception.h>
47 #include <mono/metadata/opcodes.h>
48 #include <mono/metadata/mono-endian.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/marshal.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/debug-internals.h>
54 #include <mono/metadata/gc-internals.h>
55 #include <mono/metadata/security-manager.h>
56 #include <mono/metadata/threads-types.h>
57 #include <mono/metadata/security-core-clr.h>
58 #include <mono/metadata/profiler-private.h>
59 #include <mono/metadata/profiler.h>
60 #include <mono/metadata/monitor.h>
61 #include <mono/utils/mono-memory-model.h>
62 #include <mono/utils/mono-error-internals.h>
63 #include <mono/metadata/mono-basic-block.h>
64 #include <mono/metadata/reflection-internals.h>
65 #include <mono/utils/mono-threads-coop.h>
71 #include "jit-icalls.h"
73 #include "debugger-agent.h"
74 #include "seq-points.h"
75 #include "aot-compiler.h"
76 #include "mini-llvm.h"
78 #define BRANCH_COST 10
79 #define INLINE_LENGTH_LIMIT 20
81 /* These have 'cfg' as an implicit argument */
82 #define INLINE_FAILURE(msg) do { \
83 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
84 inline_failure (cfg, msg); \
85 goto exception_exit; \
88 #define CHECK_CFG_EXCEPTION do {\
89 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
90 goto exception_exit; \
92 #define FIELD_ACCESS_FAILURE(method, field) do { \
93 field_access_failure ((cfg), (method), (field)); \
94 goto exception_exit; \
96 #define GENERIC_SHARING_FAILURE(opcode) do { \
98 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
99 goto exception_exit; \
102 #define GSHAREDVT_FAILURE(opcode) do { \
103 if (cfg->gsharedvt) { \
104 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
105 goto exception_exit; \
108 #define OUT_OF_MEMORY_FAILURE do { \
109 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
110 mono_error_set_out_of_memory (&cfg->error, ""); \
111 goto exception_exit; \
113 #define DISABLE_AOT(cfg) do { \
114 if ((cfg)->verbose_level >= 2) \
115 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
116 (cfg)->disable_aot = TRUE; \
118 #define LOAD_ERROR do { \
119 break_on_unverified (); \
120 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
121 goto exception_exit; \
124 #define TYPE_LOAD_ERROR(klass) do { \
125 cfg->exception_ptr = klass; \
129 #define CHECK_CFG_ERROR do {\
130 if (!mono_error_ok (&cfg->error)) { \
131 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
132 goto mono_error_exit; \
136 /* Determine whenever 'ins' represents a load of the 'this' argument */
137 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
139 static int ldind_to_load_membase (int opcode);
140 static int stind_to_store_membase (int opcode);
142 int mono_op_to_op_imm (int opcode);
143 int mono_op_to_op_imm_noemul (int opcode);
145 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
146 guchar *ip, guint real_offset, gboolean inline_always);
148 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
150 inline static MonoInst*
151 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg);
153 /* helper methods signatures */
154 static MonoMethodSignature *helper_sig_domain_get;
155 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
156 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
157 static MonoMethodSignature *helper_sig_jit_thread_attach;
158 static MonoMethodSignature *helper_sig_get_tls_tramp;
159 static MonoMethodSignature *helper_sig_set_tls_tramp;
161 /* type loading helpers */
162 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, "System.Runtime.CompilerServices", "RuntimeHelpers")
163 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
166 * Instruction metadata
174 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
175 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
181 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
186 /* keep in sync with the enum in mini.h */
189 #include "mini-ops.h"
194 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
195 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
197 * This should contain the index of the last sreg + 1. This is not the same
198 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
200 const gint8 ins_sreg_counts[] = {
201 #include "mini-ops.h"
207 mono_alloc_ireg (MonoCompile *cfg)
209 return alloc_ireg (cfg);
213 mono_alloc_lreg (MonoCompile *cfg)
215 return alloc_lreg (cfg);
219 mono_alloc_freg (MonoCompile *cfg)
221 return alloc_freg (cfg);
225 mono_alloc_preg (MonoCompile *cfg)
227 return alloc_preg (cfg);
231 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
233 return alloc_dreg (cfg, stack_type);
237 * mono_alloc_ireg_ref:
239 * Allocate an IREG, and mark it as holding a GC ref.
242 mono_alloc_ireg_ref (MonoCompile *cfg)
244 return alloc_ireg_ref (cfg);
248 * mono_alloc_ireg_mp:
250 * Allocate an IREG, and mark it as holding a managed pointer.
253 mono_alloc_ireg_mp (MonoCompile *cfg)
255 return alloc_ireg_mp (cfg);
259 * mono_alloc_ireg_copy:
261 * Allocate an IREG with the same GC type as VREG.
264 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
266 if (vreg_is_ref (cfg, vreg))
267 return alloc_ireg_ref (cfg);
268 else if (vreg_is_mp (cfg, vreg))
269 return alloc_ireg_mp (cfg);
271 return alloc_ireg (cfg);
275 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
280 type = mini_get_underlying_type (type);
282 switch (type->type) {
295 case MONO_TYPE_FNPTR:
297 case MONO_TYPE_CLASS:
298 case MONO_TYPE_STRING:
299 case MONO_TYPE_OBJECT:
300 case MONO_TYPE_SZARRAY:
301 case MONO_TYPE_ARRAY:
305 #if SIZEOF_REGISTER == 8
311 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
314 case MONO_TYPE_VALUETYPE:
315 if (type->data.klass->enumtype) {
316 type = mono_class_enum_basetype (type->data.klass);
319 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
322 case MONO_TYPE_TYPEDBYREF:
324 case MONO_TYPE_GENERICINST:
325 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
327 type = &type->data.generic_class->container_class->byval_arg;
331 g_assert (cfg->gshared);
332 if (mini_type_var_is_vt (type))
335 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
337 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
343 mono_print_bb (MonoBasicBlock *bb, const char *msg)
347 GString *str = g_string_new ("");
349 g_string_append_printf (str, "%s %d: [IN: ", msg, bb->block_num);
350 for (i = 0; i < bb->in_count; ++i)
351 g_string_append_printf (str, " BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
352 g_string_append_printf (str, ", OUT: ");
353 for (i = 0; i < bb->out_count; ++i)
354 g_string_append_printf (str, " BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
355 g_string_append_printf (str, " ]\n");
357 g_print ("%s", str->str);
358 g_string_free (str, TRUE);
360 for (tree = bb->code; tree; tree = tree->next)
361 mono_print_ins_index (-1, tree);
365 mono_create_helper_signatures (void)
367 helper_sig_domain_get = mono_create_icall_signature ("ptr");
368 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
369 helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
370 helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
371 helper_sig_get_tls_tramp = mono_create_icall_signature ("ptr");
372 helper_sig_set_tls_tramp = mono_create_icall_signature ("void ptr");
375 static MONO_NEVER_INLINE void
376 break_on_unverified (void)
378 if (mini_get_debug_options ()->break_on_unverified)
382 static MONO_NEVER_INLINE void
383 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
385 char *method_fname = mono_method_full_name (method, TRUE);
386 char *field_fname = mono_field_full_name (field);
387 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
388 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
389 g_free (method_fname);
390 g_free (field_fname);
393 static MONO_NEVER_INLINE void
394 inline_failure (MonoCompile *cfg, const char *msg)
396 if (cfg->verbose_level >= 2)
397 printf ("inline failed: %s\n", msg);
398 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
401 static MONO_NEVER_INLINE void
402 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
404 if (cfg->verbose_level > 2) \
405 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);
406 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
409 static MONO_NEVER_INLINE void
410 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
412 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);
413 if (cfg->verbose_level >= 2)
414 printf ("%s\n", cfg->exception_message);
415 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
419 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
420 * foo<T> (int i) { ldarg.0; box T; }
422 #define UNVERIFIED do { \
423 if (cfg->gsharedvt) { \
424 if (cfg->verbose_level > 2) \
425 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
426 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
427 goto exception_exit; \
429 break_on_unverified (); \
433 #define GET_BBLOCK(cfg,tblock,ip) do { \
434 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
436 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
437 NEW_BBLOCK (cfg, (tblock)); \
438 (tblock)->cil_code = (ip); \
439 ADD_BBLOCK (cfg, (tblock)); \
443 #if defined(TARGET_X86) || defined(TARGET_AMD64)
444 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
445 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
446 (dest)->dreg = alloc_ireg_mp ((cfg)); \
447 (dest)->sreg1 = (sr1); \
448 (dest)->sreg2 = (sr2); \
449 (dest)->inst_imm = (imm); \
450 (dest)->backend.shift_amount = (shift); \
451 MONO_ADD_INS ((cfg)->cbb, (dest)); \
455 /* Emit conversions so both operands of a binary opcode are of the same type */
457 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
459 MonoInst *arg1 = *arg1_ref;
460 MonoInst *arg2 = *arg2_ref;
463 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
464 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
467 /* Mixing r4/r8 is allowed by the spec */
468 if (arg1->type == STACK_R4) {
469 int dreg = alloc_freg (cfg);
471 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
472 conv->type = STACK_R8;
476 if (arg2->type == STACK_R4) {
477 int dreg = alloc_freg (cfg);
479 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
480 conv->type = STACK_R8;
486 #if SIZEOF_REGISTER == 8
487 /* FIXME: Need to add many more cases */
488 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
491 int dr = alloc_preg (cfg);
492 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
493 (ins)->sreg2 = widen->dreg;
498 #define ADD_BINOP(op) do { \
499 MONO_INST_NEW (cfg, ins, (op)); \
501 ins->sreg1 = sp [0]->dreg; \
502 ins->sreg2 = sp [1]->dreg; \
503 type_from_op (cfg, ins, sp [0], sp [1]); \
505 /* Have to insert a widening op */ \
506 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
507 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
508 MONO_ADD_INS ((cfg)->cbb, (ins)); \
509 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
512 #define ADD_UNOP(op) do { \
513 MONO_INST_NEW (cfg, ins, (op)); \
515 ins->sreg1 = sp [0]->dreg; \
516 type_from_op (cfg, ins, sp [0], NULL); \
518 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
519 MONO_ADD_INS ((cfg)->cbb, (ins)); \
520 *sp++ = mono_decompose_opcode (cfg, ins); \
523 #define ADD_BINCOND(next_block) do { \
526 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
527 cmp->sreg1 = sp [0]->dreg; \
528 cmp->sreg2 = sp [1]->dreg; \
529 type_from_op (cfg, cmp, sp [0], sp [1]); \
531 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
532 type_from_op (cfg, ins, sp [0], sp [1]); \
533 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
534 GET_BBLOCK (cfg, tblock, target); \
535 link_bblock (cfg, cfg->cbb, tblock); \
536 ins->inst_true_bb = tblock; \
537 if ((next_block)) { \
538 link_bblock (cfg, cfg->cbb, (next_block)); \
539 ins->inst_false_bb = (next_block); \
540 start_new_bblock = 1; \
542 GET_BBLOCK (cfg, tblock, ip); \
543 link_bblock (cfg, cfg->cbb, tblock); \
544 ins->inst_false_bb = tblock; \
545 start_new_bblock = 2; \
547 if (sp != stack_start) { \
548 handle_stack_args (cfg, stack_start, sp - stack_start); \
549 CHECK_UNVERIFIABLE (cfg); \
551 MONO_ADD_INS (cfg->cbb, cmp); \
552 MONO_ADD_INS (cfg->cbb, ins); \
556 * link_bblock: Links two basic blocks
558 * links two basic blocks in the control flow graph, the 'from'
559 * argument is the starting block and the 'to' argument is the block
560 * the control flow ends to after 'from'.
563 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
565 MonoBasicBlock **newa;
569 if (from->cil_code) {
571 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
573 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
576 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
578 printf ("edge from entry to exit\n");
583 for (i = 0; i < from->out_count; ++i) {
584 if (to == from->out_bb [i]) {
590 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
591 for (i = 0; i < from->out_count; ++i) {
592 newa [i] = from->out_bb [i];
600 for (i = 0; i < to->in_count; ++i) {
601 if (from == to->in_bb [i]) {
607 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
608 for (i = 0; i < to->in_count; ++i) {
609 newa [i] = to->in_bb [i];
618 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
620 link_bblock (cfg, from, to);
624 * mono_find_block_region:
626 * We mark each basic block with a region ID. We use that to avoid BB
627 * optimizations when blocks are in different regions.
630 * A region token that encodes where this region is, and information
631 * about the clause owner for this block.
633 * The region encodes the try/catch/filter clause that owns this block
634 * as well as the type. -1 is a special value that represents a block
635 * that is in none of try/catch/filter.
638 mono_find_block_region (MonoCompile *cfg, int offset)
640 MonoMethodHeader *header = cfg->header;
641 MonoExceptionClause *clause;
644 for (i = 0; i < header->num_clauses; ++i) {
645 clause = &header->clauses [i];
646 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
647 (offset < (clause->handler_offset)))
648 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
650 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
651 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
652 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
653 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
654 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
656 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
659 for (i = 0; i < header->num_clauses; ++i) {
660 clause = &header->clauses [i];
662 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
663 return ((i + 1) << 8) | clause->flags;
670 ip_in_finally_clause (MonoCompile *cfg, int offset)
672 MonoMethodHeader *header = cfg->header;
673 MonoExceptionClause *clause;
676 for (i = 0; i < header->num_clauses; ++i) {
677 clause = &header->clauses [i];
678 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
681 if (MONO_OFFSET_IN_HANDLER (clause, offset))
688 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
690 MonoMethodHeader *header = cfg->header;
691 MonoExceptionClause *clause;
695 for (i = 0; i < header->num_clauses; ++i) {
696 clause = &header->clauses [i];
697 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
698 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
699 if (clause->flags == type)
700 res = g_list_append (res, clause);
707 mono_create_spvar_for_region (MonoCompile *cfg, int region)
711 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
715 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
716 /* prevent it from being register allocated */
717 var->flags |= MONO_INST_VOLATILE;
719 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
723 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
725 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
729 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
733 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
737 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
738 /* prevent it from being register allocated */
739 var->flags |= MONO_INST_VOLATILE;
741 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
747 * Returns the type used in the eval stack when @type is loaded.
748 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
751 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
755 type = mini_get_underlying_type (type);
756 inst->klass = klass = mono_class_from_mono_type (type);
758 inst->type = STACK_MP;
763 switch (type->type) {
765 inst->type = STACK_INV;
773 inst->type = STACK_I4;
778 case MONO_TYPE_FNPTR:
779 inst->type = STACK_PTR;
781 case MONO_TYPE_CLASS:
782 case MONO_TYPE_STRING:
783 case MONO_TYPE_OBJECT:
784 case MONO_TYPE_SZARRAY:
785 case MONO_TYPE_ARRAY:
786 inst->type = STACK_OBJ;
790 inst->type = STACK_I8;
793 inst->type = cfg->r4_stack_type;
796 inst->type = STACK_R8;
798 case MONO_TYPE_VALUETYPE:
799 if (type->data.klass->enumtype) {
800 type = mono_class_enum_basetype (type->data.klass);
804 inst->type = STACK_VTYPE;
807 case MONO_TYPE_TYPEDBYREF:
808 inst->klass = mono_defaults.typed_reference_class;
809 inst->type = STACK_VTYPE;
811 case MONO_TYPE_GENERICINST:
812 type = &type->data.generic_class->container_class->byval_arg;
816 g_assert (cfg->gshared);
817 if (mini_is_gsharedvt_type (type)) {
818 g_assert (cfg->gsharedvt);
819 inst->type = STACK_VTYPE;
821 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
825 g_error ("unknown type 0x%02x in eval stack type", type->type);
830 * The following tables are used to quickly validate the IL code in type_from_op ().
833 bin_num_table [STACK_MAX] [STACK_MAX] = {
834 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
835 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
836 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
837 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
838 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
839 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
840 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
841 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
842 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
847 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
850 /* reduce the size of this table */
852 bin_int_table [STACK_MAX] [STACK_MAX] = {
853 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
854 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
855 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
859 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
860 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
864 bin_comp_table [STACK_MAX] [STACK_MAX] = {
865 /* Inv i L p F & O vt r4 */
867 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
868 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
869 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
870 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
871 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
872 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
873 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
874 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
877 /* reduce the size of this table */
879 shift_table [STACK_MAX] [STACK_MAX] = {
880 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
881 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
882 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
883 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
884 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
885 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
886 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
887 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
891 * Tables to map from the non-specific opcode to the matching
892 * type-specific opcode.
894 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
896 binops_op_map [STACK_MAX] = {
897 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
900 /* handles from CEE_NEG to CEE_CONV_U8 */
902 unops_op_map [STACK_MAX] = {
903 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
906 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
908 ovfops_op_map [STACK_MAX] = {
909 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
912 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
914 ovf2ops_op_map [STACK_MAX] = {
915 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
918 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
920 ovf3ops_op_map [STACK_MAX] = {
921 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
924 /* handles from CEE_BEQ to CEE_BLT_UN */
926 beqops_op_map [STACK_MAX] = {
927 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
930 /* handles from CEE_CEQ to CEE_CLT_UN */
932 ceqops_op_map [STACK_MAX] = {
933 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
937 * Sets ins->type (the type on the eval stack) according to the
938 * type of the opcode and the arguments to it.
939 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
941 * FIXME: this function sets ins->type unconditionally in some cases, but
942 * it should set it to invalid for some types (a conv.x on an object)
945 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
947 switch (ins->opcode) {
954 /* FIXME: check unverifiable args for STACK_MP */
955 ins->type = bin_num_table [src1->type] [src2->type];
956 ins->opcode += binops_op_map [ins->type];
963 ins->type = bin_int_table [src1->type] [src2->type];
964 ins->opcode += binops_op_map [ins->type];
969 ins->type = shift_table [src1->type] [src2->type];
970 ins->opcode += binops_op_map [ins->type];
975 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
976 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
977 ins->opcode = OP_LCOMPARE;
978 else if (src1->type == STACK_R4)
979 ins->opcode = OP_RCOMPARE;
980 else if (src1->type == STACK_R8)
981 ins->opcode = OP_FCOMPARE;
983 ins->opcode = OP_ICOMPARE;
985 case OP_ICOMPARE_IMM:
986 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
987 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
988 ins->opcode = OP_LCOMPARE_IMM;
1000 ins->opcode += beqops_op_map [src1->type];
1003 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1004 ins->opcode += ceqops_op_map [src1->type];
1010 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1011 ins->opcode += ceqops_op_map [src1->type];
1015 ins->type = neg_table [src1->type];
1016 ins->opcode += unops_op_map [ins->type];
1019 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1020 ins->type = src1->type;
1022 ins->type = STACK_INV;
1023 ins->opcode += unops_op_map [ins->type];
1029 ins->type = STACK_I4;
1030 ins->opcode += unops_op_map [src1->type];
1033 ins->type = STACK_R8;
1034 switch (src1->type) {
1037 ins->opcode = OP_ICONV_TO_R_UN;
1040 ins->opcode = OP_LCONV_TO_R_UN;
1044 case CEE_CONV_OVF_I1:
1045 case CEE_CONV_OVF_U1:
1046 case CEE_CONV_OVF_I2:
1047 case CEE_CONV_OVF_U2:
1048 case CEE_CONV_OVF_I4:
1049 case CEE_CONV_OVF_U4:
1050 ins->type = STACK_I4;
1051 ins->opcode += ovf3ops_op_map [src1->type];
1053 case CEE_CONV_OVF_I_UN:
1054 case CEE_CONV_OVF_U_UN:
1055 ins->type = STACK_PTR;
1056 ins->opcode += ovf2ops_op_map [src1->type];
1058 case CEE_CONV_OVF_I1_UN:
1059 case CEE_CONV_OVF_I2_UN:
1060 case CEE_CONV_OVF_I4_UN:
1061 case CEE_CONV_OVF_U1_UN:
1062 case CEE_CONV_OVF_U2_UN:
1063 case CEE_CONV_OVF_U4_UN:
1064 ins->type = STACK_I4;
1065 ins->opcode += ovf2ops_op_map [src1->type];
1068 ins->type = STACK_PTR;
1069 switch (src1->type) {
1071 ins->opcode = OP_ICONV_TO_U;
1075 #if SIZEOF_VOID_P == 8
1076 ins->opcode = OP_LCONV_TO_U;
1078 ins->opcode = OP_MOVE;
1082 ins->opcode = OP_LCONV_TO_U;
1085 ins->opcode = OP_FCONV_TO_U;
1091 ins->type = STACK_I8;
1092 ins->opcode += unops_op_map [src1->type];
1094 case CEE_CONV_OVF_I8:
1095 case CEE_CONV_OVF_U8:
1096 ins->type = STACK_I8;
1097 ins->opcode += ovf3ops_op_map [src1->type];
1099 case CEE_CONV_OVF_U8_UN:
1100 case CEE_CONV_OVF_I8_UN:
1101 ins->type = STACK_I8;
1102 ins->opcode += ovf2ops_op_map [src1->type];
1105 ins->type = cfg->r4_stack_type;
1106 ins->opcode += unops_op_map [src1->type];
1109 ins->type = STACK_R8;
1110 ins->opcode += unops_op_map [src1->type];
1113 ins->type = STACK_R8;
1117 ins->type = STACK_I4;
1118 ins->opcode += ovfops_op_map [src1->type];
1121 case CEE_CONV_OVF_I:
1122 case CEE_CONV_OVF_U:
1123 ins->type = STACK_PTR;
1124 ins->opcode += ovfops_op_map [src1->type];
1127 case CEE_ADD_OVF_UN:
1129 case CEE_MUL_OVF_UN:
1131 case CEE_SUB_OVF_UN:
1132 ins->type = bin_num_table [src1->type] [src2->type];
1133 ins->opcode += ovfops_op_map [src1->type];
1134 if (ins->type == STACK_R8)
1135 ins->type = STACK_INV;
1137 case OP_LOAD_MEMBASE:
1138 ins->type = STACK_PTR;
1140 case OP_LOADI1_MEMBASE:
1141 case OP_LOADU1_MEMBASE:
1142 case OP_LOADI2_MEMBASE:
1143 case OP_LOADU2_MEMBASE:
1144 case OP_LOADI4_MEMBASE:
1145 case OP_LOADU4_MEMBASE:
1146 ins->type = STACK_PTR;
1148 case OP_LOADI8_MEMBASE:
1149 ins->type = STACK_I8;
1151 case OP_LOADR4_MEMBASE:
1152 ins->type = cfg->r4_stack_type;
1154 case OP_LOADR8_MEMBASE:
1155 ins->type = STACK_R8;
1158 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1162 if (ins->type == STACK_MP)
1163 ins->klass = mono_defaults.object_class;
1168 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1174 param_table [STACK_MAX] [STACK_MAX] = {
1179 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1184 switch (args->type) {
1194 for (i = 0; i < sig->param_count; ++i) {
1195 switch (args [i].type) {
1199 if (!sig->params [i]->byref)
1203 if (sig->params [i]->byref)
1205 switch (sig->params [i]->type) {
1206 case MONO_TYPE_CLASS:
1207 case MONO_TYPE_STRING:
1208 case MONO_TYPE_OBJECT:
1209 case MONO_TYPE_SZARRAY:
1210 case MONO_TYPE_ARRAY:
1217 if (sig->params [i]->byref)
1219 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1228 /*if (!param_table [args [i].type] [sig->params [i]->type])
1236 * When we need a pointer to the current domain many times in a method, we
1237 * call mono_domain_get() once and we store the result in a local variable.
1238 * This function returns the variable that represents the MonoDomain*.
1240 inline static MonoInst *
1241 mono_get_domainvar (MonoCompile *cfg)
1243 if (!cfg->domainvar)
1244 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1245 return cfg->domainvar;
1249 * The got_var contains the address of the Global Offset Table when AOT
1253 mono_get_got_var (MonoCompile *cfg)
1255 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1257 if (!cfg->got_var) {
1258 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1260 return cfg->got_var;
1264 mono_get_vtable_var (MonoCompile *cfg)
1266 g_assert (cfg->gshared);
1268 if (!cfg->rgctx_var) {
1269 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1270 /* force the var to be stack allocated */
1271 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1274 return cfg->rgctx_var;
1278 type_from_stack_type (MonoInst *ins) {
1279 switch (ins->type) {
1280 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1281 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1282 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1283 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1284 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1286 return &ins->klass->this_arg;
1287 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1288 case STACK_VTYPE: return &ins->klass->byval_arg;
1290 g_error ("stack type %d to monotype not handled\n", ins->type);
1295 static G_GNUC_UNUSED int
1296 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1298 t = mono_type_get_underlying_type (t);
1310 case MONO_TYPE_FNPTR:
1312 case MONO_TYPE_CLASS:
1313 case MONO_TYPE_STRING:
1314 case MONO_TYPE_OBJECT:
1315 case MONO_TYPE_SZARRAY:
1316 case MONO_TYPE_ARRAY:
1322 return cfg->r4_stack_type;
1325 case MONO_TYPE_VALUETYPE:
1326 case MONO_TYPE_TYPEDBYREF:
1328 case MONO_TYPE_GENERICINST:
1329 if (mono_type_generic_inst_is_valuetype (t))
1335 g_assert_not_reached ();
1342 array_access_to_klass (int opcode)
1346 return mono_defaults.byte_class;
1348 return mono_defaults.uint16_class;
1351 return mono_defaults.int_class;
1354 return mono_defaults.sbyte_class;
1357 return mono_defaults.int16_class;
1360 return mono_defaults.int32_class;
1362 return mono_defaults.uint32_class;
1365 return mono_defaults.int64_class;
1368 return mono_defaults.single_class;
1371 return mono_defaults.double_class;
1372 case CEE_LDELEM_REF:
1373 case CEE_STELEM_REF:
1374 return mono_defaults.object_class;
1376 g_assert_not_reached ();
1382 * We try to share variables when possible
1385 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1390 /* inlining can result in deeper stacks */
1391 if (slot >= cfg->header->max_stack)
1392 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1394 pos = ins->type - 1 + slot * STACK_MAX;
1396 switch (ins->type) {
1403 if ((vnum = cfg->intvars [pos]))
1404 return cfg->varinfo [vnum];
1405 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1406 cfg->intvars [pos] = res->inst_c0;
1409 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1415 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1418 * Don't use this if a generic_context is set, since that means AOT can't
1419 * look up the method using just the image+token.
1420 * table == 0 means this is a reference made from a wrapper.
1422 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1423 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1424 jump_info_token->image = image;
1425 jump_info_token->token = token;
1426 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1431 * This function is called to handle items that are left on the evaluation stack
1432 * at basic block boundaries. What happens is that we save the values to local variables
1433 * and we reload them later when first entering the target basic block (with the
1434 * handle_loaded_temps () function).
1435 * A single joint point will use the same variables (stored in the array bb->out_stack or
1436 * bb->in_stack, if the basic block is before or after the joint point).
1438 * This function needs to be called _before_ emitting the last instruction of
1439 * the bb (i.e. before emitting a branch).
1440 * If the stack merge fails at a join point, cfg->unverifiable is set.
1443 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1446 MonoBasicBlock *bb = cfg->cbb;
1447 MonoBasicBlock *outb;
1448 MonoInst *inst, **locals;
1453 if (cfg->verbose_level > 3)
1454 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1455 if (!bb->out_scount) {
1456 bb->out_scount = count;
1457 //printf ("bblock %d has out:", bb->block_num);
1459 for (i = 0; i < bb->out_count; ++i) {
1460 outb = bb->out_bb [i];
1461 /* exception handlers are linked, but they should not be considered for stack args */
1462 if (outb->flags & BB_EXCEPTION_HANDLER)
1464 //printf (" %d", outb->block_num);
1465 if (outb->in_stack) {
1467 bb->out_stack = outb->in_stack;
1473 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1474 for (i = 0; i < count; ++i) {
1476 * try to reuse temps already allocated for this purpouse, if they occupy the same
1477 * stack slot and if they are of the same type.
1478 * This won't cause conflicts since if 'local' is used to
1479 * store one of the values in the in_stack of a bblock, then
1480 * the same variable will be used for the same outgoing stack
1482 * This doesn't work when inlining methods, since the bblocks
1483 * in the inlined methods do not inherit their in_stack from
1484 * the bblock they are inlined to. See bug #58863 for an
1487 if (cfg->inlined_method)
1488 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1490 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1495 for (i = 0; i < bb->out_count; ++i) {
1496 outb = bb->out_bb [i];
1497 /* exception handlers are linked, but they should not be considered for stack args */
1498 if (outb->flags & BB_EXCEPTION_HANDLER)
1500 if (outb->in_scount) {
1501 if (outb->in_scount != bb->out_scount) {
1502 cfg->unverifiable = TRUE;
1505 continue; /* check they are the same locals */
1507 outb->in_scount = count;
1508 outb->in_stack = bb->out_stack;
1511 locals = bb->out_stack;
1513 for (i = 0; i < count; ++i) {
1514 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1515 inst->cil_code = sp [i]->cil_code;
1516 sp [i] = locals [i];
1517 if (cfg->verbose_level > 3)
1518 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1522 * It is possible that the out bblocks already have in_stack assigned, and
1523 * the in_stacks differ. In this case, we will store to all the different
1530 /* Find a bblock which has a different in_stack */
1532 while (bindex < bb->out_count) {
1533 outb = bb->out_bb [bindex];
1534 /* exception handlers are linked, but they should not be considered for stack args */
1535 if (outb->flags & BB_EXCEPTION_HANDLER) {
1539 if (outb->in_stack != locals) {
1540 for (i = 0; i < count; ++i) {
1541 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1542 inst->cil_code = sp [i]->cil_code;
1543 sp [i] = locals [i];
1544 if (cfg->verbose_level > 3)
1545 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1547 locals = outb->in_stack;
1557 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1561 if (cfg->compile_aot) {
1562 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1568 ji.type = patch_type;
1569 ji.data.target = data;
1570 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1571 mono_error_assert_ok (&error);
1573 EMIT_NEW_PCONST (cfg, ins, target);
1579 mini_emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1581 return emit_runtime_constant (cfg, patch_type, data);
1585 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1589 g_assert (val == 0);
1594 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1597 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1600 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1603 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1605 #if SIZEOF_REGISTER == 8
1607 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1613 val_reg = alloc_preg (cfg);
1615 if (SIZEOF_REGISTER == 8)
1616 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1618 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1621 /* This could be optimized further if neccesary */
1623 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1630 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1632 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1637 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1644 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1649 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1654 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1661 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1668 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1669 g_assert (size < 10000);
1672 /* This could be optimized further if neccesary */
1674 cur_reg = alloc_preg (cfg);
1675 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1676 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1683 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1685 cur_reg = alloc_preg (cfg);
1686 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1687 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1695 cur_reg = alloc_preg (cfg);
1696 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1697 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1703 cur_reg = alloc_preg (cfg);
1704 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1705 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1711 cur_reg = alloc_preg (cfg);
1712 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1713 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1721 mono_create_fast_tls_getter (MonoCompile *cfg, MonoTlsKey key)
1723 int tls_offset = mono_tls_get_tls_offset (key);
1725 if (cfg->compile_aot)
1728 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1730 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
1731 ins->dreg = mono_alloc_preg (cfg);
1732 ins->inst_offset = tls_offset;
1739 mono_create_fast_tls_setter (MonoCompile *cfg, MonoInst* value, MonoTlsKey key)
1741 int tls_offset = mono_tls_get_tls_offset (key);
1743 if (cfg->compile_aot)
1746 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1748 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1749 ins->sreg1 = value->dreg;
1750 ins->inst_offset = tls_offset;
1758 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
1760 MonoInst *fast_tls = NULL;
1762 if (!mini_get_debug_options ()->use_fallback_tls)
1763 fast_tls = mono_create_fast_tls_getter (cfg, key);
1766 MONO_ADD_INS (cfg->cbb, fast_tls);
1770 if (cfg->compile_aot) {
1773 * tls getters are critical pieces of code and we don't want to resolve them
1774 * through the standard plt/tramp mechanism since we might expose ourselves
1775 * to crashes and infinite recursions.
1777 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GET_TLS_TRAMP, (void*)key);
1778 return mono_emit_calli (cfg, helper_sig_get_tls_tramp, NULL, addr, NULL, NULL);
1780 gpointer getter = mono_tls_get_tls_getter (key, FALSE);
1781 return mono_emit_jit_icall (cfg, getter, NULL);
1786 mono_create_tls_set (MonoCompile *cfg, MonoInst *value, MonoTlsKey key)
1788 MonoInst *fast_tls = NULL;
1790 if (!mini_get_debug_options ()->use_fallback_tls)
1791 fast_tls = mono_create_fast_tls_setter (cfg, value, key);
1794 MONO_ADD_INS (cfg->cbb, fast_tls);
1798 if (cfg->compile_aot) {
1800 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_SET_TLS_TRAMP, (void*)key);
1801 return mono_emit_calli (cfg, helper_sig_set_tls_tramp, &value, addr, NULL, NULL);
1803 gpointer setter = mono_tls_get_tls_setter (key, FALSE);
1804 return mono_emit_jit_icall (cfg, setter, &value);
1811 * Emit IR to push the current LMF onto the LMF stack.
1814 emit_push_lmf (MonoCompile *cfg)
1817 * Emit IR to push the LMF:
1818 * lmf_addr = <lmf_addr from tls>
1819 * lmf->lmf_addr = lmf_addr
1820 * lmf->prev_lmf = *lmf_addr
1823 MonoInst *ins, *lmf_ins;
1828 if (cfg->lmf_ir_mono_lmf) {
1829 MonoInst *lmf_vara_ins, *lmf_ins;
1830 /* Load current lmf */
1831 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF);
1833 EMIT_NEW_VARLOADA (cfg, lmf_vara_ins, cfg->lmf_var, NULL);
1834 /* Save previous_lmf */
1835 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_vara_ins->dreg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
1837 mono_create_tls_set (cfg, lmf_vara_ins, TLS_KEY_LMF);
1839 int lmf_reg, prev_lmf_reg;
1841 * Store lmf_addr in a variable, so it can be allocated to a global register.
1843 if (!cfg->lmf_addr_var)
1844 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1847 ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
1849 int jit_tls_dreg = ins->dreg;
1851 lmf_reg = alloc_preg (cfg);
1852 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
1854 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
1857 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
1859 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1860 lmf_reg = ins->dreg;
1862 prev_lmf_reg = alloc_preg (cfg);
1863 /* Save previous_lmf */
1864 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
1865 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
1867 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
1874 * Emit IR to pop the current LMF from the LMF stack.
1877 emit_pop_lmf (MonoCompile *cfg)
1879 int lmf_reg, lmf_addr_reg;
1885 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1886 lmf_reg = ins->dreg;
1888 if (cfg->lmf_ir_mono_lmf) {
1889 /* Load previous_lmf */
1890 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, alloc_preg (cfg), lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1892 mono_create_tls_set (cfg, ins, TLS_KEY_LMF);
1896 * Emit IR to pop the LMF:
1897 * *(lmf->lmf_addr) = lmf->prev_lmf
1899 /* This could be called before emit_push_lmf () */
1900 if (!cfg->lmf_addr_var)
1901 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1902 lmf_addr_reg = cfg->lmf_addr_var->dreg;
1904 prev_lmf_reg = alloc_preg (cfg);
1905 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1906 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
1911 emit_instrumentation_call (MonoCompile *cfg, void *func)
1913 MonoInst *iargs [1];
1916 * Avoid instrumenting inlined methods since it can
1917 * distort profiling results.
1919 if (cfg->method != cfg->current_method)
1922 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
1923 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
1924 mono_emit_jit_icall (cfg, func, iargs);
1929 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
1932 type = mini_get_underlying_type (type);
1933 switch (type->type) {
1934 case MONO_TYPE_VOID:
1935 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
1942 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1946 case MONO_TYPE_FNPTR:
1947 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1948 case MONO_TYPE_CLASS:
1949 case MONO_TYPE_STRING:
1950 case MONO_TYPE_OBJECT:
1951 case MONO_TYPE_SZARRAY:
1952 case MONO_TYPE_ARRAY:
1953 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1956 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
1959 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
1961 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1963 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1964 case MONO_TYPE_VALUETYPE:
1965 if (type->data.klass->enumtype) {
1966 type = mono_class_enum_basetype (type->data.klass);
1969 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1970 case MONO_TYPE_TYPEDBYREF:
1971 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1972 case MONO_TYPE_GENERICINST:
1973 type = &type->data.generic_class->container_class->byval_arg;
1976 case MONO_TYPE_MVAR:
1978 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1980 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1985 //XXX this ignores if t is byref
1986 #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)))))
1989 * target_type_is_incompatible:
1990 * @cfg: MonoCompile context
1992 * Check that the item @arg on the evaluation stack can be stored
1993 * in the target type (can be a local, or field, etc).
1994 * The cfg arg can be used to check if we need verification or just
1997 * Returns: non-0 value if arg can't be stored on a target.
2000 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2002 MonoType *simple_type;
2005 if (target->byref) {
2006 /* FIXME: check that the pointed to types match */
2007 if (arg->type == STACK_MP) {
2008 /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
2009 MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
2010 MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
2012 /* if the target is native int& or same type */
2013 if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
2016 /* Both are primitive type byrefs and the source points to a larger type that the destination */
2017 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
2018 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
2022 if (arg->type == STACK_PTR)
2027 simple_type = mini_get_underlying_type (target);
2028 switch (simple_type->type) {
2029 case MONO_TYPE_VOID:
2037 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2041 /* STACK_MP is needed when setting pinned locals */
2042 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2047 case MONO_TYPE_FNPTR:
2049 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2050 * in native int. (#688008).
2052 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2055 case MONO_TYPE_CLASS:
2056 case MONO_TYPE_STRING:
2057 case MONO_TYPE_OBJECT:
2058 case MONO_TYPE_SZARRAY:
2059 case MONO_TYPE_ARRAY:
2060 if (arg->type != STACK_OBJ)
2062 /* FIXME: check type compatibility */
2066 if (arg->type != STACK_I8)
2070 if (arg->type != cfg->r4_stack_type)
2074 if (arg->type != STACK_R8)
2077 case MONO_TYPE_VALUETYPE:
2078 if (arg->type != STACK_VTYPE)
2080 klass = mono_class_from_mono_type (simple_type);
2081 if (klass != arg->klass)
2084 case MONO_TYPE_TYPEDBYREF:
2085 if (arg->type != STACK_VTYPE)
2087 klass = mono_class_from_mono_type (simple_type);
2088 if (klass != arg->klass)
2091 case MONO_TYPE_GENERICINST:
2092 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2093 MonoClass *target_class;
2094 if (arg->type != STACK_VTYPE)
2096 klass = mono_class_from_mono_type (simple_type);
2097 target_class = mono_class_from_mono_type (target);
2098 /* The second cases is needed when doing partial sharing */
2099 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2103 if (arg->type != STACK_OBJ)
2105 /* FIXME: check type compatibility */
2109 case MONO_TYPE_MVAR:
2110 g_assert (cfg->gshared);
2111 if (mini_type_var_is_vt (simple_type)) {
2112 if (arg->type != STACK_VTYPE)
2115 if (arg->type != STACK_OBJ)
2120 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2126 * Prepare arguments for passing to a function call.
2127 * Return a non-zero value if the arguments can't be passed to the given
2129 * The type checks are not yet complete and some conversions may need
2130 * casts on 32 or 64 bit architectures.
2132 * FIXME: implement this using target_type_is_incompatible ()
2135 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2137 MonoType *simple_type;
2141 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2145 for (i = 0; i < sig->param_count; ++i) {
2146 if (sig->params [i]->byref) {
2147 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2151 simple_type = mini_get_underlying_type (sig->params [i]);
2153 switch (simple_type->type) {
2154 case MONO_TYPE_VOID:
2163 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2169 case MONO_TYPE_FNPTR:
2170 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2173 case MONO_TYPE_CLASS:
2174 case MONO_TYPE_STRING:
2175 case MONO_TYPE_OBJECT:
2176 case MONO_TYPE_SZARRAY:
2177 case MONO_TYPE_ARRAY:
2178 if (args [i]->type != STACK_OBJ)
2183 if (args [i]->type != STACK_I8)
2187 if (args [i]->type != cfg->r4_stack_type)
2191 if (args [i]->type != STACK_R8)
2194 case MONO_TYPE_VALUETYPE:
2195 if (simple_type->data.klass->enumtype) {
2196 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2199 if (args [i]->type != STACK_VTYPE)
2202 case MONO_TYPE_TYPEDBYREF:
2203 if (args [i]->type != STACK_VTYPE)
2206 case MONO_TYPE_GENERICINST:
2207 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2210 case MONO_TYPE_MVAR:
2212 if (args [i]->type != STACK_VTYPE)
2216 g_error ("unknown type 0x%02x in check_call_signature",
2224 callvirt_to_call (int opcode)
2227 case OP_CALL_MEMBASE:
2229 case OP_VOIDCALL_MEMBASE:
2231 case OP_FCALL_MEMBASE:
2233 case OP_RCALL_MEMBASE:
2235 case OP_VCALL_MEMBASE:
2237 case OP_LCALL_MEMBASE:
2240 g_assert_not_reached ();
2247 callvirt_to_call_reg (int opcode)
2250 case OP_CALL_MEMBASE:
2252 case OP_VOIDCALL_MEMBASE:
2253 return OP_VOIDCALL_REG;
2254 case OP_FCALL_MEMBASE:
2255 return OP_FCALL_REG;
2256 case OP_RCALL_MEMBASE:
2257 return OP_RCALL_REG;
2258 case OP_VCALL_MEMBASE:
2259 return OP_VCALL_REG;
2260 case OP_LCALL_MEMBASE:
2261 return OP_LCALL_REG;
2263 g_assert_not_reached ();
2269 /* Either METHOD or IMT_ARG needs to be set */
2271 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2275 if (COMPILE_LLVM (cfg)) {
2277 method_reg = alloc_preg (cfg);
2278 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2280 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2281 method_reg = ins->dreg;
2285 call->imt_arg_reg = method_reg;
2287 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2292 method_reg = alloc_preg (cfg);
2293 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2295 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2296 method_reg = ins->dreg;
2299 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2302 static MonoJumpInfo *
2303 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2305 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2309 ji->data.target = target;
2315 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2318 return mono_class_check_context_used (klass);
2324 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2327 return mono_method_check_context_used (method);
2333 * check_method_sharing:
2335 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2338 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2340 gboolean pass_vtable = FALSE;
2341 gboolean pass_mrgctx = FALSE;
2343 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2344 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2345 gboolean sharable = FALSE;
2347 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2351 * Pass vtable iff target method might
2352 * be shared, which means that sharing
2353 * is enabled for its class and its
2354 * context is sharable (and it's not a
2357 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2361 if (mini_method_get_context (cmethod) &&
2362 mini_method_get_context (cmethod)->method_inst) {
2363 g_assert (!pass_vtable);
2365 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2368 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2373 if (out_pass_vtable)
2374 *out_pass_vtable = pass_vtable;
2375 if (out_pass_mrgctx)
2376 *out_pass_mrgctx = pass_mrgctx;
2379 inline static MonoCallInst *
2380 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2381 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2385 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2393 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2395 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2397 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2400 call->signature = sig;
2401 call->rgctx_reg = rgctx;
2402 sig_ret = mini_get_underlying_type (sig->ret);
2404 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2407 if (mini_type_is_vtype (sig_ret)) {
2408 call->vret_var = cfg->vret_addr;
2409 //g_assert_not_reached ();
2411 } else if (mini_type_is_vtype (sig_ret)) {
2412 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2415 temp->backend.is_pinvoke = sig->pinvoke;
2418 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2419 * address of return value to increase optimization opportunities.
2420 * Before vtype decomposition, the dreg of the call ins itself represents the
2421 * fact the call modifies the return value. After decomposition, the call will
2422 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2423 * will be transformed into an LDADDR.
2425 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2426 loada->dreg = alloc_preg (cfg);
2427 loada->inst_p0 = temp;
2428 /* We reference the call too since call->dreg could change during optimization */
2429 loada->inst_p1 = call;
2430 MONO_ADD_INS (cfg->cbb, loada);
2432 call->inst.dreg = temp->dreg;
2434 call->vret_var = loada;
2435 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2436 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2438 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2439 if (COMPILE_SOFT_FLOAT (cfg)) {
2441 * If the call has a float argument, we would need to do an r8->r4 conversion using
2442 * an icall, but that cannot be done during the call sequence since it would clobber
2443 * the call registers + the stack. So we do it before emitting the call.
2445 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2447 MonoInst *in = call->args [i];
2449 if (i >= sig->hasthis)
2450 t = sig->params [i - sig->hasthis];
2452 t = &mono_defaults.int_class->byval_arg;
2453 t = mono_type_get_underlying_type (t);
2455 if (!t->byref && t->type == MONO_TYPE_R4) {
2456 MonoInst *iargs [1];
2460 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2462 /* The result will be in an int vreg */
2463 call->args [i] = conv;
2469 call->need_unbox_trampoline = unbox_trampoline;
2472 if (COMPILE_LLVM (cfg))
2473 mono_llvm_emit_call (cfg, call);
2475 mono_arch_emit_call (cfg, call);
2477 mono_arch_emit_call (cfg, call);
2480 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2481 cfg->flags |= MONO_CFG_HAS_CALLS;
2487 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2489 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2490 cfg->uses_rgctx_reg = TRUE;
2491 call->rgctx_reg = TRUE;
2493 call->rgctx_arg_reg = rgctx_reg;
2497 inline static MonoInst*
2498 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2503 gboolean check_sp = FALSE;
2505 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2506 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2508 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2513 rgctx_reg = mono_alloc_preg (cfg);
2514 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2518 if (!cfg->stack_inbalance_var)
2519 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2521 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2522 ins->dreg = cfg->stack_inbalance_var->dreg;
2523 MONO_ADD_INS (cfg->cbb, ins);
2526 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2528 call->inst.sreg1 = addr->dreg;
2531 emit_imt_argument (cfg, call, NULL, imt_arg);
2533 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2538 sp_reg = mono_alloc_preg (cfg);
2540 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2542 MONO_ADD_INS (cfg->cbb, ins);
2544 /* Restore the stack so we don't crash when throwing the exception */
2545 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2546 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2547 MONO_ADD_INS (cfg->cbb, ins);
2549 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2550 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2554 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2556 return (MonoInst*)call;
2560 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2563 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2566 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2567 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2569 #ifndef DISABLE_REMOTING
2570 gboolean might_be_remote = FALSE;
2572 gboolean virtual_ = this_ins != NULL;
2573 gboolean enable_for_aot = TRUE;
2576 MonoInst *call_target = NULL;
2578 gboolean need_unbox_trampoline;
2581 sig = mono_method_signature (method);
2583 if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2584 g_assert_not_reached ();
2587 rgctx_reg = mono_alloc_preg (cfg);
2588 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2591 if (method->string_ctor) {
2592 /* Create the real signature */
2593 /* FIXME: Cache these */
2594 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2595 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2600 context_used = mini_method_check_context_used (cfg, method);
2602 #ifndef DISABLE_REMOTING
2603 might_be_remote = this_ins && sig->hasthis &&
2604 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2605 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2607 if (might_be_remote && context_used) {
2610 g_assert (cfg->gshared);
2612 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2614 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2618 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2619 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2621 need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2623 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2625 #ifndef DISABLE_REMOTING
2626 if (might_be_remote)
2627 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2630 call->method = method;
2631 call->inst.flags |= MONO_INST_HAS_METHOD;
2632 call->inst.inst_left = this_ins;
2633 call->tail_call = tail;
2636 int vtable_reg, slot_reg, this_reg;
2639 this_reg = this_ins->dreg;
2641 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2642 MonoInst *dummy_use;
2644 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2646 /* Make a call to delegate->invoke_impl */
2647 call->inst.inst_basereg = this_reg;
2648 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2649 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2651 /* We must emit a dummy use here because the delegate trampoline will
2652 replace the 'this' argument with the delegate target making this activation
2653 no longer a root for the delegate.
2654 This is an issue for delegates that target collectible code such as dynamic
2655 methods of GC'able assemblies.
2657 For a test case look into #667921.
2659 FIXME: a dummy use is not the best way to do it as the local register allocator
2660 will put it on a caller save register and spil it around the call.
2661 Ideally, we would either put it on a callee save register or only do the store part.
2663 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2665 return (MonoInst*)call;
2668 if ((!cfg->compile_aot || enable_for_aot) &&
2669 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2670 (MONO_METHOD_IS_FINAL (method) &&
2671 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2672 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2674 * the method is not virtual, we just need to ensure this is not null
2675 * and then we can call the method directly.
2677 #ifndef DISABLE_REMOTING
2678 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2680 * The check above ensures method is not gshared, this is needed since
2681 * gshared methods can't have wrappers.
2683 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2687 if (!method->string_ctor)
2688 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2690 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2691 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2693 * the method is virtual, but we can statically dispatch since either
2694 * it's class or the method itself are sealed.
2695 * But first we need to ensure it's not a null reference.
2697 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2699 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2700 } else if (call_target) {
2701 vtable_reg = alloc_preg (cfg);
2702 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2704 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2705 call->inst.sreg1 = call_target->dreg;
2706 call->inst.flags &= !MONO_INST_HAS_METHOD;
2708 vtable_reg = alloc_preg (cfg);
2709 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2710 if (mono_class_is_interface (method->klass)) {
2711 guint32 imt_slot = mono_method_get_imt_slot (method);
2712 emit_imt_argument (cfg, call, call->method, imt_arg);
2713 slot_reg = vtable_reg;
2714 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2716 slot_reg = vtable_reg;
2717 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2718 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2720 g_assert (mono_method_signature (method)->generic_param_count);
2721 emit_imt_argument (cfg, call, call->method, imt_arg);
2725 call->inst.sreg1 = slot_reg;
2726 call->inst.inst_offset = offset;
2727 call->is_virtual = TRUE;
2731 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2734 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2736 return (MonoInst*)call;
2740 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2742 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2746 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2753 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2756 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2758 return (MonoInst*)call;
2762 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2764 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2768 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2772 * mono_emit_abs_call:
2774 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2776 inline static MonoInst*
2777 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2778 MonoMethodSignature *sig, MonoInst **args)
2780 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2784 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2787 if (cfg->abs_patches == NULL)
2788 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2789 g_hash_table_insert (cfg->abs_patches, ji, ji);
2790 ins = mono_emit_native_call (cfg, ji, sig, args);
2791 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2795 static MonoMethodSignature*
2796 sig_to_rgctx_sig (MonoMethodSignature *sig)
2798 // FIXME: memory allocation
2799 MonoMethodSignature *res;
2802 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2803 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2804 res->param_count = sig->param_count + 1;
2805 for (i = 0; i < sig->param_count; ++i)
2806 res->params [i] = sig->params [i];
2807 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2811 /* Make an indirect call to FSIG passing an additional argument */
2813 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
2815 MonoMethodSignature *csig;
2816 MonoInst *args_buf [16];
2818 int i, pindex, tmp_reg;
2820 /* Make a call with an rgctx/extra arg */
2821 if (fsig->param_count + 2 < 16)
2824 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
2827 args [pindex ++] = orig_args [0];
2828 for (i = 0; i < fsig->param_count; ++i)
2829 args [pindex ++] = orig_args [fsig->hasthis + i];
2830 tmp_reg = alloc_preg (cfg);
2831 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
2832 csig = sig_to_rgctx_sig (fsig);
2833 return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
2836 /* Emit an indirect call to the function descriptor ADDR */
2838 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
2840 int addr_reg, arg_reg;
2841 MonoInst *call_target;
2843 g_assert (cfg->llvm_only);
2846 * addr points to a <addr, arg> pair, load both of them, and
2847 * make a call to addr, passing arg as an extra arg.
2849 addr_reg = alloc_preg (cfg);
2850 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
2851 arg_reg = alloc_preg (cfg);
2852 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
2854 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
2858 direct_icalls_enabled (MonoCompile *cfg)
2860 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2862 if (cfg->compile_llvm && !cfg->llvm_only)
2865 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2871 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
2874 * Call the jit icall without a wrapper if possible.
2875 * The wrapper is needed for the following reasons:
2876 * - to handle exceptions thrown using mono_raise_exceptions () from the
2877 * icall function. The EH code needs the lmf frame pushed by the
2878 * wrapper to be able to unwind back to managed code.
2879 * - to be able to do stack walks for asynchronously suspended
2880 * threads when debugging.
2882 if (info->no_raise && direct_icalls_enabled (cfg)) {
2886 if (!info->wrapper_method) {
2887 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2888 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2890 mono_memory_barrier ();
2894 * Inline the wrapper method, which is basically a call to the C icall, and
2895 * an exception check.
2897 costs = inline_method (cfg, info->wrapper_method, NULL,
2898 args, NULL, il_offset, TRUE);
2899 g_assert (costs > 0);
2900 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2904 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2909 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2911 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2912 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2916 * Native code might return non register sized integers
2917 * without initializing the upper bits.
2919 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
2920 case OP_LOADI1_MEMBASE:
2921 widen_op = OP_ICONV_TO_I1;
2923 case OP_LOADU1_MEMBASE:
2924 widen_op = OP_ICONV_TO_U1;
2926 case OP_LOADI2_MEMBASE:
2927 widen_op = OP_ICONV_TO_I2;
2929 case OP_LOADU2_MEMBASE:
2930 widen_op = OP_ICONV_TO_U2;
2936 if (widen_op != -1) {
2937 int dreg = alloc_preg (cfg);
2940 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
2941 widen->type = ins->type;
2952 emit_method_access_failure (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
2954 MonoInst *args [16];
2956 args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (caller), caller, MONO_RGCTX_INFO_METHOD);
2957 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (callee), callee, MONO_RGCTX_INFO_METHOD);
2959 mono_emit_jit_icall (cfg, mono_throw_method_access, args);
2963 get_memcpy_method (void)
2965 static MonoMethod *memcpy_method = NULL;
2966 if (!memcpy_method) {
2967 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2969 g_error ("Old corlib found. Install a new one");
2971 return memcpy_method;
2975 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
2977 MonoClassField *field;
2978 gpointer iter = NULL;
2980 while ((field = mono_class_get_fields (klass, &iter))) {
2983 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2985 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
2986 if (mini_type_is_reference (mono_field_get_type (field))) {
2987 g_assert ((foffset % SIZEOF_VOID_P) == 0);
2988 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
2990 MonoClass *field_class = mono_class_from_mono_type (field->type);
2991 if (field_class->has_references)
2992 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
2998 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3000 int card_table_shift_bits;
3001 gpointer card_table_mask;
3003 MonoInst *dummy_use;
3004 int nursery_shift_bits;
3005 size_t nursery_size;
3007 if (!cfg->gen_write_barriers)
3010 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3012 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3014 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3017 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3018 wbarrier->sreg1 = ptr->dreg;
3019 wbarrier->sreg2 = value->dreg;
3020 MONO_ADD_INS (cfg->cbb, wbarrier);
3021 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3022 int offset_reg = alloc_preg (cfg);
3026 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3027 if (card_table_mask)
3028 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3030 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3031 * IMM's larger than 32bits.
3033 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3034 card_reg = ins->dreg;
3036 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3037 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3039 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3040 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3043 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3047 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3049 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3050 unsigned need_wb = 0;
3055 /*types with references can't have alignment smaller than sizeof(void*) */
3056 if (align < SIZEOF_VOID_P)
3059 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3060 if (size > 32 * SIZEOF_VOID_P)
3063 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3065 /* We don't unroll more than 5 stores to avoid code bloat. */
3066 if (size > 5 * SIZEOF_VOID_P) {
3067 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3068 size += (SIZEOF_VOID_P - 1);
3069 size &= ~(SIZEOF_VOID_P - 1);
3071 EMIT_NEW_ICONST (cfg, iargs [2], size);
3072 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3073 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3077 destreg = iargs [0]->dreg;
3078 srcreg = iargs [1]->dreg;
3081 dest_ptr_reg = alloc_preg (cfg);
3082 tmp_reg = alloc_preg (cfg);
3085 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3087 while (size >= SIZEOF_VOID_P) {
3088 MonoInst *load_inst;
3089 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3090 load_inst->dreg = tmp_reg;
3091 load_inst->inst_basereg = srcreg;
3092 load_inst->inst_offset = offset;
3093 MONO_ADD_INS (cfg->cbb, load_inst);
3095 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3098 emit_write_barrier (cfg, iargs [0], load_inst);
3100 offset += SIZEOF_VOID_P;
3101 size -= SIZEOF_VOID_P;
3104 /*tmp += sizeof (void*)*/
3105 if (size >= SIZEOF_VOID_P) {
3106 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3107 MONO_ADD_INS (cfg->cbb, iargs [0]);
3111 /* Those cannot be references since size < sizeof (void*) */
3113 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3114 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3120 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3121 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3127 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3128 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3137 * Emit code to copy a valuetype of type @klass whose address is stored in
3138 * @src->dreg to memory whose address is stored at @dest->dreg.
3141 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3143 MonoInst *iargs [4];
3146 MonoMethod *memcpy_method;
3147 MonoInst *size_ins = NULL;
3148 MonoInst *memcpy_ins = NULL;
3152 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3155 * This check breaks with spilled vars... need to handle it during verification anyway.
3156 * g_assert (klass && klass == src->klass && klass == dest->klass);
3159 if (mini_is_gsharedvt_klass (klass)) {
3161 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3162 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3166 n = mono_class_native_size (klass, &align);
3168 n = mono_class_value_size (klass, &align);
3170 /* if native is true there should be no references in the struct */
3171 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3172 /* Avoid barriers when storing to the stack */
3173 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3174 (dest->opcode == OP_LDADDR))) {
3180 context_used = mini_class_check_context_used (cfg, klass);
3182 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3183 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3185 } else if (context_used) {
3186 iargs [2] = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3188 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3189 if (!cfg->compile_aot)
3190 mono_class_compute_gc_descriptor (klass);
3194 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3196 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3201 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3202 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3203 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3208 iargs [2] = size_ins;
3210 EMIT_NEW_ICONST (cfg, iargs [2], n);
3212 memcpy_method = get_memcpy_method ();
3214 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3216 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3221 get_memset_method (void)
3223 static MonoMethod *memset_method = NULL;
3224 if (!memset_method) {
3225 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3227 g_error ("Old corlib found. Install a new one");
3229 return memset_method;
3233 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3235 MonoInst *iargs [3];
3238 MonoMethod *memset_method;
3239 MonoInst *size_ins = NULL;
3240 MonoInst *bzero_ins = NULL;
3241 static MonoMethod *bzero_method;
3243 /* FIXME: Optimize this for the case when dest is an LDADDR */
3244 mono_class_init (klass);
3245 if (mini_is_gsharedvt_klass (klass)) {
3246 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3247 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3249 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3250 g_assert (bzero_method);
3252 iargs [1] = size_ins;
3253 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3257 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3259 n = mono_class_value_size (klass, &align);
3261 if (n <= sizeof (gpointer) * 8) {
3262 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3265 memset_method = get_memset_method ();
3267 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3268 EMIT_NEW_ICONST (cfg, iargs [2], n);
3269 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3276 * Emit IR to return either the this pointer for instance method,
3277 * or the mrgctx for static methods.
3280 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3282 MonoInst *this_ins = NULL;
3284 g_assert (cfg->gshared);
3286 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3287 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3288 !method->klass->valuetype)
3289 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
3291 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3292 MonoInst *mrgctx_loc, *mrgctx_var;
3294 g_assert (!this_ins);
3295 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3297 mrgctx_loc = mono_get_vtable_var (cfg);
3298 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3301 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3302 MonoInst *vtable_loc, *vtable_var;
3304 g_assert (!this_ins);
3306 vtable_loc = mono_get_vtable_var (cfg);
3307 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3309 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3310 MonoInst *mrgctx_var = vtable_var;
3313 vtable_reg = alloc_preg (cfg);
3314 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3315 vtable_var->type = STACK_PTR;
3323 vtable_reg = alloc_preg (cfg);
3324 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3329 static MonoJumpInfoRgctxEntry *
3330 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3332 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3333 res->method = method;
3334 res->in_mrgctx = in_mrgctx;
3335 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3336 res->data->type = patch_type;
3337 res->data->data.target = patch_data;
3338 res->info_type = info_type;
3343 static inline MonoInst*
3344 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3346 MonoInst *args [16];
3349 // FIXME: No fastpath since the slot is not a compile time constant
3351 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3352 if (entry->in_mrgctx)
3353 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3355 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3359 * FIXME: This can be called during decompose, which is a problem since it creates
3361 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3363 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3365 MonoBasicBlock *is_null_bb, *end_bb;
3366 MonoInst *res, *ins, *call;
3369 slot = mini_get_rgctx_entry_slot (entry);
3371 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3372 index = MONO_RGCTX_SLOT_INDEX (slot);
3374 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3375 for (depth = 0; ; ++depth) {
3376 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3378 if (index < size - 1)
3383 NEW_BBLOCK (cfg, end_bb);
3384 NEW_BBLOCK (cfg, is_null_bb);
3387 rgctx_reg = rgctx->dreg;
3389 rgctx_reg = alloc_preg (cfg);
3391 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3392 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3393 NEW_BBLOCK (cfg, is_null_bb);
3395 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3396 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3399 for (i = 0; i < depth; ++i) {
3400 int array_reg = alloc_preg (cfg);
3402 /* load ptr to next array */
3403 if (mrgctx && i == 0)
3404 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3406 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3407 rgctx_reg = array_reg;
3408 /* is the ptr null? */
3409 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3410 /* if yes, jump to actual trampoline */
3411 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3415 val_reg = alloc_preg (cfg);
3416 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3417 /* is the slot null? */
3418 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3419 /* if yes, jump to actual trampoline */
3420 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3423 res_reg = alloc_preg (cfg);
3424 MONO_INST_NEW (cfg, ins, OP_MOVE);
3425 ins->dreg = res_reg;
3426 ins->sreg1 = val_reg;
3427 MONO_ADD_INS (cfg->cbb, ins);
3429 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3432 MONO_START_BB (cfg, is_null_bb);
3434 EMIT_NEW_ICONST (cfg, args [1], index);
3436 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3438 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3439 MONO_INST_NEW (cfg, ins, OP_MOVE);
3440 ins->dreg = res_reg;
3441 ins->sreg1 = call->dreg;
3442 MONO_ADD_INS (cfg->cbb, ins);
3443 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3445 MONO_START_BB (cfg, end_bb);
3454 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3457 static inline MonoInst*
3458 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3461 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3463 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3467 mini_emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3468 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3470 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);
3471 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3473 return emit_rgctx_fetch (cfg, rgctx, entry);
3477 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3478 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3480 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);
3481 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3483 return emit_rgctx_fetch (cfg, rgctx, entry);
3487 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3488 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3490 MonoJumpInfoGSharedVtCall *call_info;
3491 MonoJumpInfoRgctxEntry *entry;
3494 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3495 call_info->sig = sig;
3496 call_info->method = cmethod;
3498 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);
3499 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3501 return emit_rgctx_fetch (cfg, rgctx, entry);
3505 * emit_get_rgctx_virt_method:
3507 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3510 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3511 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3513 MonoJumpInfoVirtMethod *info;
3514 MonoJumpInfoRgctxEntry *entry;
3517 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3518 info->klass = klass;
3519 info->method = virt_method;
3521 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);
3522 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3524 return emit_rgctx_fetch (cfg, rgctx, entry);
3528 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3529 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3531 MonoJumpInfoRgctxEntry *entry;
3534 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);
3535 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3537 return emit_rgctx_fetch (cfg, rgctx, entry);
3541 * emit_get_rgctx_method:
3543 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3544 * normal constants, else emit a load from the rgctx.
3547 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3548 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3550 if (!context_used) {
3553 switch (rgctx_type) {
3554 case MONO_RGCTX_INFO_METHOD:
3555 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3557 case MONO_RGCTX_INFO_METHOD_RGCTX:
3558 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3561 g_assert_not_reached ();
3564 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);
3565 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3567 return emit_rgctx_fetch (cfg, rgctx, entry);
3572 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3573 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3575 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);
3576 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3578 return emit_rgctx_fetch (cfg, rgctx, entry);
3582 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3584 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3585 MonoRuntimeGenericContextInfoTemplate *template_;
3590 for (i = 0; i < info->num_entries; ++i) {
3591 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3593 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3597 if (info->num_entries == info->count_entries) {
3598 MonoRuntimeGenericContextInfoTemplate *new_entries;
3599 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3601 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3603 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3604 info->entries = new_entries;
3605 info->count_entries = new_count_entries;
3608 idx = info->num_entries;
3609 template_ = &info->entries [idx];
3610 template_->info_type = rgctx_type;
3611 template_->data = data;
3613 info->num_entries ++;
3619 * emit_get_gsharedvt_info:
3621 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3624 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3629 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3630 /* Load info->entries [idx] */
3631 dreg = alloc_preg (cfg);
3632 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3638 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3640 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3644 * On return the caller must check @klass for load errors.
3647 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3649 MonoInst *vtable_arg;
3652 context_used = mini_class_check_context_used (cfg, klass);
3655 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
3656 klass, MONO_RGCTX_INFO_VTABLE);
3658 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3662 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3665 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3669 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3670 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3672 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3673 ins->sreg1 = vtable_arg->dreg;
3674 MONO_ADD_INS (cfg->cbb, ins);
3677 MonoBasicBlock *inited_bb;
3678 MonoInst *args [16];
3680 inited_reg = alloc_ireg (cfg);
3682 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, inited_reg, vtable_arg->dreg, MONO_STRUCT_OFFSET (MonoVTable, initialized));
3684 NEW_BBLOCK (cfg, inited_bb);
3686 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3687 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3689 args [0] = vtable_arg;
3690 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3692 MONO_START_BB (cfg, inited_bb);
3697 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3701 if (cfg->gen_seq_points && cfg->method == method) {
3702 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3704 ins->flags |= MONO_INST_NONEMPTY_STACK;
3705 MONO_ADD_INS (cfg->cbb, ins);
3710 mini_save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3712 if (mini_get_debug_options ()->better_cast_details) {
3713 int vtable_reg = alloc_preg (cfg);
3714 int klass_reg = alloc_preg (cfg);
3715 MonoBasicBlock *is_null_bb = NULL;
3717 int to_klass_reg, context_used;
3720 NEW_BBLOCK (cfg, is_null_bb);
3722 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3723 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3726 tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3728 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3732 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3733 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3735 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3737 context_used = mini_class_check_context_used (cfg, klass);
3739 MonoInst *class_ins;
3741 class_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3742 to_klass_reg = class_ins->dreg;
3744 to_klass_reg = alloc_preg (cfg);
3745 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3747 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3750 MONO_START_BB (cfg, is_null_bb);
3755 mini_reset_cast_details (MonoCompile *cfg)
3757 /* Reset the variables holding the cast details */
3758 if (mini_get_debug_options ()->better_cast_details) {
3759 MonoInst *tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3760 /* It is enough to reset the from field */
3761 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3766 * On return the caller must check @array_class for load errors
3769 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3771 int vtable_reg = alloc_preg (cfg);
3774 context_used = mini_class_check_context_used (cfg, array_class);
3776 mini_save_cast_details (cfg, array_class, obj->dreg, FALSE);
3778 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3780 if (cfg->opt & MONO_OPT_SHARED) {
3781 int class_reg = alloc_preg (cfg);
3784 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3785 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3786 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3787 } else if (context_used) {
3788 MonoInst *vtable_ins;
3790 vtable_ins = mini_emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3791 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3793 if (cfg->compile_aot) {
3797 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3799 vt_reg = alloc_preg (cfg);
3800 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3801 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3804 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3806 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3810 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3812 mini_reset_cast_details (cfg);
3816 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
3817 * generic code is generated.
3820 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3822 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3825 MonoInst *rgctx, *addr;
3827 /* FIXME: What if the class is shared? We might not
3828 have to get the address of the method from the
3830 addr = emit_get_rgctx_method (cfg, context_used, method,
3831 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3832 if (cfg->llvm_only) {
3833 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
3834 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3836 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3838 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3841 gboolean pass_vtable, pass_mrgctx;
3842 MonoInst *rgctx_arg = NULL;
3844 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3845 g_assert (!pass_mrgctx);
3848 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3851 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3854 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3859 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3863 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3864 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3865 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3866 int rank_reg = alloc_dreg (cfg ,STACK_I4);
3868 obj_reg = sp [0]->dreg;
3869 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3870 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3872 /* FIXME: generics */
3873 g_assert (klass->rank == 0);
3876 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3877 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3879 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3880 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3883 MonoInst *element_class;
3885 /* This assertion is from the unboxcast insn */
3886 g_assert (klass->rank == 0);
3888 element_class = mini_emit_get_rgctx_klass (cfg, context_used,
3889 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3891 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3892 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3894 mini_save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3895 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3896 mini_reset_cast_details (cfg);
3899 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3900 MONO_ADD_INS (cfg->cbb, add);
3901 add->type = STACK_MP;
3908 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3910 MonoInst *addr, *klass_inst, *is_ref, *args[16];
3911 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3915 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3921 args [1] = klass_inst;
3924 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3926 NEW_BBLOCK (cfg, is_ref_bb);
3927 NEW_BBLOCK (cfg, is_nullable_bb);
3928 NEW_BBLOCK (cfg, end_bb);
3929 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3930 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3931 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3933 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3934 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3936 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3937 addr_reg = alloc_dreg (cfg, STACK_MP);
3941 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3942 MONO_ADD_INS (cfg->cbb, addr);
3944 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3947 MONO_START_BB (cfg, is_ref_bb);
3949 /* Save the ref to a temporary */
3950 dreg = alloc_ireg (cfg);
3951 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3952 addr->dreg = addr_reg;
3953 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3954 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3957 MONO_START_BB (cfg, is_nullable_bb);
3960 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3961 MonoInst *unbox_call;
3962 MonoMethodSignature *unbox_sig;
3964 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3965 unbox_sig->ret = &klass->byval_arg;
3966 unbox_sig->param_count = 1;
3967 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3970 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
3972 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3974 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3975 addr->dreg = addr_reg;
3978 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3981 MONO_START_BB (cfg, end_bb);
3984 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3990 * Returns NULL and set the cfg exception on error.
3993 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3995 MonoInst *iargs [2];
4000 MonoRgctxInfoType rgctx_info;
4001 MonoInst *iargs [2];
4002 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4004 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4006 if (cfg->opt & MONO_OPT_SHARED)
4007 rgctx_info = MONO_RGCTX_INFO_KLASS;
4009 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4010 data = mini_emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4012 if (cfg->opt & MONO_OPT_SHARED) {
4013 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4015 alloc_ftn = ves_icall_object_new;
4018 alloc_ftn = ves_icall_object_new_specific;
4021 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4022 if (known_instance_size) {
4023 int size = mono_class_instance_size (klass);
4024 if (size < sizeof (MonoObject))
4025 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4027 EMIT_NEW_ICONST (cfg, iargs [1], size);
4029 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4032 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4035 if (cfg->opt & MONO_OPT_SHARED) {
4036 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4037 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4039 alloc_ftn = ves_icall_object_new;
4040 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
4041 /* This happens often in argument checking code, eg. throw new FooException... */
4042 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4043 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4044 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4046 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4047 MonoMethod *managed_alloc = NULL;
4051 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4052 cfg->exception_ptr = klass;
4056 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4058 if (managed_alloc) {
4059 int size = mono_class_instance_size (klass);
4060 if (size < sizeof (MonoObject))
4061 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4063 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4064 EMIT_NEW_ICONST (cfg, iargs [1], size);
4065 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4067 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4069 guint32 lw = vtable->klass->instance_size;
4070 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4071 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4072 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4075 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4079 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4083 * Returns NULL and set the cfg exception on error.
4086 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4088 MonoInst *alloc, *ins;
4090 if (mono_class_is_nullable (klass)) {
4091 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4094 if (cfg->llvm_only && cfg->gsharedvt) {
4095 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4096 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4097 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4099 /* FIXME: What if the class is shared? We might not
4100 have to get the method address from the RGCTX. */
4101 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4102 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4103 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
4105 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4108 gboolean pass_vtable, pass_mrgctx;
4109 MonoInst *rgctx_arg = NULL;
4111 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4112 g_assert (!pass_mrgctx);
4115 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4118 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4121 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4125 if (mini_is_gsharedvt_klass (klass)) {
4126 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4127 MonoInst *res, *is_ref, *src_var, *addr;
4130 dreg = alloc_ireg (cfg);
4132 NEW_BBLOCK (cfg, is_ref_bb);
4133 NEW_BBLOCK (cfg, is_nullable_bb);
4134 NEW_BBLOCK (cfg, end_bb);
4135 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4136 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4137 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4139 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4140 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4143 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4146 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4147 ins->opcode = OP_STOREV_MEMBASE;
4149 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4150 res->type = STACK_OBJ;
4152 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4155 MONO_START_BB (cfg, is_ref_bb);
4157 /* val is a vtype, so has to load the value manually */
4158 src_var = get_vreg_to_inst (cfg, val->dreg);
4160 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4161 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4162 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4163 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4166 MONO_START_BB (cfg, is_nullable_bb);
4169 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4170 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4172 MonoMethodSignature *box_sig;
4175 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4176 * construct that method at JIT time, so have to do things by hand.
4178 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4179 box_sig->ret = &mono_defaults.object_class->byval_arg;
4180 box_sig->param_count = 1;
4181 box_sig->params [0] = &klass->byval_arg;
4184 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4186 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4187 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4188 res->type = STACK_OBJ;
4192 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4194 MONO_START_BB (cfg, end_bb);
4198 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4202 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4207 static GHashTable* direct_icall_type_hash;
4210 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4212 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4213 if (!direct_icalls_enabled (cfg))
4217 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4218 * Whitelist a few icalls for now.
4220 if (!direct_icall_type_hash) {
4221 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4223 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4224 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4225 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4226 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4227 mono_memory_barrier ();
4228 direct_icall_type_hash = h;
4231 if (cmethod->klass == mono_defaults.math_class)
4233 /* No locking needed */
4234 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4240 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
4242 if (cmethod->klass == mono_defaults.systemtype_class) {
4243 if (!strcmp (cmethod->name, "GetType"))
4249 static G_GNUC_UNUSED MonoInst*
4250 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4252 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4253 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4256 switch (enum_type->type) {
4259 #if SIZEOF_REGISTER == 8
4271 MonoInst *load, *and_, *cmp, *ceq;
4272 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4273 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4274 int dest_reg = alloc_ireg (cfg);
4276 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4277 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4278 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4279 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4281 ceq->type = STACK_I4;
4284 load = mono_decompose_opcode (cfg, load);
4285 and_ = mono_decompose_opcode (cfg, and_);
4286 cmp = mono_decompose_opcode (cfg, cmp);
4287 ceq = mono_decompose_opcode (cfg, ceq);
4295 * Returns NULL and set the cfg exception on error.
4297 static G_GNUC_UNUSED MonoInst*
4298 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
4302 gpointer trampoline;
4303 MonoInst *obj, *method_ins, *tramp_ins;
4307 if (virtual_ && !cfg->llvm_only) {
4308 MonoMethod *invoke = mono_get_delegate_invoke (klass);
4311 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
4315 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
4319 /* Inline the contents of mono_delegate_ctor */
4321 /* Set target field */
4322 /* Optimize away setting of NULL target */
4323 if (!MONO_INS_IS_PCONST_NULL (target)) {
4324 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4325 if (cfg->gen_write_barriers) {
4326 dreg = alloc_preg (cfg);
4327 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4328 emit_write_barrier (cfg, ptr, target);
4332 /* Set method field */
4333 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4334 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4337 * To avoid looking up the compiled code belonging to the target method
4338 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4339 * store it, and we fill it after the method has been compiled.
4341 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4342 MonoInst *code_slot_ins;
4345 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4347 domain = mono_domain_get ();
4348 mono_domain_lock (domain);
4349 if (!domain_jit_info (domain)->method_code_hash)
4350 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4351 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4353 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
4354 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4356 mono_domain_unlock (domain);
4358 code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4360 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
4363 if (cfg->llvm_only) {
4364 MonoInst *args [16];
4369 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4370 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
4373 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
4379 if (cfg->compile_aot) {
4380 MonoDelegateClassMethodPair *del_tramp;
4382 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4383 del_tramp->klass = klass;
4384 del_tramp->method = context_used ? NULL : method;
4385 del_tramp->is_virtual = virtual_;
4386 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4389 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4391 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4392 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4395 /* Set invoke_impl field */
4397 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4399 dreg = alloc_preg (cfg);
4400 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4401 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4403 dreg = alloc_preg (cfg);
4404 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4405 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4408 dreg = alloc_preg (cfg);
4409 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
4410 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4412 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4418 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4420 MonoJitICallInfo *info;
4422 /* Need to register the icall so it gets an icall wrapper */
4423 info = mono_get_array_new_va_icall (rank);
4425 cfg->flags |= MONO_CFG_HAS_VARARGS;
4427 /* mono_array_new_va () needs a vararg calling convention */
4428 cfg->exception_message = g_strdup ("array-new");
4429 cfg->disable_llvm = TRUE;
4431 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4432 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4436 * handle_constrained_gsharedvt_call:
4438 * Handle constrained calls where the receiver is a gsharedvt type.
4439 * Return the instruction representing the call. Set the cfg exception on failure.
4442 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4443 gboolean *ref_emit_widen)
4445 MonoInst *ins = NULL;
4446 gboolean emit_widen = *ref_emit_widen;
4449 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4450 * 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
4451 * pack the arguments into an array, and do the rest of the work in in an icall.
4453 if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4454 (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)) &&
4455 (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]))))) {
4456 MonoInst *args [16];
4459 * This case handles calls to
4460 * - object:ToString()/Equals()/GetHashCode(),
4461 * - System.IComparable<T>:CompareTo()
4462 * - System.IEquatable<T>:Equals ()
4463 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4467 if (mono_method_check_context_used (cmethod))
4468 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4470 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4471 args [2] = mini_emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4473 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4474 if (fsig->hasthis && fsig->param_count) {
4475 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4476 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4477 ins->dreg = alloc_preg (cfg);
4478 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4479 MONO_ADD_INS (cfg->cbb, ins);
4482 if (mini_is_gsharedvt_type (fsig->params [0])) {
4483 int addr_reg, deref_arg_reg;
4485 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4486 deref_arg_reg = alloc_preg (cfg);
4487 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
4488 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
4490 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4491 addr_reg = ins->dreg;
4492 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4494 EMIT_NEW_ICONST (cfg, args [3], 0);
4495 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4498 EMIT_NEW_ICONST (cfg, args [3], 0);
4499 EMIT_NEW_ICONST (cfg, args [4], 0);
4501 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4504 if (mini_is_gsharedvt_type (fsig->ret)) {
4505 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
4506 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret))) {
4510 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4511 MONO_ADD_INS (cfg->cbb, add);
4513 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4514 MONO_ADD_INS (cfg->cbb, ins);
4515 /* ins represents the call result */
4518 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4521 *ref_emit_widen = emit_widen;
4530 mono_emit_load_got_addr (MonoCompile *cfg)
4532 MonoInst *getaddr, *dummy_use;
4534 if (!cfg->got_var || cfg->got_var_allocated)
4537 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4538 getaddr->cil_code = cfg->header->code;
4539 getaddr->dreg = cfg->got_var->dreg;
4541 /* Add it to the start of the first bblock */
4542 if (cfg->bb_entry->code) {
4543 getaddr->next = cfg->bb_entry->code;
4544 cfg->bb_entry->code = getaddr;
4547 MONO_ADD_INS (cfg->bb_entry, getaddr);
4549 cfg->got_var_allocated = TRUE;
4552 * Add a dummy use to keep the got_var alive, since real uses might
4553 * only be generated by the back ends.
4554 * Add it to end_bblock, so the variable's lifetime covers the whole
4556 * It would be better to make the usage of the got var explicit in all
4557 * cases when the backend needs it (i.e. calls, throw etc.), so this
4558 * wouldn't be needed.
4560 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
4561 MONO_ADD_INS (cfg->bb_exit, dummy_use);
4564 static int inline_limit;
4565 static gboolean inline_limit_inited;
4568 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
4570 MonoMethodHeaderSummary header;
4572 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4573 MonoMethodSignature *sig = mono_method_signature (method);
4577 if (cfg->disable_inline)
4582 if (cfg->inline_depth > 10)
4585 if (!mono_method_get_header_summary (method, &header))
4588 /*runtime, icall and pinvoke are checked by summary call*/
4589 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
4590 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
4591 (mono_class_is_marshalbyref (method->klass)) ||
4595 /* also consider num_locals? */
4596 /* Do the size check early to avoid creating vtables */
4597 if (!inline_limit_inited) {
4598 if (g_getenv ("MONO_INLINELIMIT"))
4599 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
4601 inline_limit = INLINE_LENGTH_LIMIT;
4602 inline_limit_inited = TRUE;
4604 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
4608 * if we can initialize the class of the method right away, we do,
4609 * otherwise we don't allow inlining if the class needs initialization,
4610 * since it would mean inserting a call to mono_runtime_class_init()
4611 * inside the inlined code
4613 if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
4616 if (!(cfg->opt & MONO_OPT_SHARED)) {
4617 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
4618 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
4619 if (method->klass->has_cctor) {
4620 vtable = mono_class_vtable (cfg->domain, method->klass);
4623 if (!cfg->compile_aot) {
4625 if (!mono_runtime_class_init_full (vtable, &error)) {
4626 mono_error_cleanup (&error);
4631 } else if (mono_class_is_before_field_init (method->klass)) {
4632 if (cfg->run_cctors && method->klass->has_cctor) {
4633 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
4634 if (!method->klass->runtime_info)
4635 /* No vtable created yet */
4637 vtable = mono_class_vtable (cfg->domain, method->klass);
4640 /* This makes so that inline cannot trigger */
4641 /* .cctors: too many apps depend on them */
4642 /* running with a specific order... */
4643 if (! vtable->initialized)
4646 if (!mono_runtime_class_init_full (vtable, &error)) {
4647 mono_error_cleanup (&error);
4651 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
4652 if (!method->klass->runtime_info)
4653 /* No vtable created yet */
4655 vtable = mono_class_vtable (cfg->domain, method->klass);
4658 if (!vtable->initialized)
4663 * If we're compiling for shared code
4664 * the cctor will need to be run at aot method load time, for example,
4665 * or at the end of the compilation of the inlining method.
4667 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
4671 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4672 if (mono_arch_is_soft_float ()) {
4674 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
4676 for (i = 0; i < sig->param_count; ++i)
4677 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
4682 if (g_list_find (cfg->dont_inline, method))
4689 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
4691 if (!cfg->compile_aot) {
4693 if (vtable->initialized)
4697 if (mono_class_is_before_field_init (klass)) {
4698 if (cfg->method == method)
4702 if (!mono_class_needs_cctor_run (klass, method))
4705 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
4706 /* The initialization is already done before the method is called */
4713 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
4717 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
4720 if (mini_is_gsharedvt_variable_klass (klass)) {
4723 mono_class_init (klass);
4724 size = mono_class_array_element_size (klass);
4727 mult_reg = alloc_preg (cfg);
4728 array_reg = arr->dreg;
4729 index_reg = index->dreg;
4731 #if SIZEOF_REGISTER == 8
4732 /* The array reg is 64 bits but the index reg is only 32 */
4733 if (COMPILE_LLVM (cfg)) {
4735 index2_reg = index_reg;
4737 index2_reg = alloc_preg (cfg);
4738 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
4741 if (index->type == STACK_I8) {
4742 index2_reg = alloc_preg (cfg);
4743 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
4745 index2_reg = index_reg;
4750 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
4752 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4753 if (size == 1 || size == 2 || size == 4 || size == 8) {
4754 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
4756 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
4757 ins->klass = mono_class_get_element_class (klass);
4758 ins->type = STACK_MP;
4764 add_reg = alloc_ireg_mp (cfg);
4767 MonoInst *rgctx_ins;
4770 g_assert (cfg->gshared);
4771 context_used = mini_class_check_context_used (cfg, klass);
4772 g_assert (context_used);
4773 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
4774 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
4776 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
4778 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
4779 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4780 ins->klass = mono_class_get_element_class (klass);
4781 ins->type = STACK_MP;
4782 MONO_ADD_INS (cfg->cbb, ins);
4788 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
4790 int bounds_reg = alloc_preg (cfg);
4791 int add_reg = alloc_ireg_mp (cfg);
4792 int mult_reg = alloc_preg (cfg);
4793 int mult2_reg = alloc_preg (cfg);
4794 int low1_reg = alloc_preg (cfg);
4795 int low2_reg = alloc_preg (cfg);
4796 int high1_reg = alloc_preg (cfg);
4797 int high2_reg = alloc_preg (cfg);
4798 int realidx1_reg = alloc_preg (cfg);
4799 int realidx2_reg = alloc_preg (cfg);
4800 int sum_reg = alloc_preg (cfg);
4801 int index1, index2, tmpreg;
4805 mono_class_init (klass);
4806 size = mono_class_array_element_size (klass);
4808 index1 = index_ins1->dreg;
4809 index2 = index_ins2->dreg;
4811 #if SIZEOF_REGISTER == 8
4812 /* The array reg is 64 bits but the index reg is only 32 */
4813 if (COMPILE_LLVM (cfg)) {
4816 tmpreg = alloc_preg (cfg);
4817 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
4819 tmpreg = alloc_preg (cfg);
4820 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
4824 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
4828 /* range checking */
4829 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
4830 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4832 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
4833 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4834 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
4835 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
4836 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4837 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
4838 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4840 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
4841 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4842 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
4843 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
4844 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4845 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
4846 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4848 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
4849 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
4850 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
4851 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
4852 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4854 ins->type = STACK_MP;
4856 MONO_ADD_INS (cfg->cbb, ins);
4862 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
4866 MonoMethod *addr_method;
4868 MonoClass *eclass = cmethod->klass->element_class;
4870 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
4873 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
4875 /* emit_ldelema_2 depends on OP_LMUL */
4876 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
4877 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
4880 if (mini_is_gsharedvt_variable_klass (eclass))
4883 element_size = mono_class_array_element_size (eclass);
4884 addr_method = mono_marshal_get_array_address (rank, element_size);
4885 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
4890 static MonoBreakPolicy
4891 always_insert_breakpoint (MonoMethod *method)
4893 return MONO_BREAK_POLICY_ALWAYS;
4896 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
4899 * mono_set_break_policy:
4900 * policy_callback: the new callback function
4902 * Allow embedders to decide wherther to actually obey breakpoint instructions
4903 * (both break IL instructions and Debugger.Break () method calls), for example
4904 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
4905 * untrusted or semi-trusted code.
4907 * @policy_callback will be called every time a break point instruction needs to
4908 * be inserted with the method argument being the method that calls Debugger.Break()
4909 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
4910 * if it wants the breakpoint to not be effective in the given method.
4911 * #MONO_BREAK_POLICY_ALWAYS is the default.
4914 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
4916 if (policy_callback)
4917 break_policy_func = policy_callback;
4919 break_policy_func = always_insert_breakpoint;
4923 should_insert_brekpoint (MonoMethod *method) {
4924 switch (break_policy_func (method)) {
4925 case MONO_BREAK_POLICY_ALWAYS:
4927 case MONO_BREAK_POLICY_NEVER:
4929 case MONO_BREAK_POLICY_ON_DBG:
4930 g_warning ("mdb no longer supported");
4933 g_warning ("Incorrect value returned from break policy callback");
4938 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
4940 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4942 MonoInst *addr, *store, *load;
4943 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
4945 /* the bounds check is already done by the callers */
4946 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4948 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
4949 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
4950 if (mini_type_is_reference (&eklass->byval_arg))
4951 emit_write_barrier (cfg, addr, load);
4953 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
4954 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
4961 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4963 return mini_type_is_reference (&klass->byval_arg);
4967 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
4969 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
4970 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
4971 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
4972 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
4973 MonoInst *iargs [3];
4976 mono_class_setup_vtable (obj_array);
4977 g_assert (helper->slot);
4979 if (sp [0]->type != STACK_OBJ)
4981 if (sp [2]->type != STACK_OBJ)
4988 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
4992 if (mini_is_gsharedvt_variable_klass (klass)) {
4995 // FIXME-VT: OP_ICONST optimization
4996 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
4997 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4998 ins->opcode = OP_STOREV_MEMBASE;
4999 } else if (sp [1]->opcode == OP_ICONST) {
5000 int array_reg = sp [0]->dreg;
5001 int index_reg = sp [1]->dreg;
5002 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5004 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5005 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5008 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5009 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5011 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5012 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5013 if (generic_class_is_reference_type (cfg, klass))
5014 emit_write_barrier (cfg, addr, sp [2]);
5021 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5026 eklass = mono_class_from_mono_type (fsig->params [2]);
5028 eklass = mono_class_from_mono_type (fsig->ret);
5031 return emit_array_store (cfg, eklass, args, FALSE);
5033 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5034 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5040 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5043 int param_size, return_size;
5045 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
5046 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5048 if (cfg->verbose_level > 3)
5049 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5051 //Don't allow mixing reference types with value types
5052 if (param_klass->valuetype != return_klass->valuetype) {
5053 if (cfg->verbose_level > 3)
5054 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5058 if (!param_klass->valuetype) {
5059 if (cfg->verbose_level > 3)
5060 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5065 if (param_klass->has_references || return_klass->has_references)
5068 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5069 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5070 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5071 if (cfg->verbose_level > 3)
5072 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5076 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5077 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5078 if (cfg->verbose_level > 3)
5079 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5083 param_size = mono_class_value_size (param_klass, &align);
5084 return_size = mono_class_value_size (return_klass, &align);
5086 //We can do it if sizes match
5087 if (param_size == return_size) {
5088 if (cfg->verbose_level > 3)
5089 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5093 //No simple way to handle struct if sizes don't match
5094 if (MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg)) {
5095 if (cfg->verbose_level > 3)
5096 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5101 * Same reg size category.
5102 * A quick note on why we don't require widening here.
5103 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5105 * Since the source value comes from a function argument, the JIT will already have
5106 * the value in a VREG and performed any widening needed before (say, when loading from a field).
5108 if (param_size <= 4 && return_size <= 4) {
5109 if (cfg->verbose_level > 3)
5110 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5118 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5120 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5121 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5123 if (mini_is_gsharedvt_variable_type (fsig->ret))
5126 //Valuetypes that are semantically equivalent or numbers than can be widened to
5127 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5130 //Arrays of valuetypes that are semantically equivalent
5131 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5138 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5140 #ifdef MONO_ARCH_SIMD_INTRINSICS
5141 MonoInst *ins = NULL;
5143 if (cfg->opt & MONO_OPT_SIMD) {
5144 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5150 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5154 emit_memory_barrier (MonoCompile *cfg, int kind)
5156 MonoInst *ins = NULL;
5157 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5158 MONO_ADD_INS (cfg->cbb, ins);
5159 ins->backend.memory_barrier_kind = kind;
5165 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5167 MonoInst *ins = NULL;
5170 /* The LLVM backend supports these intrinsics */
5171 if (cmethod->klass == mono_defaults.math_class) {
5172 if (strcmp (cmethod->name, "Sin") == 0) {
5174 } else if (strcmp (cmethod->name, "Cos") == 0) {
5176 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5178 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5182 if (opcode && fsig->param_count == 1) {
5183 MONO_INST_NEW (cfg, ins, opcode);
5184 ins->type = STACK_R8;
5185 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5186 ins->sreg1 = args [0]->dreg;
5187 MONO_ADD_INS (cfg->cbb, ins);
5191 if (cfg->opt & MONO_OPT_CMOV) {
5192 if (strcmp (cmethod->name, "Min") == 0) {
5193 if (fsig->params [0]->type == MONO_TYPE_I4)
5195 if (fsig->params [0]->type == MONO_TYPE_U4)
5196 opcode = OP_IMIN_UN;
5197 else if (fsig->params [0]->type == MONO_TYPE_I8)
5199 else if (fsig->params [0]->type == MONO_TYPE_U8)
5200 opcode = OP_LMIN_UN;
5201 } else if (strcmp (cmethod->name, "Max") == 0) {
5202 if (fsig->params [0]->type == MONO_TYPE_I4)
5204 if (fsig->params [0]->type == MONO_TYPE_U4)
5205 opcode = OP_IMAX_UN;
5206 else if (fsig->params [0]->type == MONO_TYPE_I8)
5208 else if (fsig->params [0]->type == MONO_TYPE_U8)
5209 opcode = OP_LMAX_UN;
5213 if (opcode && fsig->param_count == 2) {
5214 MONO_INST_NEW (cfg, ins, opcode);
5215 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5216 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5217 ins->sreg1 = args [0]->dreg;
5218 ins->sreg2 = args [1]->dreg;
5219 MONO_ADD_INS (cfg->cbb, ins);
5227 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5229 if (cmethod->klass == mono_defaults.array_class) {
5230 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5231 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5232 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5233 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5234 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5235 return emit_array_unsafe_mov (cfg, fsig, args);
5242 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5244 MonoInst *ins = NULL;
5245 MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5247 if (cmethod->klass == mono_defaults.string_class) {
5248 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5249 int dreg = alloc_ireg (cfg);
5250 int index_reg = alloc_preg (cfg);
5251 int add_reg = alloc_preg (cfg);
5253 #if SIZEOF_REGISTER == 8
5254 if (COMPILE_LLVM (cfg)) {
5255 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5257 /* The array reg is 64 bits but the index reg is only 32 */
5258 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5261 index_reg = args [1]->dreg;
5263 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5265 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5266 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5267 add_reg = ins->dreg;
5268 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5271 int mult_reg = alloc_preg (cfg);
5272 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5273 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5274 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5275 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5277 type_from_op (cfg, ins, NULL, NULL);
5279 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5280 int dreg = alloc_ireg (cfg);
5281 /* Decompose later to allow more optimizations */
5282 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5283 ins->type = STACK_I4;
5284 ins->flags |= MONO_INST_FAULT;
5285 cfg->cbb->has_array_access = TRUE;
5286 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5291 } else if (cmethod->klass == mono_defaults.object_class) {
5292 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
5293 int dreg = alloc_ireg_ref (cfg);
5294 int vt_reg = alloc_preg (cfg);
5295 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5296 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5297 type_from_op (cfg, ins, NULL, NULL);
5300 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
5301 int dreg = alloc_ireg (cfg);
5302 int t1 = alloc_ireg (cfg);
5304 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
5305 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
5306 ins->type = STACK_I4;
5309 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
5310 MONO_INST_NEW (cfg, ins, OP_NOP);
5311 MONO_ADD_INS (cfg->cbb, ins);
5315 } else if (cmethod->klass == mono_defaults.array_class) {
5316 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5317 return emit_array_generic_access (cfg, fsig, args, FALSE);
5318 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5319 return emit_array_generic_access (cfg, fsig, args, TRUE);
5321 #ifndef MONO_BIG_ARRAYS
5323 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
5326 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
5327 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
5328 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
5329 int dreg = alloc_ireg (cfg);
5330 int bounds_reg = alloc_ireg_mp (cfg);
5331 MonoBasicBlock *end_bb, *szarray_bb;
5332 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
5334 NEW_BBLOCK (cfg, end_bb);
5335 NEW_BBLOCK (cfg, szarray_bb);
5337 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
5338 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5339 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
5340 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
5341 /* Non-szarray case */
5343 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5344 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5346 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5347 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5348 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
5349 MONO_START_BB (cfg, szarray_bb);
5352 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5353 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5355 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5356 MONO_START_BB (cfg, end_bb);
5358 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
5359 ins->type = STACK_I4;
5365 if (cmethod->name [0] != 'g')
5368 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
5369 int dreg = alloc_ireg (cfg);
5370 int vtable_reg = alloc_preg (cfg);
5371 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
5372 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5373 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5374 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5375 type_from_op (cfg, ins, NULL, NULL);
5378 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5379 int dreg = alloc_ireg (cfg);
5381 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5382 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5383 type_from_op (cfg, ins, NULL, NULL);
5388 } else if (cmethod->klass == runtime_helpers_class) {
5389 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5390 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5392 } else if (strcmp (cmethod->name, "IsReferenceOrContainsReferences") == 0 && fsig->param_count == 0) {
5393 MonoGenericContext *ctx = mono_method_get_context (cmethod);
5395 g_assert (ctx->method_inst);
5396 g_assert (ctx->method_inst->type_argc == 1);
5397 MonoType *t = mini_get_underlying_type (ctx->method_inst->type_argv [0]);
5398 MonoClass *klass = mono_class_from_mono_type (t);
5402 mono_class_init (klass);
5403 if (MONO_TYPE_IS_REFERENCE (t))
5404 EMIT_NEW_ICONST (cfg, ins, 1);
5405 else if (MONO_TYPE_IS_PRIMITIVE (t))
5406 EMIT_NEW_ICONST (cfg, ins, 0);
5407 else if (cfg->gshared && (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR) && !mini_type_var_is_vt (t))
5408 EMIT_NEW_ICONST (cfg, ins, 1);
5409 else if (!cfg->gshared || !mini_class_check_context_used (cfg, klass))
5410 EMIT_NEW_ICONST (cfg, ins, klass->has_references ? 1 : 0);
5412 g_assert (cfg->gshared);
5414 int context_used = mini_class_check_context_used (cfg, klass);
5416 /* This returns 1 or 2 */
5417 MonoInst *info = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CLASS_IS_REF_OR_CONTAINS_REFS);
5418 int dreg = alloc_ireg (cfg);
5419 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ISUB_IMM, dreg, info->dreg, 1);
5425 } else if (cmethod->klass == mono_defaults.monitor_class) {
5426 gboolean is_enter = FALSE;
5427 gboolean is_v4 = FALSE;
5429 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
5433 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
5438 * To make async stack traces work, icalls which can block should have a wrapper.
5439 * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
5441 MonoBasicBlock *end_bb;
5443 NEW_BBLOCK (cfg, end_bb);
5445 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
5446 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
5447 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
5448 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_internal : (gpointer)mono_monitor_enter_internal, args);
5449 MONO_START_BB (cfg, end_bb);
5452 } else if (cmethod->klass == mono_defaults.thread_class) {
5453 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5454 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5455 MONO_ADD_INS (cfg->cbb, ins);
5457 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5458 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5459 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5461 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5463 if (fsig->params [0]->type == MONO_TYPE_I1)
5464 opcode = OP_LOADI1_MEMBASE;
5465 else if (fsig->params [0]->type == MONO_TYPE_U1)
5466 opcode = OP_LOADU1_MEMBASE;
5467 else if (fsig->params [0]->type == MONO_TYPE_I2)
5468 opcode = OP_LOADI2_MEMBASE;
5469 else if (fsig->params [0]->type == MONO_TYPE_U2)
5470 opcode = OP_LOADU2_MEMBASE;
5471 else if (fsig->params [0]->type == MONO_TYPE_I4)
5472 opcode = OP_LOADI4_MEMBASE;
5473 else if (fsig->params [0]->type == MONO_TYPE_U4)
5474 opcode = OP_LOADU4_MEMBASE;
5475 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5476 opcode = OP_LOADI8_MEMBASE;
5477 else if (fsig->params [0]->type == MONO_TYPE_R4)
5478 opcode = OP_LOADR4_MEMBASE;
5479 else if (fsig->params [0]->type == MONO_TYPE_R8)
5480 opcode = OP_LOADR8_MEMBASE;
5481 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5482 opcode = OP_LOAD_MEMBASE;
5485 MONO_INST_NEW (cfg, ins, opcode);
5486 ins->inst_basereg = args [0]->dreg;
5487 ins->inst_offset = 0;
5488 MONO_ADD_INS (cfg->cbb, ins);
5490 switch (fsig->params [0]->type) {
5497 ins->dreg = mono_alloc_ireg (cfg);
5498 ins->type = STACK_I4;
5502 ins->dreg = mono_alloc_lreg (cfg);
5503 ins->type = STACK_I8;
5507 ins->dreg = mono_alloc_ireg (cfg);
5508 #if SIZEOF_REGISTER == 8
5509 ins->type = STACK_I8;
5511 ins->type = STACK_I4;
5516 ins->dreg = mono_alloc_freg (cfg);
5517 ins->type = STACK_R8;
5520 g_assert (mini_type_is_reference (fsig->params [0]));
5521 ins->dreg = mono_alloc_ireg_ref (cfg);
5522 ins->type = STACK_OBJ;
5526 if (opcode == OP_LOADI8_MEMBASE)
5527 ins = mono_decompose_opcode (cfg, ins);
5529 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5533 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5535 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5537 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5538 opcode = OP_STOREI1_MEMBASE_REG;
5539 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5540 opcode = OP_STOREI2_MEMBASE_REG;
5541 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5542 opcode = OP_STOREI4_MEMBASE_REG;
5543 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5544 opcode = OP_STOREI8_MEMBASE_REG;
5545 else if (fsig->params [0]->type == MONO_TYPE_R4)
5546 opcode = OP_STORER4_MEMBASE_REG;
5547 else if (fsig->params [0]->type == MONO_TYPE_R8)
5548 opcode = OP_STORER8_MEMBASE_REG;
5549 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5550 opcode = OP_STORE_MEMBASE_REG;
5553 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5555 MONO_INST_NEW (cfg, ins, opcode);
5556 ins->sreg1 = args [1]->dreg;
5557 ins->inst_destbasereg = args [0]->dreg;
5558 ins->inst_offset = 0;
5559 MONO_ADD_INS (cfg->cbb, ins);
5561 if (opcode == OP_STOREI8_MEMBASE_REG)
5562 ins = mono_decompose_opcode (cfg, ins);
5567 } else if (cmethod->klass->image == mono_defaults.corlib &&
5568 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5569 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5572 #if SIZEOF_REGISTER == 8
5573 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5574 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5575 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5576 ins->dreg = mono_alloc_preg (cfg);
5577 ins->sreg1 = args [0]->dreg;
5578 ins->type = STACK_I8;
5579 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5580 MONO_ADD_INS (cfg->cbb, ins);
5584 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5586 /* 64 bit reads are already atomic */
5587 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5588 load_ins->dreg = mono_alloc_preg (cfg);
5589 load_ins->inst_basereg = args [0]->dreg;
5590 load_ins->inst_offset = 0;
5591 load_ins->type = STACK_I8;
5592 MONO_ADD_INS (cfg->cbb, load_ins);
5594 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5601 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
5602 MonoInst *ins_iconst;
5605 if (fsig->params [0]->type == MONO_TYPE_I4) {
5606 opcode = OP_ATOMIC_ADD_I4;
5607 cfg->has_atomic_add_i4 = TRUE;
5609 #if SIZEOF_REGISTER == 8
5610 else if (fsig->params [0]->type == MONO_TYPE_I8)
5611 opcode = OP_ATOMIC_ADD_I8;
5614 if (!mono_arch_opcode_supported (opcode))
5616 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5617 ins_iconst->inst_c0 = 1;
5618 ins_iconst->dreg = mono_alloc_ireg (cfg);
5619 MONO_ADD_INS (cfg->cbb, ins_iconst);
5621 MONO_INST_NEW (cfg, ins, opcode);
5622 ins->dreg = mono_alloc_ireg (cfg);
5623 ins->inst_basereg = args [0]->dreg;
5624 ins->inst_offset = 0;
5625 ins->sreg2 = ins_iconst->dreg;
5626 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5627 MONO_ADD_INS (cfg->cbb, ins);
5629 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
5630 MonoInst *ins_iconst;
5633 if (fsig->params [0]->type == MONO_TYPE_I4) {
5634 opcode = OP_ATOMIC_ADD_I4;
5635 cfg->has_atomic_add_i4 = TRUE;
5637 #if SIZEOF_REGISTER == 8
5638 else if (fsig->params [0]->type == MONO_TYPE_I8)
5639 opcode = OP_ATOMIC_ADD_I8;
5642 if (!mono_arch_opcode_supported (opcode))
5644 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5645 ins_iconst->inst_c0 = -1;
5646 ins_iconst->dreg = mono_alloc_ireg (cfg);
5647 MONO_ADD_INS (cfg->cbb, ins_iconst);
5649 MONO_INST_NEW (cfg, ins, opcode);
5650 ins->dreg = mono_alloc_ireg (cfg);
5651 ins->inst_basereg = args [0]->dreg;
5652 ins->inst_offset = 0;
5653 ins->sreg2 = ins_iconst->dreg;
5654 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5655 MONO_ADD_INS (cfg->cbb, ins);
5657 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
5660 if (fsig->params [0]->type == MONO_TYPE_I4) {
5661 opcode = OP_ATOMIC_ADD_I4;
5662 cfg->has_atomic_add_i4 = TRUE;
5664 #if SIZEOF_REGISTER == 8
5665 else if (fsig->params [0]->type == MONO_TYPE_I8)
5666 opcode = OP_ATOMIC_ADD_I8;
5669 if (!mono_arch_opcode_supported (opcode))
5671 MONO_INST_NEW (cfg, ins, opcode);
5672 ins->dreg = mono_alloc_ireg (cfg);
5673 ins->inst_basereg = args [0]->dreg;
5674 ins->inst_offset = 0;
5675 ins->sreg2 = args [1]->dreg;
5676 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5677 MONO_ADD_INS (cfg->cbb, ins);
5680 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
5681 MonoInst *f2i = NULL, *i2f;
5682 guint32 opcode, f2i_opcode, i2f_opcode;
5683 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5684 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
5686 if (fsig->params [0]->type == MONO_TYPE_I4 ||
5687 fsig->params [0]->type == MONO_TYPE_R4) {
5688 opcode = OP_ATOMIC_EXCHANGE_I4;
5689 f2i_opcode = OP_MOVE_F_TO_I4;
5690 i2f_opcode = OP_MOVE_I4_TO_F;
5691 cfg->has_atomic_exchange_i4 = TRUE;
5693 #if SIZEOF_REGISTER == 8
5695 fsig->params [0]->type == MONO_TYPE_I8 ||
5696 fsig->params [0]->type == MONO_TYPE_R8 ||
5697 fsig->params [0]->type == MONO_TYPE_I) {
5698 opcode = OP_ATOMIC_EXCHANGE_I8;
5699 f2i_opcode = OP_MOVE_F_TO_I8;
5700 i2f_opcode = OP_MOVE_I8_TO_F;
5703 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
5704 opcode = OP_ATOMIC_EXCHANGE_I4;
5705 cfg->has_atomic_exchange_i4 = TRUE;
5711 if (!mono_arch_opcode_supported (opcode))
5715 /* TODO: Decompose these opcodes instead of bailing here. */
5716 if (COMPILE_SOFT_FLOAT (cfg))
5719 MONO_INST_NEW (cfg, f2i, f2i_opcode);
5720 f2i->dreg = mono_alloc_ireg (cfg);
5721 f2i->sreg1 = args [1]->dreg;
5722 if (f2i_opcode == OP_MOVE_F_TO_I4)
5723 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5724 MONO_ADD_INS (cfg->cbb, f2i);
5727 MONO_INST_NEW (cfg, ins, opcode);
5728 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
5729 ins->inst_basereg = args [0]->dreg;
5730 ins->inst_offset = 0;
5731 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
5732 MONO_ADD_INS (cfg->cbb, ins);
5734 switch (fsig->params [0]->type) {
5736 ins->type = STACK_I4;
5739 ins->type = STACK_I8;
5742 #if SIZEOF_REGISTER == 8
5743 ins->type = STACK_I8;
5745 ins->type = STACK_I4;
5750 ins->type = STACK_R8;
5753 g_assert (mini_type_is_reference (fsig->params [0]));
5754 ins->type = STACK_OBJ;
5759 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5760 i2f->dreg = mono_alloc_freg (cfg);
5761 i2f->sreg1 = ins->dreg;
5762 i2f->type = STACK_R8;
5763 if (i2f_opcode == OP_MOVE_I4_TO_F)
5764 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5765 MONO_ADD_INS (cfg->cbb, i2f);
5770 if (cfg->gen_write_barriers && is_ref)
5771 emit_write_barrier (cfg, args [0], args [1]);
5773 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
5774 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
5775 guint32 opcode, f2i_opcode, i2f_opcode;
5776 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
5777 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
5779 if (fsig->params [1]->type == MONO_TYPE_I4 ||
5780 fsig->params [1]->type == MONO_TYPE_R4) {
5781 opcode = OP_ATOMIC_CAS_I4;
5782 f2i_opcode = OP_MOVE_F_TO_I4;
5783 i2f_opcode = OP_MOVE_I4_TO_F;
5784 cfg->has_atomic_cas_i4 = TRUE;
5786 #if SIZEOF_REGISTER == 8
5788 fsig->params [1]->type == MONO_TYPE_I8 ||
5789 fsig->params [1]->type == MONO_TYPE_R8 ||
5790 fsig->params [1]->type == MONO_TYPE_I) {
5791 opcode = OP_ATOMIC_CAS_I8;
5792 f2i_opcode = OP_MOVE_F_TO_I8;
5793 i2f_opcode = OP_MOVE_I8_TO_F;
5796 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
5797 opcode = OP_ATOMIC_CAS_I4;
5798 cfg->has_atomic_cas_i4 = TRUE;
5804 if (!mono_arch_opcode_supported (opcode))
5808 /* TODO: Decompose these opcodes instead of bailing here. */
5809 if (COMPILE_SOFT_FLOAT (cfg))
5812 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
5813 f2i_new->dreg = mono_alloc_ireg (cfg);
5814 f2i_new->sreg1 = args [1]->dreg;
5815 if (f2i_opcode == OP_MOVE_F_TO_I4)
5816 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5817 MONO_ADD_INS (cfg->cbb, f2i_new);
5819 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
5820 f2i_cmp->dreg = mono_alloc_ireg (cfg);
5821 f2i_cmp->sreg1 = args [2]->dreg;
5822 if (f2i_opcode == OP_MOVE_F_TO_I4)
5823 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5824 MONO_ADD_INS (cfg->cbb, f2i_cmp);
5827 MONO_INST_NEW (cfg, ins, opcode);
5828 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
5829 ins->sreg1 = args [0]->dreg;
5830 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
5831 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
5832 MONO_ADD_INS (cfg->cbb, ins);
5834 switch (fsig->params [1]->type) {
5836 ins->type = STACK_I4;
5839 ins->type = STACK_I8;
5842 #if SIZEOF_REGISTER == 8
5843 ins->type = STACK_I8;
5845 ins->type = STACK_I4;
5849 ins->type = cfg->r4_stack_type;
5852 ins->type = STACK_R8;
5855 g_assert (mini_type_is_reference (fsig->params [1]));
5856 ins->type = STACK_OBJ;
5861 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5862 i2f->dreg = mono_alloc_freg (cfg);
5863 i2f->sreg1 = ins->dreg;
5864 i2f->type = STACK_R8;
5865 if (i2f_opcode == OP_MOVE_I4_TO_F)
5866 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5867 MONO_ADD_INS (cfg->cbb, i2f);
5872 if (cfg->gen_write_barriers && is_ref)
5873 emit_write_barrier (cfg, args [0], args [1]);
5875 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
5876 fsig->params [1]->type == MONO_TYPE_I4) {
5877 MonoInst *cmp, *ceq;
5879 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
5882 /* int32 r = CAS (location, value, comparand); */
5883 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
5884 ins->dreg = alloc_ireg (cfg);
5885 ins->sreg1 = args [0]->dreg;
5886 ins->sreg2 = args [1]->dreg;
5887 ins->sreg3 = args [2]->dreg;
5888 ins->type = STACK_I4;
5889 MONO_ADD_INS (cfg->cbb, ins);
5891 /* bool result = r == comparand; */
5892 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
5893 cmp->sreg1 = ins->dreg;
5894 cmp->sreg2 = args [2]->dreg;
5895 cmp->type = STACK_I4;
5896 MONO_ADD_INS (cfg->cbb, cmp);
5898 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
5899 ceq->dreg = alloc_ireg (cfg);
5900 ceq->type = STACK_I4;
5901 MONO_ADD_INS (cfg->cbb, ceq);
5903 /* *success = result; */
5904 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
5906 cfg->has_atomic_cas_i4 = TRUE;
5908 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
5909 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5913 } else if (cmethod->klass->image == mono_defaults.corlib &&
5914 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5915 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
5918 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
5920 MonoType *t = fsig->params [0];
5922 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
5924 g_assert (t->byref);
5925 /* t is a byref type, so the reference check is more complicated */
5926 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5927 if (t->type == MONO_TYPE_I1)
5928 opcode = OP_ATOMIC_LOAD_I1;
5929 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5930 opcode = OP_ATOMIC_LOAD_U1;
5931 else if (t->type == MONO_TYPE_I2)
5932 opcode = OP_ATOMIC_LOAD_I2;
5933 else if (t->type == MONO_TYPE_U2)
5934 opcode = OP_ATOMIC_LOAD_U2;
5935 else if (t->type == MONO_TYPE_I4)
5936 opcode = OP_ATOMIC_LOAD_I4;
5937 else if (t->type == MONO_TYPE_U4)
5938 opcode = OP_ATOMIC_LOAD_U4;
5939 else if (t->type == MONO_TYPE_R4)
5940 opcode = OP_ATOMIC_LOAD_R4;
5941 else if (t->type == MONO_TYPE_R8)
5942 opcode = OP_ATOMIC_LOAD_R8;
5943 #if SIZEOF_REGISTER == 8
5944 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5945 opcode = OP_ATOMIC_LOAD_I8;
5946 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5947 opcode = OP_ATOMIC_LOAD_U8;
5949 else if (t->type == MONO_TYPE_I)
5950 opcode = OP_ATOMIC_LOAD_I4;
5951 else if (is_ref || t->type == MONO_TYPE_U)
5952 opcode = OP_ATOMIC_LOAD_U4;
5956 if (!mono_arch_opcode_supported (opcode))
5959 MONO_INST_NEW (cfg, ins, opcode);
5960 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
5961 ins->sreg1 = args [0]->dreg;
5962 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
5963 MONO_ADD_INS (cfg->cbb, ins);
5966 case MONO_TYPE_BOOLEAN:
5973 ins->type = STACK_I4;
5977 ins->type = STACK_I8;
5981 #if SIZEOF_REGISTER == 8
5982 ins->type = STACK_I8;
5984 ins->type = STACK_I4;
5988 ins->type = cfg->r4_stack_type;
5991 ins->type = STACK_R8;
5995 ins->type = STACK_OBJ;
6001 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6003 MonoType *t = fsig->params [0];
6006 g_assert (t->byref);
6007 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6008 if (t->type == MONO_TYPE_I1)
6009 opcode = OP_ATOMIC_STORE_I1;
6010 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6011 opcode = OP_ATOMIC_STORE_U1;
6012 else if (t->type == MONO_TYPE_I2)
6013 opcode = OP_ATOMIC_STORE_I2;
6014 else if (t->type == MONO_TYPE_U2)
6015 opcode = OP_ATOMIC_STORE_U2;
6016 else if (t->type == MONO_TYPE_I4)
6017 opcode = OP_ATOMIC_STORE_I4;
6018 else if (t->type == MONO_TYPE_U4)
6019 opcode = OP_ATOMIC_STORE_U4;
6020 else if (t->type == MONO_TYPE_R4)
6021 opcode = OP_ATOMIC_STORE_R4;
6022 else if (t->type == MONO_TYPE_R8)
6023 opcode = OP_ATOMIC_STORE_R8;
6024 #if SIZEOF_REGISTER == 8
6025 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6026 opcode = OP_ATOMIC_STORE_I8;
6027 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6028 opcode = OP_ATOMIC_STORE_U8;
6030 else if (t->type == MONO_TYPE_I)
6031 opcode = OP_ATOMIC_STORE_I4;
6032 else if (is_ref || t->type == MONO_TYPE_U)
6033 opcode = OP_ATOMIC_STORE_U4;
6037 if (!mono_arch_opcode_supported (opcode))
6040 MONO_INST_NEW (cfg, ins, opcode);
6041 ins->dreg = args [0]->dreg;
6042 ins->sreg1 = args [1]->dreg;
6043 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6044 MONO_ADD_INS (cfg->cbb, ins);
6046 if (cfg->gen_write_barriers && is_ref)
6047 emit_write_barrier (cfg, args [0], args [1]);
6053 } else if (cmethod->klass->image == mono_defaults.corlib &&
6054 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6055 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6056 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6057 if (should_insert_brekpoint (cfg->method)) {
6058 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6060 MONO_INST_NEW (cfg, ins, OP_NOP);
6061 MONO_ADD_INS (cfg->cbb, ins);
6065 } else if (cmethod->klass->image == mono_defaults.corlib &&
6066 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6067 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6068 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6070 EMIT_NEW_ICONST (cfg, ins, 1);
6072 EMIT_NEW_ICONST (cfg, ins, 0);
6075 } else if (cmethod->klass->image == mono_defaults.corlib &&
6076 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6077 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6078 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6079 /* No stack walks are currently available, so implement this as an intrinsic */
6080 MonoInst *assembly_ins;
6082 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6083 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6086 } else if (cmethod->klass->image == mono_defaults.corlib &&
6087 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6088 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6089 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6090 /* No stack walks are currently available, so implement this as an intrinsic */
6091 MonoInst *method_ins;
6092 MonoMethod *declaring = cfg->method;
6094 /* This returns the declaring generic method */
6095 if (declaring->is_inflated)
6096 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6097 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6098 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6099 cfg->no_inline = TRUE;
6100 if (cfg->method != cfg->current_method)
6101 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6104 } else if (cmethod->klass == mono_defaults.math_class) {
6106 * There is general branchless code for Min/Max, but it does not work for
6108 * http://everything2.com/?node_id=1051618
6110 } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
6111 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
6112 MONO_INST_NEW (cfg, ins, OP_PCEQ);
6113 ins->dreg = alloc_preg (cfg);
6114 ins->type = STACK_I4;
6115 MONO_ADD_INS (cfg->cbb, ins);
6117 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6118 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6119 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6120 !strcmp (cmethod->klass->name, "Selector")) ||
6121 ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
6122 !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
6123 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6124 !strcmp (cmethod->klass->name, "Selector"))
6126 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
6127 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6128 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6131 MonoJumpInfoToken *ji;
6134 if (args [0]->opcode == OP_GOT_ENTRY) {
6135 pi = (MonoInst *)args [0]->inst_p1;
6136 g_assert (pi->opcode == OP_PATCH_INFO);
6137 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6138 ji = (MonoJumpInfoToken *)pi->inst_p0;
6140 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6141 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6144 NULLIFY_INS (args [0]);
6146 s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
6147 return_val_if_nok (&cfg->error, NULL);
6149 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6150 ins->dreg = mono_alloc_ireg (cfg);
6153 MONO_ADD_INS (cfg->cbb, ins);
6158 #ifdef MONO_ARCH_SIMD_INTRINSICS
6159 if (cfg->opt & MONO_OPT_SIMD) {
6160 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6166 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6170 if (COMPILE_LLVM (cfg)) {
6171 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6176 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6180 * This entry point could be used later for arbitrary method
6183 inline static MonoInst*
6184 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6185 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6187 if (method->klass == mono_defaults.string_class) {
6188 /* managed string allocation support */
6189 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6190 MonoInst *iargs [2];
6191 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6192 MonoMethod *managed_alloc = NULL;
6194 g_assert (vtable); /*Should not fail since it System.String*/
6195 #ifndef MONO_CROSS_COMPILE
6196 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6200 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6201 iargs [1] = args [0];
6202 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6209 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6211 MonoInst *store, *temp;
6214 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6215 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6218 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6219 * would be different than the MonoInst's used to represent arguments, and
6220 * the ldelema implementation can't deal with that.
6221 * Solution: When ldelema is used on an inline argument, create a var for
6222 * it, emit ldelema on that var, and emit the saving code below in
6223 * inline_method () if needed.
6225 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6226 cfg->args [i] = temp;
6227 /* This uses cfg->args [i] which is set by the preceeding line */
6228 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6229 store->cil_code = sp [0]->cil_code;
6234 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6235 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6237 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6239 check_inline_called_method_name_limit (MonoMethod *called_method)
6242 static const char *limit = NULL;
6244 if (limit == NULL) {
6245 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6247 if (limit_string != NULL)
6248 limit = limit_string;
6253 if (limit [0] != '\0') {
6254 char *called_method_name = mono_method_full_name (called_method, TRUE);
6256 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6257 g_free (called_method_name);
6259 //return (strncmp_result <= 0);
6260 return (strncmp_result == 0);
6267 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6269 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6272 static const char *limit = NULL;
6274 if (limit == NULL) {
6275 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6276 if (limit_string != NULL) {
6277 limit = limit_string;
6283 if (limit [0] != '\0') {
6284 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6286 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6287 g_free (caller_method_name);
6289 //return (strncmp_result <= 0);
6290 return (strncmp_result == 0);
6298 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6300 static double r8_0 = 0.0;
6301 static float r4_0 = 0.0;
6305 rtype = mini_get_underlying_type (rtype);
6309 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6310 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6311 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6312 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6313 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6314 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6315 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6316 ins->type = STACK_R4;
6317 ins->inst_p0 = (void*)&r4_0;
6319 MONO_ADD_INS (cfg->cbb, ins);
6320 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6321 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6322 ins->type = STACK_R8;
6323 ins->inst_p0 = (void*)&r8_0;
6325 MONO_ADD_INS (cfg->cbb, ins);
6326 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6327 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6328 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6329 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6330 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6332 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6337 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6341 rtype = mini_get_underlying_type (rtype);
6345 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6346 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6347 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6348 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6349 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6350 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6351 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6352 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6353 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6354 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6355 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6356 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6357 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6358 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6360 emit_init_rvar (cfg, dreg, rtype);
6364 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6366 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6368 MonoInst *var = cfg->locals [local];
6369 if (COMPILE_SOFT_FLOAT (cfg)) {
6371 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
6372 emit_init_rvar (cfg, reg, type);
6373 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6376 emit_init_rvar (cfg, var->dreg, type);
6378 emit_dummy_init_rvar (cfg, var->dreg, type);
6383 mini_inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, guchar *ip, guint real_offset, gboolean inline_always)
6385 return inline_method (cfg, cmethod, fsig, sp, ip, real_offset, inline_always);
6391 * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
6394 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6395 guchar *ip, guint real_offset, gboolean inline_always)
6398 MonoInst *ins, *rvar = NULL;
6399 MonoMethodHeader *cheader;
6400 MonoBasicBlock *ebblock, *sbblock;
6402 MonoMethod *prev_inlined_method;
6403 MonoInst **prev_locals, **prev_args;
6404 MonoType **prev_arg_types;
6405 guint prev_real_offset;
6406 GHashTable *prev_cbb_hash;
6407 MonoBasicBlock **prev_cil_offset_to_bb;
6408 MonoBasicBlock *prev_cbb;
6409 const unsigned char *prev_ip;
6410 unsigned char *prev_cil_start;
6411 guint32 prev_cil_offset_to_bb_len;
6412 MonoMethod *prev_current_method;
6413 MonoGenericContext *prev_generic_context;
6414 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
6416 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6418 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6419 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6422 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6423 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6428 fsig = mono_method_signature (cmethod);
6430 if (cfg->verbose_level > 2)
6431 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6433 if (!cmethod->inline_info) {
6434 cfg->stat_inlineable_methods++;
6435 cmethod->inline_info = 1;
6438 /* allocate local variables */
6439 cheader = mono_method_get_header_checked (cmethod, &error);
6441 if (inline_always) {
6442 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
6443 mono_error_move (&cfg->error, &error);
6445 mono_error_cleanup (&error);
6450 /*Must verify before creating locals as it can cause the JIT to assert.*/
6451 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6452 mono_metadata_free_mh (cheader);
6456 /* allocate space to store the return value */
6457 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6458 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6461 prev_locals = cfg->locals;
6462 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6463 for (i = 0; i < cheader->num_locals; ++i)
6464 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6466 /* allocate start and end blocks */
6467 /* This is needed so if the inline is aborted, we can clean up */
6468 NEW_BBLOCK (cfg, sbblock);
6469 sbblock->real_offset = real_offset;
6471 NEW_BBLOCK (cfg, ebblock);
6472 ebblock->block_num = cfg->num_bblocks++;
6473 ebblock->real_offset = real_offset;
6475 prev_args = cfg->args;
6476 prev_arg_types = cfg->arg_types;
6477 prev_inlined_method = cfg->inlined_method;
6478 cfg->inlined_method = cmethod;
6479 cfg->ret_var_set = FALSE;
6480 cfg->inline_depth ++;
6481 prev_real_offset = cfg->real_offset;
6482 prev_cbb_hash = cfg->cbb_hash;
6483 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6484 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6485 prev_cil_start = cfg->cil_start;
6487 prev_cbb = cfg->cbb;
6488 prev_current_method = cfg->current_method;
6489 prev_generic_context = cfg->generic_context;
6490 prev_ret_var_set = cfg->ret_var_set;
6491 prev_disable_inline = cfg->disable_inline;
6493 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6496 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
6498 ret_var_set = cfg->ret_var_set;
6500 cfg->inlined_method = prev_inlined_method;
6501 cfg->real_offset = prev_real_offset;
6502 cfg->cbb_hash = prev_cbb_hash;
6503 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6504 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6505 cfg->cil_start = prev_cil_start;
6507 cfg->locals = prev_locals;
6508 cfg->args = prev_args;
6509 cfg->arg_types = prev_arg_types;
6510 cfg->current_method = prev_current_method;
6511 cfg->generic_context = prev_generic_context;
6512 cfg->ret_var_set = prev_ret_var_set;
6513 cfg->disable_inline = prev_disable_inline;
6514 cfg->inline_depth --;
6516 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6517 if (cfg->verbose_level > 2)
6518 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6520 cfg->stat_inlined_methods++;
6522 /* always add some code to avoid block split failures */
6523 MONO_INST_NEW (cfg, ins, OP_NOP);
6524 MONO_ADD_INS (prev_cbb, ins);
6526 prev_cbb->next_bb = sbblock;
6527 link_bblock (cfg, prev_cbb, sbblock);
6530 * Get rid of the begin and end bblocks if possible to aid local
6533 if (prev_cbb->out_count == 1)
6534 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6536 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6537 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6539 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6540 MonoBasicBlock *prev = ebblock->in_bb [0];
6542 if (prev->next_bb == ebblock) {
6543 mono_merge_basic_blocks (cfg, prev, ebblock);
6545 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6546 mono_merge_basic_blocks (cfg, prev_cbb, prev);
6547 cfg->cbb = prev_cbb;
6550 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
6555 * Its possible that the rvar is set in some prev bblock, but not in others.
6561 for (i = 0; i < ebblock->in_count; ++i) {
6562 bb = ebblock->in_bb [i];
6564 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6567 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6577 * If the inlined method contains only a throw, then the ret var is not
6578 * set, so set it to a dummy value.
6581 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6583 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6586 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6589 if (cfg->verbose_level > 2)
6590 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6591 cfg->exception_type = MONO_EXCEPTION_NONE;
6593 /* This gets rid of the newly added bblocks */
6594 cfg->cbb = prev_cbb;
6596 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6601 * Some of these comments may well be out-of-date.
6602 * Design decisions: we do a single pass over the IL code (and we do bblock
6603 * splitting/merging in the few cases when it's required: a back jump to an IL
6604 * address that was not already seen as bblock starting point).
6605 * Code is validated as we go (full verification is still better left to metadata/verify.c).
6606 * Complex operations are decomposed in simpler ones right away. We need to let the
6607 * arch-specific code peek and poke inside this process somehow (except when the
6608 * optimizations can take advantage of the full semantic info of coarse opcodes).
6609 * All the opcodes of the form opcode.s are 'normalized' to opcode.
6610 * MonoInst->opcode initially is the IL opcode or some simplification of that
6611 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
6612 * opcode with value bigger than OP_LAST.
6613 * At this point the IR can be handed over to an interpreter, a dumb code generator
6614 * or to the optimizing code generator that will translate it to SSA form.
6616 * Profiling directed optimizations.
6617 * We may compile by default with few or no optimizations and instrument the code
6618 * or the user may indicate what methods to optimize the most either in a config file
6619 * or through repeated runs where the compiler applies offline the optimizations to
6620 * each method and then decides if it was worth it.
6623 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6624 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6625 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6626 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6627 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6628 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6629 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6630 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
6632 /* offset from br.s -> br like opcodes */
6633 #define BIG_BRANCH_OFFSET 13
6636 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6638 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6640 return b == NULL || b == bb;
6644 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6646 unsigned char *ip = start;
6647 unsigned char *target;
6650 MonoBasicBlock *bblock;
6651 const MonoOpcode *opcode;
6654 cli_addr = ip - start;
6655 i = mono_opcode_value ((const guint8 **)&ip, end);
6658 opcode = &mono_opcodes [i];
6659 switch (opcode->argument) {
6660 case MonoInlineNone:
6663 case MonoInlineString:
6664 case MonoInlineType:
6665 case MonoInlineField:
6666 case MonoInlineMethod:
6669 case MonoShortInlineR:
6676 case MonoShortInlineVar:
6677 case MonoShortInlineI:
6680 case MonoShortInlineBrTarget:
6681 target = start + cli_addr + 2 + (signed char)ip [1];
6682 GET_BBLOCK (cfg, bblock, target);
6685 GET_BBLOCK (cfg, bblock, ip);
6687 case MonoInlineBrTarget:
6688 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
6689 GET_BBLOCK (cfg, bblock, target);
6692 GET_BBLOCK (cfg, bblock, ip);
6694 case MonoInlineSwitch: {
6695 guint32 n = read32 (ip + 1);
6698 cli_addr += 5 + 4 * n;
6699 target = start + cli_addr;
6700 GET_BBLOCK (cfg, bblock, target);
6702 for (j = 0; j < n; ++j) {
6703 target = start + cli_addr + (gint32)read32 (ip);
6704 GET_BBLOCK (cfg, bblock, target);
6714 g_assert_not_reached ();
6717 if (i == CEE_THROW) {
6718 unsigned char *bb_start = ip - 1;
6720 /* Find the start of the bblock containing the throw */
6722 while ((bb_start >= start) && !bblock) {
6723 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
6727 bblock->out_of_line = 1;
6737 static inline MonoMethod *
6738 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
6744 if (m->wrapper_type != MONO_WRAPPER_NONE) {
6745 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
6747 method = mono_class_inflate_generic_method_checked (method, context, error);
6750 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
6756 static inline MonoMethod *
6757 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
6760 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
6762 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
6763 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
6767 if (!method && !cfg)
6768 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6773 static inline MonoClass*
6774 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
6779 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6780 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6782 klass = mono_class_inflate_generic_class_checked (klass, context, &error);
6783 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6786 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
6787 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6790 mono_class_init (klass);
6794 static inline MonoMethodSignature*
6795 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
6797 MonoMethodSignature *fsig;
6800 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6801 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6803 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
6804 return_val_if_nok (error, NULL);
6807 fsig = mono_inflate_generic_signature(fsig, context, error);
6813 throw_exception (void)
6815 static MonoMethod *method = NULL;
6818 MonoSecurityManager *secman = mono_security_manager_get_methods ();
6819 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
6826 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
6828 MonoMethod *thrower = throw_exception ();
6831 EMIT_NEW_PCONST (cfg, args [0], ex);
6832 mono_emit_method_call (cfg, thrower, args, NULL);
6836 * Return the original method is a wrapper is specified. We can only access
6837 * the custom attributes from the original method.
6840 get_original_method (MonoMethod *method)
6842 if (method->wrapper_type == MONO_WRAPPER_NONE)
6845 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
6846 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
6849 /* in other cases we need to find the original method */
6850 return mono_marshal_method_from_wrapper (method);
6854 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
6856 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6857 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
6859 emit_throw_exception (cfg, ex);
6863 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
6865 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6866 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
6868 emit_throw_exception (cfg, ex);
6872 * Check that the IL instructions at ip are the array initialization
6873 * sequence and return the pointer to the data and the size.
6876 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
6879 * newarr[System.Int32]
6881 * ldtoken field valuetype ...
6882 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
6884 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
6886 guint32 token = read32 (ip + 7);
6887 guint32 field_token = read32 (ip + 2);
6888 guint32 field_index = field_token & 0xffffff;
6890 const char *data_ptr;
6892 MonoMethod *cmethod;
6893 MonoClass *dummy_class;
6894 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
6898 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6902 *out_field_token = field_token;
6904 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
6907 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
6909 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
6910 case MONO_TYPE_BOOLEAN:
6914 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
6915 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
6916 case MONO_TYPE_CHAR:
6933 if (size > mono_type_size (field->type, &dummy_align))
6936 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
6937 if (!image_is_dynamic (method->klass->image)) {
6938 field_index = read32 (ip + 2) & 0xffffff;
6939 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
6940 data_ptr = mono_image_rva_map (method->klass->image, rva);
6941 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
6942 /* for aot code we do the lookup on load */
6943 if (aot && data_ptr)
6944 return (const char *)GUINT_TO_POINTER (rva);
6946 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
6948 data_ptr = mono_field_get_data (field);
6956 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
6959 char *method_fname = mono_method_full_name (method, TRUE);
6961 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
6964 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
6965 mono_error_cleanup (&error);
6966 } else if (header->code_size == 0)
6967 method_code = g_strdup ("method body is empty.");
6969 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
6970 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
6971 g_free (method_fname);
6972 g_free (method_code);
6973 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
6977 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
6980 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
6981 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
6982 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
6983 /* Optimize reg-reg moves away */
6985 * Can't optimize other opcodes, since sp[0] might point to
6986 * the last ins of a decomposed opcode.
6988 sp [0]->dreg = (cfg)->locals [n]->dreg;
6990 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
6995 * ldloca inhibits many optimizations so try to get rid of it in common
6998 static inline unsigned char *
6999 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7009 local = read16 (ip + 2);
7013 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7014 /* From the INITOBJ case */
7015 token = read32 (ip + 2);
7016 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7017 CHECK_TYPELOAD (klass);
7018 type = mini_get_underlying_type (&klass->byval_arg);
7019 emit_init_local (cfg, local, type, TRUE);
7027 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7029 MonoInst *icall_args [16];
7030 MonoInst *call_target, *ins, *vtable_ins;
7031 int arg_reg, this_reg, vtable_reg;
7032 gboolean is_iface = mono_class_is_interface (cmethod->klass);
7033 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7034 gboolean variant_iface = FALSE;
7037 gboolean special_array_interface = cmethod->klass->is_array_special_interface;
7040 * In llvm-only mode, vtables contain function descriptors instead of
7041 * method addresses/trampolines.
7043 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7046 slot = mono_method_get_imt_slot (cmethod);
7048 slot = mono_method_get_vtable_index (cmethod);
7050 this_reg = sp [0]->dreg;
7052 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7053 variant_iface = TRUE;
7055 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7057 * The simplest case, a normal virtual call.
7059 int slot_reg = alloc_preg (cfg);
7060 int addr_reg = alloc_preg (cfg);
7061 int arg_reg = alloc_preg (cfg);
7062 MonoBasicBlock *non_null_bb;
7064 vtable_reg = alloc_preg (cfg);
7065 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7066 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7068 /* Load the vtable slot, which contains a function descriptor. */
7069 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7071 NEW_BBLOCK (cfg, non_null_bb);
7073 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7074 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7075 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7078 // FIXME: Make the wrapper use the preserveall cconv
7079 // FIXME: Use one icall per slot for small slot numbers ?
7080 icall_args [0] = vtable_ins;
7081 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7082 /* Make the icall return the vtable slot value to save some code space */
7083 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7084 ins->dreg = slot_reg;
7085 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7088 MONO_START_BB (cfg, non_null_bb);
7089 /* Load the address + arg from the vtable slot */
7090 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7091 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7093 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7096 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt && !special_array_interface) {
7098 * A simple interface call
7100 * We make a call through an imt slot to obtain the function descriptor we need to call.
7101 * The imt slot contains a function descriptor for a runtime function + arg.
7103 int slot_reg = alloc_preg (cfg);
7104 int addr_reg = alloc_preg (cfg);
7105 int arg_reg = alloc_preg (cfg);
7106 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7108 vtable_reg = alloc_preg (cfg);
7109 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7110 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7113 * The slot is already initialized when the vtable is created so there is no need
7117 /* Load the imt slot, which contains a function descriptor. */
7118 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7120 /* Load the address + arg of the imt thunk from the imt slot */
7121 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7122 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7124 * IMT thunks in llvm-only mode are C functions which take an info argument
7125 * plus the imt method and return the ftndesc to call.
7127 icall_args [0] = thunk_arg_ins;
7128 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7129 cmethod, MONO_RGCTX_INFO_METHOD);
7130 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7132 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7135 if ((fsig->generic_param_count || variant_iface || special_array_interface) && !is_gsharedvt) {
7137 * This is similar to the interface case, the vtable slot points to an imt thunk which is
7138 * dynamically extended as more instantiations are discovered.
7139 * This handles generic virtual methods both on classes and interfaces.
7141 int slot_reg = alloc_preg (cfg);
7142 int addr_reg = alloc_preg (cfg);
7143 int arg_reg = alloc_preg (cfg);
7144 int ftndesc_reg = alloc_preg (cfg);
7145 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7146 MonoBasicBlock *slowpath_bb, *end_bb;
7148 NEW_BBLOCK (cfg, slowpath_bb);
7149 NEW_BBLOCK (cfg, end_bb);
7151 vtable_reg = alloc_preg (cfg);
7152 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7154 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7156 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7158 /* Load the slot, which contains a function descriptor. */
7159 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7161 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7162 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7163 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7164 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7167 /* Same as with iface calls */
7168 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7169 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7170 icall_args [0] = thunk_arg_ins;
7171 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7172 cmethod, MONO_RGCTX_INFO_METHOD);
7173 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7174 ftndesc_ins->dreg = ftndesc_reg;
7176 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7177 * they don't know about yet. Fall back to the slowpath in that case.
7179 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7180 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7182 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7185 MONO_START_BB (cfg, slowpath_bb);
7186 icall_args [0] = vtable_ins;
7187 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7188 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7189 cmethod, MONO_RGCTX_INFO_METHOD);
7191 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7193 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7194 ftndesc_ins->dreg = ftndesc_reg;
7195 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7198 MONO_START_BB (cfg, end_bb);
7199 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7203 * Non-optimized cases
7205 icall_args [0] = sp [0];
7206 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7208 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7209 cmethod, MONO_RGCTX_INFO_METHOD);
7211 arg_reg = alloc_preg (cfg);
7212 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7213 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7215 g_assert (is_gsharedvt);
7217 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7219 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7222 * Pass the extra argument even if the callee doesn't receive it, most
7223 * calling conventions allow this.
7225 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7229 is_exception_class (MonoClass *klass)
7232 if (klass == mono_defaults.exception_class)
7234 klass = klass->parent;
7240 * is_jit_optimizer_disabled:
7242 * Determine whenever M's assembly has a DebuggableAttribute with the
7243 * IsJITOptimizerDisabled flag set.
7246 is_jit_optimizer_disabled (MonoMethod *m)
7249 MonoAssembly *ass = m->klass->image->assembly;
7250 MonoCustomAttrInfo* attrs;
7253 gboolean val = FALSE;
7256 if (ass->jit_optimizer_disabled_inited)
7257 return ass->jit_optimizer_disabled;
7259 klass = mono_class_try_get_debuggable_attribute_class ();
7263 ass->jit_optimizer_disabled = FALSE;
7264 mono_memory_barrier ();
7265 ass->jit_optimizer_disabled_inited = TRUE;
7269 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
7270 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7272 for (i = 0; i < attrs->num_attrs; ++i) {
7273 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7275 MonoMethodSignature *sig;
7277 if (!attr->ctor || attr->ctor->klass != klass)
7279 /* Decode the attribute. See reflection.c */
7280 p = (const char*)attr->data;
7281 g_assert (read16 (p) == 0x0001);
7284 // FIXME: Support named parameters
7285 sig = mono_method_signature (attr->ctor);
7286 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7288 /* Two boolean arguments */
7292 mono_custom_attrs_free (attrs);
7295 ass->jit_optimizer_disabled = val;
7296 mono_memory_barrier ();
7297 ass->jit_optimizer_disabled_inited = TRUE;
7303 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7305 gboolean supported_tail_call;
7308 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7310 for (i = 0; i < fsig->param_count; ++i) {
7311 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7312 /* These can point to the current method's stack */
7313 supported_tail_call = FALSE;
7315 if (fsig->hasthis && cmethod->klass->valuetype)
7316 /* this might point to the current method's stack */
7317 supported_tail_call = FALSE;
7318 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7319 supported_tail_call = FALSE;
7320 if (cfg->method->save_lmf)
7321 supported_tail_call = FALSE;
7322 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7323 supported_tail_call = FALSE;
7324 if (call_opcode != CEE_CALL)
7325 supported_tail_call = FALSE;
7327 /* Debugging support */
7329 if (supported_tail_call) {
7330 if (!mono_debug_count ())
7331 supported_tail_call = FALSE;
7335 return supported_tail_call;
7341 * Handle calls made to ctors from NEWOBJ opcodes.
7344 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7345 MonoInst **sp, guint8 *ip, int *inline_costs)
7347 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7349 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7350 mono_method_is_generic_sharable (cmethod, TRUE)) {
7351 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7352 mono_class_vtable (cfg->domain, cmethod->klass);
7353 CHECK_TYPELOAD (cmethod->klass);
7355 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7356 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7359 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
7360 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7362 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7364 CHECK_TYPELOAD (cmethod->klass);
7365 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7370 /* Avoid virtual calls to ctors if possible */
7371 if (mono_class_is_marshalbyref (cmethod->klass))
7372 callvirt_this_arg = sp [0];
7374 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7375 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7376 CHECK_CFG_EXCEPTION;
7377 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7378 mono_method_check_inlining (cfg, cmethod) &&
7379 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7382 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7383 cfg->real_offset += 5;
7385 *inline_costs += costs - 5;
7387 INLINE_FAILURE ("inline failure");
7388 // FIXME-VT: Clean this up
7389 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7390 GSHAREDVT_FAILURE(*ip);
7391 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7393 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7396 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7398 if (cfg->llvm_only) {
7399 // FIXME: Avoid initializing vtable_arg
7400 emit_llvmonly_calli (cfg, fsig, sp, addr);
7402 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7404 } else if (context_used &&
7405 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7406 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7407 MonoInst *cmethod_addr;
7409 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7411 if (cfg->llvm_only) {
7412 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
7413 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7414 emit_llvmonly_calli (cfg, fsig, sp, addr);
7416 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7417 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7419 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7422 INLINE_FAILURE ("ctor call");
7423 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7424 callvirt_this_arg, NULL, vtable_arg);
7431 emit_setret (MonoCompile *cfg, MonoInst *val)
7433 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
7436 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
7439 if (!cfg->vret_addr) {
7440 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
7442 EMIT_NEW_RETLOADA (cfg, ret_addr);
7444 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
7445 ins->klass = mono_class_from_mono_type (ret_type);
7448 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
7449 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
7450 MonoInst *iargs [1];
7454 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
7455 mono_arch_emit_setret (cfg, cfg->method, conv);
7457 mono_arch_emit_setret (cfg, cfg->method, val);
7460 mono_arch_emit_setret (cfg, cfg->method, val);
7466 * mono_method_to_ir:
7468 * Translate the .net IL into linear IR.
7470 * @start_bblock: if not NULL, the starting basic block, used during inlining.
7471 * @end_bblock: if not NULL, the ending basic block, used during inlining.
7472 * @return_var: if not NULL, the place where the return value is stored, used during inlining.
7473 * @inline_args: if not NULL, contains the arguments to the inline call
7474 * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
7475 * @is_virtual_call: whether this method is being called as a result of a call to callvirt
7477 * This method is used to turn ECMA IL into Mono's internal Linear IR
7478 * reprensetation. It is used both for entire methods, as well as
7479 * inlining existing methods. In the former case, the @start_bblock,
7480 * @end_bblock, @return_var, @inline_args are all set to NULL, and the
7481 * inline_offset is set to zero.
7483 * Returns: the inline cost, or -1 if there was an error processing this method.
7486 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
7487 MonoInst *return_var, MonoInst **inline_args,
7488 guint inline_offset, gboolean is_virtual_call)
7491 MonoInst *ins, **sp, **stack_start;
7492 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
7493 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7494 MonoMethod *cmethod, *method_definition;
7495 MonoInst **arg_array;
7496 MonoMethodHeader *header;
7498 guint32 token, ins_flag;
7500 MonoClass *constrained_class = NULL;
7501 unsigned char *ip, *end, *target, *err_pos;
7502 MonoMethodSignature *sig;
7503 MonoGenericContext *generic_context = NULL;
7504 MonoGenericContainer *generic_container = NULL;
7505 MonoType **param_types;
7506 int i, n, start_new_bblock, dreg;
7507 int num_calls = 0, inline_costs = 0;
7508 int breakpoint_id = 0;
7510 GSList *class_inits = NULL;
7511 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7513 gboolean init_locals, seq_points, skip_dead_blocks;
7514 gboolean sym_seq_points = FALSE;
7515 MonoDebugMethodInfo *minfo;
7516 MonoBitSet *seq_point_locs = NULL;
7517 MonoBitSet *seq_point_set_locs = NULL;
7519 cfg->disable_inline = is_jit_optimizer_disabled (method);
7521 /* serialization and xdomain stuff may need access to private fields and methods */
7522 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7523 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7524 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7525 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7526 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7527 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7529 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7530 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7531 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7532 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7533 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7535 image = method->klass->image;
7536 header = mono_method_get_header_checked (method, &cfg->error);
7538 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7539 goto exception_exit;
7541 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7544 generic_container = mono_method_get_generic_container (method);
7545 sig = mono_method_signature (method);
7546 num_args = sig->hasthis + sig->param_count;
7547 ip = (unsigned char*)header->code;
7548 cfg->cil_start = ip;
7549 end = ip + header->code_size;
7550 cfg->stat_cil_code_size += header->code_size;
7552 seq_points = cfg->gen_seq_points && cfg->method == method;
7554 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7555 /* We could hit a seq point before attaching to the JIT (#8338) */
7559 if (cfg->gen_sdb_seq_points && cfg->method == method) {
7560 minfo = mono_debug_lookup_method (method);
7562 MonoSymSeqPoint *sps;
7563 int i, n_il_offsets;
7565 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7566 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7567 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);
7568 sym_seq_points = TRUE;
7569 for (i = 0; i < n_il_offsets; ++i) {
7570 if (sps [i].il_offset < header->code_size)
7571 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7575 MonoDebugMethodAsyncInfo* asyncMethod = mono_debug_lookup_method_async_debug_info (method);
7577 for (i = 0; asyncMethod != NULL && i < asyncMethod->num_awaits; i++)
7579 mono_bitset_set_fast (seq_point_locs, asyncMethod->resume_offsets[i]);
7580 mono_bitset_set_fast (seq_point_locs, asyncMethod->yield_offsets[i]);
7582 mono_debug_free_method_async_debug_info (asyncMethod);
7584 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7585 /* Methods without line number info like auto-generated property accessors */
7586 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7587 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);
7588 sym_seq_points = TRUE;
7593 * Methods without init_locals set could cause asserts in various passes
7594 * (#497220). To work around this, we emit dummy initialization opcodes
7595 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7596 * on some platforms.
7598 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
7599 init_locals = header->init_locals;
7603 method_definition = method;
7604 while (method_definition->is_inflated) {
7605 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7606 method_definition = imethod->declaring;
7609 /* SkipVerification is not allowed if core-clr is enabled */
7610 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7612 dont_verify_stloc = TRUE;
7615 if (sig->is_inflated)
7616 generic_context = mono_method_get_context (method);
7617 else if (generic_container)
7618 generic_context = &generic_container->context;
7619 cfg->generic_context = generic_context;
7622 g_assert (!sig->has_type_parameters);
7624 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7625 g_assert (method->is_inflated);
7626 g_assert (mono_method_get_context (method)->method_inst);
7628 if (method->is_inflated && mono_method_get_context (method)->method_inst)
7629 g_assert (sig->generic_param_count);
7631 if (cfg->method == method) {
7632 cfg->real_offset = 0;
7634 cfg->real_offset = inline_offset;
7637 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7638 cfg->cil_offset_to_bb_len = header->code_size;
7640 cfg->current_method = method;
7642 if (cfg->verbose_level > 2)
7643 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7645 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7647 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7648 for (n = 0; n < sig->param_count; ++n)
7649 param_types [n + sig->hasthis] = sig->params [n];
7650 cfg->arg_types = param_types;
7652 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7653 if (cfg->method == method) {
7655 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7656 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7659 NEW_BBLOCK (cfg, start_bblock);
7660 cfg->bb_entry = start_bblock;
7661 start_bblock->cil_code = NULL;
7662 start_bblock->cil_length = 0;
7665 NEW_BBLOCK (cfg, end_bblock);
7666 cfg->bb_exit = end_bblock;
7667 end_bblock->cil_code = NULL;
7668 end_bblock->cil_length = 0;
7669 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7670 g_assert (cfg->num_bblocks == 2);
7672 arg_array = cfg->args;
7674 if (header->num_clauses) {
7675 cfg->spvars = g_hash_table_new (NULL, NULL);
7676 cfg->exvars = g_hash_table_new (NULL, NULL);
7678 /* handle exception clauses */
7679 for (i = 0; i < header->num_clauses; ++i) {
7680 MonoBasicBlock *try_bb;
7681 MonoExceptionClause *clause = &header->clauses [i];
7682 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7684 try_bb->real_offset = clause->try_offset;
7685 try_bb->try_start = TRUE;
7686 try_bb->region = ((i + 1) << 8) | clause->flags;
7687 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7688 tblock->real_offset = clause->handler_offset;
7689 tblock->flags |= BB_EXCEPTION_HANDLER;
7692 * Linking the try block with the EH block hinders inlining as we won't be able to
7693 * merge the bblocks from inlining and produce an artificial hole for no good reason.
7695 if (COMPILE_LLVM (cfg))
7696 link_bblock (cfg, try_bb, tblock);
7698 if (*(ip + clause->handler_offset) == CEE_POP)
7699 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7701 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7702 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7703 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7704 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7705 MONO_ADD_INS (tblock, ins);
7707 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7708 /* finally clauses already have a seq point */
7709 /* seq points for filter clauses are emitted below */
7710 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7711 MONO_ADD_INS (tblock, ins);
7714 /* todo: is a fault block unsafe to optimize? */
7715 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7716 tblock->flags |= BB_EXCEPTION_UNSAFE;
7719 /*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);
7721 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7723 /* catch and filter blocks get the exception object on the stack */
7724 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7725 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7727 /* mostly like handle_stack_args (), but just sets the input args */
7728 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7729 tblock->in_scount = 1;
7730 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7731 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7735 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7736 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7737 if (!cfg->compile_llvm) {
7738 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7739 ins->dreg = tblock->in_stack [0]->dreg;
7740 MONO_ADD_INS (tblock, ins);
7743 MonoInst *dummy_use;
7746 * Add a dummy use for the exvar so its liveness info will be
7749 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7752 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7753 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7754 MONO_ADD_INS (tblock, ins);
7757 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7758 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7759 tblock->flags |= BB_EXCEPTION_HANDLER;
7760 tblock->real_offset = clause->data.filter_offset;
7761 tblock->in_scount = 1;
7762 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7763 /* The filter block shares the exvar with the handler block */
7764 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7765 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7766 MONO_ADD_INS (tblock, ins);
7770 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7771 clause->data.catch_class &&
7773 mono_class_check_context_used (clause->data.catch_class)) {
7775 * In shared generic code with catch
7776 * clauses containing type variables
7777 * the exception handling code has to
7778 * be able to get to the rgctx.
7779 * Therefore we have to make sure that
7780 * the vtable/mrgctx argument (for
7781 * static or generic methods) or the
7782 * "this" argument (for non-static
7783 * methods) are live.
7785 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7786 mini_method_get_context (method)->method_inst ||
7787 method->klass->valuetype) {
7788 mono_get_vtable_var (cfg);
7790 MonoInst *dummy_use;
7792 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7797 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7798 cfg->cbb = start_bblock;
7799 cfg->args = arg_array;
7800 mono_save_args (cfg, sig, inline_args);
7803 /* FIRST CODE BLOCK */
7804 NEW_BBLOCK (cfg, tblock);
7805 tblock->cil_code = ip;
7809 ADD_BBLOCK (cfg, tblock);
7811 if (cfg->method == method) {
7812 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7813 if (breakpoint_id) {
7814 MONO_INST_NEW (cfg, ins, OP_BREAK);
7815 MONO_ADD_INS (cfg->cbb, ins);
7819 /* we use a separate basic block for the initialization code */
7820 NEW_BBLOCK (cfg, init_localsbb);
7821 if (cfg->method == method)
7822 cfg->bb_init = init_localsbb;
7823 init_localsbb->real_offset = cfg->real_offset;
7824 start_bblock->next_bb = init_localsbb;
7825 init_localsbb->next_bb = cfg->cbb;
7826 link_bblock (cfg, start_bblock, init_localsbb);
7827 link_bblock (cfg, init_localsbb, cfg->cbb);
7829 cfg->cbb = init_localsbb;
7831 if (cfg->gsharedvt && cfg->method == method) {
7832 MonoGSharedVtMethodInfo *info;
7833 MonoInst *var, *locals_var;
7836 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7837 info->method = cfg->method;
7838 info->count_entries = 16;
7839 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7840 cfg->gsharedvt_info = info;
7842 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7843 /* prevent it from being register allocated */
7844 //var->flags |= MONO_INST_VOLATILE;
7845 cfg->gsharedvt_info_var = var;
7847 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7848 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7850 /* Allocate locals */
7851 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7852 /* prevent it from being register allocated */
7853 //locals_var->flags |= MONO_INST_VOLATILE;
7854 cfg->gsharedvt_locals_var = locals_var;
7856 dreg = alloc_ireg (cfg);
7857 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7859 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7860 ins->dreg = locals_var->dreg;
7862 MONO_ADD_INS (cfg->cbb, ins);
7863 cfg->gsharedvt_locals_var_ins = ins;
7865 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7868 ins->flags |= MONO_INST_INIT;
7872 if (mono_security_core_clr_enabled ()) {
7873 /* check if this is native code, e.g. an icall or a p/invoke */
7874 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
7875 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
7877 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
7878 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
7880 /* if this ia a native call then it can only be JITted from platform code */
7881 if ((icall || pinvk) && method->klass && method->klass->image) {
7882 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
7883 MonoException *ex = icall ? mono_get_exception_security () :
7884 mono_get_exception_method_access ();
7885 emit_throw_exception (cfg, ex);
7892 CHECK_CFG_EXCEPTION;
7894 if (header->code_size == 0)
7897 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
7902 if (cfg->method == method)
7903 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
7905 for (n = 0; n < header->num_locals; ++n) {
7906 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
7911 /* We force the vtable variable here for all shared methods
7912 for the possibility that they might show up in a stack
7913 trace where their exact instantiation is needed. */
7914 if (cfg->gshared && method == cfg->method) {
7915 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7916 mini_method_get_context (method)->method_inst ||
7917 method->klass->valuetype) {
7918 mono_get_vtable_var (cfg);
7920 /* FIXME: Is there a better way to do this?
7921 We need the variable live for the duration
7922 of the whole method. */
7923 cfg->args [0]->flags |= MONO_INST_VOLATILE;
7927 /* add a check for this != NULL to inlined methods */
7928 if (is_virtual_call) {
7931 NEW_ARGLOAD (cfg, arg_ins, 0);
7932 MONO_ADD_INS (cfg->cbb, arg_ins);
7933 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
7936 skip_dead_blocks = !dont_verify;
7937 if (skip_dead_blocks) {
7938 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
7943 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
7944 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
7947 start_new_bblock = 0;
7949 if (cfg->method == method)
7950 cfg->real_offset = ip - header->code;
7952 cfg->real_offset = inline_offset;
7957 if (start_new_bblock) {
7958 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
7959 if (start_new_bblock == 2) {
7960 g_assert (ip == tblock->cil_code);
7962 GET_BBLOCK (cfg, tblock, ip);
7964 cfg->cbb->next_bb = tblock;
7966 start_new_bblock = 0;
7967 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7968 if (cfg->verbose_level > 3)
7969 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7970 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7974 g_slist_free (class_inits);
7977 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
7978 link_bblock (cfg, cfg->cbb, tblock);
7979 if (sp != stack_start) {
7980 handle_stack_args (cfg, stack_start, sp - stack_start);
7982 CHECK_UNVERIFIABLE (cfg);
7984 cfg->cbb->next_bb = tblock;
7986 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7987 if (cfg->verbose_level > 3)
7988 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7989 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7992 g_slist_free (class_inits);
7997 if (skip_dead_blocks) {
7998 int ip_offset = ip - header->code;
8000 if (ip_offset == bb->end)
8004 int op_size = mono_opcode_size (ip, end);
8005 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8007 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8009 if (ip_offset + op_size == bb->end) {
8010 MONO_INST_NEW (cfg, ins, OP_NOP);
8011 MONO_ADD_INS (cfg->cbb, ins);
8012 start_new_bblock = 1;
8020 * Sequence points are points where the debugger can place a breakpoint.
8021 * Currently, we generate these automatically at points where the IL
8024 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8026 * Make methods interruptable at the beginning, and at the targets of
8027 * backward branches.
8028 * Also, do this at the start of every bblock in methods with clauses too,
8029 * to be able to handle instructions with inprecise control flow like
8031 * Backward branches are handled at the end of method-to-ir ().
8033 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8034 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8036 /* Avoid sequence points on empty IL like .volatile */
8037 // FIXME: Enable this
8038 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8039 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8040 if ((sp != stack_start) && !sym_seq_point)
8041 ins->flags |= MONO_INST_NONEMPTY_STACK;
8042 MONO_ADD_INS (cfg->cbb, ins);
8045 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8048 cfg->cbb->real_offset = cfg->real_offset;
8050 if ((cfg->method == method) && cfg->coverage_info) {
8051 guint32 cil_offset = ip - header->code;
8052 cfg->coverage_info->data [cil_offset].cil_code = ip;
8054 /* TODO: Use an increment here */
8055 #if defined(TARGET_X86)
8056 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8057 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8059 MONO_ADD_INS (cfg->cbb, ins);
8061 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8062 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8066 if (cfg->verbose_level > 3)
8067 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8071 if (seq_points && !sym_seq_points && sp != stack_start) {
8073 * The C# compiler uses these nops to notify the JIT that it should
8074 * insert seq points.
8076 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8077 MONO_ADD_INS (cfg->cbb, ins);
8079 if (cfg->keep_cil_nops)
8080 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8082 MONO_INST_NEW (cfg, ins, OP_NOP);
8084 MONO_ADD_INS (cfg->cbb, ins);
8087 if (should_insert_brekpoint (cfg->method)) {
8088 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8090 MONO_INST_NEW (cfg, ins, OP_NOP);
8093 MONO_ADD_INS (cfg->cbb, ins);
8099 CHECK_STACK_OVF (1);
8100 n = (*ip)-CEE_LDARG_0;
8102 EMIT_NEW_ARGLOAD (cfg, ins, n);
8110 CHECK_STACK_OVF (1);
8111 n = (*ip)-CEE_LDLOC_0;
8113 EMIT_NEW_LOCLOAD (cfg, ins, n);
8122 n = (*ip)-CEE_STLOC_0;
8125 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8127 emit_stloc_ir (cfg, sp, header, n);
8134 CHECK_STACK_OVF (1);
8137 EMIT_NEW_ARGLOAD (cfg, ins, n);
8143 CHECK_STACK_OVF (1);
8146 NEW_ARGLOADA (cfg, ins, n);
8147 MONO_ADD_INS (cfg->cbb, ins);
8157 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8159 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8164 CHECK_STACK_OVF (1);
8167 EMIT_NEW_LOCLOAD (cfg, ins, n);
8171 case CEE_LDLOCA_S: {
8172 unsigned char *tmp_ip;
8174 CHECK_STACK_OVF (1);
8175 CHECK_LOCAL (ip [1]);
8177 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8183 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8192 CHECK_LOCAL (ip [1]);
8193 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8195 emit_stloc_ir (cfg, sp, header, ip [1]);
8200 CHECK_STACK_OVF (1);
8201 EMIT_NEW_PCONST (cfg, ins, NULL);
8202 ins->type = STACK_OBJ;
8207 CHECK_STACK_OVF (1);
8208 EMIT_NEW_ICONST (cfg, ins, -1);
8221 CHECK_STACK_OVF (1);
8222 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8228 CHECK_STACK_OVF (1);
8230 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8236 CHECK_STACK_OVF (1);
8237 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8243 CHECK_STACK_OVF (1);
8244 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8245 ins->type = STACK_I8;
8246 ins->dreg = alloc_dreg (cfg, STACK_I8);
8248 ins->inst_l = (gint64)read64 (ip);
8249 MONO_ADD_INS (cfg->cbb, ins);
8255 gboolean use_aotconst = FALSE;
8257 #ifdef TARGET_POWERPC
8258 /* FIXME: Clean this up */
8259 if (cfg->compile_aot)
8260 use_aotconst = TRUE;
8263 /* FIXME: we should really allocate this only late in the compilation process */
8264 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8266 CHECK_STACK_OVF (1);
8272 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8274 dreg = alloc_freg (cfg);
8275 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8276 ins->type = cfg->r4_stack_type;
8278 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8279 ins->type = cfg->r4_stack_type;
8280 ins->dreg = alloc_dreg (cfg, STACK_R8);
8282 MONO_ADD_INS (cfg->cbb, ins);
8292 gboolean use_aotconst = FALSE;
8294 #ifdef TARGET_POWERPC
8295 /* FIXME: Clean this up */
8296 if (cfg->compile_aot)
8297 use_aotconst = TRUE;
8300 /* FIXME: we should really allocate this only late in the compilation process */
8301 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8303 CHECK_STACK_OVF (1);
8309 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8311 dreg = alloc_freg (cfg);
8312 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8313 ins->type = STACK_R8;
8315 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8316 ins->type = STACK_R8;
8317 ins->dreg = alloc_dreg (cfg, STACK_R8);
8319 MONO_ADD_INS (cfg->cbb, ins);
8328 MonoInst *temp, *store;
8330 CHECK_STACK_OVF (1);
8334 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8335 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8337 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8340 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8353 if (sp [0]->type == STACK_R8)
8354 /* we need to pop the value from the x86 FP stack */
8355 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8360 MonoMethodSignature *fsig;
8363 INLINE_FAILURE ("jmp");
8364 GSHAREDVT_FAILURE (*ip);
8367 if (stack_start != sp)
8369 token = read32 (ip + 1);
8370 /* FIXME: check the signature matches */
8371 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8374 if (cfg->gshared && mono_method_check_context_used (cmethod))
8375 GENERIC_SHARING_FAILURE (CEE_JMP);
8377 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8379 fsig = mono_method_signature (cmethod);
8380 n = fsig->param_count + fsig->hasthis;
8381 if (cfg->llvm_only) {
8384 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8385 for (i = 0; i < n; ++i)
8386 EMIT_NEW_ARGLOAD (cfg, args [i], i);
8387 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
8389 * The code in mono-basic-block.c treats the rest of the code as dead, but we
8390 * have to emit a normal return since llvm expects it.
8393 emit_setret (cfg, ins);
8394 MONO_INST_NEW (cfg, ins, OP_BR);
8395 ins->inst_target_bb = end_bblock;
8396 MONO_ADD_INS (cfg->cbb, ins);
8397 link_bblock (cfg, cfg->cbb, end_bblock);
8400 } else if (cfg->backend->have_op_tail_call) {
8401 /* Handle tail calls similarly to calls */
8404 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8405 call->method = cmethod;
8406 call->tail_call = TRUE;
8407 call->signature = mono_method_signature (cmethod);
8408 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8409 call->inst.inst_p0 = cmethod;
8410 for (i = 0; i < n; ++i)
8411 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8413 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
8414 call->vret_var = cfg->vret_addr;
8416 mono_arch_emit_call (cfg, call);
8417 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8418 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8420 for (i = 0; i < num_args; ++i)
8421 /* Prevent arguments from being optimized away */
8422 arg_array [i]->flags |= MONO_INST_VOLATILE;
8424 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8425 ins = (MonoInst*)call;
8426 ins->inst_p0 = cmethod;
8427 MONO_ADD_INS (cfg->cbb, ins);
8431 start_new_bblock = 1;
8436 MonoMethodSignature *fsig;
8439 token = read32 (ip + 1);
8443 //GSHAREDVT_FAILURE (*ip);
8448 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
8451 if (method->dynamic && fsig->pinvoke) {
8455 * This is a call through a function pointer using a pinvoke
8456 * signature. Have to create a wrapper and call that instead.
8457 * FIXME: This is very slow, need to create a wrapper at JIT time
8458 * instead based on the signature.
8460 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8461 EMIT_NEW_PCONST (cfg, args [1], fsig);
8463 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8466 n = fsig->param_count + fsig->hasthis;
8470 //g_assert (!virtual_ || fsig->hasthis);
8474 inline_costs += 10 * num_calls++;
8477 * Making generic calls out of gsharedvt methods.
8478 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8479 * patching gshared method addresses into a gsharedvt method.
8481 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8483 * We pass the address to the gsharedvt trampoline in the rgctx reg
8485 MonoInst *callee = addr;
8487 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8489 GSHAREDVT_FAILURE (*ip);
8493 GSHAREDVT_FAILURE (*ip);
8495 addr = emit_get_rgctx_sig (cfg, context_used,
8496 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8497 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8501 /* Prevent inlining of methods with indirect calls */
8502 INLINE_FAILURE ("indirect call");
8504 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8505 MonoJumpInfoType info_type;
8509 * Instead of emitting an indirect call, emit a direct call
8510 * with the contents of the aotconst as the patch info.
8512 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8513 info_type = (MonoJumpInfoType)addr->inst_c1;
8514 info_data = addr->inst_p0;
8516 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
8517 info_data = addr->inst_right->inst_left;
8520 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
8521 ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
8524 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8525 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8530 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8534 /* End of call, INS should contain the result of the call, if any */
8536 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8538 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8541 CHECK_CFG_EXCEPTION;
8545 constrained_class = NULL;
8549 case CEE_CALLVIRT: {
8550 MonoInst *addr = NULL;
8551 MonoMethodSignature *fsig = NULL;
8553 int virtual_ = *ip == CEE_CALLVIRT;
8554 gboolean pass_imt_from_rgctx = FALSE;
8555 MonoInst *imt_arg = NULL;
8556 MonoInst *keep_this_alive = NULL;
8557 gboolean pass_vtable = FALSE;
8558 gboolean pass_mrgctx = FALSE;
8559 MonoInst *vtable_arg = NULL;
8560 gboolean check_this = FALSE;
8561 gboolean supported_tail_call = FALSE;
8562 gboolean tail_call = FALSE;
8563 gboolean need_seq_point = FALSE;
8564 guint32 call_opcode = *ip;
8565 gboolean emit_widen = TRUE;
8566 gboolean push_res = TRUE;
8567 gboolean skip_ret = FALSE;
8568 gboolean delegate_invoke = FALSE;
8569 gboolean direct_icall = FALSE;
8570 gboolean constrained_partial_call = FALSE;
8571 MonoMethod *cil_method;
8574 token = read32 (ip + 1);
8578 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8581 cil_method = cmethod;
8583 if (constrained_class) {
8584 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8585 if (!mini_is_gsharedvt_klass (constrained_class)) {
8586 g_assert (!cmethod->klass->valuetype);
8587 if (!mini_type_is_reference (&constrained_class->byval_arg))
8588 constrained_partial_call = TRUE;
8592 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8593 if (cfg->verbose_level > 2)
8594 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8595 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8596 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8598 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8602 if (cfg->verbose_level > 2)
8603 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8605 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8607 * This is needed since get_method_constrained can't find
8608 * the method in klass representing a type var.
8609 * The type var is guaranteed to be a reference type in this
8612 if (!mini_is_gsharedvt_klass (constrained_class))
8613 g_assert (!cmethod->klass->valuetype);
8615 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8620 if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
8621 /* Use the corresponding method from the base type to avoid boxing */
8622 MonoType *base_type = mono_class_enum_basetype (constrained_class);
8623 g_assert (base_type);
8624 constrained_class = mono_class_from_mono_type (base_type);
8625 cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
8630 if (!dont_verify && !cfg->skip_visibility) {
8631 MonoMethod *target_method = cil_method;
8632 if (method->is_inflated) {
8633 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
8636 if (!mono_method_can_access_method (method_definition, target_method) &&
8637 !mono_method_can_access_method (method, cil_method))
8638 emit_method_access_failure (cfg, method, cil_method);
8641 if (mono_security_core_clr_enabled ())
8642 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8644 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8645 /* MS.NET seems to silently convert this to a callvirt */
8650 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8651 * converts to a callvirt.
8653 * tests/bug-515884.il is an example of this behavior
8655 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8656 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8657 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8661 if (!cmethod->klass->inited)
8662 if (!mono_class_init (cmethod->klass))
8663 TYPE_LOAD_ERROR (cmethod->klass);
8665 fsig = mono_method_signature (cmethod);
8668 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8669 mini_class_is_system_array (cmethod->klass)) {
8670 array_rank = cmethod->klass->rank;
8671 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8672 direct_icall = TRUE;
8673 } else if (fsig->pinvoke) {
8674 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8675 fsig = mono_method_signature (wrapper);
8676 } else if (constrained_class) {
8678 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8682 if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
8683 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
8685 /* See code below */
8686 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8687 MonoBasicBlock *tbb;
8689 GET_BBLOCK (cfg, tbb, ip + 5);
8690 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8692 * We want to extend the try block to cover the call, but we can't do it if the
8693 * call is made directly since its followed by an exception check.
8695 direct_icall = FALSE;
8699 mono_save_token_info (cfg, image, token, cil_method);
8701 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8702 need_seq_point = TRUE;
8704 /* Don't support calls made using type arguments for now */
8706 if (cfg->gsharedvt) {
8707 if (mini_is_gsharedvt_signature (fsig))
8708 GSHAREDVT_FAILURE (*ip);
8712 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8713 g_assert_not_reached ();
8715 n = fsig->param_count + fsig->hasthis;
8717 if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
8721 g_assert (!mono_method_check_context_used (cmethod));
8725 //g_assert (!virtual_ || fsig->hasthis);
8730 * We have the `constrained.' prefix opcode.
8732 if (constrained_class) {
8733 if (mini_is_gsharedvt_klass (constrained_class)) {
8734 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8735 /* The 'Own method' case below */
8736 } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
8737 /* 'The type parameter is instantiated as a reference type' case below. */
8739 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8740 CHECK_CFG_EXCEPTION;
8746 if (constrained_partial_call) {
8747 gboolean need_box = TRUE;
8750 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8751 * called method is not known at compile time either. The called method could end up being
8752 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8753 * to box the receiver.
8754 * A simple solution would be to box always and make a normal virtual call, but that would
8755 * be bad performance wise.
8757 if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
8759 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8764 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)) {
8765 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8766 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8767 ins->klass = constrained_class;
8768 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8769 CHECK_CFG_EXCEPTION;
8770 } else if (need_box) {
8772 MonoBasicBlock *is_ref_bb, *end_bb;
8773 MonoInst *nonbox_call;
8776 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8778 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8779 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8781 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8783 NEW_BBLOCK (cfg, is_ref_bb);
8784 NEW_BBLOCK (cfg, end_bb);
8786 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);
8787 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
8788 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8791 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8793 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8796 MONO_START_BB (cfg, is_ref_bb);
8797 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8798 ins->klass = constrained_class;
8799 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8800 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8802 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8804 MONO_START_BB (cfg, end_bb);
8807 nonbox_call->dreg = ins->dreg;
8810 g_assert (mono_class_is_interface (cmethod->klass));
8811 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8812 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8815 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8817 * The type parameter is instantiated as a valuetype,
8818 * but that type doesn't override the method we're
8819 * calling, so we need to box `this'.
8821 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8822 ins->klass = constrained_class;
8823 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8824 CHECK_CFG_EXCEPTION;
8825 } else if (!constrained_class->valuetype) {
8826 int dreg = alloc_ireg_ref (cfg);
8829 * The type parameter is instantiated as a reference
8830 * type. We have a managed pointer on the stack, so
8831 * we need to dereference it here.
8833 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8834 ins->type = STACK_OBJ;
8837 if (cmethod->klass->valuetype) {
8840 /* Interface method */
8843 mono_class_setup_vtable (constrained_class);
8844 CHECK_TYPELOAD (constrained_class);
8845 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8847 TYPE_LOAD_ERROR (constrained_class);
8848 slot = mono_method_get_vtable_slot (cmethod);
8850 TYPE_LOAD_ERROR (cmethod->klass);
8851 cmethod = constrained_class->vtable [ioffset + slot];
8853 if (cmethod->klass == mono_defaults.enum_class) {
8854 /* Enum implements some interfaces, so treat this as the first case */
8855 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8856 ins->klass = constrained_class;
8857 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8858 CHECK_CFG_EXCEPTION;
8863 constrained_class = NULL;
8866 if (check_call_signature (cfg, fsig, sp))
8869 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8870 delegate_invoke = TRUE;
8872 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8873 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8874 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8882 * If the callee is a shared method, then its static cctor
8883 * might not get called after the call was patched.
8885 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)) {
8886 emit_class_init (cfg, cmethod->klass);
8887 CHECK_TYPELOAD (cmethod->klass);
8890 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8893 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8895 context_used = mini_method_check_context_used (cfg, cmethod);
8897 if (context_used && mono_class_is_interface (cmethod->klass)) {
8898 /* Generic method interface
8899 calls are resolved via a
8900 helper function and don't
8902 if (!cmethod_context || !cmethod_context->method_inst)
8903 pass_imt_from_rgctx = TRUE;
8907 * If a shared method calls another
8908 * shared method then the caller must
8909 * have a generic sharing context
8910 * because the magic trampoline
8911 * requires it. FIXME: We shouldn't
8912 * have to force the vtable/mrgctx
8913 * variable here. Instead there
8914 * should be a flag in the cfg to
8915 * request a generic sharing context.
8918 ((cfg->method->flags & METHOD_ATTRIBUTE_STATIC) || cfg->method->klass->valuetype))
8919 mono_get_vtable_var (cfg);
8924 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8926 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8928 CHECK_TYPELOAD (cmethod->klass);
8929 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8934 g_assert (!vtable_arg);
8936 if (!cfg->compile_aot) {
8938 * emit_get_rgctx_method () calls mono_class_vtable () so check
8939 * for type load errors before.
8941 mono_class_setup_vtable (cmethod->klass);
8942 CHECK_TYPELOAD (cmethod->klass);
8945 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8947 /* !marshalbyref is needed to properly handle generic methods + remoting */
8948 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
8949 MONO_METHOD_IS_FINAL (cmethod)) &&
8950 !mono_class_is_marshalbyref (cmethod->klass)) {
8957 if (pass_imt_from_rgctx) {
8958 g_assert (!pass_vtable);
8960 imt_arg = emit_get_rgctx_method (cfg, context_used,
8961 cmethod, MONO_RGCTX_INFO_METHOD);
8965 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8967 /* Calling virtual generic methods */
8968 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
8969 !(MONO_METHOD_IS_FINAL (cmethod) &&
8970 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
8971 fsig->generic_param_count &&
8972 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
8974 MonoInst *this_temp, *this_arg_temp, *store;
8975 MonoInst *iargs [4];
8977 g_assert (fsig->is_inflated);
8979 /* Prevent inlining of methods that contain indirect calls */
8980 INLINE_FAILURE ("virtual generic call");
8982 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8983 GSHAREDVT_FAILURE (*ip);
8985 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
8986 g_assert (!imt_arg);
8988 g_assert (cmethod->is_inflated);
8989 imt_arg = emit_get_rgctx_method (cfg, context_used,
8990 cmethod, MONO_RGCTX_INFO_METHOD);
8991 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
8993 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
8994 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
8995 MONO_ADD_INS (cfg->cbb, store);
8997 /* FIXME: This should be a managed pointer */
8998 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9000 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9001 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9002 cmethod, MONO_RGCTX_INFO_METHOD);
9003 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9004 addr = mono_emit_jit_icall (cfg,
9005 mono_helper_compile_generic_method, iargs);
9007 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9009 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9016 * Implement a workaround for the inherent races involved in locking:
9022 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9023 * try block, the Exit () won't be executed, see:
9024 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9025 * To work around this, we extend such try blocks to include the last x bytes
9026 * of the Monitor.Enter () call.
9028 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9029 MonoBasicBlock *tbb;
9031 GET_BBLOCK (cfg, tbb, ip + 5);
9033 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9034 * from Monitor.Enter like ArgumentNullException.
9036 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9037 /* Mark this bblock as needing to be extended */
9038 tbb->extend_try_block = TRUE;
9042 /* Conversion to a JIT intrinsic */
9043 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9044 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9045 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9053 if ((cfg->opt & MONO_OPT_INLINE) &&
9054 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9055 mono_method_check_inlining (cfg, cmethod)) {
9057 gboolean always = FALSE;
9059 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9060 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9061 /* Prevent inlining of methods that call wrappers */
9062 INLINE_FAILURE ("wrapper call");
9063 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9067 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9069 cfg->real_offset += 5;
9071 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9072 /* *sp is already set by inline_method */
9077 inline_costs += costs;
9083 /* Tail recursion elimination */
9084 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9085 gboolean has_vtargs = FALSE;
9088 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9089 INLINE_FAILURE ("tail call");
9091 /* keep it simple */
9092 for (i = fsig->param_count - 1; i >= 0; i--) {
9093 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9098 if (need_seq_point) {
9099 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9100 need_seq_point = FALSE;
9102 for (i = 0; i < n; ++i)
9103 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9104 MONO_INST_NEW (cfg, ins, OP_BR);
9105 MONO_ADD_INS (cfg->cbb, ins);
9106 tblock = start_bblock->out_bb [0];
9107 link_bblock (cfg, cfg->cbb, tblock);
9108 ins->inst_target_bb = tblock;
9109 start_new_bblock = 1;
9111 /* skip the CEE_RET, too */
9112 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9119 inline_costs += 10 * num_calls++;
9122 * Synchronized wrappers.
9123 * Its hard to determine where to replace a method with its synchronized
9124 * wrapper without causing an infinite recursion. The current solution is
9125 * to add the synchronized wrapper in the trampolines, and to
9126 * change the called method to a dummy wrapper, and resolve that wrapper
9127 * to the real method in mono_jit_compile_method ().
9129 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9130 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9131 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9132 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9136 * Making generic calls out of gsharedvt methods.
9137 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9138 * patching gshared method addresses into a gsharedvt method.
9140 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
9141 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9142 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9143 MonoRgctxInfoType info_type;
9146 //if (mono_class_is_interface (cmethod->klass))
9147 //GSHAREDVT_FAILURE (*ip);
9148 // disable for possible remoting calls
9149 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9150 GSHAREDVT_FAILURE (*ip);
9151 if (fsig->generic_param_count) {
9152 /* virtual generic call */
9153 g_assert (!imt_arg);
9154 /* Same as the virtual generic case above */
9155 imt_arg = emit_get_rgctx_method (cfg, context_used,
9156 cmethod, MONO_RGCTX_INFO_METHOD);
9157 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9159 } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
9160 /* This can happen when we call a fully instantiated iface method */
9161 imt_arg = emit_get_rgctx_method (cfg, context_used,
9162 cmethod, MONO_RGCTX_INFO_METHOD);
9167 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9168 keep_this_alive = sp [0];
9170 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9171 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9173 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9174 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9176 if (cfg->llvm_only) {
9177 // FIXME: Avoid initializing vtable_arg
9178 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9180 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9185 /* Generic sharing */
9188 * Use this if the callee is gsharedvt sharable too, since
9189 * at runtime we might find an instantiation so the call cannot
9190 * be patched (the 'no_patch' code path in mini-trampolines.c).
9192 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9193 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9194 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9195 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9196 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9197 INLINE_FAILURE ("gshared");
9199 g_assert (cfg->gshared && cmethod);
9203 * We are compiling a call to a
9204 * generic method from shared code,
9205 * which means that we have to look up
9206 * the method in the rgctx and do an
9210 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9212 if (cfg->llvm_only) {
9213 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9214 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9216 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9217 // FIXME: Avoid initializing imt_arg/vtable_arg
9218 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9220 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9221 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9226 /* Direct calls to icalls */
9228 MonoMethod *wrapper;
9231 /* Inline the wrapper */
9232 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9234 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9235 g_assert (costs > 0);
9236 cfg->real_offset += 5;
9238 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9239 /* *sp is already set by inline_method */
9244 inline_costs += costs;
9253 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9254 MonoInst *val = sp [fsig->param_count];
9256 if (val->type == STACK_OBJ) {
9257 MonoInst *iargs [2];
9262 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9265 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9266 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9267 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
9268 emit_write_barrier (cfg, addr, val);
9269 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9270 GSHAREDVT_FAILURE (*ip);
9271 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9272 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9274 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9275 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9276 if (!cmethod->klass->element_class->valuetype && !readonly)
9277 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9278 CHECK_TYPELOAD (cmethod->klass);
9281 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9284 g_assert_not_reached ();
9291 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9295 /* Tail prefix / tail call optimization */
9297 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9298 /* FIXME: runtime generic context pointer for jumps? */
9299 /* FIXME: handle this for generic sharing eventually */
9300 if ((ins_flag & MONO_INST_TAILCALL) &&
9301 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9302 supported_tail_call = TRUE;
9304 if (supported_tail_call) {
9307 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9308 INLINE_FAILURE ("tail call");
9310 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9312 if (cfg->backend->have_op_tail_call) {
9313 /* Handle tail calls similarly to normal calls */
9316 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9318 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9319 call->tail_call = TRUE;
9320 call->method = cmethod;
9321 call->signature = mono_method_signature (cmethod);
9324 * We implement tail calls by storing the actual arguments into the
9325 * argument variables, then emitting a CEE_JMP.
9327 for (i = 0; i < n; ++i) {
9328 /* Prevent argument from being register allocated */
9329 arg_array [i]->flags |= MONO_INST_VOLATILE;
9330 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9332 ins = (MonoInst*)call;
9333 ins->inst_p0 = cmethod;
9334 ins->inst_p1 = arg_array [0];
9335 MONO_ADD_INS (cfg->cbb, ins);
9336 link_bblock (cfg, cfg->cbb, end_bblock);
9337 start_new_bblock = 1;
9339 // FIXME: Eliminate unreachable epilogs
9342 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9343 * only reachable from this call.
9345 GET_BBLOCK (cfg, tblock, ip + 5);
9346 if (tblock == cfg->cbb || tblock->in_count == 0)
9355 * Virtual calls in llvm-only mode.
9357 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9358 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9363 if (!(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
9364 INLINE_FAILURE ("call");
9365 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9366 imt_arg, vtable_arg);
9368 if (tail_call && !cfg->llvm_only) {
9369 link_bblock (cfg, cfg->cbb, end_bblock);
9370 start_new_bblock = 1;
9372 // FIXME: Eliminate unreachable epilogs
9375 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9376 * only reachable from this call.
9378 GET_BBLOCK (cfg, tblock, ip + 5);
9379 if (tblock == cfg->cbb || tblock->in_count == 0)
9386 /* End of call, INS should contain the result of the call, if any */
9388 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9391 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9396 if (keep_this_alive) {
9397 MonoInst *dummy_use;
9399 /* See mono_emit_method_call_full () */
9400 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9403 if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
9405 * Clang can convert these calls to tail calls which screw up the stack
9406 * walk. This happens even when the -fno-optimize-sibling-calls
9407 * option is passed to clang.
9408 * Work around this by emitting a dummy call.
9410 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
9413 CHECK_CFG_EXCEPTION;
9417 g_assert (*ip == CEE_RET);
9421 constrained_class = NULL;
9423 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9427 if (cfg->method != method) {
9428 /* return from inlined method */
9430 * If in_count == 0, that means the ret is unreachable due to
9431 * being preceeded by a throw. In that case, inline_method () will
9432 * handle setting the return value
9433 * (test case: test_0_inline_throw ()).
9435 if (return_var && cfg->cbb->in_count) {
9436 MonoType *ret_type = mono_method_signature (method)->ret;
9442 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9445 //g_assert (returnvar != -1);
9446 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9447 cfg->ret_var_set = TRUE;
9450 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9452 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
9456 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9458 if (seq_points && !sym_seq_points) {
9460 * Place a seq point here too even through the IL stack is not
9461 * empty, so a step over on
9464 * will work correctly.
9466 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9467 MONO_ADD_INS (cfg->cbb, ins);
9470 g_assert (!return_var);
9474 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9477 emit_setret (cfg, *sp);
9480 if (sp != stack_start)
9482 MONO_INST_NEW (cfg, ins, OP_BR);
9484 ins->inst_target_bb = end_bblock;
9485 MONO_ADD_INS (cfg->cbb, ins);
9486 link_bblock (cfg, cfg->cbb, end_bblock);
9487 start_new_bblock = 1;
9491 MONO_INST_NEW (cfg, ins, OP_BR);
9493 target = ip + 1 + (signed char)(*ip);
9495 GET_BBLOCK (cfg, tblock, target);
9496 link_bblock (cfg, cfg->cbb, tblock);
9497 ins->inst_target_bb = tblock;
9498 if (sp != stack_start) {
9499 handle_stack_args (cfg, stack_start, sp - stack_start);
9501 CHECK_UNVERIFIABLE (cfg);
9503 MONO_ADD_INS (cfg->cbb, ins);
9504 start_new_bblock = 1;
9505 inline_costs += BRANCH_COST;
9519 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9521 target = ip + 1 + *(signed char*)ip;
9527 inline_costs += BRANCH_COST;
9531 MONO_INST_NEW (cfg, ins, OP_BR);
9534 target = ip + 4 + (gint32)read32(ip);
9536 GET_BBLOCK (cfg, tblock, target);
9537 link_bblock (cfg, cfg->cbb, tblock);
9538 ins->inst_target_bb = tblock;
9539 if (sp != stack_start) {
9540 handle_stack_args (cfg, stack_start, sp - stack_start);
9542 CHECK_UNVERIFIABLE (cfg);
9545 MONO_ADD_INS (cfg->cbb, ins);
9547 start_new_bblock = 1;
9548 inline_costs += BRANCH_COST;
9555 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9556 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9557 guint32 opsize = is_short ? 1 : 4;
9559 CHECK_OPSIZE (opsize);
9561 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9564 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9569 GET_BBLOCK (cfg, tblock, target);
9570 link_bblock (cfg, cfg->cbb, tblock);
9571 GET_BBLOCK (cfg, tblock, ip);
9572 link_bblock (cfg, cfg->cbb, tblock);
9574 if (sp != stack_start) {
9575 handle_stack_args (cfg, stack_start, sp - stack_start);
9576 CHECK_UNVERIFIABLE (cfg);
9579 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9580 cmp->sreg1 = sp [0]->dreg;
9581 type_from_op (cfg, cmp, sp [0], NULL);
9584 #if SIZEOF_REGISTER == 4
9585 if (cmp->opcode == OP_LCOMPARE_IMM) {
9586 /* Convert it to OP_LCOMPARE */
9587 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9588 ins->type = STACK_I8;
9589 ins->dreg = alloc_dreg (cfg, STACK_I8);
9591 MONO_ADD_INS (cfg->cbb, ins);
9592 cmp->opcode = OP_LCOMPARE;
9593 cmp->sreg2 = ins->dreg;
9596 MONO_ADD_INS (cfg->cbb, cmp);
9598 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9599 type_from_op (cfg, ins, sp [0], NULL);
9600 MONO_ADD_INS (cfg->cbb, ins);
9601 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9602 GET_BBLOCK (cfg, tblock, target);
9603 ins->inst_true_bb = tblock;
9604 GET_BBLOCK (cfg, tblock, ip);
9605 ins->inst_false_bb = tblock;
9606 start_new_bblock = 2;
9609 inline_costs += BRANCH_COST;
9624 MONO_INST_NEW (cfg, ins, *ip);
9626 target = ip + 4 + (gint32)read32(ip);
9632 inline_costs += BRANCH_COST;
9636 MonoBasicBlock **targets;
9637 MonoBasicBlock *default_bblock;
9638 MonoJumpInfoBBTable *table;
9639 int offset_reg = alloc_preg (cfg);
9640 int target_reg = alloc_preg (cfg);
9641 int table_reg = alloc_preg (cfg);
9642 int sum_reg = alloc_preg (cfg);
9643 gboolean use_op_switch;
9647 n = read32 (ip + 1);
9650 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
9654 CHECK_OPSIZE (n * sizeof (guint32));
9655 target = ip + n * sizeof (guint32);
9657 GET_BBLOCK (cfg, default_bblock, target);
9658 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9660 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9661 for (i = 0; i < n; ++i) {
9662 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9663 targets [i] = tblock;
9664 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9668 if (sp != stack_start) {
9670 * Link the current bb with the targets as well, so handle_stack_args
9671 * will set their in_stack correctly.
9673 link_bblock (cfg, cfg->cbb, default_bblock);
9674 for (i = 0; i < n; ++i)
9675 link_bblock (cfg, cfg->cbb, targets [i]);
9677 handle_stack_args (cfg, stack_start, sp - stack_start);
9679 CHECK_UNVERIFIABLE (cfg);
9681 /* Undo the links */
9682 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
9683 for (i = 0; i < n; ++i)
9684 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
9687 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9688 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9690 for (i = 0; i < n; ++i)
9691 link_bblock (cfg, cfg->cbb, targets [i]);
9693 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9694 table->table = targets;
9695 table->table_size = n;
9697 use_op_switch = FALSE;
9699 /* ARM implements SWITCH statements differently */
9700 /* FIXME: Make it use the generic implementation */
9701 if (!cfg->compile_aot)
9702 use_op_switch = TRUE;
9705 if (COMPILE_LLVM (cfg))
9706 use_op_switch = TRUE;
9708 cfg->cbb->has_jump_table = 1;
9710 if (use_op_switch) {
9711 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9712 ins->sreg1 = src1->dreg;
9713 ins->inst_p0 = table;
9714 ins->inst_many_bb = targets;
9715 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
9716 MONO_ADD_INS (cfg->cbb, ins);
9718 if (sizeof (gpointer) == 8)
9719 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9721 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9723 #if SIZEOF_REGISTER == 8
9724 /* The upper word might not be zero, and we add it to a 64 bit address later */
9725 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9728 if (cfg->compile_aot) {
9729 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9731 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9732 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9733 ins->inst_p0 = table;
9734 ins->dreg = table_reg;
9735 MONO_ADD_INS (cfg->cbb, ins);
9738 /* FIXME: Use load_memindex */
9739 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9740 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9741 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9743 start_new_bblock = 1;
9744 inline_costs += (BRANCH_COST * 2);
9764 dreg = alloc_freg (cfg);
9767 dreg = alloc_lreg (cfg);
9770 dreg = alloc_ireg_ref (cfg);
9773 dreg = alloc_preg (cfg);
9776 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
9777 ins->type = ldind_type [*ip - CEE_LDIND_I1];
9778 if (*ip == CEE_LDIND_R4)
9779 ins->type = cfg->r4_stack_type;
9780 ins->flags |= ins_flag;
9781 MONO_ADD_INS (cfg->cbb, ins);
9783 if (ins_flag & MONO_INST_VOLATILE) {
9784 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9785 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9801 if (ins_flag & MONO_INST_VOLATILE) {
9802 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9803 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9806 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9807 ins->flags |= ins_flag;
9810 MONO_ADD_INS (cfg->cbb, ins);
9812 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
9813 emit_write_barrier (cfg, sp [0], sp [1]);
9822 MONO_INST_NEW (cfg, ins, (*ip));
9824 ins->sreg1 = sp [0]->dreg;
9825 ins->sreg2 = sp [1]->dreg;
9826 type_from_op (cfg, ins, sp [0], sp [1]);
9828 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9830 /* Use the immediate opcodes if possible */
9831 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9832 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9833 if (imm_opcode != -1) {
9834 ins->opcode = imm_opcode;
9835 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9838 NULLIFY_INS (sp [1]);
9842 MONO_ADD_INS ((cfg)->cbb, (ins));
9844 *sp++ = mono_decompose_opcode (cfg, ins);
9861 MONO_INST_NEW (cfg, ins, (*ip));
9863 ins->sreg1 = sp [0]->dreg;
9864 ins->sreg2 = sp [1]->dreg;
9865 type_from_op (cfg, ins, sp [0], sp [1]);
9867 add_widen_op (cfg, ins, &sp [0], &sp [1]);
9868 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9870 /* FIXME: Pass opcode to is_inst_imm */
9872 /* Use the immediate opcodes if possible */
9873 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)) {
9874 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9875 if (imm_opcode != -1) {
9876 ins->opcode = imm_opcode;
9877 if (sp [1]->opcode == OP_I8CONST) {
9878 #if SIZEOF_REGISTER == 8
9879 ins->inst_imm = sp [1]->inst_l;
9881 ins->inst_ls_word = sp [1]->inst_ls_word;
9882 ins->inst_ms_word = sp [1]->inst_ms_word;
9886 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9889 /* Might be followed by an instruction added by add_widen_op */
9890 if (sp [1]->next == NULL)
9891 NULLIFY_INS (sp [1]);
9894 MONO_ADD_INS ((cfg)->cbb, (ins));
9896 *sp++ = mono_decompose_opcode (cfg, ins);
9909 case CEE_CONV_OVF_I8:
9910 case CEE_CONV_OVF_U8:
9914 /* Special case this earlier so we have long constants in the IR */
9915 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
9916 int data = sp [-1]->inst_c0;
9917 sp [-1]->opcode = OP_I8CONST;
9918 sp [-1]->type = STACK_I8;
9919 #if SIZEOF_REGISTER == 8
9920 if ((*ip) == CEE_CONV_U8)
9921 sp [-1]->inst_c0 = (guint32)data;
9923 sp [-1]->inst_c0 = data;
9925 sp [-1]->inst_ls_word = data;
9926 if ((*ip) == CEE_CONV_U8)
9927 sp [-1]->inst_ms_word = 0;
9929 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
9931 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
9938 case CEE_CONV_OVF_I4:
9939 case CEE_CONV_OVF_I1:
9940 case CEE_CONV_OVF_I2:
9941 case CEE_CONV_OVF_I:
9942 case CEE_CONV_OVF_U:
9945 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9946 ADD_UNOP (CEE_CONV_OVF_I8);
9953 case CEE_CONV_OVF_U1:
9954 case CEE_CONV_OVF_U2:
9955 case CEE_CONV_OVF_U4:
9958 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9959 ADD_UNOP (CEE_CONV_OVF_U8);
9966 case CEE_CONV_OVF_I1_UN:
9967 case CEE_CONV_OVF_I2_UN:
9968 case CEE_CONV_OVF_I4_UN:
9969 case CEE_CONV_OVF_I8_UN:
9970 case CEE_CONV_OVF_U1_UN:
9971 case CEE_CONV_OVF_U2_UN:
9972 case CEE_CONV_OVF_U4_UN:
9973 case CEE_CONV_OVF_U8_UN:
9974 case CEE_CONV_OVF_I_UN:
9975 case CEE_CONV_OVF_U_UN:
9982 CHECK_CFG_EXCEPTION;
9986 case CEE_ADD_OVF_UN:
9988 case CEE_MUL_OVF_UN:
9990 case CEE_SUB_OVF_UN:
9996 GSHAREDVT_FAILURE (*ip);
9999 token = read32 (ip + 1);
10000 klass = mini_get_class (method, token, generic_context);
10001 CHECK_TYPELOAD (klass);
10003 if (generic_class_is_reference_type (cfg, klass)) {
10004 MonoInst *store, *load;
10005 int dreg = alloc_ireg_ref (cfg);
10007 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10008 load->flags |= ins_flag;
10009 MONO_ADD_INS (cfg->cbb, load);
10011 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10012 store->flags |= ins_flag;
10013 MONO_ADD_INS (cfg->cbb, store);
10015 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10016 emit_write_barrier (cfg, sp [0], sp [1]);
10018 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10024 int loc_index = -1;
10030 token = read32 (ip + 1);
10031 klass = mini_get_class (method, token, generic_context);
10032 CHECK_TYPELOAD (klass);
10034 /* Optimize the common ldobj+stloc combination */
10037 loc_index = ip [6];
10044 loc_index = ip [5] - CEE_STLOC_0;
10051 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10052 CHECK_LOCAL (loc_index);
10054 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10055 ins->dreg = cfg->locals [loc_index]->dreg;
10056 ins->flags |= ins_flag;
10059 if (ins_flag & MONO_INST_VOLATILE) {
10060 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10061 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10067 /* Optimize the ldobj+stobj combination */
10068 /* The reference case ends up being a load+store anyway */
10069 /* Skip this if the operation is volatile. */
10070 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass) && !(ins_flag & MONO_INST_VOLATILE)) {
10075 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10082 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10083 ins->flags |= ins_flag;
10086 if (ins_flag & MONO_INST_VOLATILE) {
10087 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10088 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10097 CHECK_STACK_OVF (1);
10099 n = read32 (ip + 1);
10101 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10102 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10103 ins->type = STACK_OBJ;
10106 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10107 MonoInst *iargs [1];
10108 char *str = (char *)mono_method_get_wrapper_data (method, n);
10110 if (cfg->compile_aot)
10111 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10113 EMIT_NEW_PCONST (cfg, iargs [0], str);
10114 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10116 if (cfg->opt & MONO_OPT_SHARED) {
10117 MonoInst *iargs [3];
10119 if (cfg->compile_aot) {
10120 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10122 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10123 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10124 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10125 *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
10126 mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10129 if (cfg->cbb->out_of_line) {
10130 MonoInst *iargs [2];
10132 if (image == mono_defaults.corlib) {
10134 * Avoid relocations in AOT and save some space by using a
10135 * version of helper_ldstr specialized to mscorlib.
10137 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10138 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10140 /* Avoid creating the string object */
10141 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10142 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10143 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10147 if (cfg->compile_aot) {
10148 NEW_LDSTRCONST (cfg, ins, image, n);
10150 MONO_ADD_INS (cfg->cbb, ins);
10153 NEW_PCONST (cfg, ins, NULL);
10154 ins->type = STACK_OBJ;
10155 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10159 OUT_OF_MEMORY_FAILURE;
10162 MONO_ADD_INS (cfg->cbb, ins);
10171 MonoInst *iargs [2];
10172 MonoMethodSignature *fsig;
10175 MonoInst *vtable_arg = NULL;
10178 token = read32 (ip + 1);
10179 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10182 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10185 mono_save_token_info (cfg, image, token, cmethod);
10187 if (!mono_class_init (cmethod->klass))
10188 TYPE_LOAD_ERROR (cmethod->klass);
10190 context_used = mini_method_check_context_used (cfg, cmethod);
10192 if (mono_security_core_clr_enabled ())
10193 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10195 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)) {
10196 emit_class_init (cfg, cmethod->klass);
10197 CHECK_TYPELOAD (cmethod->klass);
10201 if (cfg->gsharedvt) {
10202 if (mini_is_gsharedvt_variable_signature (sig))
10203 GSHAREDVT_FAILURE (*ip);
10207 n = fsig->param_count;
10211 * Generate smaller code for the common newobj <exception> instruction in
10212 * argument checking code.
10214 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10215 is_exception_class (cmethod->klass) && n <= 2 &&
10216 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10217 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10218 MonoInst *iargs [3];
10222 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10225 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10228 iargs [1] = sp [0];
10229 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10232 iargs [1] = sp [0];
10233 iargs [2] = sp [1];
10234 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10237 g_assert_not_reached ();
10245 /* move the args to allow room for 'this' in the first position */
10251 /* check_call_signature () requires sp[0] to be set */
10252 this_ins.type = STACK_OBJ;
10253 sp [0] = &this_ins;
10254 if (check_call_signature (cfg, fsig, sp))
10259 if (mini_class_is_system_array (cmethod->klass)) {
10260 *sp = emit_get_rgctx_method (cfg, context_used,
10261 cmethod, MONO_RGCTX_INFO_METHOD);
10263 /* Avoid varargs in the common case */
10264 if (fsig->param_count == 1)
10265 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10266 else if (fsig->param_count == 2)
10267 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10268 else if (fsig->param_count == 3)
10269 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10270 else if (fsig->param_count == 4)
10271 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10273 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10274 } else if (cmethod->string_ctor) {
10275 g_assert (!context_used);
10276 g_assert (!vtable_arg);
10277 /* we simply pass a null pointer */
10278 EMIT_NEW_PCONST (cfg, *sp, NULL);
10279 /* now call the string ctor */
10280 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10282 if (cmethod->klass->valuetype) {
10283 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10284 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10285 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10290 * The code generated by mini_emit_virtual_call () expects
10291 * iargs [0] to be a boxed instance, but luckily the vcall
10292 * will be transformed into a normal call there.
10294 } else if (context_used) {
10295 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10298 MonoVTable *vtable = NULL;
10300 if (!cfg->compile_aot)
10301 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10302 CHECK_TYPELOAD (cmethod->klass);
10305 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10306 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10307 * As a workaround, we call class cctors before allocating objects.
10309 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10310 emit_class_init (cfg, cmethod->klass);
10311 if (cfg->verbose_level > 2)
10312 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10313 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10316 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10319 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10322 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10324 /* Now call the actual ctor */
10325 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10326 CHECK_CFG_EXCEPTION;
10329 if (alloc == NULL) {
10331 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10332 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10340 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10341 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10344 case CEE_CASTCLASS:
10349 token = read32 (ip + 1);
10350 klass = mini_get_class (method, token, generic_context);
10351 CHECK_TYPELOAD (klass);
10352 if (sp [0]->type != STACK_OBJ)
10355 MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
10356 ins->dreg = alloc_preg (cfg);
10357 ins->sreg1 = (*sp)->dreg;
10358 ins->klass = klass;
10359 ins->type = STACK_OBJ;
10360 MONO_ADD_INS (cfg->cbb, ins);
10362 CHECK_CFG_EXCEPTION;
10366 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10369 case CEE_UNBOX_ANY: {
10370 MonoInst *res, *addr;
10375 token = read32 (ip + 1);
10376 klass = mini_get_class (method, token, generic_context);
10377 CHECK_TYPELOAD (klass);
10379 mono_save_token_info (cfg, image, token, klass);
10381 context_used = mini_class_check_context_used (cfg, klass);
10383 if (mini_is_gsharedvt_klass (klass)) {
10384 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10386 } else if (generic_class_is_reference_type (cfg, klass)) {
10387 if (MONO_INS_IS_PCONST_NULL (*sp)) {
10388 EMIT_NEW_PCONST (cfg, res, NULL);
10389 res->type = STACK_OBJ;
10391 MONO_INST_NEW (cfg, res, OP_CASTCLASS);
10392 res->dreg = alloc_preg (cfg);
10393 res->sreg1 = (*sp)->dreg;
10394 res->klass = klass;
10395 res->type = STACK_OBJ;
10396 MONO_ADD_INS (cfg->cbb, res);
10397 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10399 } else if (mono_class_is_nullable (klass)) {
10400 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10402 addr = handle_unbox (cfg, klass, sp, context_used);
10404 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10415 MonoClass *enum_class;
10416 MonoMethod *has_flag;
10422 token = read32 (ip + 1);
10423 klass = mini_get_class (method, token, generic_context);
10424 CHECK_TYPELOAD (klass);
10426 mono_save_token_info (cfg, image, token, klass);
10428 context_used = mini_class_check_context_used (cfg, klass);
10430 if (generic_class_is_reference_type (cfg, klass)) {
10436 if (klass == mono_defaults.void_class)
10438 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10440 /* frequent check in generic code: box (struct), brtrue */
10445 * <push int/long ptr>
10448 * constrained. MyFlags
10449 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10451 * If we find this sequence and the operand types on box and constrained
10452 * are equal, we can emit a specialized instruction sequence instead of
10453 * the very slow HasFlag () call.
10455 if ((cfg->opt & MONO_OPT_INTRINS) &&
10456 /* Cheap checks first. */
10457 ip + 5 + 6 + 5 < end &&
10458 ip [5] == CEE_PREFIX1 &&
10459 ip [6] == CEE_CONSTRAINED_ &&
10460 ip [11] == CEE_CALLVIRT &&
10461 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10462 mono_class_is_enum (klass) &&
10463 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10464 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10465 has_flag->klass == mono_defaults.enum_class &&
10466 !strcmp (has_flag->name, "HasFlag") &&
10467 has_flag->signature->hasthis &&
10468 has_flag->signature->param_count == 1) {
10469 CHECK_TYPELOAD (enum_class);
10471 if (enum_class == klass) {
10472 MonoInst *enum_this, *enum_flag;
10477 enum_this = sp [0];
10478 enum_flag = sp [1];
10480 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10485 // FIXME: LLVM can't handle the inconsistent bb linking
10486 if (!mono_class_is_nullable (klass) &&
10487 !mini_is_gsharedvt_klass (klass) &&
10488 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10489 (ip [5] == CEE_BRTRUE ||
10490 ip [5] == CEE_BRTRUE_S ||
10491 ip [5] == CEE_BRFALSE ||
10492 ip [5] == CEE_BRFALSE_S)) {
10493 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10495 MonoBasicBlock *true_bb, *false_bb;
10499 if (cfg->verbose_level > 3) {
10500 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10501 printf ("<box+brtrue opt>\n");
10506 case CEE_BRFALSE_S:
10509 target = ip + 1 + (signed char)(*ip);
10516 target = ip + 4 + (gint)(read32 (ip));
10520 g_assert_not_reached ();
10524 * We need to link both bblocks, since it is needed for handling stack
10525 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10526 * Branching to only one of them would lead to inconsistencies, so
10527 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10529 GET_BBLOCK (cfg, true_bb, target);
10530 GET_BBLOCK (cfg, false_bb, ip);
10532 mono_link_bblock (cfg, cfg->cbb, true_bb);
10533 mono_link_bblock (cfg, cfg->cbb, false_bb);
10535 if (sp != stack_start) {
10536 handle_stack_args (cfg, stack_start, sp - stack_start);
10538 CHECK_UNVERIFIABLE (cfg);
10541 if (COMPILE_LLVM (cfg)) {
10542 dreg = alloc_ireg (cfg);
10543 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10544 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10546 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10548 /* The JIT can't eliminate the iconst+compare */
10549 MONO_INST_NEW (cfg, ins, OP_BR);
10550 ins->inst_target_bb = is_true ? true_bb : false_bb;
10551 MONO_ADD_INS (cfg->cbb, ins);
10554 start_new_bblock = 1;
10558 *sp++ = handle_box (cfg, val, klass, context_used);
10560 CHECK_CFG_EXCEPTION;
10569 token = read32 (ip + 1);
10570 klass = mini_get_class (method, token, generic_context);
10571 CHECK_TYPELOAD (klass);
10573 mono_save_token_info (cfg, image, token, klass);
10575 context_used = mini_class_check_context_used (cfg, klass);
10577 if (mono_class_is_nullable (klass)) {
10580 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10581 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10585 ins = handle_unbox (cfg, klass, sp, context_used);
10598 MonoClassField *field;
10599 #ifndef DISABLE_REMOTING
10603 gboolean is_instance;
10605 gpointer addr = NULL;
10606 gboolean is_special_static;
10608 MonoInst *store_val = NULL;
10609 MonoInst *thread_ins;
10612 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10614 if (op == CEE_STFLD) {
10617 store_val = sp [1];
10622 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10624 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10627 if (op == CEE_STSFLD) {
10630 store_val = sp [0];
10635 token = read32 (ip + 1);
10636 if (method->wrapper_type != MONO_WRAPPER_NONE) {
10637 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
10638 klass = field->parent;
10641 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10644 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10645 FIELD_ACCESS_FAILURE (method, field);
10646 mono_class_init (klass);
10648 /* if the class is Critical then transparent code cannot access it's fields */
10649 if (!is_instance && mono_security_core_clr_enabled ())
10650 ensure_method_is_allowed_to_access_field (cfg, method, field);
10652 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10653 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
10654 if (mono_security_core_clr_enabled ())
10655 ensure_method_is_allowed_to_access_field (cfg, method, field);
10658 ftype = mono_field_get_type (field);
10661 * LDFLD etc. is usable on static fields as well, so convert those cases to
10664 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10676 g_assert_not_reached ();
10678 is_instance = FALSE;
10681 context_used = mini_class_check_context_used (cfg, klass);
10683 /* INSTANCE CASE */
10685 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10686 if (op == CEE_STFLD) {
10687 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10689 #ifndef DISABLE_REMOTING
10690 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10691 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
10692 MonoInst *iargs [5];
10694 GSHAREDVT_FAILURE (op);
10696 iargs [0] = sp [0];
10697 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10698 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10699 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
10701 iargs [4] = sp [1];
10703 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10704 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
10705 iargs, ip, cfg->real_offset, TRUE);
10706 CHECK_CFG_EXCEPTION;
10707 g_assert (costs > 0);
10709 cfg->real_offset += 5;
10711 inline_costs += costs;
10713 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10718 MonoInst *store, *wbarrier_ptr_ins = NULL;
10720 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10722 if (ins_flag & MONO_INST_VOLATILE) {
10723 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10724 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10727 if (mini_is_gsharedvt_klass (klass)) {
10728 MonoInst *offset_ins;
10730 context_used = mini_class_check_context_used (cfg, klass);
10732 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10733 /* The value is offset by 1 */
10734 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10735 dreg = alloc_ireg_mp (cfg);
10736 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10737 wbarrier_ptr_ins = ins;
10738 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
10739 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10741 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10743 if (sp [0]->opcode != OP_LDADDR)
10744 store->flags |= MONO_INST_FAULT;
10746 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
10747 if (mini_is_gsharedvt_klass (klass)) {
10748 g_assert (wbarrier_ptr_ins);
10749 emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
10751 /* insert call to write barrier */
10755 dreg = alloc_ireg_mp (cfg);
10756 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10757 emit_write_barrier (cfg, ptr, sp [1]);
10761 store->flags |= ins_flag;
10768 #ifndef DISABLE_REMOTING
10769 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10770 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
10771 MonoInst *iargs [4];
10773 GSHAREDVT_FAILURE (op);
10775 iargs [0] = sp [0];
10776 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10777 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10778 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10779 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10780 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
10781 iargs, ip, cfg->real_offset, TRUE);
10782 CHECK_CFG_EXCEPTION;
10783 g_assert (costs > 0);
10785 cfg->real_offset += 5;
10789 inline_costs += costs;
10791 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10797 if (sp [0]->type == STACK_VTYPE) {
10800 /* Have to compute the address of the variable */
10802 var = get_vreg_to_inst (cfg, sp [0]->dreg);
10804 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10806 g_assert (var->klass == klass);
10808 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10812 if (op == CEE_LDFLDA) {
10813 if (sp [0]->type == STACK_OBJ) {
10814 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10815 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10818 dreg = alloc_ireg_mp (cfg);
10820 if (mini_is_gsharedvt_klass (klass)) {
10821 MonoInst *offset_ins;
10823 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10824 /* The value is offset by 1 */
10825 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10826 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10828 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10830 ins->klass = mono_class_from_mono_type (field->type);
10831 ins->type = STACK_MP;
10836 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10838 if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
10839 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
10848 if (mini_is_gsharedvt_klass (klass)) {
10849 MonoInst *offset_ins;
10851 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10852 /* The value is offset by 1 */
10853 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10854 dreg = alloc_ireg_mp (cfg);
10855 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10856 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
10858 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
10860 load->flags |= ins_flag;
10861 if (sp [0]->opcode != OP_LDADDR)
10862 load->flags |= MONO_INST_FAULT;
10874 context_used = mini_class_check_context_used (cfg, klass);
10876 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
10877 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
10881 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10882 * to be called here.
10884 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10885 mono_class_vtable (cfg->domain, klass);
10886 CHECK_TYPELOAD (klass);
10888 mono_domain_lock (cfg->domain);
10889 if (cfg->domain->special_static_fields)
10890 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10891 mono_domain_unlock (cfg->domain);
10893 is_special_static = mono_class_field_is_special_static (field);
10895 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10896 thread_ins = mono_create_tls_get (cfg, TLS_KEY_THREAD);
10900 /* Generate IR to compute the field address */
10901 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
10903 * Fast access to TLS data
10904 * Inline version of get_thread_static_data () in
10908 int idx, static_data_reg, array_reg, dreg;
10910 if (context_used && cfg->gsharedvt && mini_is_gsharedvt_klass (klass))
10911 GSHAREDVT_FAILURE (op);
10913 static_data_reg = alloc_ireg (cfg);
10914 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
10916 if (cfg->compile_aot) {
10917 int offset_reg, offset2_reg, idx_reg;
10919 /* For TLS variables, this will return the TLS offset */
10920 EMIT_NEW_SFLDACONST (cfg, ins, field);
10921 offset_reg = ins->dreg;
10922 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
10923 idx_reg = alloc_ireg (cfg);
10924 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
10925 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
10926 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
10927 array_reg = alloc_ireg (cfg);
10928 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
10929 offset2_reg = alloc_ireg (cfg);
10930 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
10931 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
10932 dreg = alloc_ireg (cfg);
10933 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
10935 offset = (gsize)addr & 0x7fffffff;
10936 idx = offset & 0x3f;
10938 array_reg = alloc_ireg (cfg);
10939 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
10940 dreg = alloc_ireg (cfg);
10941 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
10943 } else if ((cfg->opt & MONO_OPT_SHARED) ||
10944 (cfg->compile_aot && is_special_static) ||
10945 (context_used && is_special_static)) {
10946 MonoInst *iargs [2];
10948 g_assert (field->parent);
10949 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10950 if (context_used) {
10951 iargs [1] = emit_get_rgctx_field (cfg, context_used,
10952 field, MONO_RGCTX_INFO_CLASS_FIELD);
10954 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10956 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10957 } else if (context_used) {
10958 MonoInst *static_data;
10961 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
10962 method->klass->name_space, method->klass->name, method->name,
10963 depth, field->offset);
10966 if (mono_class_needs_cctor_run (klass, method))
10967 emit_class_init (cfg, klass);
10970 * The pointer we're computing here is
10972 * super_info.static_data + field->offset
10974 static_data = mini_emit_get_rgctx_klass (cfg, context_used,
10975 klass, MONO_RGCTX_INFO_STATIC_DATA);
10977 if (mini_is_gsharedvt_klass (klass)) {
10978 MonoInst *offset_ins;
10980 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10981 /* The value is offset by 1 */
10982 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10983 dreg = alloc_ireg_mp (cfg);
10984 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
10985 } else if (field->offset == 0) {
10988 int addr_reg = mono_alloc_preg (cfg);
10989 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
10991 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
10992 MonoInst *iargs [2];
10994 g_assert (field->parent);
10995 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10996 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10997 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10999 MonoVTable *vtable = NULL;
11001 if (!cfg->compile_aot)
11002 vtable = mono_class_vtable (cfg->domain, klass);
11003 CHECK_TYPELOAD (klass);
11006 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11007 if (!(g_slist_find (class_inits, klass))) {
11008 emit_class_init (cfg, klass);
11009 if (cfg->verbose_level > 2)
11010 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11011 class_inits = g_slist_prepend (class_inits, klass);
11014 if (cfg->run_cctors) {
11015 /* This makes so that inline cannot trigger */
11016 /* .cctors: too many apps depend on them */
11017 /* running with a specific order... */
11019 if (! vtable->initialized)
11020 INLINE_FAILURE ("class init");
11021 if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
11022 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11023 goto exception_exit;
11027 if (cfg->compile_aot)
11028 EMIT_NEW_SFLDACONST (cfg, ins, field);
11031 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11033 EMIT_NEW_PCONST (cfg, ins, addr);
11036 MonoInst *iargs [1];
11037 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11038 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11042 /* Generate IR to do the actual load/store operation */
11044 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11045 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11046 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11049 if (op == CEE_LDSFLDA) {
11050 ins->klass = mono_class_from_mono_type (ftype);
11051 ins->type = STACK_PTR;
11053 } else if (op == CEE_STSFLD) {
11056 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11057 store->flags |= ins_flag;
11059 gboolean is_const = FALSE;
11060 MonoVTable *vtable = NULL;
11061 gpointer addr = NULL;
11063 if (!context_used) {
11064 vtable = mono_class_vtable (cfg->domain, klass);
11065 CHECK_TYPELOAD (klass);
11067 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11068 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11069 int ro_type = ftype->type;
11071 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11072 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11073 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11076 GSHAREDVT_FAILURE (op);
11078 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11081 case MONO_TYPE_BOOLEAN:
11083 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11087 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11090 case MONO_TYPE_CHAR:
11092 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11096 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11101 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11105 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11110 case MONO_TYPE_PTR:
11111 case MONO_TYPE_FNPTR:
11112 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11113 type_to_eval_stack_type ((cfg), field->type, *sp);
11116 case MONO_TYPE_STRING:
11117 case MONO_TYPE_OBJECT:
11118 case MONO_TYPE_CLASS:
11119 case MONO_TYPE_SZARRAY:
11120 case MONO_TYPE_ARRAY:
11121 if (!mono_gc_is_moving ()) {
11122 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11123 type_to_eval_stack_type ((cfg), field->type, *sp);
11131 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11136 case MONO_TYPE_VALUETYPE:
11146 CHECK_STACK_OVF (1);
11148 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11149 load->flags |= ins_flag;
11155 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11156 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11157 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11168 token = read32 (ip + 1);
11169 klass = mini_get_class (method, token, generic_context);
11170 CHECK_TYPELOAD (klass);
11171 if (ins_flag & MONO_INST_VOLATILE) {
11172 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11173 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11175 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11176 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11177 ins->flags |= ins_flag;
11178 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11179 generic_class_is_reference_type (cfg, klass) && !MONO_INS_IS_PCONST_NULL (sp [1])) {
11180 /* insert call to write barrier */
11181 emit_write_barrier (cfg, sp [0], sp [1]);
11193 const char *data_ptr;
11195 guint32 field_token;
11201 token = read32 (ip + 1);
11203 klass = mini_get_class (method, token, generic_context);
11204 CHECK_TYPELOAD (klass);
11206 context_used = mini_class_check_context_used (cfg, klass);
11208 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11209 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11210 ins->sreg1 = sp [0]->dreg;
11211 ins->type = STACK_I4;
11212 ins->dreg = alloc_ireg (cfg);
11213 MONO_ADD_INS (cfg->cbb, ins);
11214 *sp = mono_decompose_opcode (cfg, ins);
11217 if (context_used) {
11218 MonoInst *args [3];
11219 MonoClass *array_class = mono_array_class_get (klass, 1);
11220 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11222 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11225 args [0] = mini_emit_get_rgctx_klass (cfg, context_used,
11226 array_class, MONO_RGCTX_INFO_VTABLE);
11231 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11233 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11235 if (cfg->opt & MONO_OPT_SHARED) {
11236 /* Decompose now to avoid problems with references to the domainvar */
11237 MonoInst *iargs [3];
11239 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11240 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11241 iargs [2] = sp [0];
11243 ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
11245 /* Decompose later since it is needed by abcrem */
11246 MonoClass *array_type = mono_array_class_get (klass, 1);
11247 mono_class_vtable (cfg->domain, array_type);
11248 CHECK_TYPELOAD (array_type);
11250 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11251 ins->dreg = alloc_ireg_ref (cfg);
11252 ins->sreg1 = sp [0]->dreg;
11253 ins->inst_newa_class = klass;
11254 ins->type = STACK_OBJ;
11255 ins->klass = array_type;
11256 MONO_ADD_INS (cfg->cbb, ins);
11257 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11258 cfg->cbb->has_array_access = TRUE;
11260 /* Needed so mono_emit_load_get_addr () gets called */
11261 mono_get_got_var (cfg);
11271 * we inline/optimize the initialization sequence if possible.
11272 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11273 * for small sizes open code the memcpy
11274 * ensure the rva field is big enough
11276 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))) {
11277 MonoMethod *memcpy_method = get_memcpy_method ();
11278 MonoInst *iargs [3];
11279 int add_reg = alloc_ireg_mp (cfg);
11281 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11282 if (cfg->compile_aot) {
11283 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11285 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11287 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11288 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11297 if (sp [0]->type != STACK_OBJ)
11300 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11301 ins->dreg = alloc_preg (cfg);
11302 ins->sreg1 = sp [0]->dreg;
11303 ins->type = STACK_I4;
11304 /* This flag will be inherited by the decomposition */
11305 ins->flags |= MONO_INST_FAULT;
11306 MONO_ADD_INS (cfg->cbb, ins);
11307 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11308 cfg->cbb->has_array_access = TRUE;
11316 if (sp [0]->type != STACK_OBJ)
11319 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11321 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11322 CHECK_TYPELOAD (klass);
11323 /* we need to make sure that this array is exactly the type it needs
11324 * to be for correctness. the wrappers are lax with their usage
11325 * so we need to ignore them here
11327 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11328 MonoClass *array_class = mono_array_class_get (klass, 1);
11329 mini_emit_check_array_type (cfg, sp [0], array_class);
11330 CHECK_TYPELOAD (array_class);
11334 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11339 case CEE_LDELEM_I1:
11340 case CEE_LDELEM_U1:
11341 case CEE_LDELEM_I2:
11342 case CEE_LDELEM_U2:
11343 case CEE_LDELEM_I4:
11344 case CEE_LDELEM_U4:
11345 case CEE_LDELEM_I8:
11347 case CEE_LDELEM_R4:
11348 case CEE_LDELEM_R8:
11349 case CEE_LDELEM_REF: {
11355 if (*ip == CEE_LDELEM) {
11357 token = read32 (ip + 1);
11358 klass = mini_get_class (method, token, generic_context);
11359 CHECK_TYPELOAD (klass);
11360 mono_class_init (klass);
11363 klass = array_access_to_klass (*ip);
11365 if (sp [0]->type != STACK_OBJ)
11368 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11370 if (mini_is_gsharedvt_variable_klass (klass)) {
11371 // FIXME-VT: OP_ICONST optimization
11372 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11373 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11374 ins->opcode = OP_LOADV_MEMBASE;
11375 } else if (sp [1]->opcode == OP_ICONST) {
11376 int array_reg = sp [0]->dreg;
11377 int index_reg = sp [1]->dreg;
11378 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11380 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11381 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11383 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11384 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11386 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11387 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11390 if (*ip == CEE_LDELEM)
11397 case CEE_STELEM_I1:
11398 case CEE_STELEM_I2:
11399 case CEE_STELEM_I4:
11400 case CEE_STELEM_I8:
11401 case CEE_STELEM_R4:
11402 case CEE_STELEM_R8:
11403 case CEE_STELEM_REF:
11408 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11410 if (*ip == CEE_STELEM) {
11412 token = read32 (ip + 1);
11413 klass = mini_get_class (method, token, generic_context);
11414 CHECK_TYPELOAD (klass);
11415 mono_class_init (klass);
11418 klass = array_access_to_klass (*ip);
11420 if (sp [0]->type != STACK_OBJ)
11423 emit_array_store (cfg, klass, sp, TRUE);
11425 if (*ip == CEE_STELEM)
11432 case CEE_CKFINITE: {
11436 if (cfg->llvm_only) {
11437 MonoInst *iargs [1];
11439 iargs [0] = sp [0];
11440 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
11442 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11443 ins->sreg1 = sp [0]->dreg;
11444 ins->dreg = alloc_freg (cfg);
11445 ins->type = STACK_R8;
11446 MONO_ADD_INS (cfg->cbb, ins);
11448 *sp++ = mono_decompose_opcode (cfg, ins);
11454 case CEE_REFANYVAL: {
11455 MonoInst *src_var, *src;
11457 int klass_reg = alloc_preg (cfg);
11458 int dreg = alloc_preg (cfg);
11460 GSHAREDVT_FAILURE (*ip);
11463 MONO_INST_NEW (cfg, ins, *ip);
11466 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11467 CHECK_TYPELOAD (klass);
11469 context_used = mini_class_check_context_used (cfg, klass);
11472 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11474 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11475 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11476 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11478 if (context_used) {
11479 MonoInst *klass_ins;
11481 klass_ins = mini_emit_get_rgctx_klass (cfg, context_used,
11482 klass, MONO_RGCTX_INFO_KLASS);
11485 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11486 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11488 mini_emit_class_check (cfg, klass_reg, klass);
11490 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11491 ins->type = STACK_MP;
11492 ins->klass = klass;
11497 case CEE_MKREFANY: {
11498 MonoInst *loc, *addr;
11500 GSHAREDVT_FAILURE (*ip);
11503 MONO_INST_NEW (cfg, ins, *ip);
11506 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11507 CHECK_TYPELOAD (klass);
11509 context_used = mini_class_check_context_used (cfg, klass);
11511 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11512 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11514 if (context_used) {
11515 MonoInst *const_ins;
11516 int type_reg = alloc_preg (cfg);
11518 const_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11519 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11520 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11521 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11523 int const_reg = alloc_preg (cfg);
11524 int type_reg = alloc_preg (cfg);
11526 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11527 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11528 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11529 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11531 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11533 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11534 ins->type = STACK_VTYPE;
11535 ins->klass = mono_defaults.typed_reference_class;
11540 case CEE_LDTOKEN: {
11542 MonoClass *handle_class;
11544 CHECK_STACK_OVF (1);
11547 n = read32 (ip + 1);
11549 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11550 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11551 handle = mono_method_get_wrapper_data (method, n);
11552 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
11553 if (handle_class == mono_defaults.typehandle_class)
11554 handle = &((MonoClass*)handle)->byval_arg;
11557 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11562 mono_class_init (handle_class);
11563 if (cfg->gshared) {
11564 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11565 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11566 /* This case handles ldtoken
11567 of an open type, like for
11570 } else if (handle_class == mono_defaults.typehandle_class) {
11571 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
11572 } else if (handle_class == mono_defaults.fieldhandle_class)
11573 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11574 else if (handle_class == mono_defaults.methodhandle_class)
11575 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
11577 g_assert_not_reached ();
11580 if ((cfg->opt & MONO_OPT_SHARED) &&
11581 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11582 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11583 MonoInst *addr, *vtvar, *iargs [3];
11584 int method_context_used;
11586 method_context_used = mini_method_check_context_used (cfg, method);
11588 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11590 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11591 EMIT_NEW_ICONST (cfg, iargs [1], n);
11592 if (method_context_used) {
11593 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11594 method, MONO_RGCTX_INFO_METHOD);
11595 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11597 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11598 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11600 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11602 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11604 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11606 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11607 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
11608 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11609 (cmethod->klass == mono_defaults.systemtype_class) &&
11610 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11611 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
11613 mono_class_init (tclass);
11614 if (context_used) {
11615 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11616 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11617 } else if (cfg->compile_aot) {
11618 if (method->wrapper_type) {
11619 error_init (&error); //got to do it since there are multiple conditionals below
11620 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11621 /* Special case for static synchronized wrappers */
11622 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11624 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11625 /* FIXME: n is not a normal token */
11627 EMIT_NEW_PCONST (cfg, ins, NULL);
11630 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11633 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
11635 EMIT_NEW_PCONST (cfg, ins, rt);
11637 ins->type = STACK_OBJ;
11638 ins->klass = cmethod->klass;
11641 MonoInst *addr, *vtvar;
11643 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11645 if (context_used) {
11646 if (handle_class == mono_defaults.typehandle_class) {
11647 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11648 mono_class_from_mono_type ((MonoType *)handle),
11649 MONO_RGCTX_INFO_TYPE);
11650 } else if (handle_class == mono_defaults.methodhandle_class) {
11651 ins = emit_get_rgctx_method (cfg, context_used,
11652 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
11653 } else if (handle_class == mono_defaults.fieldhandle_class) {
11654 ins = emit_get_rgctx_field (cfg, context_used,
11655 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
11657 g_assert_not_reached ();
11659 } else if (cfg->compile_aot) {
11660 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11662 EMIT_NEW_PCONST (cfg, ins, handle);
11664 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11665 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11666 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11676 if (sp [-1]->type != STACK_OBJ)
11679 MONO_INST_NEW (cfg, ins, OP_THROW);
11681 ins->sreg1 = sp [0]->dreg;
11683 cfg->cbb->out_of_line = TRUE;
11684 MONO_ADD_INS (cfg->cbb, ins);
11685 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11686 MONO_ADD_INS (cfg->cbb, ins);
11689 link_bblock (cfg, cfg->cbb, end_bblock);
11690 start_new_bblock = 1;
11691 /* This can complicate code generation for llvm since the return value might not be defined */
11692 if (COMPILE_LLVM (cfg))
11693 INLINE_FAILURE ("throw");
11695 case CEE_ENDFINALLY:
11696 if (!ip_in_finally_clause (cfg, ip - header->code))
11698 /* mono_save_seq_point_info () depends on this */
11699 if (sp != stack_start)
11700 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11701 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11702 MONO_ADD_INS (cfg->cbb, ins);
11704 start_new_bblock = 1;
11707 * Control will leave the method so empty the stack, otherwise
11708 * the next basic block will start with a nonempty stack.
11710 while (sp != stack_start) {
11715 case CEE_LEAVE_S: {
11718 if (*ip == CEE_LEAVE) {
11720 target = ip + 5 + (gint32)read32(ip + 1);
11723 target = ip + 2 + (signed char)(ip [1]);
11726 /* empty the stack */
11727 while (sp != stack_start) {
11732 * If this leave statement is in a catch block, check for a
11733 * pending exception, and rethrow it if necessary.
11734 * We avoid doing this in runtime invoke wrappers, since those are called
11735 * by native code which excepts the wrapper to catch all exceptions.
11737 for (i = 0; i < header->num_clauses; ++i) {
11738 MonoExceptionClause *clause = &header->clauses [i];
11741 * Use <= in the final comparison to handle clauses with multiple
11742 * leave statements, like in bug #78024.
11743 * The ordering of the exception clauses guarantees that we find the
11744 * innermost clause.
11746 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) {
11748 MonoBasicBlock *dont_throw;
11753 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11756 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11758 NEW_BBLOCK (cfg, dont_throw);
11761 * Currently, we always rethrow the abort exception, despite the
11762 * fact that this is not correct. See thread6.cs for an example.
11763 * But propagating the abort exception is more important than
11764 * getting the sematics right.
11766 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11767 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11768 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11770 MONO_START_BB (cfg, dont_throw);
11775 cfg->cbb->try_end = (intptr_t)(ip - header->code);
11778 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11780 MonoExceptionClause *clause;
11782 for (tmp = handlers; tmp; tmp = tmp->next) {
11783 clause = (MonoExceptionClause *)tmp->data;
11784 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11786 link_bblock (cfg, cfg->cbb, tblock);
11787 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11788 ins->inst_target_bb = tblock;
11789 ins->inst_eh_block = clause;
11790 MONO_ADD_INS (cfg->cbb, ins);
11791 cfg->cbb->has_call_handler = 1;
11792 if (COMPILE_LLVM (cfg)) {
11793 MonoBasicBlock *target_bb;
11796 * Link the finally bblock with the target, since it will
11797 * conceptually branch there.
11799 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
11800 GET_BBLOCK (cfg, target_bb, target);
11801 link_bblock (cfg, tblock, target_bb);
11804 g_list_free (handlers);
11807 MONO_INST_NEW (cfg, ins, OP_BR);
11808 MONO_ADD_INS (cfg->cbb, ins);
11809 GET_BBLOCK (cfg, tblock, target);
11810 link_bblock (cfg, cfg->cbb, tblock);
11811 ins->inst_target_bb = tblock;
11813 start_new_bblock = 1;
11815 if (*ip == CEE_LEAVE)
11824 * Mono specific opcodes
11826 case MONO_CUSTOM_PREFIX: {
11828 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11832 case CEE_MONO_ICALL: {
11834 MonoJitICallInfo *info;
11836 token = read32 (ip + 2);
11837 func = mono_method_get_wrapper_data (method, token);
11838 info = mono_find_jit_icall_by_addr (func);
11840 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11843 CHECK_STACK (info->sig->param_count);
11844 sp -= info->sig->param_count;
11846 ins = mono_emit_jit_icall (cfg, info->func, sp);
11847 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11851 inline_costs += 10 * num_calls++;
11855 case CEE_MONO_LDPTR_CARD_TABLE:
11856 case CEE_MONO_LDPTR_NURSERY_START:
11857 case CEE_MONO_LDPTR_NURSERY_BITS:
11858 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
11859 CHECK_STACK_OVF (1);
11862 case CEE_MONO_LDPTR_CARD_TABLE:
11863 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11865 case CEE_MONO_LDPTR_NURSERY_START:
11866 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11868 case CEE_MONO_LDPTR_NURSERY_BITS:
11869 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
11871 case CEE_MONO_LDPTR_INT_REQ_FLAG:
11872 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11878 inline_costs += 10 * num_calls++;
11881 case CEE_MONO_LDPTR: {
11884 CHECK_STACK_OVF (1);
11886 token = read32 (ip + 2);
11888 ptr = mono_method_get_wrapper_data (method, token);
11889 EMIT_NEW_PCONST (cfg, ins, ptr);
11892 inline_costs += 10 * num_calls++;
11893 /* Can't embed random pointers into AOT code */
11897 case CEE_MONO_JIT_ICALL_ADDR: {
11898 MonoJitICallInfo *callinfo;
11901 CHECK_STACK_OVF (1);
11903 token = read32 (ip + 2);
11905 ptr = mono_method_get_wrapper_data (method, token);
11906 callinfo = mono_find_jit_icall_by_addr (ptr);
11907 g_assert (callinfo);
11908 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
11911 inline_costs += 10 * num_calls++;
11914 case CEE_MONO_ICALL_ADDR: {
11915 MonoMethod *cmethod;
11918 CHECK_STACK_OVF (1);
11920 token = read32 (ip + 2);
11922 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
11924 if (cfg->compile_aot) {
11925 if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
11927 * This is generated by emit_native_wrapper () to resolve the pinvoke address
11928 * before the call, its not needed when using direct pinvoke.
11929 * This is not an optimization, but its used to avoid looking up pinvokes
11930 * on platforms which don't support dlopen ().
11932 EMIT_NEW_PCONST (cfg, ins, NULL);
11934 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
11937 ptr = mono_lookup_internal_call (cmethod);
11939 EMIT_NEW_PCONST (cfg, ins, ptr);
11945 case CEE_MONO_VTADDR: {
11946 MonoInst *src_var, *src;
11952 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11953 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
11958 case CEE_MONO_NEWOBJ: {
11959 MonoInst *iargs [2];
11961 CHECK_STACK_OVF (1);
11963 token = read32 (ip + 2);
11964 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11965 mono_class_init (klass);
11966 NEW_DOMAINCONST (cfg, iargs [0]);
11967 MONO_ADD_INS (cfg->cbb, iargs [0]);
11968 NEW_CLASSCONST (cfg, iargs [1], klass);
11969 MONO_ADD_INS (cfg->cbb, iargs [1]);
11970 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
11972 inline_costs += 10 * num_calls++;
11975 case CEE_MONO_OBJADDR:
11978 MONO_INST_NEW (cfg, ins, OP_MOVE);
11979 ins->dreg = alloc_ireg_mp (cfg);
11980 ins->sreg1 = sp [0]->dreg;
11981 ins->type = STACK_MP;
11982 MONO_ADD_INS (cfg->cbb, ins);
11986 case CEE_MONO_LDNATIVEOBJ:
11988 * Similar to LDOBJ, but instead load the unmanaged
11989 * representation of the vtype to the stack.
11994 token = read32 (ip + 2);
11995 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11996 g_assert (klass->valuetype);
11997 mono_class_init (klass);
12000 MonoInst *src, *dest, *temp;
12003 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12004 temp->backend.is_pinvoke = 1;
12005 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12006 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12008 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12009 dest->type = STACK_VTYPE;
12010 dest->klass = klass;
12016 case CEE_MONO_RETOBJ: {
12018 * Same as RET, but return the native representation of a vtype
12021 g_assert (cfg->ret);
12022 g_assert (mono_method_signature (method)->pinvoke);
12027 token = read32 (ip + 2);
12028 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12030 if (!cfg->vret_addr) {
12031 g_assert (cfg->ret_var_is_local);
12033 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12035 EMIT_NEW_RETLOADA (cfg, ins);
12037 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12039 if (sp != stack_start)
12042 MONO_INST_NEW (cfg, ins, OP_BR);
12043 ins->inst_target_bb = end_bblock;
12044 MONO_ADD_INS (cfg->cbb, ins);
12045 link_bblock (cfg, cfg->cbb, end_bblock);
12046 start_new_bblock = 1;
12050 case CEE_MONO_SAVE_LMF:
12051 case CEE_MONO_RESTORE_LMF:
12054 case CEE_MONO_CLASSCONST:
12055 CHECK_STACK_OVF (1);
12057 token = read32 (ip + 2);
12058 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12061 inline_costs += 10 * num_calls++;
12063 case CEE_MONO_NOT_TAKEN:
12064 cfg->cbb->out_of_line = TRUE;
12067 case CEE_MONO_TLS: {
12070 CHECK_STACK_OVF (1);
12072 key = (MonoTlsKey)read32 (ip + 2);
12073 g_assert (key < TLS_KEY_NUM);
12075 ins = mono_create_tls_get (cfg, key);
12077 ins->type = STACK_PTR;
12082 case CEE_MONO_DYN_CALL: {
12083 MonoCallInst *call;
12085 /* It would be easier to call a trampoline, but that would put an
12086 * extra frame on the stack, confusing exception handling. So
12087 * implement it inline using an opcode for now.
12090 if (!cfg->dyn_call_var) {
12091 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12092 /* prevent it from being register allocated */
12093 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12096 /* Has to use a call inst since it local regalloc expects it */
12097 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12098 ins = (MonoInst*)call;
12100 ins->sreg1 = sp [0]->dreg;
12101 ins->sreg2 = sp [1]->dreg;
12102 MONO_ADD_INS (cfg->cbb, ins);
12104 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12107 inline_costs += 10 * num_calls++;
12111 case CEE_MONO_MEMORY_BARRIER: {
12113 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12117 case CEE_MONO_ATOMIC_STORE_I4: {
12118 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
12124 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
12125 ins->dreg = sp [0]->dreg;
12126 ins->sreg1 = sp [1]->dreg;
12127 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
12128 MONO_ADD_INS (cfg->cbb, ins);
12133 case CEE_MONO_JIT_ATTACH: {
12134 MonoInst *args [16], *domain_ins;
12135 MonoInst *ad_ins, *jit_tls_ins;
12136 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12138 g_assert (!mono_threads_is_coop_enabled ());
12140 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12142 EMIT_NEW_PCONST (cfg, ins, NULL);
12143 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12145 ad_ins = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12146 jit_tls_ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
12148 if (ad_ins && jit_tls_ins) {
12149 NEW_BBLOCK (cfg, next_bb);
12150 NEW_BBLOCK (cfg, call_bb);
12152 if (cfg->compile_aot) {
12153 /* AOT code is only used in the root domain */
12154 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12156 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12158 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12159 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12161 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12162 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12164 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12165 MONO_START_BB (cfg, call_bb);
12168 /* AOT code is only used in the root domain */
12169 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12170 if (cfg->compile_aot) {
12174 * This is called on unattached threads, so it cannot go through the trampoline
12175 * infrastructure. Use an indirect call through a got slot initialized at load time
12178 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
12179 ins = mono_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
12181 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12183 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12186 MONO_START_BB (cfg, next_bb);
12191 case CEE_MONO_JIT_DETACH: {
12192 MonoInst *args [16];
12194 /* Restore the original domain */
12195 dreg = alloc_ireg (cfg);
12196 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12197 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12201 case CEE_MONO_CALLI_EXTRA_ARG: {
12203 MonoMethodSignature *fsig;
12207 * This is the same as CEE_CALLI, but passes an additional argument
12208 * to the called method in llvmonly mode.
12209 * This is only used by delegate invoke wrappers to call the
12210 * actual delegate method.
12212 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12215 token = read32 (ip + 2);
12223 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
12226 if (cfg->llvm_only)
12227 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12229 n = fsig->param_count + fsig->hasthis + 1;
12236 if (cfg->llvm_only) {
12238 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12239 * cconv. This is set by mono_init_delegate ().
12241 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12242 MonoInst *callee = addr;
12243 MonoInst *call, *localloc_ins;
12244 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12245 int low_bit_reg = alloc_preg (cfg);
12247 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12248 NEW_BBLOCK (cfg, end_bb);
12250 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12251 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12252 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12254 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12255 addr = emit_get_rgctx_sig (cfg, context_used,
12256 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12258 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12260 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12261 ins->dreg = alloc_preg (cfg);
12262 ins->inst_imm = 2 * SIZEOF_VOID_P;
12263 MONO_ADD_INS (cfg->cbb, ins);
12264 localloc_ins = ins;
12265 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12266 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12267 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12269 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12270 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12272 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12273 MONO_START_BB (cfg, is_gsharedvt_bb);
12274 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12275 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12276 ins->dreg = call->dreg;
12278 MONO_START_BB (cfg, end_bb);
12280 /* Caller uses a normal calling conv */
12282 MonoInst *callee = addr;
12283 MonoInst *call, *localloc_ins;
12284 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12285 int low_bit_reg = alloc_preg (cfg);
12287 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12288 NEW_BBLOCK (cfg, end_bb);
12290 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12291 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12292 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12294 /* Normal case: callee uses a normal cconv, no conversion is needed */
12295 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12296 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12297 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12298 MONO_START_BB (cfg, is_gsharedvt_bb);
12299 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12300 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12301 MONO_ADD_INS (cfg->cbb, addr);
12303 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12305 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12306 ins->dreg = alloc_preg (cfg);
12307 ins->inst_imm = 2 * SIZEOF_VOID_P;
12308 MONO_ADD_INS (cfg->cbb, ins);
12309 localloc_ins = ins;
12310 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12311 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12312 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12314 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12315 ins->dreg = call->dreg;
12316 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12318 MONO_START_BB (cfg, end_bb);
12321 /* Same as CEE_CALLI */
12322 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12324 * We pass the address to the gsharedvt trampoline in the rgctx reg
12326 MonoInst *callee = addr;
12328 addr = emit_get_rgctx_sig (cfg, context_used,
12329 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12330 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12332 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12336 if (!MONO_TYPE_IS_VOID (fsig->ret))
12337 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12339 CHECK_CFG_EXCEPTION;
12343 constrained_class = NULL;
12346 case CEE_MONO_LDDOMAIN:
12347 CHECK_STACK_OVF (1);
12348 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
12352 case CEE_MONO_GET_LAST_ERROR:
12354 CHECK_STACK_OVF (1);
12356 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
12357 ins->dreg = alloc_dreg (cfg, STACK_I4);
12358 ins->type = STACK_I4;
12359 MONO_ADD_INS (cfg->cbb, ins);
12365 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12371 case CEE_PREFIX1: {
12374 case CEE_ARGLIST: {
12375 /* somewhat similar to LDTOKEN */
12376 MonoInst *addr, *vtvar;
12377 CHECK_STACK_OVF (1);
12378 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12380 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12381 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12383 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12384 ins->type = STACK_VTYPE;
12385 ins->klass = mono_defaults.argumenthandle_class;
12395 MonoInst *cmp, *arg1, *arg2;
12403 * The following transforms:
12404 * CEE_CEQ into OP_CEQ
12405 * CEE_CGT into OP_CGT
12406 * CEE_CGT_UN into OP_CGT_UN
12407 * CEE_CLT into OP_CLT
12408 * CEE_CLT_UN into OP_CLT_UN
12410 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12412 MONO_INST_NEW (cfg, ins, cmp->opcode);
12413 cmp->sreg1 = arg1->dreg;
12414 cmp->sreg2 = arg2->dreg;
12415 type_from_op (cfg, cmp, arg1, arg2);
12417 add_widen_op (cfg, cmp, &arg1, &arg2);
12418 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12419 cmp->opcode = OP_LCOMPARE;
12420 else if (arg1->type == STACK_R4)
12421 cmp->opcode = OP_RCOMPARE;
12422 else if (arg1->type == STACK_R8)
12423 cmp->opcode = OP_FCOMPARE;
12425 cmp->opcode = OP_ICOMPARE;
12426 MONO_ADD_INS (cfg->cbb, cmp);
12427 ins->type = STACK_I4;
12428 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
12429 type_from_op (cfg, ins, arg1, arg2);
12431 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12433 * The backends expect the fceq opcodes to do the
12436 ins->sreg1 = cmp->sreg1;
12437 ins->sreg2 = cmp->sreg2;
12440 MONO_ADD_INS (cfg->cbb, ins);
12446 MonoInst *argconst;
12447 MonoMethod *cil_method;
12449 CHECK_STACK_OVF (1);
12451 n = read32 (ip + 2);
12452 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12455 mono_class_init (cmethod->klass);
12457 mono_save_token_info (cfg, image, n, cmethod);
12459 context_used = mini_method_check_context_used (cfg, cmethod);
12461 cil_method = cmethod;
12462 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12463 emit_method_access_failure (cfg, method, cil_method);
12465 if (mono_security_core_clr_enabled ())
12466 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12469 * Optimize the common case of ldftn+delegate creation
12471 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12472 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12473 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12474 MonoInst *target_ins, *handle_ins;
12475 MonoMethod *invoke;
12476 int invoke_context_used;
12478 invoke = mono_get_delegate_invoke (ctor_method->klass);
12479 if (!invoke || !mono_method_signature (invoke))
12482 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12484 target_ins = sp [-1];
12486 if (mono_security_core_clr_enabled ())
12487 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12489 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12490 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12491 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12492 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12493 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12497 /* FIXME: SGEN support */
12498 if (invoke_context_used == 0 || cfg->llvm_only) {
12500 if (cfg->verbose_level > 3)
12501 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));
12502 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12505 CHECK_CFG_EXCEPTION;
12515 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12516 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12520 inline_costs += 10 * num_calls++;
12523 case CEE_LDVIRTFTN: {
12524 MonoInst *args [2];
12528 n = read32 (ip + 2);
12529 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12532 mono_class_init (cmethod->klass);
12534 context_used = mini_method_check_context_used (cfg, cmethod);
12536 if (mono_security_core_clr_enabled ())
12537 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12540 * Optimize the common case of ldvirtftn+delegate creation
12542 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)) {
12543 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12544 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12545 MonoInst *target_ins, *handle_ins;
12546 MonoMethod *invoke;
12547 int invoke_context_used;
12548 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12550 invoke = mono_get_delegate_invoke (ctor_method->klass);
12551 if (!invoke || !mono_method_signature (invoke))
12554 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12556 target_ins = sp [-1];
12558 if (mono_security_core_clr_enabled ())
12559 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12561 /* FIXME: SGEN support */
12562 if (invoke_context_used == 0 || cfg->llvm_only) {
12564 if (cfg->verbose_level > 3)
12565 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));
12566 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12569 CHECK_CFG_EXCEPTION;
12582 args [1] = emit_get_rgctx_method (cfg, context_used,
12583 cmethod, MONO_RGCTX_INFO_METHOD);
12586 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12588 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12591 inline_costs += 10 * num_calls++;
12595 CHECK_STACK_OVF (1);
12597 n = read16 (ip + 2);
12599 EMIT_NEW_ARGLOAD (cfg, ins, n);
12604 CHECK_STACK_OVF (1);
12606 n = read16 (ip + 2);
12608 NEW_ARGLOADA (cfg, ins, n);
12609 MONO_ADD_INS (cfg->cbb, ins);
12617 n = read16 (ip + 2);
12619 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12621 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12625 CHECK_STACK_OVF (1);
12627 n = read16 (ip + 2);
12629 EMIT_NEW_LOCLOAD (cfg, ins, n);
12634 unsigned char *tmp_ip;
12635 CHECK_STACK_OVF (1);
12637 n = read16 (ip + 2);
12640 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12646 EMIT_NEW_LOCLOADA (cfg, ins, n);
12655 n = read16 (ip + 2);
12657 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12659 emit_stloc_ir (cfg, sp, header, n);
12663 case CEE_LOCALLOC: {
12665 MonoBasicBlock *non_zero_bb, *end_bb;
12666 int alloc_ptr = alloc_preg (cfg);
12668 if (sp != stack_start)
12670 if (cfg->method != method)
12672 * Inlining this into a loop in a parent could lead to
12673 * stack overflows which is different behavior than the
12674 * non-inlined case, thus disable inlining in this case.
12676 INLINE_FAILURE("localloc");
12678 NEW_BBLOCK (cfg, non_zero_bb);
12679 NEW_BBLOCK (cfg, end_bb);
12681 /* if size != zero */
12682 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
12683 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
12685 //size is zero, so result is NULL
12686 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
12687 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12689 MONO_START_BB (cfg, non_zero_bb);
12690 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12691 ins->dreg = alloc_ptr;
12692 ins->sreg1 = sp [0]->dreg;
12693 ins->type = STACK_PTR;
12694 MONO_ADD_INS (cfg->cbb, ins);
12696 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12698 ins->flags |= MONO_INST_INIT;
12700 MONO_START_BB (cfg, end_bb);
12701 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
12702 ins->type = STACK_PTR;
12708 case CEE_ENDFILTER: {
12709 MonoExceptionClause *clause, *nearest;
12714 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
12716 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12717 ins->sreg1 = (*sp)->dreg;
12718 MONO_ADD_INS (cfg->cbb, ins);
12719 start_new_bblock = 1;
12723 for (cc = 0; cc < header->num_clauses; ++cc) {
12724 clause = &header->clauses [cc];
12725 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12726 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12727 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12730 g_assert (nearest);
12731 if ((ip - header->code) != nearest->handler_offset)
12736 case CEE_UNALIGNED_:
12737 ins_flag |= MONO_INST_UNALIGNED;
12738 /* FIXME: record alignment? we can assume 1 for now */
12742 case CEE_VOLATILE_:
12743 ins_flag |= MONO_INST_VOLATILE;
12747 ins_flag |= MONO_INST_TAILCALL;
12748 cfg->flags |= MONO_CFG_HAS_TAIL;
12749 /* Can't inline tail calls at this time */
12750 inline_costs += 100000;
12757 token = read32 (ip + 2);
12758 klass = mini_get_class (method, token, generic_context);
12759 CHECK_TYPELOAD (klass);
12760 if (generic_class_is_reference_type (cfg, klass))
12761 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12763 mini_emit_initobj (cfg, *sp, NULL, klass);
12767 case CEE_CONSTRAINED_:
12769 token = read32 (ip + 2);
12770 constrained_class = mini_get_class (method, token, generic_context);
12771 CHECK_TYPELOAD (constrained_class);
12775 case CEE_INITBLK: {
12776 MonoInst *iargs [3];
12780 /* Skip optimized paths for volatile operations. */
12781 if ((ip [1] == CEE_CPBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
12782 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
12783 } else if ((ip [1] == CEE_INITBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5) && (sp [1]->opcode == OP_ICONST) && (sp [1]->inst_c0 == 0)) {
12784 /* emit_memset only works when val == 0 */
12785 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
12788 iargs [0] = sp [0];
12789 iargs [1] = sp [1];
12790 iargs [2] = sp [2];
12791 if (ip [1] == CEE_CPBLK) {
12793 * FIXME: It's unclear whether we should be emitting both the acquire
12794 * and release barriers for cpblk. It is technically both a load and
12795 * store operation, so it seems like that's the sensible thing to do.
12797 * FIXME: We emit full barriers on both sides of the operation for
12798 * simplicity. We should have a separate atomic memcpy method instead.
12800 MonoMethod *memcpy_method = get_memcpy_method ();
12802 if (ins_flag & MONO_INST_VOLATILE)
12803 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12805 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
12806 call->flags |= ins_flag;
12808 if (ins_flag & MONO_INST_VOLATILE)
12809 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12811 MonoMethod *memset_method = get_memset_method ();
12812 if (ins_flag & MONO_INST_VOLATILE) {
12813 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
12814 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
12816 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
12817 call->flags |= ins_flag;
12828 ins_flag |= MONO_INST_NOTYPECHECK;
12830 ins_flag |= MONO_INST_NORANGECHECK;
12831 /* we ignore the no-nullcheck for now since we
12832 * really do it explicitly only when doing callvirt->call
12836 case CEE_RETHROW: {
12838 int handler_offset = -1;
12840 for (i = 0; i < header->num_clauses; ++i) {
12841 MonoExceptionClause *clause = &header->clauses [i];
12842 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12843 handler_offset = clause->handler_offset;
12848 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12850 if (handler_offset == -1)
12853 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12854 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12855 ins->sreg1 = load->dreg;
12856 MONO_ADD_INS (cfg->cbb, ins);
12858 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12859 MONO_ADD_INS (cfg->cbb, ins);
12862 link_bblock (cfg, cfg->cbb, end_bblock);
12863 start_new_bblock = 1;
12871 CHECK_STACK_OVF (1);
12873 token = read32 (ip + 2);
12874 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12875 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12878 val = mono_type_size (type, &ialign);
12880 MonoClass *klass = mini_get_class (method, token, generic_context);
12881 CHECK_TYPELOAD (klass);
12883 val = mono_type_size (&klass->byval_arg, &ialign);
12885 if (mini_is_gsharedvt_klass (klass))
12886 GSHAREDVT_FAILURE (*ip);
12888 EMIT_NEW_ICONST (cfg, ins, val);
12893 case CEE_REFANYTYPE: {
12894 MonoInst *src_var, *src;
12896 GSHAREDVT_FAILURE (*ip);
12902 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12904 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12905 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12906 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12911 case CEE_READONLY_:
12924 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12934 g_warning ("opcode 0x%02x not handled", *ip);
12938 if (start_new_bblock != 1)
12941 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12942 if (cfg->cbb->next_bb) {
12943 /* This could already be set because of inlining, #693905 */
12944 MonoBasicBlock *bb = cfg->cbb;
12946 while (bb->next_bb)
12948 bb->next_bb = end_bblock;
12950 cfg->cbb->next_bb = end_bblock;
12953 if (cfg->method == method && cfg->domainvar) {
12955 MonoInst *get_domain;
12957 cfg->cbb = init_localsbb;
12959 get_domain = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12960 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12961 MONO_ADD_INS (cfg->cbb, store);
12964 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12965 if (cfg->compile_aot)
12966 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12967 mono_get_got_var (cfg);
12970 if (cfg->method == method && cfg->got_var)
12971 mono_emit_load_got_addr (cfg);
12973 if (init_localsbb) {
12974 cfg->cbb = init_localsbb;
12976 for (i = 0; i < header->num_locals; ++i) {
12977 emit_init_local (cfg, i, header->locals [i], init_locals);
12981 if (cfg->init_ref_vars && cfg->method == method) {
12982 /* Emit initialization for ref vars */
12983 // FIXME: Avoid duplication initialization for IL locals.
12984 for (i = 0; i < cfg->num_varinfo; ++i) {
12985 MonoInst *ins = cfg->varinfo [i];
12987 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12988 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12992 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
12993 cfg->cbb = init_localsbb;
12994 emit_push_lmf (cfg);
12997 cfg->cbb = init_localsbb;
12998 emit_instrumentation_call (cfg, mono_profiler_method_enter);
13001 MonoBasicBlock *bb;
13004 * Make seq points at backward branch targets interruptable.
13006 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13007 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13008 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13011 /* Add a sequence point for method entry/exit events */
13012 if (seq_points && cfg->gen_sdb_seq_points) {
13013 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13014 MONO_ADD_INS (init_localsbb, ins);
13015 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13016 MONO_ADD_INS (cfg->bb_exit, ins);
13020 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13021 * the code they refer to was dead (#11880).
13023 if (sym_seq_points) {
13024 for (i = 0; i < header->code_size; ++i) {
13025 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13028 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13029 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13036 if (cfg->method == method) {
13037 MonoBasicBlock *bb;
13038 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13039 if (bb == cfg->bb_init)
13042 bb->region = mono_find_block_region (cfg, bb->real_offset);
13044 mono_create_spvar_for_region (cfg, bb->region);
13045 if (cfg->verbose_level > 2)
13046 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13049 MonoBasicBlock *bb;
13050 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
13051 for (bb = start_bblock; bb != end_bblock; bb = bb->next_bb) {
13052 bb->real_offset = inline_offset;
13056 if (inline_costs < 0) {
13059 /* Method is too large */
13060 mname = mono_method_full_name (method, TRUE);
13061 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13065 if ((cfg->verbose_level > 2) && (cfg->method == method))
13066 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13071 g_assert (!mono_error_ok (&cfg->error));
13075 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13079 set_exception_type_from_invalid_il (cfg, method, ip);
13083 g_slist_free (class_inits);
13084 mono_basic_block_free (original_bb);
13085 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13086 if (cfg->exception_type)
13089 return inline_costs;
13093 store_membase_reg_to_store_membase_imm (int opcode)
13096 case OP_STORE_MEMBASE_REG:
13097 return OP_STORE_MEMBASE_IMM;
13098 case OP_STOREI1_MEMBASE_REG:
13099 return OP_STOREI1_MEMBASE_IMM;
13100 case OP_STOREI2_MEMBASE_REG:
13101 return OP_STOREI2_MEMBASE_IMM;
13102 case OP_STOREI4_MEMBASE_REG:
13103 return OP_STOREI4_MEMBASE_IMM;
13104 case OP_STOREI8_MEMBASE_REG:
13105 return OP_STOREI8_MEMBASE_IMM;
13107 g_assert_not_reached ();
13114 mono_op_to_op_imm (int opcode)
13118 return OP_IADD_IMM;
13120 return OP_ISUB_IMM;
13122 return OP_IDIV_IMM;
13124 return OP_IDIV_UN_IMM;
13126 return OP_IREM_IMM;
13128 return OP_IREM_UN_IMM;
13130 return OP_IMUL_IMM;
13132 return OP_IAND_IMM;
13136 return OP_IXOR_IMM;
13138 return OP_ISHL_IMM;
13140 return OP_ISHR_IMM;
13142 return OP_ISHR_UN_IMM;
13145 return OP_LADD_IMM;
13147 return OP_LSUB_IMM;
13149 return OP_LAND_IMM;
13153 return OP_LXOR_IMM;
13155 return OP_LSHL_IMM;
13157 return OP_LSHR_IMM;
13159 return OP_LSHR_UN_IMM;
13160 #if SIZEOF_REGISTER == 8
13162 return OP_LREM_IMM;
13166 return OP_COMPARE_IMM;
13168 return OP_ICOMPARE_IMM;
13170 return OP_LCOMPARE_IMM;
13172 case OP_STORE_MEMBASE_REG:
13173 return OP_STORE_MEMBASE_IMM;
13174 case OP_STOREI1_MEMBASE_REG:
13175 return OP_STOREI1_MEMBASE_IMM;
13176 case OP_STOREI2_MEMBASE_REG:
13177 return OP_STOREI2_MEMBASE_IMM;
13178 case OP_STOREI4_MEMBASE_REG:
13179 return OP_STOREI4_MEMBASE_IMM;
13181 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13183 return OP_X86_PUSH_IMM;
13184 case OP_X86_COMPARE_MEMBASE_REG:
13185 return OP_X86_COMPARE_MEMBASE_IMM;
13187 #if defined(TARGET_AMD64)
13188 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13189 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13191 case OP_VOIDCALL_REG:
13192 return OP_VOIDCALL;
13200 return OP_LOCALLOC_IMM;
13207 ldind_to_load_membase (int opcode)
13211 return OP_LOADI1_MEMBASE;
13213 return OP_LOADU1_MEMBASE;
13215 return OP_LOADI2_MEMBASE;
13217 return OP_LOADU2_MEMBASE;
13219 return OP_LOADI4_MEMBASE;
13221 return OP_LOADU4_MEMBASE;
13223 return OP_LOAD_MEMBASE;
13224 case CEE_LDIND_REF:
13225 return OP_LOAD_MEMBASE;
13227 return OP_LOADI8_MEMBASE;
13229 return OP_LOADR4_MEMBASE;
13231 return OP_LOADR8_MEMBASE;
13233 g_assert_not_reached ();
13240 stind_to_store_membase (int opcode)
13244 return OP_STOREI1_MEMBASE_REG;
13246 return OP_STOREI2_MEMBASE_REG;
13248 return OP_STOREI4_MEMBASE_REG;
13250 case CEE_STIND_REF:
13251 return OP_STORE_MEMBASE_REG;
13253 return OP_STOREI8_MEMBASE_REG;
13255 return OP_STORER4_MEMBASE_REG;
13257 return OP_STORER8_MEMBASE_REG;
13259 g_assert_not_reached ();
13266 mono_load_membase_to_load_mem (int opcode)
13268 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13269 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13271 case OP_LOAD_MEMBASE:
13272 return OP_LOAD_MEM;
13273 case OP_LOADU1_MEMBASE:
13274 return OP_LOADU1_MEM;
13275 case OP_LOADU2_MEMBASE:
13276 return OP_LOADU2_MEM;
13277 case OP_LOADI4_MEMBASE:
13278 return OP_LOADI4_MEM;
13279 case OP_LOADU4_MEMBASE:
13280 return OP_LOADU4_MEM;
13281 #if SIZEOF_REGISTER == 8
13282 case OP_LOADI8_MEMBASE:
13283 return OP_LOADI8_MEM;
13292 op_to_op_dest_membase (int store_opcode, int opcode)
13294 #if defined(TARGET_X86)
13295 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13300 return OP_X86_ADD_MEMBASE_REG;
13302 return OP_X86_SUB_MEMBASE_REG;
13304 return OP_X86_AND_MEMBASE_REG;
13306 return OP_X86_OR_MEMBASE_REG;
13308 return OP_X86_XOR_MEMBASE_REG;
13311 return OP_X86_ADD_MEMBASE_IMM;
13314 return OP_X86_SUB_MEMBASE_IMM;
13317 return OP_X86_AND_MEMBASE_IMM;
13320 return OP_X86_OR_MEMBASE_IMM;
13323 return OP_X86_XOR_MEMBASE_IMM;
13329 #if defined(TARGET_AMD64)
13330 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13335 return OP_X86_ADD_MEMBASE_REG;
13337 return OP_X86_SUB_MEMBASE_REG;
13339 return OP_X86_AND_MEMBASE_REG;
13341 return OP_X86_OR_MEMBASE_REG;
13343 return OP_X86_XOR_MEMBASE_REG;
13345 return OP_X86_ADD_MEMBASE_IMM;
13347 return OP_X86_SUB_MEMBASE_IMM;
13349 return OP_X86_AND_MEMBASE_IMM;
13351 return OP_X86_OR_MEMBASE_IMM;
13353 return OP_X86_XOR_MEMBASE_IMM;
13355 return OP_AMD64_ADD_MEMBASE_REG;
13357 return OP_AMD64_SUB_MEMBASE_REG;
13359 return OP_AMD64_AND_MEMBASE_REG;
13361 return OP_AMD64_OR_MEMBASE_REG;
13363 return OP_AMD64_XOR_MEMBASE_REG;
13366 return OP_AMD64_ADD_MEMBASE_IMM;
13369 return OP_AMD64_SUB_MEMBASE_IMM;
13372 return OP_AMD64_AND_MEMBASE_IMM;
13375 return OP_AMD64_OR_MEMBASE_IMM;
13378 return OP_AMD64_XOR_MEMBASE_IMM;
13388 op_to_op_store_membase (int store_opcode, int opcode)
13390 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13393 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13394 return OP_X86_SETEQ_MEMBASE;
13396 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13397 return OP_X86_SETNE_MEMBASE;
13405 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13408 /* FIXME: This has sign extension issues */
13410 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13411 return OP_X86_COMPARE_MEMBASE8_IMM;
13414 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13419 return OP_X86_PUSH_MEMBASE;
13420 case OP_COMPARE_IMM:
13421 case OP_ICOMPARE_IMM:
13422 return OP_X86_COMPARE_MEMBASE_IMM;
13425 return OP_X86_COMPARE_MEMBASE_REG;
13429 #ifdef TARGET_AMD64
13430 /* FIXME: This has sign extension issues */
13432 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13433 return OP_X86_COMPARE_MEMBASE8_IMM;
13438 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13439 return OP_X86_PUSH_MEMBASE;
13441 /* FIXME: This only works for 32 bit immediates
13442 case OP_COMPARE_IMM:
13443 case OP_LCOMPARE_IMM:
13444 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13445 return OP_AMD64_COMPARE_MEMBASE_IMM;
13447 case OP_ICOMPARE_IMM:
13448 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13449 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13453 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
13454 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13455 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13456 return OP_AMD64_COMPARE_MEMBASE_REG;
13459 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13460 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13469 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
13472 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13478 return OP_X86_COMPARE_REG_MEMBASE;
13480 return OP_X86_ADD_REG_MEMBASE;
13482 return OP_X86_SUB_REG_MEMBASE;
13484 return OP_X86_AND_REG_MEMBASE;
13486 return OP_X86_OR_REG_MEMBASE;
13488 return OP_X86_XOR_REG_MEMBASE;
13492 #ifdef TARGET_AMD64
13493 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
13496 return OP_AMD64_ICOMPARE_REG_MEMBASE;
13498 return OP_X86_ADD_REG_MEMBASE;
13500 return OP_X86_SUB_REG_MEMBASE;
13502 return OP_X86_AND_REG_MEMBASE;
13504 return OP_X86_OR_REG_MEMBASE;
13506 return OP_X86_XOR_REG_MEMBASE;
13508 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
13512 return OP_AMD64_COMPARE_REG_MEMBASE;
13514 return OP_AMD64_ADD_REG_MEMBASE;
13516 return OP_AMD64_SUB_REG_MEMBASE;
13518 return OP_AMD64_AND_REG_MEMBASE;
13520 return OP_AMD64_OR_REG_MEMBASE;
13522 return OP_AMD64_XOR_REG_MEMBASE;
13531 mono_op_to_op_imm_noemul (int opcode)
13534 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13540 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13547 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13552 return mono_op_to_op_imm (opcode);
13557 * mono_handle_global_vregs:
13559 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
13563 mono_handle_global_vregs (MonoCompile *cfg)
13565 gint32 *vreg_to_bb;
13566 MonoBasicBlock *bb;
13569 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13571 #ifdef MONO_ARCH_SIMD_INTRINSICS
13572 if (cfg->uses_simd_intrinsics)
13573 mono_simd_simplify_indirection (cfg);
13576 /* Find local vregs used in more than one bb */
13577 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13578 MonoInst *ins = bb->code;
13579 int block_num = bb->block_num;
13581 if (cfg->verbose_level > 2)
13582 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13585 for (; ins; ins = ins->next) {
13586 const char *spec = INS_INFO (ins->opcode);
13587 int regtype = 0, regindex;
13590 if (G_UNLIKELY (cfg->verbose_level > 2))
13591 mono_print_ins (ins);
13593 g_assert (ins->opcode >= MONO_CEE_LAST);
13595 for (regindex = 0; regindex < 4; regindex ++) {
13598 if (regindex == 0) {
13599 regtype = spec [MONO_INST_DEST];
13600 if (regtype == ' ')
13603 } else if (regindex == 1) {
13604 regtype = spec [MONO_INST_SRC1];
13605 if (regtype == ' ')
13608 } else if (regindex == 2) {
13609 regtype = spec [MONO_INST_SRC2];
13610 if (regtype == ' ')
13613 } else if (regindex == 3) {
13614 regtype = spec [MONO_INST_SRC3];
13615 if (regtype == ' ')
13620 #if SIZEOF_REGISTER == 4
13621 /* In the LLVM case, the long opcodes are not decomposed */
13622 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13624 * Since some instructions reference the original long vreg,
13625 * and some reference the two component vregs, it is quite hard
13626 * to determine when it needs to be global. So be conservative.
13628 if (!get_vreg_to_inst (cfg, vreg)) {
13629 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13631 if (cfg->verbose_level > 2)
13632 printf ("LONG VREG R%d made global.\n", vreg);
13636 * Make the component vregs volatile since the optimizations can
13637 * get confused otherwise.
13639 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
13640 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
13644 g_assert (vreg != -1);
13646 prev_bb = vreg_to_bb [vreg];
13647 if (prev_bb == 0) {
13648 /* 0 is a valid block num */
13649 vreg_to_bb [vreg] = block_num + 1;
13650 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13651 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13654 if (!get_vreg_to_inst (cfg, vreg)) {
13655 if (G_UNLIKELY (cfg->verbose_level > 2))
13656 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13660 if (vreg_is_ref (cfg, vreg))
13661 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13663 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13666 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13669 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13673 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13676 g_assert_not_reached ();
13680 /* Flag as having been used in more than one bb */
13681 vreg_to_bb [vreg] = -1;
13687 /* If a variable is used in only one bblock, convert it into a local vreg */
13688 for (i = 0; i < cfg->num_varinfo; i++) {
13689 MonoInst *var = cfg->varinfo [i];
13690 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13692 switch (var->type) {
13698 #if SIZEOF_REGISTER == 8
13701 #if !defined(TARGET_X86)
13702 /* Enabling this screws up the fp stack on x86 */
13705 if (mono_arch_is_soft_float ())
13709 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
13713 /* Arguments are implicitly global */
13714 /* Putting R4 vars into registers doesn't work currently */
13715 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13716 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) {
13718 * Make that the variable's liveness interval doesn't contain a call, since
13719 * that would cause the lvreg to be spilled, making the whole optimization
13722 /* This is too slow for JIT compilation */
13724 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13726 int def_index, call_index, ins_index;
13727 gboolean spilled = FALSE;
13732 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13733 const char *spec = INS_INFO (ins->opcode);
13735 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13736 def_index = ins_index;
13738 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13739 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13740 if (call_index > def_index) {
13746 if (MONO_IS_CALL (ins))
13747 call_index = ins_index;
13757 if (G_UNLIKELY (cfg->verbose_level > 2))
13758 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13759 var->flags |= MONO_INST_IS_DEAD;
13760 cfg->vreg_to_inst [var->dreg] = NULL;
13767 * Compress the varinfo and vars tables so the liveness computation is faster and
13768 * takes up less space.
13771 for (i = 0; i < cfg->num_varinfo; ++i) {
13772 MonoInst *var = cfg->varinfo [i];
13773 if (pos < i && cfg->locals_start == i)
13774 cfg->locals_start = pos;
13775 if (!(var->flags & MONO_INST_IS_DEAD)) {
13777 cfg->varinfo [pos] = cfg->varinfo [i];
13778 cfg->varinfo [pos]->inst_c0 = pos;
13779 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13780 cfg->vars [pos].idx = pos;
13781 #if SIZEOF_REGISTER == 4
13782 if (cfg->varinfo [pos]->type == STACK_I8) {
13783 /* Modify the two component vars too */
13786 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
13787 var1->inst_c0 = pos;
13788 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
13789 var1->inst_c0 = pos;
13796 cfg->num_varinfo = pos;
13797 if (cfg->locals_start > cfg->num_varinfo)
13798 cfg->locals_start = cfg->num_varinfo;
13802 * mono_allocate_gsharedvt_vars:
13804 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
13805 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
13808 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
13812 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13814 for (i = 0; i < cfg->num_varinfo; ++i) {
13815 MonoInst *ins = cfg->varinfo [i];
13818 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13819 if (i >= cfg->locals_start) {
13821 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13822 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13823 ins->opcode = OP_GSHAREDVT_LOCAL;
13824 ins->inst_imm = idx;
13827 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
13828 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13835 * mono_spill_global_vars:
13837 * Generate spill code for variables which are not allocated to registers,
13838 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13839 * code is generated which could be optimized by the local optimization passes.
13842 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13844 MonoBasicBlock *bb;
13846 int orig_next_vreg;
13847 guint32 *vreg_to_lvreg;
13849 guint32 i, lvregs_len, lvregs_size;
13850 gboolean dest_has_lvreg = FALSE;
13851 MonoStackType stacktypes [128];
13852 MonoInst **live_range_start, **live_range_end;
13853 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13855 *need_local_opts = FALSE;
13857 memset (spec2, 0, sizeof (spec2));
13859 /* FIXME: Move this function to mini.c */
13860 stacktypes ['i'] = STACK_PTR;
13861 stacktypes ['l'] = STACK_I8;
13862 stacktypes ['f'] = STACK_R8;
13863 #ifdef MONO_ARCH_SIMD_INTRINSICS
13864 stacktypes ['x'] = STACK_VTYPE;
13867 #if SIZEOF_REGISTER == 4
13868 /* Create MonoInsts for longs */
13869 for (i = 0; i < cfg->num_varinfo; i++) {
13870 MonoInst *ins = cfg->varinfo [i];
13872 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13873 switch (ins->type) {
13878 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13881 g_assert (ins->opcode == OP_REGOFFSET);
13883 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
13885 tree->opcode = OP_REGOFFSET;
13886 tree->inst_basereg = ins->inst_basereg;
13887 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13889 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
13891 tree->opcode = OP_REGOFFSET;
13892 tree->inst_basereg = ins->inst_basereg;
13893 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13903 if (cfg->compute_gc_maps) {
13904 /* registers need liveness info even for !non refs */
13905 for (i = 0; i < cfg->num_varinfo; i++) {
13906 MonoInst *ins = cfg->varinfo [i];
13908 if (ins->opcode == OP_REGVAR)
13909 ins->flags |= MONO_INST_GC_TRACK;
13913 /* FIXME: widening and truncation */
13916 * As an optimization, when a variable allocated to the stack is first loaded into
13917 * an lvreg, we will remember the lvreg and use it the next time instead of loading
13918 * the variable again.
13920 orig_next_vreg = cfg->next_vreg;
13921 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13922 lvregs_size = 1024;
13923 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * lvregs_size);
13927 * These arrays contain the first and last instructions accessing a given
13929 * Since we emit bblocks in the same order we process them here, and we
13930 * don't split live ranges, these will precisely describe the live range of
13931 * the variable, i.e. the instruction range where a valid value can be found
13932 * in the variables location.
13933 * The live range is computed using the liveness info computed by the liveness pass.
13934 * We can't use vmv->range, since that is an abstract live range, and we need
13935 * one which is instruction precise.
13936 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13938 /* FIXME: Only do this if debugging info is requested */
13939 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13940 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13941 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13942 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13944 /* Add spill loads/stores */
13945 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13948 if (cfg->verbose_level > 2)
13949 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13951 /* Clear vreg_to_lvreg array */
13952 for (i = 0; i < lvregs_len; i++)
13953 vreg_to_lvreg [lvregs [i]] = 0;
13957 MONO_BB_FOR_EACH_INS (bb, ins) {
13958 const char *spec = INS_INFO (ins->opcode);
13959 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13960 gboolean store, no_lvreg;
13961 int sregs [MONO_MAX_SRC_REGS];
13963 if (G_UNLIKELY (cfg->verbose_level > 2))
13964 mono_print_ins (ins);
13966 if (ins->opcode == OP_NOP)
13970 * We handle LDADDR here as well, since it can only be decomposed
13971 * when variable addresses are known.
13973 if (ins->opcode == OP_LDADDR) {
13974 MonoInst *var = (MonoInst *)ins->inst_p0;
13976 if (var->opcode == OP_VTARG_ADDR) {
13977 /* Happens on SPARC/S390 where vtypes are passed by reference */
13978 MonoInst *vtaddr = var->inst_left;
13979 if (vtaddr->opcode == OP_REGVAR) {
13980 ins->opcode = OP_MOVE;
13981 ins->sreg1 = vtaddr->dreg;
13983 else if (var->inst_left->opcode == OP_REGOFFSET) {
13984 ins->opcode = OP_LOAD_MEMBASE;
13985 ins->inst_basereg = vtaddr->inst_basereg;
13986 ins->inst_offset = vtaddr->inst_offset;
13989 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
13990 /* gsharedvt arg passed by ref */
13991 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13993 ins->opcode = OP_LOAD_MEMBASE;
13994 ins->inst_basereg = var->inst_basereg;
13995 ins->inst_offset = var->inst_offset;
13996 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
13997 MonoInst *load, *load2, *load3;
13998 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
13999 int reg1, reg2, reg3;
14000 MonoInst *info_var = cfg->gsharedvt_info_var;
14001 MonoInst *locals_var = cfg->gsharedvt_locals_var;
14005 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14008 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14010 g_assert (info_var);
14011 g_assert (locals_var);
14013 /* Mark the instruction used to compute the locals var as used */
14014 cfg->gsharedvt_locals_var_ins = NULL;
14016 /* Load the offset */
14017 if (info_var->opcode == OP_REGOFFSET) {
14018 reg1 = alloc_ireg (cfg);
14019 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14020 } else if (info_var->opcode == OP_REGVAR) {
14022 reg1 = info_var->dreg;
14024 g_assert_not_reached ();
14026 reg2 = alloc_ireg (cfg);
14027 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14028 /* Load the locals area address */
14029 reg3 = alloc_ireg (cfg);
14030 if (locals_var->opcode == OP_REGOFFSET) {
14031 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14032 } else if (locals_var->opcode == OP_REGVAR) {
14033 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14035 g_assert_not_reached ();
14037 /* Compute the address */
14038 ins->opcode = OP_PADD;
14042 mono_bblock_insert_before_ins (bb, ins, load3);
14043 mono_bblock_insert_before_ins (bb, load3, load2);
14045 mono_bblock_insert_before_ins (bb, load2, load);
14047 g_assert (var->opcode == OP_REGOFFSET);
14049 ins->opcode = OP_ADD_IMM;
14050 ins->sreg1 = var->inst_basereg;
14051 ins->inst_imm = var->inst_offset;
14054 *need_local_opts = TRUE;
14055 spec = INS_INFO (ins->opcode);
14058 if (ins->opcode < MONO_CEE_LAST) {
14059 mono_print_ins (ins);
14060 g_assert_not_reached ();
14064 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14068 if (MONO_IS_STORE_MEMBASE (ins)) {
14069 tmp_reg = ins->dreg;
14070 ins->dreg = ins->sreg2;
14071 ins->sreg2 = tmp_reg;
14074 spec2 [MONO_INST_DEST] = ' ';
14075 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14076 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14077 spec2 [MONO_INST_SRC3] = ' ';
14079 } else if (MONO_IS_STORE_MEMINDEX (ins))
14080 g_assert_not_reached ();
14085 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14086 printf ("\t %.3s %d", spec, ins->dreg);
14087 num_sregs = mono_inst_get_src_registers (ins, sregs);
14088 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14089 printf (" %d", sregs [srcindex]);
14096 regtype = spec [MONO_INST_DEST];
14097 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14100 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14101 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14102 MonoInst *store_ins;
14104 MonoInst *def_ins = ins;
14105 int dreg = ins->dreg; /* The original vreg */
14107 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14109 if (var->opcode == OP_REGVAR) {
14110 ins->dreg = var->dreg;
14111 } 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)) {
14113 * Instead of emitting a load+store, use a _membase opcode.
14115 g_assert (var->opcode == OP_REGOFFSET);
14116 if (ins->opcode == OP_MOVE) {
14120 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14121 ins->inst_basereg = var->inst_basereg;
14122 ins->inst_offset = var->inst_offset;
14125 spec = INS_INFO (ins->opcode);
14129 g_assert (var->opcode == OP_REGOFFSET);
14131 prev_dreg = ins->dreg;
14133 /* Invalidate any previous lvreg for this vreg */
14134 vreg_to_lvreg [ins->dreg] = 0;
14138 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14140 store_opcode = OP_STOREI8_MEMBASE_REG;
14143 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14145 #if SIZEOF_REGISTER != 8
14146 if (regtype == 'l') {
14147 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));
14148 mono_bblock_insert_after_ins (bb, ins, store_ins);
14149 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));
14150 mono_bblock_insert_after_ins (bb, ins, store_ins);
14151 def_ins = store_ins;
14156 g_assert (store_opcode != OP_STOREV_MEMBASE);
14158 /* Try to fuse the store into the instruction itself */
14159 /* FIXME: Add more instructions */
14160 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14161 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14162 ins->inst_imm = ins->inst_c0;
14163 ins->inst_destbasereg = var->inst_basereg;
14164 ins->inst_offset = var->inst_offset;
14165 spec = INS_INFO (ins->opcode);
14166 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14167 ins->opcode = store_opcode;
14168 ins->inst_destbasereg = var->inst_basereg;
14169 ins->inst_offset = var->inst_offset;
14173 tmp_reg = ins->dreg;
14174 ins->dreg = ins->sreg2;
14175 ins->sreg2 = tmp_reg;
14178 spec2 [MONO_INST_DEST] = ' ';
14179 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14180 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14181 spec2 [MONO_INST_SRC3] = ' ';
14183 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14184 // FIXME: The backends expect the base reg to be in inst_basereg
14185 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14187 ins->inst_basereg = var->inst_basereg;
14188 ins->inst_offset = var->inst_offset;
14189 spec = INS_INFO (ins->opcode);
14191 /* printf ("INS: "); mono_print_ins (ins); */
14192 /* Create a store instruction */
14193 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14195 /* Insert it after the instruction */
14196 mono_bblock_insert_after_ins (bb, ins, store_ins);
14198 def_ins = store_ins;
14201 * We can't assign ins->dreg to var->dreg here, since the
14202 * sregs could use it. So set a flag, and do it after
14205 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)))
14206 dest_has_lvreg = TRUE;
14211 if (def_ins && !live_range_start [dreg]) {
14212 live_range_start [dreg] = def_ins;
14213 live_range_start_bb [dreg] = bb;
14216 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14219 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14220 tmp->inst_c1 = dreg;
14221 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14228 num_sregs = mono_inst_get_src_registers (ins, sregs);
14229 for (srcindex = 0; srcindex < 3; ++srcindex) {
14230 regtype = spec [MONO_INST_SRC1 + srcindex];
14231 sreg = sregs [srcindex];
14233 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14234 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14235 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14236 MonoInst *use_ins = ins;
14237 MonoInst *load_ins;
14238 guint32 load_opcode;
14240 if (var->opcode == OP_REGVAR) {
14241 sregs [srcindex] = var->dreg;
14242 //mono_inst_set_src_registers (ins, sregs);
14243 live_range_end [sreg] = use_ins;
14244 live_range_end_bb [sreg] = bb;
14246 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14249 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14250 /* var->dreg is a hreg */
14251 tmp->inst_c1 = sreg;
14252 mono_bblock_insert_after_ins (bb, ins, tmp);
14258 g_assert (var->opcode == OP_REGOFFSET);
14260 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14262 g_assert (load_opcode != OP_LOADV_MEMBASE);
14264 if (vreg_to_lvreg [sreg]) {
14265 g_assert (vreg_to_lvreg [sreg] != -1);
14267 /* The variable is already loaded to an lvreg */
14268 if (G_UNLIKELY (cfg->verbose_level > 2))
14269 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14270 sregs [srcindex] = vreg_to_lvreg [sreg];
14271 //mono_inst_set_src_registers (ins, sregs);
14275 /* Try to fuse the load into the instruction */
14276 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14277 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14278 sregs [0] = var->inst_basereg;
14279 //mono_inst_set_src_registers (ins, sregs);
14280 ins->inst_offset = var->inst_offset;
14281 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14282 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14283 sregs [1] = var->inst_basereg;
14284 //mono_inst_set_src_registers (ins, sregs);
14285 ins->inst_offset = var->inst_offset;
14287 if (MONO_IS_REAL_MOVE (ins)) {
14288 ins->opcode = OP_NOP;
14291 //printf ("%d ", srcindex); mono_print_ins (ins);
14293 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14295 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) {
14296 if (var->dreg == prev_dreg) {
14298 * sreg refers to the value loaded by the load
14299 * emitted below, but we need to use ins->dreg
14300 * since it refers to the store emitted earlier.
14304 g_assert (sreg != -1);
14305 vreg_to_lvreg [var->dreg] = sreg;
14306 if (lvregs_len >= lvregs_size) {
14307 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14308 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14309 lvregs = new_lvregs;
14312 lvregs [lvregs_len ++] = var->dreg;
14316 sregs [srcindex] = sreg;
14317 //mono_inst_set_src_registers (ins, sregs);
14319 #if SIZEOF_REGISTER != 8
14320 if (regtype == 'l') {
14321 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14322 mono_bblock_insert_before_ins (bb, ins, load_ins);
14323 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14324 mono_bblock_insert_before_ins (bb, ins, load_ins);
14325 use_ins = load_ins;
14330 #if SIZEOF_REGISTER == 4
14331 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14333 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14334 mono_bblock_insert_before_ins (bb, ins, load_ins);
14335 use_ins = load_ins;
14339 if (var->dreg < orig_next_vreg) {
14340 live_range_end [var->dreg] = use_ins;
14341 live_range_end_bb [var->dreg] = bb;
14344 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14347 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14348 tmp->inst_c1 = var->dreg;
14349 mono_bblock_insert_after_ins (bb, ins, tmp);
14353 mono_inst_set_src_registers (ins, sregs);
14355 if (dest_has_lvreg) {
14356 g_assert (ins->dreg != -1);
14357 vreg_to_lvreg [prev_dreg] = ins->dreg;
14358 if (lvregs_len >= lvregs_size) {
14359 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14360 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14361 lvregs = new_lvregs;
14364 lvregs [lvregs_len ++] = prev_dreg;
14365 dest_has_lvreg = FALSE;
14369 tmp_reg = ins->dreg;
14370 ins->dreg = ins->sreg2;
14371 ins->sreg2 = tmp_reg;
14374 if (MONO_IS_CALL (ins)) {
14375 /* Clear vreg_to_lvreg array */
14376 for (i = 0; i < lvregs_len; i++)
14377 vreg_to_lvreg [lvregs [i]] = 0;
14379 } else if (ins->opcode == OP_NOP) {
14381 MONO_INST_NULLIFY_SREGS (ins);
14384 if (cfg->verbose_level > 2)
14385 mono_print_ins_index (1, ins);
14388 /* Extend the live range based on the liveness info */
14389 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14390 for (i = 0; i < cfg->num_varinfo; i ++) {
14391 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14393 if (vreg_is_volatile (cfg, vi->vreg))
14394 /* The liveness info is incomplete */
14397 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14398 /* Live from at least the first ins of this bb */
14399 live_range_start [vi->vreg] = bb->code;
14400 live_range_start_bb [vi->vreg] = bb;
14403 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14404 /* Live at least until the last ins of this bb */
14405 live_range_end [vi->vreg] = bb->last_ins;
14406 live_range_end_bb [vi->vreg] = bb;
14413 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14414 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14416 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14417 for (i = 0; i < cfg->num_varinfo; ++i) {
14418 int vreg = MONO_VARINFO (cfg, i)->vreg;
14421 if (live_range_start [vreg]) {
14422 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14424 ins->inst_c1 = vreg;
14425 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14427 if (live_range_end [vreg]) {
14428 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14430 ins->inst_c1 = vreg;
14431 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14432 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14434 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14439 if (cfg->gsharedvt_locals_var_ins) {
14440 /* Nullify if unused */
14441 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14442 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14445 g_free (live_range_start);
14446 g_free (live_range_end);
14447 g_free (live_range_start_bb);
14448 g_free (live_range_end_bb);
14454 * - use 'iadd' instead of 'int_add'
14455 * - handling ovf opcodes: decompose in method_to_ir.
14456 * - unify iregs/fregs
14457 * -> partly done, the missing parts are:
14458 * - a more complete unification would involve unifying the hregs as well, so
14459 * code wouldn't need if (fp) all over the place. but that would mean the hregs
14460 * would no longer map to the machine hregs, so the code generators would need to
14461 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14462 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14463 * fp/non-fp branches speeds it up by about 15%.
14464 * - use sext/zext opcodes instead of shifts
14466 * - get rid of TEMPLOADs if possible and use vregs instead
14467 * - clean up usage of OP_P/OP_ opcodes
14468 * - cleanup usage of DUMMY_USE
14469 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
14471 * - set the stack type and allocate a dreg in the EMIT_NEW macros
14472 * - get rid of all the <foo>2 stuff when the new JIT is ready.
14473 * - make sure handle_stack_args () is called before the branch is emitted
14474 * - when the new IR is done, get rid of all unused stuff
14475 * - COMPARE/BEQ as separate instructions or unify them ?
14476 * - keeping them separate allows specialized compare instructions like
14477 * compare_imm, compare_membase
14478 * - most back ends unify fp compare+branch, fp compare+ceq
14479 * - integrate mono_save_args into inline_method
14480 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14481 * - handle long shift opts on 32 bit platforms somehow: they require
14482 * 3 sregs (2 for arg1 and 1 for arg2)
14483 * - make byref a 'normal' type.
14484 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14485 * variable if needed.
14486 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14487 * like inline_method.
14488 * - remove inlining restrictions
14489 * - fix LNEG and enable cfold of INEG
14490 * - generalize x86 optimizations like ldelema as a peephole optimization
14491 * - add store_mem_imm for amd64
14492 * - optimize the loading of the interruption flag in the managed->native wrappers
14493 * - avoid special handling of OP_NOP in passes
14494 * - move code inserting instructions into one function/macro.
14495 * - try a coalescing phase after liveness analysis
14496 * - add float -> vreg conversion + local optimizations on !x86
14497 * - figure out how to handle decomposed branches during optimizations, ie.
14498 * compare+branch, op_jump_table+op_br etc.
14499 * - promote RuntimeXHandles to vregs
14500 * - vtype cleanups:
14501 * - add a NEW_VARLOADA_VREG macro
14502 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
14503 * accessing vtype fields.
14504 * - get rid of I8CONST on 64 bit platforms
14505 * - dealing with the increase in code size due to branches created during opcode
14507 * - use extended basic blocks
14508 * - all parts of the JIT
14509 * - handle_global_vregs () && local regalloc
14510 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14511 * - sources of increase in code size:
14514 * - isinst and castclass
14515 * - lvregs not allocated to global registers even if used multiple times
14516 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14518 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14519 * - add all micro optimizations from the old JIT
14520 * - put tree optimizations into the deadce pass
14521 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14522 * specific function.
14523 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14524 * fcompare + branchCC.
14525 * - create a helper function for allocating a stack slot, taking into account
14526 * MONO_CFG_HAS_SPILLUP.
14528 * - merge the ia64 switch changes.
14529 * - optimize mono_regstate2_alloc_int/float.
14530 * - fix the pessimistic handling of variables accessed in exception handler blocks.
14531 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14532 * parts of the tree could be separated by other instructions, killing the tree
14533 * arguments, or stores killing loads etc. Also, should we fold loads into other
14534 * instructions if the result of the load is used multiple times ?
14535 * - make the REM_IMM optimization in mini-x86.c arch-independent.
14536 * - LAST MERGE: 108395.
14537 * - when returning vtypes in registers, generate IR and append it to the end of the
14538 * last bb instead of doing it in the epilog.
14539 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14547 - When to decompose opcodes:
14548 - earlier: this makes some optimizations hard to implement, since the low level IR
14549 no longer contains the neccessary information. But it is easier to do.
14550 - later: harder to implement, enables more optimizations.
14551 - Branches inside bblocks:
14552 - created when decomposing complex opcodes.
14553 - branches to another bblock: harmless, but not tracked by the branch
14554 optimizations, so need to branch to a label at the start of the bblock.
14555 - branches to inside the same bblock: very problematic, trips up the local
14556 reg allocator. Can be fixed by spitting the current bblock, but that is a
14557 complex operation, since some local vregs can become global vregs etc.
14558 - Local/global vregs:
14559 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14560 local register allocator.
14561 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14562 structure, created by mono_create_var (). Assigned to hregs or the stack by
14563 the global register allocator.
14564 - When to do optimizations like alu->alu_imm:
14565 - earlier -> saves work later on since the IR will be smaller/simpler
14566 - later -> can work on more instructions
14567 - Handling of valuetypes:
14568 - When a vtype is pushed on the stack, a new temporary is created, an
14569 instruction computing its address (LDADDR) is emitted and pushed on
14570 the stack. Need to optimize cases when the vtype is used immediately as in
14571 argument passing, stloc etc.
14572 - Instead of the to_end stuff in the old JIT, simply call the function handling
14573 the values on the stack before emitting the last instruction of the bb.
14576 #else /* !DISABLE_JIT */
14579 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
14583 #endif /* !DISABLE_JIT */