2 * method-to-ir.c: Convert CIL to the JIT internal representation
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <mono/utils/mono-compiler.h>
29 #ifdef HAVE_SYS_TIME_H
37 #include <mono/utils/memcheck.h>
39 #include <mono/metadata/abi-details.h>
40 #include <mono/metadata/assembly.h>
41 #include <mono/metadata/attrdefs.h>
42 #include <mono/metadata/loader.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/class.h>
45 #include <mono/metadata/object.h>
46 #include <mono/metadata/exception.h>
47 #include <mono/metadata/opcodes.h>
48 #include <mono/metadata/mono-endian.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/marshal.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/mono-debug.h>
54 #include <mono/metadata/mono-debug-debugger.h>
55 #include <mono/metadata/gc-internals.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/profiler-private.h>
60 #include <mono/metadata/profiler.h>
61 #include <mono/metadata/monitor.h>
62 #include <mono/metadata/debug-mono-symfile.h>
63 #include <mono/utils/mono-memory-model.h>
64 #include <mono/utils/mono-error-internals.h>
65 #include <mono/metadata/mono-basic-block.h>
66 #include <mono/metadata/reflection-internals.h>
67 #include <mono/utils/mono-threads-coop.h>
73 #include "jit-icalls.h"
75 #include "debugger-agent.h"
76 #include "seq-points.h"
77 #include "aot-compiler.h"
78 #include "mini-llvm.h"
80 #define BRANCH_COST 10
81 #define INLINE_LENGTH_LIMIT 20
83 /* These have 'cfg' as an implicit argument */
84 #define INLINE_FAILURE(msg) do { \
85 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
86 inline_failure (cfg, msg); \
87 goto exception_exit; \
90 #define CHECK_CFG_EXCEPTION do {\
91 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
92 goto exception_exit; \
94 #define FIELD_ACCESS_FAILURE(method, field) do { \
95 field_access_failure ((cfg), (method), (field)); \
96 goto exception_exit; \
98 #define GENERIC_SHARING_FAILURE(opcode) do { \
100 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
101 goto exception_exit; \
104 #define GSHAREDVT_FAILURE(opcode) do { \
105 if (cfg->gsharedvt) { \
106 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
107 goto exception_exit; \
110 #define OUT_OF_MEMORY_FAILURE do { \
111 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
112 mono_error_set_out_of_memory (&cfg->error, ""); \
113 goto exception_exit; \
115 #define DISABLE_AOT(cfg) do { \
116 if ((cfg)->verbose_level >= 2) \
117 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
118 (cfg)->disable_aot = TRUE; \
120 #define LOAD_ERROR do { \
121 break_on_unverified (); \
122 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
123 goto exception_exit; \
126 #define TYPE_LOAD_ERROR(klass) do { \
127 cfg->exception_ptr = klass; \
131 #define CHECK_CFG_ERROR do {\
132 if (!mono_error_ok (&cfg->error)) { \
133 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
134 goto mono_error_exit; \
138 /* Determine whenever 'ins' represents a load of the 'this' argument */
139 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
141 static int ldind_to_load_membase (int opcode);
142 static int stind_to_store_membase (int opcode);
144 int mono_op_to_op_imm (int opcode);
145 int mono_op_to_op_imm_noemul (int opcode);
147 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
149 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
150 guchar *ip, guint real_offset, gboolean inline_always);
152 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
154 inline static MonoInst*
155 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg);
157 /* helper methods signatures */
158 static MonoMethodSignature *helper_sig_domain_get;
159 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
160 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
161 static MonoMethodSignature *helper_sig_jit_thread_attach;
162 static MonoMethodSignature *helper_sig_get_tls_tramp;
163 static MonoMethodSignature *helper_sig_set_tls_tramp;
165 /* type loading helpers */
166 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, "System.Runtime.CompilerServices", "RuntimeHelpers")
167 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
170 * Instruction metadata
178 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
179 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
185 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
190 /* keep in sync with the enum in mini.h */
193 #include "mini-ops.h"
198 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
199 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
201 * This should contain the index of the last sreg + 1. This is not the same
202 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
204 const gint8 ins_sreg_counts[] = {
205 #include "mini-ops.h"
211 mono_alloc_ireg (MonoCompile *cfg)
213 return alloc_ireg (cfg);
217 mono_alloc_lreg (MonoCompile *cfg)
219 return alloc_lreg (cfg);
223 mono_alloc_freg (MonoCompile *cfg)
225 return alloc_freg (cfg);
229 mono_alloc_preg (MonoCompile *cfg)
231 return alloc_preg (cfg);
235 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
237 return alloc_dreg (cfg, stack_type);
241 * mono_alloc_ireg_ref:
243 * Allocate an IREG, and mark it as holding a GC ref.
246 mono_alloc_ireg_ref (MonoCompile *cfg)
248 return alloc_ireg_ref (cfg);
252 * mono_alloc_ireg_mp:
254 * Allocate an IREG, and mark it as holding a managed pointer.
257 mono_alloc_ireg_mp (MonoCompile *cfg)
259 return alloc_ireg_mp (cfg);
263 * mono_alloc_ireg_copy:
265 * Allocate an IREG with the same GC type as VREG.
268 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
270 if (vreg_is_ref (cfg, vreg))
271 return alloc_ireg_ref (cfg);
272 else if (vreg_is_mp (cfg, vreg))
273 return alloc_ireg_mp (cfg);
275 return alloc_ireg (cfg);
279 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
284 type = mini_get_underlying_type (type);
286 switch (type->type) {
299 case MONO_TYPE_FNPTR:
301 case MONO_TYPE_CLASS:
302 case MONO_TYPE_STRING:
303 case MONO_TYPE_OBJECT:
304 case MONO_TYPE_SZARRAY:
305 case MONO_TYPE_ARRAY:
309 #if SIZEOF_REGISTER == 8
315 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
318 case MONO_TYPE_VALUETYPE:
319 if (type->data.klass->enumtype) {
320 type = mono_class_enum_basetype (type->data.klass);
323 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
326 case MONO_TYPE_TYPEDBYREF:
328 case MONO_TYPE_GENERICINST:
329 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
331 type = &type->data.generic_class->container_class->byval_arg;
335 g_assert (cfg->gshared);
336 if (mini_type_var_is_vt (type))
339 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
341 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
347 mono_print_bb (MonoBasicBlock *bb, const char *msg)
352 printf ("\n%s %d: [IN: ", msg, bb->block_num);
353 for (i = 0; i < bb->in_count; ++i)
354 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
356 for (i = 0; i < bb->out_count; ++i)
357 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
359 for (tree = bb->code; tree; tree = tree->next)
360 mono_print_ins_index (-1, tree);
364 mono_create_helper_signatures (void)
366 helper_sig_domain_get = mono_create_icall_signature ("ptr");
367 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
368 helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
369 helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
370 helper_sig_get_tls_tramp = mono_create_icall_signature ("ptr");
371 helper_sig_set_tls_tramp = mono_create_icall_signature ("void ptr");
374 static MONO_NEVER_INLINE void
375 break_on_unverified (void)
377 if (mini_get_debug_options ()->break_on_unverified)
381 static MONO_NEVER_INLINE void
382 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
384 char *method_fname = mono_method_full_name (method, TRUE);
385 char *field_fname = mono_field_full_name (field);
386 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
387 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
388 g_free (method_fname);
389 g_free (field_fname);
392 static MONO_NEVER_INLINE void
393 inline_failure (MonoCompile *cfg, const char *msg)
395 if (cfg->verbose_level >= 2)
396 printf ("inline failed: %s\n", msg);
397 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
400 static MONO_NEVER_INLINE void
401 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
403 if (cfg->verbose_level > 2) \
404 printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), line);
405 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
408 static MONO_NEVER_INLINE void
409 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
411 cfg->exception_message = g_strdup_printf ("gsharedvt failed for method %s.%s.%s/%d opcode %s %s:%d", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), file, line);
412 if (cfg->verbose_level >= 2)
413 printf ("%s\n", cfg->exception_message);
414 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
418 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
419 * foo<T> (int i) { ldarg.0; box T; }
421 #define UNVERIFIED do { \
422 if (cfg->gsharedvt) { \
423 if (cfg->verbose_level > 2) \
424 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
425 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
426 goto exception_exit; \
428 break_on_unverified (); \
432 #define GET_BBLOCK(cfg,tblock,ip) do { \
433 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
435 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
436 NEW_BBLOCK (cfg, (tblock)); \
437 (tblock)->cil_code = (ip); \
438 ADD_BBLOCK (cfg, (tblock)); \
442 #if defined(TARGET_X86) || defined(TARGET_AMD64)
443 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
444 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
445 (dest)->dreg = alloc_ireg_mp ((cfg)); \
446 (dest)->sreg1 = (sr1); \
447 (dest)->sreg2 = (sr2); \
448 (dest)->inst_imm = (imm); \
449 (dest)->backend.shift_amount = (shift); \
450 MONO_ADD_INS ((cfg)->cbb, (dest)); \
454 /* Emit conversions so both operands of a binary opcode are of the same type */
456 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
458 MonoInst *arg1 = *arg1_ref;
459 MonoInst *arg2 = *arg2_ref;
462 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
463 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
466 /* Mixing r4/r8 is allowed by the spec */
467 if (arg1->type == STACK_R4) {
468 int dreg = alloc_freg (cfg);
470 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
471 conv->type = STACK_R8;
475 if (arg2->type == STACK_R4) {
476 int dreg = alloc_freg (cfg);
478 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
479 conv->type = STACK_R8;
485 #if SIZEOF_REGISTER == 8
486 /* FIXME: Need to add many more cases */
487 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
490 int dr = alloc_preg (cfg);
491 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
492 (ins)->sreg2 = widen->dreg;
497 #define ADD_BINOP(op) do { \
498 MONO_INST_NEW (cfg, ins, (op)); \
500 ins->sreg1 = sp [0]->dreg; \
501 ins->sreg2 = sp [1]->dreg; \
502 type_from_op (cfg, ins, sp [0], sp [1]); \
504 /* Have to insert a widening op */ \
505 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
506 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
507 MONO_ADD_INS ((cfg)->cbb, (ins)); \
508 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
511 #define ADD_UNOP(op) do { \
512 MONO_INST_NEW (cfg, ins, (op)); \
514 ins->sreg1 = sp [0]->dreg; \
515 type_from_op (cfg, ins, sp [0], NULL); \
517 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
518 MONO_ADD_INS ((cfg)->cbb, (ins)); \
519 *sp++ = mono_decompose_opcode (cfg, ins); \
522 #define ADD_BINCOND(next_block) do { \
525 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
526 cmp->sreg1 = sp [0]->dreg; \
527 cmp->sreg2 = sp [1]->dreg; \
528 type_from_op (cfg, cmp, sp [0], sp [1]); \
530 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
531 type_from_op (cfg, ins, sp [0], sp [1]); \
532 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
533 GET_BBLOCK (cfg, tblock, target); \
534 link_bblock (cfg, cfg->cbb, tblock); \
535 ins->inst_true_bb = tblock; \
536 if ((next_block)) { \
537 link_bblock (cfg, cfg->cbb, (next_block)); \
538 ins->inst_false_bb = (next_block); \
539 start_new_bblock = 1; \
541 GET_BBLOCK (cfg, tblock, ip); \
542 link_bblock (cfg, cfg->cbb, tblock); \
543 ins->inst_false_bb = tblock; \
544 start_new_bblock = 2; \
546 if (sp != stack_start) { \
547 handle_stack_args (cfg, stack_start, sp - stack_start); \
548 CHECK_UNVERIFIABLE (cfg); \
550 MONO_ADD_INS (cfg->cbb, cmp); \
551 MONO_ADD_INS (cfg->cbb, ins); \
555 * link_bblock: Links two basic blocks
557 * links two basic blocks in the control flow graph, the 'from'
558 * argument is the starting block and the 'to' argument is the block
559 * the control flow ends to after 'from'.
562 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
564 MonoBasicBlock **newa;
568 if (from->cil_code) {
570 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
572 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
575 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
577 printf ("edge from entry to exit\n");
582 for (i = 0; i < from->out_count; ++i) {
583 if (to == from->out_bb [i]) {
589 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
590 for (i = 0; i < from->out_count; ++i) {
591 newa [i] = from->out_bb [i];
599 for (i = 0; i < to->in_count; ++i) {
600 if (from == to->in_bb [i]) {
606 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
607 for (i = 0; i < to->in_count; ++i) {
608 newa [i] = to->in_bb [i];
617 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
619 link_bblock (cfg, from, to);
623 * mono_find_block_region:
625 * We mark each basic block with a region ID. We use that to avoid BB
626 * optimizations when blocks are in different regions.
629 * A region token that encodes where this region is, and information
630 * about the clause owner for this block.
632 * The region encodes the try/catch/filter clause that owns this block
633 * as well as the type. -1 is a special value that represents a block
634 * that is in none of try/catch/filter.
637 mono_find_block_region (MonoCompile *cfg, int offset)
639 MonoMethodHeader *header = cfg->header;
640 MonoExceptionClause *clause;
643 for (i = 0; i < header->num_clauses; ++i) {
644 clause = &header->clauses [i];
645 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
646 (offset < (clause->handler_offset)))
647 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
649 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
650 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
651 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
652 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
653 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
655 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
658 for (i = 0; i < header->num_clauses; ++i) {
659 clause = &header->clauses [i];
661 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
662 return ((i + 1) << 8) | clause->flags;
669 ip_in_finally_clause (MonoCompile *cfg, int offset)
671 MonoMethodHeader *header = cfg->header;
672 MonoExceptionClause *clause;
675 for (i = 0; i < header->num_clauses; ++i) {
676 clause = &header->clauses [i];
677 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
680 if (MONO_OFFSET_IN_HANDLER (clause, offset))
687 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
689 MonoMethodHeader *header = cfg->header;
690 MonoExceptionClause *clause;
694 for (i = 0; i < header->num_clauses; ++i) {
695 clause = &header->clauses [i];
696 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
697 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
698 if (clause->flags == type)
699 res = g_list_append (res, clause);
706 mono_create_spvar_for_region (MonoCompile *cfg, int region)
710 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
714 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
715 /* prevent it from being register allocated */
716 var->flags |= MONO_INST_VOLATILE;
718 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
722 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
724 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
728 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
732 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
736 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
737 /* prevent it from being register allocated */
738 var->flags |= MONO_INST_VOLATILE;
740 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
746 * Returns the type used in the eval stack when @type is loaded.
747 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
750 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
754 type = mini_get_underlying_type (type);
755 inst->klass = klass = mono_class_from_mono_type (type);
757 inst->type = STACK_MP;
762 switch (type->type) {
764 inst->type = STACK_INV;
772 inst->type = STACK_I4;
777 case MONO_TYPE_FNPTR:
778 inst->type = STACK_PTR;
780 case MONO_TYPE_CLASS:
781 case MONO_TYPE_STRING:
782 case MONO_TYPE_OBJECT:
783 case MONO_TYPE_SZARRAY:
784 case MONO_TYPE_ARRAY:
785 inst->type = STACK_OBJ;
789 inst->type = STACK_I8;
792 inst->type = cfg->r4_stack_type;
795 inst->type = STACK_R8;
797 case MONO_TYPE_VALUETYPE:
798 if (type->data.klass->enumtype) {
799 type = mono_class_enum_basetype (type->data.klass);
803 inst->type = STACK_VTYPE;
806 case MONO_TYPE_TYPEDBYREF:
807 inst->klass = mono_defaults.typed_reference_class;
808 inst->type = STACK_VTYPE;
810 case MONO_TYPE_GENERICINST:
811 type = &type->data.generic_class->container_class->byval_arg;
815 g_assert (cfg->gshared);
816 if (mini_is_gsharedvt_type (type)) {
817 g_assert (cfg->gsharedvt);
818 inst->type = STACK_VTYPE;
820 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
824 g_error ("unknown type 0x%02x in eval stack type", type->type);
829 * The following tables are used to quickly validate the IL code in type_from_op ().
832 bin_num_table [STACK_MAX] [STACK_MAX] = {
833 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
834 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
835 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
836 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
837 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
838 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
839 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
840 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
841 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
846 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
849 /* reduce the size of this table */
851 bin_int_table [STACK_MAX] [STACK_MAX] = {
852 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
853 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
854 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
855 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
859 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
863 bin_comp_table [STACK_MAX] [STACK_MAX] = {
864 /* Inv i L p F & O vt r4 */
866 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
867 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
868 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
869 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
870 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
871 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
872 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
873 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
876 /* reduce the size of this table */
878 shift_table [STACK_MAX] [STACK_MAX] = {
879 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
880 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
881 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
882 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
883 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
884 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
885 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
886 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
890 * Tables to map from the non-specific opcode to the matching
891 * type-specific opcode.
893 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
895 binops_op_map [STACK_MAX] = {
896 0, OP_IADD-CEE_ADD, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD, 0, 0, OP_RADD-CEE_ADD
899 /* handles from CEE_NEG to CEE_CONV_U8 */
901 unops_op_map [STACK_MAX] = {
902 0, OP_INEG-CEE_NEG, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG, 0, 0, OP_RNEG-CEE_NEG
905 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
907 ovfops_op_map [STACK_MAX] = {
908 0, OP_ICONV_TO_U2-CEE_CONV_U2, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, 0, OP_RCONV_TO_U2-CEE_CONV_U2
911 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
913 ovf2ops_op_map [STACK_MAX] = {
914 0, OP_ICONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, 0, 0, OP_RCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
917 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
919 ovf3ops_op_map [STACK_MAX] = {
920 0, OP_ICONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, 0, 0, OP_RCONV_TO_OVF_I1-CEE_CONV_OVF_I1
923 /* handles from CEE_BEQ to CEE_BLT_UN */
925 beqops_op_map [STACK_MAX] = {
926 0, OP_IBEQ-CEE_BEQ, OP_LBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_FBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, 0, OP_FBEQ-CEE_BEQ
929 /* handles from CEE_CEQ to CEE_CLT_UN */
931 ceqops_op_map [STACK_MAX] = {
932 0, OP_ICEQ-OP_CEQ, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, 0, OP_RCEQ-OP_CEQ
936 * Sets ins->type (the type on the eval stack) according to the
937 * type of the opcode and the arguments to it.
938 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
940 * FIXME: this function sets ins->type unconditionally in some cases, but
941 * it should set it to invalid for some types (a conv.x on an object)
944 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
946 switch (ins->opcode) {
953 /* FIXME: check unverifiable args for STACK_MP */
954 ins->type = bin_num_table [src1->type] [src2->type];
955 ins->opcode += binops_op_map [ins->type];
962 ins->type = bin_int_table [src1->type] [src2->type];
963 ins->opcode += binops_op_map [ins->type];
968 ins->type = shift_table [src1->type] [src2->type];
969 ins->opcode += binops_op_map [ins->type];
974 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
975 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
976 ins->opcode = OP_LCOMPARE;
977 else if (src1->type == STACK_R4)
978 ins->opcode = OP_RCOMPARE;
979 else if (src1->type == STACK_R8)
980 ins->opcode = OP_FCOMPARE;
982 ins->opcode = OP_ICOMPARE;
984 case OP_ICOMPARE_IMM:
985 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
986 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
987 ins->opcode = OP_LCOMPARE_IMM;
999 ins->opcode += beqops_op_map [src1->type];
1002 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1003 ins->opcode += ceqops_op_map [src1->type];
1009 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1010 ins->opcode += ceqops_op_map [src1->type];
1014 ins->type = neg_table [src1->type];
1015 ins->opcode += unops_op_map [ins->type];
1018 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1019 ins->type = src1->type;
1021 ins->type = STACK_INV;
1022 ins->opcode += unops_op_map [ins->type];
1028 ins->type = STACK_I4;
1029 ins->opcode += unops_op_map [src1->type];
1032 ins->type = STACK_R8;
1033 switch (src1->type) {
1036 ins->opcode = OP_ICONV_TO_R_UN;
1039 ins->opcode = OP_LCONV_TO_R_UN;
1043 case CEE_CONV_OVF_I1:
1044 case CEE_CONV_OVF_U1:
1045 case CEE_CONV_OVF_I2:
1046 case CEE_CONV_OVF_U2:
1047 case CEE_CONV_OVF_I4:
1048 case CEE_CONV_OVF_U4:
1049 ins->type = STACK_I4;
1050 ins->opcode += ovf3ops_op_map [src1->type];
1052 case CEE_CONV_OVF_I_UN:
1053 case CEE_CONV_OVF_U_UN:
1054 ins->type = STACK_PTR;
1055 ins->opcode += ovf2ops_op_map [src1->type];
1057 case CEE_CONV_OVF_I1_UN:
1058 case CEE_CONV_OVF_I2_UN:
1059 case CEE_CONV_OVF_I4_UN:
1060 case CEE_CONV_OVF_U1_UN:
1061 case CEE_CONV_OVF_U2_UN:
1062 case CEE_CONV_OVF_U4_UN:
1063 ins->type = STACK_I4;
1064 ins->opcode += ovf2ops_op_map [src1->type];
1067 ins->type = STACK_PTR;
1068 switch (src1->type) {
1070 ins->opcode = OP_ICONV_TO_U;
1074 #if SIZEOF_VOID_P == 8
1075 ins->opcode = OP_LCONV_TO_U;
1077 ins->opcode = OP_MOVE;
1081 ins->opcode = OP_LCONV_TO_U;
1084 ins->opcode = OP_FCONV_TO_U;
1090 ins->type = STACK_I8;
1091 ins->opcode += unops_op_map [src1->type];
1093 case CEE_CONV_OVF_I8:
1094 case CEE_CONV_OVF_U8:
1095 ins->type = STACK_I8;
1096 ins->opcode += ovf3ops_op_map [src1->type];
1098 case CEE_CONV_OVF_U8_UN:
1099 case CEE_CONV_OVF_I8_UN:
1100 ins->type = STACK_I8;
1101 ins->opcode += ovf2ops_op_map [src1->type];
1104 ins->type = cfg->r4_stack_type;
1105 ins->opcode += unops_op_map [src1->type];
1108 ins->type = STACK_R8;
1109 ins->opcode += unops_op_map [src1->type];
1112 ins->type = STACK_R8;
1116 ins->type = STACK_I4;
1117 ins->opcode += ovfops_op_map [src1->type];
1120 case CEE_CONV_OVF_I:
1121 case CEE_CONV_OVF_U:
1122 ins->type = STACK_PTR;
1123 ins->opcode += ovfops_op_map [src1->type];
1126 case CEE_ADD_OVF_UN:
1128 case CEE_MUL_OVF_UN:
1130 case CEE_SUB_OVF_UN:
1131 ins->type = bin_num_table [src1->type] [src2->type];
1132 ins->opcode += ovfops_op_map [src1->type];
1133 if (ins->type == STACK_R8)
1134 ins->type = STACK_INV;
1136 case OP_LOAD_MEMBASE:
1137 ins->type = STACK_PTR;
1139 case OP_LOADI1_MEMBASE:
1140 case OP_LOADU1_MEMBASE:
1141 case OP_LOADI2_MEMBASE:
1142 case OP_LOADU2_MEMBASE:
1143 case OP_LOADI4_MEMBASE:
1144 case OP_LOADU4_MEMBASE:
1145 ins->type = STACK_PTR;
1147 case OP_LOADI8_MEMBASE:
1148 ins->type = STACK_I8;
1150 case OP_LOADR4_MEMBASE:
1151 ins->type = cfg->r4_stack_type;
1153 case OP_LOADR8_MEMBASE:
1154 ins->type = STACK_R8;
1157 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1161 if (ins->type == STACK_MP)
1162 ins->klass = mono_defaults.object_class;
1167 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1173 param_table [STACK_MAX] [STACK_MAX] = {
1178 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1183 switch (args->type) {
1193 for (i = 0; i < sig->param_count; ++i) {
1194 switch (args [i].type) {
1198 if (!sig->params [i]->byref)
1202 if (sig->params [i]->byref)
1204 switch (sig->params [i]->type) {
1205 case MONO_TYPE_CLASS:
1206 case MONO_TYPE_STRING:
1207 case MONO_TYPE_OBJECT:
1208 case MONO_TYPE_SZARRAY:
1209 case MONO_TYPE_ARRAY:
1216 if (sig->params [i]->byref)
1218 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1227 /*if (!param_table [args [i].type] [sig->params [i]->type])
1235 * When we need a pointer to the current domain many times in a method, we
1236 * call mono_domain_get() once and we store the result in a local variable.
1237 * This function returns the variable that represents the MonoDomain*.
1239 inline static MonoInst *
1240 mono_get_domainvar (MonoCompile *cfg)
1242 if (!cfg->domainvar)
1243 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1244 return cfg->domainvar;
1248 * The got_var contains the address of the Global Offset Table when AOT
1252 mono_get_got_var (MonoCompile *cfg)
1254 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1256 if (!cfg->got_var) {
1257 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1259 return cfg->got_var;
1263 mono_get_vtable_var (MonoCompile *cfg)
1265 g_assert (cfg->gshared);
1267 if (!cfg->rgctx_var) {
1268 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1269 /* force the var to be stack allocated */
1270 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1273 return cfg->rgctx_var;
1277 type_from_stack_type (MonoInst *ins) {
1278 switch (ins->type) {
1279 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1280 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1281 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1282 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1283 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1285 return &ins->klass->this_arg;
1286 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1287 case STACK_VTYPE: return &ins->klass->byval_arg;
1289 g_error ("stack type %d to monotype not handled\n", ins->type);
1294 static G_GNUC_UNUSED int
1295 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1297 t = mono_type_get_underlying_type (t);
1309 case MONO_TYPE_FNPTR:
1311 case MONO_TYPE_CLASS:
1312 case MONO_TYPE_STRING:
1313 case MONO_TYPE_OBJECT:
1314 case MONO_TYPE_SZARRAY:
1315 case MONO_TYPE_ARRAY:
1321 return cfg->r4_stack_type;
1324 case MONO_TYPE_VALUETYPE:
1325 case MONO_TYPE_TYPEDBYREF:
1327 case MONO_TYPE_GENERICINST:
1328 if (mono_type_generic_inst_is_valuetype (t))
1334 g_assert_not_reached ();
1341 array_access_to_klass (int opcode)
1345 return mono_defaults.byte_class;
1347 return mono_defaults.uint16_class;
1350 return mono_defaults.int_class;
1353 return mono_defaults.sbyte_class;
1356 return mono_defaults.int16_class;
1359 return mono_defaults.int32_class;
1361 return mono_defaults.uint32_class;
1364 return mono_defaults.int64_class;
1367 return mono_defaults.single_class;
1370 return mono_defaults.double_class;
1371 case CEE_LDELEM_REF:
1372 case CEE_STELEM_REF:
1373 return mono_defaults.object_class;
1375 g_assert_not_reached ();
1381 * We try to share variables when possible
1384 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1389 /* inlining can result in deeper stacks */
1390 if (slot >= cfg->header->max_stack)
1391 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1393 pos = ins->type - 1 + slot * STACK_MAX;
1395 switch (ins->type) {
1402 if ((vnum = cfg->intvars [pos]))
1403 return cfg->varinfo [vnum];
1404 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1405 cfg->intvars [pos] = res->inst_c0;
1408 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1414 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1417 * Don't use this if a generic_context is set, since that means AOT can't
1418 * look up the method using just the image+token.
1419 * table == 0 means this is a reference made from a wrapper.
1421 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1422 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1423 jump_info_token->image = image;
1424 jump_info_token->token = token;
1425 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1430 * This function is called to handle items that are left on the evaluation stack
1431 * at basic block boundaries. What happens is that we save the values to local variables
1432 * and we reload them later when first entering the target basic block (with the
1433 * handle_loaded_temps () function).
1434 * A single joint point will use the same variables (stored in the array bb->out_stack or
1435 * bb->in_stack, if the basic block is before or after the joint point).
1437 * This function needs to be called _before_ emitting the last instruction of
1438 * the bb (i.e. before emitting a branch).
1439 * If the stack merge fails at a join point, cfg->unverifiable is set.
1442 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1445 MonoBasicBlock *bb = cfg->cbb;
1446 MonoBasicBlock *outb;
1447 MonoInst *inst, **locals;
1452 if (cfg->verbose_level > 3)
1453 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1454 if (!bb->out_scount) {
1455 bb->out_scount = count;
1456 //printf ("bblock %d has out:", bb->block_num);
1458 for (i = 0; i < bb->out_count; ++i) {
1459 outb = bb->out_bb [i];
1460 /* exception handlers are linked, but they should not be considered for stack args */
1461 if (outb->flags & BB_EXCEPTION_HANDLER)
1463 //printf (" %d", outb->block_num);
1464 if (outb->in_stack) {
1466 bb->out_stack = outb->in_stack;
1472 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1473 for (i = 0; i < count; ++i) {
1475 * try to reuse temps already allocated for this purpouse, if they occupy the same
1476 * stack slot and if they are of the same type.
1477 * This won't cause conflicts since if 'local' is used to
1478 * store one of the values in the in_stack of a bblock, then
1479 * the same variable will be used for the same outgoing stack
1481 * This doesn't work when inlining methods, since the bblocks
1482 * in the inlined methods do not inherit their in_stack from
1483 * the bblock they are inlined to. See bug #58863 for an
1486 if (cfg->inlined_method)
1487 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1489 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1494 for (i = 0; i < bb->out_count; ++i) {
1495 outb = bb->out_bb [i];
1496 /* exception handlers are linked, but they should not be considered for stack args */
1497 if (outb->flags & BB_EXCEPTION_HANDLER)
1499 if (outb->in_scount) {
1500 if (outb->in_scount != bb->out_scount) {
1501 cfg->unverifiable = TRUE;
1504 continue; /* check they are the same locals */
1506 outb->in_scount = count;
1507 outb->in_stack = bb->out_stack;
1510 locals = bb->out_stack;
1512 for (i = 0; i < count; ++i) {
1513 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1514 inst->cil_code = sp [i]->cil_code;
1515 sp [i] = locals [i];
1516 if (cfg->verbose_level > 3)
1517 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1521 * It is possible that the out bblocks already have in_stack assigned, and
1522 * the in_stacks differ. In this case, we will store to all the different
1529 /* Find a bblock which has a different in_stack */
1531 while (bindex < bb->out_count) {
1532 outb = bb->out_bb [bindex];
1533 /* exception handlers are linked, but they should not be considered for stack args */
1534 if (outb->flags & BB_EXCEPTION_HANDLER) {
1538 if (outb->in_stack != locals) {
1539 for (i = 0; i < count; ++i) {
1540 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1541 inst->cil_code = sp [i]->cil_code;
1542 sp [i] = locals [i];
1543 if (cfg->verbose_level > 3)
1544 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1546 locals = outb->in_stack;
1556 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1560 if (cfg->compile_aot) {
1561 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1567 ji.type = patch_type;
1568 ji.data.target = data;
1569 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1570 mono_error_assert_ok (&error);
1572 EMIT_NEW_PCONST (cfg, ins, target);
1578 mini_emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1580 return emit_runtime_constant (cfg, patch_type, data);
1584 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1588 g_assert (val == 0);
1593 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1596 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1599 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1602 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1604 #if SIZEOF_REGISTER == 8
1606 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1612 val_reg = alloc_preg (cfg);
1614 if (SIZEOF_REGISTER == 8)
1615 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1617 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1620 /* This could be optimized further if neccesary */
1622 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1629 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1631 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1636 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1643 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1648 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1653 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1660 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1667 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1668 g_assert (size < 10000);
1671 /* This could be optimized further if neccesary */
1673 cur_reg = alloc_preg (cfg);
1674 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1675 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1682 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1684 cur_reg = alloc_preg (cfg);
1685 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1686 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1694 cur_reg = alloc_preg (cfg);
1695 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1696 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1702 cur_reg = alloc_preg (cfg);
1703 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1704 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1710 cur_reg = alloc_preg (cfg);
1711 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1712 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1720 mono_create_fast_tls_getter (MonoCompile *cfg, MonoTlsKey key)
1722 int tls_offset = mono_tls_get_tls_offset (key);
1724 if (cfg->compile_aot)
1727 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1729 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
1730 ins->dreg = mono_alloc_preg (cfg);
1731 ins->inst_offset = tls_offset;
1738 mono_create_fast_tls_setter (MonoCompile *cfg, MonoInst* value, MonoTlsKey key)
1740 int tls_offset = mono_tls_get_tls_offset (key);
1742 if (cfg->compile_aot)
1745 if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1747 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1748 ins->sreg1 = value->dreg;
1749 ins->inst_offset = tls_offset;
1757 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
1759 MonoInst *fast_tls = NULL;
1761 if (!mini_get_debug_options ()->use_fallback_tls)
1762 fast_tls = mono_create_fast_tls_getter (cfg, key);
1765 MONO_ADD_INS (cfg->cbb, fast_tls);
1769 if (cfg->compile_aot) {
1772 * tls getters are critical pieces of code and we don't want to resolve them
1773 * through the standard plt/tramp mechanism since we might expose ourselves
1774 * to crashes and infinite recursions.
1776 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GET_TLS_TRAMP, (void*)key);
1777 return mono_emit_calli (cfg, helper_sig_get_tls_tramp, NULL, addr, NULL, NULL);
1779 gpointer getter = mono_tls_get_tls_getter (key, FALSE);
1780 return mono_emit_jit_icall (cfg, getter, NULL);
1785 mono_create_tls_set (MonoCompile *cfg, MonoInst *value, MonoTlsKey key)
1787 MonoInst *fast_tls = NULL;
1789 if (!mini_get_debug_options ()->use_fallback_tls)
1790 fast_tls = mono_create_fast_tls_setter (cfg, value, key);
1793 MONO_ADD_INS (cfg->cbb, fast_tls);
1797 if (cfg->compile_aot) {
1799 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_SET_TLS_TRAMP, (void*)key);
1800 return mono_emit_calli (cfg, helper_sig_set_tls_tramp, &value, addr, NULL, NULL);
1802 gpointer setter = mono_tls_get_tls_setter (key, FALSE);
1803 return mono_emit_jit_icall (cfg, setter, &value);
1810 * Emit IR to push the current LMF onto the LMF stack.
1813 emit_push_lmf (MonoCompile *cfg)
1816 * Emit IR to push the LMF:
1817 * lmf_addr = <lmf_addr from tls>
1818 * lmf->lmf_addr = lmf_addr
1819 * lmf->prev_lmf = *lmf_addr
1822 MonoInst *ins, *lmf_ins;
1827 if (cfg->lmf_ir_mono_lmf) {
1828 MonoInst *lmf_vara_ins, *lmf_ins;
1829 /* Load current lmf */
1830 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF);
1832 EMIT_NEW_VARLOADA (cfg, lmf_vara_ins, cfg->lmf_var, NULL);
1833 /* Save previous_lmf */
1834 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_vara_ins->dreg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
1836 mono_create_tls_set (cfg, lmf_vara_ins, TLS_KEY_LMF);
1838 int lmf_reg, prev_lmf_reg;
1840 * Store lmf_addr in a variable, so it can be allocated to a global register.
1842 if (!cfg->lmf_addr_var)
1843 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1846 ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
1848 int jit_tls_dreg = ins->dreg;
1850 lmf_reg = alloc_preg (cfg);
1851 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
1853 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
1856 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
1858 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1859 lmf_reg = ins->dreg;
1861 prev_lmf_reg = alloc_preg (cfg);
1862 /* Save previous_lmf */
1863 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
1864 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
1866 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
1873 * Emit IR to pop the current LMF from the LMF stack.
1876 emit_pop_lmf (MonoCompile *cfg)
1878 int lmf_reg, lmf_addr_reg;
1884 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1885 lmf_reg = ins->dreg;
1887 if (cfg->lmf_ir_mono_lmf) {
1888 /* Load previous_lmf */
1889 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, alloc_preg (cfg), lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1891 mono_create_tls_set (cfg, ins, TLS_KEY_LMF);
1895 * Emit IR to pop the LMF:
1896 * *(lmf->lmf_addr) = lmf->prev_lmf
1898 /* This could be called before emit_push_lmf () */
1899 if (!cfg->lmf_addr_var)
1900 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1901 lmf_addr_reg = cfg->lmf_addr_var->dreg;
1903 prev_lmf_reg = alloc_preg (cfg);
1904 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1905 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
1910 emit_instrumentation_call (MonoCompile *cfg, void *func)
1912 MonoInst *iargs [1];
1915 * Avoid instrumenting inlined methods since it can
1916 * distort profiling results.
1918 if (cfg->method != cfg->current_method)
1921 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
1922 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
1923 mono_emit_jit_icall (cfg, func, iargs);
1928 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
1931 type = mini_get_underlying_type (type);
1932 switch (type->type) {
1933 case MONO_TYPE_VOID:
1934 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
1941 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1945 case MONO_TYPE_FNPTR:
1946 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1947 case MONO_TYPE_CLASS:
1948 case MONO_TYPE_STRING:
1949 case MONO_TYPE_OBJECT:
1950 case MONO_TYPE_SZARRAY:
1951 case MONO_TYPE_ARRAY:
1952 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1955 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
1958 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
1960 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1962 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1963 case MONO_TYPE_VALUETYPE:
1964 if (type->data.klass->enumtype) {
1965 type = mono_class_enum_basetype (type->data.klass);
1968 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1969 case MONO_TYPE_TYPEDBYREF:
1970 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1971 case MONO_TYPE_GENERICINST:
1972 type = &type->data.generic_class->container_class->byval_arg;
1975 case MONO_TYPE_MVAR:
1977 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1979 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1984 //XXX this ignores if t is byref
1985 #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)))))
1988 * target_type_is_incompatible:
1989 * @cfg: MonoCompile context
1991 * Check that the item @arg on the evaluation stack can be stored
1992 * in the target type (can be a local, or field, etc).
1993 * The cfg arg can be used to check if we need verification or just
1996 * Returns: non-0 value if arg can't be stored on a target.
1999 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2001 MonoType *simple_type;
2004 if (target->byref) {
2005 /* FIXME: check that the pointed to types match */
2006 if (arg->type == STACK_MP) {
2007 /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
2008 MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
2009 MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
2011 /* if the target is native int& or same type */
2012 if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
2015 /* Both are primitive type byrefs and the source points to a larger type that the destination */
2016 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
2017 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
2021 if (arg->type == STACK_PTR)
2026 simple_type = mini_get_underlying_type (target);
2027 switch (simple_type->type) {
2028 case MONO_TYPE_VOID:
2036 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2040 /* STACK_MP is needed when setting pinned locals */
2041 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2046 case MONO_TYPE_FNPTR:
2048 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2049 * in native int. (#688008).
2051 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2054 case MONO_TYPE_CLASS:
2055 case MONO_TYPE_STRING:
2056 case MONO_TYPE_OBJECT:
2057 case MONO_TYPE_SZARRAY:
2058 case MONO_TYPE_ARRAY:
2059 if (arg->type != STACK_OBJ)
2061 /* FIXME: check type compatibility */
2065 if (arg->type != STACK_I8)
2069 if (arg->type != cfg->r4_stack_type)
2073 if (arg->type != STACK_R8)
2076 case MONO_TYPE_VALUETYPE:
2077 if (arg->type != STACK_VTYPE)
2079 klass = mono_class_from_mono_type (simple_type);
2080 if (klass != arg->klass)
2083 case MONO_TYPE_TYPEDBYREF:
2084 if (arg->type != STACK_VTYPE)
2086 klass = mono_class_from_mono_type (simple_type);
2087 if (klass != arg->klass)
2090 case MONO_TYPE_GENERICINST:
2091 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2092 MonoClass *target_class;
2093 if (arg->type != STACK_VTYPE)
2095 klass = mono_class_from_mono_type (simple_type);
2096 target_class = mono_class_from_mono_type (target);
2097 /* The second cases is needed when doing partial sharing */
2098 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2102 if (arg->type != STACK_OBJ)
2104 /* FIXME: check type compatibility */
2108 case MONO_TYPE_MVAR:
2109 g_assert (cfg->gshared);
2110 if (mini_type_var_is_vt (simple_type)) {
2111 if (arg->type != STACK_VTYPE)
2114 if (arg->type != STACK_OBJ)
2119 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2125 * Prepare arguments for passing to a function call.
2126 * Return a non-zero value if the arguments can't be passed to the given
2128 * The type checks are not yet complete and some conversions may need
2129 * casts on 32 or 64 bit architectures.
2131 * FIXME: implement this using target_type_is_incompatible ()
2134 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2136 MonoType *simple_type;
2140 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2144 for (i = 0; i < sig->param_count; ++i) {
2145 if (sig->params [i]->byref) {
2146 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2150 simple_type = mini_get_underlying_type (sig->params [i]);
2152 switch (simple_type->type) {
2153 case MONO_TYPE_VOID:
2162 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2168 case MONO_TYPE_FNPTR:
2169 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2172 case MONO_TYPE_CLASS:
2173 case MONO_TYPE_STRING:
2174 case MONO_TYPE_OBJECT:
2175 case MONO_TYPE_SZARRAY:
2176 case MONO_TYPE_ARRAY:
2177 if (args [i]->type != STACK_OBJ)
2182 if (args [i]->type != STACK_I8)
2186 if (args [i]->type != cfg->r4_stack_type)
2190 if (args [i]->type != STACK_R8)
2193 case MONO_TYPE_VALUETYPE:
2194 if (simple_type->data.klass->enumtype) {
2195 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2198 if (args [i]->type != STACK_VTYPE)
2201 case MONO_TYPE_TYPEDBYREF:
2202 if (args [i]->type != STACK_VTYPE)
2205 case MONO_TYPE_GENERICINST:
2206 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2209 case MONO_TYPE_MVAR:
2211 if (args [i]->type != STACK_VTYPE)
2215 g_error ("unknown type 0x%02x in check_call_signature",
2223 callvirt_to_call (int opcode)
2226 case OP_CALL_MEMBASE:
2228 case OP_VOIDCALL_MEMBASE:
2230 case OP_FCALL_MEMBASE:
2232 case OP_RCALL_MEMBASE:
2234 case OP_VCALL_MEMBASE:
2236 case OP_LCALL_MEMBASE:
2239 g_assert_not_reached ();
2246 callvirt_to_call_reg (int opcode)
2249 case OP_CALL_MEMBASE:
2251 case OP_VOIDCALL_MEMBASE:
2252 return OP_VOIDCALL_REG;
2253 case OP_FCALL_MEMBASE:
2254 return OP_FCALL_REG;
2255 case OP_RCALL_MEMBASE:
2256 return OP_RCALL_REG;
2257 case OP_VCALL_MEMBASE:
2258 return OP_VCALL_REG;
2259 case OP_LCALL_MEMBASE:
2260 return OP_LCALL_REG;
2262 g_assert_not_reached ();
2268 /* Either METHOD or IMT_ARG needs to be set */
2270 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2274 if (COMPILE_LLVM (cfg)) {
2276 method_reg = alloc_preg (cfg);
2277 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2279 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2280 method_reg = ins->dreg;
2284 call->imt_arg_reg = method_reg;
2286 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2291 method_reg = alloc_preg (cfg);
2292 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2294 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2295 method_reg = ins->dreg;
2298 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2301 static MonoJumpInfo *
2302 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2304 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2308 ji->data.target = target;
2314 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2317 return mono_class_check_context_used (klass);
2323 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2326 return mono_method_check_context_used (method);
2332 * check_method_sharing:
2334 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2337 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2339 gboolean pass_vtable = FALSE;
2340 gboolean pass_mrgctx = FALSE;
2342 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2343 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2344 gboolean sharable = FALSE;
2346 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2350 * Pass vtable iff target method might
2351 * be shared, which means that sharing
2352 * is enabled for its class and its
2353 * context is sharable (and it's not a
2356 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2360 if (mini_method_get_context (cmethod) &&
2361 mini_method_get_context (cmethod)->method_inst) {
2362 g_assert (!pass_vtable);
2364 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2367 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2372 if (out_pass_vtable)
2373 *out_pass_vtable = pass_vtable;
2374 if (out_pass_mrgctx)
2375 *out_pass_mrgctx = pass_mrgctx;
2378 inline static MonoCallInst *
2379 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2380 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2384 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2392 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2394 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2396 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2399 call->signature = sig;
2400 call->rgctx_reg = rgctx;
2401 sig_ret = mini_get_underlying_type (sig->ret);
2403 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2406 if (mini_type_is_vtype (sig_ret)) {
2407 call->vret_var = cfg->vret_addr;
2408 //g_assert_not_reached ();
2410 } else if (mini_type_is_vtype (sig_ret)) {
2411 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2414 temp->backend.is_pinvoke = sig->pinvoke;
2417 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2418 * address of return value to increase optimization opportunities.
2419 * Before vtype decomposition, the dreg of the call ins itself represents the
2420 * fact the call modifies the return value. After decomposition, the call will
2421 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2422 * will be transformed into an LDADDR.
2424 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2425 loada->dreg = alloc_preg (cfg);
2426 loada->inst_p0 = temp;
2427 /* We reference the call too since call->dreg could change during optimization */
2428 loada->inst_p1 = call;
2429 MONO_ADD_INS (cfg->cbb, loada);
2431 call->inst.dreg = temp->dreg;
2433 call->vret_var = loada;
2434 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2435 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2437 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2438 if (COMPILE_SOFT_FLOAT (cfg)) {
2440 * If the call has a float argument, we would need to do an r8->r4 conversion using
2441 * an icall, but that cannot be done during the call sequence since it would clobber
2442 * the call registers + the stack. So we do it before emitting the call.
2444 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2446 MonoInst *in = call->args [i];
2448 if (i >= sig->hasthis)
2449 t = sig->params [i - sig->hasthis];
2451 t = &mono_defaults.int_class->byval_arg;
2452 t = mono_type_get_underlying_type (t);
2454 if (!t->byref && t->type == MONO_TYPE_R4) {
2455 MonoInst *iargs [1];
2459 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2461 /* The result will be in an int vreg */
2462 call->args [i] = conv;
2468 call->need_unbox_trampoline = unbox_trampoline;
2471 if (COMPILE_LLVM (cfg))
2472 mono_llvm_emit_call (cfg, call);
2474 mono_arch_emit_call (cfg, call);
2476 mono_arch_emit_call (cfg, call);
2479 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2480 cfg->flags |= MONO_CFG_HAS_CALLS;
2486 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2488 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2489 cfg->uses_rgctx_reg = TRUE;
2490 call->rgctx_reg = TRUE;
2492 call->rgctx_arg_reg = rgctx_reg;
2496 inline static MonoInst*
2497 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2502 gboolean check_sp = FALSE;
2504 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2505 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2507 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2512 rgctx_reg = mono_alloc_preg (cfg);
2513 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2517 if (!cfg->stack_inbalance_var)
2518 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2520 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2521 ins->dreg = cfg->stack_inbalance_var->dreg;
2522 MONO_ADD_INS (cfg->cbb, ins);
2525 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2527 call->inst.sreg1 = addr->dreg;
2530 emit_imt_argument (cfg, call, NULL, imt_arg);
2532 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2537 sp_reg = mono_alloc_preg (cfg);
2539 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2541 MONO_ADD_INS (cfg->cbb, ins);
2543 /* Restore the stack so we don't crash when throwing the exception */
2544 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2545 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2546 MONO_ADD_INS (cfg->cbb, ins);
2548 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2549 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2553 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2555 return (MonoInst*)call;
2559 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2562 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2565 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2566 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2568 #ifndef DISABLE_REMOTING
2569 gboolean might_be_remote = FALSE;
2571 gboolean virtual_ = this_ins != NULL;
2572 gboolean enable_for_aot = TRUE;
2575 MonoInst *call_target = NULL;
2577 gboolean need_unbox_trampoline;
2580 sig = mono_method_signature (method);
2582 if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2583 g_assert_not_reached ();
2586 rgctx_reg = mono_alloc_preg (cfg);
2587 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2590 if (method->string_ctor) {
2591 /* Create the real signature */
2592 /* FIXME: Cache these */
2593 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2594 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2599 context_used = mini_method_check_context_used (cfg, method);
2601 #ifndef DISABLE_REMOTING
2602 might_be_remote = this_ins && sig->hasthis &&
2603 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2604 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2606 if (might_be_remote && context_used) {
2609 g_assert (cfg->gshared);
2611 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2613 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2617 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2618 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2620 need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2622 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2624 #ifndef DISABLE_REMOTING
2625 if (might_be_remote)
2626 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2629 call->method = method;
2630 call->inst.flags |= MONO_INST_HAS_METHOD;
2631 call->inst.inst_left = this_ins;
2632 call->tail_call = tail;
2635 int vtable_reg, slot_reg, this_reg;
2638 this_reg = this_ins->dreg;
2640 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2641 MonoInst *dummy_use;
2643 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2645 /* Make a call to delegate->invoke_impl */
2646 call->inst.inst_basereg = this_reg;
2647 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2648 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2650 /* We must emit a dummy use here because the delegate trampoline will
2651 replace the 'this' argument with the delegate target making this activation
2652 no longer a root for the delegate.
2653 This is an issue for delegates that target collectible code such as dynamic
2654 methods of GC'able assemblies.
2656 For a test case look into #667921.
2658 FIXME: a dummy use is not the best way to do it as the local register allocator
2659 will put it on a caller save register and spil it around the call.
2660 Ideally, we would either put it on a callee save register or only do the store part.
2662 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2664 return (MonoInst*)call;
2667 if ((!cfg->compile_aot || enable_for_aot) &&
2668 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2669 (MONO_METHOD_IS_FINAL (method) &&
2670 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2671 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2673 * the method is not virtual, we just need to ensure this is not null
2674 * and then we can call the method directly.
2676 #ifndef DISABLE_REMOTING
2677 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2679 * The check above ensures method is not gshared, this is needed since
2680 * gshared methods can't have wrappers.
2682 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2686 if (!method->string_ctor)
2687 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2689 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2690 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2692 * the method is virtual, but we can statically dispatch since either
2693 * it's class or the method itself are sealed.
2694 * But first we need to ensure it's not a null reference.
2696 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2698 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2699 } else if (call_target) {
2700 vtable_reg = alloc_preg (cfg);
2701 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2703 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2704 call->inst.sreg1 = call_target->dreg;
2705 call->inst.flags &= !MONO_INST_HAS_METHOD;
2707 vtable_reg = alloc_preg (cfg);
2708 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2709 if (mono_class_is_interface (method->klass)) {
2710 guint32 imt_slot = mono_method_get_imt_slot (method);
2711 emit_imt_argument (cfg, call, call->method, imt_arg);
2712 slot_reg = vtable_reg;
2713 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2715 slot_reg = vtable_reg;
2716 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2717 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2719 g_assert (mono_method_signature (method)->generic_param_count);
2720 emit_imt_argument (cfg, call, call->method, imt_arg);
2724 call->inst.sreg1 = slot_reg;
2725 call->inst.inst_offset = offset;
2726 call->is_virtual = TRUE;
2730 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2733 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2735 return (MonoInst*)call;
2739 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2741 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2745 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2752 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2755 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2757 return (MonoInst*)call;
2761 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2763 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2767 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2771 * mono_emit_abs_call:
2773 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2775 inline static MonoInst*
2776 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2777 MonoMethodSignature *sig, MonoInst **args)
2779 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2783 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2786 if (cfg->abs_patches == NULL)
2787 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2788 g_hash_table_insert (cfg->abs_patches, ji, ji);
2789 ins = mono_emit_native_call (cfg, ji, sig, args);
2790 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2794 static MonoMethodSignature*
2795 sig_to_rgctx_sig (MonoMethodSignature *sig)
2797 // FIXME: memory allocation
2798 MonoMethodSignature *res;
2801 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2802 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2803 res->param_count = sig->param_count + 1;
2804 for (i = 0; i < sig->param_count; ++i)
2805 res->params [i] = sig->params [i];
2806 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2810 /* Make an indirect call to FSIG passing an additional argument */
2812 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
2814 MonoMethodSignature *csig;
2815 MonoInst *args_buf [16];
2817 int i, pindex, tmp_reg;
2819 /* Make a call with an rgctx/extra arg */
2820 if (fsig->param_count + 2 < 16)
2823 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
2826 args [pindex ++] = orig_args [0];
2827 for (i = 0; i < fsig->param_count; ++i)
2828 args [pindex ++] = orig_args [fsig->hasthis + i];
2829 tmp_reg = alloc_preg (cfg);
2830 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
2831 csig = sig_to_rgctx_sig (fsig);
2832 return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
2835 /* Emit an indirect call to the function descriptor ADDR */
2837 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
2839 int addr_reg, arg_reg;
2840 MonoInst *call_target;
2842 g_assert (cfg->llvm_only);
2845 * addr points to a <addr, arg> pair, load both of them, and
2846 * make a call to addr, passing arg as an extra arg.
2848 addr_reg = alloc_preg (cfg);
2849 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
2850 arg_reg = alloc_preg (cfg);
2851 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
2853 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
2857 direct_icalls_enabled (MonoCompile *cfg)
2859 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2861 if (cfg->compile_llvm && !cfg->llvm_only)
2864 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2870 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
2873 * Call the jit icall without a wrapper if possible.
2874 * The wrapper is needed for the following reasons:
2875 * - to handle exceptions thrown using mono_raise_exceptions () from the
2876 * icall function. The EH code needs the lmf frame pushed by the
2877 * wrapper to be able to unwind back to managed code.
2878 * - to be able to do stack walks for asynchronously suspended
2879 * threads when debugging.
2881 if (info->no_raise && direct_icalls_enabled (cfg)) {
2885 if (!info->wrapper_method) {
2886 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2887 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2889 mono_memory_barrier ();
2893 * Inline the wrapper method, which is basically a call to the C icall, and
2894 * an exception check.
2896 costs = inline_method (cfg, info->wrapper_method, NULL,
2897 args, NULL, il_offset, TRUE);
2898 g_assert (costs > 0);
2899 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2903 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2908 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2910 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2911 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2915 * Native code might return non register sized integers
2916 * without initializing the upper bits.
2918 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
2919 case OP_LOADI1_MEMBASE:
2920 widen_op = OP_ICONV_TO_I1;
2922 case OP_LOADU1_MEMBASE:
2923 widen_op = OP_ICONV_TO_U1;
2925 case OP_LOADI2_MEMBASE:
2926 widen_op = OP_ICONV_TO_I2;
2928 case OP_LOADU2_MEMBASE:
2929 widen_op = OP_ICONV_TO_U2;
2935 if (widen_op != -1) {
2936 int dreg = alloc_preg (cfg);
2939 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
2940 widen->type = ins->type;
2951 emit_method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
2953 MonoInst *args [16];
2955 args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (method), method, MONO_RGCTX_INFO_METHOD);
2956 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cil_method), cil_method, MONO_RGCTX_INFO_METHOD);
2958 mono_emit_jit_icall (cfg, mono_throw_method_access, args);
2962 get_memcpy_method (void)
2964 static MonoMethod *memcpy_method = NULL;
2965 if (!memcpy_method) {
2966 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2968 g_error ("Old corlib found. Install a new one");
2970 return memcpy_method;
2974 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
2976 MonoClassField *field;
2977 gpointer iter = NULL;
2979 while ((field = mono_class_get_fields (klass, &iter))) {
2982 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2984 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
2985 if (mini_type_is_reference (mono_field_get_type (field))) {
2986 g_assert ((foffset % SIZEOF_VOID_P) == 0);
2987 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
2989 MonoClass *field_class = mono_class_from_mono_type (field->type);
2990 if (field_class->has_references)
2991 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
2997 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
2999 int card_table_shift_bits;
3000 gpointer card_table_mask;
3002 MonoInst *dummy_use;
3003 int nursery_shift_bits;
3004 size_t nursery_size;
3006 if (!cfg->gen_write_barriers)
3009 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3011 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3013 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3016 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3017 wbarrier->sreg1 = ptr->dreg;
3018 wbarrier->sreg2 = value->dreg;
3019 MONO_ADD_INS (cfg->cbb, wbarrier);
3020 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3021 int offset_reg = alloc_preg (cfg);
3025 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3026 if (card_table_mask)
3027 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3029 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3030 * IMM's larger than 32bits.
3032 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3033 card_reg = ins->dreg;
3035 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3036 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3038 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3039 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3042 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3046 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3048 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3049 unsigned need_wb = 0;
3054 /*types with references can't have alignment smaller than sizeof(void*) */
3055 if (align < SIZEOF_VOID_P)
3058 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3059 if (size > 32 * SIZEOF_VOID_P)
3062 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3064 /* We don't unroll more than 5 stores to avoid code bloat. */
3065 if (size > 5 * SIZEOF_VOID_P) {
3066 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3067 size += (SIZEOF_VOID_P - 1);
3068 size &= ~(SIZEOF_VOID_P - 1);
3070 EMIT_NEW_ICONST (cfg, iargs [2], size);
3071 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3072 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3076 destreg = iargs [0]->dreg;
3077 srcreg = iargs [1]->dreg;
3080 dest_ptr_reg = alloc_preg (cfg);
3081 tmp_reg = alloc_preg (cfg);
3084 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3086 while (size >= SIZEOF_VOID_P) {
3087 MonoInst *load_inst;
3088 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3089 load_inst->dreg = tmp_reg;
3090 load_inst->inst_basereg = srcreg;
3091 load_inst->inst_offset = offset;
3092 MONO_ADD_INS (cfg->cbb, load_inst);
3094 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3097 emit_write_barrier (cfg, iargs [0], load_inst);
3099 offset += SIZEOF_VOID_P;
3100 size -= SIZEOF_VOID_P;
3103 /*tmp += sizeof (void*)*/
3104 if (size >= SIZEOF_VOID_P) {
3105 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3106 MONO_ADD_INS (cfg->cbb, iargs [0]);
3110 /* Those cannot be references since size < sizeof (void*) */
3112 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3113 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3119 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3120 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3126 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3127 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3136 * Emit code to copy a valuetype of type @klass whose address is stored in
3137 * @src->dreg to memory whose address is stored at @dest->dreg.
3140 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3142 MonoInst *iargs [4];
3145 MonoMethod *memcpy_method;
3146 MonoInst *size_ins = NULL;
3147 MonoInst *memcpy_ins = NULL;
3151 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3154 * This check breaks with spilled vars... need to handle it during verification anyway.
3155 * g_assert (klass && klass == src->klass && klass == dest->klass);
3158 if (mini_is_gsharedvt_klass (klass)) {
3160 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3161 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3165 n = mono_class_native_size (klass, &align);
3167 n = mono_class_value_size (klass, &align);
3169 /* if native is true there should be no references in the struct */
3170 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3171 /* Avoid barriers when storing to the stack */
3172 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3173 (dest->opcode == OP_LDADDR))) {
3179 context_used = mini_class_check_context_used (cfg, klass);
3181 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3182 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3184 } else if (context_used) {
3185 iargs [2] = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3187 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3188 if (!cfg->compile_aot)
3189 mono_class_compute_gc_descriptor (klass);
3193 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3195 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3200 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3201 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3202 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3207 iargs [2] = size_ins;
3209 EMIT_NEW_ICONST (cfg, iargs [2], n);
3211 memcpy_method = get_memcpy_method ();
3213 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3215 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3220 get_memset_method (void)
3222 static MonoMethod *memset_method = NULL;
3223 if (!memset_method) {
3224 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3226 g_error ("Old corlib found. Install a new one");
3228 return memset_method;
3232 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3234 MonoInst *iargs [3];
3237 MonoMethod *memset_method;
3238 MonoInst *size_ins = NULL;
3239 MonoInst *bzero_ins = NULL;
3240 static MonoMethod *bzero_method;
3242 /* FIXME: Optimize this for the case when dest is an LDADDR */
3243 mono_class_init (klass);
3244 if (mini_is_gsharedvt_klass (klass)) {
3245 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3246 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3248 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3249 g_assert (bzero_method);
3251 iargs [1] = size_ins;
3252 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3256 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3258 n = mono_class_value_size (klass, &align);
3260 if (n <= sizeof (gpointer) * 8) {
3261 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3264 memset_method = get_memset_method ();
3266 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3267 EMIT_NEW_ICONST (cfg, iargs [2], n);
3268 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3275 * Emit IR to return either the this pointer for instance method,
3276 * or the mrgctx for static methods.
3279 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3281 MonoInst *this_ins = NULL;
3283 g_assert (cfg->gshared);
3285 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3286 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3287 !method->klass->valuetype)
3288 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
3290 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3291 MonoInst *mrgctx_loc, *mrgctx_var;
3293 g_assert (!this_ins);
3294 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3296 mrgctx_loc = mono_get_vtable_var (cfg);
3297 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3300 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3301 MonoInst *vtable_loc, *vtable_var;
3303 g_assert (!this_ins);
3305 vtable_loc = mono_get_vtable_var (cfg);
3306 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3308 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3309 MonoInst *mrgctx_var = vtable_var;
3312 vtable_reg = alloc_preg (cfg);
3313 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3314 vtable_var->type = STACK_PTR;
3322 vtable_reg = alloc_preg (cfg);
3323 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3328 static MonoJumpInfoRgctxEntry *
3329 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3331 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3332 res->method = method;
3333 res->in_mrgctx = in_mrgctx;
3334 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3335 res->data->type = patch_type;
3336 res->data->data.target = patch_data;
3337 res->info_type = info_type;
3342 static inline MonoInst*
3343 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3345 MonoInst *args [16];
3348 // FIXME: No fastpath since the slot is not a compile time constant
3350 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3351 if (entry->in_mrgctx)
3352 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3354 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3358 * FIXME: This can be called during decompose, which is a problem since it creates
3360 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3362 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3364 MonoBasicBlock *is_null_bb, *end_bb;
3365 MonoInst *res, *ins, *call;
3368 slot = mini_get_rgctx_entry_slot (entry);
3370 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3371 index = MONO_RGCTX_SLOT_INDEX (slot);
3373 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3374 for (depth = 0; ; ++depth) {
3375 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3377 if (index < size - 1)
3382 NEW_BBLOCK (cfg, end_bb);
3383 NEW_BBLOCK (cfg, is_null_bb);
3386 rgctx_reg = rgctx->dreg;
3388 rgctx_reg = alloc_preg (cfg);
3390 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3391 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3392 NEW_BBLOCK (cfg, is_null_bb);
3394 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3395 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3398 for (i = 0; i < depth; ++i) {
3399 int array_reg = alloc_preg (cfg);
3401 /* load ptr to next array */
3402 if (mrgctx && i == 0)
3403 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3405 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3406 rgctx_reg = array_reg;
3407 /* is the ptr null? */
3408 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3409 /* if yes, jump to actual trampoline */
3410 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3414 val_reg = alloc_preg (cfg);
3415 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3416 /* is the slot null? */
3417 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3418 /* if yes, jump to actual trampoline */
3419 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3422 res_reg = alloc_preg (cfg);
3423 MONO_INST_NEW (cfg, ins, OP_MOVE);
3424 ins->dreg = res_reg;
3425 ins->sreg1 = val_reg;
3426 MONO_ADD_INS (cfg->cbb, ins);
3428 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3431 MONO_START_BB (cfg, is_null_bb);
3433 EMIT_NEW_ICONST (cfg, args [1], index);
3435 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3437 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3438 MONO_INST_NEW (cfg, ins, OP_MOVE);
3439 ins->dreg = res_reg;
3440 ins->sreg1 = call->dreg;
3441 MONO_ADD_INS (cfg->cbb, ins);
3442 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3444 MONO_START_BB (cfg, end_bb);
3453 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3456 static inline MonoInst*
3457 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3460 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3462 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3466 mini_emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3467 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3469 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);
3470 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3472 return emit_rgctx_fetch (cfg, rgctx, entry);
3476 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3477 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3479 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);
3480 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3482 return emit_rgctx_fetch (cfg, rgctx, entry);
3486 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3487 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3489 MonoJumpInfoGSharedVtCall *call_info;
3490 MonoJumpInfoRgctxEntry *entry;
3493 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3494 call_info->sig = sig;
3495 call_info->method = cmethod;
3497 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);
3498 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3500 return emit_rgctx_fetch (cfg, rgctx, entry);
3504 * emit_get_rgctx_virt_method:
3506 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3509 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3510 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3512 MonoJumpInfoVirtMethod *info;
3513 MonoJumpInfoRgctxEntry *entry;
3516 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3517 info->klass = klass;
3518 info->method = virt_method;
3520 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);
3521 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3523 return emit_rgctx_fetch (cfg, rgctx, entry);
3527 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3528 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3530 MonoJumpInfoRgctxEntry *entry;
3533 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);
3534 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3536 return emit_rgctx_fetch (cfg, rgctx, entry);
3540 * emit_get_rgctx_method:
3542 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3543 * normal constants, else emit a load from the rgctx.
3546 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3547 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3549 if (!context_used) {
3552 switch (rgctx_type) {
3553 case MONO_RGCTX_INFO_METHOD:
3554 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3556 case MONO_RGCTX_INFO_METHOD_RGCTX:
3557 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3560 g_assert_not_reached ();
3563 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);
3564 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3566 return emit_rgctx_fetch (cfg, rgctx, entry);
3571 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3572 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3574 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);
3575 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3577 return emit_rgctx_fetch (cfg, rgctx, entry);
3581 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3583 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3584 MonoRuntimeGenericContextInfoTemplate *template_;
3589 for (i = 0; i < info->num_entries; ++i) {
3590 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3592 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3596 if (info->num_entries == info->count_entries) {
3597 MonoRuntimeGenericContextInfoTemplate *new_entries;
3598 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3600 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3602 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3603 info->entries = new_entries;
3604 info->count_entries = new_count_entries;
3607 idx = info->num_entries;
3608 template_ = &info->entries [idx];
3609 template_->info_type = rgctx_type;
3610 template_->data = data;
3612 info->num_entries ++;
3618 * emit_get_gsharedvt_info:
3620 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3623 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3628 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3629 /* Load info->entries [idx] */
3630 dreg = alloc_preg (cfg);
3631 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3637 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3639 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3643 * On return the caller must check @klass for load errors.
3646 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3648 MonoInst *vtable_arg;
3651 context_used = mini_class_check_context_used (cfg, klass);
3654 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
3655 klass, MONO_RGCTX_INFO_VTABLE);
3657 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3661 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3664 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3668 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3669 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3671 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3672 ins->sreg1 = vtable_arg->dreg;
3673 MONO_ADD_INS (cfg->cbb, ins);
3676 MonoBasicBlock *inited_bb;
3677 MonoInst *args [16];
3679 inited_reg = alloc_ireg (cfg);
3681 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, inited_reg, vtable_arg->dreg, MONO_STRUCT_OFFSET (MonoVTable, initialized));
3683 NEW_BBLOCK (cfg, inited_bb);
3685 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3686 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3688 args [0] = vtable_arg;
3689 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3691 MONO_START_BB (cfg, inited_bb);
3696 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3700 if (cfg->gen_seq_points && cfg->method == method) {
3701 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3703 ins->flags |= MONO_INST_NONEMPTY_STACK;
3704 MONO_ADD_INS (cfg->cbb, ins);
3709 mini_save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3711 if (mini_get_debug_options ()->better_cast_details) {
3712 int vtable_reg = alloc_preg (cfg);
3713 int klass_reg = alloc_preg (cfg);
3714 MonoBasicBlock *is_null_bb = NULL;
3716 int to_klass_reg, context_used;
3719 NEW_BBLOCK (cfg, is_null_bb);
3721 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3722 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3725 tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3727 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3731 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3732 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3734 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3736 context_used = mini_class_check_context_used (cfg, klass);
3738 MonoInst *class_ins;
3740 class_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3741 to_klass_reg = class_ins->dreg;
3743 to_klass_reg = alloc_preg (cfg);
3744 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3746 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3749 MONO_START_BB (cfg, is_null_bb);
3754 mini_reset_cast_details (MonoCompile *cfg)
3756 /* Reset the variables holding the cast details */
3757 if (mini_get_debug_options ()->better_cast_details) {
3758 MonoInst *tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3759 /* It is enough to reset the from field */
3760 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3765 * On return the caller must check @array_class for load errors
3768 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3770 int vtable_reg = alloc_preg (cfg);
3773 context_used = mini_class_check_context_used (cfg, array_class);
3775 mini_save_cast_details (cfg, array_class, obj->dreg, FALSE);
3777 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3779 if (cfg->opt & MONO_OPT_SHARED) {
3780 int class_reg = alloc_preg (cfg);
3783 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3784 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3785 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3786 } else if (context_used) {
3787 MonoInst *vtable_ins;
3789 vtable_ins = mini_emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3790 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3792 if (cfg->compile_aot) {
3796 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3798 vt_reg = alloc_preg (cfg);
3799 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3800 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3803 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3805 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3809 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3811 mini_reset_cast_details (cfg);
3815 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
3816 * generic code is generated.
3819 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3821 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3824 MonoInst *rgctx, *addr;
3826 /* FIXME: What if the class is shared? We might not
3827 have to get the address of the method from the
3829 addr = emit_get_rgctx_method (cfg, context_used, method,
3830 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3831 if (cfg->llvm_only) {
3832 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
3833 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3835 rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3837 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3840 gboolean pass_vtable, pass_mrgctx;
3841 MonoInst *rgctx_arg = NULL;
3843 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3844 g_assert (!pass_mrgctx);
3847 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3850 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3853 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3858 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3862 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3863 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3864 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3865 int rank_reg = alloc_dreg (cfg ,STACK_I4);
3867 obj_reg = sp [0]->dreg;
3868 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3869 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3871 /* FIXME: generics */
3872 g_assert (klass->rank == 0);
3875 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3876 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3878 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3879 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3882 MonoInst *element_class;
3884 /* This assertion is from the unboxcast insn */
3885 g_assert (klass->rank == 0);
3887 element_class = mini_emit_get_rgctx_klass (cfg, context_used,
3888 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3890 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3891 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3893 mini_save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3894 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3895 mini_reset_cast_details (cfg);
3898 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3899 MONO_ADD_INS (cfg->cbb, add);
3900 add->type = STACK_MP;
3907 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3909 MonoInst *addr, *klass_inst, *is_ref, *args[16];
3910 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3914 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3920 args [1] = klass_inst;
3923 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3925 NEW_BBLOCK (cfg, is_ref_bb);
3926 NEW_BBLOCK (cfg, is_nullable_bb);
3927 NEW_BBLOCK (cfg, end_bb);
3928 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3929 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3930 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3932 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3933 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3935 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3936 addr_reg = alloc_dreg (cfg, STACK_MP);
3940 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3941 MONO_ADD_INS (cfg->cbb, addr);
3943 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3946 MONO_START_BB (cfg, is_ref_bb);
3948 /* Save the ref to a temporary */
3949 dreg = alloc_ireg (cfg);
3950 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3951 addr->dreg = addr_reg;
3952 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3953 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3956 MONO_START_BB (cfg, is_nullable_bb);
3959 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3960 MonoInst *unbox_call;
3961 MonoMethodSignature *unbox_sig;
3963 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3964 unbox_sig->ret = &klass->byval_arg;
3965 unbox_sig->param_count = 1;
3966 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3969 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
3971 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3973 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3974 addr->dreg = addr_reg;
3977 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3980 MONO_START_BB (cfg, end_bb);
3983 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3989 * Returns NULL and set the cfg exception on error.
3992 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3994 MonoInst *iargs [2];
3999 MonoRgctxInfoType rgctx_info;
4000 MonoInst *iargs [2];
4001 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4003 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4005 if (cfg->opt & MONO_OPT_SHARED)
4006 rgctx_info = MONO_RGCTX_INFO_KLASS;
4008 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4009 data = mini_emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4011 if (cfg->opt & MONO_OPT_SHARED) {
4012 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4014 alloc_ftn = ves_icall_object_new;
4017 alloc_ftn = ves_icall_object_new_specific;
4020 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4021 if (known_instance_size) {
4022 int size = mono_class_instance_size (klass);
4023 if (size < sizeof (MonoObject))
4024 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4026 EMIT_NEW_ICONST (cfg, iargs [1], size);
4028 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4031 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4034 if (cfg->opt & MONO_OPT_SHARED) {
4035 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4036 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4038 alloc_ftn = ves_icall_object_new;
4039 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
4040 /* This happens often in argument checking code, eg. throw new FooException... */
4041 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4042 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4043 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4045 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4046 MonoMethod *managed_alloc = NULL;
4050 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4051 cfg->exception_ptr = klass;
4055 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4057 if (managed_alloc) {
4058 int size = mono_class_instance_size (klass);
4059 if (size < sizeof (MonoObject))
4060 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4062 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4063 EMIT_NEW_ICONST (cfg, iargs [1], size);
4064 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4066 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4068 guint32 lw = vtable->klass->instance_size;
4069 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4070 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4071 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4074 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4078 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4082 * Returns NULL and set the cfg exception on error.
4085 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4087 MonoInst *alloc, *ins;
4089 if (mono_class_is_nullable (klass)) {
4090 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4093 if (cfg->llvm_only && cfg->gsharedvt) {
4094 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4095 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4096 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4098 /* FIXME: What if the class is shared? We might not
4099 have to get the method address from the RGCTX. */
4100 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4101 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4102 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
4104 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4107 gboolean pass_vtable, pass_mrgctx;
4108 MonoInst *rgctx_arg = NULL;
4110 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4111 g_assert (!pass_mrgctx);
4114 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4117 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4120 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4124 if (mini_is_gsharedvt_klass (klass)) {
4125 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4126 MonoInst *res, *is_ref, *src_var, *addr;
4129 dreg = alloc_ireg (cfg);
4131 NEW_BBLOCK (cfg, is_ref_bb);
4132 NEW_BBLOCK (cfg, is_nullable_bb);
4133 NEW_BBLOCK (cfg, end_bb);
4134 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4135 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4136 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4138 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4139 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4142 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4145 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4146 ins->opcode = OP_STOREV_MEMBASE;
4148 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4149 res->type = STACK_OBJ;
4151 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4154 MONO_START_BB (cfg, is_ref_bb);
4156 /* val is a vtype, so has to load the value manually */
4157 src_var = get_vreg_to_inst (cfg, val->dreg);
4159 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4160 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4161 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4162 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4165 MONO_START_BB (cfg, is_nullable_bb);
4168 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4169 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4171 MonoMethodSignature *box_sig;
4174 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4175 * construct that method at JIT time, so have to do things by hand.
4177 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4178 box_sig->ret = &mono_defaults.object_class->byval_arg;
4179 box_sig->param_count = 1;
4180 box_sig->params [0] = &klass->byval_arg;
4183 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4185 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4186 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4187 res->type = STACK_OBJ;
4191 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4193 MONO_START_BB (cfg, end_bb);
4197 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4201 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4206 static GHashTable* direct_icall_type_hash;
4209 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4211 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4212 if (!direct_icalls_enabled (cfg))
4216 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4217 * Whitelist a few icalls for now.
4219 if (!direct_icall_type_hash) {
4220 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4222 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4223 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4224 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4225 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4226 mono_memory_barrier ();
4227 direct_icall_type_hash = h;
4230 if (cmethod->klass == mono_defaults.math_class)
4232 /* No locking needed */
4233 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4239 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
4241 if (cmethod->klass == mono_defaults.systemtype_class) {
4242 if (!strcmp (cmethod->name, "GetType"))
4248 static G_GNUC_UNUSED MonoInst*
4249 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4251 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4252 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4255 switch (enum_type->type) {
4258 #if SIZEOF_REGISTER == 8
4270 MonoInst *load, *and_, *cmp, *ceq;
4271 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4272 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4273 int dest_reg = alloc_ireg (cfg);
4275 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4276 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4277 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4278 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4280 ceq->type = STACK_I4;
4283 load = mono_decompose_opcode (cfg, load);
4284 and_ = mono_decompose_opcode (cfg, and_);
4285 cmp = mono_decompose_opcode (cfg, cmp);
4286 ceq = mono_decompose_opcode (cfg, ceq);
4294 * Returns NULL and set the cfg exception on error.
4296 static G_GNUC_UNUSED MonoInst*
4297 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
4301 gpointer trampoline;
4302 MonoInst *obj, *method_ins, *tramp_ins;
4306 if (virtual_ && !cfg->llvm_only) {
4307 MonoMethod *invoke = mono_get_delegate_invoke (klass);
4310 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
4314 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
4318 /* Inline the contents of mono_delegate_ctor */
4320 /* Set target field */
4321 /* Optimize away setting of NULL target */
4322 if (!MONO_INS_IS_PCONST_NULL (target)) {
4323 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4324 if (cfg->gen_write_barriers) {
4325 dreg = alloc_preg (cfg);
4326 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4327 emit_write_barrier (cfg, ptr, target);
4331 /* Set method field */
4332 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4333 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4336 * To avoid looking up the compiled code belonging to the target method
4337 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4338 * store it, and we fill it after the method has been compiled.
4340 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4341 MonoInst *code_slot_ins;
4344 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4346 domain = mono_domain_get ();
4347 mono_domain_lock (domain);
4348 if (!domain_jit_info (domain)->method_code_hash)
4349 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4350 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4352 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
4353 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4355 mono_domain_unlock (domain);
4357 code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4359 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
4362 if (cfg->llvm_only) {
4363 MonoInst *args [16];
4368 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4369 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
4372 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
4378 if (cfg->compile_aot) {
4379 MonoDelegateClassMethodPair *del_tramp;
4381 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4382 del_tramp->klass = klass;
4383 del_tramp->method = context_used ? NULL : method;
4384 del_tramp->is_virtual = virtual_;
4385 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4388 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4390 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4391 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4394 /* Set invoke_impl field */
4396 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4398 dreg = alloc_preg (cfg);
4399 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4400 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4402 dreg = alloc_preg (cfg);
4403 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4404 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4407 dreg = alloc_preg (cfg);
4408 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
4409 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4411 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4417 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4419 MonoJitICallInfo *info;
4421 /* Need to register the icall so it gets an icall wrapper */
4422 info = mono_get_array_new_va_icall (rank);
4424 cfg->flags |= MONO_CFG_HAS_VARARGS;
4426 /* mono_array_new_va () needs a vararg calling convention */
4427 cfg->exception_message = g_strdup ("array-new");
4428 cfg->disable_llvm = TRUE;
4430 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4431 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4435 * handle_constrained_gsharedvt_call:
4437 * Handle constrained calls where the receiver is a gsharedvt type.
4438 * Return the instruction representing the call. Set the cfg exception on failure.
4441 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4442 gboolean *ref_emit_widen)
4444 MonoInst *ins = NULL;
4445 gboolean emit_widen = *ref_emit_widen;
4448 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4449 * 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
4450 * pack the arguments into an array, and do the rest of the work in in an icall.
4452 if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4453 (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)) &&
4454 (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]))))) {
4455 MonoInst *args [16];
4458 * This case handles calls to
4459 * - object:ToString()/Equals()/GetHashCode(),
4460 * - System.IComparable<T>:CompareTo()
4461 * - System.IEquatable<T>:Equals ()
4462 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4466 if (mono_method_check_context_used (cmethod))
4467 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4469 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4470 args [2] = mini_emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4472 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4473 if (fsig->hasthis && fsig->param_count) {
4474 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4475 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4476 ins->dreg = alloc_preg (cfg);
4477 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4478 MONO_ADD_INS (cfg->cbb, ins);
4481 if (mini_is_gsharedvt_type (fsig->params [0])) {
4482 int addr_reg, deref_arg_reg;
4484 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4485 deref_arg_reg = alloc_preg (cfg);
4486 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
4487 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
4489 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4490 addr_reg = ins->dreg;
4491 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4493 EMIT_NEW_ICONST (cfg, args [3], 0);
4494 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4497 EMIT_NEW_ICONST (cfg, args [3], 0);
4498 EMIT_NEW_ICONST (cfg, args [4], 0);
4500 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4503 if (mini_is_gsharedvt_type (fsig->ret)) {
4504 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
4505 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret))) {
4509 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4510 MONO_ADD_INS (cfg->cbb, add);
4512 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4513 MONO_ADD_INS (cfg->cbb, ins);
4514 /* ins represents the call result */
4517 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4520 *ref_emit_widen = emit_widen;
4529 mono_emit_load_got_addr (MonoCompile *cfg)
4531 MonoInst *getaddr, *dummy_use;
4533 if (!cfg->got_var || cfg->got_var_allocated)
4536 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4537 getaddr->cil_code = cfg->header->code;
4538 getaddr->dreg = cfg->got_var->dreg;
4540 /* Add it to the start of the first bblock */
4541 if (cfg->bb_entry->code) {
4542 getaddr->next = cfg->bb_entry->code;
4543 cfg->bb_entry->code = getaddr;
4546 MONO_ADD_INS (cfg->bb_entry, getaddr);
4548 cfg->got_var_allocated = TRUE;
4551 * Add a dummy use to keep the got_var alive, since real uses might
4552 * only be generated by the back ends.
4553 * Add it to end_bblock, so the variable's lifetime covers the whole
4555 * It would be better to make the usage of the got var explicit in all
4556 * cases when the backend needs it (i.e. calls, throw etc.), so this
4557 * wouldn't be needed.
4559 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
4560 MONO_ADD_INS (cfg->bb_exit, dummy_use);
4563 static int inline_limit;
4564 static gboolean inline_limit_inited;
4567 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
4569 MonoMethodHeaderSummary header;
4571 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4572 MonoMethodSignature *sig = mono_method_signature (method);
4576 if (cfg->disable_inline)
4581 if (cfg->inline_depth > 10)
4584 if (!mono_method_get_header_summary (method, &header))
4587 /*runtime, icall and pinvoke are checked by summary call*/
4588 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
4589 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
4590 (mono_class_is_marshalbyref (method->klass)) ||
4594 /* also consider num_locals? */
4595 /* Do the size check early to avoid creating vtables */
4596 if (!inline_limit_inited) {
4597 if (g_getenv ("MONO_INLINELIMIT"))
4598 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
4600 inline_limit = INLINE_LENGTH_LIMIT;
4601 inline_limit_inited = TRUE;
4603 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
4607 * if we can initialize the class of the method right away, we do,
4608 * otherwise we don't allow inlining if the class needs initialization,
4609 * since it would mean inserting a call to mono_runtime_class_init()
4610 * inside the inlined code
4612 if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
4615 if (!(cfg->opt & MONO_OPT_SHARED)) {
4616 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
4617 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
4618 if (method->klass->has_cctor) {
4619 vtable = mono_class_vtable (cfg->domain, method->klass);
4622 if (!cfg->compile_aot) {
4624 if (!mono_runtime_class_init_full (vtable, &error)) {
4625 mono_error_cleanup (&error);
4630 } else if (mono_class_is_before_field_init (method->klass)) {
4631 if (cfg->run_cctors && method->klass->has_cctor) {
4632 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
4633 if (!method->klass->runtime_info)
4634 /* No vtable created yet */
4636 vtable = mono_class_vtable (cfg->domain, method->klass);
4639 /* This makes so that inline cannot trigger */
4640 /* .cctors: too many apps depend on them */
4641 /* running with a specific order... */
4642 if (! vtable->initialized)
4645 if (!mono_runtime_class_init_full (vtable, &error)) {
4646 mono_error_cleanup (&error);
4650 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
4651 if (!method->klass->runtime_info)
4652 /* No vtable created yet */
4654 vtable = mono_class_vtable (cfg->domain, method->klass);
4657 if (!vtable->initialized)
4662 * If we're compiling for shared code
4663 * the cctor will need to be run at aot method load time, for example,
4664 * or at the end of the compilation of the inlining method.
4666 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
4670 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4671 if (mono_arch_is_soft_float ()) {
4673 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
4675 for (i = 0; i < sig->param_count; ++i)
4676 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
4681 if (g_list_find (cfg->dont_inline, method))
4688 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
4690 if (!cfg->compile_aot) {
4692 if (vtable->initialized)
4696 if (mono_class_is_before_field_init (klass)) {
4697 if (cfg->method == method)
4701 if (!mono_class_needs_cctor_run (klass, method))
4704 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
4705 /* The initialization is already done before the method is called */
4712 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
4716 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
4719 if (mini_is_gsharedvt_variable_klass (klass)) {
4722 mono_class_init (klass);
4723 size = mono_class_array_element_size (klass);
4726 mult_reg = alloc_preg (cfg);
4727 array_reg = arr->dreg;
4728 index_reg = index->dreg;
4730 #if SIZEOF_REGISTER == 8
4731 /* The array reg is 64 bits but the index reg is only 32 */
4732 if (COMPILE_LLVM (cfg)) {
4734 index2_reg = index_reg;
4736 index2_reg = alloc_preg (cfg);
4737 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
4740 if (index->type == STACK_I8) {
4741 index2_reg = alloc_preg (cfg);
4742 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
4744 index2_reg = index_reg;
4749 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
4751 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4752 if (size == 1 || size == 2 || size == 4 || size == 8) {
4753 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
4755 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
4756 ins->klass = mono_class_get_element_class (klass);
4757 ins->type = STACK_MP;
4763 add_reg = alloc_ireg_mp (cfg);
4766 MonoInst *rgctx_ins;
4769 g_assert (cfg->gshared);
4770 context_used = mini_class_check_context_used (cfg, klass);
4771 g_assert (context_used);
4772 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
4773 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
4775 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
4777 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
4778 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4779 ins->klass = mono_class_get_element_class (klass);
4780 ins->type = STACK_MP;
4781 MONO_ADD_INS (cfg->cbb, ins);
4787 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
4789 int bounds_reg = alloc_preg (cfg);
4790 int add_reg = alloc_ireg_mp (cfg);
4791 int mult_reg = alloc_preg (cfg);
4792 int mult2_reg = alloc_preg (cfg);
4793 int low1_reg = alloc_preg (cfg);
4794 int low2_reg = alloc_preg (cfg);
4795 int high1_reg = alloc_preg (cfg);
4796 int high2_reg = alloc_preg (cfg);
4797 int realidx1_reg = alloc_preg (cfg);
4798 int realidx2_reg = alloc_preg (cfg);
4799 int sum_reg = alloc_preg (cfg);
4800 int index1, index2, tmpreg;
4804 mono_class_init (klass);
4805 size = mono_class_array_element_size (klass);
4807 index1 = index_ins1->dreg;
4808 index2 = index_ins2->dreg;
4810 #if SIZEOF_REGISTER == 8
4811 /* The array reg is 64 bits but the index reg is only 32 */
4812 if (COMPILE_LLVM (cfg)) {
4815 tmpreg = alloc_preg (cfg);
4816 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
4818 tmpreg = alloc_preg (cfg);
4819 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
4823 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
4827 /* range checking */
4828 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
4829 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4831 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
4832 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4833 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
4834 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
4835 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4836 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
4837 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4839 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
4840 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4841 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
4842 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
4843 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4844 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
4845 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4847 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
4848 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
4849 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
4850 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
4851 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4853 ins->type = STACK_MP;
4855 MONO_ADD_INS (cfg->cbb, ins);
4861 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
4865 MonoMethod *addr_method;
4867 MonoClass *eclass = cmethod->klass->element_class;
4869 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
4872 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
4874 /* emit_ldelema_2 depends on OP_LMUL */
4875 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
4876 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
4879 if (mini_is_gsharedvt_variable_klass (eclass))
4882 element_size = mono_class_array_element_size (eclass);
4883 addr_method = mono_marshal_get_array_address (rank, element_size);
4884 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
4889 static MonoBreakPolicy
4890 always_insert_breakpoint (MonoMethod *method)
4892 return MONO_BREAK_POLICY_ALWAYS;
4895 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
4898 * mono_set_break_policy:
4899 * policy_callback: the new callback function
4901 * Allow embedders to decide wherther to actually obey breakpoint instructions
4902 * (both break IL instructions and Debugger.Break () method calls), for example
4903 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
4904 * untrusted or semi-trusted code.
4906 * @policy_callback will be called every time a break point instruction needs to
4907 * be inserted with the method argument being the method that calls Debugger.Break()
4908 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
4909 * if it wants the breakpoint to not be effective in the given method.
4910 * #MONO_BREAK_POLICY_ALWAYS is the default.
4913 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
4915 if (policy_callback)
4916 break_policy_func = policy_callback;
4918 break_policy_func = always_insert_breakpoint;
4922 should_insert_brekpoint (MonoMethod *method) {
4923 switch (break_policy_func (method)) {
4924 case MONO_BREAK_POLICY_ALWAYS:
4926 case MONO_BREAK_POLICY_NEVER:
4928 case MONO_BREAK_POLICY_ON_DBG:
4929 g_warning ("mdb no longer supported");
4932 g_warning ("Incorrect value returned from break policy callback");
4937 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
4939 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4941 MonoInst *addr, *store, *load;
4942 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
4944 /* the bounds check is already done by the callers */
4945 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4947 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
4948 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
4949 if (mini_type_is_reference (&eklass->byval_arg))
4950 emit_write_barrier (cfg, addr, load);
4952 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
4953 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
4960 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4962 return mini_type_is_reference (&klass->byval_arg);
4966 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
4968 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
4969 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
4970 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
4971 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
4972 MonoInst *iargs [3];
4975 mono_class_setup_vtable (obj_array);
4976 g_assert (helper->slot);
4978 if (sp [0]->type != STACK_OBJ)
4980 if (sp [2]->type != STACK_OBJ)
4987 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
4991 if (mini_is_gsharedvt_variable_klass (klass)) {
4994 // FIXME-VT: OP_ICONST optimization
4995 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
4996 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4997 ins->opcode = OP_STOREV_MEMBASE;
4998 } else if (sp [1]->opcode == OP_ICONST) {
4999 int array_reg = sp [0]->dreg;
5000 int index_reg = sp [1]->dreg;
5001 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5003 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5004 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5007 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5008 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5010 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5011 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5012 if (generic_class_is_reference_type (cfg, klass))
5013 emit_write_barrier (cfg, addr, sp [2]);
5020 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5025 eklass = mono_class_from_mono_type (fsig->params [2]);
5027 eklass = mono_class_from_mono_type (fsig->ret);
5030 return emit_array_store (cfg, eklass, args, FALSE);
5032 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5033 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5039 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5042 int param_size, return_size;
5044 param_klass = mono_class_from_mono_type (mini_get_underlying_type (¶m_klass->byval_arg));
5045 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5047 if (cfg->verbose_level > 3)
5048 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5050 //Don't allow mixing reference types with value types
5051 if (param_klass->valuetype != return_klass->valuetype) {
5052 if (cfg->verbose_level > 3)
5053 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5057 if (!param_klass->valuetype) {
5058 if (cfg->verbose_level > 3)
5059 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5064 if (param_klass->has_references || return_klass->has_references)
5067 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5068 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5069 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5070 if (cfg->verbose_level > 3)
5071 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5075 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5076 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5077 if (cfg->verbose_level > 3)
5078 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5082 param_size = mono_class_value_size (param_klass, &align);
5083 return_size = mono_class_value_size (return_klass, &align);
5085 //We can do it if sizes match
5086 if (param_size == return_size) {
5087 if (cfg->verbose_level > 3)
5088 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5092 //No simple way to handle struct if sizes don't match
5093 if (MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg)) {
5094 if (cfg->verbose_level > 3)
5095 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5100 * Same reg size category.
5101 * A quick note on why we don't require widening here.
5102 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5104 * Since the source value comes from a function argument, the JIT will already have
5105 * the value in a VREG and performed any widening needed before (say, when loading from a field).
5107 if (param_size <= 4 && return_size <= 4) {
5108 if (cfg->verbose_level > 3)
5109 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5117 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5119 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5120 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5122 if (mini_is_gsharedvt_variable_type (fsig->ret))
5125 //Valuetypes that are semantically equivalent or numbers than can be widened to
5126 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5129 //Arrays of valuetypes that are semantically equivalent
5130 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5137 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5139 #ifdef MONO_ARCH_SIMD_INTRINSICS
5140 MonoInst *ins = NULL;
5142 if (cfg->opt & MONO_OPT_SIMD) {
5143 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5149 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5153 emit_memory_barrier (MonoCompile *cfg, int kind)
5155 MonoInst *ins = NULL;
5156 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5157 MONO_ADD_INS (cfg->cbb, ins);
5158 ins->backend.memory_barrier_kind = kind;
5164 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5166 MonoInst *ins = NULL;
5169 /* The LLVM backend supports these intrinsics */
5170 if (cmethod->klass == mono_defaults.math_class) {
5171 if (strcmp (cmethod->name, "Sin") == 0) {
5173 } else if (strcmp (cmethod->name, "Cos") == 0) {
5175 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5177 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5181 if (opcode && fsig->param_count == 1) {
5182 MONO_INST_NEW (cfg, ins, opcode);
5183 ins->type = STACK_R8;
5184 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5185 ins->sreg1 = args [0]->dreg;
5186 MONO_ADD_INS (cfg->cbb, ins);
5190 if (cfg->opt & MONO_OPT_CMOV) {
5191 if (strcmp (cmethod->name, "Min") == 0) {
5192 if (fsig->params [0]->type == MONO_TYPE_I4)
5194 if (fsig->params [0]->type == MONO_TYPE_U4)
5195 opcode = OP_IMIN_UN;
5196 else if (fsig->params [0]->type == MONO_TYPE_I8)
5198 else if (fsig->params [0]->type == MONO_TYPE_U8)
5199 opcode = OP_LMIN_UN;
5200 } else if (strcmp (cmethod->name, "Max") == 0) {
5201 if (fsig->params [0]->type == MONO_TYPE_I4)
5203 if (fsig->params [0]->type == MONO_TYPE_U4)
5204 opcode = OP_IMAX_UN;
5205 else if (fsig->params [0]->type == MONO_TYPE_I8)
5207 else if (fsig->params [0]->type == MONO_TYPE_U8)
5208 opcode = OP_LMAX_UN;
5212 if (opcode && fsig->param_count == 2) {
5213 MONO_INST_NEW (cfg, ins, opcode);
5214 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5215 ins->dreg = mono_alloc_dreg (cfg, ins->type);
5216 ins->sreg1 = args [0]->dreg;
5217 ins->sreg2 = args [1]->dreg;
5218 MONO_ADD_INS (cfg->cbb, ins);
5226 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5228 if (cmethod->klass == mono_defaults.array_class) {
5229 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5230 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5231 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5232 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5233 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5234 return emit_array_unsafe_mov (cfg, fsig, args);
5241 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5243 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));
5394 } else if (cmethod->klass == mono_defaults.monitor_class) {
5395 gboolean is_enter = FALSE;
5396 gboolean is_v4 = FALSE;
5398 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
5402 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
5407 * To make async stack traces work, icalls which can block should have a wrapper.
5408 * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
5410 MonoBasicBlock *end_bb;
5412 NEW_BBLOCK (cfg, end_bb);
5414 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
5415 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
5416 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
5417 ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_internal : (gpointer)mono_monitor_enter_internal, args);
5418 MONO_START_BB (cfg, end_bb);
5421 } else if (cmethod->klass == mono_defaults.thread_class) {
5422 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5423 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5424 MONO_ADD_INS (cfg->cbb, ins);
5426 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5427 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5428 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5430 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5432 if (fsig->params [0]->type == MONO_TYPE_I1)
5433 opcode = OP_LOADI1_MEMBASE;
5434 else if (fsig->params [0]->type == MONO_TYPE_U1)
5435 opcode = OP_LOADU1_MEMBASE;
5436 else if (fsig->params [0]->type == MONO_TYPE_I2)
5437 opcode = OP_LOADI2_MEMBASE;
5438 else if (fsig->params [0]->type == MONO_TYPE_U2)
5439 opcode = OP_LOADU2_MEMBASE;
5440 else if (fsig->params [0]->type == MONO_TYPE_I4)
5441 opcode = OP_LOADI4_MEMBASE;
5442 else if (fsig->params [0]->type == MONO_TYPE_U4)
5443 opcode = OP_LOADU4_MEMBASE;
5444 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5445 opcode = OP_LOADI8_MEMBASE;
5446 else if (fsig->params [0]->type == MONO_TYPE_R4)
5447 opcode = OP_LOADR4_MEMBASE;
5448 else if (fsig->params [0]->type == MONO_TYPE_R8)
5449 opcode = OP_LOADR8_MEMBASE;
5450 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5451 opcode = OP_LOAD_MEMBASE;
5454 MONO_INST_NEW (cfg, ins, opcode);
5455 ins->inst_basereg = args [0]->dreg;
5456 ins->inst_offset = 0;
5457 MONO_ADD_INS (cfg->cbb, ins);
5459 switch (fsig->params [0]->type) {
5466 ins->dreg = mono_alloc_ireg (cfg);
5467 ins->type = STACK_I4;
5471 ins->dreg = mono_alloc_lreg (cfg);
5472 ins->type = STACK_I8;
5476 ins->dreg = mono_alloc_ireg (cfg);
5477 #if SIZEOF_REGISTER == 8
5478 ins->type = STACK_I8;
5480 ins->type = STACK_I4;
5485 ins->dreg = mono_alloc_freg (cfg);
5486 ins->type = STACK_R8;
5489 g_assert (mini_type_is_reference (fsig->params [0]));
5490 ins->dreg = mono_alloc_ireg_ref (cfg);
5491 ins->type = STACK_OBJ;
5495 if (opcode == OP_LOADI8_MEMBASE)
5496 ins = mono_decompose_opcode (cfg, ins);
5498 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5502 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5504 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5506 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5507 opcode = OP_STOREI1_MEMBASE_REG;
5508 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5509 opcode = OP_STOREI2_MEMBASE_REG;
5510 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5511 opcode = OP_STOREI4_MEMBASE_REG;
5512 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5513 opcode = OP_STOREI8_MEMBASE_REG;
5514 else if (fsig->params [0]->type == MONO_TYPE_R4)
5515 opcode = OP_STORER4_MEMBASE_REG;
5516 else if (fsig->params [0]->type == MONO_TYPE_R8)
5517 opcode = OP_STORER8_MEMBASE_REG;
5518 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5519 opcode = OP_STORE_MEMBASE_REG;
5522 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5524 MONO_INST_NEW (cfg, ins, opcode);
5525 ins->sreg1 = args [1]->dreg;
5526 ins->inst_destbasereg = args [0]->dreg;
5527 ins->inst_offset = 0;
5528 MONO_ADD_INS (cfg->cbb, ins);
5530 if (opcode == OP_STOREI8_MEMBASE_REG)
5531 ins = mono_decompose_opcode (cfg, ins);
5536 } else if (cmethod->klass->image == mono_defaults.corlib &&
5537 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5538 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5541 #if SIZEOF_REGISTER == 8
5542 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5543 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5544 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5545 ins->dreg = mono_alloc_preg (cfg);
5546 ins->sreg1 = args [0]->dreg;
5547 ins->type = STACK_I8;
5548 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5549 MONO_ADD_INS (cfg->cbb, ins);
5553 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5555 /* 64 bit reads are already atomic */
5556 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5557 load_ins->dreg = mono_alloc_preg (cfg);
5558 load_ins->inst_basereg = args [0]->dreg;
5559 load_ins->inst_offset = 0;
5560 load_ins->type = STACK_I8;
5561 MONO_ADD_INS (cfg->cbb, load_ins);
5563 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5570 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
5571 MonoInst *ins_iconst;
5574 if (fsig->params [0]->type == MONO_TYPE_I4) {
5575 opcode = OP_ATOMIC_ADD_I4;
5576 cfg->has_atomic_add_i4 = TRUE;
5578 #if SIZEOF_REGISTER == 8
5579 else if (fsig->params [0]->type == MONO_TYPE_I8)
5580 opcode = OP_ATOMIC_ADD_I8;
5583 if (!mono_arch_opcode_supported (opcode))
5585 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5586 ins_iconst->inst_c0 = 1;
5587 ins_iconst->dreg = mono_alloc_ireg (cfg);
5588 MONO_ADD_INS (cfg->cbb, ins_iconst);
5590 MONO_INST_NEW (cfg, ins, opcode);
5591 ins->dreg = mono_alloc_ireg (cfg);
5592 ins->inst_basereg = args [0]->dreg;
5593 ins->inst_offset = 0;
5594 ins->sreg2 = ins_iconst->dreg;
5595 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5596 MONO_ADD_INS (cfg->cbb, ins);
5598 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
5599 MonoInst *ins_iconst;
5602 if (fsig->params [0]->type == MONO_TYPE_I4) {
5603 opcode = OP_ATOMIC_ADD_I4;
5604 cfg->has_atomic_add_i4 = TRUE;
5606 #if SIZEOF_REGISTER == 8
5607 else if (fsig->params [0]->type == MONO_TYPE_I8)
5608 opcode = OP_ATOMIC_ADD_I8;
5611 if (!mono_arch_opcode_supported (opcode))
5613 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5614 ins_iconst->inst_c0 = -1;
5615 ins_iconst->dreg = mono_alloc_ireg (cfg);
5616 MONO_ADD_INS (cfg->cbb, ins_iconst);
5618 MONO_INST_NEW (cfg, ins, opcode);
5619 ins->dreg = mono_alloc_ireg (cfg);
5620 ins->inst_basereg = args [0]->dreg;
5621 ins->inst_offset = 0;
5622 ins->sreg2 = ins_iconst->dreg;
5623 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5624 MONO_ADD_INS (cfg->cbb, ins);
5626 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
5629 if (fsig->params [0]->type == MONO_TYPE_I4) {
5630 opcode = OP_ATOMIC_ADD_I4;
5631 cfg->has_atomic_add_i4 = TRUE;
5633 #if SIZEOF_REGISTER == 8
5634 else if (fsig->params [0]->type == MONO_TYPE_I8)
5635 opcode = OP_ATOMIC_ADD_I8;
5638 if (!mono_arch_opcode_supported (opcode))
5640 MONO_INST_NEW (cfg, ins, opcode);
5641 ins->dreg = mono_alloc_ireg (cfg);
5642 ins->inst_basereg = args [0]->dreg;
5643 ins->inst_offset = 0;
5644 ins->sreg2 = args [1]->dreg;
5645 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5646 MONO_ADD_INS (cfg->cbb, ins);
5649 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
5650 MonoInst *f2i = NULL, *i2f;
5651 guint32 opcode, f2i_opcode, i2f_opcode;
5652 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5653 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
5655 if (fsig->params [0]->type == MONO_TYPE_I4 ||
5656 fsig->params [0]->type == MONO_TYPE_R4) {
5657 opcode = OP_ATOMIC_EXCHANGE_I4;
5658 f2i_opcode = OP_MOVE_F_TO_I4;
5659 i2f_opcode = OP_MOVE_I4_TO_F;
5660 cfg->has_atomic_exchange_i4 = TRUE;
5662 #if SIZEOF_REGISTER == 8
5664 fsig->params [0]->type == MONO_TYPE_I8 ||
5665 fsig->params [0]->type == MONO_TYPE_R8 ||
5666 fsig->params [0]->type == MONO_TYPE_I) {
5667 opcode = OP_ATOMIC_EXCHANGE_I8;
5668 f2i_opcode = OP_MOVE_F_TO_I8;
5669 i2f_opcode = OP_MOVE_I8_TO_F;
5672 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
5673 opcode = OP_ATOMIC_EXCHANGE_I4;
5674 cfg->has_atomic_exchange_i4 = TRUE;
5680 if (!mono_arch_opcode_supported (opcode))
5684 /* TODO: Decompose these opcodes instead of bailing here. */
5685 if (COMPILE_SOFT_FLOAT (cfg))
5688 MONO_INST_NEW (cfg, f2i, f2i_opcode);
5689 f2i->dreg = mono_alloc_ireg (cfg);
5690 f2i->sreg1 = args [1]->dreg;
5691 if (f2i_opcode == OP_MOVE_F_TO_I4)
5692 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5693 MONO_ADD_INS (cfg->cbb, f2i);
5696 MONO_INST_NEW (cfg, ins, opcode);
5697 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
5698 ins->inst_basereg = args [0]->dreg;
5699 ins->inst_offset = 0;
5700 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
5701 MONO_ADD_INS (cfg->cbb, ins);
5703 switch (fsig->params [0]->type) {
5705 ins->type = STACK_I4;
5708 ins->type = STACK_I8;
5711 #if SIZEOF_REGISTER == 8
5712 ins->type = STACK_I8;
5714 ins->type = STACK_I4;
5719 ins->type = STACK_R8;
5722 g_assert (mini_type_is_reference (fsig->params [0]));
5723 ins->type = STACK_OBJ;
5728 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5729 i2f->dreg = mono_alloc_freg (cfg);
5730 i2f->sreg1 = ins->dreg;
5731 i2f->type = STACK_R8;
5732 if (i2f_opcode == OP_MOVE_I4_TO_F)
5733 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5734 MONO_ADD_INS (cfg->cbb, i2f);
5739 if (cfg->gen_write_barriers && is_ref)
5740 emit_write_barrier (cfg, args [0], args [1]);
5742 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
5743 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
5744 guint32 opcode, f2i_opcode, i2f_opcode;
5745 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
5746 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
5748 if (fsig->params [1]->type == MONO_TYPE_I4 ||
5749 fsig->params [1]->type == MONO_TYPE_R4) {
5750 opcode = OP_ATOMIC_CAS_I4;
5751 f2i_opcode = OP_MOVE_F_TO_I4;
5752 i2f_opcode = OP_MOVE_I4_TO_F;
5753 cfg->has_atomic_cas_i4 = TRUE;
5755 #if SIZEOF_REGISTER == 8
5757 fsig->params [1]->type == MONO_TYPE_I8 ||
5758 fsig->params [1]->type == MONO_TYPE_R8 ||
5759 fsig->params [1]->type == MONO_TYPE_I) {
5760 opcode = OP_ATOMIC_CAS_I8;
5761 f2i_opcode = OP_MOVE_F_TO_I8;
5762 i2f_opcode = OP_MOVE_I8_TO_F;
5765 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
5766 opcode = OP_ATOMIC_CAS_I4;
5767 cfg->has_atomic_cas_i4 = TRUE;
5773 if (!mono_arch_opcode_supported (opcode))
5777 /* TODO: Decompose these opcodes instead of bailing here. */
5778 if (COMPILE_SOFT_FLOAT (cfg))
5781 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
5782 f2i_new->dreg = mono_alloc_ireg (cfg);
5783 f2i_new->sreg1 = args [1]->dreg;
5784 if (f2i_opcode == OP_MOVE_F_TO_I4)
5785 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5786 MONO_ADD_INS (cfg->cbb, f2i_new);
5788 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
5789 f2i_cmp->dreg = mono_alloc_ireg (cfg);
5790 f2i_cmp->sreg1 = args [2]->dreg;
5791 if (f2i_opcode == OP_MOVE_F_TO_I4)
5792 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5793 MONO_ADD_INS (cfg->cbb, f2i_cmp);
5796 MONO_INST_NEW (cfg, ins, opcode);
5797 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
5798 ins->sreg1 = args [0]->dreg;
5799 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
5800 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
5801 MONO_ADD_INS (cfg->cbb, ins);
5803 switch (fsig->params [1]->type) {
5805 ins->type = STACK_I4;
5808 ins->type = STACK_I8;
5811 #if SIZEOF_REGISTER == 8
5812 ins->type = STACK_I8;
5814 ins->type = STACK_I4;
5818 ins->type = cfg->r4_stack_type;
5821 ins->type = STACK_R8;
5824 g_assert (mini_type_is_reference (fsig->params [1]));
5825 ins->type = STACK_OBJ;
5830 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5831 i2f->dreg = mono_alloc_freg (cfg);
5832 i2f->sreg1 = ins->dreg;
5833 i2f->type = STACK_R8;
5834 if (i2f_opcode == OP_MOVE_I4_TO_F)
5835 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5836 MONO_ADD_INS (cfg->cbb, i2f);
5841 if (cfg->gen_write_barriers && is_ref)
5842 emit_write_barrier (cfg, args [0], args [1]);
5844 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
5845 fsig->params [1]->type == MONO_TYPE_I4) {
5846 MonoInst *cmp, *ceq;
5848 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
5851 /* int32 r = CAS (location, value, comparand); */
5852 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
5853 ins->dreg = alloc_ireg (cfg);
5854 ins->sreg1 = args [0]->dreg;
5855 ins->sreg2 = args [1]->dreg;
5856 ins->sreg3 = args [2]->dreg;
5857 ins->type = STACK_I4;
5858 MONO_ADD_INS (cfg->cbb, ins);
5860 /* bool result = r == comparand; */
5861 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
5862 cmp->sreg1 = ins->dreg;
5863 cmp->sreg2 = args [2]->dreg;
5864 cmp->type = STACK_I4;
5865 MONO_ADD_INS (cfg->cbb, cmp);
5867 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
5868 ceq->dreg = alloc_ireg (cfg);
5869 ceq->type = STACK_I4;
5870 MONO_ADD_INS (cfg->cbb, ceq);
5872 /* *success = result; */
5873 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
5875 cfg->has_atomic_cas_i4 = TRUE;
5877 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
5878 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5882 } else if (cmethod->klass->image == mono_defaults.corlib &&
5883 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5884 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
5887 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
5889 MonoType *t = fsig->params [0];
5891 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
5893 g_assert (t->byref);
5894 /* t is a byref type, so the reference check is more complicated */
5895 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5896 if (t->type == MONO_TYPE_I1)
5897 opcode = OP_ATOMIC_LOAD_I1;
5898 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5899 opcode = OP_ATOMIC_LOAD_U1;
5900 else if (t->type == MONO_TYPE_I2)
5901 opcode = OP_ATOMIC_LOAD_I2;
5902 else if (t->type == MONO_TYPE_U2)
5903 opcode = OP_ATOMIC_LOAD_U2;
5904 else if (t->type == MONO_TYPE_I4)
5905 opcode = OP_ATOMIC_LOAD_I4;
5906 else if (t->type == MONO_TYPE_U4)
5907 opcode = OP_ATOMIC_LOAD_U4;
5908 else if (t->type == MONO_TYPE_R4)
5909 opcode = OP_ATOMIC_LOAD_R4;
5910 else if (t->type == MONO_TYPE_R8)
5911 opcode = OP_ATOMIC_LOAD_R8;
5912 #if SIZEOF_REGISTER == 8
5913 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5914 opcode = OP_ATOMIC_LOAD_I8;
5915 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5916 opcode = OP_ATOMIC_LOAD_U8;
5918 else if (t->type == MONO_TYPE_I)
5919 opcode = OP_ATOMIC_LOAD_I4;
5920 else if (is_ref || t->type == MONO_TYPE_U)
5921 opcode = OP_ATOMIC_LOAD_U4;
5925 if (!mono_arch_opcode_supported (opcode))
5928 MONO_INST_NEW (cfg, ins, opcode);
5929 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
5930 ins->sreg1 = args [0]->dreg;
5931 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
5932 MONO_ADD_INS (cfg->cbb, ins);
5935 case MONO_TYPE_BOOLEAN:
5942 ins->type = STACK_I4;
5946 ins->type = STACK_I8;
5950 #if SIZEOF_REGISTER == 8
5951 ins->type = STACK_I8;
5953 ins->type = STACK_I4;
5957 ins->type = cfg->r4_stack_type;
5960 ins->type = STACK_R8;
5964 ins->type = STACK_OBJ;
5970 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
5972 MonoType *t = fsig->params [0];
5975 g_assert (t->byref);
5976 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5977 if (t->type == MONO_TYPE_I1)
5978 opcode = OP_ATOMIC_STORE_I1;
5979 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5980 opcode = OP_ATOMIC_STORE_U1;
5981 else if (t->type == MONO_TYPE_I2)
5982 opcode = OP_ATOMIC_STORE_I2;
5983 else if (t->type == MONO_TYPE_U2)
5984 opcode = OP_ATOMIC_STORE_U2;
5985 else if (t->type == MONO_TYPE_I4)
5986 opcode = OP_ATOMIC_STORE_I4;
5987 else if (t->type == MONO_TYPE_U4)
5988 opcode = OP_ATOMIC_STORE_U4;
5989 else if (t->type == MONO_TYPE_R4)
5990 opcode = OP_ATOMIC_STORE_R4;
5991 else if (t->type == MONO_TYPE_R8)
5992 opcode = OP_ATOMIC_STORE_R8;
5993 #if SIZEOF_REGISTER == 8
5994 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5995 opcode = OP_ATOMIC_STORE_I8;
5996 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5997 opcode = OP_ATOMIC_STORE_U8;
5999 else if (t->type == MONO_TYPE_I)
6000 opcode = OP_ATOMIC_STORE_I4;
6001 else if (is_ref || t->type == MONO_TYPE_U)
6002 opcode = OP_ATOMIC_STORE_U4;
6006 if (!mono_arch_opcode_supported (opcode))
6009 MONO_INST_NEW (cfg, ins, opcode);
6010 ins->dreg = args [0]->dreg;
6011 ins->sreg1 = args [1]->dreg;
6012 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6013 MONO_ADD_INS (cfg->cbb, ins);
6015 if (cfg->gen_write_barriers && is_ref)
6016 emit_write_barrier (cfg, args [0], args [1]);
6022 } else if (cmethod->klass->image == mono_defaults.corlib &&
6023 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6024 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6025 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6026 if (should_insert_brekpoint (cfg->method)) {
6027 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6029 MONO_INST_NEW (cfg, ins, OP_NOP);
6030 MONO_ADD_INS (cfg->cbb, ins);
6034 } else if (cmethod->klass->image == mono_defaults.corlib &&
6035 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6036 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6037 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6039 EMIT_NEW_ICONST (cfg, ins, 1);
6041 EMIT_NEW_ICONST (cfg, ins, 0);
6044 } else if (cmethod->klass->image == mono_defaults.corlib &&
6045 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6046 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6047 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6048 /* No stack walks are currently available, so implement this as an intrinsic */
6049 MonoInst *assembly_ins;
6051 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6052 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6055 } else if (cmethod->klass->image == mono_defaults.corlib &&
6056 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6057 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6058 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6059 /* No stack walks are currently available, so implement this as an intrinsic */
6060 MonoInst *method_ins;
6061 MonoMethod *declaring = cfg->method;
6063 /* This returns the declaring generic method */
6064 if (declaring->is_inflated)
6065 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6066 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6067 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6068 cfg->no_inline = TRUE;
6069 if (cfg->method != cfg->current_method)
6070 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6073 } else if (cmethod->klass == mono_defaults.math_class) {
6075 * There is general branchless code for Min/Max, but it does not work for
6077 * http://everything2.com/?node_id=1051618
6079 } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
6080 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
6081 MONO_INST_NEW (cfg, ins, OP_PCEQ);
6082 ins->dreg = alloc_preg (cfg);
6083 ins->type = STACK_I4;
6084 MONO_ADD_INS (cfg->cbb, ins);
6086 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6087 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6088 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6089 !strcmp (cmethod->klass->name, "Selector")) ||
6090 ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
6091 !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
6092 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6093 !strcmp (cmethod->klass->name, "Selector"))
6095 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
6096 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6097 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6100 MonoJumpInfoToken *ji;
6103 if (args [0]->opcode == OP_GOT_ENTRY) {
6104 pi = (MonoInst *)args [0]->inst_p1;
6105 g_assert (pi->opcode == OP_PATCH_INFO);
6106 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6107 ji = (MonoJumpInfoToken *)pi->inst_p0;
6109 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6110 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6113 NULLIFY_INS (args [0]);
6115 s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
6116 return_val_if_nok (&cfg->error, NULL);
6118 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6119 ins->dreg = mono_alloc_ireg (cfg);
6122 MONO_ADD_INS (cfg->cbb, ins);
6127 #ifdef MONO_ARCH_SIMD_INTRINSICS
6128 if (cfg->opt & MONO_OPT_SIMD) {
6129 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6135 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6139 if (COMPILE_LLVM (cfg)) {
6140 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6145 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6149 * This entry point could be used later for arbitrary method
6152 inline static MonoInst*
6153 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6154 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6156 if (method->klass == mono_defaults.string_class) {
6157 /* managed string allocation support */
6158 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6159 MonoInst *iargs [2];
6160 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6161 MonoMethod *managed_alloc = NULL;
6163 g_assert (vtable); /*Should not fail since it System.String*/
6164 #ifndef MONO_CROSS_COMPILE
6165 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6169 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6170 iargs [1] = args [0];
6171 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6178 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6180 MonoInst *store, *temp;
6183 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6184 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6187 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6188 * would be different than the MonoInst's used to represent arguments, and
6189 * the ldelema implementation can't deal with that.
6190 * Solution: When ldelema is used on an inline argument, create a var for
6191 * it, emit ldelema on that var, and emit the saving code below in
6192 * inline_method () if needed.
6194 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6195 cfg->args [i] = temp;
6196 /* This uses cfg->args [i] which is set by the preceeding line */
6197 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6198 store->cil_code = sp [0]->cil_code;
6203 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6204 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6206 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6208 check_inline_called_method_name_limit (MonoMethod *called_method)
6211 static const char *limit = NULL;
6213 if (limit == NULL) {
6214 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6216 if (limit_string != NULL)
6217 limit = limit_string;
6222 if (limit [0] != '\0') {
6223 char *called_method_name = mono_method_full_name (called_method, TRUE);
6225 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6226 g_free (called_method_name);
6228 //return (strncmp_result <= 0);
6229 return (strncmp_result == 0);
6236 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6238 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6241 static const char *limit = NULL;
6243 if (limit == NULL) {
6244 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6245 if (limit_string != NULL) {
6246 limit = limit_string;
6252 if (limit [0] != '\0') {
6253 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6255 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6256 g_free (caller_method_name);
6258 //return (strncmp_result <= 0);
6259 return (strncmp_result == 0);
6267 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6269 static double r8_0 = 0.0;
6270 static float r4_0 = 0.0;
6274 rtype = mini_get_underlying_type (rtype);
6278 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6279 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6280 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6281 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6282 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6283 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6284 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6285 ins->type = STACK_R4;
6286 ins->inst_p0 = (void*)&r4_0;
6288 MONO_ADD_INS (cfg->cbb, ins);
6289 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6290 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6291 ins->type = STACK_R8;
6292 ins->inst_p0 = (void*)&r8_0;
6294 MONO_ADD_INS (cfg->cbb, ins);
6295 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6296 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6297 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6298 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6299 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6301 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6306 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6310 rtype = mini_get_underlying_type (rtype);
6314 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6315 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6316 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6317 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6318 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6319 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6320 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6321 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6322 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6323 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6324 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6325 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6326 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6327 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6329 emit_init_rvar (cfg, dreg, rtype);
6333 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6335 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6337 MonoInst *var = cfg->locals [local];
6338 if (COMPILE_SOFT_FLOAT (cfg)) {
6340 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
6341 emit_init_rvar (cfg, reg, type);
6342 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6345 emit_init_rvar (cfg, var->dreg, type);
6347 emit_dummy_init_rvar (cfg, var->dreg, type);
6352 mini_inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, guchar *ip, guint real_offset, gboolean inline_always)
6354 return inline_method (cfg, cmethod, fsig, sp, ip, real_offset, inline_always);
6360 * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
6363 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6364 guchar *ip, guint real_offset, gboolean inline_always)
6367 MonoInst *ins, *rvar = NULL;
6368 MonoMethodHeader *cheader;
6369 MonoBasicBlock *ebblock, *sbblock;
6371 MonoMethod *prev_inlined_method;
6372 MonoInst **prev_locals, **prev_args;
6373 MonoType **prev_arg_types;
6374 guint prev_real_offset;
6375 GHashTable *prev_cbb_hash;
6376 MonoBasicBlock **prev_cil_offset_to_bb;
6377 MonoBasicBlock *prev_cbb;
6378 const unsigned char *prev_ip;
6379 unsigned char *prev_cil_start;
6380 guint32 prev_cil_offset_to_bb_len;
6381 MonoMethod *prev_current_method;
6382 MonoGenericContext *prev_generic_context;
6383 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
6385 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6387 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6388 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6391 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6392 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6397 fsig = mono_method_signature (cmethod);
6399 if (cfg->verbose_level > 2)
6400 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6402 if (!cmethod->inline_info) {
6403 cfg->stat_inlineable_methods++;
6404 cmethod->inline_info = 1;
6407 /* allocate local variables */
6408 cheader = mono_method_get_header_checked (cmethod, &error);
6410 if (inline_always) {
6411 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
6412 mono_error_move (&cfg->error, &error);
6414 mono_error_cleanup (&error);
6419 /*Must verify before creating locals as it can cause the JIT to assert.*/
6420 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6421 mono_metadata_free_mh (cheader);
6425 /* allocate space to store the return value */
6426 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6427 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6430 prev_locals = cfg->locals;
6431 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6432 for (i = 0; i < cheader->num_locals; ++i)
6433 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6435 /* allocate start and end blocks */
6436 /* This is needed so if the inline is aborted, we can clean up */
6437 NEW_BBLOCK (cfg, sbblock);
6438 sbblock->real_offset = real_offset;
6440 NEW_BBLOCK (cfg, ebblock);
6441 ebblock->block_num = cfg->num_bblocks++;
6442 ebblock->real_offset = real_offset;
6444 prev_args = cfg->args;
6445 prev_arg_types = cfg->arg_types;
6446 prev_inlined_method = cfg->inlined_method;
6447 cfg->inlined_method = cmethod;
6448 cfg->ret_var_set = FALSE;
6449 cfg->inline_depth ++;
6450 prev_real_offset = cfg->real_offset;
6451 prev_cbb_hash = cfg->cbb_hash;
6452 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6453 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6454 prev_cil_start = cfg->cil_start;
6456 prev_cbb = cfg->cbb;
6457 prev_current_method = cfg->current_method;
6458 prev_generic_context = cfg->generic_context;
6459 prev_ret_var_set = cfg->ret_var_set;
6460 prev_disable_inline = cfg->disable_inline;
6462 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6465 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
6467 ret_var_set = cfg->ret_var_set;
6469 cfg->inlined_method = prev_inlined_method;
6470 cfg->real_offset = prev_real_offset;
6471 cfg->cbb_hash = prev_cbb_hash;
6472 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6473 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6474 cfg->cil_start = prev_cil_start;
6476 cfg->locals = prev_locals;
6477 cfg->args = prev_args;
6478 cfg->arg_types = prev_arg_types;
6479 cfg->current_method = prev_current_method;
6480 cfg->generic_context = prev_generic_context;
6481 cfg->ret_var_set = prev_ret_var_set;
6482 cfg->disable_inline = prev_disable_inline;
6483 cfg->inline_depth --;
6485 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6486 if (cfg->verbose_level > 2)
6487 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6489 cfg->stat_inlined_methods++;
6491 /* always add some code to avoid block split failures */
6492 MONO_INST_NEW (cfg, ins, OP_NOP);
6493 MONO_ADD_INS (prev_cbb, ins);
6495 prev_cbb->next_bb = sbblock;
6496 link_bblock (cfg, prev_cbb, sbblock);
6499 * Get rid of the begin and end bblocks if possible to aid local
6502 if (prev_cbb->out_count == 1)
6503 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6505 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6506 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6508 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6509 MonoBasicBlock *prev = ebblock->in_bb [0];
6511 if (prev->next_bb == ebblock) {
6512 mono_merge_basic_blocks (cfg, prev, ebblock);
6514 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6515 mono_merge_basic_blocks (cfg, prev_cbb, prev);
6516 cfg->cbb = prev_cbb;
6519 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
6524 * Its possible that the rvar is set in some prev bblock, but not in others.
6530 for (i = 0; i < ebblock->in_count; ++i) {
6531 bb = ebblock->in_bb [i];
6533 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6536 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6546 * If the inlined method contains only a throw, then the ret var is not
6547 * set, so set it to a dummy value.
6550 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6552 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6555 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6558 if (cfg->verbose_level > 2)
6559 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6560 cfg->exception_type = MONO_EXCEPTION_NONE;
6562 /* This gets rid of the newly added bblocks */
6563 cfg->cbb = prev_cbb;
6565 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6570 * Some of these comments may well be out-of-date.
6571 * Design decisions: we do a single pass over the IL code (and we do bblock
6572 * splitting/merging in the few cases when it's required: a back jump to an IL
6573 * address that was not already seen as bblock starting point).
6574 * Code is validated as we go (full verification is still better left to metadata/verify.c).
6575 * Complex operations are decomposed in simpler ones right away. We need to let the
6576 * arch-specific code peek and poke inside this process somehow (except when the
6577 * optimizations can take advantage of the full semantic info of coarse opcodes).
6578 * All the opcodes of the form opcode.s are 'normalized' to opcode.
6579 * MonoInst->opcode initially is the IL opcode or some simplification of that
6580 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
6581 * opcode with value bigger than OP_LAST.
6582 * At this point the IR can be handed over to an interpreter, a dumb code generator
6583 * or to the optimizing code generator that will translate it to SSA form.
6585 * Profiling directed optimizations.
6586 * We may compile by default with few or no optimizations and instrument the code
6587 * or the user may indicate what methods to optimize the most either in a config file
6588 * or through repeated runs where the compiler applies offline the optimizations to
6589 * each method and then decides if it was worth it.
6592 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6593 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6594 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6595 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6596 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6597 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6598 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6599 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
6601 /* offset from br.s -> br like opcodes */
6602 #define BIG_BRANCH_OFFSET 13
6605 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6607 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6609 return b == NULL || b == bb;
6613 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6615 unsigned char *ip = start;
6616 unsigned char *target;
6619 MonoBasicBlock *bblock;
6620 const MonoOpcode *opcode;
6623 cli_addr = ip - start;
6624 i = mono_opcode_value ((const guint8 **)&ip, end);
6627 opcode = &mono_opcodes [i];
6628 switch (opcode->argument) {
6629 case MonoInlineNone:
6632 case MonoInlineString:
6633 case MonoInlineType:
6634 case MonoInlineField:
6635 case MonoInlineMethod:
6638 case MonoShortInlineR:
6645 case MonoShortInlineVar:
6646 case MonoShortInlineI:
6649 case MonoShortInlineBrTarget:
6650 target = start + cli_addr + 2 + (signed char)ip [1];
6651 GET_BBLOCK (cfg, bblock, target);
6654 GET_BBLOCK (cfg, bblock, ip);
6656 case MonoInlineBrTarget:
6657 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
6658 GET_BBLOCK (cfg, bblock, target);
6661 GET_BBLOCK (cfg, bblock, ip);
6663 case MonoInlineSwitch: {
6664 guint32 n = read32 (ip + 1);
6667 cli_addr += 5 + 4 * n;
6668 target = start + cli_addr;
6669 GET_BBLOCK (cfg, bblock, target);
6671 for (j = 0; j < n; ++j) {
6672 target = start + cli_addr + (gint32)read32 (ip);
6673 GET_BBLOCK (cfg, bblock, target);
6683 g_assert_not_reached ();
6686 if (i == CEE_THROW) {
6687 unsigned char *bb_start = ip - 1;
6689 /* Find the start of the bblock containing the throw */
6691 while ((bb_start >= start) && !bblock) {
6692 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
6696 bblock->out_of_line = 1;
6706 static inline MonoMethod *
6707 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
6713 if (m->wrapper_type != MONO_WRAPPER_NONE) {
6714 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
6716 method = mono_class_inflate_generic_method_checked (method, context, error);
6719 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
6725 static inline MonoMethod *
6726 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
6729 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
6731 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
6732 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
6736 if (!method && !cfg)
6737 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6742 static inline MonoClass*
6743 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
6748 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6749 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6751 klass = mono_class_inflate_generic_class_checked (klass, context, &error);
6752 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6755 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
6756 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6759 mono_class_init (klass);
6763 static inline MonoMethodSignature*
6764 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
6766 MonoMethodSignature *fsig;
6769 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6770 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6772 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
6773 return_val_if_nok (error, NULL);
6776 fsig = mono_inflate_generic_signature(fsig, context, error);
6782 throw_exception (void)
6784 static MonoMethod *method = NULL;
6787 MonoSecurityManager *secman = mono_security_manager_get_methods ();
6788 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
6795 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
6797 MonoMethod *thrower = throw_exception ();
6800 EMIT_NEW_PCONST (cfg, args [0], ex);
6801 mono_emit_method_call (cfg, thrower, args, NULL);
6805 * Return the original method is a wrapper is specified. We can only access
6806 * the custom attributes from the original method.
6809 get_original_method (MonoMethod *method)
6811 if (method->wrapper_type == MONO_WRAPPER_NONE)
6814 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
6815 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
6818 /* in other cases we need to find the original method */
6819 return mono_marshal_method_from_wrapper (method);
6823 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
6825 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6826 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
6828 emit_throw_exception (cfg, ex);
6832 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
6834 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6835 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
6837 emit_throw_exception (cfg, ex);
6841 * Check that the IL instructions at ip are the array initialization
6842 * sequence and return the pointer to the data and the size.
6845 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
6848 * newarr[System.Int32]
6850 * ldtoken field valuetype ...
6851 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
6853 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
6855 guint32 token = read32 (ip + 7);
6856 guint32 field_token = read32 (ip + 2);
6857 guint32 field_index = field_token & 0xffffff;
6859 const char *data_ptr;
6861 MonoMethod *cmethod;
6862 MonoClass *dummy_class;
6863 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
6867 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6871 *out_field_token = field_token;
6873 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
6876 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
6878 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
6879 case MONO_TYPE_BOOLEAN:
6883 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
6884 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
6885 case MONO_TYPE_CHAR:
6902 if (size > mono_type_size (field->type, &dummy_align))
6905 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
6906 if (!image_is_dynamic (method->klass->image)) {
6907 field_index = read32 (ip + 2) & 0xffffff;
6908 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
6909 data_ptr = mono_image_rva_map (method->klass->image, rva);
6910 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
6911 /* for aot code we do the lookup on load */
6912 if (aot && data_ptr)
6913 return (const char *)GUINT_TO_POINTER (rva);
6915 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
6917 data_ptr = mono_field_get_data (field);
6925 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
6928 char *method_fname = mono_method_full_name (method, TRUE);
6930 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
6933 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
6934 mono_error_cleanup (&error);
6935 } else if (header->code_size == 0)
6936 method_code = g_strdup ("method body is empty.");
6938 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
6939 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
6940 g_free (method_fname);
6941 g_free (method_code);
6942 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
6946 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
6949 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
6950 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
6951 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
6952 /* Optimize reg-reg moves away */
6954 * Can't optimize other opcodes, since sp[0] might point to
6955 * the last ins of a decomposed opcode.
6957 sp [0]->dreg = (cfg)->locals [n]->dreg;
6959 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
6964 * ldloca inhibits many optimizations so try to get rid of it in common
6967 static inline unsigned char *
6968 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
6978 local = read16 (ip + 2);
6982 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
6983 /* From the INITOBJ case */
6984 token = read32 (ip + 2);
6985 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
6986 CHECK_TYPELOAD (klass);
6987 type = mini_get_underlying_type (&klass->byval_arg);
6988 emit_init_local (cfg, local, type, TRUE);
6996 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
6998 MonoInst *icall_args [16];
6999 MonoInst *call_target, *ins, *vtable_ins;
7000 int arg_reg, this_reg, vtable_reg;
7001 gboolean is_iface = mono_class_is_interface (cmethod->klass);
7002 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7003 gboolean variant_iface = FALSE;
7006 gboolean special_array_interface = cmethod->klass->is_array_special_interface;
7009 * In llvm-only mode, vtables contain function descriptors instead of
7010 * method addresses/trampolines.
7012 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7015 slot = mono_method_get_imt_slot (cmethod);
7017 slot = mono_method_get_vtable_index (cmethod);
7019 this_reg = sp [0]->dreg;
7021 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7022 variant_iface = TRUE;
7024 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7026 * The simplest case, a normal virtual call.
7028 int slot_reg = alloc_preg (cfg);
7029 int addr_reg = alloc_preg (cfg);
7030 int arg_reg = alloc_preg (cfg);
7031 MonoBasicBlock *non_null_bb;
7033 vtable_reg = alloc_preg (cfg);
7034 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7035 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7037 /* Load the vtable slot, which contains a function descriptor. */
7038 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7040 NEW_BBLOCK (cfg, non_null_bb);
7042 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7043 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7044 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7047 // FIXME: Make the wrapper use the preserveall cconv
7048 // FIXME: Use one icall per slot for small slot numbers ?
7049 icall_args [0] = vtable_ins;
7050 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7051 /* Make the icall return the vtable slot value to save some code space */
7052 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7053 ins->dreg = slot_reg;
7054 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7057 MONO_START_BB (cfg, non_null_bb);
7058 /* Load the address + arg from the vtable slot */
7059 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7060 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7062 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7065 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt && !special_array_interface) {
7067 * A simple interface call
7069 * We make a call through an imt slot to obtain the function descriptor we need to call.
7070 * The imt slot contains a function descriptor for a runtime function + arg.
7072 int slot_reg = alloc_preg (cfg);
7073 int addr_reg = alloc_preg (cfg);
7074 int arg_reg = alloc_preg (cfg);
7075 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7077 vtable_reg = alloc_preg (cfg);
7078 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7079 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7082 * The slot is already initialized when the vtable is created so there is no need
7086 /* Load the imt slot, which contains a function descriptor. */
7087 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7089 /* Load the address + arg of the imt thunk from the imt slot */
7090 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7091 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7093 * IMT thunks in llvm-only mode are C functions which take an info argument
7094 * plus the imt method and return the ftndesc to call.
7096 icall_args [0] = thunk_arg_ins;
7097 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7098 cmethod, MONO_RGCTX_INFO_METHOD);
7099 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7101 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7104 if ((fsig->generic_param_count || variant_iface || special_array_interface) && !is_gsharedvt) {
7106 * This is similar to the interface case, the vtable slot points to an imt thunk which is
7107 * dynamically extended as more instantiations are discovered.
7108 * This handles generic virtual methods both on classes and interfaces.
7110 int slot_reg = alloc_preg (cfg);
7111 int addr_reg = alloc_preg (cfg);
7112 int arg_reg = alloc_preg (cfg);
7113 int ftndesc_reg = alloc_preg (cfg);
7114 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7115 MonoBasicBlock *slowpath_bb, *end_bb;
7117 NEW_BBLOCK (cfg, slowpath_bb);
7118 NEW_BBLOCK (cfg, end_bb);
7120 vtable_reg = alloc_preg (cfg);
7121 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7123 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7125 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7127 /* Load the slot, which contains a function descriptor. */
7128 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7130 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7131 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7132 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7133 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7136 /* Same as with iface calls */
7137 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7138 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7139 icall_args [0] = thunk_arg_ins;
7140 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7141 cmethod, MONO_RGCTX_INFO_METHOD);
7142 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7143 ftndesc_ins->dreg = ftndesc_reg;
7145 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7146 * they don't know about yet. Fall back to the slowpath in that case.
7148 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7149 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7151 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7154 MONO_START_BB (cfg, slowpath_bb);
7155 icall_args [0] = vtable_ins;
7156 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7157 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7158 cmethod, MONO_RGCTX_INFO_METHOD);
7160 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7162 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7163 ftndesc_ins->dreg = ftndesc_reg;
7164 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7167 MONO_START_BB (cfg, end_bb);
7168 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7172 * Non-optimized cases
7174 icall_args [0] = sp [0];
7175 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7177 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7178 cmethod, MONO_RGCTX_INFO_METHOD);
7180 arg_reg = alloc_preg (cfg);
7181 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7182 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7184 g_assert (is_gsharedvt);
7186 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7188 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7191 * Pass the extra argument even if the callee doesn't receive it, most
7192 * calling conventions allow this.
7194 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7198 is_exception_class (MonoClass *klass)
7201 if (klass == mono_defaults.exception_class)
7203 klass = klass->parent;
7209 * is_jit_optimizer_disabled:
7211 * Determine whenever M's assembly has a DebuggableAttribute with the
7212 * IsJITOptimizerDisabled flag set.
7215 is_jit_optimizer_disabled (MonoMethod *m)
7218 MonoAssembly *ass = m->klass->image->assembly;
7219 MonoCustomAttrInfo* attrs;
7222 gboolean val = FALSE;
7225 if (ass->jit_optimizer_disabled_inited)
7226 return ass->jit_optimizer_disabled;
7228 klass = mono_class_try_get_debuggable_attribute_class ();
7232 ass->jit_optimizer_disabled = FALSE;
7233 mono_memory_barrier ();
7234 ass->jit_optimizer_disabled_inited = TRUE;
7238 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
7239 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7241 for (i = 0; i < attrs->num_attrs; ++i) {
7242 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7244 MonoMethodSignature *sig;
7246 if (!attr->ctor || attr->ctor->klass != klass)
7248 /* Decode the attribute. See reflection.c */
7249 p = (const char*)attr->data;
7250 g_assert (read16 (p) == 0x0001);
7253 // FIXME: Support named parameters
7254 sig = mono_method_signature (attr->ctor);
7255 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7257 /* Two boolean arguments */
7261 mono_custom_attrs_free (attrs);
7264 ass->jit_optimizer_disabled = val;
7265 mono_memory_barrier ();
7266 ass->jit_optimizer_disabled_inited = TRUE;
7272 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7274 gboolean supported_tail_call;
7277 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7279 for (i = 0; i < fsig->param_count; ++i) {
7280 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7281 /* These can point to the current method's stack */
7282 supported_tail_call = FALSE;
7284 if (fsig->hasthis && cmethod->klass->valuetype)
7285 /* this might point to the current method's stack */
7286 supported_tail_call = FALSE;
7287 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7288 supported_tail_call = FALSE;
7289 if (cfg->method->save_lmf)
7290 supported_tail_call = FALSE;
7291 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7292 supported_tail_call = FALSE;
7293 if (call_opcode != CEE_CALL)
7294 supported_tail_call = FALSE;
7296 /* Debugging support */
7298 if (supported_tail_call) {
7299 if (!mono_debug_count ())
7300 supported_tail_call = FALSE;
7304 return supported_tail_call;
7310 * Handle calls made to ctors from NEWOBJ opcodes.
7313 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7314 MonoInst **sp, guint8 *ip, int *inline_costs)
7316 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7318 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7319 mono_method_is_generic_sharable (cmethod, TRUE)) {
7320 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7321 mono_class_vtable (cfg->domain, cmethod->klass);
7322 CHECK_TYPELOAD (cmethod->klass);
7324 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7325 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7328 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
7329 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7331 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7333 CHECK_TYPELOAD (cmethod->klass);
7334 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7339 /* Avoid virtual calls to ctors if possible */
7340 if (mono_class_is_marshalbyref (cmethod->klass))
7341 callvirt_this_arg = sp [0];
7343 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7344 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7345 CHECK_CFG_EXCEPTION;
7346 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7347 mono_method_check_inlining (cfg, cmethod) &&
7348 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7351 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7352 cfg->real_offset += 5;
7354 *inline_costs += costs - 5;
7356 INLINE_FAILURE ("inline failure");
7357 // FIXME-VT: Clean this up
7358 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7359 GSHAREDVT_FAILURE(*ip);
7360 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7362 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7365 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7367 if (cfg->llvm_only) {
7368 // FIXME: Avoid initializing vtable_arg
7369 emit_llvmonly_calli (cfg, fsig, sp, addr);
7371 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7373 } else if (context_used &&
7374 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7375 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7376 MonoInst *cmethod_addr;
7378 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7380 if (cfg->llvm_only) {
7381 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
7382 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7383 emit_llvmonly_calli (cfg, fsig, sp, addr);
7385 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7386 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7388 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7391 INLINE_FAILURE ("ctor call");
7392 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7393 callvirt_this_arg, NULL, vtable_arg);
7400 emit_setret (MonoCompile *cfg, MonoInst *val)
7402 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
7405 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
7408 if (!cfg->vret_addr) {
7409 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
7411 EMIT_NEW_RETLOADA (cfg, ret_addr);
7413 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
7414 ins->klass = mono_class_from_mono_type (ret_type);
7417 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
7418 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
7419 MonoInst *iargs [1];
7423 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
7424 mono_arch_emit_setret (cfg, cfg->method, conv);
7426 mono_arch_emit_setret (cfg, cfg->method, val);
7429 mono_arch_emit_setret (cfg, cfg->method, val);
7435 * mono_method_to_ir:
7437 * Translate the .net IL into linear IR.
7439 * @start_bblock: if not NULL, the starting basic block, used during inlining.
7440 * @end_bblock: if not NULL, the ending basic block, used during inlining.
7441 * @return_var: if not NULL, the place where the return value is stored, used during inlining.
7442 * @inline_args: if not NULL, contains the arguments to the inline call
7443 * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
7444 * @is_virtual_call: whether this method is being called as a result of a call to callvirt
7446 * This method is used to turn ECMA IL into Mono's internal Linear IR
7447 * reprensetation. It is used both for entire methods, as well as
7448 * inlining existing methods. In the former case, the @start_bblock,
7449 * @end_bblock, @return_var, @inline_args are all set to NULL, and the
7450 * inline_offset is set to zero.
7452 * Returns: the inline cost, or -1 if there was an error processing this method.
7455 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
7456 MonoInst *return_var, MonoInst **inline_args,
7457 guint inline_offset, gboolean is_virtual_call)
7460 MonoInst *ins, **sp, **stack_start;
7461 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
7462 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7463 MonoMethod *cmethod, *method_definition;
7464 MonoInst **arg_array;
7465 MonoMethodHeader *header;
7467 guint32 token, ins_flag;
7469 MonoClass *constrained_class = NULL;
7470 unsigned char *ip, *end, *target, *err_pos;
7471 MonoMethodSignature *sig;
7472 MonoGenericContext *generic_context = NULL;
7473 MonoGenericContainer *generic_container = NULL;
7474 MonoType **param_types;
7475 int i, n, start_new_bblock, dreg;
7476 int num_calls = 0, inline_costs = 0;
7477 int breakpoint_id = 0;
7479 GSList *class_inits = NULL;
7480 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7482 gboolean init_locals, seq_points, skip_dead_blocks;
7483 gboolean sym_seq_points = FALSE;
7484 MonoDebugMethodInfo *minfo;
7485 MonoBitSet *seq_point_locs = NULL;
7486 MonoBitSet *seq_point_set_locs = NULL;
7488 cfg->disable_inline = is_jit_optimizer_disabled (method);
7490 /* serialization and xdomain stuff may need access to private fields and methods */
7491 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7492 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7493 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7494 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7495 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7496 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7498 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7499 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7500 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7501 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7502 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7504 image = method->klass->image;
7505 header = mono_method_get_header_checked (method, &cfg->error);
7507 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7508 goto exception_exit;
7510 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7513 generic_container = mono_method_get_generic_container (method);
7514 sig = mono_method_signature (method);
7515 num_args = sig->hasthis + sig->param_count;
7516 ip = (unsigned char*)header->code;
7517 cfg->cil_start = ip;
7518 end = ip + header->code_size;
7519 cfg->stat_cil_code_size += header->code_size;
7521 seq_points = cfg->gen_seq_points && cfg->method == method;
7523 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7524 /* We could hit a seq point before attaching to the JIT (#8338) */
7528 if (cfg->gen_sdb_seq_points && cfg->method == method) {
7529 minfo = mono_debug_lookup_method (method);
7531 MonoSymSeqPoint *sps;
7532 int i, n_il_offsets;
7534 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7535 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7536 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);
7537 sym_seq_points = TRUE;
7538 for (i = 0; i < n_il_offsets; ++i) {
7539 if (sps [i].il_offset < header->code_size)
7540 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7544 MonoDebugMethodAsyncInfo* asyncMethod = mono_debug_lookup_method_async_debug_info (method);
7546 for (i = 0; asyncMethod != NULL && i < asyncMethod->num_awaits; i++)
7548 mono_bitset_set_fast (seq_point_locs, asyncMethod->resume_offsets[i]);
7549 mono_bitset_set_fast (seq_point_locs, asyncMethod->yield_offsets[i]);
7551 mono_debug_free_method_async_debug_info (asyncMethod);
7553 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7554 /* Methods without line number info like auto-generated property accessors */
7555 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7556 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);
7557 sym_seq_points = TRUE;
7562 * Methods without init_locals set could cause asserts in various passes
7563 * (#497220). To work around this, we emit dummy initialization opcodes
7564 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7565 * on some platforms.
7567 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
7568 init_locals = header->init_locals;
7572 method_definition = method;
7573 while (method_definition->is_inflated) {
7574 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7575 method_definition = imethod->declaring;
7578 /* SkipVerification is not allowed if core-clr is enabled */
7579 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7581 dont_verify_stloc = TRUE;
7584 if (sig->is_inflated)
7585 generic_context = mono_method_get_context (method);
7586 else if (generic_container)
7587 generic_context = &generic_container->context;
7588 cfg->generic_context = generic_context;
7591 g_assert (!sig->has_type_parameters);
7593 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7594 g_assert (method->is_inflated);
7595 g_assert (mono_method_get_context (method)->method_inst);
7597 if (method->is_inflated && mono_method_get_context (method)->method_inst)
7598 g_assert (sig->generic_param_count);
7600 if (cfg->method == method) {
7601 cfg->real_offset = 0;
7603 cfg->real_offset = inline_offset;
7606 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7607 cfg->cil_offset_to_bb_len = header->code_size;
7609 cfg->current_method = method;
7611 if (cfg->verbose_level > 2)
7612 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7614 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7616 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7617 for (n = 0; n < sig->param_count; ++n)
7618 param_types [n + sig->hasthis] = sig->params [n];
7619 cfg->arg_types = param_types;
7621 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7622 if (cfg->method == method) {
7624 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7625 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7628 NEW_BBLOCK (cfg, start_bblock);
7629 cfg->bb_entry = start_bblock;
7630 start_bblock->cil_code = NULL;
7631 start_bblock->cil_length = 0;
7634 NEW_BBLOCK (cfg, end_bblock);
7635 cfg->bb_exit = end_bblock;
7636 end_bblock->cil_code = NULL;
7637 end_bblock->cil_length = 0;
7638 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7639 g_assert (cfg->num_bblocks == 2);
7641 arg_array = cfg->args;
7643 if (header->num_clauses) {
7644 cfg->spvars = g_hash_table_new (NULL, NULL);
7645 cfg->exvars = g_hash_table_new (NULL, NULL);
7647 /* handle exception clauses */
7648 for (i = 0; i < header->num_clauses; ++i) {
7649 MonoBasicBlock *try_bb;
7650 MonoExceptionClause *clause = &header->clauses [i];
7651 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7653 try_bb->real_offset = clause->try_offset;
7654 try_bb->try_start = TRUE;
7655 try_bb->region = ((i + 1) << 8) | clause->flags;
7656 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7657 tblock->real_offset = clause->handler_offset;
7658 tblock->flags |= BB_EXCEPTION_HANDLER;
7661 * Linking the try block with the EH block hinders inlining as we won't be able to
7662 * merge the bblocks from inlining and produce an artificial hole for no good reason.
7664 if (COMPILE_LLVM (cfg))
7665 link_bblock (cfg, try_bb, tblock);
7667 if (*(ip + clause->handler_offset) == CEE_POP)
7668 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7670 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7671 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7672 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7673 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7674 MONO_ADD_INS (tblock, ins);
7676 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7677 /* finally clauses already have a seq point */
7678 /* seq points for filter clauses are emitted below */
7679 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7680 MONO_ADD_INS (tblock, ins);
7683 /* todo: is a fault block unsafe to optimize? */
7684 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7685 tblock->flags |= BB_EXCEPTION_UNSAFE;
7688 /*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);
7690 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7692 /* catch and filter blocks get the exception object on the stack */
7693 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7694 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7696 /* mostly like handle_stack_args (), but just sets the input args */
7697 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7698 tblock->in_scount = 1;
7699 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7700 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7704 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7705 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7706 if (!cfg->compile_llvm) {
7707 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7708 ins->dreg = tblock->in_stack [0]->dreg;
7709 MONO_ADD_INS (tblock, ins);
7712 MonoInst *dummy_use;
7715 * Add a dummy use for the exvar so its liveness info will be
7718 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7721 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7722 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7723 MONO_ADD_INS (tblock, ins);
7726 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7727 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7728 tblock->flags |= BB_EXCEPTION_HANDLER;
7729 tblock->real_offset = clause->data.filter_offset;
7730 tblock->in_scount = 1;
7731 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7732 /* The filter block shares the exvar with the handler block */
7733 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7734 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7735 MONO_ADD_INS (tblock, ins);
7739 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7740 clause->data.catch_class &&
7742 mono_class_check_context_used (clause->data.catch_class)) {
7744 * In shared generic code with catch
7745 * clauses containing type variables
7746 * the exception handling code has to
7747 * be able to get to the rgctx.
7748 * Therefore we have to make sure that
7749 * the vtable/mrgctx argument (for
7750 * static or generic methods) or the
7751 * "this" argument (for non-static
7752 * methods) are live.
7754 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7755 mini_method_get_context (method)->method_inst ||
7756 method->klass->valuetype) {
7757 mono_get_vtable_var (cfg);
7759 MonoInst *dummy_use;
7761 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7766 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7767 cfg->cbb = start_bblock;
7768 cfg->args = arg_array;
7769 mono_save_args (cfg, sig, inline_args);
7772 /* FIRST CODE BLOCK */
7773 NEW_BBLOCK (cfg, tblock);
7774 tblock->cil_code = ip;
7778 ADD_BBLOCK (cfg, tblock);
7780 if (cfg->method == method) {
7781 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7782 if (breakpoint_id) {
7783 MONO_INST_NEW (cfg, ins, OP_BREAK);
7784 MONO_ADD_INS (cfg->cbb, ins);
7788 /* we use a separate basic block for the initialization code */
7789 NEW_BBLOCK (cfg, init_localsbb);
7790 if (cfg->method == method)
7791 cfg->bb_init = init_localsbb;
7792 init_localsbb->real_offset = cfg->real_offset;
7793 start_bblock->next_bb = init_localsbb;
7794 init_localsbb->next_bb = cfg->cbb;
7795 link_bblock (cfg, start_bblock, init_localsbb);
7796 link_bblock (cfg, init_localsbb, cfg->cbb);
7798 cfg->cbb = init_localsbb;
7800 if (cfg->gsharedvt && cfg->method == method) {
7801 MonoGSharedVtMethodInfo *info;
7802 MonoInst *var, *locals_var;
7805 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7806 info->method = cfg->method;
7807 info->count_entries = 16;
7808 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7809 cfg->gsharedvt_info = info;
7811 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7812 /* prevent it from being register allocated */
7813 //var->flags |= MONO_INST_VOLATILE;
7814 cfg->gsharedvt_info_var = var;
7816 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7817 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7819 /* Allocate locals */
7820 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7821 /* prevent it from being register allocated */
7822 //locals_var->flags |= MONO_INST_VOLATILE;
7823 cfg->gsharedvt_locals_var = locals_var;
7825 dreg = alloc_ireg (cfg);
7826 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7828 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7829 ins->dreg = locals_var->dreg;
7831 MONO_ADD_INS (cfg->cbb, ins);
7832 cfg->gsharedvt_locals_var_ins = ins;
7834 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7837 ins->flags |= MONO_INST_INIT;
7841 if (mono_security_core_clr_enabled ()) {
7842 /* check if this is native code, e.g. an icall or a p/invoke */
7843 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
7844 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
7846 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
7847 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
7849 /* if this ia a native call then it can only be JITted from platform code */
7850 if ((icall || pinvk) && method->klass && method->klass->image) {
7851 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
7852 MonoException *ex = icall ? mono_get_exception_security () :
7853 mono_get_exception_method_access ();
7854 emit_throw_exception (cfg, ex);
7861 CHECK_CFG_EXCEPTION;
7863 if (header->code_size == 0)
7866 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
7871 if (cfg->method == method)
7872 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
7874 for (n = 0; n < header->num_locals; ++n) {
7875 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
7880 /* We force the vtable variable here for all shared methods
7881 for the possibility that they might show up in a stack
7882 trace where their exact instantiation is needed. */
7883 if (cfg->gshared && method == cfg->method) {
7884 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7885 mini_method_get_context (method)->method_inst ||
7886 method->klass->valuetype) {
7887 mono_get_vtable_var (cfg);
7889 /* FIXME: Is there a better way to do this?
7890 We need the variable live for the duration
7891 of the whole method. */
7892 cfg->args [0]->flags |= MONO_INST_VOLATILE;
7896 /* add a check for this != NULL to inlined methods */
7897 if (is_virtual_call) {
7900 NEW_ARGLOAD (cfg, arg_ins, 0);
7901 MONO_ADD_INS (cfg->cbb, arg_ins);
7902 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
7905 skip_dead_blocks = !dont_verify;
7906 if (skip_dead_blocks) {
7907 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
7912 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
7913 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
7916 start_new_bblock = 0;
7918 if (cfg->method == method)
7919 cfg->real_offset = ip - header->code;
7921 cfg->real_offset = inline_offset;
7926 if (start_new_bblock) {
7927 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
7928 if (start_new_bblock == 2) {
7929 g_assert (ip == tblock->cil_code);
7931 GET_BBLOCK (cfg, tblock, ip);
7933 cfg->cbb->next_bb = tblock;
7935 start_new_bblock = 0;
7936 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7937 if (cfg->verbose_level > 3)
7938 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7939 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7943 g_slist_free (class_inits);
7946 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
7947 link_bblock (cfg, cfg->cbb, tblock);
7948 if (sp != stack_start) {
7949 handle_stack_args (cfg, stack_start, sp - stack_start);
7951 CHECK_UNVERIFIABLE (cfg);
7953 cfg->cbb->next_bb = tblock;
7955 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7956 if (cfg->verbose_level > 3)
7957 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7958 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7961 g_slist_free (class_inits);
7966 if (skip_dead_blocks) {
7967 int ip_offset = ip - header->code;
7969 if (ip_offset == bb->end)
7973 int op_size = mono_opcode_size (ip, end);
7974 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
7976 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
7978 if (ip_offset + op_size == bb->end) {
7979 MONO_INST_NEW (cfg, ins, OP_NOP);
7980 MONO_ADD_INS (cfg->cbb, ins);
7981 start_new_bblock = 1;
7989 * Sequence points are points where the debugger can place a breakpoint.
7990 * Currently, we generate these automatically at points where the IL
7993 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
7995 * Make methods interruptable at the beginning, and at the targets of
7996 * backward branches.
7997 * Also, do this at the start of every bblock in methods with clauses too,
7998 * to be able to handle instructions with inprecise control flow like
8000 * Backward branches are handled at the end of method-to-ir ().
8002 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8003 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8005 /* Avoid sequence points on empty IL like .volatile */
8006 // FIXME: Enable this
8007 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8008 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8009 if ((sp != stack_start) && !sym_seq_point)
8010 ins->flags |= MONO_INST_NONEMPTY_STACK;
8011 MONO_ADD_INS (cfg->cbb, ins);
8014 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8017 cfg->cbb->real_offset = cfg->real_offset;
8019 if ((cfg->method == method) && cfg->coverage_info) {
8020 guint32 cil_offset = ip - header->code;
8021 cfg->coverage_info->data [cil_offset].cil_code = ip;
8023 /* TODO: Use an increment here */
8024 #if defined(TARGET_X86)
8025 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8026 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8028 MONO_ADD_INS (cfg->cbb, ins);
8030 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8031 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8035 if (cfg->verbose_level > 3)
8036 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8040 if (seq_points && !sym_seq_points && sp != stack_start) {
8042 * The C# compiler uses these nops to notify the JIT that it should
8043 * insert seq points.
8045 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8046 MONO_ADD_INS (cfg->cbb, ins);
8048 if (cfg->keep_cil_nops)
8049 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8051 MONO_INST_NEW (cfg, ins, OP_NOP);
8053 MONO_ADD_INS (cfg->cbb, ins);
8056 if (should_insert_brekpoint (cfg->method)) {
8057 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8059 MONO_INST_NEW (cfg, ins, OP_NOP);
8062 MONO_ADD_INS (cfg->cbb, ins);
8068 CHECK_STACK_OVF (1);
8069 n = (*ip)-CEE_LDARG_0;
8071 EMIT_NEW_ARGLOAD (cfg, ins, n);
8079 CHECK_STACK_OVF (1);
8080 n = (*ip)-CEE_LDLOC_0;
8082 EMIT_NEW_LOCLOAD (cfg, ins, n);
8091 n = (*ip)-CEE_STLOC_0;
8094 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8096 emit_stloc_ir (cfg, sp, header, n);
8103 CHECK_STACK_OVF (1);
8106 EMIT_NEW_ARGLOAD (cfg, ins, n);
8112 CHECK_STACK_OVF (1);
8115 NEW_ARGLOADA (cfg, ins, n);
8116 MONO_ADD_INS (cfg->cbb, ins);
8126 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8128 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8133 CHECK_STACK_OVF (1);
8136 EMIT_NEW_LOCLOAD (cfg, ins, n);
8140 case CEE_LDLOCA_S: {
8141 unsigned char *tmp_ip;
8143 CHECK_STACK_OVF (1);
8144 CHECK_LOCAL (ip [1]);
8146 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8152 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8161 CHECK_LOCAL (ip [1]);
8162 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8164 emit_stloc_ir (cfg, sp, header, ip [1]);
8169 CHECK_STACK_OVF (1);
8170 EMIT_NEW_PCONST (cfg, ins, NULL);
8171 ins->type = STACK_OBJ;
8176 CHECK_STACK_OVF (1);
8177 EMIT_NEW_ICONST (cfg, ins, -1);
8190 CHECK_STACK_OVF (1);
8191 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8197 CHECK_STACK_OVF (1);
8199 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8205 CHECK_STACK_OVF (1);
8206 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8212 CHECK_STACK_OVF (1);
8213 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8214 ins->type = STACK_I8;
8215 ins->dreg = alloc_dreg (cfg, STACK_I8);
8217 ins->inst_l = (gint64)read64 (ip);
8218 MONO_ADD_INS (cfg->cbb, ins);
8224 gboolean use_aotconst = FALSE;
8226 #ifdef TARGET_POWERPC
8227 /* FIXME: Clean this up */
8228 if (cfg->compile_aot)
8229 use_aotconst = TRUE;
8232 /* FIXME: we should really allocate this only late in the compilation process */
8233 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8235 CHECK_STACK_OVF (1);
8241 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8243 dreg = alloc_freg (cfg);
8244 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8245 ins->type = cfg->r4_stack_type;
8247 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8248 ins->type = cfg->r4_stack_type;
8249 ins->dreg = alloc_dreg (cfg, STACK_R8);
8251 MONO_ADD_INS (cfg->cbb, ins);
8261 gboolean use_aotconst = FALSE;
8263 #ifdef TARGET_POWERPC
8264 /* FIXME: Clean this up */
8265 if (cfg->compile_aot)
8266 use_aotconst = TRUE;
8269 /* FIXME: we should really allocate this only late in the compilation process */
8270 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8272 CHECK_STACK_OVF (1);
8278 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8280 dreg = alloc_freg (cfg);
8281 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8282 ins->type = STACK_R8;
8284 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8285 ins->type = STACK_R8;
8286 ins->dreg = alloc_dreg (cfg, STACK_R8);
8288 MONO_ADD_INS (cfg->cbb, ins);
8297 MonoInst *temp, *store;
8299 CHECK_STACK_OVF (1);
8303 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8304 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8306 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8309 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8322 if (sp [0]->type == STACK_R8)
8323 /* we need to pop the value from the x86 FP stack */
8324 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8329 MonoMethodSignature *fsig;
8332 INLINE_FAILURE ("jmp");
8333 GSHAREDVT_FAILURE (*ip);
8336 if (stack_start != sp)
8338 token = read32 (ip + 1);
8339 /* FIXME: check the signature matches */
8340 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8343 if (cfg->gshared && mono_method_check_context_used (cmethod))
8344 GENERIC_SHARING_FAILURE (CEE_JMP);
8346 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8348 fsig = mono_method_signature (cmethod);
8349 n = fsig->param_count + fsig->hasthis;
8350 if (cfg->llvm_only) {
8353 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8354 for (i = 0; i < n; ++i)
8355 EMIT_NEW_ARGLOAD (cfg, args [i], i);
8356 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
8358 * The code in mono-basic-block.c treats the rest of the code as dead, but we
8359 * have to emit a normal return since llvm expects it.
8362 emit_setret (cfg, ins);
8363 MONO_INST_NEW (cfg, ins, OP_BR);
8364 ins->inst_target_bb = end_bblock;
8365 MONO_ADD_INS (cfg->cbb, ins);
8366 link_bblock (cfg, cfg->cbb, end_bblock);
8369 } else if (cfg->backend->have_op_tail_call) {
8370 /* Handle tail calls similarly to calls */
8373 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8374 call->method = cmethod;
8375 call->tail_call = TRUE;
8376 call->signature = mono_method_signature (cmethod);
8377 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8378 call->inst.inst_p0 = cmethod;
8379 for (i = 0; i < n; ++i)
8380 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8382 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
8383 call->vret_var = cfg->vret_addr;
8385 mono_arch_emit_call (cfg, call);
8386 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8387 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8389 for (i = 0; i < num_args; ++i)
8390 /* Prevent arguments from being optimized away */
8391 arg_array [i]->flags |= MONO_INST_VOLATILE;
8393 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8394 ins = (MonoInst*)call;
8395 ins->inst_p0 = cmethod;
8396 MONO_ADD_INS (cfg->cbb, ins);
8400 start_new_bblock = 1;
8405 MonoMethodSignature *fsig;
8408 token = read32 (ip + 1);
8412 //GSHAREDVT_FAILURE (*ip);
8417 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
8420 if (method->dynamic && fsig->pinvoke) {
8424 * This is a call through a function pointer using a pinvoke
8425 * signature. Have to create a wrapper and call that instead.
8426 * FIXME: This is very slow, need to create a wrapper at JIT time
8427 * instead based on the signature.
8429 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8430 EMIT_NEW_PCONST (cfg, args [1], fsig);
8432 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8435 n = fsig->param_count + fsig->hasthis;
8439 //g_assert (!virtual_ || fsig->hasthis);
8443 inline_costs += 10 * num_calls++;
8446 * Making generic calls out of gsharedvt methods.
8447 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8448 * patching gshared method addresses into a gsharedvt method.
8450 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8452 * We pass the address to the gsharedvt trampoline in the rgctx reg
8454 MonoInst *callee = addr;
8456 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8458 GSHAREDVT_FAILURE (*ip);
8462 GSHAREDVT_FAILURE (*ip);
8464 addr = emit_get_rgctx_sig (cfg, context_used,
8465 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8466 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8470 /* Prevent inlining of methods with indirect calls */
8471 INLINE_FAILURE ("indirect call");
8473 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8474 MonoJumpInfoType info_type;
8478 * Instead of emitting an indirect call, emit a direct call
8479 * with the contents of the aotconst as the patch info.
8481 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8482 info_type = (MonoJumpInfoType)addr->inst_c1;
8483 info_data = addr->inst_p0;
8485 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
8486 info_data = addr->inst_right->inst_left;
8489 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
8490 ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
8493 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8494 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8499 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8503 /* End of call, INS should contain the result of the call, if any */
8505 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8507 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8510 CHECK_CFG_EXCEPTION;
8514 constrained_class = NULL;
8518 case CEE_CALLVIRT: {
8519 MonoInst *addr = NULL;
8520 MonoMethodSignature *fsig = NULL;
8522 int virtual_ = *ip == CEE_CALLVIRT;
8523 gboolean pass_imt_from_rgctx = FALSE;
8524 MonoInst *imt_arg = NULL;
8525 MonoInst *keep_this_alive = NULL;
8526 gboolean pass_vtable = FALSE;
8527 gboolean pass_mrgctx = FALSE;
8528 MonoInst *vtable_arg = NULL;
8529 gboolean check_this = FALSE;
8530 gboolean supported_tail_call = FALSE;
8531 gboolean tail_call = FALSE;
8532 gboolean need_seq_point = FALSE;
8533 guint32 call_opcode = *ip;
8534 gboolean emit_widen = TRUE;
8535 gboolean push_res = TRUE;
8536 gboolean skip_ret = FALSE;
8537 gboolean delegate_invoke = FALSE;
8538 gboolean direct_icall = FALSE;
8539 gboolean constrained_partial_call = FALSE;
8540 MonoMethod *cil_method;
8543 token = read32 (ip + 1);
8547 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8550 cil_method = cmethod;
8552 if (constrained_class) {
8553 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8554 if (!mini_is_gsharedvt_klass (constrained_class)) {
8555 g_assert (!cmethod->klass->valuetype);
8556 if (!mini_type_is_reference (&constrained_class->byval_arg))
8557 constrained_partial_call = TRUE;
8561 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8562 if (cfg->verbose_level > 2)
8563 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8564 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8565 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8567 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8571 if (cfg->verbose_level > 2)
8572 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8574 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8576 * This is needed since get_method_constrained can't find
8577 * the method in klass representing a type var.
8578 * The type var is guaranteed to be a reference type in this
8581 if (!mini_is_gsharedvt_klass (constrained_class))
8582 g_assert (!cmethod->klass->valuetype);
8584 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8589 if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
8590 /* Use the corresponding method from the base type to avoid boxing */
8591 MonoType *base_type = mono_class_enum_basetype (constrained_class);
8592 g_assert (base_type);
8593 constrained_class = mono_class_from_mono_type (base_type);
8594 cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
8599 if (!dont_verify && !cfg->skip_visibility) {
8600 MonoMethod *target_method = cil_method;
8601 if (method->is_inflated) {
8602 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
8605 if (!mono_method_can_access_method (method_definition, target_method) &&
8606 !mono_method_can_access_method (method, cil_method))
8607 emit_method_access_failure (cfg, method, cil_method);
8610 if (mono_security_core_clr_enabled ())
8611 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8613 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8614 /* MS.NET seems to silently convert this to a callvirt */
8619 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8620 * converts to a callvirt.
8622 * tests/bug-515884.il is an example of this behavior
8624 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8625 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8626 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8630 if (!cmethod->klass->inited)
8631 if (!mono_class_init (cmethod->klass))
8632 TYPE_LOAD_ERROR (cmethod->klass);
8634 fsig = mono_method_signature (cmethod);
8637 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8638 mini_class_is_system_array (cmethod->klass)) {
8639 array_rank = cmethod->klass->rank;
8640 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8641 direct_icall = TRUE;
8642 } else if (fsig->pinvoke) {
8643 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8644 fsig = mono_method_signature (wrapper);
8645 } else if (constrained_class) {
8647 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8651 if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
8652 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
8654 /* See code below */
8655 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8656 MonoBasicBlock *tbb;
8658 GET_BBLOCK (cfg, tbb, ip + 5);
8659 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8661 * We want to extend the try block to cover the call, but we can't do it if the
8662 * call is made directly since its followed by an exception check.
8664 direct_icall = FALSE;
8668 mono_save_token_info (cfg, image, token, cil_method);
8670 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8671 need_seq_point = TRUE;
8673 /* Don't support calls made using type arguments for now */
8675 if (cfg->gsharedvt) {
8676 if (mini_is_gsharedvt_signature (fsig))
8677 GSHAREDVT_FAILURE (*ip);
8681 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8682 g_assert_not_reached ();
8684 n = fsig->param_count + fsig->hasthis;
8686 if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
8690 g_assert (!mono_method_check_context_used (cmethod));
8694 //g_assert (!virtual_ || fsig->hasthis);
8699 * We have the `constrained.' prefix opcode.
8701 if (constrained_class) {
8702 if (mini_is_gsharedvt_klass (constrained_class)) {
8703 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8704 /* The 'Own method' case below */
8705 } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
8706 /* 'The type parameter is instantiated as a reference type' case below. */
8708 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8709 CHECK_CFG_EXCEPTION;
8715 if (constrained_partial_call) {
8716 gboolean need_box = TRUE;
8719 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8720 * called method is not known at compile time either. The called method could end up being
8721 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8722 * to box the receiver.
8723 * A simple solution would be to box always and make a normal virtual call, but that would
8724 * be bad performance wise.
8726 if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
8728 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8733 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)) {
8734 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8735 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8736 ins->klass = constrained_class;
8737 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8738 CHECK_CFG_EXCEPTION;
8739 } else if (need_box) {
8741 MonoBasicBlock *is_ref_bb, *end_bb;
8742 MonoInst *nonbox_call;
8745 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8747 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8748 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8750 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8752 NEW_BBLOCK (cfg, is_ref_bb);
8753 NEW_BBLOCK (cfg, end_bb);
8755 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);
8756 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
8757 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8760 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8762 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8765 MONO_START_BB (cfg, is_ref_bb);
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 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8771 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8773 MONO_START_BB (cfg, end_bb);
8776 nonbox_call->dreg = ins->dreg;
8779 g_assert (mono_class_is_interface (cmethod->klass));
8780 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8781 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8784 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8786 * The type parameter is instantiated as a valuetype,
8787 * but that type doesn't override the method we're
8788 * calling, so we need to box `this'.
8790 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8791 ins->klass = constrained_class;
8792 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8793 CHECK_CFG_EXCEPTION;
8794 } else if (!constrained_class->valuetype) {
8795 int dreg = alloc_ireg_ref (cfg);
8798 * The type parameter is instantiated as a reference
8799 * type. We have a managed pointer on the stack, so
8800 * we need to dereference it here.
8802 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8803 ins->type = STACK_OBJ;
8806 if (cmethod->klass->valuetype) {
8809 /* Interface method */
8812 mono_class_setup_vtable (constrained_class);
8813 CHECK_TYPELOAD (constrained_class);
8814 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8816 TYPE_LOAD_ERROR (constrained_class);
8817 slot = mono_method_get_vtable_slot (cmethod);
8819 TYPE_LOAD_ERROR (cmethod->klass);
8820 cmethod = constrained_class->vtable [ioffset + slot];
8822 if (cmethod->klass == mono_defaults.enum_class) {
8823 /* Enum implements some interfaces, so treat this as the first case */
8824 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8825 ins->klass = constrained_class;
8826 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8827 CHECK_CFG_EXCEPTION;
8832 constrained_class = NULL;
8835 if (check_call_signature (cfg, fsig, sp))
8838 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8839 delegate_invoke = TRUE;
8841 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8842 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8843 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8851 * If the callee is a shared method, then its static cctor
8852 * might not get called after the call was patched.
8854 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)) {
8855 emit_class_init (cfg, cmethod->klass);
8856 CHECK_TYPELOAD (cmethod->klass);
8859 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8862 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8864 context_used = mini_method_check_context_used (cfg, cmethod);
8866 if (context_used && mono_class_is_interface (cmethod->klass)) {
8867 /* Generic method interface
8868 calls are resolved via a
8869 helper function and don't
8871 if (!cmethod_context || !cmethod_context->method_inst)
8872 pass_imt_from_rgctx = TRUE;
8876 * If a shared method calls another
8877 * shared method then the caller must
8878 * have a generic sharing context
8879 * because the magic trampoline
8880 * requires it. FIXME: We shouldn't
8881 * have to force the vtable/mrgctx
8882 * variable here. Instead there
8883 * should be a flag in the cfg to
8884 * request a generic sharing context.
8887 ((cfg->method->flags & METHOD_ATTRIBUTE_STATIC) || cfg->method->klass->valuetype))
8888 mono_get_vtable_var (cfg);
8893 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8895 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8897 CHECK_TYPELOAD (cmethod->klass);
8898 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8903 g_assert (!vtable_arg);
8905 if (!cfg->compile_aot) {
8907 * emit_get_rgctx_method () calls mono_class_vtable () so check
8908 * for type load errors before.
8910 mono_class_setup_vtable (cmethod->klass);
8911 CHECK_TYPELOAD (cmethod->klass);
8914 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8916 /* !marshalbyref is needed to properly handle generic methods + remoting */
8917 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
8918 MONO_METHOD_IS_FINAL (cmethod)) &&
8919 !mono_class_is_marshalbyref (cmethod->klass)) {
8926 if (pass_imt_from_rgctx) {
8927 g_assert (!pass_vtable);
8929 imt_arg = emit_get_rgctx_method (cfg, context_used,
8930 cmethod, MONO_RGCTX_INFO_METHOD);
8934 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8936 /* Calling virtual generic methods */
8937 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
8938 !(MONO_METHOD_IS_FINAL (cmethod) &&
8939 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
8940 fsig->generic_param_count &&
8941 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
8943 MonoInst *this_temp, *this_arg_temp, *store;
8944 MonoInst *iargs [4];
8946 g_assert (fsig->is_inflated);
8948 /* Prevent inlining of methods that contain indirect calls */
8949 INLINE_FAILURE ("virtual generic call");
8951 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8952 GSHAREDVT_FAILURE (*ip);
8954 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
8955 g_assert (!imt_arg);
8957 g_assert (cmethod->is_inflated);
8958 imt_arg = emit_get_rgctx_method (cfg, context_used,
8959 cmethod, MONO_RGCTX_INFO_METHOD);
8960 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
8962 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
8963 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
8964 MONO_ADD_INS (cfg->cbb, store);
8966 /* FIXME: This should be a managed pointer */
8967 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8969 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
8970 iargs [1] = emit_get_rgctx_method (cfg, context_used,
8971 cmethod, MONO_RGCTX_INFO_METHOD);
8972 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
8973 addr = mono_emit_jit_icall (cfg,
8974 mono_helper_compile_generic_method, iargs);
8976 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
8978 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8985 * Implement a workaround for the inherent races involved in locking:
8991 * If a thread abort happens between the call to Monitor.Enter () and the start of the
8992 * try block, the Exit () won't be executed, see:
8993 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
8994 * To work around this, we extend such try blocks to include the last x bytes
8995 * of the Monitor.Enter () call.
8997 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8998 MonoBasicBlock *tbb;
9000 GET_BBLOCK (cfg, tbb, ip + 5);
9002 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9003 * from Monitor.Enter like ArgumentNullException.
9005 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9006 /* Mark this bblock as needing to be extended */
9007 tbb->extend_try_block = TRUE;
9011 /* Conversion to a JIT intrinsic */
9012 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9013 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9014 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9022 if ((cfg->opt & MONO_OPT_INLINE) &&
9023 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9024 mono_method_check_inlining (cfg, cmethod)) {
9026 gboolean always = FALSE;
9028 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9029 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9030 /* Prevent inlining of methods that call wrappers */
9031 INLINE_FAILURE ("wrapper call");
9032 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9036 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9038 cfg->real_offset += 5;
9040 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9041 /* *sp is already set by inline_method */
9046 inline_costs += costs;
9052 /* Tail recursion elimination */
9053 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9054 gboolean has_vtargs = FALSE;
9057 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9058 INLINE_FAILURE ("tail call");
9060 /* keep it simple */
9061 for (i = fsig->param_count - 1; i >= 0; i--) {
9062 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9067 if (need_seq_point) {
9068 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9069 need_seq_point = FALSE;
9071 for (i = 0; i < n; ++i)
9072 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9073 MONO_INST_NEW (cfg, ins, OP_BR);
9074 MONO_ADD_INS (cfg->cbb, ins);
9075 tblock = start_bblock->out_bb [0];
9076 link_bblock (cfg, cfg->cbb, tblock);
9077 ins->inst_target_bb = tblock;
9078 start_new_bblock = 1;
9080 /* skip the CEE_RET, too */
9081 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9088 inline_costs += 10 * num_calls++;
9091 * Synchronized wrappers.
9092 * Its hard to determine where to replace a method with its synchronized
9093 * wrapper without causing an infinite recursion. The current solution is
9094 * to add the synchronized wrapper in the trampolines, and to
9095 * change the called method to a dummy wrapper, and resolve that wrapper
9096 * to the real method in mono_jit_compile_method ().
9098 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9099 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9100 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9101 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9105 * Making generic calls out of gsharedvt methods.
9106 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9107 * patching gshared method addresses into a gsharedvt method.
9109 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
9110 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9111 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9112 MonoRgctxInfoType info_type;
9115 //if (mono_class_is_interface (cmethod->klass))
9116 //GSHAREDVT_FAILURE (*ip);
9117 // disable for possible remoting calls
9118 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9119 GSHAREDVT_FAILURE (*ip);
9120 if (fsig->generic_param_count) {
9121 /* virtual generic call */
9122 g_assert (!imt_arg);
9123 /* Same as the virtual generic case above */
9124 imt_arg = emit_get_rgctx_method (cfg, context_used,
9125 cmethod, MONO_RGCTX_INFO_METHOD);
9126 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9128 } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
9129 /* This can happen when we call a fully instantiated iface method */
9130 imt_arg = emit_get_rgctx_method (cfg, context_used,
9131 cmethod, MONO_RGCTX_INFO_METHOD);
9136 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9137 keep_this_alive = sp [0];
9139 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9140 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9142 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9143 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9145 if (cfg->llvm_only) {
9146 // FIXME: Avoid initializing vtable_arg
9147 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9149 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9154 /* Generic sharing */
9157 * Use this if the callee is gsharedvt sharable too, since
9158 * at runtime we might find an instantiation so the call cannot
9159 * be patched (the 'no_patch' code path in mini-trampolines.c).
9161 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9162 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9163 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9164 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9165 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9166 INLINE_FAILURE ("gshared");
9168 g_assert (cfg->gshared && cmethod);
9172 * We are compiling a call to a
9173 * generic method from shared code,
9174 * which means that we have to look up
9175 * the method in the rgctx and do an
9179 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9181 if (cfg->llvm_only) {
9182 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9183 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9185 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9186 // FIXME: Avoid initializing imt_arg/vtable_arg
9187 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9189 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9190 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9195 /* Direct calls to icalls */
9197 MonoMethod *wrapper;
9200 /* Inline the wrapper */
9201 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9203 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9204 g_assert (costs > 0);
9205 cfg->real_offset += 5;
9207 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9208 /* *sp is already set by inline_method */
9213 inline_costs += costs;
9222 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9223 MonoInst *val = sp [fsig->param_count];
9225 if (val->type == STACK_OBJ) {
9226 MonoInst *iargs [2];
9231 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9234 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9235 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9236 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
9237 emit_write_barrier (cfg, addr, val);
9238 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9239 GSHAREDVT_FAILURE (*ip);
9240 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9241 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9243 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9244 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9245 if (!cmethod->klass->element_class->valuetype && !readonly)
9246 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9247 CHECK_TYPELOAD (cmethod->klass);
9250 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9253 g_assert_not_reached ();
9260 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9264 /* Tail prefix / tail call optimization */
9266 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9267 /* FIXME: runtime generic context pointer for jumps? */
9268 /* FIXME: handle this for generic sharing eventually */
9269 if ((ins_flag & MONO_INST_TAILCALL) &&
9270 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9271 supported_tail_call = TRUE;
9273 if (supported_tail_call) {
9276 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9277 INLINE_FAILURE ("tail call");
9279 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9281 if (cfg->backend->have_op_tail_call) {
9282 /* Handle tail calls similarly to normal calls */
9285 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9287 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9288 call->tail_call = TRUE;
9289 call->method = cmethod;
9290 call->signature = mono_method_signature (cmethod);
9293 * We implement tail calls by storing the actual arguments into the
9294 * argument variables, then emitting a CEE_JMP.
9296 for (i = 0; i < n; ++i) {
9297 /* Prevent argument from being register allocated */
9298 arg_array [i]->flags |= MONO_INST_VOLATILE;
9299 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9301 ins = (MonoInst*)call;
9302 ins->inst_p0 = cmethod;
9303 ins->inst_p1 = arg_array [0];
9304 MONO_ADD_INS (cfg->cbb, ins);
9305 link_bblock (cfg, cfg->cbb, end_bblock);
9306 start_new_bblock = 1;
9308 // FIXME: Eliminate unreachable epilogs
9311 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9312 * only reachable from this call.
9314 GET_BBLOCK (cfg, tblock, ip + 5);
9315 if (tblock == cfg->cbb || tblock->in_count == 0)
9324 * Virtual calls in llvm-only mode.
9326 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9327 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9332 if (!(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
9333 INLINE_FAILURE ("call");
9334 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9335 imt_arg, vtable_arg);
9337 if (tail_call && !cfg->llvm_only) {
9338 link_bblock (cfg, cfg->cbb, end_bblock);
9339 start_new_bblock = 1;
9341 // FIXME: Eliminate unreachable epilogs
9344 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9345 * only reachable from this call.
9347 GET_BBLOCK (cfg, tblock, ip + 5);
9348 if (tblock == cfg->cbb || tblock->in_count == 0)
9355 /* End of call, INS should contain the result of the call, if any */
9357 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9360 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9365 if (keep_this_alive) {
9366 MonoInst *dummy_use;
9368 /* See mono_emit_method_call_full () */
9369 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9372 if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
9374 * Clang can convert these calls to tail calls which screw up the stack
9375 * walk. This happens even when the -fno-optimize-sibling-calls
9376 * option is passed to clang.
9377 * Work around this by emitting a dummy call.
9379 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
9382 CHECK_CFG_EXCEPTION;
9386 g_assert (*ip == CEE_RET);
9390 constrained_class = NULL;
9392 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9396 if (cfg->method != method) {
9397 /* return from inlined method */
9399 * If in_count == 0, that means the ret is unreachable due to
9400 * being preceeded by a throw. In that case, inline_method () will
9401 * handle setting the return value
9402 * (test case: test_0_inline_throw ()).
9404 if (return_var && cfg->cbb->in_count) {
9405 MonoType *ret_type = mono_method_signature (method)->ret;
9411 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9414 //g_assert (returnvar != -1);
9415 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9416 cfg->ret_var_set = TRUE;
9419 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9421 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
9425 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9427 if (seq_points && !sym_seq_points) {
9429 * Place a seq point here too even through the IL stack is not
9430 * empty, so a step over on
9433 * will work correctly.
9435 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9436 MONO_ADD_INS (cfg->cbb, ins);
9439 g_assert (!return_var);
9443 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9446 emit_setret (cfg, *sp);
9449 if (sp != stack_start)
9451 MONO_INST_NEW (cfg, ins, OP_BR);
9453 ins->inst_target_bb = end_bblock;
9454 MONO_ADD_INS (cfg->cbb, ins);
9455 link_bblock (cfg, cfg->cbb, end_bblock);
9456 start_new_bblock = 1;
9460 MONO_INST_NEW (cfg, ins, OP_BR);
9462 target = ip + 1 + (signed char)(*ip);
9464 GET_BBLOCK (cfg, tblock, target);
9465 link_bblock (cfg, cfg->cbb, tblock);
9466 ins->inst_target_bb = tblock;
9467 if (sp != stack_start) {
9468 handle_stack_args (cfg, stack_start, sp - stack_start);
9470 CHECK_UNVERIFIABLE (cfg);
9472 MONO_ADD_INS (cfg->cbb, ins);
9473 start_new_bblock = 1;
9474 inline_costs += BRANCH_COST;
9488 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9490 target = ip + 1 + *(signed char*)ip;
9496 inline_costs += BRANCH_COST;
9500 MONO_INST_NEW (cfg, ins, OP_BR);
9503 target = ip + 4 + (gint32)read32(ip);
9505 GET_BBLOCK (cfg, tblock, target);
9506 link_bblock (cfg, cfg->cbb, tblock);
9507 ins->inst_target_bb = tblock;
9508 if (sp != stack_start) {
9509 handle_stack_args (cfg, stack_start, sp - stack_start);
9511 CHECK_UNVERIFIABLE (cfg);
9514 MONO_ADD_INS (cfg->cbb, ins);
9516 start_new_bblock = 1;
9517 inline_costs += BRANCH_COST;
9524 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9525 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9526 guint32 opsize = is_short ? 1 : 4;
9528 CHECK_OPSIZE (opsize);
9530 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9533 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9538 GET_BBLOCK (cfg, tblock, target);
9539 link_bblock (cfg, cfg->cbb, tblock);
9540 GET_BBLOCK (cfg, tblock, ip);
9541 link_bblock (cfg, cfg->cbb, tblock);
9543 if (sp != stack_start) {
9544 handle_stack_args (cfg, stack_start, sp - stack_start);
9545 CHECK_UNVERIFIABLE (cfg);
9548 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9549 cmp->sreg1 = sp [0]->dreg;
9550 type_from_op (cfg, cmp, sp [0], NULL);
9553 #if SIZEOF_REGISTER == 4
9554 if (cmp->opcode == OP_LCOMPARE_IMM) {
9555 /* Convert it to OP_LCOMPARE */
9556 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9557 ins->type = STACK_I8;
9558 ins->dreg = alloc_dreg (cfg, STACK_I8);
9560 MONO_ADD_INS (cfg->cbb, ins);
9561 cmp->opcode = OP_LCOMPARE;
9562 cmp->sreg2 = ins->dreg;
9565 MONO_ADD_INS (cfg->cbb, cmp);
9567 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9568 type_from_op (cfg, ins, sp [0], NULL);
9569 MONO_ADD_INS (cfg->cbb, ins);
9570 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9571 GET_BBLOCK (cfg, tblock, target);
9572 ins->inst_true_bb = tblock;
9573 GET_BBLOCK (cfg, tblock, ip);
9574 ins->inst_false_bb = tblock;
9575 start_new_bblock = 2;
9578 inline_costs += BRANCH_COST;
9593 MONO_INST_NEW (cfg, ins, *ip);
9595 target = ip + 4 + (gint32)read32(ip);
9601 inline_costs += BRANCH_COST;
9605 MonoBasicBlock **targets;
9606 MonoBasicBlock *default_bblock;
9607 MonoJumpInfoBBTable *table;
9608 int offset_reg = alloc_preg (cfg);
9609 int target_reg = alloc_preg (cfg);
9610 int table_reg = alloc_preg (cfg);
9611 int sum_reg = alloc_preg (cfg);
9612 gboolean use_op_switch;
9616 n = read32 (ip + 1);
9619 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
9623 CHECK_OPSIZE (n * sizeof (guint32));
9624 target = ip + n * sizeof (guint32);
9626 GET_BBLOCK (cfg, default_bblock, target);
9627 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9629 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9630 for (i = 0; i < n; ++i) {
9631 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9632 targets [i] = tblock;
9633 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9637 if (sp != stack_start) {
9639 * Link the current bb with the targets as well, so handle_stack_args
9640 * will set their in_stack correctly.
9642 link_bblock (cfg, cfg->cbb, default_bblock);
9643 for (i = 0; i < n; ++i)
9644 link_bblock (cfg, cfg->cbb, targets [i]);
9646 handle_stack_args (cfg, stack_start, sp - stack_start);
9648 CHECK_UNVERIFIABLE (cfg);
9650 /* Undo the links */
9651 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
9652 for (i = 0; i < n; ++i)
9653 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
9656 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9657 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9659 for (i = 0; i < n; ++i)
9660 link_bblock (cfg, cfg->cbb, targets [i]);
9662 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9663 table->table = targets;
9664 table->table_size = n;
9666 use_op_switch = FALSE;
9668 /* ARM implements SWITCH statements differently */
9669 /* FIXME: Make it use the generic implementation */
9670 if (!cfg->compile_aot)
9671 use_op_switch = TRUE;
9674 if (COMPILE_LLVM (cfg))
9675 use_op_switch = TRUE;
9677 cfg->cbb->has_jump_table = 1;
9679 if (use_op_switch) {
9680 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9681 ins->sreg1 = src1->dreg;
9682 ins->inst_p0 = table;
9683 ins->inst_many_bb = targets;
9684 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
9685 MONO_ADD_INS (cfg->cbb, ins);
9687 if (sizeof (gpointer) == 8)
9688 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9690 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9692 #if SIZEOF_REGISTER == 8
9693 /* The upper word might not be zero, and we add it to a 64 bit address later */
9694 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9697 if (cfg->compile_aot) {
9698 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9700 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9701 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9702 ins->inst_p0 = table;
9703 ins->dreg = table_reg;
9704 MONO_ADD_INS (cfg->cbb, ins);
9707 /* FIXME: Use load_memindex */
9708 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9709 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9710 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9712 start_new_bblock = 1;
9713 inline_costs += (BRANCH_COST * 2);
9733 dreg = alloc_freg (cfg);
9736 dreg = alloc_lreg (cfg);
9739 dreg = alloc_ireg_ref (cfg);
9742 dreg = alloc_preg (cfg);
9745 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
9746 ins->type = ldind_type [*ip - CEE_LDIND_I1];
9747 if (*ip == CEE_LDIND_R4)
9748 ins->type = cfg->r4_stack_type;
9749 ins->flags |= ins_flag;
9750 MONO_ADD_INS (cfg->cbb, ins);
9752 if (ins_flag & MONO_INST_VOLATILE) {
9753 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9754 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9770 if (ins_flag & MONO_INST_VOLATILE) {
9771 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9772 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9775 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9776 ins->flags |= ins_flag;
9779 MONO_ADD_INS (cfg->cbb, ins);
9781 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
9782 emit_write_barrier (cfg, sp [0], sp [1]);
9791 MONO_INST_NEW (cfg, ins, (*ip));
9793 ins->sreg1 = sp [0]->dreg;
9794 ins->sreg2 = sp [1]->dreg;
9795 type_from_op (cfg, ins, sp [0], sp [1]);
9797 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9799 /* Use the immediate opcodes if possible */
9800 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9801 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9802 if (imm_opcode != -1) {
9803 ins->opcode = imm_opcode;
9804 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9807 NULLIFY_INS (sp [1]);
9811 MONO_ADD_INS ((cfg)->cbb, (ins));
9813 *sp++ = mono_decompose_opcode (cfg, ins);
9830 MONO_INST_NEW (cfg, ins, (*ip));
9832 ins->sreg1 = sp [0]->dreg;
9833 ins->sreg2 = sp [1]->dreg;
9834 type_from_op (cfg, ins, sp [0], sp [1]);
9836 add_widen_op (cfg, ins, &sp [0], &sp [1]);
9837 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9839 /* FIXME: Pass opcode to is_inst_imm */
9841 /* Use the immediate opcodes if possible */
9842 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)) {
9843 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9844 if (imm_opcode != -1) {
9845 ins->opcode = imm_opcode;
9846 if (sp [1]->opcode == OP_I8CONST) {
9847 #if SIZEOF_REGISTER == 8
9848 ins->inst_imm = sp [1]->inst_l;
9850 ins->inst_ls_word = sp [1]->inst_ls_word;
9851 ins->inst_ms_word = sp [1]->inst_ms_word;
9855 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9858 /* Might be followed by an instruction added by add_widen_op */
9859 if (sp [1]->next == NULL)
9860 NULLIFY_INS (sp [1]);
9863 MONO_ADD_INS ((cfg)->cbb, (ins));
9865 *sp++ = mono_decompose_opcode (cfg, ins);
9878 case CEE_CONV_OVF_I8:
9879 case CEE_CONV_OVF_U8:
9883 /* Special case this earlier so we have long constants in the IR */
9884 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
9885 int data = sp [-1]->inst_c0;
9886 sp [-1]->opcode = OP_I8CONST;
9887 sp [-1]->type = STACK_I8;
9888 #if SIZEOF_REGISTER == 8
9889 if ((*ip) == CEE_CONV_U8)
9890 sp [-1]->inst_c0 = (guint32)data;
9892 sp [-1]->inst_c0 = data;
9894 sp [-1]->inst_ls_word = data;
9895 if ((*ip) == CEE_CONV_U8)
9896 sp [-1]->inst_ms_word = 0;
9898 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
9900 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
9907 case CEE_CONV_OVF_I4:
9908 case CEE_CONV_OVF_I1:
9909 case CEE_CONV_OVF_I2:
9910 case CEE_CONV_OVF_I:
9911 case CEE_CONV_OVF_U:
9914 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9915 ADD_UNOP (CEE_CONV_OVF_I8);
9922 case CEE_CONV_OVF_U1:
9923 case CEE_CONV_OVF_U2:
9924 case CEE_CONV_OVF_U4:
9927 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9928 ADD_UNOP (CEE_CONV_OVF_U8);
9935 case CEE_CONV_OVF_I1_UN:
9936 case CEE_CONV_OVF_I2_UN:
9937 case CEE_CONV_OVF_I4_UN:
9938 case CEE_CONV_OVF_I8_UN:
9939 case CEE_CONV_OVF_U1_UN:
9940 case CEE_CONV_OVF_U2_UN:
9941 case CEE_CONV_OVF_U4_UN:
9942 case CEE_CONV_OVF_U8_UN:
9943 case CEE_CONV_OVF_I_UN:
9944 case CEE_CONV_OVF_U_UN:
9951 CHECK_CFG_EXCEPTION;
9955 case CEE_ADD_OVF_UN:
9957 case CEE_MUL_OVF_UN:
9959 case CEE_SUB_OVF_UN:
9965 GSHAREDVT_FAILURE (*ip);
9968 token = read32 (ip + 1);
9969 klass = mini_get_class (method, token, generic_context);
9970 CHECK_TYPELOAD (klass);
9972 if (generic_class_is_reference_type (cfg, klass)) {
9973 MonoInst *store, *load;
9974 int dreg = alloc_ireg_ref (cfg);
9976 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
9977 load->flags |= ins_flag;
9978 MONO_ADD_INS (cfg->cbb, load);
9980 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
9981 store->flags |= ins_flag;
9982 MONO_ADD_INS (cfg->cbb, store);
9984 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
9985 emit_write_barrier (cfg, sp [0], sp [1]);
9987 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
9999 token = read32 (ip + 1);
10000 klass = mini_get_class (method, token, generic_context);
10001 CHECK_TYPELOAD (klass);
10003 /* Optimize the common ldobj+stloc combination */
10006 loc_index = ip [6];
10013 loc_index = ip [5] - CEE_STLOC_0;
10020 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10021 CHECK_LOCAL (loc_index);
10023 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10024 ins->dreg = cfg->locals [loc_index]->dreg;
10025 ins->flags |= ins_flag;
10028 if (ins_flag & MONO_INST_VOLATILE) {
10029 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10030 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10036 /* Optimize the ldobj+stobj combination */
10037 /* The reference case ends up being a load+store anyway */
10038 /* Skip this if the operation is volatile. */
10039 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)) {
10044 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10051 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10052 ins->flags |= ins_flag;
10055 if (ins_flag & MONO_INST_VOLATILE) {
10056 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10057 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10066 CHECK_STACK_OVF (1);
10068 n = read32 (ip + 1);
10070 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10071 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10072 ins->type = STACK_OBJ;
10075 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10076 MonoInst *iargs [1];
10077 char *str = (char *)mono_method_get_wrapper_data (method, n);
10079 if (cfg->compile_aot)
10080 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10082 EMIT_NEW_PCONST (cfg, iargs [0], str);
10083 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10085 if (cfg->opt & MONO_OPT_SHARED) {
10086 MonoInst *iargs [3];
10088 if (cfg->compile_aot) {
10089 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10091 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10092 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10093 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10094 *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
10095 mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10098 if (cfg->cbb->out_of_line) {
10099 MonoInst *iargs [2];
10101 if (image == mono_defaults.corlib) {
10103 * Avoid relocations in AOT and save some space by using a
10104 * version of helper_ldstr specialized to mscorlib.
10106 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10107 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10109 /* Avoid creating the string object */
10110 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10111 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10112 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10116 if (cfg->compile_aot) {
10117 NEW_LDSTRCONST (cfg, ins, image, n);
10119 MONO_ADD_INS (cfg->cbb, ins);
10122 NEW_PCONST (cfg, ins, NULL);
10123 ins->type = STACK_OBJ;
10124 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10128 OUT_OF_MEMORY_FAILURE;
10131 MONO_ADD_INS (cfg->cbb, ins);
10140 MonoInst *iargs [2];
10141 MonoMethodSignature *fsig;
10144 MonoInst *vtable_arg = NULL;
10147 token = read32 (ip + 1);
10148 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10151 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10154 mono_save_token_info (cfg, image, token, cmethod);
10156 if (!mono_class_init (cmethod->klass))
10157 TYPE_LOAD_ERROR (cmethod->klass);
10159 context_used = mini_method_check_context_used (cfg, cmethod);
10161 if (mono_security_core_clr_enabled ())
10162 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10164 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)) {
10165 emit_class_init (cfg, cmethod->klass);
10166 CHECK_TYPELOAD (cmethod->klass);
10170 if (cfg->gsharedvt) {
10171 if (mini_is_gsharedvt_variable_signature (sig))
10172 GSHAREDVT_FAILURE (*ip);
10176 n = fsig->param_count;
10180 * Generate smaller code for the common newobj <exception> instruction in
10181 * argument checking code.
10183 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10184 is_exception_class (cmethod->klass) && n <= 2 &&
10185 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10186 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10187 MonoInst *iargs [3];
10191 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10194 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10197 iargs [1] = sp [0];
10198 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10201 iargs [1] = sp [0];
10202 iargs [2] = sp [1];
10203 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10206 g_assert_not_reached ();
10214 /* move the args to allow room for 'this' in the first position */
10220 /* check_call_signature () requires sp[0] to be set */
10221 this_ins.type = STACK_OBJ;
10222 sp [0] = &this_ins;
10223 if (check_call_signature (cfg, fsig, sp))
10228 if (mini_class_is_system_array (cmethod->klass)) {
10229 *sp = emit_get_rgctx_method (cfg, context_used,
10230 cmethod, MONO_RGCTX_INFO_METHOD);
10232 /* Avoid varargs in the common case */
10233 if (fsig->param_count == 1)
10234 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10235 else if (fsig->param_count == 2)
10236 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10237 else if (fsig->param_count == 3)
10238 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10239 else if (fsig->param_count == 4)
10240 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10242 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10243 } else if (cmethod->string_ctor) {
10244 g_assert (!context_used);
10245 g_assert (!vtable_arg);
10246 /* we simply pass a null pointer */
10247 EMIT_NEW_PCONST (cfg, *sp, NULL);
10248 /* now call the string ctor */
10249 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10251 if (cmethod->klass->valuetype) {
10252 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10253 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10254 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10259 * The code generated by mini_emit_virtual_call () expects
10260 * iargs [0] to be a boxed instance, but luckily the vcall
10261 * will be transformed into a normal call there.
10263 } else if (context_used) {
10264 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10267 MonoVTable *vtable = NULL;
10269 if (!cfg->compile_aot)
10270 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10271 CHECK_TYPELOAD (cmethod->klass);
10274 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10275 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10276 * As a workaround, we call class cctors before allocating objects.
10278 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10279 emit_class_init (cfg, cmethod->klass);
10280 if (cfg->verbose_level > 2)
10281 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10282 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10285 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10288 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10291 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10293 /* Now call the actual ctor */
10294 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10295 CHECK_CFG_EXCEPTION;
10298 if (alloc == NULL) {
10300 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10301 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10309 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10310 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10313 case CEE_CASTCLASS:
10318 token = read32 (ip + 1);
10319 klass = mini_get_class (method, token, generic_context);
10320 CHECK_TYPELOAD (klass);
10321 if (sp [0]->type != STACK_OBJ)
10324 MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
10325 ins->dreg = alloc_preg (cfg);
10326 ins->sreg1 = (*sp)->dreg;
10327 ins->klass = klass;
10328 ins->type = STACK_OBJ;
10329 MONO_ADD_INS (cfg->cbb, ins);
10331 CHECK_CFG_EXCEPTION;
10335 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10338 case CEE_UNBOX_ANY: {
10339 MonoInst *res, *addr;
10344 token = read32 (ip + 1);
10345 klass = mini_get_class (method, token, generic_context);
10346 CHECK_TYPELOAD (klass);
10348 mono_save_token_info (cfg, image, token, klass);
10350 context_used = mini_class_check_context_used (cfg, klass);
10352 if (mini_is_gsharedvt_klass (klass)) {
10353 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10355 } else if (generic_class_is_reference_type (cfg, klass)) {
10356 if (MONO_INS_IS_PCONST_NULL (*sp)) {
10357 EMIT_NEW_PCONST (cfg, res, NULL);
10358 res->type = STACK_OBJ;
10360 MONO_INST_NEW (cfg, res, OP_CASTCLASS);
10361 res->dreg = alloc_preg (cfg);
10362 res->sreg1 = (*sp)->dreg;
10363 res->klass = klass;
10364 res->type = STACK_OBJ;
10365 MONO_ADD_INS (cfg->cbb, res);
10366 cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10368 } else if (mono_class_is_nullable (klass)) {
10369 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10371 addr = handle_unbox (cfg, klass, sp, context_used);
10373 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10384 MonoClass *enum_class;
10385 MonoMethod *has_flag;
10391 token = read32 (ip + 1);
10392 klass = mini_get_class (method, token, generic_context);
10393 CHECK_TYPELOAD (klass);
10395 mono_save_token_info (cfg, image, token, klass);
10397 context_used = mini_class_check_context_used (cfg, klass);
10399 if (generic_class_is_reference_type (cfg, klass)) {
10405 if (klass == mono_defaults.void_class)
10407 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10409 /* frequent check in generic code: box (struct), brtrue */
10414 * <push int/long ptr>
10417 * constrained. MyFlags
10418 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10420 * If we find this sequence and the operand types on box and constrained
10421 * are equal, we can emit a specialized instruction sequence instead of
10422 * the very slow HasFlag () call.
10424 if ((cfg->opt & MONO_OPT_INTRINS) &&
10425 /* Cheap checks first. */
10426 ip + 5 + 6 + 5 < end &&
10427 ip [5] == CEE_PREFIX1 &&
10428 ip [6] == CEE_CONSTRAINED_ &&
10429 ip [11] == CEE_CALLVIRT &&
10430 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10431 mono_class_is_enum (klass) &&
10432 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10433 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10434 has_flag->klass == mono_defaults.enum_class &&
10435 !strcmp (has_flag->name, "HasFlag") &&
10436 has_flag->signature->hasthis &&
10437 has_flag->signature->param_count == 1) {
10438 CHECK_TYPELOAD (enum_class);
10440 if (enum_class == klass) {
10441 MonoInst *enum_this, *enum_flag;
10446 enum_this = sp [0];
10447 enum_flag = sp [1];
10449 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10454 // FIXME: LLVM can't handle the inconsistent bb linking
10455 if (!mono_class_is_nullable (klass) &&
10456 !mini_is_gsharedvt_klass (klass) &&
10457 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10458 (ip [5] == CEE_BRTRUE ||
10459 ip [5] == CEE_BRTRUE_S ||
10460 ip [5] == CEE_BRFALSE ||
10461 ip [5] == CEE_BRFALSE_S)) {
10462 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10464 MonoBasicBlock *true_bb, *false_bb;
10468 if (cfg->verbose_level > 3) {
10469 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10470 printf ("<box+brtrue opt>\n");
10475 case CEE_BRFALSE_S:
10478 target = ip + 1 + (signed char)(*ip);
10485 target = ip + 4 + (gint)(read32 (ip));
10489 g_assert_not_reached ();
10493 * We need to link both bblocks, since it is needed for handling stack
10494 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10495 * Branching to only one of them would lead to inconsistencies, so
10496 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10498 GET_BBLOCK (cfg, true_bb, target);
10499 GET_BBLOCK (cfg, false_bb, ip);
10501 mono_link_bblock (cfg, cfg->cbb, true_bb);
10502 mono_link_bblock (cfg, cfg->cbb, false_bb);
10504 if (sp != stack_start) {
10505 handle_stack_args (cfg, stack_start, sp - stack_start);
10507 CHECK_UNVERIFIABLE (cfg);
10510 if (COMPILE_LLVM (cfg)) {
10511 dreg = alloc_ireg (cfg);
10512 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10513 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10515 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10517 /* The JIT can't eliminate the iconst+compare */
10518 MONO_INST_NEW (cfg, ins, OP_BR);
10519 ins->inst_target_bb = is_true ? true_bb : false_bb;
10520 MONO_ADD_INS (cfg->cbb, ins);
10523 start_new_bblock = 1;
10527 *sp++ = handle_box (cfg, val, klass, context_used);
10529 CHECK_CFG_EXCEPTION;
10538 token = read32 (ip + 1);
10539 klass = mini_get_class (method, token, generic_context);
10540 CHECK_TYPELOAD (klass);
10542 mono_save_token_info (cfg, image, token, klass);
10544 context_used = mini_class_check_context_used (cfg, klass);
10546 if (mono_class_is_nullable (klass)) {
10549 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10550 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10554 ins = handle_unbox (cfg, klass, sp, context_used);
10567 MonoClassField *field;
10568 #ifndef DISABLE_REMOTING
10572 gboolean is_instance;
10574 gpointer addr = NULL;
10575 gboolean is_special_static;
10577 MonoInst *store_val = NULL;
10578 MonoInst *thread_ins;
10581 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10583 if (op == CEE_STFLD) {
10586 store_val = sp [1];
10591 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10593 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10596 if (op == CEE_STSFLD) {
10599 store_val = sp [0];
10604 token = read32 (ip + 1);
10605 if (method->wrapper_type != MONO_WRAPPER_NONE) {
10606 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
10607 klass = field->parent;
10610 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10613 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10614 FIELD_ACCESS_FAILURE (method, field);
10615 mono_class_init (klass);
10617 /* if the class is Critical then transparent code cannot access it's fields */
10618 if (!is_instance && mono_security_core_clr_enabled ())
10619 ensure_method_is_allowed_to_access_field (cfg, method, field);
10621 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10622 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
10623 if (mono_security_core_clr_enabled ())
10624 ensure_method_is_allowed_to_access_field (cfg, method, field);
10627 ftype = mono_field_get_type (field);
10630 * LDFLD etc. is usable on static fields as well, so convert those cases to
10633 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10645 g_assert_not_reached ();
10647 is_instance = FALSE;
10650 context_used = mini_class_check_context_used (cfg, klass);
10652 /* INSTANCE CASE */
10654 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10655 if (op == CEE_STFLD) {
10656 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10658 #ifndef DISABLE_REMOTING
10659 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10660 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
10661 MonoInst *iargs [5];
10663 GSHAREDVT_FAILURE (op);
10665 iargs [0] = sp [0];
10666 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10667 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10668 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
10670 iargs [4] = sp [1];
10672 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10673 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
10674 iargs, ip, cfg->real_offset, TRUE);
10675 CHECK_CFG_EXCEPTION;
10676 g_assert (costs > 0);
10678 cfg->real_offset += 5;
10680 inline_costs += costs;
10682 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10687 MonoInst *store, *wbarrier_ptr_ins = NULL;
10689 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10691 if (ins_flag & MONO_INST_VOLATILE) {
10692 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10693 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10696 if (mini_is_gsharedvt_klass (klass)) {
10697 MonoInst *offset_ins;
10699 context_used = mini_class_check_context_used (cfg, klass);
10701 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10702 /* The value is offset by 1 */
10703 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10704 dreg = alloc_ireg_mp (cfg);
10705 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10706 wbarrier_ptr_ins = ins;
10707 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
10708 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10710 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10712 if (sp [0]->opcode != OP_LDADDR)
10713 store->flags |= MONO_INST_FAULT;
10715 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
10716 if (mini_is_gsharedvt_klass (klass)) {
10717 g_assert (wbarrier_ptr_ins);
10718 emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
10720 /* insert call to write barrier */
10724 dreg = alloc_ireg_mp (cfg);
10725 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10726 emit_write_barrier (cfg, ptr, sp [1]);
10730 store->flags |= ins_flag;
10737 #ifndef DISABLE_REMOTING
10738 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10739 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
10740 MonoInst *iargs [4];
10742 GSHAREDVT_FAILURE (op);
10744 iargs [0] = sp [0];
10745 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10746 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10747 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10748 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10749 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
10750 iargs, ip, cfg->real_offset, TRUE);
10751 CHECK_CFG_EXCEPTION;
10752 g_assert (costs > 0);
10754 cfg->real_offset += 5;
10758 inline_costs += costs;
10760 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10766 if (sp [0]->type == STACK_VTYPE) {
10769 /* Have to compute the address of the variable */
10771 var = get_vreg_to_inst (cfg, sp [0]->dreg);
10773 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10775 g_assert (var->klass == klass);
10777 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10781 if (op == CEE_LDFLDA) {
10782 if (sp [0]->type == STACK_OBJ) {
10783 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10784 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10787 dreg = alloc_ireg_mp (cfg);
10789 if (mini_is_gsharedvt_klass (klass)) {
10790 MonoInst *offset_ins;
10792 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10793 /* The value is offset by 1 */
10794 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10795 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10797 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10799 ins->klass = mono_class_from_mono_type (field->type);
10800 ins->type = STACK_MP;
10805 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10807 if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
10808 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
10817 if (mini_is_gsharedvt_klass (klass)) {
10818 MonoInst *offset_ins;
10820 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10821 /* The value is offset by 1 */
10822 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10823 dreg = alloc_ireg_mp (cfg);
10824 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10825 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
10827 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
10829 load->flags |= ins_flag;
10830 if (sp [0]->opcode != OP_LDADDR)
10831 load->flags |= MONO_INST_FAULT;
10843 context_used = mini_class_check_context_used (cfg, klass);
10845 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
10846 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
10850 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10851 * to be called here.
10853 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10854 mono_class_vtable (cfg->domain, klass);
10855 CHECK_TYPELOAD (klass);
10857 mono_domain_lock (cfg->domain);
10858 if (cfg->domain->special_static_fields)
10859 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10860 mono_domain_unlock (cfg->domain);
10862 is_special_static = mono_class_field_is_special_static (field);
10864 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10865 thread_ins = mono_create_tls_get (cfg, TLS_KEY_THREAD);
10869 /* Generate IR to compute the field address */
10870 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
10872 * Fast access to TLS data
10873 * Inline version of get_thread_static_data () in
10877 int idx, static_data_reg, array_reg, dreg;
10879 if (context_used && cfg->gsharedvt && mini_is_gsharedvt_klass (klass))
10880 GSHAREDVT_FAILURE (op);
10882 static_data_reg = alloc_ireg (cfg);
10883 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
10885 if (cfg->compile_aot) {
10886 int offset_reg, offset2_reg, idx_reg;
10888 /* For TLS variables, this will return the TLS offset */
10889 EMIT_NEW_SFLDACONST (cfg, ins, field);
10890 offset_reg = ins->dreg;
10891 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
10892 idx_reg = alloc_ireg (cfg);
10893 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
10894 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
10895 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
10896 array_reg = alloc_ireg (cfg);
10897 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
10898 offset2_reg = alloc_ireg (cfg);
10899 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
10900 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
10901 dreg = alloc_ireg (cfg);
10902 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
10904 offset = (gsize)addr & 0x7fffffff;
10905 idx = offset & 0x3f;
10907 array_reg = alloc_ireg (cfg);
10908 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
10909 dreg = alloc_ireg (cfg);
10910 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
10912 } else if ((cfg->opt & MONO_OPT_SHARED) ||
10913 (cfg->compile_aot && is_special_static) ||
10914 (context_used && is_special_static)) {
10915 MonoInst *iargs [2];
10917 g_assert (field->parent);
10918 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10919 if (context_used) {
10920 iargs [1] = emit_get_rgctx_field (cfg, context_used,
10921 field, MONO_RGCTX_INFO_CLASS_FIELD);
10923 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10925 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10926 } else if (context_used) {
10927 MonoInst *static_data;
10930 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
10931 method->klass->name_space, method->klass->name, method->name,
10932 depth, field->offset);
10935 if (mono_class_needs_cctor_run (klass, method))
10936 emit_class_init (cfg, klass);
10939 * The pointer we're computing here is
10941 * super_info.static_data + field->offset
10943 static_data = mini_emit_get_rgctx_klass (cfg, context_used,
10944 klass, MONO_RGCTX_INFO_STATIC_DATA);
10946 if (mini_is_gsharedvt_klass (klass)) {
10947 MonoInst *offset_ins;
10949 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10950 /* The value is offset by 1 */
10951 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10952 dreg = alloc_ireg_mp (cfg);
10953 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
10954 } else if (field->offset == 0) {
10957 int addr_reg = mono_alloc_preg (cfg);
10958 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
10960 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
10961 MonoInst *iargs [2];
10963 g_assert (field->parent);
10964 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10965 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10966 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10968 MonoVTable *vtable = NULL;
10970 if (!cfg->compile_aot)
10971 vtable = mono_class_vtable (cfg->domain, klass);
10972 CHECK_TYPELOAD (klass);
10975 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
10976 if (!(g_slist_find (class_inits, klass))) {
10977 emit_class_init (cfg, klass);
10978 if (cfg->verbose_level > 2)
10979 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
10980 class_inits = g_slist_prepend (class_inits, klass);
10983 if (cfg->run_cctors) {
10984 /* This makes so that inline cannot trigger */
10985 /* .cctors: too many apps depend on them */
10986 /* running with a specific order... */
10988 if (! vtable->initialized)
10989 INLINE_FAILURE ("class init");
10990 if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
10991 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
10992 goto exception_exit;
10996 if (cfg->compile_aot)
10997 EMIT_NEW_SFLDACONST (cfg, ins, field);
11000 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11002 EMIT_NEW_PCONST (cfg, ins, addr);
11005 MonoInst *iargs [1];
11006 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11007 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11011 /* Generate IR to do the actual load/store operation */
11013 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11014 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11015 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11018 if (op == CEE_LDSFLDA) {
11019 ins->klass = mono_class_from_mono_type (ftype);
11020 ins->type = STACK_PTR;
11022 } else if (op == CEE_STSFLD) {
11025 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11026 store->flags |= ins_flag;
11028 gboolean is_const = FALSE;
11029 MonoVTable *vtable = NULL;
11030 gpointer addr = NULL;
11032 if (!context_used) {
11033 vtable = mono_class_vtable (cfg->domain, klass);
11034 CHECK_TYPELOAD (klass);
11036 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11037 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11038 int ro_type = ftype->type;
11040 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11041 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11042 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11045 GSHAREDVT_FAILURE (op);
11047 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11050 case MONO_TYPE_BOOLEAN:
11052 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11056 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11059 case MONO_TYPE_CHAR:
11061 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11065 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11070 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11074 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11079 case MONO_TYPE_PTR:
11080 case MONO_TYPE_FNPTR:
11081 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11082 type_to_eval_stack_type ((cfg), field->type, *sp);
11085 case MONO_TYPE_STRING:
11086 case MONO_TYPE_OBJECT:
11087 case MONO_TYPE_CLASS:
11088 case MONO_TYPE_SZARRAY:
11089 case MONO_TYPE_ARRAY:
11090 if (!mono_gc_is_moving ()) {
11091 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11092 type_to_eval_stack_type ((cfg), field->type, *sp);
11100 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11105 case MONO_TYPE_VALUETYPE:
11115 CHECK_STACK_OVF (1);
11117 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11118 load->flags |= ins_flag;
11124 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11125 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11126 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11137 token = read32 (ip + 1);
11138 klass = mini_get_class (method, token, generic_context);
11139 CHECK_TYPELOAD (klass);
11140 if (ins_flag & MONO_INST_VOLATILE) {
11141 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11142 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11144 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11145 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11146 ins->flags |= ins_flag;
11147 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11148 generic_class_is_reference_type (cfg, klass) && !MONO_INS_IS_PCONST_NULL (sp [1])) {
11149 /* insert call to write barrier */
11150 emit_write_barrier (cfg, sp [0], sp [1]);
11162 const char *data_ptr;
11164 guint32 field_token;
11170 token = read32 (ip + 1);
11172 klass = mini_get_class (method, token, generic_context);
11173 CHECK_TYPELOAD (klass);
11175 context_used = mini_class_check_context_used (cfg, klass);
11177 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11178 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11179 ins->sreg1 = sp [0]->dreg;
11180 ins->type = STACK_I4;
11181 ins->dreg = alloc_ireg (cfg);
11182 MONO_ADD_INS (cfg->cbb, ins);
11183 *sp = mono_decompose_opcode (cfg, ins);
11186 if (context_used) {
11187 MonoInst *args [3];
11188 MonoClass *array_class = mono_array_class_get (klass, 1);
11189 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11191 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11194 args [0] = mini_emit_get_rgctx_klass (cfg, context_used,
11195 array_class, MONO_RGCTX_INFO_VTABLE);
11200 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11202 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11204 if (cfg->opt & MONO_OPT_SHARED) {
11205 /* Decompose now to avoid problems with references to the domainvar */
11206 MonoInst *iargs [3];
11208 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11209 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11210 iargs [2] = sp [0];
11212 ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
11214 /* Decompose later since it is needed by abcrem */
11215 MonoClass *array_type = mono_array_class_get (klass, 1);
11216 mono_class_vtable (cfg->domain, array_type);
11217 CHECK_TYPELOAD (array_type);
11219 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11220 ins->dreg = alloc_ireg_ref (cfg);
11221 ins->sreg1 = sp [0]->dreg;
11222 ins->inst_newa_class = klass;
11223 ins->type = STACK_OBJ;
11224 ins->klass = array_type;
11225 MONO_ADD_INS (cfg->cbb, ins);
11226 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11227 cfg->cbb->has_array_access = TRUE;
11229 /* Needed so mono_emit_load_get_addr () gets called */
11230 mono_get_got_var (cfg);
11240 * we inline/optimize the initialization sequence if possible.
11241 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11242 * for small sizes open code the memcpy
11243 * ensure the rva field is big enough
11245 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))) {
11246 MonoMethod *memcpy_method = get_memcpy_method ();
11247 MonoInst *iargs [3];
11248 int add_reg = alloc_ireg_mp (cfg);
11250 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11251 if (cfg->compile_aot) {
11252 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11254 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11256 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11257 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11266 if (sp [0]->type != STACK_OBJ)
11269 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11270 ins->dreg = alloc_preg (cfg);
11271 ins->sreg1 = sp [0]->dreg;
11272 ins->type = STACK_I4;
11273 /* This flag will be inherited by the decomposition */
11274 ins->flags |= MONO_INST_FAULT;
11275 MONO_ADD_INS (cfg->cbb, ins);
11276 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11277 cfg->cbb->has_array_access = TRUE;
11285 if (sp [0]->type != STACK_OBJ)
11288 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11290 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11291 CHECK_TYPELOAD (klass);
11292 /* we need to make sure that this array is exactly the type it needs
11293 * to be for correctness. the wrappers are lax with their usage
11294 * so we need to ignore them here
11296 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11297 MonoClass *array_class = mono_array_class_get (klass, 1);
11298 mini_emit_check_array_type (cfg, sp [0], array_class);
11299 CHECK_TYPELOAD (array_class);
11303 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11308 case CEE_LDELEM_I1:
11309 case CEE_LDELEM_U1:
11310 case CEE_LDELEM_I2:
11311 case CEE_LDELEM_U2:
11312 case CEE_LDELEM_I4:
11313 case CEE_LDELEM_U4:
11314 case CEE_LDELEM_I8:
11316 case CEE_LDELEM_R4:
11317 case CEE_LDELEM_R8:
11318 case CEE_LDELEM_REF: {
11324 if (*ip == CEE_LDELEM) {
11326 token = read32 (ip + 1);
11327 klass = mini_get_class (method, token, generic_context);
11328 CHECK_TYPELOAD (klass);
11329 mono_class_init (klass);
11332 klass = array_access_to_klass (*ip);
11334 if (sp [0]->type != STACK_OBJ)
11337 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11339 if (mini_is_gsharedvt_variable_klass (klass)) {
11340 // FIXME-VT: OP_ICONST optimization
11341 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11342 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11343 ins->opcode = OP_LOADV_MEMBASE;
11344 } else if (sp [1]->opcode == OP_ICONST) {
11345 int array_reg = sp [0]->dreg;
11346 int index_reg = sp [1]->dreg;
11347 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11349 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11350 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11352 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11353 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11355 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11356 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11359 if (*ip == CEE_LDELEM)
11366 case CEE_STELEM_I1:
11367 case CEE_STELEM_I2:
11368 case CEE_STELEM_I4:
11369 case CEE_STELEM_I8:
11370 case CEE_STELEM_R4:
11371 case CEE_STELEM_R8:
11372 case CEE_STELEM_REF:
11377 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11379 if (*ip == CEE_STELEM) {
11381 token = read32 (ip + 1);
11382 klass = mini_get_class (method, token, generic_context);
11383 CHECK_TYPELOAD (klass);
11384 mono_class_init (klass);
11387 klass = array_access_to_klass (*ip);
11389 if (sp [0]->type != STACK_OBJ)
11392 emit_array_store (cfg, klass, sp, TRUE);
11394 if (*ip == CEE_STELEM)
11401 case CEE_CKFINITE: {
11405 if (cfg->llvm_only) {
11406 MonoInst *iargs [1];
11408 iargs [0] = sp [0];
11409 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
11411 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11412 ins->sreg1 = sp [0]->dreg;
11413 ins->dreg = alloc_freg (cfg);
11414 ins->type = STACK_R8;
11415 MONO_ADD_INS (cfg->cbb, ins);
11417 *sp++ = mono_decompose_opcode (cfg, ins);
11423 case CEE_REFANYVAL: {
11424 MonoInst *src_var, *src;
11426 int klass_reg = alloc_preg (cfg);
11427 int dreg = alloc_preg (cfg);
11429 GSHAREDVT_FAILURE (*ip);
11432 MONO_INST_NEW (cfg, ins, *ip);
11435 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11436 CHECK_TYPELOAD (klass);
11438 context_used = mini_class_check_context_used (cfg, klass);
11441 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11443 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11444 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11445 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11447 if (context_used) {
11448 MonoInst *klass_ins;
11450 klass_ins = mini_emit_get_rgctx_klass (cfg, context_used,
11451 klass, MONO_RGCTX_INFO_KLASS);
11454 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11455 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11457 mini_emit_class_check (cfg, klass_reg, klass);
11459 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11460 ins->type = STACK_MP;
11461 ins->klass = klass;
11466 case CEE_MKREFANY: {
11467 MonoInst *loc, *addr;
11469 GSHAREDVT_FAILURE (*ip);
11472 MONO_INST_NEW (cfg, ins, *ip);
11475 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11476 CHECK_TYPELOAD (klass);
11478 context_used = mini_class_check_context_used (cfg, klass);
11480 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11481 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11483 if (context_used) {
11484 MonoInst *const_ins;
11485 int type_reg = alloc_preg (cfg);
11487 const_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11488 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11489 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11490 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11492 int const_reg = alloc_preg (cfg);
11493 int type_reg = alloc_preg (cfg);
11495 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11496 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11497 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11498 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11500 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11502 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11503 ins->type = STACK_VTYPE;
11504 ins->klass = mono_defaults.typed_reference_class;
11509 case CEE_LDTOKEN: {
11511 MonoClass *handle_class;
11513 CHECK_STACK_OVF (1);
11516 n = read32 (ip + 1);
11518 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11519 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11520 handle = mono_method_get_wrapper_data (method, n);
11521 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
11522 if (handle_class == mono_defaults.typehandle_class)
11523 handle = &((MonoClass*)handle)->byval_arg;
11526 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11531 mono_class_init (handle_class);
11532 if (cfg->gshared) {
11533 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11534 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11535 /* This case handles ldtoken
11536 of an open type, like for
11539 } else if (handle_class == mono_defaults.typehandle_class) {
11540 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
11541 } else if (handle_class == mono_defaults.fieldhandle_class)
11542 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11543 else if (handle_class == mono_defaults.methodhandle_class)
11544 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
11546 g_assert_not_reached ();
11549 if ((cfg->opt & MONO_OPT_SHARED) &&
11550 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11551 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11552 MonoInst *addr, *vtvar, *iargs [3];
11553 int method_context_used;
11555 method_context_used = mini_method_check_context_used (cfg, method);
11557 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11559 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11560 EMIT_NEW_ICONST (cfg, iargs [1], n);
11561 if (method_context_used) {
11562 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11563 method, MONO_RGCTX_INFO_METHOD);
11564 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11566 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11567 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11569 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11571 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11573 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11575 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11576 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
11577 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11578 (cmethod->klass == mono_defaults.systemtype_class) &&
11579 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11580 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
11582 mono_class_init (tclass);
11583 if (context_used) {
11584 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11585 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11586 } else if (cfg->compile_aot) {
11587 if (method->wrapper_type) {
11588 error_init (&error); //got to do it since there are multiple conditionals below
11589 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11590 /* Special case for static synchronized wrappers */
11591 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11593 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11594 /* FIXME: n is not a normal token */
11596 EMIT_NEW_PCONST (cfg, ins, NULL);
11599 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11602 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
11604 EMIT_NEW_PCONST (cfg, ins, rt);
11606 ins->type = STACK_OBJ;
11607 ins->klass = cmethod->klass;
11610 MonoInst *addr, *vtvar;
11612 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11614 if (context_used) {
11615 if (handle_class == mono_defaults.typehandle_class) {
11616 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11617 mono_class_from_mono_type ((MonoType *)handle),
11618 MONO_RGCTX_INFO_TYPE);
11619 } else if (handle_class == mono_defaults.methodhandle_class) {
11620 ins = emit_get_rgctx_method (cfg, context_used,
11621 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
11622 } else if (handle_class == mono_defaults.fieldhandle_class) {
11623 ins = emit_get_rgctx_field (cfg, context_used,
11624 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
11626 g_assert_not_reached ();
11628 } else if (cfg->compile_aot) {
11629 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11631 EMIT_NEW_PCONST (cfg, ins, handle);
11633 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11634 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11635 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11645 if (sp [-1]->type != STACK_OBJ)
11648 MONO_INST_NEW (cfg, ins, OP_THROW);
11650 ins->sreg1 = sp [0]->dreg;
11652 cfg->cbb->out_of_line = TRUE;
11653 MONO_ADD_INS (cfg->cbb, ins);
11654 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11655 MONO_ADD_INS (cfg->cbb, ins);
11658 link_bblock (cfg, cfg->cbb, end_bblock);
11659 start_new_bblock = 1;
11660 /* This can complicate code generation for llvm since the return value might not be defined */
11661 if (COMPILE_LLVM (cfg))
11662 INLINE_FAILURE ("throw");
11664 case CEE_ENDFINALLY:
11665 if (!ip_in_finally_clause (cfg, ip - header->code))
11667 /* mono_save_seq_point_info () depends on this */
11668 if (sp != stack_start)
11669 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11670 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11671 MONO_ADD_INS (cfg->cbb, ins);
11673 start_new_bblock = 1;
11676 * Control will leave the method so empty the stack, otherwise
11677 * the next basic block will start with a nonempty stack.
11679 while (sp != stack_start) {
11684 case CEE_LEAVE_S: {
11687 if (*ip == CEE_LEAVE) {
11689 target = ip + 5 + (gint32)read32(ip + 1);
11692 target = ip + 2 + (signed char)(ip [1]);
11695 /* empty the stack */
11696 while (sp != stack_start) {
11701 * If this leave statement is in a catch block, check for a
11702 * pending exception, and rethrow it if necessary.
11703 * We avoid doing this in runtime invoke wrappers, since those are called
11704 * by native code which excepts the wrapper to catch all exceptions.
11706 for (i = 0; i < header->num_clauses; ++i) {
11707 MonoExceptionClause *clause = &header->clauses [i];
11710 * Use <= in the final comparison to handle clauses with multiple
11711 * leave statements, like in bug #78024.
11712 * The ordering of the exception clauses guarantees that we find the
11713 * innermost clause.
11715 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) {
11717 MonoBasicBlock *dont_throw;
11722 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11725 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11727 NEW_BBLOCK (cfg, dont_throw);
11730 * Currently, we always rethrow the abort exception, despite the
11731 * fact that this is not correct. See thread6.cs for an example.
11732 * But propagating the abort exception is more important than
11733 * getting the sematics right.
11735 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11736 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11737 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11739 MONO_START_BB (cfg, dont_throw);
11744 cfg->cbb->try_end = (intptr_t)(ip - header->code);
11747 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11749 MonoExceptionClause *clause;
11751 for (tmp = handlers; tmp; tmp = tmp->next) {
11752 clause = (MonoExceptionClause *)tmp->data;
11753 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11755 link_bblock (cfg, cfg->cbb, tblock);
11756 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11757 ins->inst_target_bb = tblock;
11758 ins->inst_eh_block = clause;
11759 MONO_ADD_INS (cfg->cbb, ins);
11760 cfg->cbb->has_call_handler = 1;
11761 if (COMPILE_LLVM (cfg)) {
11762 MonoBasicBlock *target_bb;
11765 * Link the finally bblock with the target, since it will
11766 * conceptually branch there.
11768 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
11769 GET_BBLOCK (cfg, target_bb, target);
11770 link_bblock (cfg, tblock, target_bb);
11773 g_list_free (handlers);
11776 MONO_INST_NEW (cfg, ins, OP_BR);
11777 MONO_ADD_INS (cfg->cbb, ins);
11778 GET_BBLOCK (cfg, tblock, target);
11779 link_bblock (cfg, cfg->cbb, tblock);
11780 ins->inst_target_bb = tblock;
11782 start_new_bblock = 1;
11784 if (*ip == CEE_LEAVE)
11793 * Mono specific opcodes
11795 case MONO_CUSTOM_PREFIX: {
11797 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11801 case CEE_MONO_ICALL: {
11803 MonoJitICallInfo *info;
11805 token = read32 (ip + 2);
11806 func = mono_method_get_wrapper_data (method, token);
11807 info = mono_find_jit_icall_by_addr (func);
11809 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11812 CHECK_STACK (info->sig->param_count);
11813 sp -= info->sig->param_count;
11815 ins = mono_emit_jit_icall (cfg, info->func, sp);
11816 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11820 inline_costs += 10 * num_calls++;
11824 case CEE_MONO_LDPTR_CARD_TABLE:
11825 case CEE_MONO_LDPTR_NURSERY_START:
11826 case CEE_MONO_LDPTR_NURSERY_BITS:
11827 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
11828 CHECK_STACK_OVF (1);
11831 case CEE_MONO_LDPTR_CARD_TABLE:
11832 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11834 case CEE_MONO_LDPTR_NURSERY_START:
11835 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11837 case CEE_MONO_LDPTR_NURSERY_BITS:
11838 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
11840 case CEE_MONO_LDPTR_INT_REQ_FLAG:
11841 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11847 inline_costs += 10 * num_calls++;
11850 case CEE_MONO_LDPTR: {
11853 CHECK_STACK_OVF (1);
11855 token = read32 (ip + 2);
11857 ptr = mono_method_get_wrapper_data (method, token);
11858 EMIT_NEW_PCONST (cfg, ins, ptr);
11861 inline_costs += 10 * num_calls++;
11862 /* Can't embed random pointers into AOT code */
11866 case CEE_MONO_JIT_ICALL_ADDR: {
11867 MonoJitICallInfo *callinfo;
11870 CHECK_STACK_OVF (1);
11872 token = read32 (ip + 2);
11874 ptr = mono_method_get_wrapper_data (method, token);
11875 callinfo = mono_find_jit_icall_by_addr (ptr);
11876 g_assert (callinfo);
11877 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
11880 inline_costs += 10 * num_calls++;
11883 case CEE_MONO_ICALL_ADDR: {
11884 MonoMethod *cmethod;
11887 CHECK_STACK_OVF (1);
11889 token = read32 (ip + 2);
11891 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
11893 if (cfg->compile_aot) {
11894 if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
11896 * This is generated by emit_native_wrapper () to resolve the pinvoke address
11897 * before the call, its not needed when using direct pinvoke.
11898 * This is not an optimization, but its used to avoid looking up pinvokes
11899 * on platforms which don't support dlopen ().
11901 EMIT_NEW_PCONST (cfg, ins, NULL);
11903 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
11906 ptr = mono_lookup_internal_call (cmethod);
11908 EMIT_NEW_PCONST (cfg, ins, ptr);
11914 case CEE_MONO_VTADDR: {
11915 MonoInst *src_var, *src;
11921 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11922 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
11927 case CEE_MONO_NEWOBJ: {
11928 MonoInst *iargs [2];
11930 CHECK_STACK_OVF (1);
11932 token = read32 (ip + 2);
11933 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11934 mono_class_init (klass);
11935 NEW_DOMAINCONST (cfg, iargs [0]);
11936 MONO_ADD_INS (cfg->cbb, iargs [0]);
11937 NEW_CLASSCONST (cfg, iargs [1], klass);
11938 MONO_ADD_INS (cfg->cbb, iargs [1]);
11939 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
11941 inline_costs += 10 * num_calls++;
11944 case CEE_MONO_OBJADDR:
11947 MONO_INST_NEW (cfg, ins, OP_MOVE);
11948 ins->dreg = alloc_ireg_mp (cfg);
11949 ins->sreg1 = sp [0]->dreg;
11950 ins->type = STACK_MP;
11951 MONO_ADD_INS (cfg->cbb, ins);
11955 case CEE_MONO_LDNATIVEOBJ:
11957 * Similar to LDOBJ, but instead load the unmanaged
11958 * representation of the vtype to the stack.
11963 token = read32 (ip + 2);
11964 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11965 g_assert (klass->valuetype);
11966 mono_class_init (klass);
11969 MonoInst *src, *dest, *temp;
11972 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
11973 temp->backend.is_pinvoke = 1;
11974 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
11975 mini_emit_stobj (cfg, dest, src, klass, TRUE);
11977 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
11978 dest->type = STACK_VTYPE;
11979 dest->klass = klass;
11985 case CEE_MONO_RETOBJ: {
11987 * Same as RET, but return the native representation of a vtype
11990 g_assert (cfg->ret);
11991 g_assert (mono_method_signature (method)->pinvoke);
11996 token = read32 (ip + 2);
11997 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11999 if (!cfg->vret_addr) {
12000 g_assert (cfg->ret_var_is_local);
12002 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12004 EMIT_NEW_RETLOADA (cfg, ins);
12006 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12008 if (sp != stack_start)
12011 MONO_INST_NEW (cfg, ins, OP_BR);
12012 ins->inst_target_bb = end_bblock;
12013 MONO_ADD_INS (cfg->cbb, ins);
12014 link_bblock (cfg, cfg->cbb, end_bblock);
12015 start_new_bblock = 1;
12019 case CEE_MONO_SAVE_LMF:
12020 case CEE_MONO_RESTORE_LMF:
12023 case CEE_MONO_CLASSCONST:
12024 CHECK_STACK_OVF (1);
12026 token = read32 (ip + 2);
12027 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12030 inline_costs += 10 * num_calls++;
12032 case CEE_MONO_NOT_TAKEN:
12033 cfg->cbb->out_of_line = TRUE;
12036 case CEE_MONO_TLS: {
12039 CHECK_STACK_OVF (1);
12041 key = (MonoTlsKey)read32 (ip + 2);
12042 g_assert (key < TLS_KEY_NUM);
12044 ins = mono_create_tls_get (cfg, key);
12046 ins->type = STACK_PTR;
12051 case CEE_MONO_DYN_CALL: {
12052 MonoCallInst *call;
12054 /* It would be easier to call a trampoline, but that would put an
12055 * extra frame on the stack, confusing exception handling. So
12056 * implement it inline using an opcode for now.
12059 if (!cfg->dyn_call_var) {
12060 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12061 /* prevent it from being register allocated */
12062 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12065 /* Has to use a call inst since it local regalloc expects it */
12066 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12067 ins = (MonoInst*)call;
12069 ins->sreg1 = sp [0]->dreg;
12070 ins->sreg2 = sp [1]->dreg;
12071 MONO_ADD_INS (cfg->cbb, ins);
12073 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12076 inline_costs += 10 * num_calls++;
12080 case CEE_MONO_MEMORY_BARRIER: {
12082 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12086 case CEE_MONO_ATOMIC_STORE_I4: {
12087 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
12093 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
12094 ins->dreg = sp [0]->dreg;
12095 ins->sreg1 = sp [1]->dreg;
12096 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
12097 MONO_ADD_INS (cfg->cbb, ins);
12102 case CEE_MONO_JIT_ATTACH: {
12103 MonoInst *args [16], *domain_ins;
12104 MonoInst *ad_ins, *jit_tls_ins;
12105 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12107 g_assert (!mono_threads_is_coop_enabled ());
12109 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12111 EMIT_NEW_PCONST (cfg, ins, NULL);
12112 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12114 ad_ins = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12115 jit_tls_ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
12117 if (ad_ins && jit_tls_ins) {
12118 NEW_BBLOCK (cfg, next_bb);
12119 NEW_BBLOCK (cfg, call_bb);
12121 if (cfg->compile_aot) {
12122 /* AOT code is only used in the root domain */
12123 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12125 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12127 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12128 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12130 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12131 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12133 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12134 MONO_START_BB (cfg, call_bb);
12137 /* AOT code is only used in the root domain */
12138 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12139 if (cfg->compile_aot) {
12143 * This is called on unattached threads, so it cannot go through the trampoline
12144 * infrastructure. Use an indirect call through a got slot initialized at load time
12147 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
12148 ins = mono_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
12150 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12152 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12155 MONO_START_BB (cfg, next_bb);
12160 case CEE_MONO_JIT_DETACH: {
12161 MonoInst *args [16];
12163 /* Restore the original domain */
12164 dreg = alloc_ireg (cfg);
12165 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12166 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12170 case CEE_MONO_CALLI_EXTRA_ARG: {
12172 MonoMethodSignature *fsig;
12176 * This is the same as CEE_CALLI, but passes an additional argument
12177 * to the called method in llvmonly mode.
12178 * This is only used by delegate invoke wrappers to call the
12179 * actual delegate method.
12181 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12184 token = read32 (ip + 2);
12192 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
12195 if (cfg->llvm_only)
12196 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12198 n = fsig->param_count + fsig->hasthis + 1;
12205 if (cfg->llvm_only) {
12207 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12208 * cconv. This is set by mono_init_delegate ().
12210 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12211 MonoInst *callee = addr;
12212 MonoInst *call, *localloc_ins;
12213 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12214 int low_bit_reg = alloc_preg (cfg);
12216 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12217 NEW_BBLOCK (cfg, end_bb);
12219 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12220 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12221 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12223 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12224 addr = emit_get_rgctx_sig (cfg, context_used,
12225 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12227 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12229 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12230 ins->dreg = alloc_preg (cfg);
12231 ins->inst_imm = 2 * SIZEOF_VOID_P;
12232 MONO_ADD_INS (cfg->cbb, ins);
12233 localloc_ins = ins;
12234 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12235 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12236 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12238 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12239 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12241 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12242 MONO_START_BB (cfg, is_gsharedvt_bb);
12243 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12244 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12245 ins->dreg = call->dreg;
12247 MONO_START_BB (cfg, end_bb);
12249 /* Caller uses a normal calling conv */
12251 MonoInst *callee = addr;
12252 MonoInst *call, *localloc_ins;
12253 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12254 int low_bit_reg = alloc_preg (cfg);
12256 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12257 NEW_BBLOCK (cfg, end_bb);
12259 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12260 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12261 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12263 /* Normal case: callee uses a normal cconv, no conversion is needed */
12264 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12265 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12266 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12267 MONO_START_BB (cfg, is_gsharedvt_bb);
12268 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12269 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12270 MONO_ADD_INS (cfg->cbb, addr);
12272 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12274 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12275 ins->dreg = alloc_preg (cfg);
12276 ins->inst_imm = 2 * SIZEOF_VOID_P;
12277 MONO_ADD_INS (cfg->cbb, ins);
12278 localloc_ins = ins;
12279 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12280 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12281 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12283 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12284 ins->dreg = call->dreg;
12285 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12287 MONO_START_BB (cfg, end_bb);
12290 /* Same as CEE_CALLI */
12291 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12293 * We pass the address to the gsharedvt trampoline in the rgctx reg
12295 MonoInst *callee = addr;
12297 addr = emit_get_rgctx_sig (cfg, context_used,
12298 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12299 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12301 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12305 if (!MONO_TYPE_IS_VOID (fsig->ret))
12306 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12308 CHECK_CFG_EXCEPTION;
12312 constrained_class = NULL;
12315 case CEE_MONO_LDDOMAIN:
12316 CHECK_STACK_OVF (1);
12317 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
12321 case CEE_MONO_GET_LAST_ERROR:
12323 CHECK_STACK_OVF (1);
12325 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
12326 ins->dreg = alloc_dreg (cfg, STACK_I4);
12327 ins->type = STACK_I4;
12328 MONO_ADD_INS (cfg->cbb, ins);
12334 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12340 case CEE_PREFIX1: {
12343 case CEE_ARGLIST: {
12344 /* somewhat similar to LDTOKEN */
12345 MonoInst *addr, *vtvar;
12346 CHECK_STACK_OVF (1);
12347 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12349 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12350 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12352 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12353 ins->type = STACK_VTYPE;
12354 ins->klass = mono_defaults.argumenthandle_class;
12364 MonoInst *cmp, *arg1, *arg2;
12372 * The following transforms:
12373 * CEE_CEQ into OP_CEQ
12374 * CEE_CGT into OP_CGT
12375 * CEE_CGT_UN into OP_CGT_UN
12376 * CEE_CLT into OP_CLT
12377 * CEE_CLT_UN into OP_CLT_UN
12379 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12381 MONO_INST_NEW (cfg, ins, cmp->opcode);
12382 cmp->sreg1 = arg1->dreg;
12383 cmp->sreg2 = arg2->dreg;
12384 type_from_op (cfg, cmp, arg1, arg2);
12386 add_widen_op (cfg, cmp, &arg1, &arg2);
12387 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12388 cmp->opcode = OP_LCOMPARE;
12389 else if (arg1->type == STACK_R4)
12390 cmp->opcode = OP_RCOMPARE;
12391 else if (arg1->type == STACK_R8)
12392 cmp->opcode = OP_FCOMPARE;
12394 cmp->opcode = OP_ICOMPARE;
12395 MONO_ADD_INS (cfg->cbb, cmp);
12396 ins->type = STACK_I4;
12397 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
12398 type_from_op (cfg, ins, arg1, arg2);
12400 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12402 * The backends expect the fceq opcodes to do the
12405 ins->sreg1 = cmp->sreg1;
12406 ins->sreg2 = cmp->sreg2;
12409 MONO_ADD_INS (cfg->cbb, ins);
12415 MonoInst *argconst;
12416 MonoMethod *cil_method;
12418 CHECK_STACK_OVF (1);
12420 n = read32 (ip + 2);
12421 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12424 mono_class_init (cmethod->klass);
12426 mono_save_token_info (cfg, image, n, cmethod);
12428 context_used = mini_method_check_context_used (cfg, cmethod);
12430 cil_method = cmethod;
12431 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12432 emit_method_access_failure (cfg, method, cil_method);
12434 if (mono_security_core_clr_enabled ())
12435 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12438 * Optimize the common case of ldftn+delegate creation
12440 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12441 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12442 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12443 MonoInst *target_ins, *handle_ins;
12444 MonoMethod *invoke;
12445 int invoke_context_used;
12447 invoke = mono_get_delegate_invoke (ctor_method->klass);
12448 if (!invoke || !mono_method_signature (invoke))
12451 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12453 target_ins = sp [-1];
12455 if (mono_security_core_clr_enabled ())
12456 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12458 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12459 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12460 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12461 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12462 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12466 /* FIXME: SGEN support */
12467 if (invoke_context_used == 0 || cfg->llvm_only) {
12469 if (cfg->verbose_level > 3)
12470 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));
12471 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12474 CHECK_CFG_EXCEPTION;
12484 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12485 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12489 inline_costs += 10 * num_calls++;
12492 case CEE_LDVIRTFTN: {
12493 MonoInst *args [2];
12497 n = read32 (ip + 2);
12498 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12501 mono_class_init (cmethod->klass);
12503 context_used = mini_method_check_context_used (cfg, cmethod);
12505 if (mono_security_core_clr_enabled ())
12506 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12509 * Optimize the common case of ldvirtftn+delegate creation
12511 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)) {
12512 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12513 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12514 MonoInst *target_ins, *handle_ins;
12515 MonoMethod *invoke;
12516 int invoke_context_used;
12517 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12519 invoke = mono_get_delegate_invoke (ctor_method->klass);
12520 if (!invoke || !mono_method_signature (invoke))
12523 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12525 target_ins = sp [-1];
12527 if (mono_security_core_clr_enabled ())
12528 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12530 /* FIXME: SGEN support */
12531 if (invoke_context_used == 0 || cfg->llvm_only) {
12533 if (cfg->verbose_level > 3)
12534 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));
12535 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12538 CHECK_CFG_EXCEPTION;
12551 args [1] = emit_get_rgctx_method (cfg, context_used,
12552 cmethod, MONO_RGCTX_INFO_METHOD);
12555 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12557 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12560 inline_costs += 10 * num_calls++;
12564 CHECK_STACK_OVF (1);
12566 n = read16 (ip + 2);
12568 EMIT_NEW_ARGLOAD (cfg, ins, n);
12573 CHECK_STACK_OVF (1);
12575 n = read16 (ip + 2);
12577 NEW_ARGLOADA (cfg, ins, n);
12578 MONO_ADD_INS (cfg->cbb, ins);
12586 n = read16 (ip + 2);
12588 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12590 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12594 CHECK_STACK_OVF (1);
12596 n = read16 (ip + 2);
12598 EMIT_NEW_LOCLOAD (cfg, ins, n);
12603 unsigned char *tmp_ip;
12604 CHECK_STACK_OVF (1);
12606 n = read16 (ip + 2);
12609 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12615 EMIT_NEW_LOCLOADA (cfg, ins, n);
12624 n = read16 (ip + 2);
12626 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12628 emit_stloc_ir (cfg, sp, header, n);
12632 case CEE_LOCALLOC: {
12634 MonoBasicBlock *non_zero_bb, *end_bb;
12635 int alloc_ptr = alloc_preg (cfg);
12637 if (sp != stack_start)
12639 if (cfg->method != method)
12641 * Inlining this into a loop in a parent could lead to
12642 * stack overflows which is different behavior than the
12643 * non-inlined case, thus disable inlining in this case.
12645 INLINE_FAILURE("localloc");
12647 NEW_BBLOCK (cfg, non_zero_bb);
12648 NEW_BBLOCK (cfg, end_bb);
12650 /* if size != zero */
12651 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
12652 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
12654 //size is zero, so result is NULL
12655 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
12656 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12658 MONO_START_BB (cfg, non_zero_bb);
12659 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12660 ins->dreg = alloc_ptr;
12661 ins->sreg1 = sp [0]->dreg;
12662 ins->type = STACK_PTR;
12663 MONO_ADD_INS (cfg->cbb, ins);
12665 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12667 ins->flags |= MONO_INST_INIT;
12669 MONO_START_BB (cfg, end_bb);
12670 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
12671 ins->type = STACK_PTR;
12677 case CEE_ENDFILTER: {
12678 MonoExceptionClause *clause, *nearest;
12683 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
12685 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12686 ins->sreg1 = (*sp)->dreg;
12687 MONO_ADD_INS (cfg->cbb, ins);
12688 start_new_bblock = 1;
12692 for (cc = 0; cc < header->num_clauses; ++cc) {
12693 clause = &header->clauses [cc];
12694 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12695 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12696 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12699 g_assert (nearest);
12700 if ((ip - header->code) != nearest->handler_offset)
12705 case CEE_UNALIGNED_:
12706 ins_flag |= MONO_INST_UNALIGNED;
12707 /* FIXME: record alignment? we can assume 1 for now */
12711 case CEE_VOLATILE_:
12712 ins_flag |= MONO_INST_VOLATILE;
12716 ins_flag |= MONO_INST_TAILCALL;
12717 cfg->flags |= MONO_CFG_HAS_TAIL;
12718 /* Can't inline tail calls at this time */
12719 inline_costs += 100000;
12726 token = read32 (ip + 2);
12727 klass = mini_get_class (method, token, generic_context);
12728 CHECK_TYPELOAD (klass);
12729 if (generic_class_is_reference_type (cfg, klass))
12730 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12732 mini_emit_initobj (cfg, *sp, NULL, klass);
12736 case CEE_CONSTRAINED_:
12738 token = read32 (ip + 2);
12739 constrained_class = mini_get_class (method, token, generic_context);
12740 CHECK_TYPELOAD (constrained_class);
12744 case CEE_INITBLK: {
12745 MonoInst *iargs [3];
12749 /* Skip optimized paths for volatile operations. */
12750 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)) {
12751 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
12752 } 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)) {
12753 /* emit_memset only works when val == 0 */
12754 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
12757 iargs [0] = sp [0];
12758 iargs [1] = sp [1];
12759 iargs [2] = sp [2];
12760 if (ip [1] == CEE_CPBLK) {
12762 * FIXME: It's unclear whether we should be emitting both the acquire
12763 * and release barriers for cpblk. It is technically both a load and
12764 * store operation, so it seems like that's the sensible thing to do.
12766 * FIXME: We emit full barriers on both sides of the operation for
12767 * simplicity. We should have a separate atomic memcpy method instead.
12769 MonoMethod *memcpy_method = get_memcpy_method ();
12771 if (ins_flag & MONO_INST_VOLATILE)
12772 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12774 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
12775 call->flags |= ins_flag;
12777 if (ins_flag & MONO_INST_VOLATILE)
12778 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12780 MonoMethod *memset_method = get_memset_method ();
12781 if (ins_flag & MONO_INST_VOLATILE) {
12782 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
12783 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
12785 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
12786 call->flags |= ins_flag;
12797 ins_flag |= MONO_INST_NOTYPECHECK;
12799 ins_flag |= MONO_INST_NORANGECHECK;
12800 /* we ignore the no-nullcheck for now since we
12801 * really do it explicitly only when doing callvirt->call
12805 case CEE_RETHROW: {
12807 int handler_offset = -1;
12809 for (i = 0; i < header->num_clauses; ++i) {
12810 MonoExceptionClause *clause = &header->clauses [i];
12811 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12812 handler_offset = clause->handler_offset;
12817 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12819 if (handler_offset == -1)
12822 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12823 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12824 ins->sreg1 = load->dreg;
12825 MONO_ADD_INS (cfg->cbb, ins);
12827 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12828 MONO_ADD_INS (cfg->cbb, ins);
12831 link_bblock (cfg, cfg->cbb, end_bblock);
12832 start_new_bblock = 1;
12840 CHECK_STACK_OVF (1);
12842 token = read32 (ip + 2);
12843 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12844 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12847 val = mono_type_size (type, &ialign);
12849 MonoClass *klass = mini_get_class (method, token, generic_context);
12850 CHECK_TYPELOAD (klass);
12852 val = mono_type_size (&klass->byval_arg, &ialign);
12854 if (mini_is_gsharedvt_klass (klass))
12855 GSHAREDVT_FAILURE (*ip);
12857 EMIT_NEW_ICONST (cfg, ins, val);
12862 case CEE_REFANYTYPE: {
12863 MonoInst *src_var, *src;
12865 GSHAREDVT_FAILURE (*ip);
12871 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12873 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12874 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12875 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12880 case CEE_READONLY_:
12893 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12903 g_warning ("opcode 0x%02x not handled", *ip);
12907 if (start_new_bblock != 1)
12910 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12911 if (cfg->cbb->next_bb) {
12912 /* This could already be set because of inlining, #693905 */
12913 MonoBasicBlock *bb = cfg->cbb;
12915 while (bb->next_bb)
12917 bb->next_bb = end_bblock;
12919 cfg->cbb->next_bb = end_bblock;
12922 if (cfg->method == method && cfg->domainvar) {
12924 MonoInst *get_domain;
12926 cfg->cbb = init_localsbb;
12928 get_domain = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12929 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12930 MONO_ADD_INS (cfg->cbb, store);
12933 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12934 if (cfg->compile_aot)
12935 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12936 mono_get_got_var (cfg);
12939 if (cfg->method == method && cfg->got_var)
12940 mono_emit_load_got_addr (cfg);
12942 if (init_localsbb) {
12943 cfg->cbb = init_localsbb;
12945 for (i = 0; i < header->num_locals; ++i) {
12946 emit_init_local (cfg, i, header->locals [i], init_locals);
12950 if (cfg->init_ref_vars && cfg->method == method) {
12951 /* Emit initialization for ref vars */
12952 // FIXME: Avoid duplication initialization for IL locals.
12953 for (i = 0; i < cfg->num_varinfo; ++i) {
12954 MonoInst *ins = cfg->varinfo [i];
12956 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12957 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12961 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
12962 cfg->cbb = init_localsbb;
12963 emit_push_lmf (cfg);
12966 cfg->cbb = init_localsbb;
12967 emit_instrumentation_call (cfg, mono_profiler_method_enter);
12970 MonoBasicBlock *bb;
12973 * Make seq points at backward branch targets interruptable.
12975 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12976 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12977 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12980 /* Add a sequence point for method entry/exit events */
12981 if (seq_points && cfg->gen_sdb_seq_points) {
12982 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12983 MONO_ADD_INS (init_localsbb, ins);
12984 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12985 MONO_ADD_INS (cfg->bb_exit, ins);
12989 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12990 * the code they refer to was dead (#11880).
12992 if (sym_seq_points) {
12993 for (i = 0; i < header->code_size; ++i) {
12994 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12997 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12998 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13005 if (cfg->method == method) {
13006 MonoBasicBlock *bb;
13007 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13008 if (bb == cfg->bb_init)
13011 bb->region = mono_find_block_region (cfg, bb->real_offset);
13013 mono_create_spvar_for_region (cfg, bb->region);
13014 if (cfg->verbose_level > 2)
13015 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13018 MonoBasicBlock *bb;
13019 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
13020 for (bb = start_bblock; bb != end_bblock; bb = bb->next_bb) {
13021 bb->real_offset = inline_offset;
13025 if (inline_costs < 0) {
13028 /* Method is too large */
13029 mname = mono_method_full_name (method, TRUE);
13030 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13034 if ((cfg->verbose_level > 2) && (cfg->method == method))
13035 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13040 g_assert (!mono_error_ok (&cfg->error));
13044 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13048 set_exception_type_from_invalid_il (cfg, method, ip);
13052 g_slist_free (class_inits);
13053 mono_basic_block_free (original_bb);
13054 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13055 if (cfg->exception_type)
13058 return inline_costs;
13062 store_membase_reg_to_store_membase_imm (int opcode)
13065 case OP_STORE_MEMBASE_REG:
13066 return OP_STORE_MEMBASE_IMM;
13067 case OP_STOREI1_MEMBASE_REG:
13068 return OP_STOREI1_MEMBASE_IMM;
13069 case OP_STOREI2_MEMBASE_REG:
13070 return OP_STOREI2_MEMBASE_IMM;
13071 case OP_STOREI4_MEMBASE_REG:
13072 return OP_STOREI4_MEMBASE_IMM;
13073 case OP_STOREI8_MEMBASE_REG:
13074 return OP_STOREI8_MEMBASE_IMM;
13076 g_assert_not_reached ();
13083 mono_op_to_op_imm (int opcode)
13087 return OP_IADD_IMM;
13089 return OP_ISUB_IMM;
13091 return OP_IDIV_IMM;
13093 return OP_IDIV_UN_IMM;
13095 return OP_IREM_IMM;
13097 return OP_IREM_UN_IMM;
13099 return OP_IMUL_IMM;
13101 return OP_IAND_IMM;
13105 return OP_IXOR_IMM;
13107 return OP_ISHL_IMM;
13109 return OP_ISHR_IMM;
13111 return OP_ISHR_UN_IMM;
13114 return OP_LADD_IMM;
13116 return OP_LSUB_IMM;
13118 return OP_LAND_IMM;
13122 return OP_LXOR_IMM;
13124 return OP_LSHL_IMM;
13126 return OP_LSHR_IMM;
13128 return OP_LSHR_UN_IMM;
13129 #if SIZEOF_REGISTER == 8
13131 return OP_LREM_IMM;
13135 return OP_COMPARE_IMM;
13137 return OP_ICOMPARE_IMM;
13139 return OP_LCOMPARE_IMM;
13141 case OP_STORE_MEMBASE_REG:
13142 return OP_STORE_MEMBASE_IMM;
13143 case OP_STOREI1_MEMBASE_REG:
13144 return OP_STOREI1_MEMBASE_IMM;
13145 case OP_STOREI2_MEMBASE_REG:
13146 return OP_STOREI2_MEMBASE_IMM;
13147 case OP_STOREI4_MEMBASE_REG:
13148 return OP_STOREI4_MEMBASE_IMM;
13150 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13152 return OP_X86_PUSH_IMM;
13153 case OP_X86_COMPARE_MEMBASE_REG:
13154 return OP_X86_COMPARE_MEMBASE_IMM;
13156 #if defined(TARGET_AMD64)
13157 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13158 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13160 case OP_VOIDCALL_REG:
13161 return OP_VOIDCALL;
13169 return OP_LOCALLOC_IMM;
13176 ldind_to_load_membase (int opcode)
13180 return OP_LOADI1_MEMBASE;
13182 return OP_LOADU1_MEMBASE;
13184 return OP_LOADI2_MEMBASE;
13186 return OP_LOADU2_MEMBASE;
13188 return OP_LOADI4_MEMBASE;
13190 return OP_LOADU4_MEMBASE;
13192 return OP_LOAD_MEMBASE;
13193 case CEE_LDIND_REF:
13194 return OP_LOAD_MEMBASE;
13196 return OP_LOADI8_MEMBASE;
13198 return OP_LOADR4_MEMBASE;
13200 return OP_LOADR8_MEMBASE;
13202 g_assert_not_reached ();
13209 stind_to_store_membase (int opcode)
13213 return OP_STOREI1_MEMBASE_REG;
13215 return OP_STOREI2_MEMBASE_REG;
13217 return OP_STOREI4_MEMBASE_REG;
13219 case CEE_STIND_REF:
13220 return OP_STORE_MEMBASE_REG;
13222 return OP_STOREI8_MEMBASE_REG;
13224 return OP_STORER4_MEMBASE_REG;
13226 return OP_STORER8_MEMBASE_REG;
13228 g_assert_not_reached ();
13235 mono_load_membase_to_load_mem (int opcode)
13237 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13238 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13240 case OP_LOAD_MEMBASE:
13241 return OP_LOAD_MEM;
13242 case OP_LOADU1_MEMBASE:
13243 return OP_LOADU1_MEM;
13244 case OP_LOADU2_MEMBASE:
13245 return OP_LOADU2_MEM;
13246 case OP_LOADI4_MEMBASE:
13247 return OP_LOADI4_MEM;
13248 case OP_LOADU4_MEMBASE:
13249 return OP_LOADU4_MEM;
13250 #if SIZEOF_REGISTER == 8
13251 case OP_LOADI8_MEMBASE:
13252 return OP_LOADI8_MEM;
13261 op_to_op_dest_membase (int store_opcode, int opcode)
13263 #if defined(TARGET_X86)
13264 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13269 return OP_X86_ADD_MEMBASE_REG;
13271 return OP_X86_SUB_MEMBASE_REG;
13273 return OP_X86_AND_MEMBASE_REG;
13275 return OP_X86_OR_MEMBASE_REG;
13277 return OP_X86_XOR_MEMBASE_REG;
13280 return OP_X86_ADD_MEMBASE_IMM;
13283 return OP_X86_SUB_MEMBASE_IMM;
13286 return OP_X86_AND_MEMBASE_IMM;
13289 return OP_X86_OR_MEMBASE_IMM;
13292 return OP_X86_XOR_MEMBASE_IMM;
13298 #if defined(TARGET_AMD64)
13299 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13304 return OP_X86_ADD_MEMBASE_REG;
13306 return OP_X86_SUB_MEMBASE_REG;
13308 return OP_X86_AND_MEMBASE_REG;
13310 return OP_X86_OR_MEMBASE_REG;
13312 return OP_X86_XOR_MEMBASE_REG;
13314 return OP_X86_ADD_MEMBASE_IMM;
13316 return OP_X86_SUB_MEMBASE_IMM;
13318 return OP_X86_AND_MEMBASE_IMM;
13320 return OP_X86_OR_MEMBASE_IMM;
13322 return OP_X86_XOR_MEMBASE_IMM;
13324 return OP_AMD64_ADD_MEMBASE_REG;
13326 return OP_AMD64_SUB_MEMBASE_REG;
13328 return OP_AMD64_AND_MEMBASE_REG;
13330 return OP_AMD64_OR_MEMBASE_REG;
13332 return OP_AMD64_XOR_MEMBASE_REG;
13335 return OP_AMD64_ADD_MEMBASE_IMM;
13338 return OP_AMD64_SUB_MEMBASE_IMM;
13341 return OP_AMD64_AND_MEMBASE_IMM;
13344 return OP_AMD64_OR_MEMBASE_IMM;
13347 return OP_AMD64_XOR_MEMBASE_IMM;
13357 op_to_op_store_membase (int store_opcode, int opcode)
13359 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13362 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13363 return OP_X86_SETEQ_MEMBASE;
13365 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13366 return OP_X86_SETNE_MEMBASE;
13374 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13377 /* FIXME: This has sign extension issues */
13379 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13380 return OP_X86_COMPARE_MEMBASE8_IMM;
13383 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13388 return OP_X86_PUSH_MEMBASE;
13389 case OP_COMPARE_IMM:
13390 case OP_ICOMPARE_IMM:
13391 return OP_X86_COMPARE_MEMBASE_IMM;
13394 return OP_X86_COMPARE_MEMBASE_REG;
13398 #ifdef TARGET_AMD64
13399 /* FIXME: This has sign extension issues */
13401 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13402 return OP_X86_COMPARE_MEMBASE8_IMM;
13407 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13408 return OP_X86_PUSH_MEMBASE;
13410 /* FIXME: This only works for 32 bit immediates
13411 case OP_COMPARE_IMM:
13412 case OP_LCOMPARE_IMM:
13413 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13414 return OP_AMD64_COMPARE_MEMBASE_IMM;
13416 case OP_ICOMPARE_IMM:
13417 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13418 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13422 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
13423 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13424 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13425 return OP_AMD64_COMPARE_MEMBASE_REG;
13428 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13429 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13438 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
13441 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13447 return OP_X86_COMPARE_REG_MEMBASE;
13449 return OP_X86_ADD_REG_MEMBASE;
13451 return OP_X86_SUB_REG_MEMBASE;
13453 return OP_X86_AND_REG_MEMBASE;
13455 return OP_X86_OR_REG_MEMBASE;
13457 return OP_X86_XOR_REG_MEMBASE;
13461 #ifdef TARGET_AMD64
13462 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
13465 return OP_AMD64_ICOMPARE_REG_MEMBASE;
13467 return OP_X86_ADD_REG_MEMBASE;
13469 return OP_X86_SUB_REG_MEMBASE;
13471 return OP_X86_AND_REG_MEMBASE;
13473 return OP_X86_OR_REG_MEMBASE;
13475 return OP_X86_XOR_REG_MEMBASE;
13477 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
13481 return OP_AMD64_COMPARE_REG_MEMBASE;
13483 return OP_AMD64_ADD_REG_MEMBASE;
13485 return OP_AMD64_SUB_REG_MEMBASE;
13487 return OP_AMD64_AND_REG_MEMBASE;
13489 return OP_AMD64_OR_REG_MEMBASE;
13491 return OP_AMD64_XOR_REG_MEMBASE;
13500 mono_op_to_op_imm_noemul (int opcode)
13503 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13509 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13516 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13521 return mono_op_to_op_imm (opcode);
13526 * mono_handle_global_vregs:
13528 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
13532 mono_handle_global_vregs (MonoCompile *cfg)
13534 gint32 *vreg_to_bb;
13535 MonoBasicBlock *bb;
13538 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13540 #ifdef MONO_ARCH_SIMD_INTRINSICS
13541 if (cfg->uses_simd_intrinsics)
13542 mono_simd_simplify_indirection (cfg);
13545 /* Find local vregs used in more than one bb */
13546 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13547 MonoInst *ins = bb->code;
13548 int block_num = bb->block_num;
13550 if (cfg->verbose_level > 2)
13551 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13554 for (; ins; ins = ins->next) {
13555 const char *spec = INS_INFO (ins->opcode);
13556 int regtype = 0, regindex;
13559 if (G_UNLIKELY (cfg->verbose_level > 2))
13560 mono_print_ins (ins);
13562 g_assert (ins->opcode >= MONO_CEE_LAST);
13564 for (regindex = 0; regindex < 4; regindex ++) {
13567 if (regindex == 0) {
13568 regtype = spec [MONO_INST_DEST];
13569 if (regtype == ' ')
13572 } else if (regindex == 1) {
13573 regtype = spec [MONO_INST_SRC1];
13574 if (regtype == ' ')
13577 } else if (regindex == 2) {
13578 regtype = spec [MONO_INST_SRC2];
13579 if (regtype == ' ')
13582 } else if (regindex == 3) {
13583 regtype = spec [MONO_INST_SRC3];
13584 if (regtype == ' ')
13589 #if SIZEOF_REGISTER == 4
13590 /* In the LLVM case, the long opcodes are not decomposed */
13591 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13593 * Since some instructions reference the original long vreg,
13594 * and some reference the two component vregs, it is quite hard
13595 * to determine when it needs to be global. So be conservative.
13597 if (!get_vreg_to_inst (cfg, vreg)) {
13598 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13600 if (cfg->verbose_level > 2)
13601 printf ("LONG VREG R%d made global.\n", vreg);
13605 * Make the component vregs volatile since the optimizations can
13606 * get confused otherwise.
13608 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
13609 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
13613 g_assert (vreg != -1);
13615 prev_bb = vreg_to_bb [vreg];
13616 if (prev_bb == 0) {
13617 /* 0 is a valid block num */
13618 vreg_to_bb [vreg] = block_num + 1;
13619 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13620 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13623 if (!get_vreg_to_inst (cfg, vreg)) {
13624 if (G_UNLIKELY (cfg->verbose_level > 2))
13625 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13629 if (vreg_is_ref (cfg, vreg))
13630 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13632 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13635 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13638 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13642 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13645 g_assert_not_reached ();
13649 /* Flag as having been used in more than one bb */
13650 vreg_to_bb [vreg] = -1;
13656 /* If a variable is used in only one bblock, convert it into a local vreg */
13657 for (i = 0; i < cfg->num_varinfo; i++) {
13658 MonoInst *var = cfg->varinfo [i];
13659 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13661 switch (var->type) {
13667 #if SIZEOF_REGISTER == 8
13670 #if !defined(TARGET_X86)
13671 /* Enabling this screws up the fp stack on x86 */
13674 if (mono_arch_is_soft_float ())
13678 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
13682 /* Arguments are implicitly global */
13683 /* Putting R4 vars into registers doesn't work currently */
13684 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13685 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) {
13687 * Make that the variable's liveness interval doesn't contain a call, since
13688 * that would cause the lvreg to be spilled, making the whole optimization
13691 /* This is too slow for JIT compilation */
13693 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13695 int def_index, call_index, ins_index;
13696 gboolean spilled = FALSE;
13701 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13702 const char *spec = INS_INFO (ins->opcode);
13704 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13705 def_index = ins_index;
13707 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13708 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13709 if (call_index > def_index) {
13715 if (MONO_IS_CALL (ins))
13716 call_index = ins_index;
13726 if (G_UNLIKELY (cfg->verbose_level > 2))
13727 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13728 var->flags |= MONO_INST_IS_DEAD;
13729 cfg->vreg_to_inst [var->dreg] = NULL;
13736 * Compress the varinfo and vars tables so the liveness computation is faster and
13737 * takes up less space.
13740 for (i = 0; i < cfg->num_varinfo; ++i) {
13741 MonoInst *var = cfg->varinfo [i];
13742 if (pos < i && cfg->locals_start == i)
13743 cfg->locals_start = pos;
13744 if (!(var->flags & MONO_INST_IS_DEAD)) {
13746 cfg->varinfo [pos] = cfg->varinfo [i];
13747 cfg->varinfo [pos]->inst_c0 = pos;
13748 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13749 cfg->vars [pos].idx = pos;
13750 #if SIZEOF_REGISTER == 4
13751 if (cfg->varinfo [pos]->type == STACK_I8) {
13752 /* Modify the two component vars too */
13755 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
13756 var1->inst_c0 = pos;
13757 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
13758 var1->inst_c0 = pos;
13765 cfg->num_varinfo = pos;
13766 if (cfg->locals_start > cfg->num_varinfo)
13767 cfg->locals_start = cfg->num_varinfo;
13771 * mono_allocate_gsharedvt_vars:
13773 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
13774 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
13777 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
13781 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13783 for (i = 0; i < cfg->num_varinfo; ++i) {
13784 MonoInst *ins = cfg->varinfo [i];
13787 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13788 if (i >= cfg->locals_start) {
13790 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13791 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13792 ins->opcode = OP_GSHAREDVT_LOCAL;
13793 ins->inst_imm = idx;
13796 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
13797 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13804 * mono_spill_global_vars:
13806 * Generate spill code for variables which are not allocated to registers,
13807 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13808 * code is generated which could be optimized by the local optimization passes.
13811 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13813 MonoBasicBlock *bb;
13815 int orig_next_vreg;
13816 guint32 *vreg_to_lvreg;
13818 guint32 i, lvregs_len, lvregs_size;
13819 gboolean dest_has_lvreg = FALSE;
13820 MonoStackType stacktypes [128];
13821 MonoInst **live_range_start, **live_range_end;
13822 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13824 *need_local_opts = FALSE;
13826 memset (spec2, 0, sizeof (spec2));
13828 /* FIXME: Move this function to mini.c */
13829 stacktypes ['i'] = STACK_PTR;
13830 stacktypes ['l'] = STACK_I8;
13831 stacktypes ['f'] = STACK_R8;
13832 #ifdef MONO_ARCH_SIMD_INTRINSICS
13833 stacktypes ['x'] = STACK_VTYPE;
13836 #if SIZEOF_REGISTER == 4
13837 /* Create MonoInsts for longs */
13838 for (i = 0; i < cfg->num_varinfo; i++) {
13839 MonoInst *ins = cfg->varinfo [i];
13841 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13842 switch (ins->type) {
13847 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13850 g_assert (ins->opcode == OP_REGOFFSET);
13852 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
13854 tree->opcode = OP_REGOFFSET;
13855 tree->inst_basereg = ins->inst_basereg;
13856 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13858 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
13860 tree->opcode = OP_REGOFFSET;
13861 tree->inst_basereg = ins->inst_basereg;
13862 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13872 if (cfg->compute_gc_maps) {
13873 /* registers need liveness info even for !non refs */
13874 for (i = 0; i < cfg->num_varinfo; i++) {
13875 MonoInst *ins = cfg->varinfo [i];
13877 if (ins->opcode == OP_REGVAR)
13878 ins->flags |= MONO_INST_GC_TRACK;
13882 /* FIXME: widening and truncation */
13885 * As an optimization, when a variable allocated to the stack is first loaded into
13886 * an lvreg, we will remember the lvreg and use it the next time instead of loading
13887 * the variable again.
13889 orig_next_vreg = cfg->next_vreg;
13890 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13891 lvregs_size = 1024;
13892 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * lvregs_size);
13896 * These arrays contain the first and last instructions accessing a given
13898 * Since we emit bblocks in the same order we process them here, and we
13899 * don't split live ranges, these will precisely describe the live range of
13900 * the variable, i.e. the instruction range where a valid value can be found
13901 * in the variables location.
13902 * The live range is computed using the liveness info computed by the liveness pass.
13903 * We can't use vmv->range, since that is an abstract live range, and we need
13904 * one which is instruction precise.
13905 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13907 /* FIXME: Only do this if debugging info is requested */
13908 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13909 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13910 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13911 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13913 /* Add spill loads/stores */
13914 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13917 if (cfg->verbose_level > 2)
13918 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13920 /* Clear vreg_to_lvreg array */
13921 for (i = 0; i < lvregs_len; i++)
13922 vreg_to_lvreg [lvregs [i]] = 0;
13926 MONO_BB_FOR_EACH_INS (bb, ins) {
13927 const char *spec = INS_INFO (ins->opcode);
13928 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13929 gboolean store, no_lvreg;
13930 int sregs [MONO_MAX_SRC_REGS];
13932 if (G_UNLIKELY (cfg->verbose_level > 2))
13933 mono_print_ins (ins);
13935 if (ins->opcode == OP_NOP)
13939 * We handle LDADDR here as well, since it can only be decomposed
13940 * when variable addresses are known.
13942 if (ins->opcode == OP_LDADDR) {
13943 MonoInst *var = (MonoInst *)ins->inst_p0;
13945 if (var->opcode == OP_VTARG_ADDR) {
13946 /* Happens on SPARC/S390 where vtypes are passed by reference */
13947 MonoInst *vtaddr = var->inst_left;
13948 if (vtaddr->opcode == OP_REGVAR) {
13949 ins->opcode = OP_MOVE;
13950 ins->sreg1 = vtaddr->dreg;
13952 else if (var->inst_left->opcode == OP_REGOFFSET) {
13953 ins->opcode = OP_LOAD_MEMBASE;
13954 ins->inst_basereg = vtaddr->inst_basereg;
13955 ins->inst_offset = vtaddr->inst_offset;
13958 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
13959 /* gsharedvt arg passed by ref */
13960 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13962 ins->opcode = OP_LOAD_MEMBASE;
13963 ins->inst_basereg = var->inst_basereg;
13964 ins->inst_offset = var->inst_offset;
13965 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
13966 MonoInst *load, *load2, *load3;
13967 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
13968 int reg1, reg2, reg3;
13969 MonoInst *info_var = cfg->gsharedvt_info_var;
13970 MonoInst *locals_var = cfg->gsharedvt_locals_var;
13974 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13977 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13979 g_assert (info_var);
13980 g_assert (locals_var);
13982 /* Mark the instruction used to compute the locals var as used */
13983 cfg->gsharedvt_locals_var_ins = NULL;
13985 /* Load the offset */
13986 if (info_var->opcode == OP_REGOFFSET) {
13987 reg1 = alloc_ireg (cfg);
13988 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13989 } else if (info_var->opcode == OP_REGVAR) {
13991 reg1 = info_var->dreg;
13993 g_assert_not_reached ();
13995 reg2 = alloc_ireg (cfg);
13996 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13997 /* Load the locals area address */
13998 reg3 = alloc_ireg (cfg);
13999 if (locals_var->opcode == OP_REGOFFSET) {
14000 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14001 } else if (locals_var->opcode == OP_REGVAR) {
14002 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14004 g_assert_not_reached ();
14006 /* Compute the address */
14007 ins->opcode = OP_PADD;
14011 mono_bblock_insert_before_ins (bb, ins, load3);
14012 mono_bblock_insert_before_ins (bb, load3, load2);
14014 mono_bblock_insert_before_ins (bb, load2, load);
14016 g_assert (var->opcode == OP_REGOFFSET);
14018 ins->opcode = OP_ADD_IMM;
14019 ins->sreg1 = var->inst_basereg;
14020 ins->inst_imm = var->inst_offset;
14023 *need_local_opts = TRUE;
14024 spec = INS_INFO (ins->opcode);
14027 if (ins->opcode < MONO_CEE_LAST) {
14028 mono_print_ins (ins);
14029 g_assert_not_reached ();
14033 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14037 if (MONO_IS_STORE_MEMBASE (ins)) {
14038 tmp_reg = ins->dreg;
14039 ins->dreg = ins->sreg2;
14040 ins->sreg2 = tmp_reg;
14043 spec2 [MONO_INST_DEST] = ' ';
14044 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14045 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14046 spec2 [MONO_INST_SRC3] = ' ';
14048 } else if (MONO_IS_STORE_MEMINDEX (ins))
14049 g_assert_not_reached ();
14054 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14055 printf ("\t %.3s %d", spec, ins->dreg);
14056 num_sregs = mono_inst_get_src_registers (ins, sregs);
14057 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14058 printf (" %d", sregs [srcindex]);
14065 regtype = spec [MONO_INST_DEST];
14066 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14069 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14070 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14071 MonoInst *store_ins;
14073 MonoInst *def_ins = ins;
14074 int dreg = ins->dreg; /* The original vreg */
14076 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14078 if (var->opcode == OP_REGVAR) {
14079 ins->dreg = var->dreg;
14080 } 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)) {
14082 * Instead of emitting a load+store, use a _membase opcode.
14084 g_assert (var->opcode == OP_REGOFFSET);
14085 if (ins->opcode == OP_MOVE) {
14089 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14090 ins->inst_basereg = var->inst_basereg;
14091 ins->inst_offset = var->inst_offset;
14094 spec = INS_INFO (ins->opcode);
14098 g_assert (var->opcode == OP_REGOFFSET);
14100 prev_dreg = ins->dreg;
14102 /* Invalidate any previous lvreg for this vreg */
14103 vreg_to_lvreg [ins->dreg] = 0;
14107 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14109 store_opcode = OP_STOREI8_MEMBASE_REG;
14112 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14114 #if SIZEOF_REGISTER != 8
14115 if (regtype == 'l') {
14116 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));
14117 mono_bblock_insert_after_ins (bb, ins, store_ins);
14118 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));
14119 mono_bblock_insert_after_ins (bb, ins, store_ins);
14120 def_ins = store_ins;
14125 g_assert (store_opcode != OP_STOREV_MEMBASE);
14127 /* Try to fuse the store into the instruction itself */
14128 /* FIXME: Add more instructions */
14129 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14130 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14131 ins->inst_imm = ins->inst_c0;
14132 ins->inst_destbasereg = var->inst_basereg;
14133 ins->inst_offset = var->inst_offset;
14134 spec = INS_INFO (ins->opcode);
14135 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14136 ins->opcode = store_opcode;
14137 ins->inst_destbasereg = var->inst_basereg;
14138 ins->inst_offset = var->inst_offset;
14142 tmp_reg = ins->dreg;
14143 ins->dreg = ins->sreg2;
14144 ins->sreg2 = tmp_reg;
14147 spec2 [MONO_INST_DEST] = ' ';
14148 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14149 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14150 spec2 [MONO_INST_SRC3] = ' ';
14152 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14153 // FIXME: The backends expect the base reg to be in inst_basereg
14154 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14156 ins->inst_basereg = var->inst_basereg;
14157 ins->inst_offset = var->inst_offset;
14158 spec = INS_INFO (ins->opcode);
14160 /* printf ("INS: "); mono_print_ins (ins); */
14161 /* Create a store instruction */
14162 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14164 /* Insert it after the instruction */
14165 mono_bblock_insert_after_ins (bb, ins, store_ins);
14167 def_ins = store_ins;
14170 * We can't assign ins->dreg to var->dreg here, since the
14171 * sregs could use it. So set a flag, and do it after
14174 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)))
14175 dest_has_lvreg = TRUE;
14180 if (def_ins && !live_range_start [dreg]) {
14181 live_range_start [dreg] = def_ins;
14182 live_range_start_bb [dreg] = bb;
14185 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14188 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14189 tmp->inst_c1 = dreg;
14190 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14197 num_sregs = mono_inst_get_src_registers (ins, sregs);
14198 for (srcindex = 0; srcindex < 3; ++srcindex) {
14199 regtype = spec [MONO_INST_SRC1 + srcindex];
14200 sreg = sregs [srcindex];
14202 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14203 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14204 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14205 MonoInst *use_ins = ins;
14206 MonoInst *load_ins;
14207 guint32 load_opcode;
14209 if (var->opcode == OP_REGVAR) {
14210 sregs [srcindex] = var->dreg;
14211 //mono_inst_set_src_registers (ins, sregs);
14212 live_range_end [sreg] = use_ins;
14213 live_range_end_bb [sreg] = bb;
14215 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14218 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14219 /* var->dreg is a hreg */
14220 tmp->inst_c1 = sreg;
14221 mono_bblock_insert_after_ins (bb, ins, tmp);
14227 g_assert (var->opcode == OP_REGOFFSET);
14229 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14231 g_assert (load_opcode != OP_LOADV_MEMBASE);
14233 if (vreg_to_lvreg [sreg]) {
14234 g_assert (vreg_to_lvreg [sreg] != -1);
14236 /* The variable is already loaded to an lvreg */
14237 if (G_UNLIKELY (cfg->verbose_level > 2))
14238 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14239 sregs [srcindex] = vreg_to_lvreg [sreg];
14240 //mono_inst_set_src_registers (ins, sregs);
14244 /* Try to fuse the load into the instruction */
14245 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14246 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14247 sregs [0] = var->inst_basereg;
14248 //mono_inst_set_src_registers (ins, sregs);
14249 ins->inst_offset = var->inst_offset;
14250 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14251 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14252 sregs [1] = var->inst_basereg;
14253 //mono_inst_set_src_registers (ins, sregs);
14254 ins->inst_offset = var->inst_offset;
14256 if (MONO_IS_REAL_MOVE (ins)) {
14257 ins->opcode = OP_NOP;
14260 //printf ("%d ", srcindex); mono_print_ins (ins);
14262 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14264 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) {
14265 if (var->dreg == prev_dreg) {
14267 * sreg refers to the value loaded by the load
14268 * emitted below, but we need to use ins->dreg
14269 * since it refers to the store emitted earlier.
14273 g_assert (sreg != -1);
14274 vreg_to_lvreg [var->dreg] = sreg;
14275 if (lvregs_len >= lvregs_size) {
14276 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14277 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14278 lvregs = new_lvregs;
14281 lvregs [lvregs_len ++] = var->dreg;
14285 sregs [srcindex] = sreg;
14286 //mono_inst_set_src_registers (ins, sregs);
14288 #if SIZEOF_REGISTER != 8
14289 if (regtype == 'l') {
14290 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14291 mono_bblock_insert_before_ins (bb, ins, load_ins);
14292 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14293 mono_bblock_insert_before_ins (bb, ins, load_ins);
14294 use_ins = load_ins;
14299 #if SIZEOF_REGISTER == 4
14300 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14302 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14303 mono_bblock_insert_before_ins (bb, ins, load_ins);
14304 use_ins = load_ins;
14308 if (var->dreg < orig_next_vreg) {
14309 live_range_end [var->dreg] = use_ins;
14310 live_range_end_bb [var->dreg] = bb;
14313 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14316 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14317 tmp->inst_c1 = var->dreg;
14318 mono_bblock_insert_after_ins (bb, ins, tmp);
14322 mono_inst_set_src_registers (ins, sregs);
14324 if (dest_has_lvreg) {
14325 g_assert (ins->dreg != -1);
14326 vreg_to_lvreg [prev_dreg] = ins->dreg;
14327 if (lvregs_len >= lvregs_size) {
14328 guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14329 memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14330 lvregs = new_lvregs;
14333 lvregs [lvregs_len ++] = prev_dreg;
14334 dest_has_lvreg = FALSE;
14338 tmp_reg = ins->dreg;
14339 ins->dreg = ins->sreg2;
14340 ins->sreg2 = tmp_reg;
14343 if (MONO_IS_CALL (ins)) {
14344 /* Clear vreg_to_lvreg array */
14345 for (i = 0; i < lvregs_len; i++)
14346 vreg_to_lvreg [lvregs [i]] = 0;
14348 } else if (ins->opcode == OP_NOP) {
14350 MONO_INST_NULLIFY_SREGS (ins);
14353 if (cfg->verbose_level > 2)
14354 mono_print_ins_index (1, ins);
14357 /* Extend the live range based on the liveness info */
14358 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14359 for (i = 0; i < cfg->num_varinfo; i ++) {
14360 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14362 if (vreg_is_volatile (cfg, vi->vreg))
14363 /* The liveness info is incomplete */
14366 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14367 /* Live from at least the first ins of this bb */
14368 live_range_start [vi->vreg] = bb->code;
14369 live_range_start_bb [vi->vreg] = bb;
14372 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14373 /* Live at least until the last ins of this bb */
14374 live_range_end [vi->vreg] = bb->last_ins;
14375 live_range_end_bb [vi->vreg] = bb;
14382 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14383 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14385 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14386 for (i = 0; i < cfg->num_varinfo; ++i) {
14387 int vreg = MONO_VARINFO (cfg, i)->vreg;
14390 if (live_range_start [vreg]) {
14391 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14393 ins->inst_c1 = vreg;
14394 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14396 if (live_range_end [vreg]) {
14397 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14399 ins->inst_c1 = vreg;
14400 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14401 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14403 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14408 if (cfg->gsharedvt_locals_var_ins) {
14409 /* Nullify if unused */
14410 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14411 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14414 g_free (live_range_start);
14415 g_free (live_range_end);
14416 g_free (live_range_start_bb);
14417 g_free (live_range_end_bb);
14423 * - use 'iadd' instead of 'int_add'
14424 * - handling ovf opcodes: decompose in method_to_ir.
14425 * - unify iregs/fregs
14426 * -> partly done, the missing parts are:
14427 * - a more complete unification would involve unifying the hregs as well, so
14428 * code wouldn't need if (fp) all over the place. but that would mean the hregs
14429 * would no longer map to the machine hregs, so the code generators would need to
14430 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14431 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14432 * fp/non-fp branches speeds it up by about 15%.
14433 * - use sext/zext opcodes instead of shifts
14435 * - get rid of TEMPLOADs if possible and use vregs instead
14436 * - clean up usage of OP_P/OP_ opcodes
14437 * - cleanup usage of DUMMY_USE
14438 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
14440 * - set the stack type and allocate a dreg in the EMIT_NEW macros
14441 * - get rid of all the <foo>2 stuff when the new JIT is ready.
14442 * - make sure handle_stack_args () is called before the branch is emitted
14443 * - when the new IR is done, get rid of all unused stuff
14444 * - COMPARE/BEQ as separate instructions or unify them ?
14445 * - keeping them separate allows specialized compare instructions like
14446 * compare_imm, compare_membase
14447 * - most back ends unify fp compare+branch, fp compare+ceq
14448 * - integrate mono_save_args into inline_method
14449 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14450 * - handle long shift opts on 32 bit platforms somehow: they require
14451 * 3 sregs (2 for arg1 and 1 for arg2)
14452 * - make byref a 'normal' type.
14453 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14454 * variable if needed.
14455 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14456 * like inline_method.
14457 * - remove inlining restrictions
14458 * - fix LNEG and enable cfold of INEG
14459 * - generalize x86 optimizations like ldelema as a peephole optimization
14460 * - add store_mem_imm for amd64
14461 * - optimize the loading of the interruption flag in the managed->native wrappers
14462 * - avoid special handling of OP_NOP in passes
14463 * - move code inserting instructions into one function/macro.
14464 * - try a coalescing phase after liveness analysis
14465 * - add float -> vreg conversion + local optimizations on !x86
14466 * - figure out how to handle decomposed branches during optimizations, ie.
14467 * compare+branch, op_jump_table+op_br etc.
14468 * - promote RuntimeXHandles to vregs
14469 * - vtype cleanups:
14470 * - add a NEW_VARLOADA_VREG macro
14471 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
14472 * accessing vtype fields.
14473 * - get rid of I8CONST on 64 bit platforms
14474 * - dealing with the increase in code size due to branches created during opcode
14476 * - use extended basic blocks
14477 * - all parts of the JIT
14478 * - handle_global_vregs () && local regalloc
14479 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14480 * - sources of increase in code size:
14483 * - isinst and castclass
14484 * - lvregs not allocated to global registers even if used multiple times
14485 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14487 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14488 * - add all micro optimizations from the old JIT
14489 * - put tree optimizations into the deadce pass
14490 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14491 * specific function.
14492 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14493 * fcompare + branchCC.
14494 * - create a helper function for allocating a stack slot, taking into account
14495 * MONO_CFG_HAS_SPILLUP.
14497 * - merge the ia64 switch changes.
14498 * - optimize mono_regstate2_alloc_int/float.
14499 * - fix the pessimistic handling of variables accessed in exception handler blocks.
14500 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14501 * parts of the tree could be separated by other instructions, killing the tree
14502 * arguments, or stores killing loads etc. Also, should we fold loads into other
14503 * instructions if the result of the load is used multiple times ?
14504 * - make the REM_IMM optimization in mini-x86.c arch-independent.
14505 * - LAST MERGE: 108395.
14506 * - when returning vtypes in registers, generate IR and append it to the end of the
14507 * last bb instead of doing it in the epilog.
14508 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14516 - When to decompose opcodes:
14517 - earlier: this makes some optimizations hard to implement, since the low level IR
14518 no longer contains the neccessary information. But it is easier to do.
14519 - later: harder to implement, enables more optimizations.
14520 - Branches inside bblocks:
14521 - created when decomposing complex opcodes.
14522 - branches to another bblock: harmless, but not tracked by the branch
14523 optimizations, so need to branch to a label at the start of the bblock.
14524 - branches to inside the same bblock: very problematic, trips up the local
14525 reg allocator. Can be fixed by spitting the current bblock, but that is a
14526 complex operation, since some local vregs can become global vregs etc.
14527 - Local/global vregs:
14528 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14529 local register allocator.
14530 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14531 structure, created by mono_create_var (). Assigned to hregs or the stack by
14532 the global register allocator.
14533 - When to do optimizations like alu->alu_imm:
14534 - earlier -> saves work later on since the IR will be smaller/simpler
14535 - later -> can work on more instructions
14536 - Handling of valuetypes:
14537 - When a vtype is pushed on the stack, a new temporary is created, an
14538 instruction computing its address (LDADDR) is emitted and pushed on
14539 the stack. Need to optimize cases when the vtype is used immediately as in
14540 argument passing, stloc etc.
14541 - Instead of the to_end stuff in the old JIT, simply call the function handling
14542 the values on the stack before emitting the last instruction of the bb.
14545 #else /* !DISABLE_JIT */
14547 MONO_EMPTY_SOURCE_FILE (method_to_ir);
14549 #endif /* !DISABLE_JIT */