2 * method-to-ir.c: Convert CIL to the JIT internal representation
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
27 #ifdef HAVE_SYS_TIME_H
35 #include <mono/utils/memcheck.h>
37 #include <mono/metadata/abi-details.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/attrdefs.h>
40 #include <mono/metadata/loader.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/class.h>
43 #include <mono/metadata/object.h>
44 #include <mono/metadata/exception.h>
45 #include <mono/metadata/opcodes.h>
46 #include <mono/metadata/mono-endian.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/debug-helpers.h>
51 #include <mono/metadata/mono-debug.h>
52 #include <mono/metadata/gc-internal.h>
53 #include <mono/metadata/security-manager.h>
54 #include <mono/metadata/threads-types.h>
55 #include <mono/metadata/security-core-clr.h>
56 #include <mono/metadata/monitor.h>
57 #include <mono/metadata/profiler-private.h>
58 #include <mono/metadata/profiler.h>
59 #include <mono/metadata/debug-mono-symfile.h>
60 #include <mono/utils/mono-compiler.h>
61 #include <mono/utils/mono-memory-model.h>
62 #include <mono/metadata/mono-basic-block.h>
68 #include "jit-icalls.h"
70 #include "debugger-agent.h"
71 #include "seq-points.h"
73 #define BRANCH_COST 10
74 #define INLINE_LENGTH_LIMIT 20
76 /* These have 'cfg' as an implicit argument */
77 #define INLINE_FAILURE(msg) do { \
78 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
79 inline_failure (cfg, msg); \
80 goto exception_exit; \
83 #define CHECK_CFG_EXCEPTION do {\
84 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
85 goto exception_exit; \
87 #define METHOD_ACCESS_FAILURE(method, cmethod) do { \
88 method_access_failure ((cfg), (method), (cmethod)); \
89 goto exception_exit; \
91 #define FIELD_ACCESS_FAILURE(method, field) do { \
92 field_access_failure ((cfg), (method), (field)); \
93 goto exception_exit; \
95 #define GENERIC_SHARING_FAILURE(opcode) do { \
97 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
98 goto exception_exit; \
101 #define GSHAREDVT_FAILURE(opcode) do { \
102 if (cfg->gsharedvt) { \
103 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
104 goto exception_exit; \
107 #define OUT_OF_MEMORY_FAILURE do { \
108 mono_cfg_set_exception (cfg, MONO_EXCEPTION_OUT_OF_MEMORY); \
109 goto exception_exit; \
111 #define DISABLE_AOT(cfg) do { \
112 if ((cfg)->verbose_level >= 2) \
113 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
114 (cfg)->disable_aot = TRUE; \
116 #define LOAD_ERROR do { \
117 break_on_unverified (); \
118 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
119 goto exception_exit; \
122 #define TYPE_LOAD_ERROR(klass) do { \
123 cfg->exception_ptr = klass; \
127 #define CHECK_CFG_ERROR do {\
128 if (!mono_error_ok (&cfg->error)) { \
129 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
130 goto mono_error_exit; \
134 /* Determine whenever 'ins' represents a load of the 'this' argument */
135 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
137 static int ldind_to_load_membase (int opcode);
138 static int stind_to_store_membase (int opcode);
140 int mono_op_to_op_imm (int opcode);
141 int mono_op_to_op_imm_noemul (int opcode);
143 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
145 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
146 guchar *ip, guint real_offset, gboolean inline_always, MonoBasicBlock **out_cbb);
148 /* helper methods signatures */
149 static MonoMethodSignature *helper_sig_class_init_trampoline;
150 static MonoMethodSignature *helper_sig_domain_get;
151 static MonoMethodSignature *helper_sig_generic_class_init_trampoline;
152 static MonoMethodSignature *helper_sig_generic_class_init_trampoline_llvm;
153 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
154 static MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline;
155 static MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline_llvm;
156 static MonoMethodSignature *helper_sig_monitor_enter_v4_trampoline_llvm;
159 * Instruction metadata
167 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
168 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
174 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
179 /* keep in sync with the enum in mini.h */
182 #include "mini-ops.h"
187 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
188 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
190 * This should contain the index of the last sreg + 1. This is not the same
191 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
193 const gint8 ins_sreg_counts[] = {
194 #include "mini-ops.h"
199 #define MONO_INIT_VARINFO(vi,id) do { \
200 (vi)->range.first_use.pos.bid = 0xffff; \
206 mono_alloc_ireg (MonoCompile *cfg)
208 return alloc_ireg (cfg);
212 mono_alloc_lreg (MonoCompile *cfg)
214 return alloc_lreg (cfg);
218 mono_alloc_freg (MonoCompile *cfg)
220 return alloc_freg (cfg);
224 mono_alloc_preg (MonoCompile *cfg)
226 return alloc_preg (cfg);
230 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
232 return alloc_dreg (cfg, stack_type);
236 * mono_alloc_ireg_ref:
238 * Allocate an IREG, and mark it as holding a GC ref.
241 mono_alloc_ireg_ref (MonoCompile *cfg)
243 return alloc_ireg_ref (cfg);
247 * mono_alloc_ireg_mp:
249 * Allocate an IREG, and mark it as holding a managed pointer.
252 mono_alloc_ireg_mp (MonoCompile *cfg)
254 return alloc_ireg_mp (cfg);
258 * mono_alloc_ireg_copy:
260 * Allocate an IREG with the same GC type as VREG.
263 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
265 if (vreg_is_ref (cfg, vreg))
266 return alloc_ireg_ref (cfg);
267 else if (vreg_is_mp (cfg, vreg))
268 return alloc_ireg_mp (cfg);
270 return alloc_ireg (cfg);
274 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
279 type = mini_get_underlying_type (cfg, type);
281 switch (type->type) {
284 case MONO_TYPE_BOOLEAN:
296 case MONO_TYPE_FNPTR:
298 case MONO_TYPE_CLASS:
299 case MONO_TYPE_STRING:
300 case MONO_TYPE_OBJECT:
301 case MONO_TYPE_SZARRAY:
302 case MONO_TYPE_ARRAY:
306 #if SIZEOF_REGISTER == 8
312 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
315 case MONO_TYPE_VALUETYPE:
316 if (type->data.klass->enumtype) {
317 type = mono_class_enum_basetype (type->data.klass);
320 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
323 case MONO_TYPE_TYPEDBYREF:
325 case MONO_TYPE_GENERICINST:
326 type = &type->data.generic_class->container_class->byval_arg;
330 g_assert (cfg->generic_sharing_context);
331 if (mini_type_var_is_vt (cfg, type))
334 return mono_type_to_regmove (cfg, mini_get_underlying_type (cfg, type));
336 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
342 mono_print_bb (MonoBasicBlock *bb, const char *msg)
347 printf ("\n%s %d: [IN: ", msg, bb->block_num);
348 for (i = 0; i < bb->in_count; ++i)
349 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
351 for (i = 0; i < bb->out_count; ++i)
352 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
354 for (tree = bb->code; tree; tree = tree->next)
355 mono_print_ins_index (-1, tree);
359 mono_create_helper_signatures (void)
361 helper_sig_domain_get = mono_create_icall_signature ("ptr");
362 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
363 helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
364 helper_sig_generic_class_init_trampoline_llvm = mono_create_icall_signature ("void ptr");
365 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
366 helper_sig_monitor_enter_exit_trampoline = mono_create_icall_signature ("void");
367 helper_sig_monitor_enter_exit_trampoline_llvm = mono_create_icall_signature ("void object");
368 helper_sig_monitor_enter_v4_trampoline_llvm = mono_create_icall_signature ("void object ptr");
371 static MONO_NEVER_INLINE void
372 break_on_unverified (void)
374 if (mini_get_debug_options ()->break_on_unverified)
378 static MONO_NEVER_INLINE void
379 method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
381 char *method_fname = mono_method_full_name (method, TRUE);
382 char *cil_method_fname = mono_method_full_name (cil_method, TRUE);
383 mono_cfg_set_exception (cfg, MONO_EXCEPTION_METHOD_ACCESS);
384 cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);
385 g_free (method_fname);
386 g_free (cil_method_fname);
389 static MONO_NEVER_INLINE void
390 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
392 char *method_fname = mono_method_full_name (method, TRUE);
393 char *field_fname = mono_field_full_name (field);
394 mono_cfg_set_exception (cfg, MONO_EXCEPTION_FIELD_ACCESS);
395 cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
396 g_free (method_fname);
397 g_free (field_fname);
400 static MONO_NEVER_INLINE void
401 inline_failure (MonoCompile *cfg, const char *msg)
403 if (cfg->verbose_level >= 2)
404 printf ("inline failed: %s\n", msg);
405 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
408 static MONO_NEVER_INLINE void
409 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
411 if (cfg->verbose_level > 2) \
412 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__);
413 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
416 static MONO_NEVER_INLINE void
417 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
419 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);
420 if (cfg->verbose_level >= 2)
421 printf ("%s\n", cfg->exception_message);
422 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
426 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
427 * foo<T> (int i) { ldarg.0; box T; }
429 #define UNVERIFIED do { \
430 if (cfg->gsharedvt) { \
431 if (cfg->verbose_level > 2) \
432 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
433 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
434 goto exception_exit; \
436 break_on_unverified (); \
440 #define GET_BBLOCK(cfg,tblock,ip) do { \
441 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
443 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
444 NEW_BBLOCK (cfg, (tblock)); \
445 (tblock)->cil_code = (ip); \
446 ADD_BBLOCK (cfg, (tblock)); \
450 #if defined(TARGET_X86) || defined(TARGET_AMD64)
451 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
452 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
453 (dest)->dreg = alloc_ireg_mp ((cfg)); \
454 (dest)->sreg1 = (sr1); \
455 (dest)->sreg2 = (sr2); \
456 (dest)->inst_imm = (imm); \
457 (dest)->backend.shift_amount = (shift); \
458 MONO_ADD_INS ((cfg)->cbb, (dest)); \
462 /* Emit conversions so both operands of a binary opcode are of the same type */
464 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
466 MonoInst *arg1 = *arg1_ref;
467 MonoInst *arg2 = *arg2_ref;
470 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
471 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
474 /* Mixing r4/r8 is allowed by the spec */
475 if (arg1->type == STACK_R4) {
476 int dreg = alloc_freg (cfg);
478 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
479 conv->type = STACK_R8;
483 if (arg2->type == STACK_R4) {
484 int dreg = alloc_freg (cfg);
486 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
487 conv->type = STACK_R8;
493 #if SIZEOF_REGISTER == 8
494 /* FIXME: Need to add many more cases */
495 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
498 int dr = alloc_preg (cfg);
499 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
500 (ins)->sreg2 = widen->dreg;
505 #define ADD_BINOP(op) do { \
506 MONO_INST_NEW (cfg, ins, (op)); \
508 ins->sreg1 = sp [0]->dreg; \
509 ins->sreg2 = sp [1]->dreg; \
510 type_from_op (cfg, ins, sp [0], sp [1]); \
512 /* Have to insert a widening op */ \
513 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
514 ins->dreg = alloc_dreg ((cfg), (ins)->type); \
515 MONO_ADD_INS ((cfg)->cbb, (ins)); \
516 *sp++ = mono_decompose_opcode ((cfg), (ins), &bblock); \
519 #define ADD_UNOP(op) do { \
520 MONO_INST_NEW (cfg, ins, (op)); \
522 ins->sreg1 = sp [0]->dreg; \
523 type_from_op (cfg, ins, sp [0], NULL); \
525 (ins)->dreg = alloc_dreg ((cfg), (ins)->type); \
526 MONO_ADD_INS ((cfg)->cbb, (ins)); \
527 *sp++ = mono_decompose_opcode (cfg, ins, &bblock); \
530 #define ADD_BINCOND(next_block) do { \
533 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
534 cmp->sreg1 = sp [0]->dreg; \
535 cmp->sreg2 = sp [1]->dreg; \
536 type_from_op (cfg, cmp, sp [0], sp [1]); \
538 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
539 type_from_op (cfg, ins, sp [0], sp [1]); \
540 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
541 GET_BBLOCK (cfg, tblock, target); \
542 link_bblock (cfg, bblock, tblock); \
543 ins->inst_true_bb = tblock; \
544 if ((next_block)) { \
545 link_bblock (cfg, bblock, (next_block)); \
546 ins->inst_false_bb = (next_block); \
547 start_new_bblock = 1; \
549 GET_BBLOCK (cfg, tblock, ip); \
550 link_bblock (cfg, bblock, tblock); \
551 ins->inst_false_bb = tblock; \
552 start_new_bblock = 2; \
554 if (sp != stack_start) { \
555 handle_stack_args (cfg, stack_start, sp - stack_start); \
556 CHECK_UNVERIFIABLE (cfg); \
558 MONO_ADD_INS (bblock, cmp); \
559 MONO_ADD_INS (bblock, ins); \
563 * link_bblock: Links two basic blocks
565 * links two basic blocks in the control flow graph, the 'from'
566 * argument is the starting block and the 'to' argument is the block
567 * the control flow ends to after 'from'.
570 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
572 MonoBasicBlock **newa;
576 if (from->cil_code) {
578 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
580 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
583 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
585 printf ("edge from entry to exit\n");
590 for (i = 0; i < from->out_count; ++i) {
591 if (to == from->out_bb [i]) {
597 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
598 for (i = 0; i < from->out_count; ++i) {
599 newa [i] = from->out_bb [i];
607 for (i = 0; i < to->in_count; ++i) {
608 if (from == to->in_bb [i]) {
614 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
615 for (i = 0; i < to->in_count; ++i) {
616 newa [i] = to->in_bb [i];
625 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
627 link_bblock (cfg, from, to);
631 * mono_find_block_region:
633 * We mark each basic block with a region ID. We use that to avoid BB
634 * optimizations when blocks are in different regions.
637 * A region token that encodes where this region is, and information
638 * about the clause owner for this block.
640 * The region encodes the try/catch/filter clause that owns this block
641 * as well as the type. -1 is a special value that represents a block
642 * that is in none of try/catch/filter.
645 mono_find_block_region (MonoCompile *cfg, int offset)
647 MonoMethodHeader *header = cfg->header;
648 MonoExceptionClause *clause;
651 for (i = 0; i < header->num_clauses; ++i) {
652 clause = &header->clauses [i];
653 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
654 (offset < (clause->handler_offset)))
655 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
657 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
658 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
659 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
660 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
661 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
663 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
666 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
667 return ((i + 1) << 8) | clause->flags;
674 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
676 MonoMethodHeader *header = cfg->header;
677 MonoExceptionClause *clause;
681 for (i = 0; i < header->num_clauses; ++i) {
682 clause = &header->clauses [i];
683 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
684 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
685 if (clause->flags == type)
686 res = g_list_append (res, clause);
693 mono_create_spvar_for_region (MonoCompile *cfg, int region)
697 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
701 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
702 /* prevent it from being register allocated */
703 var->flags |= MONO_INST_VOLATILE;
705 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
709 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
711 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
715 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
719 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
723 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
724 /* prevent it from being register allocated */
725 var->flags |= MONO_INST_VOLATILE;
727 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
733 * Returns the type used in the eval stack when @type is loaded.
734 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
737 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
741 type = mini_get_underlying_type (cfg, type);
742 inst->klass = klass = mono_class_from_mono_type (type);
744 inst->type = STACK_MP;
749 switch (type->type) {
751 inst->type = STACK_INV;
755 case MONO_TYPE_BOOLEAN:
761 inst->type = STACK_I4;
766 case MONO_TYPE_FNPTR:
767 inst->type = STACK_PTR;
769 case MONO_TYPE_CLASS:
770 case MONO_TYPE_STRING:
771 case MONO_TYPE_OBJECT:
772 case MONO_TYPE_SZARRAY:
773 case MONO_TYPE_ARRAY:
774 inst->type = STACK_OBJ;
778 inst->type = STACK_I8;
781 inst->type = cfg->r4_stack_type;
784 inst->type = STACK_R8;
786 case MONO_TYPE_VALUETYPE:
787 if (type->data.klass->enumtype) {
788 type = mono_class_enum_basetype (type->data.klass);
792 inst->type = STACK_VTYPE;
795 case MONO_TYPE_TYPEDBYREF:
796 inst->klass = mono_defaults.typed_reference_class;
797 inst->type = STACK_VTYPE;
799 case MONO_TYPE_GENERICINST:
800 type = &type->data.generic_class->container_class->byval_arg;
804 g_assert (cfg->generic_sharing_context);
805 if (mini_is_gsharedvt_type (cfg, type)) {
806 g_assert (cfg->gsharedvt);
807 inst->type = STACK_VTYPE;
809 type_to_eval_stack_type (cfg, mini_get_underlying_type (cfg, type), inst);
813 g_error ("unknown type 0x%02x in eval stack type", type->type);
818 * The following tables are used to quickly validate the IL code in type_from_op ().
821 bin_num_table [STACK_MAX] [STACK_MAX] = {
822 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
823 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
824 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
825 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
826 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
827 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
828 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
829 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
830 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
835 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
838 /* reduce the size of this table */
840 bin_int_table [STACK_MAX] [STACK_MAX] = {
841 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
842 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
843 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
844 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
845 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
846 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
847 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
848 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
852 bin_comp_table [STACK_MAX] [STACK_MAX] = {
853 /* Inv i L p F & O vt r4 */
855 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
856 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
857 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
858 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
859 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
860 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
861 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
862 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
865 /* reduce the size of this table */
867 shift_table [STACK_MAX] [STACK_MAX] = {
868 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
869 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
870 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
871 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
872 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
873 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
874 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
875 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
879 * Tables to map from the non-specific opcode to the matching
880 * type-specific opcode.
882 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
884 binops_op_map [STACK_MAX] = {
885 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
888 /* handles from CEE_NEG to CEE_CONV_U8 */
890 unops_op_map [STACK_MAX] = {
891 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
894 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
896 ovfops_op_map [STACK_MAX] = {
897 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
900 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
902 ovf2ops_op_map [STACK_MAX] = {
903 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
906 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
908 ovf3ops_op_map [STACK_MAX] = {
909 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
912 /* handles from CEE_BEQ to CEE_BLT_UN */
914 beqops_op_map [STACK_MAX] = {
915 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
918 /* handles from CEE_CEQ to CEE_CLT_UN */
920 ceqops_op_map [STACK_MAX] = {
921 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
925 * Sets ins->type (the type on the eval stack) according to the
926 * type of the opcode and the arguments to it.
927 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
929 * FIXME: this function sets ins->type unconditionally in some cases, but
930 * it should set it to invalid for some types (a conv.x on an object)
933 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
935 switch (ins->opcode) {
942 /* FIXME: check unverifiable args for STACK_MP */
943 ins->type = bin_num_table [src1->type] [src2->type];
944 ins->opcode += binops_op_map [ins->type];
951 ins->type = bin_int_table [src1->type] [src2->type];
952 ins->opcode += binops_op_map [ins->type];
957 ins->type = shift_table [src1->type] [src2->type];
958 ins->opcode += binops_op_map [ins->type];
963 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
964 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
965 ins->opcode = OP_LCOMPARE;
966 else if (src1->type == STACK_R4)
967 ins->opcode = OP_RCOMPARE;
968 else if (src1->type == STACK_R8)
969 ins->opcode = OP_FCOMPARE;
971 ins->opcode = OP_ICOMPARE;
973 case OP_ICOMPARE_IMM:
974 ins->type = bin_comp_table [src1->type] [src1->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_IMM;
988 ins->opcode += beqops_op_map [src1->type];
991 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
992 ins->opcode += ceqops_op_map [src1->type];
998 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
999 ins->opcode += ceqops_op_map [src1->type];
1003 ins->type = neg_table [src1->type];
1004 ins->opcode += unops_op_map [ins->type];
1007 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1008 ins->type = src1->type;
1010 ins->type = STACK_INV;
1011 ins->opcode += unops_op_map [ins->type];
1017 ins->type = STACK_I4;
1018 ins->opcode += unops_op_map [src1->type];
1021 ins->type = STACK_R8;
1022 switch (src1->type) {
1025 ins->opcode = OP_ICONV_TO_R_UN;
1028 ins->opcode = OP_LCONV_TO_R_UN;
1032 case CEE_CONV_OVF_I1:
1033 case CEE_CONV_OVF_U1:
1034 case CEE_CONV_OVF_I2:
1035 case CEE_CONV_OVF_U2:
1036 case CEE_CONV_OVF_I4:
1037 case CEE_CONV_OVF_U4:
1038 ins->type = STACK_I4;
1039 ins->opcode += ovf3ops_op_map [src1->type];
1041 case CEE_CONV_OVF_I_UN:
1042 case CEE_CONV_OVF_U_UN:
1043 ins->type = STACK_PTR;
1044 ins->opcode += ovf2ops_op_map [src1->type];
1046 case CEE_CONV_OVF_I1_UN:
1047 case CEE_CONV_OVF_I2_UN:
1048 case CEE_CONV_OVF_I4_UN:
1049 case CEE_CONV_OVF_U1_UN:
1050 case CEE_CONV_OVF_U2_UN:
1051 case CEE_CONV_OVF_U4_UN:
1052 ins->type = STACK_I4;
1053 ins->opcode += ovf2ops_op_map [src1->type];
1056 ins->type = STACK_PTR;
1057 switch (src1->type) {
1059 ins->opcode = OP_ICONV_TO_U;
1063 #if SIZEOF_VOID_P == 8
1064 ins->opcode = OP_LCONV_TO_U;
1066 ins->opcode = OP_MOVE;
1070 ins->opcode = OP_LCONV_TO_U;
1073 ins->opcode = OP_FCONV_TO_U;
1079 ins->type = STACK_I8;
1080 ins->opcode += unops_op_map [src1->type];
1082 case CEE_CONV_OVF_I8:
1083 case CEE_CONV_OVF_U8:
1084 ins->type = STACK_I8;
1085 ins->opcode += ovf3ops_op_map [src1->type];
1087 case CEE_CONV_OVF_U8_UN:
1088 case CEE_CONV_OVF_I8_UN:
1089 ins->type = STACK_I8;
1090 ins->opcode += ovf2ops_op_map [src1->type];
1093 ins->type = cfg->r4_stack_type;
1094 ins->opcode += unops_op_map [src1->type];
1097 ins->type = STACK_R8;
1098 ins->opcode += unops_op_map [src1->type];
1101 ins->type = STACK_R8;
1105 ins->type = STACK_I4;
1106 ins->opcode += ovfops_op_map [src1->type];
1109 case CEE_CONV_OVF_I:
1110 case CEE_CONV_OVF_U:
1111 ins->type = STACK_PTR;
1112 ins->opcode += ovfops_op_map [src1->type];
1115 case CEE_ADD_OVF_UN:
1117 case CEE_MUL_OVF_UN:
1119 case CEE_SUB_OVF_UN:
1120 ins->type = bin_num_table [src1->type] [src2->type];
1121 ins->opcode += ovfops_op_map [src1->type];
1122 if (ins->type == STACK_R8)
1123 ins->type = STACK_INV;
1125 case OP_LOAD_MEMBASE:
1126 ins->type = STACK_PTR;
1128 case OP_LOADI1_MEMBASE:
1129 case OP_LOADU1_MEMBASE:
1130 case OP_LOADI2_MEMBASE:
1131 case OP_LOADU2_MEMBASE:
1132 case OP_LOADI4_MEMBASE:
1133 case OP_LOADU4_MEMBASE:
1134 ins->type = STACK_PTR;
1136 case OP_LOADI8_MEMBASE:
1137 ins->type = STACK_I8;
1139 case OP_LOADR4_MEMBASE:
1140 ins->type = cfg->r4_stack_type;
1142 case OP_LOADR8_MEMBASE:
1143 ins->type = STACK_R8;
1146 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1150 if (ins->type == STACK_MP)
1151 ins->klass = mono_defaults.object_class;
1156 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1162 param_table [STACK_MAX] [STACK_MAX] = {
1167 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1171 switch (args->type) {
1181 for (i = 0; i < sig->param_count; ++i) {
1182 switch (args [i].type) {
1186 if (!sig->params [i]->byref)
1190 if (sig->params [i]->byref)
1192 switch (sig->params [i]->type) {
1193 case MONO_TYPE_CLASS:
1194 case MONO_TYPE_STRING:
1195 case MONO_TYPE_OBJECT:
1196 case MONO_TYPE_SZARRAY:
1197 case MONO_TYPE_ARRAY:
1204 if (sig->params [i]->byref)
1206 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1215 /*if (!param_table [args [i].type] [sig->params [i]->type])
1223 * When we need a pointer to the current domain many times in a method, we
1224 * call mono_domain_get() once and we store the result in a local variable.
1225 * This function returns the variable that represents the MonoDomain*.
1227 inline static MonoInst *
1228 mono_get_domainvar (MonoCompile *cfg)
1230 if (!cfg->domainvar)
1231 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1232 return cfg->domainvar;
1236 * The got_var contains the address of the Global Offset Table when AOT
1240 mono_get_got_var (MonoCompile *cfg)
1242 #ifdef MONO_ARCH_NEED_GOT_VAR
1243 if (!cfg->compile_aot)
1245 if (!cfg->got_var) {
1246 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1248 return cfg->got_var;
1255 mono_get_vtable_var (MonoCompile *cfg)
1257 g_assert (cfg->generic_sharing_context);
1259 if (!cfg->rgctx_var) {
1260 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1261 /* force the var to be stack allocated */
1262 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1265 return cfg->rgctx_var;
1269 type_from_stack_type (MonoInst *ins) {
1270 switch (ins->type) {
1271 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1272 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1273 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1274 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1275 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1277 return &ins->klass->this_arg;
1278 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1279 case STACK_VTYPE: return &ins->klass->byval_arg;
1281 g_error ("stack type %d to monotype not handled\n", ins->type);
1286 static G_GNUC_UNUSED int
1287 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1289 t = mono_type_get_underlying_type (t);
1293 case MONO_TYPE_BOOLEAN:
1296 case MONO_TYPE_CHAR:
1303 case MONO_TYPE_FNPTR:
1305 case MONO_TYPE_CLASS:
1306 case MONO_TYPE_STRING:
1307 case MONO_TYPE_OBJECT:
1308 case MONO_TYPE_SZARRAY:
1309 case MONO_TYPE_ARRAY:
1315 return cfg->r4_stack_type;
1318 case MONO_TYPE_VALUETYPE:
1319 case MONO_TYPE_TYPEDBYREF:
1321 case MONO_TYPE_GENERICINST:
1322 if (mono_type_generic_inst_is_valuetype (t))
1328 g_assert_not_reached ();
1335 array_access_to_klass (int opcode)
1339 return mono_defaults.byte_class;
1341 return mono_defaults.uint16_class;
1344 return mono_defaults.int_class;
1347 return mono_defaults.sbyte_class;
1350 return mono_defaults.int16_class;
1353 return mono_defaults.int32_class;
1355 return mono_defaults.uint32_class;
1358 return mono_defaults.int64_class;
1361 return mono_defaults.single_class;
1364 return mono_defaults.double_class;
1365 case CEE_LDELEM_REF:
1366 case CEE_STELEM_REF:
1367 return mono_defaults.object_class;
1369 g_assert_not_reached ();
1375 * We try to share variables when possible
1378 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1383 /* inlining can result in deeper stacks */
1384 if (slot >= cfg->header->max_stack)
1385 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1387 pos = ins->type - 1 + slot * STACK_MAX;
1389 switch (ins->type) {
1396 if ((vnum = cfg->intvars [pos]))
1397 return cfg->varinfo [vnum];
1398 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1399 cfg->intvars [pos] = res->inst_c0;
1402 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1408 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1411 * Don't use this if a generic_context is set, since that means AOT can't
1412 * look up the method using just the image+token.
1413 * table == 0 means this is a reference made from a wrapper.
1415 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1416 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1417 jump_info_token->image = image;
1418 jump_info_token->token = token;
1419 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1424 * This function is called to handle items that are left on the evaluation stack
1425 * at basic block boundaries. What happens is that we save the values to local variables
1426 * and we reload them later when first entering the target basic block (with the
1427 * handle_loaded_temps () function).
1428 * A single joint point will use the same variables (stored in the array bb->out_stack or
1429 * bb->in_stack, if the basic block is before or after the joint point).
1431 * This function needs to be called _before_ emitting the last instruction of
1432 * the bb (i.e. before emitting a branch).
1433 * If the stack merge fails at a join point, cfg->unverifiable is set.
1436 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1439 MonoBasicBlock *bb = cfg->cbb;
1440 MonoBasicBlock *outb;
1441 MonoInst *inst, **locals;
1446 if (cfg->verbose_level > 3)
1447 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1448 if (!bb->out_scount) {
1449 bb->out_scount = count;
1450 //printf ("bblock %d has out:", bb->block_num);
1452 for (i = 0; i < bb->out_count; ++i) {
1453 outb = bb->out_bb [i];
1454 /* exception handlers are linked, but they should not be considered for stack args */
1455 if (outb->flags & BB_EXCEPTION_HANDLER)
1457 //printf (" %d", outb->block_num);
1458 if (outb->in_stack) {
1460 bb->out_stack = outb->in_stack;
1466 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1467 for (i = 0; i < count; ++i) {
1469 * try to reuse temps already allocated for this purpouse, if they occupy the same
1470 * stack slot and if they are of the same type.
1471 * This won't cause conflicts since if 'local' is used to
1472 * store one of the values in the in_stack of a bblock, then
1473 * the same variable will be used for the same outgoing stack
1475 * This doesn't work when inlining methods, since the bblocks
1476 * in the inlined methods do not inherit their in_stack from
1477 * the bblock they are inlined to. See bug #58863 for an
1480 if (cfg->inlined_method)
1481 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1483 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1488 for (i = 0; i < bb->out_count; ++i) {
1489 outb = bb->out_bb [i];
1490 /* exception handlers are linked, but they should not be considered for stack args */
1491 if (outb->flags & BB_EXCEPTION_HANDLER)
1493 if (outb->in_scount) {
1494 if (outb->in_scount != bb->out_scount) {
1495 cfg->unverifiable = TRUE;
1498 continue; /* check they are the same locals */
1500 outb->in_scount = count;
1501 outb->in_stack = bb->out_stack;
1504 locals = bb->out_stack;
1506 for (i = 0; i < count; ++i) {
1507 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1508 inst->cil_code = sp [i]->cil_code;
1509 sp [i] = locals [i];
1510 if (cfg->verbose_level > 3)
1511 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1515 * It is possible that the out bblocks already have in_stack assigned, and
1516 * the in_stacks differ. In this case, we will store to all the different
1523 /* Find a bblock which has a different in_stack */
1525 while (bindex < bb->out_count) {
1526 outb = bb->out_bb [bindex];
1527 /* exception handlers are linked, but they should not be considered for stack args */
1528 if (outb->flags & BB_EXCEPTION_HANDLER) {
1532 if (outb->in_stack != locals) {
1533 for (i = 0; i < count; ++i) {
1534 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1535 inst->cil_code = sp [i]->cil_code;
1536 sp [i] = locals [i];
1537 if (cfg->verbose_level > 3)
1538 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1540 locals = outb->in_stack;
1549 /* Emit code which loads interface_offsets [klass->interface_id]
1550 * The array is stored in memory before vtable.
1553 mini_emit_load_intf_reg_vtable (MonoCompile *cfg, int intf_reg, int vtable_reg, MonoClass *klass)
1555 if (cfg->compile_aot) {
1556 int ioffset_reg = alloc_preg (cfg);
1557 int iid_reg = alloc_preg (cfg);
1559 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_ADJUSTED_IID);
1560 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ioffset_reg, iid_reg, vtable_reg);
1561 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, intf_reg, ioffset_reg, 0);
1564 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, intf_reg, vtable_reg, -((klass->interface_id + 1) * SIZEOF_VOID_P));
1569 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1571 int ibitmap_reg = alloc_preg (cfg);
1572 #ifdef COMPRESSED_INTERFACE_BITMAP
1574 MonoInst *res, *ins;
1575 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1576 MONO_ADD_INS (cfg->cbb, ins);
1578 if (cfg->compile_aot)
1579 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_IID, klass);
1581 EMIT_NEW_ICONST (cfg, args [1], klass->interface_id);
1582 res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1583 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1585 int ibitmap_byte_reg = alloc_preg (cfg);
1587 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1589 if (cfg->compile_aot) {
1590 int iid_reg = alloc_preg (cfg);
1591 int shifted_iid_reg = alloc_preg (cfg);
1592 int ibitmap_byte_address_reg = alloc_preg (cfg);
1593 int masked_iid_reg = alloc_preg (cfg);
1594 int iid_one_bit_reg = alloc_preg (cfg);
1595 int iid_bit_reg = alloc_preg (cfg);
1596 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1597 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1598 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1599 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1600 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1601 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1602 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1603 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1605 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1606 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1612 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1613 * stored in "klass_reg" implements the interface "klass".
1616 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1618 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1622 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1623 * stored in "vtable_reg" implements the interface "klass".
1626 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1628 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1632 * Emit code which checks whenever the interface id of @klass is smaller than
1633 * than the value given by max_iid_reg.
1636 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1637 MonoBasicBlock *false_target)
1639 if (cfg->compile_aot) {
1640 int iid_reg = alloc_preg (cfg);
1641 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1642 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1645 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1647 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1649 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1652 /* Same as above, but obtains max_iid from a vtable */
1654 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1655 MonoBasicBlock *false_target)
1657 int max_iid_reg = alloc_preg (cfg);
1659 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1660 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1663 /* Same as above, but obtains max_iid from a klass */
1665 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1666 MonoBasicBlock *false_target)
1668 int max_iid_reg = alloc_preg (cfg);
1670 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1671 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1675 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1677 int idepth_reg = alloc_preg (cfg);
1678 int stypes_reg = alloc_preg (cfg);
1679 int stype = alloc_preg (cfg);
1681 mono_class_setup_supertypes (klass);
1683 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1684 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1685 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1686 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1688 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1689 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1691 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1692 } else if (cfg->compile_aot) {
1693 int const_reg = alloc_preg (cfg);
1694 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1695 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1697 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1699 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1703 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1705 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1709 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1711 int intf_reg = alloc_preg (cfg);
1713 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1714 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1715 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1717 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1719 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1723 * Variant of the above that takes a register to the class, not the vtable.
1726 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1728 int intf_bit_reg = alloc_preg (cfg);
1730 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1731 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1732 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1734 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1736 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1740 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1743 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1744 } else if (cfg->compile_aot) {
1745 int const_reg = alloc_preg (cfg);
1746 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1747 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1749 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1751 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1755 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1757 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1761 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1763 if (cfg->compile_aot) {
1764 int const_reg = alloc_preg (cfg);
1765 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1766 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1768 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1770 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1774 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1777 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1780 int rank_reg = alloc_preg (cfg);
1781 int eclass_reg = alloc_preg (cfg);
1783 g_assert (!klass_inst);
1784 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1785 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1786 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1787 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1788 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1789 if (klass->cast_class == mono_defaults.object_class) {
1790 int parent_reg = alloc_preg (cfg);
1791 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1792 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1793 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1794 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1795 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1796 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1797 } else if (klass->cast_class == mono_defaults.enum_class) {
1798 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1799 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1800 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1802 // Pass -1 as obj_reg to skip the check below for arrays of arrays
1803 mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1806 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1807 /* Check that the object is a vector too */
1808 int bounds_reg = alloc_preg (cfg);
1809 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1810 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1811 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1814 int idepth_reg = alloc_preg (cfg);
1815 int stypes_reg = alloc_preg (cfg);
1816 int stype = alloc_preg (cfg);
1818 mono_class_setup_supertypes (klass);
1820 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1821 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1822 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1823 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1825 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1826 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1827 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1832 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1834 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1838 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1842 g_assert (val == 0);
1847 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1850 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1853 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1856 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1858 #if SIZEOF_REGISTER == 8
1860 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1866 val_reg = alloc_preg (cfg);
1868 if (SIZEOF_REGISTER == 8)
1869 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1871 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1874 /* This could be optimized further if neccesary */
1876 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1883 #if !NO_UNALIGNED_ACCESS
1884 if (SIZEOF_REGISTER == 8) {
1886 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1891 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1899 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1904 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1909 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1916 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1923 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1924 g_assert (size < 10000);
1927 /* This could be optimized further if neccesary */
1929 cur_reg = alloc_preg (cfg);
1930 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1931 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1938 #if !NO_UNALIGNED_ACCESS
1939 if (SIZEOF_REGISTER == 8) {
1941 cur_reg = alloc_preg (cfg);
1942 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1943 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1952 cur_reg = alloc_preg (cfg);
1953 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1954 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1960 cur_reg = alloc_preg (cfg);
1961 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1962 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1968 cur_reg = alloc_preg (cfg);
1969 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1970 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1978 emit_tls_set (MonoCompile *cfg, int sreg1, int tls_key)
1982 if (cfg->compile_aot) {
1983 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1984 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1986 ins->sreg2 = c->dreg;
1987 MONO_ADD_INS (cfg->cbb, ins);
1989 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1991 ins->inst_offset = mini_get_tls_offset (tls_key);
1992 MONO_ADD_INS (cfg->cbb, ins);
1999 * Emit IR to push the current LMF onto the LMF stack.
2002 emit_push_lmf (MonoCompile *cfg)
2005 * Emit IR to push the LMF:
2006 * lmf_addr = <lmf_addr from tls>
2007 * lmf->lmf_addr = lmf_addr
2008 * lmf->prev_lmf = *lmf_addr
2011 int lmf_reg, prev_lmf_reg;
2012 MonoInst *ins, *lmf_ins;
2017 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2018 /* Load current lmf */
2019 lmf_ins = mono_get_lmf_intrinsic (cfg);
2021 MONO_ADD_INS (cfg->cbb, lmf_ins);
2022 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2023 lmf_reg = ins->dreg;
2024 /* Save previous_lmf */
2025 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
2027 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
2030 * Store lmf_addr in a variable, so it can be allocated to a global register.
2032 if (!cfg->lmf_addr_var)
2033 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2036 ins = mono_get_jit_tls_intrinsic (cfg);
2038 int jit_tls_dreg = ins->dreg;
2040 MONO_ADD_INS (cfg->cbb, ins);
2041 lmf_reg = alloc_preg (cfg);
2042 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2044 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2047 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2049 MONO_ADD_INS (cfg->cbb, lmf_ins);
2052 MonoInst *args [16], *jit_tls_ins, *ins;
2054 /* Inline mono_get_lmf_addr () */
2055 /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2057 /* Load mono_jit_tls_id */
2058 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2059 /* call pthread_getspecific () */
2060 jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2061 /* lmf_addr = &jit_tls->lmf */
2062 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2065 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2069 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2071 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2072 lmf_reg = ins->dreg;
2074 prev_lmf_reg = alloc_preg (cfg);
2075 /* Save previous_lmf */
2076 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2077 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2079 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2086 * Emit IR to pop the current LMF from the LMF stack.
2089 emit_pop_lmf (MonoCompile *cfg)
2091 int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2097 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2098 lmf_reg = ins->dreg;
2100 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2101 /* Load previous_lmf */
2102 prev_lmf_reg = alloc_preg (cfg);
2103 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2105 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2108 * Emit IR to pop the LMF:
2109 * *(lmf->lmf_addr) = lmf->prev_lmf
2111 /* This could be called before emit_push_lmf () */
2112 if (!cfg->lmf_addr_var)
2113 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2114 lmf_addr_reg = cfg->lmf_addr_var->dreg;
2116 prev_lmf_reg = alloc_preg (cfg);
2117 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2118 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2123 emit_instrumentation_call (MonoCompile *cfg, void *func)
2125 MonoInst *iargs [1];
2128 * Avoid instrumenting inlined methods since it can
2129 * distort profiling results.
2131 if (cfg->method != cfg->current_method)
2134 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2135 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2136 mono_emit_jit_icall (cfg, func, iargs);
2141 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt, MonoGenericSharingContext *gsctx)
2144 type = mini_get_underlying_type (cfg, type);
2145 switch (type->type) {
2146 case MONO_TYPE_VOID:
2147 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2150 case MONO_TYPE_BOOLEAN:
2153 case MONO_TYPE_CHAR:
2156 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2160 case MONO_TYPE_FNPTR:
2161 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2162 case MONO_TYPE_CLASS:
2163 case MONO_TYPE_STRING:
2164 case MONO_TYPE_OBJECT:
2165 case MONO_TYPE_SZARRAY:
2166 case MONO_TYPE_ARRAY:
2167 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2170 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2173 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2175 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2177 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2178 case MONO_TYPE_VALUETYPE:
2179 if (type->data.klass->enumtype) {
2180 type = mono_class_enum_basetype (type->data.klass);
2183 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2184 case MONO_TYPE_TYPEDBYREF:
2185 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2186 case MONO_TYPE_GENERICINST:
2187 type = &type->data.generic_class->container_class->byval_arg;
2190 case MONO_TYPE_MVAR:
2192 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2194 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2200 * target_type_is_incompatible:
2201 * @cfg: MonoCompile context
2203 * Check that the item @arg on the evaluation stack can be stored
2204 * in the target type (can be a local, or field, etc).
2205 * The cfg arg can be used to check if we need verification or just
2208 * Returns: non-0 value if arg can't be stored on a target.
2211 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2213 MonoType *simple_type;
2216 if (target->byref) {
2217 /* FIXME: check that the pointed to types match */
2218 if (arg->type == STACK_MP)
2219 return arg->klass != mono_class_from_mono_type (target);
2220 if (arg->type == STACK_PTR)
2225 simple_type = mini_get_underlying_type (cfg, target);
2226 switch (simple_type->type) {
2227 case MONO_TYPE_VOID:
2231 case MONO_TYPE_BOOLEAN:
2234 case MONO_TYPE_CHAR:
2237 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2241 /* STACK_MP is needed when setting pinned locals */
2242 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2247 case MONO_TYPE_FNPTR:
2249 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2250 * in native int. (#688008).
2252 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2255 case MONO_TYPE_CLASS:
2256 case MONO_TYPE_STRING:
2257 case MONO_TYPE_OBJECT:
2258 case MONO_TYPE_SZARRAY:
2259 case MONO_TYPE_ARRAY:
2260 if (arg->type != STACK_OBJ)
2262 /* FIXME: check type compatibility */
2266 if (arg->type != STACK_I8)
2270 if (arg->type != cfg->r4_stack_type)
2274 if (arg->type != STACK_R8)
2277 case MONO_TYPE_VALUETYPE:
2278 if (arg->type != STACK_VTYPE)
2280 klass = mono_class_from_mono_type (simple_type);
2281 if (klass != arg->klass)
2284 case MONO_TYPE_TYPEDBYREF:
2285 if (arg->type != STACK_VTYPE)
2287 klass = mono_class_from_mono_type (simple_type);
2288 if (klass != arg->klass)
2291 case MONO_TYPE_GENERICINST:
2292 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2293 if (arg->type != STACK_VTYPE)
2295 klass = mono_class_from_mono_type (simple_type);
2296 if (klass != arg->klass)
2300 if (arg->type != STACK_OBJ)
2302 /* FIXME: check type compatibility */
2306 case MONO_TYPE_MVAR:
2307 g_assert (cfg->generic_sharing_context);
2308 if (mini_type_var_is_vt (cfg, simple_type)) {
2309 if (arg->type != STACK_VTYPE)
2312 if (arg->type != STACK_OBJ)
2317 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2323 * Prepare arguments for passing to a function call.
2324 * Return a non-zero value if the arguments can't be passed to the given
2326 * The type checks are not yet complete and some conversions may need
2327 * casts on 32 or 64 bit architectures.
2329 * FIXME: implement this using target_type_is_incompatible ()
2332 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2334 MonoType *simple_type;
2338 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2342 for (i = 0; i < sig->param_count; ++i) {
2343 if (sig->params [i]->byref) {
2344 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2348 simple_type = mini_get_underlying_type (cfg, sig->params [i]);
2350 switch (simple_type->type) {
2351 case MONO_TYPE_VOID:
2356 case MONO_TYPE_BOOLEAN:
2359 case MONO_TYPE_CHAR:
2362 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2368 case MONO_TYPE_FNPTR:
2369 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2372 case MONO_TYPE_CLASS:
2373 case MONO_TYPE_STRING:
2374 case MONO_TYPE_OBJECT:
2375 case MONO_TYPE_SZARRAY:
2376 case MONO_TYPE_ARRAY:
2377 if (args [i]->type != STACK_OBJ)
2382 if (args [i]->type != STACK_I8)
2386 if (args [i]->type != cfg->r4_stack_type)
2390 if (args [i]->type != STACK_R8)
2393 case MONO_TYPE_VALUETYPE:
2394 if (simple_type->data.klass->enumtype) {
2395 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2398 if (args [i]->type != STACK_VTYPE)
2401 case MONO_TYPE_TYPEDBYREF:
2402 if (args [i]->type != STACK_VTYPE)
2405 case MONO_TYPE_GENERICINST:
2406 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2409 case MONO_TYPE_MVAR:
2411 if (args [i]->type != STACK_VTYPE)
2415 g_error ("unknown type 0x%02x in check_call_signature",
2423 callvirt_to_call (int opcode)
2426 case OP_CALL_MEMBASE:
2428 case OP_VOIDCALL_MEMBASE:
2430 case OP_FCALL_MEMBASE:
2432 case OP_RCALL_MEMBASE:
2434 case OP_VCALL_MEMBASE:
2436 case OP_LCALL_MEMBASE:
2439 g_assert_not_reached ();
2445 /* Either METHOD or IMT_ARG needs to be set */
2447 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2451 if (COMPILE_LLVM (cfg)) {
2452 method_reg = alloc_preg (cfg);
2455 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2456 } else if (cfg->compile_aot) {
2457 MONO_EMIT_NEW_AOTCONST (cfg, method_reg, method, MONO_PATCH_INFO_METHODCONST);
2460 MONO_INST_NEW (cfg, ins, OP_PCONST);
2461 ins->inst_p0 = method;
2462 ins->dreg = method_reg;
2463 MONO_ADD_INS (cfg->cbb, ins);
2467 call->imt_arg_reg = method_reg;
2469 #ifdef MONO_ARCH_IMT_REG
2470 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2472 /* Need this to keep the IMT arg alive */
2473 mono_call_inst_add_outarg_reg (cfg, call, method_reg, 0, FALSE);
2478 #ifdef MONO_ARCH_IMT_REG
2479 method_reg = alloc_preg (cfg);
2482 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2483 } else if (cfg->compile_aot) {
2484 MONO_EMIT_NEW_AOTCONST (cfg, method_reg, method, MONO_PATCH_INFO_METHODCONST);
2487 MONO_INST_NEW (cfg, ins, OP_PCONST);
2488 ins->inst_p0 = method;
2489 ins->dreg = method_reg;
2490 MONO_ADD_INS (cfg->cbb, ins);
2493 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2495 mono_arch_emit_imt_argument (cfg, call, imt_arg);
2499 static MonoJumpInfo *
2500 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2502 MonoJumpInfo *ji = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2506 ji->data.target = target;
2512 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2514 if (cfg->generic_sharing_context)
2515 return mono_class_check_context_used (klass);
2521 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2523 if (cfg->generic_sharing_context)
2524 return mono_method_check_context_used (method);
2530 * check_method_sharing:
2532 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2535 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2537 gboolean pass_vtable = FALSE;
2538 gboolean pass_mrgctx = FALSE;
2540 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2541 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
2542 gboolean sharable = FALSE;
2544 if (mono_method_is_generic_sharable (cmethod, TRUE)) {
2547 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
2548 MonoGenericContext *context = mini_class_get_context (cmethod->klass);
2549 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
2551 sharable = sharing_enabled && context_sharable;
2555 * Pass vtable iff target method might
2556 * be shared, which means that sharing
2557 * is enabled for its class and its
2558 * context is sharable (and it's not a
2561 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2565 if (mini_method_get_context (cmethod) &&
2566 mini_method_get_context (cmethod)->method_inst) {
2567 g_assert (!pass_vtable);
2569 if (mono_method_is_generic_sharable (cmethod, TRUE)) {
2572 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
2573 MonoGenericContext *context = mini_method_get_context (cmethod);
2574 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
2576 if (sharing_enabled && context_sharable)
2578 if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, mono_method_signature (cmethod)))
2583 if (out_pass_vtable)
2584 *out_pass_vtable = pass_vtable;
2585 if (out_pass_mrgctx)
2586 *out_pass_mrgctx = pass_mrgctx;
2589 inline static MonoCallInst *
2590 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2591 MonoInst **args, int calli, int virtual, int tail, int rgctx, int unbox_trampoline)
2595 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2600 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2602 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2604 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual, cfg->generic_sharing_context));
2607 call->signature = sig;
2608 call->rgctx_reg = rgctx;
2609 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2611 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2614 if (mini_type_is_vtype (cfg, sig_ret)) {
2615 call->vret_var = cfg->vret_addr;
2616 //g_assert_not_reached ();
2618 } else if (mini_type_is_vtype (cfg, sig_ret)) {
2619 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2622 temp->backend.is_pinvoke = sig->pinvoke;
2625 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2626 * address of return value to increase optimization opportunities.
2627 * Before vtype decomposition, the dreg of the call ins itself represents the
2628 * fact the call modifies the return value. After decomposition, the call will
2629 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2630 * will be transformed into an LDADDR.
2632 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2633 loada->dreg = alloc_preg (cfg);
2634 loada->inst_p0 = temp;
2635 /* We reference the call too since call->dreg could change during optimization */
2636 loada->inst_p1 = call;
2637 MONO_ADD_INS (cfg->cbb, loada);
2639 call->inst.dreg = temp->dreg;
2641 call->vret_var = loada;
2642 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2643 call->inst.dreg = alloc_dreg (cfg, call->inst.type);
2645 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2646 if (COMPILE_SOFT_FLOAT (cfg)) {
2648 * If the call has a float argument, we would need to do an r8->r4 conversion using
2649 * an icall, but that cannot be done during the call sequence since it would clobber
2650 * the call registers + the stack. So we do it before emitting the call.
2652 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2654 MonoInst *in = call->args [i];
2656 if (i >= sig->hasthis)
2657 t = sig->params [i - sig->hasthis];
2659 t = &mono_defaults.int_class->byval_arg;
2660 t = mono_type_get_underlying_type (t);
2662 if (!t->byref && t->type == MONO_TYPE_R4) {
2663 MonoInst *iargs [1];
2667 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2669 /* The result will be in an int vreg */
2670 call->args [i] = conv;
2676 call->need_unbox_trampoline = unbox_trampoline;
2679 if (COMPILE_LLVM (cfg))
2680 mono_llvm_emit_call (cfg, call);
2682 mono_arch_emit_call (cfg, call);
2684 mono_arch_emit_call (cfg, call);
2687 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2688 cfg->flags |= MONO_CFG_HAS_CALLS;
2694 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2696 #ifdef MONO_ARCH_RGCTX_REG
2697 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2698 cfg->uses_rgctx_reg = TRUE;
2699 call->rgctx_reg = TRUE;
2701 call->rgctx_arg_reg = rgctx_reg;
2708 inline static MonoInst*
2709 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2714 gboolean check_sp = FALSE;
2716 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2717 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2719 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2724 rgctx_reg = mono_alloc_preg (cfg);
2725 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2729 if (!cfg->stack_inbalance_var)
2730 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2732 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2733 ins->dreg = cfg->stack_inbalance_var->dreg;
2734 MONO_ADD_INS (cfg->cbb, ins);
2737 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2739 call->inst.sreg1 = addr->dreg;
2742 emit_imt_argument (cfg, call, NULL, imt_arg);
2744 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2749 sp_reg = mono_alloc_preg (cfg);
2751 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2753 MONO_ADD_INS (cfg->cbb, ins);
2755 /* Restore the stack so we don't crash when throwing the exception */
2756 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2757 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2758 MONO_ADD_INS (cfg->cbb, ins);
2760 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2761 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2765 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2767 return (MonoInst*)call;
2771 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2774 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2776 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2779 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2780 MonoInst **args, MonoInst *this, MonoInst *imt_arg, MonoInst *rgctx_arg)
2782 #ifndef DISABLE_REMOTING
2783 gboolean might_be_remote = FALSE;
2785 gboolean virtual = this != NULL;
2786 gboolean enable_for_aot = TRUE;
2790 gboolean need_unbox_trampoline;
2793 sig = mono_method_signature (method);
2796 rgctx_reg = mono_alloc_preg (cfg);
2797 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2800 if (method->string_ctor) {
2801 /* Create the real signature */
2802 /* FIXME: Cache these */
2803 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2804 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2809 context_used = mini_method_check_context_used (cfg, method);
2811 #ifndef DISABLE_REMOTING
2812 might_be_remote = this && sig->hasthis &&
2813 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2814 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this) || context_used);
2816 if (might_be_remote && context_used) {
2819 g_assert (cfg->generic_sharing_context);
2821 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2823 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2827 need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
2829 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2831 #ifndef DISABLE_REMOTING
2832 if (might_be_remote)
2833 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2836 call->method = method;
2837 call->inst.flags |= MONO_INST_HAS_METHOD;
2838 call->inst.inst_left = this;
2839 call->tail_call = tail;
2842 int vtable_reg, slot_reg, this_reg;
2845 this_reg = this->dreg;
2847 if (ARCH_HAVE_DELEGATE_TRAMPOLINES && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2848 MonoInst *dummy_use;
2850 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2852 /* Make a call to delegate->invoke_impl */
2853 call->inst.inst_basereg = this_reg;
2854 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2855 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2857 /* We must emit a dummy use here because the delegate trampoline will
2858 replace the 'this' argument with the delegate target making this activation
2859 no longer a root for the delegate.
2860 This is an issue for delegates that target collectible code such as dynamic
2861 methods of GC'able assemblies.
2863 For a test case look into #667921.
2865 FIXME: a dummy use is not the best way to do it as the local register allocator
2866 will put it on a caller save register and spil it around the call.
2867 Ideally, we would either put it on a callee save register or only do the store part.
2869 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2871 return (MonoInst*)call;
2874 if ((!cfg->compile_aot || enable_for_aot) &&
2875 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2876 (MONO_METHOD_IS_FINAL (method) &&
2877 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2878 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2880 * the method is not virtual, we just need to ensure this is not null
2881 * and then we can call the method directly.
2883 #ifndef DISABLE_REMOTING
2884 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2886 * The check above ensures method is not gshared, this is needed since
2887 * gshared methods can't have wrappers.
2889 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2893 if (!method->string_ctor)
2894 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2896 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2897 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2899 * the method is virtual, but we can statically dispatch since either
2900 * it's class or the method itself are sealed.
2901 * But first we need to ensure it's not a null reference.
2903 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2905 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2907 vtable_reg = alloc_preg (cfg);
2908 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2909 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2912 guint32 imt_slot = mono_method_get_imt_slot (method);
2913 emit_imt_argument (cfg, call, call->method, imt_arg);
2914 slot_reg = vtable_reg;
2915 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2917 if (slot_reg == -1) {
2918 slot_reg = alloc_preg (cfg);
2919 mini_emit_load_intf_reg_vtable (cfg, slot_reg, vtable_reg, method->klass);
2920 offset = mono_method_get_vtable_index (method) * SIZEOF_VOID_P;
2923 slot_reg = vtable_reg;
2924 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2925 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2927 g_assert (mono_method_signature (method)->generic_param_count);
2928 emit_imt_argument (cfg, call, call->method, imt_arg);
2932 call->inst.sreg1 = slot_reg;
2933 call->inst.inst_offset = offset;
2934 call->virtual = TRUE;
2938 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2941 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2943 return (MonoInst*)call;
2947 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this)
2949 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this, NULL, NULL);
2953 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2960 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2963 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2965 return (MonoInst*)call;
2969 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2971 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2975 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2979 * mono_emit_abs_call:
2981 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2983 inline static MonoInst*
2984 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2985 MonoMethodSignature *sig, MonoInst **args)
2987 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2991 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2994 if (cfg->abs_patches == NULL)
2995 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2996 g_hash_table_insert (cfg->abs_patches, ji, ji);
2997 ins = mono_emit_native_call (cfg, ji, sig, args);
2998 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
3003 mono_emit_jit_icall_by_info (MonoCompile *cfg, MonoJitICallInfo *info, MonoInst **args, MonoBasicBlock **out_cbb)
3005 gboolean no_wrapper = FALSE;
3008 * Call the jit icall without a wrapper if possible.
3009 * The wrapper is needed for the following reasons:
3010 * - to handle exceptions thrown using mono_raise_exceptions () from the
3011 * icall function. The EH code needs the lmf frame pushed by the
3012 * wrapper to be able to unwind back to managed code.
3013 * - to be able to do stack walks for asynchronously suspended
3014 * threads when debugging.
3016 if (info->no_raise) {
3017 if (cfg->compile_aot) {
3018 // FIXME: This might be loaded into a runtime during debugging
3019 // even if it is not compiled using 'soft-debug'.
3022 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3023 if ((cfg->compile_llvm && SIZEOF_VOID_P == 8) || cfg->gen_seq_points_debug_data)
3032 if (!info->wrapper_method) {
3033 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
3034 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
3036 mono_memory_barrier ();
3040 * Inline the wrapper method, which is basically a call to the C icall, and
3041 * an exception check.
3043 costs = inline_method (cfg, info->wrapper_method, NULL,
3044 args, NULL, cfg->real_offset, TRUE, out_cbb);
3045 g_assert (costs > 0);
3046 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
3050 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
3055 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
3057 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3058 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
3062 * Native code might return non register sized integers
3063 * without initializing the upper bits.
3065 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3066 case OP_LOADI1_MEMBASE:
3067 widen_op = OP_ICONV_TO_I1;
3069 case OP_LOADU1_MEMBASE:
3070 widen_op = OP_ICONV_TO_U1;
3072 case OP_LOADI2_MEMBASE:
3073 widen_op = OP_ICONV_TO_I2;
3075 case OP_LOADU2_MEMBASE:
3076 widen_op = OP_ICONV_TO_U2;
3082 if (widen_op != -1) {
3083 int dreg = alloc_preg (cfg);
3086 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3087 widen->type = ins->type;
3097 get_memcpy_method (void)
3099 static MonoMethod *memcpy_method = NULL;
3100 if (!memcpy_method) {
3101 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3103 g_error ("Old corlib found. Install a new one");
3105 return memcpy_method;
3109 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3111 MonoClassField *field;
3112 gpointer iter = NULL;
3114 while ((field = mono_class_get_fields (klass, &iter))) {
3117 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3119 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3120 if (mini_type_is_reference (cfg, mono_field_get_type (field))) {
3121 g_assert ((foffset % SIZEOF_VOID_P) == 0);
3122 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3124 MonoClass *field_class = mono_class_from_mono_type (field->type);
3125 if (field_class->has_references)
3126 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3132 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3134 int card_table_shift_bits;
3135 gpointer card_table_mask;
3137 MonoInst *dummy_use;
3138 int nursery_shift_bits;
3139 size_t nursery_size;
3140 gboolean has_card_table_wb = FALSE;
3142 if (!cfg->gen_write_barriers)
3145 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3147 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3149 #ifdef MONO_ARCH_HAVE_CARD_TABLE_WBARRIER
3150 has_card_table_wb = TRUE;
3153 if (has_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3156 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3157 wbarrier->sreg1 = ptr->dreg;
3158 wbarrier->sreg2 = value->dreg;
3159 MONO_ADD_INS (cfg->cbb, wbarrier);
3160 } else if (card_table) {
3161 int offset_reg = alloc_preg (cfg);
3162 int card_reg = alloc_preg (cfg);
3165 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3166 if (card_table_mask)
3167 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3169 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3170 * IMM's larger than 32bits.
3172 if (cfg->compile_aot) {
3173 MONO_EMIT_NEW_AOTCONST (cfg, card_reg, NULL, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR);
3175 MONO_INST_NEW (cfg, ins, OP_PCONST);
3176 ins->inst_p0 = card_table;
3177 ins->dreg = card_reg;
3178 MONO_ADD_INS (cfg->cbb, ins);
3181 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3182 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3184 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3185 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3188 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3192 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3194 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3195 unsigned need_wb = 0;
3200 /*types with references can't have alignment smaller than sizeof(void*) */
3201 if (align < SIZEOF_VOID_P)
3204 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3205 if (size > 32 * SIZEOF_VOID_P)
3208 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3210 /* We don't unroll more than 5 stores to avoid code bloat. */
3211 if (size > 5 * SIZEOF_VOID_P) {
3212 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3213 size += (SIZEOF_VOID_P - 1);
3214 size &= ~(SIZEOF_VOID_P - 1);
3216 EMIT_NEW_ICONST (cfg, iargs [2], size);
3217 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3218 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3222 destreg = iargs [0]->dreg;
3223 srcreg = iargs [1]->dreg;
3226 dest_ptr_reg = alloc_preg (cfg);
3227 tmp_reg = alloc_preg (cfg);
3230 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3232 while (size >= SIZEOF_VOID_P) {
3233 MonoInst *load_inst;
3234 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3235 load_inst->dreg = tmp_reg;
3236 load_inst->inst_basereg = srcreg;
3237 load_inst->inst_offset = offset;
3238 MONO_ADD_INS (cfg->cbb, load_inst);
3240 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3243 emit_write_barrier (cfg, iargs [0], load_inst);
3245 offset += SIZEOF_VOID_P;
3246 size -= SIZEOF_VOID_P;
3249 /*tmp += sizeof (void*)*/
3250 if (size >= SIZEOF_VOID_P) {
3251 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3252 MONO_ADD_INS (cfg->cbb, iargs [0]);
3256 /* Those cannot be references since size < sizeof (void*) */
3258 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3259 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3265 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3266 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3272 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3273 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3282 * Emit code to copy a valuetype of type @klass whose address is stored in
3283 * @src->dreg to memory whose address is stored at @dest->dreg.
3286 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3288 MonoInst *iargs [4];
3291 MonoMethod *memcpy_method;
3292 MonoInst *size_ins = NULL;
3293 MonoInst *memcpy_ins = NULL;
3296 if (cfg->generic_sharing_context)
3297 klass = mono_class_from_mono_type (mini_get_underlying_type (cfg, &klass->byval_arg));
3300 * This check breaks with spilled vars... need to handle it during verification anyway.
3301 * g_assert (klass && klass == src->klass && klass == dest->klass);
3304 if (mini_is_gsharedvt_klass (cfg, klass)) {
3306 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3307 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3311 n = mono_class_native_size (klass, &align);
3313 n = mono_class_value_size (klass, &align);
3315 /* if native is true there should be no references in the struct */
3316 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3317 /* Avoid barriers when storing to the stack */
3318 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3319 (dest->opcode == OP_LDADDR))) {
3325 context_used = mini_class_check_context_used (cfg, klass);
3327 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3328 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3330 } else if (context_used) {
3331 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3333 if (cfg->compile_aot) {
3334 EMIT_NEW_CLASSCONST (cfg, iargs [2], klass);
3336 EMIT_NEW_PCONST (cfg, iargs [2], klass);
3337 mono_class_compute_gc_descriptor (klass);
3342 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3344 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3349 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3350 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3351 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3356 iargs [2] = size_ins;
3358 EMIT_NEW_ICONST (cfg, iargs [2], n);
3360 memcpy_method = get_memcpy_method ();
3362 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3364 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3369 get_memset_method (void)
3371 static MonoMethod *memset_method = NULL;
3372 if (!memset_method) {
3373 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3375 g_error ("Old corlib found. Install a new one");
3377 return memset_method;
3381 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3383 MonoInst *iargs [3];
3386 MonoMethod *memset_method;
3387 MonoInst *size_ins = NULL;
3388 MonoInst *bzero_ins = NULL;
3389 static MonoMethod *bzero_method;
3391 /* FIXME: Optimize this for the case when dest is an LDADDR */
3392 mono_class_init (klass);
3393 if (mini_is_gsharedvt_klass (cfg, klass)) {
3394 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3395 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3397 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3398 g_assert (bzero_method);
3400 iargs [1] = size_ins;
3401 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3405 n = mono_class_value_size (klass, &align);
3407 if (n <= sizeof (gpointer) * 8) {
3408 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3411 memset_method = get_memset_method ();
3413 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3414 EMIT_NEW_ICONST (cfg, iargs [2], n);
3415 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3420 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3422 MonoInst *this = NULL;
3424 g_assert (cfg->generic_sharing_context);
3426 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3427 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3428 !method->klass->valuetype)
3429 EMIT_NEW_ARGLOAD (cfg, this, 0);
3431 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3432 MonoInst *mrgctx_loc, *mrgctx_var;
3435 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3437 mrgctx_loc = mono_get_vtable_var (cfg);
3438 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3441 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3442 MonoInst *vtable_loc, *vtable_var;
3446 vtable_loc = mono_get_vtable_var (cfg);
3447 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3449 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3450 MonoInst *mrgctx_var = vtable_var;
3453 vtable_reg = alloc_preg (cfg);
3454 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3455 vtable_var->type = STACK_PTR;
3463 vtable_reg = alloc_preg (cfg);
3464 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3469 static MonoJumpInfoRgctxEntry *
3470 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3472 MonoJumpInfoRgctxEntry *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3473 res->method = method;
3474 res->in_mrgctx = in_mrgctx;
3475 res->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3476 res->data->type = patch_type;
3477 res->data->data.target = patch_data;
3478 res->info_type = info_type;
3483 static inline MonoInst*
3484 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3486 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3490 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3491 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3493 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3494 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3496 return emit_rgctx_fetch (cfg, rgctx, entry);
3500 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3501 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3503 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3504 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3506 return emit_rgctx_fetch (cfg, rgctx, entry);
3510 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3511 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3513 MonoJumpInfoGSharedVtCall *call_info;
3514 MonoJumpInfoRgctxEntry *entry;
3517 call_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3518 call_info->sig = sig;
3519 call_info->method = cmethod;
3521 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3522 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3524 return emit_rgctx_fetch (cfg, rgctx, entry);
3529 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3530 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3532 MonoJumpInfoRgctxEntry *entry;
3535 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3536 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3538 return emit_rgctx_fetch (cfg, rgctx, entry);
3542 * emit_get_rgctx_method:
3544 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3545 * normal constants, else emit a load from the rgctx.
3548 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3549 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3551 if (!context_used) {
3554 switch (rgctx_type) {
3555 case MONO_RGCTX_INFO_METHOD:
3556 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3558 case MONO_RGCTX_INFO_METHOD_RGCTX:
3559 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3562 g_assert_not_reached ();
3565 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3566 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3568 return emit_rgctx_fetch (cfg, rgctx, entry);
3573 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3574 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3576 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3577 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3579 return emit_rgctx_fetch (cfg, rgctx, entry);
3583 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3585 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3586 MonoRuntimeGenericContextInfoTemplate *template;
3591 for (i = 0; i < info->num_entries; ++i) {
3592 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3594 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3598 if (info->num_entries == info->count_entries) {
3599 MonoRuntimeGenericContextInfoTemplate *new_entries;
3600 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3602 new_entries = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3604 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3605 info->entries = new_entries;
3606 info->count_entries = new_count_entries;
3609 idx = info->num_entries;
3610 template = &info->entries [idx];
3611 template->info_type = rgctx_type;
3612 template->data = data;
3614 info->num_entries ++;
3620 * emit_get_gsharedvt_info:
3622 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3625 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3630 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3631 /* Load info->entries [idx] */
3632 dreg = alloc_preg (cfg);
3633 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3639 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3641 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3645 * On return the caller must check @klass for load errors.
3648 emit_generic_class_init (MonoCompile *cfg, MonoClass *klass)
3650 MonoInst *vtable_arg;
3654 context_used = mini_class_check_context_used (cfg, klass);
3657 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3658 klass, MONO_RGCTX_INFO_VTABLE);
3660 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3664 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3667 if (COMPILE_LLVM (cfg))
3668 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline_llvm, &vtable_arg);
3670 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline, &vtable_arg);
3671 #ifdef MONO_ARCH_VTABLE_REG
3672 mono_call_inst_add_outarg_reg (cfg, call, vtable_arg->dreg, MONO_ARCH_VTABLE_REG, FALSE);
3673 cfg->uses_vtable_reg = TRUE;
3680 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3684 if (cfg->gen_seq_points && cfg->method == method) {
3685 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3687 ins->flags |= MONO_INST_NONEMPTY_STACK;
3688 MONO_ADD_INS (cfg->cbb, ins);
3693 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check, MonoBasicBlock **out_bblock)
3695 if (mini_get_debug_options ()->better_cast_details) {
3696 int vtable_reg = alloc_preg (cfg);
3697 int klass_reg = alloc_preg (cfg);
3698 MonoBasicBlock *is_null_bb = NULL;
3700 int to_klass_reg, context_used;
3703 NEW_BBLOCK (cfg, is_null_bb);
3705 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3706 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3709 tls_get = mono_get_jit_tls_intrinsic (cfg);
3711 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3715 MONO_ADD_INS (cfg->cbb, tls_get);
3716 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3717 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3719 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3721 context_used = mini_class_check_context_used (cfg, klass);
3723 MonoInst *class_ins;
3725 class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3726 to_klass_reg = class_ins->dreg;
3728 to_klass_reg = alloc_preg (cfg);
3729 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3731 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3734 MONO_START_BB (cfg, is_null_bb);
3736 *out_bblock = cfg->cbb;
3742 reset_cast_details (MonoCompile *cfg)
3744 /* Reset the variables holding the cast details */
3745 if (mini_get_debug_options ()->better_cast_details) {
3746 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3748 MONO_ADD_INS (cfg->cbb, tls_get);
3749 /* It is enough to reset the from field */
3750 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3755 * On return the caller must check @array_class for load errors
3758 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3760 int vtable_reg = alloc_preg (cfg);
3763 context_used = mini_class_check_context_used (cfg, array_class);
3765 save_cast_details (cfg, array_class, obj->dreg, FALSE, NULL);
3767 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3769 if (cfg->opt & MONO_OPT_SHARED) {
3770 int class_reg = alloc_preg (cfg);
3771 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3772 if (cfg->compile_aot) {
3773 int klass_reg = alloc_preg (cfg);
3774 MONO_EMIT_NEW_CLASSCONST (cfg, klass_reg, array_class);
3775 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, klass_reg);
3777 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, class_reg, array_class);
3779 } else if (context_used) {
3780 MonoInst *vtable_ins;
3782 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3783 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3785 if (cfg->compile_aot) {
3789 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3791 vt_reg = alloc_preg (cfg);
3792 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3793 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3796 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3798 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3802 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3804 reset_cast_details (cfg);
3808 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
3809 * generic code is generated.
3812 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3814 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3817 MonoInst *rgctx, *addr;
3819 /* FIXME: What if the class is shared? We might not
3820 have to get the address of the method from the
3822 addr = emit_get_rgctx_method (cfg, context_used, method,
3823 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3825 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3827 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3829 gboolean pass_vtable, pass_mrgctx;
3830 MonoInst *rgctx_arg = NULL;
3832 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3833 g_assert (!pass_mrgctx);
3836 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3839 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3842 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3847 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3851 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3852 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3853 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3854 int rank_reg = alloc_dreg (cfg ,STACK_I4);
3856 obj_reg = sp [0]->dreg;
3857 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3858 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3860 /* FIXME: generics */
3861 g_assert (klass->rank == 0);
3864 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3865 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3867 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3868 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3871 MonoInst *element_class;
3873 /* This assertion is from the unboxcast insn */
3874 g_assert (klass->rank == 0);
3876 element_class = emit_get_rgctx_klass (cfg, context_used,
3877 klass->element_class, MONO_RGCTX_INFO_KLASS);
3879 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3880 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3882 save_cast_details (cfg, klass->element_class, obj_reg, FALSE, NULL);
3883 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3884 reset_cast_details (cfg);
3887 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3888 MONO_ADD_INS (cfg->cbb, add);
3889 add->type = STACK_MP;
3896 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj, MonoBasicBlock **out_cbb)
3898 MonoInst *addr, *klass_inst, *is_ref, *args[16];
3899 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3903 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3909 args [1] = klass_inst;
3912 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3914 NEW_BBLOCK (cfg, is_ref_bb);
3915 NEW_BBLOCK (cfg, is_nullable_bb);
3916 NEW_BBLOCK (cfg, end_bb);
3917 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3918 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 1);
3919 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3921 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 2);
3922 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3924 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3925 addr_reg = alloc_dreg (cfg, STACK_MP);
3929 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3930 MONO_ADD_INS (cfg->cbb, addr);
3932 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3935 MONO_START_BB (cfg, is_ref_bb);
3937 /* Save the ref to a temporary */
3938 dreg = alloc_ireg (cfg);
3939 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3940 addr->dreg = addr_reg;
3941 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3942 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3945 MONO_START_BB (cfg, is_nullable_bb);
3948 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3949 MonoInst *unbox_call;
3950 MonoMethodSignature *unbox_sig;
3952 unbox_sig = mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3953 unbox_sig->ret = &klass->byval_arg;
3954 unbox_sig->param_count = 1;
3955 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3956 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3958 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3959 addr->dreg = addr_reg;
3962 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3965 MONO_START_BB (cfg, end_bb);
3968 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3970 *out_cbb = cfg->cbb;
3976 * Returns NULL and set the cfg exception on error.
3979 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3981 MonoInst *iargs [2];
3987 MonoInst *iargs [2];
3988 gboolean known_instance_size = !mini_is_gsharedvt_klass (cfg, klass);
3990 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
3992 if (cfg->opt & MONO_OPT_SHARED)
3993 rgctx_info = MONO_RGCTX_INFO_KLASS;
3995 rgctx_info = MONO_RGCTX_INFO_VTABLE;
3996 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
3998 if (cfg->opt & MONO_OPT_SHARED) {
3999 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4001 alloc_ftn = mono_object_new;
4004 alloc_ftn = mono_object_new_specific;
4007 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4008 if (known_instance_size)
4009 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (klass->instance_size));
4010 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4013 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4016 if (cfg->opt & MONO_OPT_SHARED) {
4017 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4018 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4020 alloc_ftn = mono_object_new;
4021 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !klass->generic_class) {
4022 /* This happens often in argument checking code, eg. throw new FooException... */
4023 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4024 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4025 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4027 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4028 MonoMethod *managed_alloc = NULL;
4032 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4033 cfg->exception_ptr = klass;
4037 #ifndef MONO_CROSS_COMPILE
4038 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4041 if (managed_alloc) {
4042 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4043 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (klass->instance_size));
4044 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4046 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4048 guint32 lw = vtable->klass->instance_size;
4049 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4050 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4051 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4054 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4058 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4062 * Returns NULL and set the cfg exception on error.
4065 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used, MonoBasicBlock **out_cbb)
4067 MonoInst *alloc, *ins;
4069 *out_cbb = cfg->cbb;
4071 if (mono_class_is_nullable (klass)) {
4072 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4075 /* FIXME: What if the class is shared? We might not
4076 have to get the method address from the RGCTX. */
4077 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4078 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4079 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4081 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4083 gboolean pass_vtable, pass_mrgctx;
4084 MonoInst *rgctx_arg = NULL;
4086 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4087 g_assert (!pass_mrgctx);
4090 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4093 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4096 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4100 if (mini_is_gsharedvt_klass (cfg, klass)) {
4101 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4102 MonoInst *res, *is_ref, *src_var, *addr;
4105 dreg = alloc_ireg (cfg);
4107 NEW_BBLOCK (cfg, is_ref_bb);
4108 NEW_BBLOCK (cfg, is_nullable_bb);
4109 NEW_BBLOCK (cfg, end_bb);
4110 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4111 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 1);
4112 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4114 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 2);
4115 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4118 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4121 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4122 ins->opcode = OP_STOREV_MEMBASE;
4124 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4125 res->type = STACK_OBJ;
4127 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4130 MONO_START_BB (cfg, is_ref_bb);
4132 /* val is a vtype, so has to load the value manually */
4133 src_var = get_vreg_to_inst (cfg, val->dreg);
4135 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4136 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4137 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4138 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4141 MONO_START_BB (cfg, is_nullable_bb);
4144 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4145 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4147 MonoMethodSignature *box_sig;
4150 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4151 * construct that method at JIT time, so have to do things by hand.
4153 box_sig = mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4154 box_sig->ret = &mono_defaults.object_class->byval_arg;
4155 box_sig->param_count = 1;
4156 box_sig->params [0] = &klass->byval_arg;
4157 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4158 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4159 res->type = STACK_OBJ;
4163 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4165 MONO_START_BB (cfg, end_bb);
4167 *out_cbb = cfg->cbb;
4171 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4175 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4182 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4185 MonoGenericContainer *container;
4186 MonoGenericInst *ginst;
4188 if (klass->generic_class) {
4189 container = klass->generic_class->container_class->generic_container;
4190 ginst = klass->generic_class->context.class_inst;
4191 } else if (klass->generic_container && context_used) {
4192 container = klass->generic_container;
4193 ginst = container->context.class_inst;
4198 for (i = 0; i < container->type_argc; ++i) {
4200 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4202 type = ginst->type_argv [i];
4203 if (mini_type_is_reference (cfg, type))
4209 #define is_complex_isinst(klass) ((klass->flags & TYPE_ATTRIBUTE_INTERFACE) || klass->rank || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || (klass->flags & TYPE_ATTRIBUTE_SEALED) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
4212 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args, MonoBasicBlock **out_bblock)
4214 MonoMethod *mono_castclass;
4217 mono_castclass = mono_marshal_get_castclass_with_cache ();
4219 save_cast_details (cfg, klass, args [0]->dreg, TRUE, out_bblock);
4220 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4221 reset_cast_details (cfg);
4222 *out_bblock = cfg->cbb;
4228 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass, MonoBasicBlock **out_bblock)
4237 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4240 if (cfg->compile_aot) {
4241 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4242 cfg->castclass_cache_index ++;
4243 idx = (cfg->method_index << 16) | cfg->castclass_cache_index;
4244 EMIT_NEW_AOTCONST (cfg, args [2], MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4246 EMIT_NEW_PCONST (cfg, args [2], mono_domain_alloc0 (cfg->domain, sizeof (gpointer)));
4249 /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4251 return emit_castclass_with_cache (cfg, klass, args, out_bblock);
4255 * Returns NULL and set the cfg exception on error.
4258 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, guint8 *ip, MonoBasicBlock **out_bb, int *inline_costs)
4260 MonoBasicBlock *is_null_bb;
4261 int obj_reg = src->dreg;
4262 int vtable_reg = alloc_preg (cfg);
4264 MonoInst *klass_inst = NULL, *res;
4265 MonoBasicBlock *bblock;
4269 context_used = mini_class_check_context_used (cfg, klass);
4271 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
4272 res = emit_castclass_with_cache_nonshared (cfg, src, klass, &bblock);
4273 (*inline_costs) += 2;
4276 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
4277 MonoMethod *mono_castclass;
4278 MonoInst *iargs [1];
4281 mono_castclass = mono_marshal_get_castclass (klass);
4284 save_cast_details (cfg, klass, src->dreg, TRUE, &bblock);
4285 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
4286 iargs, ip, cfg->real_offset, TRUE, &bblock);
4287 reset_cast_details (cfg);
4288 CHECK_CFG_EXCEPTION;
4289 g_assert (costs > 0);
4291 cfg->real_offset += 5;
4293 (*inline_costs) += costs;
4302 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4303 MonoInst *cache_ins;
4305 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4310 /* klass - it's the second element of the cache entry*/
4311 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4314 args [2] = cache_ins;
4316 return emit_castclass_with_cache (cfg, klass, args, out_bb);
4319 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4322 NEW_BBLOCK (cfg, is_null_bb);
4324 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4325 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4327 save_cast_details (cfg, klass, obj_reg, FALSE, NULL);
4329 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4330 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4331 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4333 int klass_reg = alloc_preg (cfg);
4335 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4337 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4338 /* the remoting code is broken, access the class for now */
4339 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4340 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4342 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4343 cfg->exception_ptr = klass;
4346 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4348 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4349 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4351 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4353 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4354 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4358 MONO_START_BB (cfg, is_null_bb);
4360 reset_cast_details (cfg);
4371 * Returns NULL and set the cfg exception on error.
4374 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4377 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4378 int obj_reg = src->dreg;
4379 int vtable_reg = alloc_preg (cfg);
4380 int res_reg = alloc_ireg_ref (cfg);
4381 MonoInst *klass_inst = NULL;
4386 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4387 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4388 MonoInst *cache_ins;
4390 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4395 /* klass - it's the second element of the cache entry*/
4396 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4399 args [2] = cache_ins;
4401 return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4404 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4407 NEW_BBLOCK (cfg, is_null_bb);
4408 NEW_BBLOCK (cfg, false_bb);
4409 NEW_BBLOCK (cfg, end_bb);
4411 /* Do the assignment at the beginning, so the other assignment can be if converted */
4412 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4413 ins->type = STACK_OBJ;
4416 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4417 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4419 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4421 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4422 g_assert (!context_used);
4423 /* the is_null_bb target simply copies the input register to the output */
4424 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4426 int klass_reg = alloc_preg (cfg);
4429 int rank_reg = alloc_preg (cfg);
4430 int eclass_reg = alloc_preg (cfg);
4432 g_assert (!context_used);
4433 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4434 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4435 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4436 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4437 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4438 if (klass->cast_class == mono_defaults.object_class) {
4439 int parent_reg = alloc_preg (cfg);
4440 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4441 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4442 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4443 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4444 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4445 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4446 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4447 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4448 } else if (klass->cast_class == mono_defaults.enum_class) {
4449 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4450 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4451 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4452 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4454 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4455 /* Check that the object is a vector too */
4456 int bounds_reg = alloc_preg (cfg);
4457 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4458 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4459 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4462 /* the is_null_bb target simply copies the input register to the output */
4463 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4465 } else if (mono_class_is_nullable (klass)) {
4466 g_assert (!context_used);
4467 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4468 /* the is_null_bb target simply copies the input register to the output */
4469 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4471 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4472 g_assert (!context_used);
4473 /* the remoting code is broken, access the class for now */
4474 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4475 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4477 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4478 cfg->exception_ptr = klass;
4481 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4483 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4484 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4486 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4487 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4489 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4490 /* the is_null_bb target simply copies the input register to the output */
4491 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4496 MONO_START_BB (cfg, false_bb);
4498 MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4499 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4501 MONO_START_BB (cfg, is_null_bb);
4503 MONO_START_BB (cfg, end_bb);
4509 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4511 /* This opcode takes as input an object reference and a class, and returns:
4512 0) if the object is an instance of the class,
4513 1) if the object is not instance of the class,
4514 2) if the object is a proxy whose type cannot be determined */
4517 #ifndef DISABLE_REMOTING
4518 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4520 MonoBasicBlock *true_bb, *false_bb, *end_bb;
4522 int obj_reg = src->dreg;
4523 int dreg = alloc_ireg (cfg);
4525 #ifndef DISABLE_REMOTING
4526 int klass_reg = alloc_preg (cfg);
4529 NEW_BBLOCK (cfg, true_bb);
4530 NEW_BBLOCK (cfg, false_bb);
4531 NEW_BBLOCK (cfg, end_bb);
4532 #ifndef DISABLE_REMOTING
4533 NEW_BBLOCK (cfg, false2_bb);
4534 NEW_BBLOCK (cfg, no_proxy_bb);
4537 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4538 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4540 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4541 #ifndef DISABLE_REMOTING
4542 NEW_BBLOCK (cfg, interface_fail_bb);
4545 tmp_reg = alloc_preg (cfg);
4546 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4547 #ifndef DISABLE_REMOTING
4548 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4549 MONO_START_BB (cfg, interface_fail_bb);
4550 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4552 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4554 tmp_reg = alloc_preg (cfg);
4555 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4556 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4557 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
4559 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4562 #ifndef DISABLE_REMOTING
4563 tmp_reg = alloc_preg (cfg);
4564 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4565 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4567 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4568 tmp_reg = alloc_preg (cfg);
4569 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4570 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4572 tmp_reg = alloc_preg (cfg);
4573 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4574 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4575 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4577 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4578 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4580 MONO_START_BB (cfg, no_proxy_bb);
4582 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4584 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4588 MONO_START_BB (cfg, false_bb);
4590 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4591 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4593 #ifndef DISABLE_REMOTING
4594 MONO_START_BB (cfg, false2_bb);
4596 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4597 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4600 MONO_START_BB (cfg, true_bb);
4602 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4604 MONO_START_BB (cfg, end_bb);
4607 MONO_INST_NEW (cfg, ins, OP_ICONST);
4609 ins->type = STACK_I4;
4615 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4617 /* This opcode takes as input an object reference and a class, and returns:
4618 0) if the object is an instance of the class,
4619 1) if the object is a proxy whose type cannot be determined
4620 an InvalidCastException exception is thrown otherwhise*/
4623 #ifndef DISABLE_REMOTING
4624 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4626 MonoBasicBlock *ok_result_bb;
4628 int obj_reg = src->dreg;
4629 int dreg = alloc_ireg (cfg);
4630 int tmp_reg = alloc_preg (cfg);
4632 #ifndef DISABLE_REMOTING
4633 int klass_reg = alloc_preg (cfg);
4634 NEW_BBLOCK (cfg, end_bb);
4637 NEW_BBLOCK (cfg, ok_result_bb);
4639 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4640 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4642 save_cast_details (cfg, klass, obj_reg, FALSE, NULL);
4644 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4645 #ifndef DISABLE_REMOTING
4646 NEW_BBLOCK (cfg, interface_fail_bb);
4648 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4649 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4650 MONO_START_BB (cfg, interface_fail_bb);
4651 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4653 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4655 tmp_reg = alloc_preg (cfg);
4656 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4657 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4658 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4660 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4661 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4663 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4664 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
4665 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4668 #ifndef DISABLE_REMOTING
4669 NEW_BBLOCK (cfg, no_proxy_bb);
4671 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4672 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4673 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4675 tmp_reg = alloc_preg (cfg);
4676 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4677 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4679 tmp_reg = alloc_preg (cfg);
4680 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4681 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4682 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4684 NEW_BBLOCK (cfg, fail_1_bb);
4686 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4688 MONO_START_BB (cfg, fail_1_bb);
4690 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4691 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4693 MONO_START_BB (cfg, no_proxy_bb);
4695 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4697 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4701 MONO_START_BB (cfg, ok_result_bb);
4703 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4705 #ifndef DISABLE_REMOTING
4706 MONO_START_BB (cfg, end_bb);
4710 MONO_INST_NEW (cfg, ins, OP_ICONST);
4712 ins->type = STACK_I4;
4717 static G_GNUC_UNUSED MonoInst*
4718 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4720 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4721 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4724 switch (enum_type->type) {
4727 #if SIZEOF_REGISTER == 8
4739 MonoInst *load, *and, *cmp, *ceq;
4740 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4741 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4742 int dest_reg = alloc_ireg (cfg);
4744 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4745 EMIT_NEW_BIALU (cfg, and, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4746 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4747 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4749 ceq->type = STACK_I4;
4752 load = mono_decompose_opcode (cfg, load, NULL);
4753 and = mono_decompose_opcode (cfg, and, NULL);
4754 cmp = mono_decompose_opcode (cfg, cmp, NULL);
4755 ceq = mono_decompose_opcode (cfg, ceq, NULL);
4763 * Returns NULL and set the cfg exception on error.
4765 static G_GNUC_UNUSED MonoInst*
4766 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual)
4770 gpointer trampoline;
4771 MonoInst *obj, *method_ins, *tramp_ins;
4775 // FIXME reenable optimisation for virtual case
4780 MonoMethod *invoke = mono_get_delegate_invoke (klass);
4783 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
4787 obj = handle_alloc (cfg, klass, FALSE, 0);
4791 /* Inline the contents of mono_delegate_ctor */
4793 /* Set target field */
4794 /* Optimize away setting of NULL target */
4795 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
4796 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4797 if (cfg->gen_write_barriers) {
4798 dreg = alloc_preg (cfg);
4799 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4800 emit_write_barrier (cfg, ptr, target);
4804 /* Set method field */
4805 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4806 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4809 * To avoid looking up the compiled code belonging to the target method
4810 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4811 * store it, and we fill it after the method has been compiled.
4813 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4814 MonoInst *code_slot_ins;
4817 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4819 domain = mono_domain_get ();
4820 mono_domain_lock (domain);
4821 if (!domain_jit_info (domain)->method_code_hash)
4822 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4823 code_slot = g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4825 code_slot = mono_domain_alloc0 (domain, sizeof (gpointer));
4826 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4828 mono_domain_unlock (domain);
4830 if (cfg->compile_aot)
4831 EMIT_NEW_AOTCONST (cfg, code_slot_ins, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4833 EMIT_NEW_PCONST (cfg, code_slot_ins, code_slot);
4835 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
4838 if (cfg->compile_aot) {
4839 MonoDelegateClassMethodPair *del_tramp;
4841 del_tramp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4842 del_tramp->klass = klass;
4843 del_tramp->method = context_used ? NULL : method;
4844 del_tramp->virtual = virtual;
4845 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4848 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4850 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4851 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4854 /* Set invoke_impl field */
4856 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4858 dreg = alloc_preg (cfg);
4859 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4860 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4862 dreg = alloc_preg (cfg);
4863 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4864 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4867 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4873 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4875 MonoJitICallInfo *info;
4877 /* Need to register the icall so it gets an icall wrapper */
4878 info = mono_get_array_new_va_icall (rank);
4880 cfg->flags |= MONO_CFG_HAS_VARARGS;
4882 /* mono_array_new_va () needs a vararg calling convention */
4883 cfg->disable_llvm = TRUE;
4885 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4886 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4890 * handle_constrained_gsharedvt_call:
4892 * Handle constrained calls where the receiver is a gsharedvt type.
4893 * Return the instruction representing the call. Set the cfg exception on failure.
4896 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4897 gboolean *ref_emit_widen, MonoBasicBlock **ref_bblock)
4899 MonoInst *ins = NULL;
4900 MonoBasicBlock *bblock = *ref_bblock;
4901 gboolean emit_widen = *ref_emit_widen;
4904 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4905 * 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
4906 * pack the arguments into an array, and do the rest of the work in in an icall.
4908 if (((cmethod->klass == mono_defaults.object_class) || (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4909 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mini_is_gsharedvt_type (cfg, fsig->ret)) &&
4910 (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 (cfg, fsig->params [0]))))) {
4911 MonoInst *args [16];
4914 * This case handles calls to
4915 * - object:ToString()/Equals()/GetHashCode(),
4916 * - System.IComparable<T>:CompareTo()
4917 * - System.IEquatable<T>:Equals ()
4918 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4922 if (mono_method_check_context_used (cmethod))
4923 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4925 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4926 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4928 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4929 if (fsig->hasthis && fsig->param_count) {
4930 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4931 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4932 ins->dreg = alloc_preg (cfg);
4933 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4934 MONO_ADD_INS (cfg->cbb, ins);
4937 if (mini_is_gsharedvt_type (cfg, fsig->params [0])) {
4940 args [3] = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4942 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4943 addr_reg = ins->dreg;
4944 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4946 EMIT_NEW_ICONST (cfg, args [3], 0);
4947 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4950 EMIT_NEW_ICONST (cfg, args [3], 0);
4951 EMIT_NEW_ICONST (cfg, args [4], 0);
4953 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4956 if (mini_is_gsharedvt_type (cfg, fsig->ret)) {
4957 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins, &bblock);
4958 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
4962 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4963 MONO_ADD_INS (cfg->cbb, add);
4965 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4966 MONO_ADD_INS (cfg->cbb, ins);
4967 /* ins represents the call result */
4970 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4973 *ref_emit_widen = emit_widen;
4974 *ref_bblock = bblock;
4983 mono_emit_load_got_addr (MonoCompile *cfg)
4985 MonoInst *getaddr, *dummy_use;
4987 if (!cfg->got_var || cfg->got_var_allocated)
4990 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4991 getaddr->cil_code = cfg->header->code;
4992 getaddr->dreg = cfg->got_var->dreg;
4994 /* Add it to the start of the first bblock */
4995 if (cfg->bb_entry->code) {
4996 getaddr->next = cfg->bb_entry->code;
4997 cfg->bb_entry->code = getaddr;
5000 MONO_ADD_INS (cfg->bb_entry, getaddr);
5002 cfg->got_var_allocated = TRUE;
5005 * Add a dummy use to keep the got_var alive, since real uses might
5006 * only be generated by the back ends.
5007 * Add it to end_bblock, so the variable's lifetime covers the whole
5009 * It would be better to make the usage of the got var explicit in all
5010 * cases when the backend needs it (i.e. calls, throw etc.), so this
5011 * wouldn't be needed.
5013 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5014 MONO_ADD_INS (cfg->bb_exit, dummy_use);
5017 static int inline_limit;
5018 static gboolean inline_limit_inited;
5021 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5023 MonoMethodHeaderSummary header;
5025 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5026 MonoMethodSignature *sig = mono_method_signature (method);
5030 if (cfg->disable_inline)
5032 if (cfg->generic_sharing_context)
5035 if (cfg->inline_depth > 10)
5038 #ifdef MONO_ARCH_HAVE_LMF_OPS
5039 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5040 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
5041 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
5046 if (!mono_method_get_header_summary (method, &header))
5049 /*runtime, icall and pinvoke are checked by summary call*/
5050 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5051 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5052 (mono_class_is_marshalbyref (method->klass)) ||
5056 /* also consider num_locals? */
5057 /* Do the size check early to avoid creating vtables */
5058 if (!inline_limit_inited) {
5059 if (g_getenv ("MONO_INLINELIMIT"))
5060 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5062 inline_limit = INLINE_LENGTH_LIMIT;
5063 inline_limit_inited = TRUE;
5065 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5069 * if we can initialize the class of the method right away, we do,
5070 * otherwise we don't allow inlining if the class needs initialization,
5071 * since it would mean inserting a call to mono_runtime_class_init()
5072 * inside the inlined code
5074 if (!(cfg->opt & MONO_OPT_SHARED)) {
5075 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5076 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5077 vtable = mono_class_vtable (cfg->domain, method->klass);
5080 if (!cfg->compile_aot)
5081 mono_runtime_class_init (vtable);
5082 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5083 if (cfg->run_cctors && method->klass->has_cctor) {
5084 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5085 if (!method->klass->runtime_info)
5086 /* No vtable created yet */
5088 vtable = mono_class_vtable (cfg->domain, method->klass);
5091 /* This makes so that inline cannot trigger */
5092 /* .cctors: too many apps depend on them */
5093 /* running with a specific order... */
5094 if (! vtable->initialized)
5096 mono_runtime_class_init (vtable);
5098 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5099 if (!method->klass->runtime_info)
5100 /* No vtable created yet */
5102 vtable = mono_class_vtable (cfg->domain, method->klass);
5105 if (!vtable->initialized)
5110 * If we're compiling for shared code
5111 * the cctor will need to be run at aot method load time, for example,
5112 * or at the end of the compilation of the inlining method.
5114 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5119 * CAS - do not inline methods with declarative security
5120 * Note: this has to be before any possible return TRUE;
5122 if (mono_security_method_has_declsec (method))
5125 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5126 if (mono_arch_is_soft_float ()) {
5128 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5130 for (i = 0; i < sig->param_count; ++i)
5131 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5136 if (g_list_find (cfg->dont_inline, method))
5143 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5145 if (!cfg->compile_aot) {
5147 if (vtable->initialized)
5151 if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5152 if (cfg->method == method)
5156 if (!mono_class_needs_cctor_run (klass, method))
5159 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5160 /* The initialization is already done before the method is called */
5167 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5171 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5174 if (mini_is_gsharedvt_variable_klass (cfg, klass)) {
5177 mono_class_init (klass);
5178 size = mono_class_array_element_size (klass);
5181 mult_reg = alloc_preg (cfg);
5182 array_reg = arr->dreg;
5183 index_reg = index->dreg;
5185 #if SIZEOF_REGISTER == 8
5186 /* The array reg is 64 bits but the index reg is only 32 */
5187 if (COMPILE_LLVM (cfg)) {
5189 index2_reg = index_reg;
5191 index2_reg = alloc_preg (cfg);
5192 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5195 if (index->type == STACK_I8) {
5196 index2_reg = alloc_preg (cfg);
5197 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5199 index2_reg = index_reg;
5204 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5206 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5207 if (size == 1 || size == 2 || size == 4 || size == 8) {
5208 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5210 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5211 ins->klass = mono_class_get_element_class (klass);
5212 ins->type = STACK_MP;
5218 add_reg = alloc_ireg_mp (cfg);
5221 MonoInst *rgctx_ins;
5224 g_assert (cfg->generic_sharing_context);
5225 context_used = mini_class_check_context_used (cfg, klass);
5226 g_assert (context_used);
5227 rgctx_ins = emit_get_gsharedvt_info (cfg, &klass->byval_arg, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5228 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5230 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5232 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5233 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5234 ins->klass = mono_class_get_element_class (klass);
5235 ins->type = STACK_MP;
5236 MONO_ADD_INS (cfg->cbb, ins);
5241 #ifndef MONO_ARCH_EMULATE_MUL_DIV
5243 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5245 int bounds_reg = alloc_preg (cfg);
5246 int add_reg = alloc_ireg_mp (cfg);
5247 int mult_reg = alloc_preg (cfg);
5248 int mult2_reg = alloc_preg (cfg);
5249 int low1_reg = alloc_preg (cfg);
5250 int low2_reg = alloc_preg (cfg);
5251 int high1_reg = alloc_preg (cfg);
5252 int high2_reg = alloc_preg (cfg);
5253 int realidx1_reg = alloc_preg (cfg);
5254 int realidx2_reg = alloc_preg (cfg);
5255 int sum_reg = alloc_preg (cfg);
5256 int index1, index2, tmpreg;
5260 mono_class_init (klass);
5261 size = mono_class_array_element_size (klass);
5263 index1 = index_ins1->dreg;
5264 index2 = index_ins2->dreg;
5266 #if SIZEOF_REGISTER == 8
5267 /* The array reg is 64 bits but the index reg is only 32 */
5268 if (COMPILE_LLVM (cfg)) {
5271 tmpreg = alloc_preg (cfg);
5272 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5274 tmpreg = alloc_preg (cfg);
5275 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5279 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5283 /* range checking */
5284 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
5285 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5287 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
5288 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5289 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5290 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
5291 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5292 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5293 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5295 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
5296 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5297 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5298 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
5299 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5300 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5301 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5303 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5304 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5305 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5306 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5307 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5309 ins->type = STACK_MP;
5311 MONO_ADD_INS (cfg->cbb, ins);
5318 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5322 MonoMethod *addr_method;
5324 MonoClass *eclass = cmethod->klass->element_class;
5326 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5329 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5331 #ifndef MONO_ARCH_EMULATE_MUL_DIV
5332 /* emit_ldelema_2 depends on OP_LMUL */
5333 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (cfg, eclass)) {
5334 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5338 if (mini_is_gsharedvt_variable_klass (cfg, eclass))
5341 element_size = mono_class_array_element_size (eclass);
5342 addr_method = mono_marshal_get_array_address (rank, element_size);
5343 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5348 static MonoBreakPolicy
5349 always_insert_breakpoint (MonoMethod *method)
5351 return MONO_BREAK_POLICY_ALWAYS;
5354 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5357 * mono_set_break_policy:
5358 * policy_callback: the new callback function
5360 * Allow embedders to decide wherther to actually obey breakpoint instructions
5361 * (both break IL instructions and Debugger.Break () method calls), for example
5362 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5363 * untrusted or semi-trusted code.
5365 * @policy_callback will be called every time a break point instruction needs to
5366 * be inserted with the method argument being the method that calls Debugger.Break()
5367 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5368 * if it wants the breakpoint to not be effective in the given method.
5369 * #MONO_BREAK_POLICY_ALWAYS is the default.
5372 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5374 if (policy_callback)
5375 break_policy_func = policy_callback;
5377 break_policy_func = always_insert_breakpoint;
5381 should_insert_brekpoint (MonoMethod *method) {
5382 switch (break_policy_func (method)) {
5383 case MONO_BREAK_POLICY_ALWAYS:
5385 case MONO_BREAK_POLICY_NEVER:
5387 case MONO_BREAK_POLICY_ON_DBG:
5388 g_warning ("mdb no longer supported");
5391 g_warning ("Incorrect value returned from break policy callback");
5396 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5398 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5400 MonoInst *addr, *store, *load;
5401 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5403 /* the bounds check is already done by the callers */
5404 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5406 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5407 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5408 if (mini_type_is_reference (cfg, fsig->params [2]))
5409 emit_write_barrier (cfg, addr, load);
5411 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5412 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5419 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5421 return mini_type_is_reference (cfg, &klass->byval_arg);
5425 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5427 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5428 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5429 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5430 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5431 MonoInst *iargs [3];
5434 mono_class_setup_vtable (obj_array);
5435 g_assert (helper->slot);
5437 if (sp [0]->type != STACK_OBJ)
5439 if (sp [2]->type != STACK_OBJ)
5446 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5450 if (mini_is_gsharedvt_variable_klass (cfg, klass)) {
5453 // FIXME-VT: OP_ICONST optimization
5454 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5455 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5456 ins->opcode = OP_STOREV_MEMBASE;
5457 } else if (sp [1]->opcode == OP_ICONST) {
5458 int array_reg = sp [0]->dreg;
5459 int index_reg = sp [1]->dreg;
5460 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5463 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5464 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5466 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5467 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5468 if (generic_class_is_reference_type (cfg, klass))
5469 emit_write_barrier (cfg, addr, sp [2]);
5476 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5481 eklass = mono_class_from_mono_type (fsig->params [2]);
5483 eklass = mono_class_from_mono_type (fsig->ret);
5486 return emit_array_store (cfg, eklass, args, FALSE);
5488 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5489 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5495 is_unsafe_mov_compatible (MonoClass *param_klass, MonoClass *return_klass)
5499 //Only allow for valuetypes
5500 if (!param_klass->valuetype || !return_klass->valuetype)
5504 if (param_klass->has_references || return_klass->has_references)
5507 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5508 if ((MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5509 (!MONO_TYPE_ISSTRUCT (¶m_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)))
5512 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5513 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8)
5516 //And have the same size
5517 if (mono_class_value_size (param_klass, &align) != mono_class_value_size (return_klass, &align))
5523 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5525 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5526 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5528 //Valuetypes that are semantically equivalent
5529 if (is_unsafe_mov_compatible (param_klass, return_klass))
5532 //Arrays of valuetypes that are semantically equivalent
5533 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (param_klass->element_class, return_klass->element_class))
5540 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5542 #ifdef MONO_ARCH_SIMD_INTRINSICS
5543 MonoInst *ins = NULL;
5545 if (cfg->opt & MONO_OPT_SIMD) {
5546 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5552 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5556 emit_memory_barrier (MonoCompile *cfg, int kind)
5558 MonoInst *ins = NULL;
5559 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5560 MONO_ADD_INS (cfg->cbb, ins);
5561 ins->backend.memory_barrier_kind = kind;
5567 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5569 MonoInst *ins = NULL;
5572 /* The LLVM backend supports these intrinsics */
5573 if (cmethod->klass == mono_defaults.math_class) {
5574 if (strcmp (cmethod->name, "Sin") == 0) {
5576 } else if (strcmp (cmethod->name, "Cos") == 0) {
5578 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5580 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5584 if (opcode && fsig->param_count == 1) {
5585 MONO_INST_NEW (cfg, ins, opcode);
5586 ins->type = STACK_R8;
5587 ins->dreg = mono_alloc_freg (cfg);
5588 ins->sreg1 = args [0]->dreg;
5589 MONO_ADD_INS (cfg->cbb, ins);
5593 if (cfg->opt & MONO_OPT_CMOV) {
5594 if (strcmp (cmethod->name, "Min") == 0) {
5595 if (fsig->params [0]->type == MONO_TYPE_I4)
5597 if (fsig->params [0]->type == MONO_TYPE_U4)
5598 opcode = OP_IMIN_UN;
5599 else if (fsig->params [0]->type == MONO_TYPE_I8)
5601 else if (fsig->params [0]->type == MONO_TYPE_U8)
5602 opcode = OP_LMIN_UN;
5603 } else if (strcmp (cmethod->name, "Max") == 0) {
5604 if (fsig->params [0]->type == MONO_TYPE_I4)
5606 if (fsig->params [0]->type == MONO_TYPE_U4)
5607 opcode = OP_IMAX_UN;
5608 else if (fsig->params [0]->type == MONO_TYPE_I8)
5610 else if (fsig->params [0]->type == MONO_TYPE_U8)
5611 opcode = OP_LMAX_UN;
5615 if (opcode && fsig->param_count == 2) {
5616 MONO_INST_NEW (cfg, ins, opcode);
5617 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5618 ins->dreg = mono_alloc_ireg (cfg);
5619 ins->sreg1 = args [0]->dreg;
5620 ins->sreg2 = args [1]->dreg;
5621 MONO_ADD_INS (cfg->cbb, ins);
5629 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5631 if (cmethod->klass == mono_defaults.array_class) {
5632 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5633 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5634 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5635 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5636 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5637 return emit_array_unsafe_mov (cfg, fsig, args);
5644 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5646 MonoInst *ins = NULL;
5648 static MonoClass *runtime_helpers_class = NULL;
5649 if (! runtime_helpers_class)
5650 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
5651 "System.Runtime.CompilerServices", "RuntimeHelpers");
5653 if (cmethod->klass == mono_defaults.string_class) {
5654 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count == 2) {
5655 int dreg = alloc_ireg (cfg);
5656 int index_reg = alloc_preg (cfg);
5657 int add_reg = alloc_preg (cfg);
5659 #if SIZEOF_REGISTER == 8
5660 /* The array reg is 64 bits but the index reg is only 32 */
5661 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5663 index_reg = args [1]->dreg;
5665 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5667 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5668 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5669 add_reg = ins->dreg;
5670 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5673 int mult_reg = alloc_preg (cfg);
5674 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5675 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5676 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5677 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5679 type_from_op (cfg, ins, NULL, NULL);
5681 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count == 1) {
5682 int dreg = alloc_ireg (cfg);
5683 /* Decompose later to allow more optimizations */
5684 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5685 ins->type = STACK_I4;
5686 ins->flags |= MONO_INST_FAULT;
5687 cfg->cbb->has_array_access = TRUE;
5688 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5691 } else if (strcmp (cmethod->name, "InternalSetChar") == 0 && fsig->param_count == 3) {
5692 int mult_reg = alloc_preg (cfg);
5693 int add_reg = alloc_preg (cfg);
5695 /* The corlib functions check for oob already. */
5696 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, args [1]->dreg, 1);
5697 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5698 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, add_reg, MONO_STRUCT_OFFSET (MonoString, chars), args [2]->dreg);
5699 return cfg->cbb->last_ins;
5702 } else if (cmethod->klass == mono_defaults.object_class) {
5704 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count == 1) {
5705 int dreg = alloc_ireg_ref (cfg);
5706 int vt_reg = alloc_preg (cfg);
5707 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5708 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5709 type_from_op (cfg, ins, NULL, NULL);
5712 #if !defined(MONO_ARCH_EMULATE_MUL_DIV)
5713 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
5714 int dreg = alloc_ireg (cfg);
5715 int t1 = alloc_ireg (cfg);
5717 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
5718 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
5719 ins->type = STACK_I4;
5723 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
5724 MONO_INST_NEW (cfg, ins, OP_NOP);
5725 MONO_ADD_INS (cfg->cbb, ins);
5729 } else if (cmethod->klass == mono_defaults.array_class) {
5730 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count == 3 && !cfg->gsharedvt)
5731 return emit_array_generic_access (cfg, fsig, args, FALSE);
5732 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count == 3 && !cfg->gsharedvt)
5733 return emit_array_generic_access (cfg, fsig, args, TRUE);
5735 #ifndef MONO_BIG_ARRAYS
5737 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
5740 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count == 2) ||
5741 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count == 2)) &&
5742 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
5743 int dreg = alloc_ireg (cfg);
5744 int bounds_reg = alloc_ireg_mp (cfg);
5745 MonoBasicBlock *end_bb, *szarray_bb;
5746 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
5748 NEW_BBLOCK (cfg, end_bb);
5749 NEW_BBLOCK (cfg, szarray_bb);
5751 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
5752 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5753 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
5754 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
5755 /* Non-szarray case */
5757 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5758 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5760 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5761 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5762 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
5763 MONO_START_BB (cfg, szarray_bb);
5766 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5767 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5769 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5770 MONO_START_BB (cfg, end_bb);
5772 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
5773 ins->type = STACK_I4;
5779 if (cmethod->name [0] != 'g')
5782 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count == 1) {
5783 int dreg = alloc_ireg (cfg);
5784 int vtable_reg = alloc_preg (cfg);
5785 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
5786 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5787 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5788 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5789 type_from_op (cfg, ins, NULL, NULL);
5792 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count == 1) {
5793 int dreg = alloc_ireg (cfg);
5795 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5796 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5797 type_from_op (cfg, ins, NULL, NULL);
5802 } else if (cmethod->klass == runtime_helpers_class) {
5804 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5805 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5809 } else if (cmethod->klass == mono_defaults.thread_class) {
5810 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5811 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5812 MONO_ADD_INS (cfg->cbb, ins);
5814 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5815 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5816 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5818 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [0]);
5820 if (fsig->params [0]->type == MONO_TYPE_I1)
5821 opcode = OP_LOADI1_MEMBASE;
5822 else if (fsig->params [0]->type == MONO_TYPE_U1)
5823 opcode = OP_LOADU1_MEMBASE;
5824 else if (fsig->params [0]->type == MONO_TYPE_I2)
5825 opcode = OP_LOADI2_MEMBASE;
5826 else if (fsig->params [0]->type == MONO_TYPE_U2)
5827 opcode = OP_LOADU2_MEMBASE;
5828 else if (fsig->params [0]->type == MONO_TYPE_I4)
5829 opcode = OP_LOADI4_MEMBASE;
5830 else if (fsig->params [0]->type == MONO_TYPE_U4)
5831 opcode = OP_LOADU4_MEMBASE;
5832 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5833 opcode = OP_LOADI8_MEMBASE;
5834 else if (fsig->params [0]->type == MONO_TYPE_R4)
5835 opcode = OP_LOADR4_MEMBASE;
5836 else if (fsig->params [0]->type == MONO_TYPE_R8)
5837 opcode = OP_LOADR8_MEMBASE;
5838 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5839 opcode = OP_LOAD_MEMBASE;
5842 MONO_INST_NEW (cfg, ins, opcode);
5843 ins->inst_basereg = args [0]->dreg;
5844 ins->inst_offset = 0;
5845 MONO_ADD_INS (cfg->cbb, ins);
5847 switch (fsig->params [0]->type) {
5854 ins->dreg = mono_alloc_ireg (cfg);
5855 ins->type = STACK_I4;
5859 ins->dreg = mono_alloc_lreg (cfg);
5860 ins->type = STACK_I8;
5864 ins->dreg = mono_alloc_ireg (cfg);
5865 #if SIZEOF_REGISTER == 8
5866 ins->type = STACK_I8;
5868 ins->type = STACK_I4;
5873 ins->dreg = mono_alloc_freg (cfg);
5874 ins->type = STACK_R8;
5877 g_assert (mini_type_is_reference (cfg, fsig->params [0]));
5878 ins->dreg = mono_alloc_ireg_ref (cfg);
5879 ins->type = STACK_OBJ;
5883 if (opcode == OP_LOADI8_MEMBASE)
5884 ins = mono_decompose_opcode (cfg, ins, NULL);
5886 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
5890 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5892 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [0]);
5894 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5895 opcode = OP_STOREI1_MEMBASE_REG;
5896 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5897 opcode = OP_STOREI2_MEMBASE_REG;
5898 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5899 opcode = OP_STOREI4_MEMBASE_REG;
5900 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5901 opcode = OP_STOREI8_MEMBASE_REG;
5902 else if (fsig->params [0]->type == MONO_TYPE_R4)
5903 opcode = OP_STORER4_MEMBASE_REG;
5904 else if (fsig->params [0]->type == MONO_TYPE_R8)
5905 opcode = OP_STORER8_MEMBASE_REG;
5906 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5907 opcode = OP_STORE_MEMBASE_REG;
5910 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
5912 MONO_INST_NEW (cfg, ins, opcode);
5913 ins->sreg1 = args [1]->dreg;
5914 ins->inst_destbasereg = args [0]->dreg;
5915 ins->inst_offset = 0;
5916 MONO_ADD_INS (cfg->cbb, ins);
5918 if (opcode == OP_STOREI8_MEMBASE_REG)
5919 ins = mono_decompose_opcode (cfg, ins, NULL);
5924 } else if (cmethod->klass == mono_defaults.monitor_class) {
5925 #if defined(MONO_ARCH_MONITOR_OBJECT_REG)
5926 if (strcmp (cmethod->name, "Enter") == 0 && fsig->param_count == 1) {
5929 if (COMPILE_LLVM (cfg)) {
5931 * Pass the argument normally, the LLVM backend will handle the
5932 * calling convention problems.
5934 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_ENTER, NULL, helper_sig_monitor_enter_exit_trampoline_llvm, args);
5936 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_ENTER,
5937 NULL, helper_sig_monitor_enter_exit_trampoline, NULL);
5938 mono_call_inst_add_outarg_reg (cfg, call, args [0]->dreg,
5939 MONO_ARCH_MONITOR_OBJECT_REG, FALSE);
5942 return (MonoInst*)call;
5943 #if defined(MONO_ARCH_MONITOR_LOCK_TAKEN_REG)
5944 } else if (strcmp (cmethod->name, "Enter") == 0 && fsig->param_count == 2) {
5947 if (COMPILE_LLVM (cfg)) {
5949 * Pass the argument normally, the LLVM backend will handle the
5950 * calling convention problems.
5952 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_ENTER_V4, NULL, helper_sig_monitor_enter_v4_trampoline_llvm, args);
5954 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_ENTER_V4,
5955 NULL, helper_sig_monitor_enter_exit_trampoline, NULL);
5956 mono_call_inst_add_outarg_reg (cfg, call, args [0]->dreg, MONO_ARCH_MONITOR_OBJECT_REG, FALSE);
5957 mono_call_inst_add_outarg_reg (cfg, call, args [1]->dreg, MONO_ARCH_MONITOR_LOCK_TAKEN_REG, FALSE);
5960 return (MonoInst*)call;
5962 } else if (strcmp (cmethod->name, "Exit") == 0 && fsig->param_count == 1) {
5965 if (COMPILE_LLVM (cfg)) {
5966 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_EXIT, NULL, helper_sig_monitor_enter_exit_trampoline_llvm, args);
5968 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_MONITOR_EXIT,
5969 NULL, helper_sig_monitor_enter_exit_trampoline, NULL);
5970 mono_call_inst_add_outarg_reg (cfg, call, args [0]->dreg,
5971 MONO_ARCH_MONITOR_OBJECT_REG, FALSE);
5974 return (MonoInst*)call;
5977 } else if (cmethod->klass->image == mono_defaults.corlib &&
5978 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5979 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5982 #if SIZEOF_REGISTER == 8
5983 if (strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5984 if (mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5985 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5986 ins->dreg = mono_alloc_preg (cfg);
5987 ins->sreg1 = args [0]->dreg;
5988 ins->type = STACK_I8;
5989 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5990 MONO_ADD_INS (cfg->cbb, ins);
5994 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5996 /* 64 bit reads are already atomic */
5997 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5998 load_ins->dreg = mono_alloc_preg (cfg);
5999 load_ins->inst_basereg = args [0]->dreg;
6000 load_ins->inst_offset = 0;
6001 load_ins->type = STACK_I8;
6002 MONO_ADD_INS (cfg->cbb, load_ins);
6004 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6011 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6012 MonoInst *ins_iconst;
6015 if (fsig->params [0]->type == MONO_TYPE_I4) {
6016 opcode = OP_ATOMIC_ADD_I4;
6017 cfg->has_atomic_add_i4 = TRUE;
6019 #if SIZEOF_REGISTER == 8
6020 else if (fsig->params [0]->type == MONO_TYPE_I8)
6021 opcode = OP_ATOMIC_ADD_I8;
6024 if (!mono_arch_opcode_supported (opcode))
6026 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6027 ins_iconst->inst_c0 = 1;
6028 ins_iconst->dreg = mono_alloc_ireg (cfg);
6029 MONO_ADD_INS (cfg->cbb, ins_iconst);
6031 MONO_INST_NEW (cfg, ins, opcode);
6032 ins->dreg = mono_alloc_ireg (cfg);
6033 ins->inst_basereg = args [0]->dreg;
6034 ins->inst_offset = 0;
6035 ins->sreg2 = ins_iconst->dreg;
6036 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6037 MONO_ADD_INS (cfg->cbb, ins);
6039 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6040 MonoInst *ins_iconst;
6043 if (fsig->params [0]->type == MONO_TYPE_I4) {
6044 opcode = OP_ATOMIC_ADD_I4;
6045 cfg->has_atomic_add_i4 = TRUE;
6047 #if SIZEOF_REGISTER == 8
6048 else if (fsig->params [0]->type == MONO_TYPE_I8)
6049 opcode = OP_ATOMIC_ADD_I8;
6052 if (!mono_arch_opcode_supported (opcode))
6054 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6055 ins_iconst->inst_c0 = -1;
6056 ins_iconst->dreg = mono_alloc_ireg (cfg);
6057 MONO_ADD_INS (cfg->cbb, ins_iconst);
6059 MONO_INST_NEW (cfg, ins, opcode);
6060 ins->dreg = mono_alloc_ireg (cfg);
6061 ins->inst_basereg = args [0]->dreg;
6062 ins->inst_offset = 0;
6063 ins->sreg2 = ins_iconst->dreg;
6064 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6065 MONO_ADD_INS (cfg->cbb, ins);
6067 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6070 if (fsig->params [0]->type == MONO_TYPE_I4) {
6071 opcode = OP_ATOMIC_ADD_I4;
6072 cfg->has_atomic_add_i4 = TRUE;
6074 #if SIZEOF_REGISTER == 8
6075 else if (fsig->params [0]->type == MONO_TYPE_I8)
6076 opcode = OP_ATOMIC_ADD_I8;
6079 if (!mono_arch_opcode_supported (opcode))
6081 MONO_INST_NEW (cfg, ins, opcode);
6082 ins->dreg = mono_alloc_ireg (cfg);
6083 ins->inst_basereg = args [0]->dreg;
6084 ins->inst_offset = 0;
6085 ins->sreg2 = args [1]->dreg;
6086 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6087 MONO_ADD_INS (cfg->cbb, ins);
6090 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6091 MonoInst *f2i = NULL, *i2f;
6092 guint32 opcode, f2i_opcode, i2f_opcode;
6093 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [0]);
6094 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6096 if (fsig->params [0]->type == MONO_TYPE_I4 ||
6097 fsig->params [0]->type == MONO_TYPE_R4) {
6098 opcode = OP_ATOMIC_EXCHANGE_I4;
6099 f2i_opcode = OP_MOVE_F_TO_I4;
6100 i2f_opcode = OP_MOVE_I4_TO_F;
6101 cfg->has_atomic_exchange_i4 = TRUE;
6103 #if SIZEOF_REGISTER == 8
6105 fsig->params [0]->type == MONO_TYPE_I8 ||
6106 fsig->params [0]->type == MONO_TYPE_R8 ||
6107 fsig->params [0]->type == MONO_TYPE_I) {
6108 opcode = OP_ATOMIC_EXCHANGE_I8;
6109 f2i_opcode = OP_MOVE_F_TO_I8;
6110 i2f_opcode = OP_MOVE_I8_TO_F;
6113 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6114 opcode = OP_ATOMIC_EXCHANGE_I4;
6115 cfg->has_atomic_exchange_i4 = TRUE;
6121 if (!mono_arch_opcode_supported (opcode))
6125 /* TODO: Decompose these opcodes instead of bailing here. */
6126 if (COMPILE_SOFT_FLOAT (cfg))
6129 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6130 f2i->dreg = mono_alloc_ireg (cfg);
6131 f2i->sreg1 = args [1]->dreg;
6132 if (f2i_opcode == OP_MOVE_F_TO_I4)
6133 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6134 MONO_ADD_INS (cfg->cbb, f2i);
6137 MONO_INST_NEW (cfg, ins, opcode);
6138 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6139 ins->inst_basereg = args [0]->dreg;
6140 ins->inst_offset = 0;
6141 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6142 MONO_ADD_INS (cfg->cbb, ins);
6144 switch (fsig->params [0]->type) {
6146 ins->type = STACK_I4;
6149 ins->type = STACK_I8;
6152 #if SIZEOF_REGISTER == 8
6153 ins->type = STACK_I8;
6155 ins->type = STACK_I4;
6160 ins->type = STACK_R8;
6163 g_assert (mini_type_is_reference (cfg, fsig->params [0]));
6164 ins->type = STACK_OBJ;
6169 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6170 i2f->dreg = mono_alloc_freg (cfg);
6171 i2f->sreg1 = ins->dreg;
6172 i2f->type = STACK_R8;
6173 if (i2f_opcode == OP_MOVE_I4_TO_F)
6174 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6175 MONO_ADD_INS (cfg->cbb, i2f);
6180 if (cfg->gen_write_barriers && is_ref)
6181 emit_write_barrier (cfg, args [0], args [1]);
6183 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6184 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6185 guint32 opcode, f2i_opcode, i2f_opcode;
6186 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [1]);
6187 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6189 if (fsig->params [1]->type == MONO_TYPE_I4 ||
6190 fsig->params [1]->type == MONO_TYPE_R4) {
6191 opcode = OP_ATOMIC_CAS_I4;
6192 f2i_opcode = OP_MOVE_F_TO_I4;
6193 i2f_opcode = OP_MOVE_I4_TO_F;
6194 cfg->has_atomic_cas_i4 = TRUE;
6196 #if SIZEOF_REGISTER == 8
6198 fsig->params [1]->type == MONO_TYPE_I8 ||
6199 fsig->params [1]->type == MONO_TYPE_R8 ||
6200 fsig->params [1]->type == MONO_TYPE_I) {
6201 opcode = OP_ATOMIC_CAS_I8;
6202 f2i_opcode = OP_MOVE_F_TO_I8;
6203 i2f_opcode = OP_MOVE_I8_TO_F;
6206 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6207 opcode = OP_ATOMIC_CAS_I4;
6208 cfg->has_atomic_cas_i4 = TRUE;
6214 if (!mono_arch_opcode_supported (opcode))
6218 /* TODO: Decompose these opcodes instead of bailing here. */
6219 if (COMPILE_SOFT_FLOAT (cfg))
6222 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6223 f2i_new->dreg = mono_alloc_ireg (cfg);
6224 f2i_new->sreg1 = args [1]->dreg;
6225 if (f2i_opcode == OP_MOVE_F_TO_I4)
6226 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6227 MONO_ADD_INS (cfg->cbb, f2i_new);
6229 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6230 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6231 f2i_cmp->sreg1 = args [2]->dreg;
6232 if (f2i_opcode == OP_MOVE_F_TO_I4)
6233 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6234 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6237 MONO_INST_NEW (cfg, ins, opcode);
6238 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6239 ins->sreg1 = args [0]->dreg;
6240 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6241 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6242 MONO_ADD_INS (cfg->cbb, ins);
6244 switch (fsig->params [1]->type) {
6246 ins->type = STACK_I4;
6249 ins->type = STACK_I8;
6252 #if SIZEOF_REGISTER == 8
6253 ins->type = STACK_I8;
6255 ins->type = STACK_I4;
6260 ins->type = STACK_R8;
6263 g_assert (mini_type_is_reference (cfg, fsig->params [1]));
6264 ins->type = STACK_OBJ;
6269 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6270 i2f->dreg = mono_alloc_freg (cfg);
6271 i2f->sreg1 = ins->dreg;
6272 i2f->type = STACK_R8;
6273 if (i2f_opcode == OP_MOVE_I4_TO_F)
6274 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6275 MONO_ADD_INS (cfg->cbb, i2f);
6280 if (cfg->gen_write_barriers && is_ref)
6281 emit_write_barrier (cfg, args [0], args [1]);
6283 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6284 fsig->params [1]->type == MONO_TYPE_I4) {
6285 MonoInst *cmp, *ceq;
6287 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6290 /* int32 r = CAS (location, value, comparand); */
6291 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6292 ins->dreg = alloc_ireg (cfg);
6293 ins->sreg1 = args [0]->dreg;
6294 ins->sreg2 = args [1]->dreg;
6295 ins->sreg3 = args [2]->dreg;
6296 ins->type = STACK_I4;
6297 MONO_ADD_INS (cfg->cbb, ins);
6299 /* bool result = r == comparand; */
6300 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6301 cmp->sreg1 = ins->dreg;
6302 cmp->sreg2 = args [2]->dreg;
6303 cmp->type = STACK_I4;
6304 MONO_ADD_INS (cfg->cbb, cmp);
6306 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6307 ceq->dreg = alloc_ireg (cfg);
6308 ceq->type = STACK_I4;
6309 MONO_ADD_INS (cfg->cbb, ceq);
6311 /* *success = result; */
6312 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6314 cfg->has_atomic_cas_i4 = TRUE;
6316 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6317 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6321 } else if (cmethod->klass->image == mono_defaults.corlib &&
6322 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6323 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6326 if (!strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6328 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [0]);
6329 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6331 if (fsig->params [0]->type == MONO_TYPE_I1)
6332 opcode = OP_ATOMIC_LOAD_I1;
6333 else if (fsig->params [0]->type == MONO_TYPE_U1 || fsig->params [0]->type == MONO_TYPE_BOOLEAN)
6334 opcode = OP_ATOMIC_LOAD_U1;
6335 else if (fsig->params [0]->type == MONO_TYPE_I2)
6336 opcode = OP_ATOMIC_LOAD_I2;
6337 else if (fsig->params [0]->type == MONO_TYPE_U2)
6338 opcode = OP_ATOMIC_LOAD_U2;
6339 else if (fsig->params [0]->type == MONO_TYPE_I4)
6340 opcode = OP_ATOMIC_LOAD_I4;
6341 else if (fsig->params [0]->type == MONO_TYPE_U4)
6342 opcode = OP_ATOMIC_LOAD_U4;
6343 else if (fsig->params [0]->type == MONO_TYPE_R4)
6344 opcode = OP_ATOMIC_LOAD_R4;
6345 else if (fsig->params [0]->type == MONO_TYPE_R8)
6346 opcode = OP_ATOMIC_LOAD_R8;
6347 #if SIZEOF_REGISTER == 8
6348 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_I)
6349 opcode = OP_ATOMIC_LOAD_I8;
6350 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U8 || fsig->params [0]->type == MONO_TYPE_U)
6351 opcode = OP_ATOMIC_LOAD_U8;
6353 else if (fsig->params [0]->type == MONO_TYPE_I)
6354 opcode = OP_ATOMIC_LOAD_I4;
6355 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U)
6356 opcode = OP_ATOMIC_LOAD_U4;
6360 if (!mono_arch_opcode_supported (opcode))
6363 MONO_INST_NEW (cfg, ins, opcode);
6364 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6365 ins->sreg1 = args [0]->dreg;
6366 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6367 MONO_ADD_INS (cfg->cbb, ins);
6369 switch (fsig->params [0]->type) {
6370 case MONO_TYPE_BOOLEAN:
6377 ins->type = STACK_I4;
6381 ins->type = STACK_I8;
6385 #if SIZEOF_REGISTER == 8
6386 ins->type = STACK_I8;
6388 ins->type = STACK_I4;
6393 ins->type = STACK_R8;
6396 g_assert (mini_type_is_reference (cfg, fsig->params [0]));
6397 ins->type = STACK_OBJ;
6403 if (!strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6405 gboolean is_ref = mini_type_is_reference (cfg, fsig->params [0]);
6407 if (fsig->params [0]->type == MONO_TYPE_I1)
6408 opcode = OP_ATOMIC_STORE_I1;
6409 else if (fsig->params [0]->type == MONO_TYPE_U1 || fsig->params [0]->type == MONO_TYPE_BOOLEAN)
6410 opcode = OP_ATOMIC_STORE_U1;
6411 else if (fsig->params [0]->type == MONO_TYPE_I2)
6412 opcode = OP_ATOMIC_STORE_I2;
6413 else if (fsig->params [0]->type == MONO_TYPE_U2)
6414 opcode = OP_ATOMIC_STORE_U2;
6415 else if (fsig->params [0]->type == MONO_TYPE_I4)
6416 opcode = OP_ATOMIC_STORE_I4;
6417 else if (fsig->params [0]->type == MONO_TYPE_U4)
6418 opcode = OP_ATOMIC_STORE_U4;
6419 else if (fsig->params [0]->type == MONO_TYPE_R4)
6420 opcode = OP_ATOMIC_STORE_R4;
6421 else if (fsig->params [0]->type == MONO_TYPE_R8)
6422 opcode = OP_ATOMIC_STORE_R8;
6423 #if SIZEOF_REGISTER == 8
6424 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_I)
6425 opcode = OP_ATOMIC_STORE_I8;
6426 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U8 || fsig->params [0]->type == MONO_TYPE_U)
6427 opcode = OP_ATOMIC_STORE_U8;
6429 else if (fsig->params [0]->type == MONO_TYPE_I)
6430 opcode = OP_ATOMIC_STORE_I4;
6431 else if (is_ref || fsig->params [0]->type == MONO_TYPE_U)
6432 opcode = OP_ATOMIC_STORE_U4;
6436 if (!mono_arch_opcode_supported (opcode))
6439 MONO_INST_NEW (cfg, ins, opcode);
6440 ins->dreg = args [0]->dreg;
6441 ins->sreg1 = args [1]->dreg;
6442 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6443 MONO_ADD_INS (cfg->cbb, ins);
6445 if (cfg->gen_write_barriers && is_ref)
6446 emit_write_barrier (cfg, args [0], args [1]);
6452 } else if (cmethod->klass->image == mono_defaults.corlib &&
6453 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6454 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6455 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6456 if (should_insert_brekpoint (cfg->method)) {
6457 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6459 MONO_INST_NEW (cfg, ins, OP_NOP);
6460 MONO_ADD_INS (cfg->cbb, ins);
6464 } else if (cmethod->klass->image == mono_defaults.corlib &&
6465 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6466 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6467 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6469 EMIT_NEW_ICONST (cfg, ins, 1);
6471 EMIT_NEW_ICONST (cfg, ins, 0);
6474 } else if (cmethod->klass == mono_defaults.math_class) {
6476 * There is general branchless code for Min/Max, but it does not work for
6478 * http://everything2.com/?node_id=1051618
6480 } else if ((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6481 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6482 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6483 !strcmp (cmethod->klass->name, "Selector")) {
6484 #ifdef MONO_ARCH_HAVE_OBJC_GET_SELECTOR
6485 if (!strcmp (cmethod->klass->name, "GetHandle") && fsig->param_count == 1 &&
6486 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6489 MonoJumpInfoToken *ji;
6492 cfg->disable_llvm = TRUE;
6494 if (args [0]->opcode == OP_GOT_ENTRY) {
6495 pi = args [0]->inst_p1;
6496 g_assert (pi->opcode == OP_PATCH_INFO);
6497 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6500 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6501 ji = args [0]->inst_p0;
6504 NULLIFY_INS (args [0]);
6507 s = mono_ldstr (cfg->domain, ji->image, mono_metadata_token_index (ji->token));
6508 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6509 ins->dreg = mono_alloc_ireg (cfg);
6511 ins->inst_p0 = mono_string_to_utf8 (s);
6512 MONO_ADD_INS (cfg->cbb, ins);
6518 #ifdef MONO_ARCH_SIMD_INTRINSICS
6519 if (cfg->opt & MONO_OPT_SIMD) {
6520 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6526 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6530 if (COMPILE_LLVM (cfg)) {
6531 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6536 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6540 * This entry point could be used later for arbitrary method
6543 inline static MonoInst*
6544 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6545 MonoMethodSignature *signature, MonoInst **args, MonoInst *this)
6547 if (method->klass == mono_defaults.string_class) {
6548 /* managed string allocation support */
6549 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6550 MonoInst *iargs [2];
6551 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6552 MonoMethod *managed_alloc = NULL;
6554 g_assert (vtable); /*Should not fail since it System.String*/
6555 #ifndef MONO_CROSS_COMPILE
6556 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6560 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6561 iargs [1] = args [0];
6562 return mono_emit_method_call (cfg, managed_alloc, iargs, this);
6569 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6571 MonoInst *store, *temp;
6574 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6575 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6578 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6579 * would be different than the MonoInst's used to represent arguments, and
6580 * the ldelema implementation can't deal with that.
6581 * Solution: When ldelema is used on an inline argument, create a var for
6582 * it, emit ldelema on that var, and emit the saving code below in
6583 * inline_method () if needed.
6585 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6586 cfg->args [i] = temp;
6587 /* This uses cfg->args [i] which is set by the preceeding line */
6588 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6589 store->cil_code = sp [0]->cil_code;
6594 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6595 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6597 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6599 check_inline_called_method_name_limit (MonoMethod *called_method)
6602 static const char *limit = NULL;
6604 if (limit == NULL) {
6605 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6607 if (limit_string != NULL)
6608 limit = limit_string;
6613 if (limit [0] != '\0') {
6614 char *called_method_name = mono_method_full_name (called_method, TRUE);
6616 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6617 g_free (called_method_name);
6619 //return (strncmp_result <= 0);
6620 return (strncmp_result == 0);
6627 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6629 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6632 static const char *limit = NULL;
6634 if (limit == NULL) {
6635 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6636 if (limit_string != NULL) {
6637 limit = limit_string;
6643 if (limit [0] != '\0') {
6644 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6646 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6647 g_free (caller_method_name);
6649 //return (strncmp_result <= 0);
6650 return (strncmp_result == 0);
6658 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6660 static double r8_0 = 0.0;
6661 static float r4_0 = 0.0;
6665 rtype = mini_get_underlying_type (cfg, rtype);
6669 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6670 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6671 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6672 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6673 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6674 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6675 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6676 ins->type = STACK_R4;
6677 ins->inst_p0 = (void*)&r4_0;
6679 MONO_ADD_INS (cfg->cbb, ins);
6680 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6681 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6682 ins->type = STACK_R8;
6683 ins->inst_p0 = (void*)&r8_0;
6685 MONO_ADD_INS (cfg->cbb, ins);
6686 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6687 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6688 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6689 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (cfg, rtype)) {
6690 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6692 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6697 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6701 rtype = mini_get_underlying_type (cfg, rtype);
6705 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6706 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6707 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6708 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6709 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6710 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6711 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6712 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6713 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6714 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6715 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6716 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6717 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (cfg, rtype)) {
6718 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6720 emit_init_rvar (cfg, dreg, rtype);
6724 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6726 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6728 MonoInst *var = cfg->locals [local];
6729 if (COMPILE_SOFT_FLOAT (cfg)) {
6731 int reg = alloc_dreg (cfg, var->type);
6732 emit_init_rvar (cfg, reg, type);
6733 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6736 emit_init_rvar (cfg, var->dreg, type);
6738 emit_dummy_init_rvar (cfg, var->dreg, type);
6745 * Return the cost of inlining CMETHOD.
6748 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6749 guchar *ip, guint real_offset, gboolean inline_always, MonoBasicBlock **out_cbb)
6751 MonoInst *ins, *rvar = NULL;
6752 MonoMethodHeader *cheader;
6753 MonoBasicBlock *ebblock, *sbblock;
6755 MonoMethod *prev_inlined_method;
6756 MonoInst **prev_locals, **prev_args;
6757 MonoType **prev_arg_types;
6758 guint prev_real_offset;
6759 GHashTable *prev_cbb_hash;
6760 MonoBasicBlock **prev_cil_offset_to_bb;
6761 MonoBasicBlock *prev_cbb;
6762 unsigned char* prev_cil_start;
6763 guint32 prev_cil_offset_to_bb_len;
6764 MonoMethod *prev_current_method;
6765 MonoGenericContext *prev_generic_context;
6766 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual = FALSE;
6768 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6770 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6771 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6774 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6775 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6780 fsig = mono_method_signature (cmethod);
6782 if (cfg->verbose_level > 2)
6783 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6785 if (!cmethod->inline_info) {
6786 cfg->stat_inlineable_methods++;
6787 cmethod->inline_info = 1;
6790 /* allocate local variables */
6791 cheader = mono_method_get_header (cmethod);
6793 if (cheader == NULL || mono_loader_get_last_error ()) {
6794 MonoLoaderError *error = mono_loader_get_last_error ();
6797 mono_metadata_free_mh (cheader);
6798 if (inline_always && error)
6799 mono_cfg_set_exception (cfg, error->exception_type);
6801 mono_loader_clear_error ();
6805 /*Must verify before creating locals as it can cause the JIT to assert.*/
6806 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6807 mono_metadata_free_mh (cheader);
6811 /* allocate space to store the return value */
6812 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6813 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6816 prev_locals = cfg->locals;
6817 cfg->locals = mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6818 for (i = 0; i < cheader->num_locals; ++i)
6819 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6821 /* allocate start and end blocks */
6822 /* This is needed so if the inline is aborted, we can clean up */
6823 NEW_BBLOCK (cfg, sbblock);
6824 sbblock->real_offset = real_offset;
6826 NEW_BBLOCK (cfg, ebblock);
6827 ebblock->block_num = cfg->num_bblocks++;
6828 ebblock->real_offset = real_offset;
6830 prev_args = cfg->args;
6831 prev_arg_types = cfg->arg_types;
6832 prev_inlined_method = cfg->inlined_method;
6833 cfg->inlined_method = cmethod;
6834 cfg->ret_var_set = FALSE;
6835 cfg->inline_depth ++;
6836 prev_real_offset = cfg->real_offset;
6837 prev_cbb_hash = cfg->cbb_hash;
6838 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6839 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6840 prev_cil_start = cfg->cil_start;
6841 prev_cbb = cfg->cbb;
6842 prev_current_method = cfg->current_method;
6843 prev_generic_context = cfg->generic_context;
6844 prev_ret_var_set = cfg->ret_var_set;
6845 prev_disable_inline = cfg->disable_inline;
6847 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6850 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual);
6852 ret_var_set = cfg->ret_var_set;
6854 cfg->inlined_method = prev_inlined_method;
6855 cfg->real_offset = prev_real_offset;
6856 cfg->cbb_hash = prev_cbb_hash;
6857 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6858 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6859 cfg->cil_start = prev_cil_start;
6860 cfg->locals = prev_locals;
6861 cfg->args = prev_args;
6862 cfg->arg_types = prev_arg_types;
6863 cfg->current_method = prev_current_method;
6864 cfg->generic_context = prev_generic_context;
6865 cfg->ret_var_set = prev_ret_var_set;
6866 cfg->disable_inline = prev_disable_inline;
6867 cfg->inline_depth --;
6869 if ((costs >= 0 && costs < 60) || inline_always) {
6870 if (cfg->verbose_level > 2)
6871 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6873 cfg->stat_inlined_methods++;
6875 /* always add some code to avoid block split failures */
6876 MONO_INST_NEW (cfg, ins, OP_NOP);
6877 MONO_ADD_INS (prev_cbb, ins);
6879 prev_cbb->next_bb = sbblock;
6880 link_bblock (cfg, prev_cbb, sbblock);
6883 * Get rid of the begin and end bblocks if possible to aid local
6886 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6888 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6889 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6891 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6892 MonoBasicBlock *prev = ebblock->in_bb [0];
6893 mono_merge_basic_blocks (cfg, prev, ebblock);
6895 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6896 mono_merge_basic_blocks (cfg, prev_cbb, prev);
6897 cfg->cbb = prev_cbb;
6901 * Its possible that the rvar is set in some prev bblock, but not in others.
6907 for (i = 0; i < ebblock->in_count; ++i) {
6908 bb = ebblock->in_bb [i];
6910 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6913 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6922 *out_cbb = cfg->cbb;
6926 * If the inlined method contains only a throw, then the ret var is not
6927 * set, so set it to a dummy value.
6930 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6932 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6935 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6938 if (cfg->verbose_level > 2)
6939 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6940 cfg->exception_type = MONO_EXCEPTION_NONE;
6941 mono_loader_clear_error ();
6943 /* This gets rid of the newly added bblocks */
6944 cfg->cbb = prev_cbb;
6946 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6951 * Some of these comments may well be out-of-date.
6952 * Design decisions: we do a single pass over the IL code (and we do bblock
6953 * splitting/merging in the few cases when it's required: a back jump to an IL
6954 * address that was not already seen as bblock starting point).
6955 * Code is validated as we go (full verification is still better left to metadata/verify.c).
6956 * Complex operations are decomposed in simpler ones right away. We need to let the
6957 * arch-specific code peek and poke inside this process somehow (except when the
6958 * optimizations can take advantage of the full semantic info of coarse opcodes).
6959 * All the opcodes of the form opcode.s are 'normalized' to opcode.
6960 * MonoInst->opcode initially is the IL opcode or some simplification of that
6961 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
6962 * opcode with value bigger than OP_LAST.
6963 * At this point the IR can be handed over to an interpreter, a dumb code generator
6964 * or to the optimizing code generator that will translate it to SSA form.
6966 * Profiling directed optimizations.
6967 * We may compile by default with few or no optimizations and instrument the code
6968 * or the user may indicate what methods to optimize the most either in a config file
6969 * or through repeated runs where the compiler applies offline the optimizations to
6970 * each method and then decides if it was worth it.
6973 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6974 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6975 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6976 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6977 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6978 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6979 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6980 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) TYPE_LOAD_ERROR ((klass))
6982 /* offset from br.s -> br like opcodes */
6983 #define BIG_BRANCH_OFFSET 13
6986 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6988 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6990 return b == NULL || b == bb;
6994 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6996 unsigned char *ip = start;
6997 unsigned char *target;
7000 MonoBasicBlock *bblock;
7001 const MonoOpcode *opcode;
7004 cli_addr = ip - start;
7005 i = mono_opcode_value ((const guint8 **)&ip, end);
7008 opcode = &mono_opcodes [i];
7009 switch (opcode->argument) {
7010 case MonoInlineNone:
7013 case MonoInlineString:
7014 case MonoInlineType:
7015 case MonoInlineField:
7016 case MonoInlineMethod:
7019 case MonoShortInlineR:
7026 case MonoShortInlineVar:
7027 case MonoShortInlineI:
7030 case MonoShortInlineBrTarget:
7031 target = start + cli_addr + 2 + (signed char)ip [1];
7032 GET_BBLOCK (cfg, bblock, target);
7035 GET_BBLOCK (cfg, bblock, ip);
7037 case MonoInlineBrTarget:
7038 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7039 GET_BBLOCK (cfg, bblock, target);
7042 GET_BBLOCK (cfg, bblock, ip);
7044 case MonoInlineSwitch: {
7045 guint32 n = read32 (ip + 1);
7048 cli_addr += 5 + 4 * n;
7049 target = start + cli_addr;
7050 GET_BBLOCK (cfg, bblock, target);
7052 for (j = 0; j < n; ++j) {
7053 target = start + cli_addr + (gint32)read32 (ip);
7054 GET_BBLOCK (cfg, bblock, target);
7064 g_assert_not_reached ();
7067 if (i == CEE_THROW) {
7068 unsigned char *bb_start = ip - 1;
7070 /* Find the start of the bblock containing the throw */
7072 while ((bb_start >= start) && !bblock) {
7073 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7077 bblock->out_of_line = 1;
7087 static inline MonoMethod *
7088 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7092 if (m->wrapper_type != MONO_WRAPPER_NONE) {
7093 method = mono_method_get_wrapper_data (m, token);
7096 method = mono_class_inflate_generic_method_checked (method, context, &error);
7097 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
7100 method = mono_get_method_full (m->klass->image, token, klass, context);
7106 static inline MonoMethod *
7107 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7109 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
7111 if (method && cfg && !cfg->generic_sharing_context && mono_class_is_open_constructed_type (&method->klass->byval_arg))
7117 static inline MonoClass*
7118 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7123 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7124 klass = mono_method_get_wrapper_data (method, token);
7126 klass = mono_class_inflate_generic_class (klass, context);
7128 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7129 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7132 mono_class_init (klass);
7136 static inline MonoMethodSignature*
7137 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
7139 MonoMethodSignature *fsig;
7141 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7144 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7146 fsig = mono_inflate_generic_signature (fsig, context, &error);
7148 g_assert (mono_error_ok (&error));
7151 fsig = mono_metadata_parse_signature (method->klass->image, token);
7157 * Returns TRUE if the JIT should abort inlining because "callee"
7158 * is influenced by security attributes.
7161 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7165 if ((cfg->method != caller) && mono_security_method_has_declsec (callee)) {
7169 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
7170 if (result == MONO_JIT_SECURITY_OK)
7173 if (result == MONO_JIT_LINKDEMAND_ECMA) {
7174 /* Generate code to throw a SecurityException before the actual call/link */
7175 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7178 NEW_ICONST (cfg, args [0], 4);
7179 NEW_METHODCONST (cfg, args [1], caller);
7180 mono_emit_method_call (cfg, secman->linkdemandsecurityexception, args, NULL);
7181 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
7182 /* don't hide previous results */
7183 mono_cfg_set_exception (cfg, MONO_EXCEPTION_SECURITY_LINKDEMAND);
7184 cfg->exception_data = result;
7192 throw_exception (void)
7194 static MonoMethod *method = NULL;
7197 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7198 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7205 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7207 MonoMethod *thrower = throw_exception ();
7210 EMIT_NEW_PCONST (cfg, args [0], ex);
7211 mono_emit_method_call (cfg, thrower, args, NULL);
7215 * Return the original method is a wrapper is specified. We can only access
7216 * the custom attributes from the original method.
7219 get_original_method (MonoMethod *method)
7221 if (method->wrapper_type == MONO_WRAPPER_NONE)
7224 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7225 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7228 /* in other cases we need to find the original method */
7229 return mono_marshal_method_from_wrapper (method);
7233 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field,
7234 MonoBasicBlock *bblock, unsigned char *ip)
7236 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7237 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7239 emit_throw_exception (cfg, ex);
7243 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
7244 MonoBasicBlock *bblock, unsigned char *ip)
7246 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7247 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7249 emit_throw_exception (cfg, ex);
7253 * Check that the IL instructions at ip are the array initialization
7254 * sequence and return the pointer to the data and the size.
7257 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7260 * newarr[System.Int32]
7262 * ldtoken field valuetype ...
7263 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7265 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7267 guint32 token = read32 (ip + 7);
7268 guint32 field_token = read32 (ip + 2);
7269 guint32 field_index = field_token & 0xffffff;
7271 const char *data_ptr;
7273 MonoMethod *cmethod;
7274 MonoClass *dummy_class;
7275 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7279 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7283 *out_field_token = field_token;
7285 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7288 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7290 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7291 case MONO_TYPE_BOOLEAN:
7295 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7296 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7297 case MONO_TYPE_CHAR:
7314 if (size > mono_type_size (field->type, &dummy_align))
7317 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7318 if (!image_is_dynamic (method->klass->image)) {
7319 field_index = read32 (ip + 2) & 0xffffff;
7320 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7321 data_ptr = mono_image_rva_map (method->klass->image, rva);
7322 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7323 /* for aot code we do the lookup on load */
7324 if (aot && data_ptr)
7325 return GUINT_TO_POINTER (rva);
7327 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
7329 data_ptr = mono_field_get_data (field);
7337 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7339 char *method_fname = mono_method_full_name (method, TRUE);
7341 MonoMethodHeader *header = mono_method_get_header (method);
7343 if (header->code_size == 0)
7344 method_code = g_strdup ("method body is empty.");
7346 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7347 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
7348 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
7349 g_free (method_fname);
7350 g_free (method_code);
7351 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7355 set_exception_object (MonoCompile *cfg, MonoException *exception)
7357 mono_cfg_set_exception (cfg, MONO_EXCEPTION_OBJECT_SUPPLIED);
7358 MONO_GC_REGISTER_ROOT_SINGLE (cfg->exception_ptr);
7359 cfg->exception_ptr = exception;
7363 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7366 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7367 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
7368 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7369 /* Optimize reg-reg moves away */
7371 * Can't optimize other opcodes, since sp[0] might point to
7372 * the last ins of a decomposed opcode.
7374 sp [0]->dreg = (cfg)->locals [n]->dreg;
7376 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7381 * ldloca inhibits many optimizations so try to get rid of it in common
7384 static inline unsigned char *
7385 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7395 local = read16 (ip + 2);
7399 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7400 /* From the INITOBJ case */
7401 token = read32 (ip + 2);
7402 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7403 CHECK_TYPELOAD (klass);
7404 type = mini_get_underlying_type (cfg, &klass->byval_arg);
7405 emit_init_local (cfg, local, type, TRUE);
7413 is_exception_class (MonoClass *class)
7416 if (class == mono_defaults.exception_class)
7418 class = class->parent;
7424 * is_jit_optimizer_disabled:
7426 * Determine whenever M's assembly has a DebuggableAttribute with the
7427 * IsJITOptimizerDisabled flag set.
7430 is_jit_optimizer_disabled (MonoMethod *m)
7432 MonoAssembly *ass = m->klass->image->assembly;
7433 MonoCustomAttrInfo* attrs;
7434 static MonoClass *klass;
7436 gboolean val = FALSE;
7439 if (ass->jit_optimizer_disabled_inited)
7440 return ass->jit_optimizer_disabled;
7443 klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggableAttribute");
7446 ass->jit_optimizer_disabled = FALSE;
7447 mono_memory_barrier ();
7448 ass->jit_optimizer_disabled_inited = TRUE;
7452 attrs = mono_custom_attrs_from_assembly (ass);
7454 for (i = 0; i < attrs->num_attrs; ++i) {
7455 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7457 MonoMethodSignature *sig;
7459 if (!attr->ctor || attr->ctor->klass != klass)
7461 /* Decode the attribute. See reflection.c */
7462 p = (const char*)attr->data;
7463 g_assert (read16 (p) == 0x0001);
7466 // FIXME: Support named parameters
7467 sig = mono_method_signature (attr->ctor);
7468 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7470 /* Two boolean arguments */
7474 mono_custom_attrs_free (attrs);
7477 ass->jit_optimizer_disabled = val;
7478 mono_memory_barrier ();
7479 ass->jit_optimizer_disabled_inited = TRUE;
7485 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7487 gboolean supported_tail_call;
7490 #ifdef MONO_ARCH_HAVE_OP_TAIL_CALL
7491 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7493 supported_tail_call = mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)) && !MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->ret);
7496 for (i = 0; i < fsig->param_count; ++i) {
7497 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7498 /* These can point to the current method's stack */
7499 supported_tail_call = FALSE;
7501 if (fsig->hasthis && cmethod->klass->valuetype)
7502 /* this might point to the current method's stack */
7503 supported_tail_call = FALSE;
7504 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7505 supported_tail_call = FALSE;
7506 if (cfg->method->save_lmf)
7507 supported_tail_call = FALSE;
7508 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7509 supported_tail_call = FALSE;
7510 if (call_opcode != CEE_CALL)
7511 supported_tail_call = FALSE;
7513 /* Debugging support */
7515 if (supported_tail_call) {
7516 if (!mono_debug_count ())
7517 supported_tail_call = FALSE;
7521 return supported_tail_call;
7524 /* the JIT intercepts ldflda instructions to the tlsdata field in ThreadLocal<T> and redirects
7525 * it to the thread local value based on the tls_offset field. Every other kind of access to
7526 * the field causes an assert.
7529 is_magic_tls_access (MonoClassField *field)
7531 if (strcmp (field->name, "tlsdata"))
7533 if (strcmp (field->parent->name, "ThreadLocal`1"))
7535 return field->parent->image == mono_defaults.corlib;
7538 /* emits the code needed to access a managed tls var (like ThreadStatic)
7539 * with the value of the tls offset in offset_reg. thread_ins represents the MonoInternalThread
7540 * pointer for the current thread.
7541 * Returns the MonoInst* representing the address of the tls var.
7544 emit_managed_static_data_access (MonoCompile *cfg, MonoInst *thread_ins, int offset_reg)
7547 int static_data_reg, array_reg, dreg;
7548 int offset2_reg, idx_reg;
7549 // inlined access to the tls data
7550 // idx = (offset >> 24) - 1;
7551 // return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
7552 static_data_reg = alloc_ireg (cfg);
7553 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
7554 idx_reg = alloc_ireg (cfg);
7555 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, idx_reg, offset_reg, 24);
7556 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISUB_IMM, idx_reg, idx_reg, 1);
7557 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
7558 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
7559 array_reg = alloc_ireg (cfg);
7560 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
7561 offset2_reg = alloc_ireg (cfg);
7562 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset_reg, 0xffffff);
7563 dreg = alloc_ireg (cfg);
7564 EMIT_NEW_BIALU (cfg, addr, OP_PADD, dreg, array_reg, offset2_reg);
7569 * redirect access to the tlsdata field to the tls var given by the tls_offset field.
7570 * this address is cached per-method in cached_tls_addr.
7573 create_magic_tls_access (MonoCompile *cfg, MonoClassField *tls_field, MonoInst **cached_tls_addr, MonoInst *thread_local)
7575 MonoInst *load, *addr, *temp, *store, *thread_ins;
7576 MonoClassField *offset_field;
7578 if (*cached_tls_addr) {
7579 EMIT_NEW_TEMPLOAD (cfg, addr, (*cached_tls_addr)->inst_c0);
7582 thread_ins = mono_get_thread_intrinsic (cfg);
7583 offset_field = mono_class_get_field_from_name (tls_field->parent, "tls_offset");
7585 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, offset_field->type, thread_local->dreg, offset_field->offset);
7587 MONO_ADD_INS (cfg->cbb, thread_ins);
7589 MonoMethod *thread_method;
7590 thread_method = mono_class_get_method_from_name (mono_get_thread_class(), "CurrentInternalThread_internal", 0);
7591 thread_ins = mono_emit_method_call (cfg, thread_method, NULL, NULL);
7593 addr = emit_managed_static_data_access (cfg, thread_ins, load->dreg);
7594 addr->klass = mono_class_from_mono_type (tls_field->type);
7595 addr->type = STACK_MP;
7596 *cached_tls_addr = temp = mono_compile_create_var (cfg, type_from_stack_type (addr), OP_LOCAL);
7597 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, addr);
7599 EMIT_NEW_TEMPLOAD (cfg, addr, temp->inst_c0);
7606 * Handle calls made to ctors from NEWOBJ opcodes.
7608 * REF_BBLOCK will point to the current bblock after the call.
7611 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7612 MonoInst **sp, guint8 *ip, MonoBasicBlock **ref_bblock, int *inline_costs)
7614 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7615 MonoBasicBlock *bblock = *ref_bblock;
7617 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7618 mono_method_is_generic_sharable (cmethod, TRUE)) {
7619 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7620 mono_class_vtable (cfg->domain, cmethod->klass);
7621 CHECK_TYPELOAD (cmethod->klass);
7623 vtable_arg = emit_get_rgctx_method (cfg, context_used,
7624 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7627 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
7628 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7630 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7632 CHECK_TYPELOAD (cmethod->klass);
7633 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7638 /* Avoid virtual calls to ctors if possible */
7639 if (mono_class_is_marshalbyref (cmethod->klass))
7640 callvirt_this_arg = sp [0];
7642 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7643 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7644 CHECK_CFG_EXCEPTION;
7645 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7646 mono_method_check_inlining (cfg, cmethod) &&
7647 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7650 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE, &bblock))) {
7651 cfg->real_offset += 5;
7653 *inline_costs += costs - 5;
7654 *ref_bblock = bblock;
7656 INLINE_FAILURE ("inline failure");
7657 // FIXME-VT: Clean this up
7658 if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig))
7659 GSHAREDVT_FAILURE(*ip);
7660 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7662 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig)) {
7665 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7666 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7667 } else if (context_used &&
7668 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7669 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7670 MonoInst *cmethod_addr;
7672 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7674 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7675 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7677 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7679 INLINE_FAILURE ("ctor call");
7680 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7681 callvirt_this_arg, NULL, vtable_arg);
7688 * mono_method_to_ir:
7690 * Translate the .net IL into linear IR.
7693 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
7694 MonoInst *return_var, MonoInst **inline_args,
7695 guint inline_offset, gboolean is_virtual_call)
7698 MonoInst *ins, **sp, **stack_start;
7699 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
7700 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7701 MonoMethod *cmethod, *method_definition;
7702 MonoInst **arg_array;
7703 MonoMethodHeader *header;
7705 guint32 token, ins_flag;
7707 MonoClass *constrained_class = NULL;
7708 unsigned char *ip, *end, *target, *err_pos;
7709 MonoMethodSignature *sig;
7710 MonoGenericContext *generic_context = NULL;
7711 MonoGenericContainer *generic_container = NULL;
7712 MonoType **param_types;
7713 int i, n, start_new_bblock, dreg;
7714 int num_calls = 0, inline_costs = 0;
7715 int breakpoint_id = 0;
7717 MonoBoolean security, pinvoke;
7718 MonoSecurityManager* secman = NULL;
7719 MonoDeclSecurityActions actions;
7720 GSList *class_inits = NULL;
7721 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7723 gboolean init_locals, seq_points, skip_dead_blocks;
7724 gboolean sym_seq_points = FALSE;
7725 MonoInst *cached_tls_addr = NULL;
7726 MonoDebugMethodInfo *minfo;
7727 MonoBitSet *seq_point_locs = NULL;
7728 MonoBitSet *seq_point_set_locs = NULL;
7730 cfg->disable_inline = is_jit_optimizer_disabled (method);
7732 /* serialization and xdomain stuff may need access to private fields and methods */
7733 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7734 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7735 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7736 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7737 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7738 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7740 dont_verify |= mono_security_smcs_hack_enabled ();
7742 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7743 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7744 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7745 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7746 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7748 image = method->klass->image;
7749 header = mono_method_get_header (method);
7751 MonoLoaderError *error;
7753 if ((error = mono_loader_get_last_error ())) {
7754 mono_cfg_set_exception (cfg, error->exception_type);
7756 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
7757 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
7759 goto exception_exit;
7761 generic_container = mono_method_get_generic_container (method);
7762 sig = mono_method_signature (method);
7763 num_args = sig->hasthis + sig->param_count;
7764 ip = (unsigned char*)header->code;
7765 cfg->cil_start = ip;
7766 end = ip + header->code_size;
7767 cfg->stat_cil_code_size += header->code_size;
7769 seq_points = cfg->gen_seq_points && cfg->method == method;
7770 #ifdef PLATFORM_ANDROID
7771 seq_points &= cfg->method->wrapper_type == MONO_WRAPPER_NONE;
7774 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7775 /* We could hit a seq point before attaching to the JIT (#8338) */
7779 if (cfg->gen_seq_points_debug_data && cfg->method == method) {
7780 minfo = mono_debug_lookup_method (method);
7782 int i, n_il_offsets;
7786 mono_debug_symfile_get_line_numbers_full (minfo, NULL, NULL, &n_il_offsets, &il_offsets, &line_numbers, NULL, NULL, NULL, NULL);
7787 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7788 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);
7789 sym_seq_points = TRUE;
7790 for (i = 0; i < n_il_offsets; ++i) {
7791 if (il_offsets [i] < header->code_size)
7792 mono_bitset_set_fast (seq_point_locs, il_offsets [i]);
7794 g_free (il_offsets);
7795 g_free (line_numbers);
7796 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7797 /* Methods without line number info like auto-generated property accessors */
7798 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7799 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);
7800 sym_seq_points = TRUE;
7805 * Methods without init_locals set could cause asserts in various passes
7806 * (#497220). To work around this, we emit dummy initialization opcodes
7807 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7808 * on some platforms.
7810 if ((cfg->opt & MONO_OPT_UNSAFE) && ARCH_HAVE_DUMMY_INIT)
7811 init_locals = header->init_locals;
7815 method_definition = method;
7816 while (method_definition->is_inflated) {
7817 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7818 method_definition = imethod->declaring;
7821 /* SkipVerification is not allowed if core-clr is enabled */
7822 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7824 dont_verify_stloc = TRUE;
7827 if (sig->is_inflated)
7828 generic_context = mono_method_get_context (method);
7829 else if (generic_container)
7830 generic_context = &generic_container->context;
7831 cfg->generic_context = generic_context;
7833 if (!cfg->generic_sharing_context)
7834 g_assert (!sig->has_type_parameters);
7836 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7837 g_assert (method->is_inflated);
7838 g_assert (mono_method_get_context (method)->method_inst);
7840 if (method->is_inflated && mono_method_get_context (method)->method_inst)
7841 g_assert (sig->generic_param_count);
7843 if (cfg->method == method) {
7844 cfg->real_offset = 0;
7846 cfg->real_offset = inline_offset;
7849 cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7850 cfg->cil_offset_to_bb_len = header->code_size;
7852 cfg->current_method = method;
7854 if (cfg->verbose_level > 2)
7855 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7857 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7859 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7860 for (n = 0; n < sig->param_count; ++n)
7861 param_types [n + sig->hasthis] = sig->params [n];
7862 cfg->arg_types = param_types;
7864 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7865 if (cfg->method == method) {
7867 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7868 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7871 NEW_BBLOCK (cfg, start_bblock);
7872 cfg->bb_entry = start_bblock;
7873 start_bblock->cil_code = NULL;
7874 start_bblock->cil_length = 0;
7875 #if defined(__native_client_codegen__)
7876 MONO_INST_NEW (cfg, ins, OP_NACL_GC_SAFE_POINT);
7877 ins->dreg = alloc_dreg (cfg, STACK_I4);
7878 MONO_ADD_INS (start_bblock, ins);
7882 NEW_BBLOCK (cfg, end_bblock);
7883 cfg->bb_exit = end_bblock;
7884 end_bblock->cil_code = NULL;
7885 end_bblock->cil_length = 0;
7886 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7887 g_assert (cfg->num_bblocks == 2);
7889 arg_array = cfg->args;
7891 if (header->num_clauses) {
7892 cfg->spvars = g_hash_table_new (NULL, NULL);
7893 cfg->exvars = g_hash_table_new (NULL, NULL);
7895 /* handle exception clauses */
7896 for (i = 0; i < header->num_clauses; ++i) {
7897 MonoBasicBlock *try_bb;
7898 MonoExceptionClause *clause = &header->clauses [i];
7899 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7900 try_bb->real_offset = clause->try_offset;
7901 try_bb->try_start = TRUE;
7902 try_bb->region = ((i + 1) << 8) | clause->flags;
7903 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7904 tblock->real_offset = clause->handler_offset;
7905 tblock->flags |= BB_EXCEPTION_HANDLER;
7908 * Linking the try block with the EH block hinders inlining as we won't be able to
7909 * merge the bblocks from inlining and produce an artificial hole for no good reason.
7911 if (COMPILE_LLVM (cfg))
7912 link_bblock (cfg, try_bb, tblock);
7914 if (*(ip + clause->handler_offset) == CEE_POP)
7915 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7917 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7918 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7919 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7920 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7921 MONO_ADD_INS (tblock, ins);
7923 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY) {
7924 /* finally clauses already have a seq point */
7925 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7926 MONO_ADD_INS (tblock, ins);
7929 /* todo: is a fault block unsafe to optimize? */
7930 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7931 tblock->flags |= BB_EXCEPTION_UNSAFE;
7935 /*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);
7937 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7939 /* catch and filter blocks get the exception object on the stack */
7940 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7941 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7942 MonoInst *dummy_use;
7944 /* mostly like handle_stack_args (), but just sets the input args */
7945 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7946 tblock->in_scount = 1;
7947 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7948 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7951 * Add a dummy use for the exvar so its liveness info will be
7955 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7957 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7958 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7959 tblock->flags |= BB_EXCEPTION_HANDLER;
7960 tblock->real_offset = clause->data.filter_offset;
7961 tblock->in_scount = 1;
7962 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7963 /* The filter block shares the exvar with the handler block */
7964 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7965 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7966 MONO_ADD_INS (tblock, ins);
7970 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7971 clause->data.catch_class &&
7972 cfg->generic_sharing_context &&
7973 mono_class_check_context_used (clause->data.catch_class)) {
7975 * In shared generic code with catch
7976 * clauses containing type variables
7977 * the exception handling code has to
7978 * be able to get to the rgctx.
7979 * Therefore we have to make sure that
7980 * the vtable/mrgctx argument (for
7981 * static or generic methods) or the
7982 * "this" argument (for non-static
7983 * methods) are live.
7985 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7986 mini_method_get_context (method)->method_inst ||
7987 method->klass->valuetype) {
7988 mono_get_vtable_var (cfg);
7990 MonoInst *dummy_use;
7992 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7997 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7998 cfg->cbb = start_bblock;
7999 cfg->args = arg_array;
8000 mono_save_args (cfg, sig, inline_args);
8003 /* FIRST CODE BLOCK */
8004 NEW_BBLOCK (cfg, bblock);
8005 bblock->cil_code = ip;
8009 ADD_BBLOCK (cfg, bblock);
8011 if (cfg->method == method) {
8012 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8013 if (breakpoint_id) {
8014 MONO_INST_NEW (cfg, ins, OP_BREAK);
8015 MONO_ADD_INS (bblock, ins);
8019 if (mono_security_cas_enabled ())
8020 secman = mono_security_manager_get_methods ();
8022 security = (secman && mono_security_method_has_declsec (method));
8023 /* at this point having security doesn't mean we have any code to generate */
8024 if (security && (cfg->method == method)) {
8025 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
8026 * And we do not want to enter the next section (with allocation) if we
8027 * have nothing to generate */
8028 security = mono_declsec_get_demands (method, &actions);
8031 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
8032 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
8034 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8035 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
8036 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
8038 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
8039 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
8043 mono_custom_attrs_free (custom);
8046 custom = mono_custom_attrs_from_class (wrapped->klass);
8047 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
8051 mono_custom_attrs_free (custom);
8054 /* not a P/Invoke after all */
8059 /* we use a separate basic block for the initialization code */
8060 NEW_BBLOCK (cfg, init_localsbb);
8061 cfg->bb_init = init_localsbb;
8062 init_localsbb->real_offset = cfg->real_offset;
8063 start_bblock->next_bb = init_localsbb;
8064 init_localsbb->next_bb = bblock;
8065 link_bblock (cfg, start_bblock, init_localsbb);
8066 link_bblock (cfg, init_localsbb, bblock);
8068 cfg->cbb = init_localsbb;
8070 if (cfg->gsharedvt && cfg->method == method) {
8071 MonoGSharedVtMethodInfo *info;
8072 MonoInst *var, *locals_var;
8075 info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8076 info->method = cfg->method;
8077 info->count_entries = 16;
8078 info->entries = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8079 cfg->gsharedvt_info = info;
8081 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8082 /* prevent it from being register allocated */
8083 //var->flags |= MONO_INST_VOLATILE;
8084 cfg->gsharedvt_info_var = var;
8086 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8087 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8089 /* Allocate locals */
8090 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8091 /* prevent it from being register allocated */
8092 //locals_var->flags |= MONO_INST_VOLATILE;
8093 cfg->gsharedvt_locals_var = locals_var;
8095 dreg = alloc_ireg (cfg);
8096 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8098 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8099 ins->dreg = locals_var->dreg;
8101 MONO_ADD_INS (cfg->cbb, ins);
8102 cfg->gsharedvt_locals_var_ins = ins;
8104 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8107 ins->flags |= MONO_INST_INIT;
8111 /* at this point we know, if security is TRUE, that some code needs to be generated */
8112 if (security && (cfg->method == method)) {
8115 cfg->stat_cas_demand_generation++;
8117 if (actions.demand.blob) {
8118 /* Add code for SecurityAction.Demand */
8119 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
8120 EMIT_NEW_ICONST (cfg, args [1], actions.demand.size);
8121 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
8122 mono_emit_method_call (cfg, secman->demand, args, NULL);
8124 if (actions.noncasdemand.blob) {
8125 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
8126 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
8127 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
8128 EMIT_NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
8129 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
8130 mono_emit_method_call (cfg, secman->demand, args, NULL);
8132 if (actions.demandchoice.blob) {
8133 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
8134 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
8135 EMIT_NEW_ICONST (cfg, args [1], actions.demandchoice.size);
8136 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
8137 mono_emit_method_call (cfg, secman->demandchoice, args, NULL);
8141 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
8143 mono_emit_method_call (cfg, secman->demandunmanaged, NULL, NULL);
8146 if (mono_security_core_clr_enabled ()) {
8147 /* check if this is native code, e.g. an icall or a p/invoke */
8148 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8149 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8151 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8152 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8154 /* if this ia a native call then it can only be JITted from platform code */
8155 if ((icall || pinvk) && method->klass && method->klass->image) {
8156 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8157 MonoException *ex = icall ? mono_get_exception_security () :
8158 mono_get_exception_method_access ();
8159 emit_throw_exception (cfg, ex);
8166 CHECK_CFG_EXCEPTION;
8168 if (header->code_size == 0)
8171 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8176 if (cfg->method == method)
8177 mono_debug_init_method (cfg, bblock, breakpoint_id);
8179 for (n = 0; n < header->num_locals; ++n) {
8180 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8185 /* We force the vtable variable here for all shared methods
8186 for the possibility that they might show up in a stack
8187 trace where their exact instantiation is needed. */
8188 if (cfg->generic_sharing_context && method == cfg->method) {
8189 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8190 mini_method_get_context (method)->method_inst ||
8191 method->klass->valuetype) {
8192 mono_get_vtable_var (cfg);
8194 /* FIXME: Is there a better way to do this?
8195 We need the variable live for the duration
8196 of the whole method. */
8197 cfg->args [0]->flags |= MONO_INST_VOLATILE;
8201 /* add a check for this != NULL to inlined methods */
8202 if (is_virtual_call) {
8205 NEW_ARGLOAD (cfg, arg_ins, 0);
8206 MONO_ADD_INS (cfg->cbb, arg_ins);
8207 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8210 skip_dead_blocks = !dont_verify;
8211 if (skip_dead_blocks) {
8212 original_bb = bb = mono_basic_block_split (method, &cfg->error);
8217 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8218 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8221 start_new_bblock = 0;
8224 if (cfg->method == method)
8225 cfg->real_offset = ip - header->code;
8227 cfg->real_offset = inline_offset;
8232 if (start_new_bblock) {
8233 bblock->cil_length = ip - bblock->cil_code;
8234 if (start_new_bblock == 2) {
8235 g_assert (ip == tblock->cil_code);
8237 GET_BBLOCK (cfg, tblock, ip);
8239 bblock->next_bb = tblock;
8242 start_new_bblock = 0;
8243 for (i = 0; i < bblock->in_scount; ++i) {
8244 if (cfg->verbose_level > 3)
8245 printf ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
8246 EMIT_NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
8250 g_slist_free (class_inits);
8253 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
8254 link_bblock (cfg, bblock, tblock);
8255 if (sp != stack_start) {
8256 handle_stack_args (cfg, stack_start, sp - stack_start);
8258 CHECK_UNVERIFIABLE (cfg);
8260 bblock->next_bb = tblock;
8263 for (i = 0; i < bblock->in_scount; ++i) {
8264 if (cfg->verbose_level > 3)
8265 printf ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
8266 EMIT_NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
8269 g_slist_free (class_inits);
8274 if (skip_dead_blocks) {
8275 int ip_offset = ip - header->code;
8277 if (ip_offset == bb->end)
8281 int op_size = mono_opcode_size (ip, end);
8282 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8284 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8286 if (ip_offset + op_size == bb->end) {
8287 MONO_INST_NEW (cfg, ins, OP_NOP);
8288 MONO_ADD_INS (bblock, ins);
8289 start_new_bblock = 1;
8297 * Sequence points are points where the debugger can place a breakpoint.
8298 * Currently, we generate these automatically at points where the IL
8301 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8303 * Make methods interruptable at the beginning, and at the targets of
8304 * backward branches.
8305 * Also, do this at the start of every bblock in methods with clauses too,
8306 * to be able to handle instructions with inprecise control flow like
8308 * Backward branches are handled at the end of method-to-ir ().
8310 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8312 /* Avoid sequence points on empty IL like .volatile */
8313 // FIXME: Enable this
8314 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8315 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8316 if (sp != stack_start)
8317 ins->flags |= MONO_INST_NONEMPTY_STACK;
8318 MONO_ADD_INS (cfg->cbb, ins);
8321 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8324 bblock->real_offset = cfg->real_offset;
8326 if ((cfg->method == method) && cfg->coverage_info) {
8327 guint32 cil_offset = ip - header->code;
8328 cfg->coverage_info->data [cil_offset].cil_code = ip;
8330 /* TODO: Use an increment here */
8331 #if defined(TARGET_X86)
8332 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8333 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8335 MONO_ADD_INS (cfg->cbb, ins);
8337 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8338 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8342 if (cfg->verbose_level > 3)
8343 printf ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8347 if (seq_points && !sym_seq_points && sp != stack_start) {
8349 * The C# compiler uses these nops to notify the JIT that it should
8350 * insert seq points.
8352 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8353 MONO_ADD_INS (cfg->cbb, ins);
8355 if (cfg->keep_cil_nops)
8356 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8358 MONO_INST_NEW (cfg, ins, OP_NOP);
8360 MONO_ADD_INS (bblock, ins);
8363 if (should_insert_brekpoint (cfg->method)) {
8364 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8366 MONO_INST_NEW (cfg, ins, OP_NOP);
8369 MONO_ADD_INS (bblock, ins);
8375 CHECK_STACK_OVF (1);
8376 n = (*ip)-CEE_LDARG_0;
8378 EMIT_NEW_ARGLOAD (cfg, ins, n);
8386 CHECK_STACK_OVF (1);
8387 n = (*ip)-CEE_LDLOC_0;
8389 EMIT_NEW_LOCLOAD (cfg, ins, n);
8398 n = (*ip)-CEE_STLOC_0;
8401 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8403 emit_stloc_ir (cfg, sp, header, n);
8410 CHECK_STACK_OVF (1);
8413 EMIT_NEW_ARGLOAD (cfg, ins, n);
8419 CHECK_STACK_OVF (1);
8422 NEW_ARGLOADA (cfg, ins, n);
8423 MONO_ADD_INS (cfg->cbb, ins);
8433 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8435 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8440 CHECK_STACK_OVF (1);
8443 EMIT_NEW_LOCLOAD (cfg, ins, n);
8447 case CEE_LDLOCA_S: {
8448 unsigned char *tmp_ip;
8450 CHECK_STACK_OVF (1);
8451 CHECK_LOCAL (ip [1]);
8453 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8459 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8468 CHECK_LOCAL (ip [1]);
8469 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8471 emit_stloc_ir (cfg, sp, header, ip [1]);
8476 CHECK_STACK_OVF (1);
8477 EMIT_NEW_PCONST (cfg, ins, NULL);
8478 ins->type = STACK_OBJ;
8483 CHECK_STACK_OVF (1);
8484 EMIT_NEW_ICONST (cfg, ins, -1);
8497 CHECK_STACK_OVF (1);
8498 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8504 CHECK_STACK_OVF (1);
8506 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8512 CHECK_STACK_OVF (1);
8513 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8519 CHECK_STACK_OVF (1);
8520 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8521 ins->type = STACK_I8;
8522 ins->dreg = alloc_dreg (cfg, STACK_I8);
8524 ins->inst_l = (gint64)read64 (ip);
8525 MONO_ADD_INS (bblock, ins);
8531 gboolean use_aotconst = FALSE;
8533 #ifdef TARGET_POWERPC
8534 /* FIXME: Clean this up */
8535 if (cfg->compile_aot)
8536 use_aotconst = TRUE;
8539 /* FIXME: we should really allocate this only late in the compilation process */
8540 f = mono_domain_alloc (cfg->domain, sizeof (float));
8542 CHECK_STACK_OVF (1);
8548 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8550 dreg = alloc_freg (cfg);
8551 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8552 ins->type = cfg->r4_stack_type;
8554 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8555 ins->type = cfg->r4_stack_type;
8556 ins->dreg = alloc_dreg (cfg, STACK_R8);
8558 MONO_ADD_INS (bblock, ins);
8568 gboolean use_aotconst = FALSE;
8570 #ifdef TARGET_POWERPC
8571 /* FIXME: Clean this up */
8572 if (cfg->compile_aot)
8573 use_aotconst = TRUE;
8576 /* FIXME: we should really allocate this only late in the compilation process */
8577 d = mono_domain_alloc (cfg->domain, sizeof (double));
8579 CHECK_STACK_OVF (1);
8585 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8587 dreg = alloc_freg (cfg);
8588 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8589 ins->type = STACK_R8;
8591 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8592 ins->type = STACK_R8;
8593 ins->dreg = alloc_dreg (cfg, STACK_R8);
8595 MONO_ADD_INS (bblock, ins);
8604 MonoInst *temp, *store;
8606 CHECK_STACK_OVF (1);
8610 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8611 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8613 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8616 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8629 if (sp [0]->type == STACK_R8)
8630 /* we need to pop the value from the x86 FP stack */
8631 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8637 INLINE_FAILURE ("jmp");
8638 GSHAREDVT_FAILURE (*ip);
8641 if (stack_start != sp)
8643 token = read32 (ip + 1);
8644 /* FIXME: check the signature matches */
8645 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8647 if (!cmethod || mono_loader_get_last_error ())
8650 if (cfg->generic_sharing_context && mono_method_check_context_used (cmethod))
8651 GENERIC_SHARING_FAILURE (CEE_JMP);
8653 if (mono_security_cas_enabled ())
8654 CHECK_CFG_EXCEPTION;
8656 emit_instrumentation_call (cfg, mono_profiler_method_leave);
8658 if (ARCH_HAVE_OP_TAIL_CALL) {
8659 MonoMethodSignature *fsig = mono_method_signature (cmethod);
8662 /* Handle tail calls similarly to calls */
8663 n = fsig->param_count + fsig->hasthis;
8667 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8668 call->method = cmethod;
8669 call->tail_call = TRUE;
8670 call->signature = mono_method_signature (cmethod);
8671 call->args = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8672 call->inst.inst_p0 = cmethod;
8673 for (i = 0; i < n; ++i)
8674 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8676 mono_arch_emit_call (cfg, call);
8677 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8678 MONO_ADD_INS (bblock, (MonoInst*)call);
8680 for (i = 0; i < num_args; ++i)
8681 /* Prevent arguments from being optimized away */
8682 arg_array [i]->flags |= MONO_INST_VOLATILE;
8684 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8685 ins = (MonoInst*)call;
8686 ins->inst_p0 = cmethod;
8687 MONO_ADD_INS (bblock, ins);
8691 start_new_bblock = 1;
8696 case CEE_CALLVIRT: {
8697 MonoInst *addr = NULL;
8698 MonoMethodSignature *fsig = NULL;
8700 int virtual = *ip == CEE_CALLVIRT;
8701 int calli = *ip == CEE_CALLI;
8702 gboolean pass_imt_from_rgctx = FALSE;
8703 MonoInst *imt_arg = NULL;
8704 MonoInst *keep_this_alive = NULL;
8705 gboolean pass_vtable = FALSE;
8706 gboolean pass_mrgctx = FALSE;
8707 MonoInst *vtable_arg = NULL;
8708 gboolean check_this = FALSE;
8709 gboolean supported_tail_call = FALSE;
8710 gboolean tail_call = FALSE;
8711 gboolean need_seq_point = FALSE;
8712 guint32 call_opcode = *ip;
8713 gboolean emit_widen = TRUE;
8714 gboolean push_res = TRUE;
8715 gboolean skip_ret = FALSE;
8716 gboolean delegate_invoke = FALSE;
8719 token = read32 (ip + 1);
8724 //GSHAREDVT_FAILURE (*ip);
8729 fsig = mini_get_signature (method, token, generic_context);
8730 n = fsig->param_count + fsig->hasthis;
8732 if (method->dynamic && fsig->pinvoke) {
8736 * This is a call through a function pointer using a pinvoke
8737 * signature. Have to create a wrapper and call that instead.
8738 * FIXME: This is very slow, need to create a wrapper at JIT time
8739 * instead based on the signature.
8741 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8742 EMIT_NEW_PCONST (cfg, args [1], fsig);
8744 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8747 MonoMethod *cil_method;
8749 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8750 cil_method = cmethod;
8752 if (constrained_class) {
8753 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8754 if (cfg->verbose_level > 2)
8755 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8756 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8757 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8758 cfg->generic_sharing_context)) {
8759 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8763 if (cfg->verbose_level > 2)
8764 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8766 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->generic_sharing_context) {
8768 * This is needed since get_method_constrained can't find
8769 * the method in klass representing a type var.
8770 * The type var is guaranteed to be a reference type in this
8773 if (!mini_is_gsharedvt_klass (cfg, constrained_class))
8774 g_assert (!cmethod->klass->valuetype);
8776 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8782 if (!cmethod || mono_loader_get_last_error ())
8784 if (!dont_verify && !cfg->skip_visibility) {
8785 MonoMethod *target_method = cil_method;
8786 if (method->is_inflated) {
8787 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
8789 if (!mono_method_can_access_method (method_definition, target_method) &&
8790 !mono_method_can_access_method (method, cil_method))
8791 METHOD_ACCESS_FAILURE (method, cil_method);
8794 if (mono_security_core_clr_enabled ())
8795 ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
8797 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8798 /* MS.NET seems to silently convert this to a callvirt */
8803 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8804 * converts to a callvirt.
8806 * tests/bug-515884.il is an example of this behavior
8808 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8809 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8810 if (!virtual && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8814 if (!cmethod->klass->inited)
8815 if (!mono_class_init (cmethod->klass))
8816 TYPE_LOAD_ERROR (cmethod->klass);
8818 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8819 mini_class_is_system_array (cmethod->klass)) {
8820 array_rank = cmethod->klass->rank;
8821 fsig = mono_method_signature (cmethod);
8823 fsig = mono_method_signature (cmethod);
8828 if (fsig->pinvoke) {
8829 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod,
8830 check_for_pending_exc, cfg->compile_aot);
8831 fsig = mono_method_signature (wrapper);
8832 } else if (constrained_class) {
8833 fsig = mono_method_signature (cmethod);
8835 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8840 mono_save_token_info (cfg, image, token, cil_method);
8842 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8843 need_seq_point = TRUE;
8845 n = fsig->param_count + fsig->hasthis;
8847 /* Don't support calls made using type arguments for now */
8849 if (cfg->gsharedvt) {
8850 if (mini_is_gsharedvt_signature (cfg, fsig))
8851 GSHAREDVT_FAILURE (*ip);
8855 if (mono_security_cas_enabled ()) {
8856 if (check_linkdemand (cfg, method, cmethod))
8857 INLINE_FAILURE ("linkdemand");
8858 CHECK_CFG_EXCEPTION;
8861 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8862 g_assert_not_reached ();
8865 if (!cfg->generic_sharing_context && cmethod && cmethod->klass->generic_container)
8868 if (!cfg->generic_sharing_context && cmethod)
8869 g_assert (!mono_method_check_context_used (cmethod));
8873 //g_assert (!virtual || fsig->hasthis);
8877 if (constrained_class) {
8878 if (mini_is_gsharedvt_klass (cfg, constrained_class)) {
8879 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8880 /* The 'Own method' case below */
8881 } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
8882 /* 'The type parameter is instantiated as a reference type' case below. */
8884 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen, &bblock);
8885 CHECK_CFG_EXCEPTION;
8892 * We have the `constrained.' prefix opcode.
8894 if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8896 * The type parameter is instantiated as a valuetype,
8897 * but that type doesn't override the method we're
8898 * calling, so we need to box `this'.
8900 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8901 ins->klass = constrained_class;
8902 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class), &bblock);
8903 CHECK_CFG_EXCEPTION;
8904 } else if (!constrained_class->valuetype) {
8905 int dreg = alloc_ireg_ref (cfg);
8908 * The type parameter is instantiated as a reference
8909 * type. We have a managed pointer on the stack, so
8910 * we need to dereference it here.
8912 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8913 ins->type = STACK_OBJ;
8916 if (cmethod->klass->valuetype) {
8919 /* Interface method */
8922 mono_class_setup_vtable (constrained_class);
8923 CHECK_TYPELOAD (constrained_class);
8924 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8926 TYPE_LOAD_ERROR (constrained_class);
8927 slot = mono_method_get_vtable_slot (cmethod);
8929 TYPE_LOAD_ERROR (cmethod->klass);
8930 cmethod = constrained_class->vtable [ioffset + slot];
8932 if (cmethod->klass == mono_defaults.enum_class) {
8933 /* Enum implements some interfaces, so treat this as the first case */
8934 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8935 ins->klass = constrained_class;
8936 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class), &bblock);
8937 CHECK_CFG_EXCEPTION;
8942 constrained_class = NULL;
8945 if (!calli && check_call_signature (cfg, fsig, sp))
8948 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
8949 if (cmethod && (cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8950 delegate_invoke = TRUE;
8953 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8955 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8956 type_to_eval_stack_type ((cfg), fsig->ret, ins);
8964 * If the callee is a shared method, then its static cctor
8965 * might not get called after the call was patched.
8967 if (cfg->generic_sharing_context && cmethod && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
8968 emit_generic_class_init (cfg, cmethod->klass);
8969 CHECK_TYPELOAD (cmethod->klass);
8973 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8975 if (cfg->generic_sharing_context && cmethod) {
8976 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8978 context_used = mini_method_check_context_used (cfg, cmethod);
8980 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
8981 /* Generic method interface
8982 calls are resolved via a
8983 helper function and don't
8985 if (!cmethod_context || !cmethod_context->method_inst)
8986 pass_imt_from_rgctx = TRUE;
8990 * If a shared method calls another
8991 * shared method then the caller must
8992 * have a generic sharing context
8993 * because the magic trampoline
8994 * requires it. FIXME: We shouldn't
8995 * have to force the vtable/mrgctx
8996 * variable here. Instead there
8997 * should be a flag in the cfg to
8998 * request a generic sharing context.
9001 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9002 mono_get_vtable_var (cfg);
9007 vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9009 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9011 CHECK_TYPELOAD (cmethod->klass);
9012 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9017 g_assert (!vtable_arg);
9019 if (!cfg->compile_aot) {
9021 * emit_get_rgctx_method () calls mono_class_vtable () so check
9022 * for type load errors before.
9024 mono_class_setup_vtable (cmethod->klass);
9025 CHECK_TYPELOAD (cmethod->klass);
9028 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9030 /* !marshalbyref is needed to properly handle generic methods + remoting */
9031 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9032 MONO_METHOD_IS_FINAL (cmethod)) &&
9033 !mono_class_is_marshalbyref (cmethod->klass)) {
9040 if (pass_imt_from_rgctx) {
9041 g_assert (!pass_vtable);
9044 imt_arg = emit_get_rgctx_method (cfg, context_used,
9045 cmethod, MONO_RGCTX_INFO_METHOD);
9049 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9051 /* Calling virtual generic methods */
9052 if (cmethod && virtual &&
9053 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9054 !(MONO_METHOD_IS_FINAL (cmethod) &&
9055 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9056 fsig->generic_param_count &&
9057 !(cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig))) {
9058 MonoInst *this_temp, *this_arg_temp, *store;
9059 MonoInst *iargs [4];
9060 gboolean use_imt = FALSE;
9062 g_assert (fsig->is_inflated);
9064 /* Prevent inlining of methods that contain indirect calls */
9065 INLINE_FAILURE ("virtual generic call");
9067 if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig))
9068 GSHAREDVT_FAILURE (*ip);
9070 #if MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK && defined(MONO_ARCH_GSHARED_SUPPORTED)
9071 if (cmethod->wrapper_type == MONO_WRAPPER_NONE && mono_use_imt)
9076 g_assert (!imt_arg);
9078 g_assert (cmethod->is_inflated);
9079 imt_arg = emit_get_rgctx_method (cfg, context_used,
9080 cmethod, MONO_RGCTX_INFO_METHOD);
9081 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9083 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9084 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9085 MONO_ADD_INS (bblock, store);
9087 /* FIXME: This should be a managed pointer */
9088 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9090 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9091 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9092 cmethod, MONO_RGCTX_INFO_METHOD);
9093 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9094 addr = mono_emit_jit_icall (cfg,
9095 mono_helper_compile_generic_method, iargs);
9097 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9099 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9106 * Implement a workaround for the inherent races involved in locking:
9112 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9113 * try block, the Exit () won't be executed, see:
9114 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9115 * To work around this, we extend such try blocks to include the last x bytes
9116 * of the Monitor.Enter () call.
9118 if (cmethod && cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9119 MonoBasicBlock *tbb;
9121 GET_BBLOCK (cfg, tbb, ip + 5);
9123 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9124 * from Monitor.Enter like ArgumentNullException.
9126 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9127 /* Mark this bblock as needing to be extended */
9128 tbb->extend_try_block = TRUE;
9132 /* Conversion to a JIT intrinsic */
9133 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9135 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9136 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9143 if (cmethod && (cfg->opt & MONO_OPT_INLINE) &&
9144 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9145 mono_method_check_inlining (cfg, cmethod)) {
9147 gboolean always = FALSE;
9149 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9150 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9151 /* Prevent inlining of methods that call wrappers */
9152 INLINE_FAILURE ("wrapper call");
9153 cmethod = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
9157 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always, &bblock);
9159 cfg->real_offset += 5;
9161 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9162 /* *sp is already set by inline_method */
9167 inline_costs += costs;
9173 /* Tail recursion elimination */
9174 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9175 gboolean has_vtargs = FALSE;
9178 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9179 INLINE_FAILURE ("tail call");
9181 /* keep it simple */
9182 for (i = fsig->param_count - 1; i >= 0; i--) {
9183 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9188 for (i = 0; i < n; ++i)
9189 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9190 MONO_INST_NEW (cfg, ins, OP_BR);
9191 MONO_ADD_INS (bblock, ins);
9192 tblock = start_bblock->out_bb [0];
9193 link_bblock (cfg, bblock, tblock);
9194 ins->inst_target_bb = tblock;
9195 start_new_bblock = 1;
9197 /* skip the CEE_RET, too */
9198 if (ip_in_bb (cfg, bblock, ip + 5))
9205 inline_costs += 10 * num_calls++;
9208 * Making generic calls out of gsharedvt methods.
9209 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9210 * patching gshared method addresses into a gsharedvt method.
9212 if (cmethod && cfg->gsharedvt && (mini_is_gsharedvt_signature (cfg, fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9213 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY)) {
9214 MonoRgctxInfoType info_type;
9217 //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9218 //GSHAREDVT_FAILURE (*ip);
9219 // disable for possible remoting calls
9220 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9221 GSHAREDVT_FAILURE (*ip);
9222 if (fsig->generic_param_count) {
9223 /* virtual generic call */
9224 g_assert (mono_use_imt);
9225 g_assert (!imt_arg);
9226 /* Same as the virtual generic case above */
9227 imt_arg = emit_get_rgctx_method (cfg, context_used,
9228 cmethod, MONO_RGCTX_INFO_METHOD);
9229 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9231 } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9232 /* This can happen when we call a fully instantiated iface method */
9233 imt_arg = emit_get_rgctx_method (cfg, context_used,
9234 cmethod, MONO_RGCTX_INFO_METHOD);
9239 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9240 keep_this_alive = sp [0];
9242 if (virtual && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9243 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9245 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9246 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9248 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9250 } else if (calli && cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig)) {
9252 * We pass the address to the gsharedvt trampoline in the rgctx reg
9254 MonoInst *callee = addr;
9256 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9258 GSHAREDVT_FAILURE (*ip);
9260 addr = emit_get_rgctx_sig (cfg, context_used,
9261 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9262 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9266 /* Generic sharing */
9269 * Use this if the callee is gsharedvt sharable too, since
9270 * at runtime we might find an instantiation so the call cannot
9271 * be patched (the 'no_patch' code path in mini-trampolines.c).
9273 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9274 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9275 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9276 (!virtual || MONO_METHOD_IS_FINAL (cmethod) ||
9277 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9278 INLINE_FAILURE ("gshared");
9280 g_assert (cfg->generic_sharing_context && cmethod);
9284 * We are compiling a call to a
9285 * generic method from shared code,
9286 * which means that we have to look up
9287 * the method in the rgctx and do an
9291 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9293 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9294 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9298 /* Indirect calls */
9300 if (call_opcode == CEE_CALL)
9301 g_assert (context_used);
9302 else if (call_opcode == CEE_CALLI)
9303 g_assert (!vtable_arg);
9305 /* FIXME: what the hell is this??? */
9306 g_assert (cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
9307 !(cmethod->flags & METHOD_ATTRIBUTE_FINAL));
9309 /* Prevent inlining of methods with indirect calls */
9310 INLINE_FAILURE ("indirect call");
9312 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9317 * Instead of emitting an indirect call, emit a direct call
9318 * with the contents of the aotconst as the patch info.
9320 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9321 info_type = addr->inst_c1;
9322 info_data = addr->inst_p0;
9324 info_type = addr->inst_right->inst_c1;
9325 info_data = addr->inst_right->inst_left;
9328 if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9329 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9334 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9342 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9343 MonoInst *val = sp [fsig->param_count];
9345 if (val->type == STACK_OBJ) {
9346 MonoInst *iargs [2];
9351 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9354 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9355 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9356 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9357 emit_write_barrier (cfg, addr, val);
9358 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cfg, cmethod->klass))
9359 GSHAREDVT_FAILURE (*ip);
9360 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9361 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9363 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9364 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9365 if (!cmethod->klass->element_class->valuetype && !readonly)
9366 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9367 CHECK_TYPELOAD (cmethod->klass);
9370 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9373 g_assert_not_reached ();
9380 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL);
9384 /* Tail prefix / tail call optimization */
9386 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9387 /* FIXME: runtime generic context pointer for jumps? */
9388 /* FIXME: handle this for generic sharing eventually */
9389 if (cmethod && (ins_flag & MONO_INST_TAILCALL) &&
9390 !vtable_arg && !cfg->generic_sharing_context && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9391 supported_tail_call = TRUE;
9393 if (supported_tail_call) {
9396 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9397 INLINE_FAILURE ("tail call");
9399 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9401 if (ARCH_HAVE_OP_TAIL_CALL) {
9402 /* Handle tail calls similarly to normal calls */
9405 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9407 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9408 call->tail_call = TRUE;
9409 call->method = cmethod;
9410 call->signature = mono_method_signature (cmethod);
9413 * We implement tail calls by storing the actual arguments into the
9414 * argument variables, then emitting a CEE_JMP.
9416 for (i = 0; i < n; ++i) {
9417 /* Prevent argument from being register allocated */
9418 arg_array [i]->flags |= MONO_INST_VOLATILE;
9419 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9421 ins = (MonoInst*)call;
9422 ins->inst_p0 = cmethod;
9423 ins->inst_p1 = arg_array [0];
9424 MONO_ADD_INS (bblock, ins);
9425 link_bblock (cfg, bblock, end_bblock);
9426 start_new_bblock = 1;
9428 // FIXME: Eliminate unreachable epilogs
9431 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9432 * only reachable from this call.
9434 GET_BBLOCK (cfg, tblock, ip + 5);
9435 if (tblock == bblock || tblock->in_count == 0)
9444 * Synchronized wrappers.
9445 * Its hard to determine where to replace a method with its synchronized
9446 * wrapper without causing an infinite recursion. The current solution is
9447 * to add the synchronized wrapper in the trampolines, and to
9448 * change the called method to a dummy wrapper, and resolve that wrapper
9449 * to the real method in mono_jit_compile_method ().
9451 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9452 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9453 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9454 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9458 INLINE_FAILURE ("call");
9459 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual ? sp [0] : NULL,
9460 imt_arg, vtable_arg);
9463 link_bblock (cfg, bblock, end_bblock);
9464 start_new_bblock = 1;
9466 // FIXME: Eliminate unreachable epilogs
9469 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9470 * only reachable from this call.
9472 GET_BBLOCK (cfg, tblock, ip + 5);
9473 if (tblock == bblock || tblock->in_count == 0)
9480 /* End of call, INS should contain the result of the call, if any */
9482 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9485 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9490 if (keep_this_alive) {
9491 MonoInst *dummy_use;
9493 /* See mono_emit_method_call_full () */
9494 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9497 CHECK_CFG_EXCEPTION;
9501 g_assert (*ip == CEE_RET);
9505 constrained_class = NULL;
9507 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9511 if (cfg->method != method) {
9512 /* return from inlined method */
9514 * If in_count == 0, that means the ret is unreachable due to
9515 * being preceeded by a throw. In that case, inline_method () will
9516 * handle setting the return value
9517 * (test case: test_0_inline_throw ()).
9519 if (return_var && cfg->cbb->in_count) {
9520 MonoType *ret_type = mono_method_signature (method)->ret;
9526 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9529 //g_assert (returnvar != -1);
9530 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9531 cfg->ret_var_set = TRUE;
9534 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9536 if (cfg->lmf_var && cfg->cbb->in_count)
9540 MonoType *ret_type = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
9542 if (seq_points && !sym_seq_points) {
9544 * Place a seq point here too even through the IL stack is not
9545 * empty, so a step over on
9548 * will work correctly.
9550 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9551 MONO_ADD_INS (cfg->cbb, ins);
9554 g_assert (!return_var);
9558 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9561 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
9564 if (!cfg->vret_addr) {
9567 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, (*sp));
9569 EMIT_NEW_RETLOADA (cfg, ret_addr);
9571 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, (*sp)->dreg);
9572 ins->klass = mono_class_from_mono_type (ret_type);
9575 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
9576 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
9577 MonoInst *iargs [1];
9581 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
9582 mono_arch_emit_setret (cfg, method, conv);
9584 mono_arch_emit_setret (cfg, method, *sp);
9587 mono_arch_emit_setret (cfg, method, *sp);
9592 if (sp != stack_start)
9594 MONO_INST_NEW (cfg, ins, OP_BR);
9596 ins->inst_target_bb = end_bblock;
9597 MONO_ADD_INS (bblock, ins);
9598 link_bblock (cfg, bblock, end_bblock);
9599 start_new_bblock = 1;
9603 MONO_INST_NEW (cfg, ins, OP_BR);
9605 target = ip + 1 + (signed char)(*ip);
9607 GET_BBLOCK (cfg, tblock, target);
9608 link_bblock (cfg, bblock, tblock);
9609 ins->inst_target_bb = tblock;
9610 if (sp != stack_start) {
9611 handle_stack_args (cfg, stack_start, sp - stack_start);
9613 CHECK_UNVERIFIABLE (cfg);
9615 MONO_ADD_INS (bblock, ins);
9616 start_new_bblock = 1;
9617 inline_costs += BRANCH_COST;
9631 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9633 target = ip + 1 + *(signed char*)ip;
9639 inline_costs += BRANCH_COST;
9643 MONO_INST_NEW (cfg, ins, OP_BR);
9646 target = ip + 4 + (gint32)read32(ip);
9648 GET_BBLOCK (cfg, tblock, target);
9649 link_bblock (cfg, bblock, tblock);
9650 ins->inst_target_bb = tblock;
9651 if (sp != stack_start) {
9652 handle_stack_args (cfg, stack_start, sp - stack_start);
9654 CHECK_UNVERIFIABLE (cfg);
9657 MONO_ADD_INS (bblock, ins);
9659 start_new_bblock = 1;
9660 inline_costs += BRANCH_COST;
9667 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9668 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9669 guint32 opsize = is_short ? 1 : 4;
9671 CHECK_OPSIZE (opsize);
9673 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9676 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9681 GET_BBLOCK (cfg, tblock, target);
9682 link_bblock (cfg, bblock, tblock);
9683 GET_BBLOCK (cfg, tblock, ip);
9684 link_bblock (cfg, bblock, tblock);
9686 if (sp != stack_start) {
9687 handle_stack_args (cfg, stack_start, sp - stack_start);
9688 CHECK_UNVERIFIABLE (cfg);
9691 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9692 cmp->sreg1 = sp [0]->dreg;
9693 type_from_op (cfg, cmp, sp [0], NULL);
9696 #if SIZEOF_REGISTER == 4
9697 if (cmp->opcode == OP_LCOMPARE_IMM) {
9698 /* Convert it to OP_LCOMPARE */
9699 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9700 ins->type = STACK_I8;
9701 ins->dreg = alloc_dreg (cfg, STACK_I8);
9703 MONO_ADD_INS (bblock, ins);
9704 cmp->opcode = OP_LCOMPARE;
9705 cmp->sreg2 = ins->dreg;
9708 MONO_ADD_INS (bblock, cmp);
9710 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9711 type_from_op (cfg, ins, sp [0], NULL);
9712 MONO_ADD_INS (bblock, ins);
9713 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9714 GET_BBLOCK (cfg, tblock, target);
9715 ins->inst_true_bb = tblock;
9716 GET_BBLOCK (cfg, tblock, ip);
9717 ins->inst_false_bb = tblock;
9718 start_new_bblock = 2;
9721 inline_costs += BRANCH_COST;
9736 MONO_INST_NEW (cfg, ins, *ip);
9738 target = ip + 4 + (gint32)read32(ip);
9744 inline_costs += BRANCH_COST;
9748 MonoBasicBlock **targets;
9749 MonoBasicBlock *default_bblock;
9750 MonoJumpInfoBBTable *table;
9751 int offset_reg = alloc_preg (cfg);
9752 int target_reg = alloc_preg (cfg);
9753 int table_reg = alloc_preg (cfg);
9754 int sum_reg = alloc_preg (cfg);
9755 gboolean use_op_switch;
9759 n = read32 (ip + 1);
9762 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
9766 CHECK_OPSIZE (n * sizeof (guint32));
9767 target = ip + n * sizeof (guint32);
9769 GET_BBLOCK (cfg, default_bblock, target);
9770 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9772 targets = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9773 for (i = 0; i < n; ++i) {
9774 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9775 targets [i] = tblock;
9776 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9780 if (sp != stack_start) {
9782 * Link the current bb with the targets as well, so handle_stack_args
9783 * will set their in_stack correctly.
9785 link_bblock (cfg, bblock, default_bblock);
9786 for (i = 0; i < n; ++i)
9787 link_bblock (cfg, bblock, targets [i]);
9789 handle_stack_args (cfg, stack_start, sp - stack_start);
9791 CHECK_UNVERIFIABLE (cfg);
9794 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9795 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9798 for (i = 0; i < n; ++i)
9799 link_bblock (cfg, bblock, targets [i]);
9801 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9802 table->table = targets;
9803 table->table_size = n;
9805 use_op_switch = FALSE;
9807 /* ARM implements SWITCH statements differently */
9808 /* FIXME: Make it use the generic implementation */
9809 if (!cfg->compile_aot)
9810 use_op_switch = TRUE;
9813 if (COMPILE_LLVM (cfg))
9814 use_op_switch = TRUE;
9816 cfg->cbb->has_jump_table = 1;
9818 if (use_op_switch) {
9819 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9820 ins->sreg1 = src1->dreg;
9821 ins->inst_p0 = table;
9822 ins->inst_many_bb = targets;
9823 ins->klass = GUINT_TO_POINTER (n);
9824 MONO_ADD_INS (cfg->cbb, ins);
9826 if (sizeof (gpointer) == 8)
9827 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9829 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9831 #if SIZEOF_REGISTER == 8
9832 /* The upper word might not be zero, and we add it to a 64 bit address later */
9833 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9836 if (cfg->compile_aot) {
9837 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9839 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9840 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9841 ins->inst_p0 = table;
9842 ins->dreg = table_reg;
9843 MONO_ADD_INS (cfg->cbb, ins);
9846 /* FIXME: Use load_memindex */
9847 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9848 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9849 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9851 start_new_bblock = 1;
9852 inline_costs += (BRANCH_COST * 2);
9872 dreg = alloc_freg (cfg);
9875 dreg = alloc_lreg (cfg);
9878 dreg = alloc_ireg_ref (cfg);
9881 dreg = alloc_preg (cfg);
9884 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
9885 ins->type = ldind_type [*ip - CEE_LDIND_I1];
9886 if (*ip == CEE_LDIND_R4)
9887 ins->type = cfg->r4_stack_type;
9888 ins->flags |= ins_flag;
9889 MONO_ADD_INS (bblock, ins);
9891 if (ins_flag & MONO_INST_VOLATILE) {
9892 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9893 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9909 if (ins_flag & MONO_INST_VOLATILE) {
9910 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9911 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9914 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9915 ins->flags |= ins_flag;
9918 MONO_ADD_INS (bblock, ins);
9920 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [1]->opcode == OP_PCONST) && (sp [1]->inst_p0 == 0)))
9921 emit_write_barrier (cfg, sp [0], sp [1]);
9930 MONO_INST_NEW (cfg, ins, (*ip));
9932 ins->sreg1 = sp [0]->dreg;
9933 ins->sreg2 = sp [1]->dreg;
9934 type_from_op (cfg, ins, sp [0], sp [1]);
9936 ins->dreg = alloc_dreg ((cfg), (ins)->type);
9938 /* Use the immediate opcodes if possible */
9939 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9940 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9941 if (imm_opcode != -1) {
9942 ins->opcode = imm_opcode;
9943 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9946 NULLIFY_INS (sp [1]);
9950 MONO_ADD_INS ((cfg)->cbb, (ins));
9952 *sp++ = mono_decompose_opcode (cfg, ins, &bblock);
9969 MONO_INST_NEW (cfg, ins, (*ip));
9971 ins->sreg1 = sp [0]->dreg;
9972 ins->sreg2 = sp [1]->dreg;
9973 type_from_op (cfg, ins, sp [0], sp [1]);
9975 add_widen_op (cfg, ins, &sp [0], &sp [1]);
9976 ins->dreg = alloc_dreg ((cfg), (ins)->type);
9978 /* FIXME: Pass opcode to is_inst_imm */
9980 /* Use the immediate opcodes if possible */
9981 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)) {
9984 imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9985 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
9986 /* Keep emulated opcodes which are optimized away later */
9987 if ((ins->opcode == OP_IREM_UN || ins->opcode == OP_IDIV_UN_IMM) && (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP)) && sp [1]->opcode == OP_ICONST && mono_is_power_of_two (sp [1]->inst_c0) >= 0) {
9988 imm_opcode = mono_op_to_op_imm (ins->opcode);
9991 if (imm_opcode != -1) {
9992 ins->opcode = imm_opcode;
9993 if (sp [1]->opcode == OP_I8CONST) {
9994 #if SIZEOF_REGISTER == 8
9995 ins->inst_imm = sp [1]->inst_l;
9997 ins->inst_ls_word = sp [1]->inst_ls_word;
9998 ins->inst_ms_word = sp [1]->inst_ms_word;
10002 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10005 /* Might be followed by an instruction added by add_widen_op */
10006 if (sp [1]->next == NULL)
10007 NULLIFY_INS (sp [1]);
10010 MONO_ADD_INS ((cfg)->cbb, (ins));
10012 *sp++ = mono_decompose_opcode (cfg, ins, &bblock);
10025 case CEE_CONV_OVF_I8:
10026 case CEE_CONV_OVF_U8:
10027 case CEE_CONV_R_UN:
10030 /* Special case this earlier so we have long constants in the IR */
10031 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10032 int data = sp [-1]->inst_c0;
10033 sp [-1]->opcode = OP_I8CONST;
10034 sp [-1]->type = STACK_I8;
10035 #if SIZEOF_REGISTER == 8
10036 if ((*ip) == CEE_CONV_U8)
10037 sp [-1]->inst_c0 = (guint32)data;
10039 sp [-1]->inst_c0 = data;
10041 sp [-1]->inst_ls_word = data;
10042 if ((*ip) == CEE_CONV_U8)
10043 sp [-1]->inst_ms_word = 0;
10045 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10047 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10054 case CEE_CONV_OVF_I4:
10055 case CEE_CONV_OVF_I1:
10056 case CEE_CONV_OVF_I2:
10057 case CEE_CONV_OVF_I:
10058 case CEE_CONV_OVF_U:
10061 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10062 ADD_UNOP (CEE_CONV_OVF_I8);
10069 case CEE_CONV_OVF_U1:
10070 case CEE_CONV_OVF_U2:
10071 case CEE_CONV_OVF_U4:
10074 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10075 ADD_UNOP (CEE_CONV_OVF_U8);
10082 case CEE_CONV_OVF_I1_UN:
10083 case CEE_CONV_OVF_I2_UN:
10084 case CEE_CONV_OVF_I4_UN:
10085 case CEE_CONV_OVF_I8_UN:
10086 case CEE_CONV_OVF_U1_UN:
10087 case CEE_CONV_OVF_U2_UN:
10088 case CEE_CONV_OVF_U4_UN:
10089 case CEE_CONV_OVF_U8_UN:
10090 case CEE_CONV_OVF_I_UN:
10091 case CEE_CONV_OVF_U_UN:
10098 CHECK_CFG_EXCEPTION;
10102 case CEE_ADD_OVF_UN:
10104 case CEE_MUL_OVF_UN:
10106 case CEE_SUB_OVF_UN:
10112 GSHAREDVT_FAILURE (*ip);
10115 token = read32 (ip + 1);
10116 klass = mini_get_class (method, token, generic_context);
10117 CHECK_TYPELOAD (klass);
10119 if (generic_class_is_reference_type (cfg, klass)) {
10120 MonoInst *store, *load;
10121 int dreg = alloc_ireg_ref (cfg);
10123 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10124 load->flags |= ins_flag;
10125 MONO_ADD_INS (cfg->cbb, load);
10127 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10128 store->flags |= ins_flag;
10129 MONO_ADD_INS (cfg->cbb, store);
10131 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10132 emit_write_barrier (cfg, sp [0], sp [1]);
10134 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10140 int loc_index = -1;
10146 token = read32 (ip + 1);
10147 klass = mini_get_class (method, token, generic_context);
10148 CHECK_TYPELOAD (klass);
10150 /* Optimize the common ldobj+stloc combination */
10153 loc_index = ip [6];
10160 loc_index = ip [5] - CEE_STLOC_0;
10167 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
10168 CHECK_LOCAL (loc_index);
10170 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10171 ins->dreg = cfg->locals [loc_index]->dreg;
10172 ins->flags |= ins_flag;
10175 if (ins_flag & MONO_INST_VOLATILE) {
10176 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10177 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10183 /* Optimize the ldobj+stobj combination */
10184 /* The reference case ends up being a load+store anyway */
10185 /* Skip this if the operation is volatile. */
10186 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, bblock, ip + 5) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass) && !(ins_flag & MONO_INST_VOLATILE)) {
10191 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10198 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10199 ins->flags |= ins_flag;
10202 if (ins_flag & MONO_INST_VOLATILE) {
10203 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10204 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10213 CHECK_STACK_OVF (1);
10215 n = read32 (ip + 1);
10217 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10218 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10219 ins->type = STACK_OBJ;
10222 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10223 MonoInst *iargs [1];
10224 char *str = mono_method_get_wrapper_data (method, n);
10226 if (cfg->compile_aot)
10227 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10229 EMIT_NEW_PCONST (cfg, iargs [0], str);
10230 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10232 if (cfg->opt & MONO_OPT_SHARED) {
10233 MonoInst *iargs [3];
10235 if (cfg->compile_aot) {
10236 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10238 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10239 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10240 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10241 *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
10242 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10244 if (bblock->out_of_line) {
10245 MonoInst *iargs [2];
10247 if (image == mono_defaults.corlib) {
10249 * Avoid relocations in AOT and save some space by using a
10250 * version of helper_ldstr specialized to mscorlib.
10252 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10253 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10255 /* Avoid creating the string object */
10256 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10257 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10258 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10262 if (cfg->compile_aot) {
10263 NEW_LDSTRCONST (cfg, ins, image, n);
10265 MONO_ADD_INS (bblock, ins);
10268 NEW_PCONST (cfg, ins, NULL);
10269 ins->type = STACK_OBJ;
10270 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10272 OUT_OF_MEMORY_FAILURE;
10275 MONO_ADD_INS (bblock, ins);
10284 MonoInst *iargs [2];
10285 MonoMethodSignature *fsig;
10288 MonoInst *vtable_arg = NULL;
10291 token = read32 (ip + 1);
10292 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10293 if (!cmethod || mono_loader_get_last_error ())
10295 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10298 mono_save_token_info (cfg, image, token, cmethod);
10300 if (!mono_class_init (cmethod->klass))
10301 TYPE_LOAD_ERROR (cmethod->klass);
10303 context_used = mini_method_check_context_used (cfg, cmethod);
10305 if (mono_security_cas_enabled ()) {
10306 if (check_linkdemand (cfg, method, cmethod))
10307 INLINE_FAILURE ("linkdemand");
10308 CHECK_CFG_EXCEPTION;
10309 } else if (mono_security_core_clr_enabled ()) {
10310 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
10313 if (cfg->generic_sharing_context && cmethod && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
10314 emit_generic_class_init (cfg, cmethod->klass);
10315 CHECK_TYPELOAD (cmethod->klass);
10319 if (cfg->gsharedvt) {
10320 if (mini_is_gsharedvt_variable_signature (sig))
10321 GSHAREDVT_FAILURE (*ip);
10325 n = fsig->param_count;
10329 * Generate smaller code for the common newobj <exception> instruction in
10330 * argument checking code.
10332 if (bblock->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10333 is_exception_class (cmethod->klass) && n <= 2 &&
10334 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10335 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10336 MonoInst *iargs [3];
10340 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10343 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10346 iargs [1] = sp [0];
10347 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10350 iargs [1] = sp [0];
10351 iargs [2] = sp [1];
10352 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10355 g_assert_not_reached ();
10363 /* move the args to allow room for 'this' in the first position */
10369 /* check_call_signature () requires sp[0] to be set */
10370 this_ins.type = STACK_OBJ;
10371 sp [0] = &this_ins;
10372 if (check_call_signature (cfg, fsig, sp))
10377 if (mini_class_is_system_array (cmethod->klass)) {
10378 *sp = emit_get_rgctx_method (cfg, context_used,
10379 cmethod, MONO_RGCTX_INFO_METHOD);
10381 /* Avoid varargs in the common case */
10382 if (fsig->param_count == 1)
10383 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10384 else if (fsig->param_count == 2)
10385 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10386 else if (fsig->param_count == 3)
10387 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10388 else if (fsig->param_count == 4)
10389 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10391 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10392 } else if (cmethod->string_ctor) {
10393 g_assert (!context_used);
10394 g_assert (!vtable_arg);
10395 /* we simply pass a null pointer */
10396 EMIT_NEW_PCONST (cfg, *sp, NULL);
10397 /* now call the string ctor */
10398 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10400 if (cmethod->klass->valuetype) {
10401 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10402 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10403 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10408 * The code generated by mini_emit_virtual_call () expects
10409 * iargs [0] to be a boxed instance, but luckily the vcall
10410 * will be transformed into a normal call there.
10412 } else if (context_used) {
10413 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10416 MonoVTable *vtable = NULL;
10418 if (!cfg->compile_aot)
10419 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10420 CHECK_TYPELOAD (cmethod->klass);
10423 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10424 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10425 * As a workaround, we call class cctors before allocating objects.
10427 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10428 mono_emit_abs_call (cfg, MONO_PATCH_INFO_CLASS_INIT, cmethod->klass, helper_sig_class_init_trampoline, NULL);
10429 if (cfg->verbose_level > 2)
10430 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10431 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10434 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10437 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10440 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10442 /* Now call the actual ctor */
10443 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &bblock, &inline_costs);
10444 CHECK_CFG_EXCEPTION;
10447 if (alloc == NULL) {
10449 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10450 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10458 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10459 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10462 case CEE_CASTCLASS:
10466 token = read32 (ip + 1);
10467 klass = mini_get_class (method, token, generic_context);
10468 CHECK_TYPELOAD (klass);
10469 if (sp [0]->type != STACK_OBJ)
10472 ins = handle_castclass (cfg, klass, *sp, ip, &bblock, &inline_costs);
10473 CHECK_CFG_EXCEPTION;
10482 token = read32 (ip + 1);
10483 klass = mini_get_class (method, token, generic_context);
10484 CHECK_TYPELOAD (klass);
10485 if (sp [0]->type != STACK_OBJ)
10488 context_used = mini_class_check_context_used (cfg, klass);
10490 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10491 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10492 MonoInst *args [3];
10498 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10501 if (cfg->compile_aot)
10502 EMIT_NEW_AOTCONST (cfg, args [2], MONO_PATCH_INFO_CASTCLASS_CACHE, NULL);
10504 EMIT_NEW_PCONST (cfg, args [2], mono_domain_alloc0 (cfg->domain, sizeof (gpointer)));
10506 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
10509 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
10510 MonoMethod *mono_isinst;
10511 MonoInst *iargs [1];
10514 mono_isinst = mono_marshal_get_isinst (klass);
10515 iargs [0] = sp [0];
10517 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst),
10518 iargs, ip, cfg->real_offset, TRUE, &bblock);
10519 CHECK_CFG_EXCEPTION;
10520 g_assert (costs > 0);
10523 cfg->real_offset += 5;
10527 inline_costs += costs;
10530 ins = handle_isinst (cfg, klass, *sp, context_used);
10531 CHECK_CFG_EXCEPTION;
10538 case CEE_UNBOX_ANY: {
10539 MonoInst *res, *addr;
10544 token = read32 (ip + 1);
10545 klass = mini_get_class (method, token, generic_context);
10546 CHECK_TYPELOAD (klass);
10548 mono_save_token_info (cfg, image, token, klass);
10550 context_used = mini_class_check_context_used (cfg, klass);
10552 if (mini_is_gsharedvt_klass (cfg, klass)) {
10553 res = handle_unbox_gsharedvt (cfg, klass, *sp, &bblock);
10555 } else if (generic_class_is_reference_type (cfg, klass)) {
10556 res = handle_castclass (cfg, klass, *sp, ip, &bblock, &inline_costs);
10557 CHECK_CFG_EXCEPTION;
10558 } else if (mono_class_is_nullable (klass)) {
10559 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10561 addr = handle_unbox (cfg, klass, sp, context_used);
10563 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10574 MonoClass *enum_class;
10575 MonoMethod *has_flag;
10581 token = read32 (ip + 1);
10582 klass = mini_get_class (method, token, generic_context);
10583 CHECK_TYPELOAD (klass);
10585 mono_save_token_info (cfg, image, token, klass);
10587 context_used = mini_class_check_context_used (cfg, klass);
10589 if (generic_class_is_reference_type (cfg, klass)) {
10595 if (klass == mono_defaults.void_class)
10597 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10599 /* frequent check in generic code: box (struct), brtrue */
10604 * <push int/long ptr>
10607 * constrained. MyFlags
10608 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10610 * If we find this sequence and the operand types on box and constrained
10611 * are equal, we can emit a specialized instruction sequence instead of
10612 * the very slow HasFlag () call.
10614 if ((cfg->opt & MONO_OPT_INTRINS) &&
10615 /* Cheap checks first. */
10616 ip + 5 + 6 + 5 < end &&
10617 ip [5] == CEE_PREFIX1 &&
10618 ip [6] == CEE_CONSTRAINED_ &&
10619 ip [11] == CEE_CALLVIRT &&
10620 ip_in_bb (cfg, bblock, ip + 5 + 6 + 5) &&
10621 mono_class_is_enum (klass) &&
10622 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10623 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10624 has_flag->klass == mono_defaults.enum_class &&
10625 !strcmp (has_flag->name, "HasFlag") &&
10626 has_flag->signature->hasthis &&
10627 has_flag->signature->param_count == 1) {
10628 CHECK_TYPELOAD (enum_class);
10630 if (enum_class == klass) {
10631 MonoInst *enum_this, *enum_flag;
10636 enum_this = sp [0];
10637 enum_flag = sp [1];
10639 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10644 // FIXME: LLVM can't handle the inconsistent bb linking
10645 if (!mono_class_is_nullable (klass) &&
10646 ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) &&
10647 (ip [5] == CEE_BRTRUE ||
10648 ip [5] == CEE_BRTRUE_S ||
10649 ip [5] == CEE_BRFALSE ||
10650 ip [5] == CEE_BRFALSE_S)) {
10651 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10653 MonoBasicBlock *true_bb, *false_bb;
10657 if (cfg->verbose_level > 3) {
10658 printf ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10659 printf ("<box+brtrue opt>\n");
10664 case CEE_BRFALSE_S:
10667 target = ip + 1 + (signed char)(*ip);
10674 target = ip + 4 + (gint)(read32 (ip));
10678 g_assert_not_reached ();
10682 * We need to link both bblocks, since it is needed for handling stack
10683 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10684 * Branching to only one of them would lead to inconsistencies, so
10685 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10687 GET_BBLOCK (cfg, true_bb, target);
10688 GET_BBLOCK (cfg, false_bb, ip);
10690 mono_link_bblock (cfg, cfg->cbb, true_bb);
10691 mono_link_bblock (cfg, cfg->cbb, false_bb);
10693 if (sp != stack_start) {
10694 handle_stack_args (cfg, stack_start, sp - stack_start);
10696 CHECK_UNVERIFIABLE (cfg);
10699 if (COMPILE_LLVM (cfg)) {
10700 dreg = alloc_ireg (cfg);
10701 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10702 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10704 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10706 /* The JIT can't eliminate the iconst+compare */
10707 MONO_INST_NEW (cfg, ins, OP_BR);
10708 ins->inst_target_bb = is_true ? true_bb : false_bb;
10709 MONO_ADD_INS (cfg->cbb, ins);
10712 start_new_bblock = 1;
10716 *sp++ = handle_box (cfg, val, klass, context_used, &bblock);
10718 CHECK_CFG_EXCEPTION;
10727 token = read32 (ip + 1);
10728 klass = mini_get_class (method, token, generic_context);
10729 CHECK_TYPELOAD (klass);
10731 mono_save_token_info (cfg, image, token, klass);
10733 context_used = mini_class_check_context_used (cfg, klass);
10735 if (mono_class_is_nullable (klass)) {
10738 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10739 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10743 ins = handle_unbox (cfg, klass, sp, context_used);
10756 MonoClassField *field;
10757 #ifndef DISABLE_REMOTING
10761 gboolean is_instance;
10763 gpointer addr = NULL;
10764 gboolean is_special_static;
10766 MonoInst *store_val = NULL;
10767 MonoInst *thread_ins;
10770 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10772 if (op == CEE_STFLD) {
10775 store_val = sp [1];
10780 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10782 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10785 if (op == CEE_STSFLD) {
10788 store_val = sp [0];
10793 token = read32 (ip + 1);
10794 if (method->wrapper_type != MONO_WRAPPER_NONE) {
10795 field = mono_method_get_wrapper_data (method, token);
10796 klass = field->parent;
10799 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10802 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10803 FIELD_ACCESS_FAILURE (method, field);
10804 mono_class_init (klass);
10806 if (is_instance && *ip != CEE_LDFLDA && is_magic_tls_access (field))
10809 /* if the class is Critical then transparent code cannot access it's fields */
10810 if (!is_instance && mono_security_core_clr_enabled ())
10811 ensure_method_is_allowed_to_access_field (cfg, method, field, bblock, ip);
10813 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10814 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
10815 if (mono_security_core_clr_enabled ())
10816 ensure_method_is_allowed_to_access_field (cfg, method, field, bblock, ip);
10820 * LDFLD etc. is usable on static fields as well, so convert those cases to
10823 if (is_instance && field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
10835 g_assert_not_reached ();
10837 is_instance = FALSE;
10840 context_used = mini_class_check_context_used (cfg, klass);
10842 /* INSTANCE CASE */
10844 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10845 if (op == CEE_STFLD) {
10846 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10848 #ifndef DISABLE_REMOTING
10849 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10850 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
10851 MonoInst *iargs [5];
10853 GSHAREDVT_FAILURE (op);
10855 iargs [0] = sp [0];
10856 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10857 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10858 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
10860 iargs [4] = sp [1];
10862 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10863 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
10864 iargs, ip, cfg->real_offset, TRUE, &bblock);
10865 CHECK_CFG_EXCEPTION;
10866 g_assert (costs > 0);
10868 cfg->real_offset += 5;
10870 inline_costs += costs;
10872 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10879 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10881 if (mini_is_gsharedvt_klass (cfg, klass)) {
10882 MonoInst *offset_ins;
10884 context_used = mini_class_check_context_used (cfg, klass);
10886 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10887 dreg = alloc_ireg_mp (cfg);
10888 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10889 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
10890 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10892 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10894 if (sp [0]->opcode != OP_LDADDR)
10895 store->flags |= MONO_INST_FAULT;
10897 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
10898 /* insert call to write barrier */
10902 dreg = alloc_ireg_mp (cfg);
10903 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10904 emit_write_barrier (cfg, ptr, sp [1]);
10907 store->flags |= ins_flag;
10914 #ifndef DISABLE_REMOTING
10915 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10916 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
10917 MonoInst *iargs [4];
10919 GSHAREDVT_FAILURE (op);
10921 iargs [0] = sp [0];
10922 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10923 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10924 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10925 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10926 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
10927 iargs, ip, cfg->real_offset, TRUE, &bblock);
10928 CHECK_CFG_EXCEPTION;
10929 g_assert (costs > 0);
10931 cfg->real_offset += 5;
10935 inline_costs += costs;
10937 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10943 if (sp [0]->type == STACK_VTYPE) {
10946 /* Have to compute the address of the variable */
10948 var = get_vreg_to_inst (cfg, sp [0]->dreg);
10950 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10952 g_assert (var->klass == klass);
10954 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10958 if (op == CEE_LDFLDA) {
10959 if (is_magic_tls_access (field)) {
10960 GSHAREDVT_FAILURE (*ip);
10962 *sp++ = create_magic_tls_access (cfg, field, &cached_tls_addr, ins);
10964 if (sp [0]->type == STACK_OBJ) {
10965 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10966 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10969 dreg = alloc_ireg_mp (cfg);
10971 if (mini_is_gsharedvt_klass (cfg, klass)) {
10972 MonoInst *offset_ins;
10974 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10975 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10977 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10979 ins->klass = mono_class_from_mono_type (field->type);
10980 ins->type = STACK_MP;
10986 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10988 if (mini_is_gsharedvt_klass (cfg, klass)) {
10989 MonoInst *offset_ins;
10991 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10992 dreg = alloc_ireg_mp (cfg);
10993 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10994 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
10996 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
10998 load->flags |= ins_flag;
10999 if (sp [0]->opcode != OP_LDADDR)
11000 load->flags |= MONO_INST_FAULT;
11014 * We can only support shared generic static
11015 * field access on architectures where the
11016 * trampoline code has been extended to handle
11017 * the generic class init.
11019 #ifndef MONO_ARCH_VTABLE_REG
11020 GENERIC_SHARING_FAILURE (op);
11023 context_used = mini_class_check_context_used (cfg, klass);
11025 ftype = mono_field_get_type (field);
11027 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL)
11030 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11031 * to be called here.
11033 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11034 mono_class_vtable (cfg->domain, klass);
11035 CHECK_TYPELOAD (klass);
11037 mono_domain_lock (cfg->domain);
11038 if (cfg->domain->special_static_fields)
11039 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11040 mono_domain_unlock (cfg->domain);
11042 is_special_static = mono_class_field_is_special_static (field);
11044 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11045 thread_ins = mono_get_thread_intrinsic (cfg);
11049 /* Generate IR to compute the field address */
11050 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11052 * Fast access to TLS data
11053 * Inline version of get_thread_static_data () in
11057 int idx, static_data_reg, array_reg, dreg;
11059 GSHAREDVT_FAILURE (op);
11061 // offset &= 0x7fffffff;
11062 // idx = (offset >> 24) - 1;
11063 // return ((char*) thread->static_data [idx]) + (offset & 0xffffff);
11064 MONO_ADD_INS (cfg->cbb, thread_ins);
11065 static_data_reg = alloc_ireg (cfg);
11066 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11068 if (cfg->compile_aot) {
11069 int offset_reg, offset2_reg, idx_reg;
11071 /* For TLS variables, this will return the TLS offset */
11072 EMIT_NEW_SFLDACONST (cfg, ins, field);
11073 offset_reg = ins->dreg;
11074 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11075 idx_reg = alloc_ireg (cfg);
11076 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, idx_reg, offset_reg, 24);
11077 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISUB_IMM, idx_reg, idx_reg, 1);
11078 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11079 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11080 array_reg = alloc_ireg (cfg);
11081 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11082 offset2_reg = alloc_ireg (cfg);
11083 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset_reg, 0xffffff);
11084 dreg = alloc_ireg (cfg);
11085 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11087 offset = (gsize)addr & 0x7fffffff;
11088 idx = (offset >> 24) - 1;
11090 array_reg = alloc_ireg (cfg);
11091 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11092 dreg = alloc_ireg (cfg);
11093 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, (offset & 0xffffff));
11095 } else if ((cfg->opt & MONO_OPT_SHARED) ||
11096 (cfg->compile_aot && is_special_static) ||
11097 (context_used && is_special_static)) {
11098 MonoInst *iargs [2];
11100 g_assert (field->parent);
11101 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11102 if (context_used) {
11103 iargs [1] = emit_get_rgctx_field (cfg, context_used,
11104 field, MONO_RGCTX_INFO_CLASS_FIELD);
11106 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11108 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11109 } else if (context_used) {
11110 MonoInst *static_data;
11113 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11114 method->klass->name_space, method->klass->name, method->name,
11115 depth, field->offset);
11118 if (mono_class_needs_cctor_run (klass, method))
11119 emit_generic_class_init (cfg, klass);
11122 * The pointer we're computing here is
11124 * super_info.static_data + field->offset
11126 static_data = emit_get_rgctx_klass (cfg, context_used,
11127 klass, MONO_RGCTX_INFO_STATIC_DATA);
11129 if (mini_is_gsharedvt_klass (cfg, klass)) {
11130 MonoInst *offset_ins;
11132 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11133 dreg = alloc_ireg_mp (cfg);
11134 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11135 } else if (field->offset == 0) {
11138 int addr_reg = mono_alloc_preg (cfg);
11139 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11141 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11142 MonoInst *iargs [2];
11144 g_assert (field->parent);
11145 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11146 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11147 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11149 MonoVTable *vtable = NULL;
11151 if (!cfg->compile_aot)
11152 vtable = mono_class_vtable (cfg->domain, klass);
11153 CHECK_TYPELOAD (klass);
11156 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11157 if (!(g_slist_find (class_inits, klass))) {
11158 mono_emit_abs_call (cfg, MONO_PATCH_INFO_CLASS_INIT, klass, helper_sig_class_init_trampoline, NULL);
11159 if (cfg->verbose_level > 2)
11160 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11161 class_inits = g_slist_prepend (class_inits, klass);
11164 if (cfg->run_cctors) {
11166 /* This makes so that inline cannot trigger */
11167 /* .cctors: too many apps depend on them */
11168 /* running with a specific order... */
11170 if (! vtable->initialized)
11171 INLINE_FAILURE ("class init");
11172 ex = mono_runtime_class_init_full (vtable, FALSE);
11174 set_exception_object (cfg, ex);
11175 goto exception_exit;
11179 if (cfg->compile_aot)
11180 EMIT_NEW_SFLDACONST (cfg, ins, field);
11183 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11185 EMIT_NEW_PCONST (cfg, ins, addr);
11188 MonoInst *iargs [1];
11189 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11190 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11194 /* Generate IR to do the actual load/store operation */
11196 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11197 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11198 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11201 if (op == CEE_LDSFLDA) {
11202 ins->klass = mono_class_from_mono_type (ftype);
11203 ins->type = STACK_PTR;
11205 } else if (op == CEE_STSFLD) {
11208 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11209 store->flags |= ins_flag;
11211 gboolean is_const = FALSE;
11212 MonoVTable *vtable = NULL;
11213 gpointer addr = NULL;
11215 if (!context_used) {
11216 vtable = mono_class_vtable (cfg->domain, klass);
11217 CHECK_TYPELOAD (klass);
11219 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11220 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11221 int ro_type = ftype->type;
11223 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11224 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11225 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11228 GSHAREDVT_FAILURE (op);
11230 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11233 case MONO_TYPE_BOOLEAN:
11235 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11239 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11242 case MONO_TYPE_CHAR:
11244 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11248 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11253 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11257 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11262 case MONO_TYPE_PTR:
11263 case MONO_TYPE_FNPTR:
11264 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11265 type_to_eval_stack_type ((cfg), field->type, *sp);
11268 case MONO_TYPE_STRING:
11269 case MONO_TYPE_OBJECT:
11270 case MONO_TYPE_CLASS:
11271 case MONO_TYPE_SZARRAY:
11272 case MONO_TYPE_ARRAY:
11273 if (!mono_gc_is_moving ()) {
11274 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11275 type_to_eval_stack_type ((cfg), field->type, *sp);
11283 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11288 case MONO_TYPE_VALUETYPE:
11298 CHECK_STACK_OVF (1);
11300 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11301 load->flags |= ins_flag;
11307 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11308 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11309 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11320 token = read32 (ip + 1);
11321 klass = mini_get_class (method, token, generic_context);
11322 CHECK_TYPELOAD (klass);
11323 if (ins_flag & MONO_INST_VOLATILE) {
11324 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11325 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11327 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11328 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11329 ins->flags |= ins_flag;
11330 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11331 generic_class_is_reference_type (cfg, klass)) {
11332 /* insert call to write barrier */
11333 emit_write_barrier (cfg, sp [0], sp [1]);
11345 const char *data_ptr;
11347 guint32 field_token;
11353 token = read32 (ip + 1);
11355 klass = mini_get_class (method, token, generic_context);
11356 CHECK_TYPELOAD (klass);
11358 context_used = mini_class_check_context_used (cfg, klass);
11360 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11361 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11362 ins->sreg1 = sp [0]->dreg;
11363 ins->type = STACK_I4;
11364 ins->dreg = alloc_ireg (cfg);
11365 MONO_ADD_INS (cfg->cbb, ins);
11366 *sp = mono_decompose_opcode (cfg, ins, &bblock);
11369 if (context_used) {
11370 MonoInst *args [3];
11371 MonoClass *array_class = mono_array_class_get (klass, 1);
11372 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11374 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11377 args [0] = emit_get_rgctx_klass (cfg, context_used,
11378 array_class, MONO_RGCTX_INFO_VTABLE);
11383 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11385 ins = mono_emit_jit_icall (cfg, mono_array_new_specific, args);
11387 if (cfg->opt & MONO_OPT_SHARED) {
11388 /* Decompose now to avoid problems with references to the domainvar */
11389 MonoInst *iargs [3];
11391 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11392 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11393 iargs [2] = sp [0];
11395 ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
11397 /* Decompose later since it is needed by abcrem */
11398 MonoClass *array_type = mono_array_class_get (klass, 1);
11399 mono_class_vtable (cfg->domain, array_type);
11400 CHECK_TYPELOAD (array_type);
11402 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11403 ins->dreg = alloc_ireg_ref (cfg);
11404 ins->sreg1 = sp [0]->dreg;
11405 ins->inst_newa_class = klass;
11406 ins->type = STACK_OBJ;
11407 ins->klass = array_type;
11408 MONO_ADD_INS (cfg->cbb, ins);
11409 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11410 cfg->cbb->has_array_access = TRUE;
11412 /* Needed so mono_emit_load_get_addr () gets called */
11413 mono_get_got_var (cfg);
11423 * we inline/optimize the initialization sequence if possible.
11424 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11425 * for small sizes open code the memcpy
11426 * ensure the rva field is big enough
11428 if ((cfg->opt & MONO_OPT_INTRINS) && ip + 6 < end && ip_in_bb (cfg, bblock, 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))) {
11429 MonoMethod *memcpy_method = get_memcpy_method ();
11430 MonoInst *iargs [3];
11431 int add_reg = alloc_ireg_mp (cfg);
11433 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11434 if (cfg->compile_aot) {
11435 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11437 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11439 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11440 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11449 if (sp [0]->type != STACK_OBJ)
11452 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11453 ins->dreg = alloc_preg (cfg);
11454 ins->sreg1 = sp [0]->dreg;
11455 ins->type = STACK_I4;
11456 /* This flag will be inherited by the decomposition */
11457 ins->flags |= MONO_INST_FAULT;
11458 MONO_ADD_INS (cfg->cbb, ins);
11459 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11460 cfg->cbb->has_array_access = TRUE;
11468 if (sp [0]->type != STACK_OBJ)
11471 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11473 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11474 CHECK_TYPELOAD (klass);
11475 /* we need to make sure that this array is exactly the type it needs
11476 * to be for correctness. the wrappers are lax with their usage
11477 * so we need to ignore them here
11479 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11480 MonoClass *array_class = mono_array_class_get (klass, 1);
11481 mini_emit_check_array_type (cfg, sp [0], array_class);
11482 CHECK_TYPELOAD (array_class);
11486 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11491 case CEE_LDELEM_I1:
11492 case CEE_LDELEM_U1:
11493 case CEE_LDELEM_I2:
11494 case CEE_LDELEM_U2:
11495 case CEE_LDELEM_I4:
11496 case CEE_LDELEM_U4:
11497 case CEE_LDELEM_I8:
11499 case CEE_LDELEM_R4:
11500 case CEE_LDELEM_R8:
11501 case CEE_LDELEM_REF: {
11507 if (*ip == CEE_LDELEM) {
11509 token = read32 (ip + 1);
11510 klass = mini_get_class (method, token, generic_context);
11511 CHECK_TYPELOAD (klass);
11512 mono_class_init (klass);
11515 klass = array_access_to_klass (*ip);
11517 if (sp [0]->type != STACK_OBJ)
11520 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11522 if (mini_is_gsharedvt_variable_klass (cfg, klass)) {
11523 // FIXME-VT: OP_ICONST optimization
11524 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11525 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11526 ins->opcode = OP_LOADV_MEMBASE;
11527 } else if (sp [1]->opcode == OP_ICONST) {
11528 int array_reg = sp [0]->dreg;
11529 int index_reg = sp [1]->dreg;
11530 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11532 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11533 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11535 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11536 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11539 if (*ip == CEE_LDELEM)
11546 case CEE_STELEM_I1:
11547 case CEE_STELEM_I2:
11548 case CEE_STELEM_I4:
11549 case CEE_STELEM_I8:
11550 case CEE_STELEM_R4:
11551 case CEE_STELEM_R8:
11552 case CEE_STELEM_REF:
11557 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11559 if (*ip == CEE_STELEM) {
11561 token = read32 (ip + 1);
11562 klass = mini_get_class (method, token, generic_context);
11563 CHECK_TYPELOAD (klass);
11564 mono_class_init (klass);
11567 klass = array_access_to_klass (*ip);
11569 if (sp [0]->type != STACK_OBJ)
11572 emit_array_store (cfg, klass, sp, TRUE);
11574 if (*ip == CEE_STELEM)
11581 case CEE_CKFINITE: {
11585 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11586 ins->sreg1 = sp [0]->dreg;
11587 ins->dreg = alloc_freg (cfg);
11588 ins->type = STACK_R8;
11589 MONO_ADD_INS (bblock, ins);
11591 *sp++ = mono_decompose_opcode (cfg, ins, &bblock);
11596 case CEE_REFANYVAL: {
11597 MonoInst *src_var, *src;
11599 int klass_reg = alloc_preg (cfg);
11600 int dreg = alloc_preg (cfg);
11602 GSHAREDVT_FAILURE (*ip);
11605 MONO_INST_NEW (cfg, ins, *ip);
11608 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11609 CHECK_TYPELOAD (klass);
11611 context_used = mini_class_check_context_used (cfg, klass);
11614 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11616 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11617 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11618 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11620 if (context_used) {
11621 MonoInst *klass_ins;
11623 klass_ins = emit_get_rgctx_klass (cfg, context_used,
11624 klass, MONO_RGCTX_INFO_KLASS);
11627 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11628 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11630 mini_emit_class_check (cfg, klass_reg, klass);
11632 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11633 ins->type = STACK_MP;
11638 case CEE_MKREFANY: {
11639 MonoInst *loc, *addr;
11641 GSHAREDVT_FAILURE (*ip);
11644 MONO_INST_NEW (cfg, ins, *ip);
11647 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11648 CHECK_TYPELOAD (klass);
11650 context_used = mini_class_check_context_used (cfg, klass);
11652 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11653 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11655 if (context_used) {
11656 MonoInst *const_ins;
11657 int type_reg = alloc_preg (cfg);
11659 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11660 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11661 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11662 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11663 } else if (cfg->compile_aot) {
11664 int const_reg = alloc_preg (cfg);
11665 int type_reg = alloc_preg (cfg);
11667 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11668 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11669 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11670 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11672 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
11673 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
11675 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11677 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11678 ins->type = STACK_VTYPE;
11679 ins->klass = mono_defaults.typed_reference_class;
11684 case CEE_LDTOKEN: {
11686 MonoClass *handle_class;
11688 CHECK_STACK_OVF (1);
11691 n = read32 (ip + 1);
11693 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11694 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11695 handle = mono_method_get_wrapper_data (method, n);
11696 handle_class = mono_method_get_wrapper_data (method, n + 1);
11697 if (handle_class == mono_defaults.typehandle_class)
11698 handle = &((MonoClass*)handle)->byval_arg;
11701 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11706 mono_class_init (handle_class);
11707 if (cfg->generic_sharing_context) {
11708 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11709 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11710 /* This case handles ldtoken
11711 of an open type, like for
11714 } else if (handle_class == mono_defaults.typehandle_class) {
11715 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type (handle));
11716 } else if (handle_class == mono_defaults.fieldhandle_class)
11717 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11718 else if (handle_class == mono_defaults.methodhandle_class)
11719 context_used = mini_method_check_context_used (cfg, handle);
11721 g_assert_not_reached ();
11724 if ((cfg->opt & MONO_OPT_SHARED) &&
11725 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11726 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11727 MonoInst *addr, *vtvar, *iargs [3];
11728 int method_context_used;
11730 method_context_used = mini_method_check_context_used (cfg, method);
11732 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11734 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11735 EMIT_NEW_ICONST (cfg, iargs [1], n);
11736 if (method_context_used) {
11737 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11738 method, MONO_RGCTX_INFO_METHOD);
11739 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11741 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11742 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11744 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11746 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11748 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11750 if ((ip + 5 < end) && ip_in_bb (cfg, bblock, ip + 5) &&
11751 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
11752 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11753 (cmethod->klass == mono_defaults.systemtype_class) &&
11754 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11755 MonoClass *tclass = mono_class_from_mono_type (handle);
11757 mono_class_init (tclass);
11758 if (context_used) {
11759 ins = emit_get_rgctx_klass (cfg, context_used,
11760 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11761 } else if (cfg->compile_aot) {
11762 if (method->wrapper_type) {
11763 mono_error_init (&error); //got to do it since there are multiple conditionals below
11764 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11765 /* Special case for static synchronized wrappers */
11766 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11768 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11769 /* FIXME: n is not a normal token */
11771 EMIT_NEW_PCONST (cfg, ins, NULL);
11774 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11777 EMIT_NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
11779 ins->type = STACK_OBJ;
11780 ins->klass = cmethod->klass;
11783 MonoInst *addr, *vtvar;
11785 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11787 if (context_used) {
11788 if (handle_class == mono_defaults.typehandle_class) {
11789 ins = emit_get_rgctx_klass (cfg, context_used,
11790 mono_class_from_mono_type (handle),
11791 MONO_RGCTX_INFO_TYPE);
11792 } else if (handle_class == mono_defaults.methodhandle_class) {
11793 ins = emit_get_rgctx_method (cfg, context_used,
11794 handle, MONO_RGCTX_INFO_METHOD);
11795 } else if (handle_class == mono_defaults.fieldhandle_class) {
11796 ins = emit_get_rgctx_field (cfg, context_used,
11797 handle, MONO_RGCTX_INFO_CLASS_FIELD);
11799 g_assert_not_reached ();
11801 } else if (cfg->compile_aot) {
11802 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11804 EMIT_NEW_PCONST (cfg, ins, handle);
11806 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11807 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11808 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11818 MONO_INST_NEW (cfg, ins, OP_THROW);
11820 ins->sreg1 = sp [0]->dreg;
11822 bblock->out_of_line = TRUE;
11823 MONO_ADD_INS (bblock, ins);
11824 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11825 MONO_ADD_INS (bblock, ins);
11828 link_bblock (cfg, bblock, end_bblock);
11829 start_new_bblock = 1;
11831 case CEE_ENDFINALLY:
11832 /* mono_save_seq_point_info () depends on this */
11833 if (sp != stack_start)
11834 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11835 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11836 MONO_ADD_INS (bblock, ins);
11838 start_new_bblock = 1;
11841 * Control will leave the method so empty the stack, otherwise
11842 * the next basic block will start with a nonempty stack.
11844 while (sp != stack_start) {
11849 case CEE_LEAVE_S: {
11852 if (*ip == CEE_LEAVE) {
11854 target = ip + 5 + (gint32)read32(ip + 1);
11857 target = ip + 2 + (signed char)(ip [1]);
11860 /* empty the stack */
11861 while (sp != stack_start) {
11866 * If this leave statement is in a catch block, check for a
11867 * pending exception, and rethrow it if necessary.
11868 * We avoid doing this in runtime invoke wrappers, since those are called
11869 * by native code which excepts the wrapper to catch all exceptions.
11871 for (i = 0; i < header->num_clauses; ++i) {
11872 MonoExceptionClause *clause = &header->clauses [i];
11875 * Use <= in the final comparison to handle clauses with multiple
11876 * leave statements, like in bug #78024.
11877 * The ordering of the exception clauses guarantees that we find the
11878 * innermost clause.
11880 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) {
11882 MonoBasicBlock *dont_throw;
11887 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11890 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11892 NEW_BBLOCK (cfg, dont_throw);
11895 * Currently, we always rethrow the abort exception, despite the
11896 * fact that this is not correct. See thread6.cs for an example.
11897 * But propagating the abort exception is more important than
11898 * getting the sematics right.
11900 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11901 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11902 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11904 MONO_START_BB (cfg, dont_throw);
11909 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11911 MonoExceptionClause *clause;
11913 for (tmp = handlers; tmp; tmp = tmp->next) {
11914 clause = tmp->data;
11915 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11917 link_bblock (cfg, bblock, tblock);
11918 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11919 ins->inst_target_bb = tblock;
11920 ins->inst_eh_block = clause;
11921 MONO_ADD_INS (bblock, ins);
11922 bblock->has_call_handler = 1;
11923 if (COMPILE_LLVM (cfg)) {
11924 MonoBasicBlock *target_bb;
11927 * Link the finally bblock with the target, since it will
11928 * conceptually branch there.
11929 * FIXME: Have to link the bblock containing the endfinally.
11931 GET_BBLOCK (cfg, target_bb, target);
11932 link_bblock (cfg, tblock, target_bb);
11935 g_list_free (handlers);
11938 MONO_INST_NEW (cfg, ins, OP_BR);
11939 MONO_ADD_INS (bblock, ins);
11940 GET_BBLOCK (cfg, tblock, target);
11941 link_bblock (cfg, bblock, tblock);
11942 ins->inst_target_bb = tblock;
11943 start_new_bblock = 1;
11945 if (*ip == CEE_LEAVE)
11954 * Mono specific opcodes
11956 case MONO_CUSTOM_PREFIX: {
11958 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11962 case CEE_MONO_ICALL: {
11964 MonoJitICallInfo *info;
11966 token = read32 (ip + 2);
11967 func = mono_method_get_wrapper_data (method, token);
11968 info = mono_find_jit_icall_by_addr (func);
11970 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11973 CHECK_STACK (info->sig->param_count);
11974 sp -= info->sig->param_count;
11976 ins = mono_emit_jit_icall (cfg, info->func, sp);
11977 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11981 inline_costs += 10 * num_calls++;
11985 case CEE_MONO_LDPTR: {
11988 CHECK_STACK_OVF (1);
11990 token = read32 (ip + 2);
11992 ptr = mono_method_get_wrapper_data (method, token);
11993 /* FIXME: Generalize this */
11994 if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
11995 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12000 EMIT_NEW_PCONST (cfg, ins, ptr);
12003 inline_costs += 10 * num_calls++;
12004 /* Can't embed random pointers into AOT code */
12008 case CEE_MONO_JIT_ICALL_ADDR: {
12009 MonoJitICallInfo *callinfo;
12012 CHECK_STACK_OVF (1);
12014 token = read32 (ip + 2);
12016 ptr = mono_method_get_wrapper_data (method, token);
12017 callinfo = mono_find_jit_icall_by_addr (ptr);
12018 g_assert (callinfo);
12019 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12022 inline_costs += 10 * num_calls++;
12025 case CEE_MONO_ICALL_ADDR: {
12026 MonoMethod *cmethod;
12029 CHECK_STACK_OVF (1);
12031 token = read32 (ip + 2);
12033 cmethod = mono_method_get_wrapper_data (method, token);
12035 if (cfg->compile_aot) {
12036 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12038 ptr = mono_lookup_internal_call (cmethod);
12040 EMIT_NEW_PCONST (cfg, ins, ptr);
12046 case CEE_MONO_VTADDR: {
12047 MonoInst *src_var, *src;
12053 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12054 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12059 case CEE_MONO_NEWOBJ: {
12060 MonoInst *iargs [2];
12062 CHECK_STACK_OVF (1);
12064 token = read32 (ip + 2);
12065 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12066 mono_class_init (klass);
12067 NEW_DOMAINCONST (cfg, iargs [0]);
12068 MONO_ADD_INS (cfg->cbb, iargs [0]);
12069 NEW_CLASSCONST (cfg, iargs [1], klass);
12070 MONO_ADD_INS (cfg->cbb, iargs [1]);
12071 *sp++ = mono_emit_jit_icall (cfg, mono_object_new, iargs);
12073 inline_costs += 10 * num_calls++;
12076 case CEE_MONO_OBJADDR:
12079 MONO_INST_NEW (cfg, ins, OP_MOVE);
12080 ins->dreg = alloc_ireg_mp (cfg);
12081 ins->sreg1 = sp [0]->dreg;
12082 ins->type = STACK_MP;
12083 MONO_ADD_INS (cfg->cbb, ins);
12087 case CEE_MONO_LDNATIVEOBJ:
12089 * Similar to LDOBJ, but instead load the unmanaged
12090 * representation of the vtype to the stack.
12095 token = read32 (ip + 2);
12096 klass = mono_method_get_wrapper_data (method, token);
12097 g_assert (klass->valuetype);
12098 mono_class_init (klass);
12101 MonoInst *src, *dest, *temp;
12104 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12105 temp->backend.is_pinvoke = 1;
12106 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12107 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12109 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12110 dest->type = STACK_VTYPE;
12111 dest->klass = klass;
12117 case CEE_MONO_RETOBJ: {
12119 * Same as RET, but return the native representation of a vtype
12122 g_assert (cfg->ret);
12123 g_assert (mono_method_signature (method)->pinvoke);
12128 token = read32 (ip + 2);
12129 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12131 if (!cfg->vret_addr) {
12132 g_assert (cfg->ret_var_is_local);
12134 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12136 EMIT_NEW_RETLOADA (cfg, ins);
12138 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12140 if (sp != stack_start)
12143 MONO_INST_NEW (cfg, ins, OP_BR);
12144 ins->inst_target_bb = end_bblock;
12145 MONO_ADD_INS (bblock, ins);
12146 link_bblock (cfg, bblock, end_bblock);
12147 start_new_bblock = 1;
12151 case CEE_MONO_CISINST:
12152 case CEE_MONO_CCASTCLASS: {
12157 token = read32 (ip + 2);
12158 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12159 if (ip [1] == CEE_MONO_CISINST)
12160 ins = handle_cisinst (cfg, klass, sp [0]);
12162 ins = handle_ccastclass (cfg, klass, sp [0]);
12168 case CEE_MONO_SAVE_LMF:
12169 case CEE_MONO_RESTORE_LMF:
12170 #ifdef MONO_ARCH_HAVE_LMF_OPS
12171 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
12172 MONO_ADD_INS (bblock, ins);
12173 cfg->need_lmf_area = TRUE;
12177 case CEE_MONO_CLASSCONST:
12178 CHECK_STACK_OVF (1);
12180 token = read32 (ip + 2);
12181 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12184 inline_costs += 10 * num_calls++;
12186 case CEE_MONO_NOT_TAKEN:
12187 bblock->out_of_line = TRUE;
12190 case CEE_MONO_TLS: {
12193 CHECK_STACK_OVF (1);
12195 key = (gint32)read32 (ip + 2);
12196 g_assert (key < TLS_KEY_NUM);
12198 ins = mono_create_tls_get (cfg, key);
12200 if (cfg->compile_aot) {
12202 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12203 ins->dreg = alloc_preg (cfg);
12204 ins->type = STACK_PTR;
12206 g_assert_not_reached ();
12209 ins->type = STACK_PTR;
12210 MONO_ADD_INS (bblock, ins);
12215 case CEE_MONO_DYN_CALL: {
12216 MonoCallInst *call;
12218 /* It would be easier to call a trampoline, but that would put an
12219 * extra frame on the stack, confusing exception handling. So
12220 * implement it inline using an opcode for now.
12223 if (!cfg->dyn_call_var) {
12224 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12225 /* prevent it from being register allocated */
12226 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12229 /* Has to use a call inst since it local regalloc expects it */
12230 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12231 ins = (MonoInst*)call;
12233 ins->sreg1 = sp [0]->dreg;
12234 ins->sreg2 = sp [1]->dreg;
12235 MONO_ADD_INS (bblock, ins);
12237 cfg->param_area = MAX (cfg->param_area, MONO_ARCH_DYN_CALL_PARAM_AREA);
12240 inline_costs += 10 * num_calls++;
12244 case CEE_MONO_MEMORY_BARRIER: {
12246 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12250 case CEE_MONO_JIT_ATTACH: {
12251 MonoInst *args [16], *domain_ins;
12252 MonoInst *ad_ins, *jit_tls_ins;
12253 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12255 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12257 EMIT_NEW_PCONST (cfg, ins, NULL);
12258 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12260 ad_ins = mono_get_domain_intrinsic (cfg);
12261 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12263 if (MONO_ARCH_HAVE_TLS_GET && ad_ins && jit_tls_ins) {
12264 NEW_BBLOCK (cfg, next_bb);
12265 NEW_BBLOCK (cfg, call_bb);
12267 if (cfg->compile_aot) {
12268 /* AOT code is only used in the root domain */
12269 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12271 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12273 MONO_ADD_INS (cfg->cbb, ad_ins);
12274 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12275 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12277 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12278 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12279 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12281 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12282 MONO_START_BB (cfg, call_bb);
12285 if (cfg->compile_aot) {
12286 /* AOT code is only used in the root domain */
12287 EMIT_NEW_PCONST (cfg, args [0], NULL);
12289 EMIT_NEW_PCONST (cfg, args [0], cfg->domain);
12291 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12292 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12295 MONO_START_BB (cfg, next_bb);
12301 case CEE_MONO_JIT_DETACH: {
12302 MonoInst *args [16];
12304 /* Restore the original domain */
12305 dreg = alloc_ireg (cfg);
12306 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12307 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12312 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12318 case CEE_PREFIX1: {
12321 case CEE_ARGLIST: {
12322 /* somewhat similar to LDTOKEN */
12323 MonoInst *addr, *vtvar;
12324 CHECK_STACK_OVF (1);
12325 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12327 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12328 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12330 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12331 ins->type = STACK_VTYPE;
12332 ins->klass = mono_defaults.argumenthandle_class;
12342 MonoInst *cmp, *arg1, *arg2;
12350 * The following transforms:
12351 * CEE_CEQ into OP_CEQ
12352 * CEE_CGT into OP_CGT
12353 * CEE_CGT_UN into OP_CGT_UN
12354 * CEE_CLT into OP_CLT
12355 * CEE_CLT_UN into OP_CLT_UN
12357 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12359 MONO_INST_NEW (cfg, ins, cmp->opcode);
12360 cmp->sreg1 = arg1->dreg;
12361 cmp->sreg2 = arg2->dreg;
12362 type_from_op (cfg, cmp, arg1, arg2);
12364 add_widen_op (cfg, cmp, &arg1, &arg2);
12365 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12366 cmp->opcode = OP_LCOMPARE;
12367 else if (arg1->type == STACK_R4)
12368 cmp->opcode = OP_RCOMPARE;
12369 else if (arg1->type == STACK_R8)
12370 cmp->opcode = OP_FCOMPARE;
12372 cmp->opcode = OP_ICOMPARE;
12373 MONO_ADD_INS (bblock, cmp);
12374 ins->type = STACK_I4;
12375 ins->dreg = alloc_dreg (cfg, ins->type);
12376 type_from_op (cfg, ins, arg1, arg2);
12378 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12380 * The backends expect the fceq opcodes to do the
12383 ins->sreg1 = cmp->sreg1;
12384 ins->sreg2 = cmp->sreg2;
12387 MONO_ADD_INS (bblock, ins);
12393 MonoInst *argconst;
12394 MonoMethod *cil_method;
12396 CHECK_STACK_OVF (1);
12398 n = read32 (ip + 2);
12399 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12400 if (!cmethod || mono_loader_get_last_error ())
12402 mono_class_init (cmethod->klass);
12404 mono_save_token_info (cfg, image, n, cmethod);
12406 context_used = mini_method_check_context_used (cfg, cmethod);
12408 cil_method = cmethod;
12409 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12410 METHOD_ACCESS_FAILURE (method, cil_method);
12412 if (mono_security_cas_enabled ()) {
12413 if (check_linkdemand (cfg, method, cmethod))
12414 INLINE_FAILURE ("linkdemand");
12415 CHECK_CFG_EXCEPTION;
12416 } else if (mono_security_core_clr_enabled ()) {
12417 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
12421 * Optimize the common case of ldftn+delegate creation
12423 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, bblock, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12424 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12425 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12426 MonoInst *target_ins, *handle_ins;
12427 MonoMethod *invoke;
12428 int invoke_context_used;
12430 invoke = mono_get_delegate_invoke (ctor_method->klass);
12431 if (!invoke || !mono_method_signature (invoke))
12434 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12436 target_ins = sp [-1];
12438 if (mono_security_core_clr_enabled ())
12439 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method, bblock, ip);
12441 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12442 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12443 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12444 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12445 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12449 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE)
12450 /* FIXME: SGEN support */
12451 if (invoke_context_used == 0) {
12453 if (cfg->verbose_level > 3)
12454 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
12455 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12458 CHECK_CFG_EXCEPTION;
12469 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12470 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12474 inline_costs += 10 * num_calls++;
12477 case CEE_LDVIRTFTN: {
12478 MonoInst *args [2];
12482 n = read32 (ip + 2);
12483 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12484 if (!cmethod || mono_loader_get_last_error ())
12486 mono_class_init (cmethod->klass);
12488 context_used = mini_method_check_context_used (cfg, cmethod);
12490 if (mono_security_cas_enabled ()) {
12491 if (check_linkdemand (cfg, method, cmethod))
12492 INLINE_FAILURE ("linkdemand");
12493 CHECK_CFG_EXCEPTION;
12494 } else if (mono_security_core_clr_enabled ()) {
12495 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
12499 * Optimize the common case of ldvirtftn+delegate creation
12501 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, bblock, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ip > header->code && ip [-1] == CEE_DUP)) {
12502 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12503 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12504 MonoInst *target_ins, *handle_ins;
12505 MonoMethod *invoke;
12506 int invoke_context_used;
12508 invoke = mono_get_delegate_invoke (ctor_method->klass);
12509 if (!invoke || !mono_method_signature (invoke))
12512 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12514 target_ins = sp [-1];
12516 if (mono_security_core_clr_enabled ())
12517 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method, bblock, ip);
12519 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE)
12520 /* FIXME: SGEN support */
12521 if (invoke_context_used == 0) {
12523 if (cfg->verbose_level > 3)
12524 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
12525 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, TRUE))) {
12528 CHECK_CFG_EXCEPTION;
12542 args [1] = emit_get_rgctx_method (cfg, context_used,
12543 cmethod, MONO_RGCTX_INFO_METHOD);
12546 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12548 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12551 inline_costs += 10 * num_calls++;
12555 CHECK_STACK_OVF (1);
12557 n = read16 (ip + 2);
12559 EMIT_NEW_ARGLOAD (cfg, ins, n);
12564 CHECK_STACK_OVF (1);
12566 n = read16 (ip + 2);
12568 NEW_ARGLOADA (cfg, ins, n);
12569 MONO_ADD_INS (cfg->cbb, ins);
12577 n = read16 (ip + 2);
12579 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12581 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12585 CHECK_STACK_OVF (1);
12587 n = read16 (ip + 2);
12589 EMIT_NEW_LOCLOAD (cfg, ins, n);
12594 unsigned char *tmp_ip;
12595 CHECK_STACK_OVF (1);
12597 n = read16 (ip + 2);
12600 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12606 EMIT_NEW_LOCLOADA (cfg, ins, n);
12615 n = read16 (ip + 2);
12617 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12619 emit_stloc_ir (cfg, sp, header, n);
12626 if (sp != stack_start)
12628 if (cfg->method != method)
12630 * Inlining this into a loop in a parent could lead to
12631 * stack overflows which is different behavior than the
12632 * non-inlined case, thus disable inlining in this case.
12634 INLINE_FAILURE("localloc");
12636 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12637 ins->dreg = alloc_preg (cfg);
12638 ins->sreg1 = sp [0]->dreg;
12639 ins->type = STACK_PTR;
12640 MONO_ADD_INS (cfg->cbb, ins);
12642 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12644 ins->flags |= MONO_INST_INIT;
12649 case CEE_ENDFILTER: {
12650 MonoExceptionClause *clause, *nearest;
12655 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
12657 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12658 ins->sreg1 = (*sp)->dreg;
12659 MONO_ADD_INS (bblock, ins);
12660 start_new_bblock = 1;
12664 for (cc = 0; cc < header->num_clauses; ++cc) {
12665 clause = &header->clauses [cc];
12666 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12667 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12668 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12671 g_assert (nearest);
12672 if ((ip - header->code) != nearest->handler_offset)
12677 case CEE_UNALIGNED_:
12678 ins_flag |= MONO_INST_UNALIGNED;
12679 /* FIXME: record alignment? we can assume 1 for now */
12683 case CEE_VOLATILE_:
12684 ins_flag |= MONO_INST_VOLATILE;
12688 ins_flag |= MONO_INST_TAILCALL;
12689 cfg->flags |= MONO_CFG_HAS_TAIL;
12690 /* Can't inline tail calls at this time */
12691 inline_costs += 100000;
12698 token = read32 (ip + 2);
12699 klass = mini_get_class (method, token, generic_context);
12700 CHECK_TYPELOAD (klass);
12701 if (generic_class_is_reference_type (cfg, klass))
12702 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12704 mini_emit_initobj (cfg, *sp, NULL, klass);
12708 case CEE_CONSTRAINED_:
12710 token = read32 (ip + 2);
12711 constrained_class = mini_get_class (method, token, generic_context);
12712 CHECK_TYPELOAD (constrained_class);
12716 case CEE_INITBLK: {
12717 MonoInst *iargs [3];
12721 /* Skip optimized paths for volatile operations. */
12722 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)) {
12723 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
12724 } 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)) {
12725 /* emit_memset only works when val == 0 */
12726 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
12729 iargs [0] = sp [0];
12730 iargs [1] = sp [1];
12731 iargs [2] = sp [2];
12732 if (ip [1] == CEE_CPBLK) {
12734 * FIXME: It's unclear whether we should be emitting both the acquire
12735 * and release barriers for cpblk. It is technically both a load and
12736 * store operation, so it seems like that's the sensible thing to do.
12738 * FIXME: We emit full barriers on both sides of the operation for
12739 * simplicity. We should have a separate atomic memcpy method instead.
12741 MonoMethod *memcpy_method = get_memcpy_method ();
12743 if (ins_flag & MONO_INST_VOLATILE)
12744 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12746 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
12747 call->flags |= ins_flag;
12749 if (ins_flag & MONO_INST_VOLATILE)
12750 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12752 MonoMethod *memset_method = get_memset_method ();
12753 if (ins_flag & MONO_INST_VOLATILE) {
12754 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
12755 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
12757 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
12758 call->flags |= ins_flag;
12769 ins_flag |= MONO_INST_NOTYPECHECK;
12771 ins_flag |= MONO_INST_NORANGECHECK;
12772 /* we ignore the no-nullcheck for now since we
12773 * really do it explicitly only when doing callvirt->call
12777 case CEE_RETHROW: {
12779 int handler_offset = -1;
12781 for (i = 0; i < header->num_clauses; ++i) {
12782 MonoExceptionClause *clause = &header->clauses [i];
12783 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12784 handler_offset = clause->handler_offset;
12789 bblock->flags |= BB_EXCEPTION_UNSAFE;
12791 if (handler_offset == -1)
12794 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12795 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12796 ins->sreg1 = load->dreg;
12797 MONO_ADD_INS (bblock, ins);
12799 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12800 MONO_ADD_INS (bblock, ins);
12803 link_bblock (cfg, bblock, end_bblock);
12804 start_new_bblock = 1;
12812 CHECK_STACK_OVF (1);
12814 token = read32 (ip + 2);
12815 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12816 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12819 val = mono_type_size (type, &ialign);
12821 MonoClass *klass = mini_get_class (method, token, generic_context);
12822 CHECK_TYPELOAD (klass);
12824 val = mono_type_size (&klass->byval_arg, &ialign);
12826 if (mini_is_gsharedvt_klass (cfg, klass))
12827 GSHAREDVT_FAILURE (*ip);
12829 EMIT_NEW_ICONST (cfg, ins, val);
12834 case CEE_REFANYTYPE: {
12835 MonoInst *src_var, *src;
12837 GSHAREDVT_FAILURE (*ip);
12843 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12845 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12846 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12847 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12852 case CEE_READONLY_:
12865 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12875 g_warning ("opcode 0x%02x not handled", *ip);
12879 if (start_new_bblock != 1)
12882 bblock->cil_length = ip - bblock->cil_code;
12883 if (bblock->next_bb) {
12884 /* This could already be set because of inlining, #693905 */
12885 MonoBasicBlock *bb = bblock;
12887 while (bb->next_bb)
12889 bb->next_bb = end_bblock;
12891 bblock->next_bb = end_bblock;
12894 if (cfg->method == method && cfg->domainvar) {
12896 MonoInst *get_domain;
12898 cfg->cbb = init_localsbb;
12900 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
12901 MONO_ADD_INS (cfg->cbb, get_domain);
12903 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
12905 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12906 MONO_ADD_INS (cfg->cbb, store);
12909 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12910 if (cfg->compile_aot)
12911 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12912 mono_get_got_var (cfg);
12915 if (cfg->method == method && cfg->got_var)
12916 mono_emit_load_got_addr (cfg);
12918 if (init_localsbb) {
12919 cfg->cbb = init_localsbb;
12921 for (i = 0; i < header->num_locals; ++i) {
12922 emit_init_local (cfg, i, header->locals [i], init_locals);
12926 if (cfg->init_ref_vars && cfg->method == method) {
12927 /* Emit initialization for ref vars */
12928 // FIXME: Avoid duplication initialization for IL locals.
12929 for (i = 0; i < cfg->num_varinfo; ++i) {
12930 MonoInst *ins = cfg->varinfo [i];
12932 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12933 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12937 if (cfg->lmf_var && cfg->method == method) {
12938 cfg->cbb = init_localsbb;
12939 emit_push_lmf (cfg);
12942 cfg->cbb = init_localsbb;
12943 emit_instrumentation_call (cfg, mono_profiler_method_enter);
12946 MonoBasicBlock *bb;
12949 * Make seq points at backward branch targets interruptable.
12951 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12952 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12953 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12956 /* Add a sequence point for method entry/exit events */
12957 if (cfg->gen_seq_points_debug_data) {
12958 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12959 MONO_ADD_INS (init_localsbb, ins);
12960 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12961 MONO_ADD_INS (cfg->bb_exit, ins);
12965 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12966 * the code they refer to was dead (#11880).
12968 if (sym_seq_points) {
12969 for (i = 0; i < header->code_size; ++i) {
12970 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12973 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12974 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
12981 if (cfg->method == method) {
12982 MonoBasicBlock *bb;
12983 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12984 bb->region = mono_find_block_region (cfg, bb->real_offset);
12986 mono_create_spvar_for_region (cfg, bb->region);
12987 if (cfg->verbose_level > 2)
12988 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
12992 if (inline_costs < 0) {
12995 /* Method is too large */
12996 mname = mono_method_full_name (method, TRUE);
12997 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INVALID_PROGRAM);
12998 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
13002 if ((cfg->verbose_level > 2) && (cfg->method == method))
13003 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13008 g_assert (!mono_error_ok (&cfg->error));
13012 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13016 set_exception_type_from_invalid_il (cfg, method, ip);
13020 g_slist_free (class_inits);
13021 mono_basic_block_free (original_bb);
13022 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13023 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
13024 if (cfg->exception_type)
13027 return inline_costs;
13031 store_membase_reg_to_store_membase_imm (int opcode)
13034 case OP_STORE_MEMBASE_REG:
13035 return OP_STORE_MEMBASE_IMM;
13036 case OP_STOREI1_MEMBASE_REG:
13037 return OP_STOREI1_MEMBASE_IMM;
13038 case OP_STOREI2_MEMBASE_REG:
13039 return OP_STOREI2_MEMBASE_IMM;
13040 case OP_STOREI4_MEMBASE_REG:
13041 return OP_STOREI4_MEMBASE_IMM;
13042 case OP_STOREI8_MEMBASE_REG:
13043 return OP_STOREI8_MEMBASE_IMM;
13045 g_assert_not_reached ();
13052 mono_op_to_op_imm (int opcode)
13056 return OP_IADD_IMM;
13058 return OP_ISUB_IMM;
13060 return OP_IDIV_IMM;
13062 return OP_IDIV_UN_IMM;
13064 return OP_IREM_IMM;
13066 return OP_IREM_UN_IMM;
13068 return OP_IMUL_IMM;
13070 return OP_IAND_IMM;
13074 return OP_IXOR_IMM;
13076 return OP_ISHL_IMM;
13078 return OP_ISHR_IMM;
13080 return OP_ISHR_UN_IMM;
13083 return OP_LADD_IMM;
13085 return OP_LSUB_IMM;
13087 return OP_LAND_IMM;
13091 return OP_LXOR_IMM;
13093 return OP_LSHL_IMM;
13095 return OP_LSHR_IMM;
13097 return OP_LSHR_UN_IMM;
13098 #if SIZEOF_REGISTER == 8
13100 return OP_LREM_IMM;
13104 return OP_COMPARE_IMM;
13106 return OP_ICOMPARE_IMM;
13108 return OP_LCOMPARE_IMM;
13110 case OP_STORE_MEMBASE_REG:
13111 return OP_STORE_MEMBASE_IMM;
13112 case OP_STOREI1_MEMBASE_REG:
13113 return OP_STOREI1_MEMBASE_IMM;
13114 case OP_STOREI2_MEMBASE_REG:
13115 return OP_STOREI2_MEMBASE_IMM;
13116 case OP_STOREI4_MEMBASE_REG:
13117 return OP_STOREI4_MEMBASE_IMM;
13119 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13121 return OP_X86_PUSH_IMM;
13122 case OP_X86_COMPARE_MEMBASE_REG:
13123 return OP_X86_COMPARE_MEMBASE_IMM;
13125 #if defined(TARGET_AMD64)
13126 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13127 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13129 case OP_VOIDCALL_REG:
13130 return OP_VOIDCALL;
13138 return OP_LOCALLOC_IMM;
13145 ldind_to_load_membase (int opcode)
13149 return OP_LOADI1_MEMBASE;
13151 return OP_LOADU1_MEMBASE;
13153 return OP_LOADI2_MEMBASE;
13155 return OP_LOADU2_MEMBASE;
13157 return OP_LOADI4_MEMBASE;
13159 return OP_LOADU4_MEMBASE;
13161 return OP_LOAD_MEMBASE;
13162 case CEE_LDIND_REF:
13163 return OP_LOAD_MEMBASE;
13165 return OP_LOADI8_MEMBASE;
13167 return OP_LOADR4_MEMBASE;
13169 return OP_LOADR8_MEMBASE;
13171 g_assert_not_reached ();
13178 stind_to_store_membase (int opcode)
13182 return OP_STOREI1_MEMBASE_REG;
13184 return OP_STOREI2_MEMBASE_REG;
13186 return OP_STOREI4_MEMBASE_REG;
13188 case CEE_STIND_REF:
13189 return OP_STORE_MEMBASE_REG;
13191 return OP_STOREI8_MEMBASE_REG;
13193 return OP_STORER4_MEMBASE_REG;
13195 return OP_STORER8_MEMBASE_REG;
13197 g_assert_not_reached ();
13204 mono_load_membase_to_load_mem (int opcode)
13206 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13207 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13209 case OP_LOAD_MEMBASE:
13210 return OP_LOAD_MEM;
13211 case OP_LOADU1_MEMBASE:
13212 return OP_LOADU1_MEM;
13213 case OP_LOADU2_MEMBASE:
13214 return OP_LOADU2_MEM;
13215 case OP_LOADI4_MEMBASE:
13216 return OP_LOADI4_MEM;
13217 case OP_LOADU4_MEMBASE:
13218 return OP_LOADU4_MEM;
13219 #if SIZEOF_REGISTER == 8
13220 case OP_LOADI8_MEMBASE:
13221 return OP_LOADI8_MEM;
13230 op_to_op_dest_membase (int store_opcode, int opcode)
13232 #if defined(TARGET_X86)
13233 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13238 return OP_X86_ADD_MEMBASE_REG;
13240 return OP_X86_SUB_MEMBASE_REG;
13242 return OP_X86_AND_MEMBASE_REG;
13244 return OP_X86_OR_MEMBASE_REG;
13246 return OP_X86_XOR_MEMBASE_REG;
13249 return OP_X86_ADD_MEMBASE_IMM;
13252 return OP_X86_SUB_MEMBASE_IMM;
13255 return OP_X86_AND_MEMBASE_IMM;
13258 return OP_X86_OR_MEMBASE_IMM;
13261 return OP_X86_XOR_MEMBASE_IMM;
13267 #if defined(TARGET_AMD64)
13268 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13273 return OP_X86_ADD_MEMBASE_REG;
13275 return OP_X86_SUB_MEMBASE_REG;
13277 return OP_X86_AND_MEMBASE_REG;
13279 return OP_X86_OR_MEMBASE_REG;
13281 return OP_X86_XOR_MEMBASE_REG;
13283 return OP_X86_ADD_MEMBASE_IMM;
13285 return OP_X86_SUB_MEMBASE_IMM;
13287 return OP_X86_AND_MEMBASE_IMM;
13289 return OP_X86_OR_MEMBASE_IMM;
13291 return OP_X86_XOR_MEMBASE_IMM;
13293 return OP_AMD64_ADD_MEMBASE_REG;
13295 return OP_AMD64_SUB_MEMBASE_REG;
13297 return OP_AMD64_AND_MEMBASE_REG;
13299 return OP_AMD64_OR_MEMBASE_REG;
13301 return OP_AMD64_XOR_MEMBASE_REG;
13304 return OP_AMD64_ADD_MEMBASE_IMM;
13307 return OP_AMD64_SUB_MEMBASE_IMM;
13310 return OP_AMD64_AND_MEMBASE_IMM;
13313 return OP_AMD64_OR_MEMBASE_IMM;
13316 return OP_AMD64_XOR_MEMBASE_IMM;
13326 op_to_op_store_membase (int store_opcode, int opcode)
13328 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13331 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13332 return OP_X86_SETEQ_MEMBASE;
13334 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13335 return OP_X86_SETNE_MEMBASE;
13343 op_to_op_src1_membase (int load_opcode, int opcode)
13346 /* FIXME: This has sign extension issues */
13348 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13349 return OP_X86_COMPARE_MEMBASE8_IMM;
13352 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13357 return OP_X86_PUSH_MEMBASE;
13358 case OP_COMPARE_IMM:
13359 case OP_ICOMPARE_IMM:
13360 return OP_X86_COMPARE_MEMBASE_IMM;
13363 return OP_X86_COMPARE_MEMBASE_REG;
13367 #ifdef TARGET_AMD64
13368 /* FIXME: This has sign extension issues */
13370 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13371 return OP_X86_COMPARE_MEMBASE8_IMM;
13376 #ifdef __mono_ilp32__
13377 if (load_opcode == OP_LOADI8_MEMBASE)
13379 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13381 return OP_X86_PUSH_MEMBASE;
13383 /* FIXME: This only works for 32 bit immediates
13384 case OP_COMPARE_IMM:
13385 case OP_LCOMPARE_IMM:
13386 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13387 return OP_AMD64_COMPARE_MEMBASE_IMM;
13389 case OP_ICOMPARE_IMM:
13390 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13391 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13395 #ifdef __mono_ilp32__
13396 if (load_opcode == OP_LOAD_MEMBASE)
13397 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13398 if (load_opcode == OP_LOADI8_MEMBASE)
13400 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13402 return OP_AMD64_COMPARE_MEMBASE_REG;
13405 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13406 return OP_AMD64_ICOMPARE_MEMBASE_REG;
13415 op_to_op_src2_membase (int load_opcode, int opcode)
13418 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13424 return OP_X86_COMPARE_REG_MEMBASE;
13426 return OP_X86_ADD_REG_MEMBASE;
13428 return OP_X86_SUB_REG_MEMBASE;
13430 return OP_X86_AND_REG_MEMBASE;
13432 return OP_X86_OR_REG_MEMBASE;
13434 return OP_X86_XOR_REG_MEMBASE;
13438 #ifdef TARGET_AMD64
13439 #ifdef __mono_ilp32__
13440 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE) ) {
13442 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)) {
13446 return OP_AMD64_ICOMPARE_REG_MEMBASE;
13448 return OP_X86_ADD_REG_MEMBASE;
13450 return OP_X86_SUB_REG_MEMBASE;
13452 return OP_X86_AND_REG_MEMBASE;
13454 return OP_X86_OR_REG_MEMBASE;
13456 return OP_X86_XOR_REG_MEMBASE;
13458 #ifdef __mono_ilp32__
13459 } else if (load_opcode == OP_LOADI8_MEMBASE) {
13461 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE)) {
13466 return OP_AMD64_COMPARE_REG_MEMBASE;
13468 return OP_AMD64_ADD_REG_MEMBASE;
13470 return OP_AMD64_SUB_REG_MEMBASE;
13472 return OP_AMD64_AND_REG_MEMBASE;
13474 return OP_AMD64_OR_REG_MEMBASE;
13476 return OP_AMD64_XOR_REG_MEMBASE;
13485 mono_op_to_op_imm_noemul (int opcode)
13488 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13494 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13501 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13506 return mono_op_to_op_imm (opcode);
13511 * mono_handle_global_vregs:
13513 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
13517 mono_handle_global_vregs (MonoCompile *cfg)
13519 gint32 *vreg_to_bb;
13520 MonoBasicBlock *bb;
13523 vreg_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13525 #ifdef MONO_ARCH_SIMD_INTRINSICS
13526 if (cfg->uses_simd_intrinsics)
13527 mono_simd_simplify_indirection (cfg);
13530 /* Find local vregs used in more than one bb */
13531 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13532 MonoInst *ins = bb->code;
13533 int block_num = bb->block_num;
13535 if (cfg->verbose_level > 2)
13536 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13539 for (; ins; ins = ins->next) {
13540 const char *spec = INS_INFO (ins->opcode);
13541 int regtype = 0, regindex;
13544 if (G_UNLIKELY (cfg->verbose_level > 2))
13545 mono_print_ins (ins);
13547 g_assert (ins->opcode >= MONO_CEE_LAST);
13549 for (regindex = 0; regindex < 4; regindex ++) {
13552 if (regindex == 0) {
13553 regtype = spec [MONO_INST_DEST];
13554 if (regtype == ' ')
13557 } else if (regindex == 1) {
13558 regtype = spec [MONO_INST_SRC1];
13559 if (regtype == ' ')
13562 } else if (regindex == 2) {
13563 regtype = spec [MONO_INST_SRC2];
13564 if (regtype == ' ')
13567 } else if (regindex == 3) {
13568 regtype = spec [MONO_INST_SRC3];
13569 if (regtype == ' ')
13574 #if SIZEOF_REGISTER == 4
13575 /* In the LLVM case, the long opcodes are not decomposed */
13576 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13578 * Since some instructions reference the original long vreg,
13579 * and some reference the two component vregs, it is quite hard
13580 * to determine when it needs to be global. So be conservative.
13582 if (!get_vreg_to_inst (cfg, vreg)) {
13583 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13585 if (cfg->verbose_level > 2)
13586 printf ("LONG VREG R%d made global.\n", vreg);
13590 * Make the component vregs volatile since the optimizations can
13591 * get confused otherwise.
13593 get_vreg_to_inst (cfg, vreg + 1)->flags |= MONO_INST_VOLATILE;
13594 get_vreg_to_inst (cfg, vreg + 2)->flags |= MONO_INST_VOLATILE;
13598 g_assert (vreg != -1);
13600 prev_bb = vreg_to_bb [vreg];
13601 if (prev_bb == 0) {
13602 /* 0 is a valid block num */
13603 vreg_to_bb [vreg] = block_num + 1;
13604 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13605 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13608 if (!get_vreg_to_inst (cfg, vreg)) {
13609 if (G_UNLIKELY (cfg->verbose_level > 2))
13610 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13614 if (vreg_is_ref (cfg, vreg))
13615 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13617 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13620 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13623 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13626 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13629 g_assert_not_reached ();
13633 /* Flag as having been used in more than one bb */
13634 vreg_to_bb [vreg] = -1;
13640 /* If a variable is used in only one bblock, convert it into a local vreg */
13641 for (i = 0; i < cfg->num_varinfo; i++) {
13642 MonoInst *var = cfg->varinfo [i];
13643 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13645 switch (var->type) {
13651 #if SIZEOF_REGISTER == 8
13654 #if !defined(TARGET_X86)
13655 /* Enabling this screws up the fp stack on x86 */
13658 if (mono_arch_is_soft_float ())
13661 /* Arguments are implicitly global */
13662 /* Putting R4 vars into registers doesn't work currently */
13663 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13664 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) {
13666 * Make that the variable's liveness interval doesn't contain a call, since
13667 * that would cause the lvreg to be spilled, making the whole optimization
13670 /* This is too slow for JIT compilation */
13672 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13674 int def_index, call_index, ins_index;
13675 gboolean spilled = FALSE;
13680 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13681 const char *spec = INS_INFO (ins->opcode);
13683 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13684 def_index = ins_index;
13686 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13687 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13688 if (call_index > def_index) {
13694 if (MONO_IS_CALL (ins))
13695 call_index = ins_index;
13705 if (G_UNLIKELY (cfg->verbose_level > 2))
13706 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13707 var->flags |= MONO_INST_IS_DEAD;
13708 cfg->vreg_to_inst [var->dreg] = NULL;
13715 * Compress the varinfo and vars tables so the liveness computation is faster and
13716 * takes up less space.
13719 for (i = 0; i < cfg->num_varinfo; ++i) {
13720 MonoInst *var = cfg->varinfo [i];
13721 if (pos < i && cfg->locals_start == i)
13722 cfg->locals_start = pos;
13723 if (!(var->flags & MONO_INST_IS_DEAD)) {
13725 cfg->varinfo [pos] = cfg->varinfo [i];
13726 cfg->varinfo [pos]->inst_c0 = pos;
13727 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13728 cfg->vars [pos].idx = pos;
13729 #if SIZEOF_REGISTER == 4
13730 if (cfg->varinfo [pos]->type == STACK_I8) {
13731 /* Modify the two component vars too */
13734 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 1);
13735 var1->inst_c0 = pos;
13736 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 2);
13737 var1->inst_c0 = pos;
13744 cfg->num_varinfo = pos;
13745 if (cfg->locals_start > cfg->num_varinfo)
13746 cfg->locals_start = cfg->num_varinfo;
13750 * mono_spill_global_vars:
13752 * Generate spill code for variables which are not allocated to registers,
13753 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13754 * code is generated which could be optimized by the local optimization passes.
13757 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13759 MonoBasicBlock *bb;
13761 int orig_next_vreg;
13762 guint32 *vreg_to_lvreg;
13764 guint32 i, lvregs_len;
13765 gboolean dest_has_lvreg = FALSE;
13766 guint32 stacktypes [128];
13767 MonoInst **live_range_start, **live_range_end;
13768 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13769 int *gsharedvt_vreg_to_idx = NULL;
13771 *need_local_opts = FALSE;
13773 memset (spec2, 0, sizeof (spec2));
13775 /* FIXME: Move this function to mini.c */
13776 stacktypes ['i'] = STACK_PTR;
13777 stacktypes ['l'] = STACK_I8;
13778 stacktypes ['f'] = STACK_R8;
13779 #ifdef MONO_ARCH_SIMD_INTRINSICS
13780 stacktypes ['x'] = STACK_VTYPE;
13783 #if SIZEOF_REGISTER == 4
13784 /* Create MonoInsts for longs */
13785 for (i = 0; i < cfg->num_varinfo; i++) {
13786 MonoInst *ins = cfg->varinfo [i];
13788 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13789 switch (ins->type) {
13794 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13797 g_assert (ins->opcode == OP_REGOFFSET);
13799 tree = get_vreg_to_inst (cfg, ins->dreg + 1);
13801 tree->opcode = OP_REGOFFSET;
13802 tree->inst_basereg = ins->inst_basereg;
13803 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13805 tree = get_vreg_to_inst (cfg, ins->dreg + 2);
13807 tree->opcode = OP_REGOFFSET;
13808 tree->inst_basereg = ins->inst_basereg;
13809 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13819 if (cfg->compute_gc_maps) {
13820 /* registers need liveness info even for !non refs */
13821 for (i = 0; i < cfg->num_varinfo; i++) {
13822 MonoInst *ins = cfg->varinfo [i];
13824 if (ins->opcode == OP_REGVAR)
13825 ins->flags |= MONO_INST_GC_TRACK;
13829 if (cfg->gsharedvt) {
13830 gsharedvt_vreg_to_idx = mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13832 for (i = 0; i < cfg->num_varinfo; ++i) {
13833 MonoInst *ins = cfg->varinfo [i];
13836 if (mini_is_gsharedvt_variable_type (cfg, ins->inst_vtype)) {
13837 if (i >= cfg->locals_start) {
13839 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13840 gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13841 ins->opcode = OP_GSHAREDVT_LOCAL;
13842 ins->inst_imm = idx;
13845 gsharedvt_vreg_to_idx [ins->dreg] = -1;
13846 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13852 /* FIXME: widening and truncation */
13855 * As an optimization, when a variable allocated to the stack is first loaded into
13856 * an lvreg, we will remember the lvreg and use it the next time instead of loading
13857 * the variable again.
13859 orig_next_vreg = cfg->next_vreg;
13860 vreg_to_lvreg = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13861 lvregs = mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
13865 * These arrays contain the first and last instructions accessing a given
13867 * Since we emit bblocks in the same order we process them here, and we
13868 * don't split live ranges, these will precisely describe the live range of
13869 * the variable, i.e. the instruction range where a valid value can be found
13870 * in the variables location.
13871 * The live range is computed using the liveness info computed by the liveness pass.
13872 * We can't use vmv->range, since that is an abstract live range, and we need
13873 * one which is instruction precise.
13874 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13876 /* FIXME: Only do this if debugging info is requested */
13877 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13878 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13879 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13880 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13882 /* Add spill loads/stores */
13883 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13886 if (cfg->verbose_level > 2)
13887 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13889 /* Clear vreg_to_lvreg array */
13890 for (i = 0; i < lvregs_len; i++)
13891 vreg_to_lvreg [lvregs [i]] = 0;
13895 MONO_BB_FOR_EACH_INS (bb, ins) {
13896 const char *spec = INS_INFO (ins->opcode);
13897 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13898 gboolean store, no_lvreg;
13899 int sregs [MONO_MAX_SRC_REGS];
13901 if (G_UNLIKELY (cfg->verbose_level > 2))
13902 mono_print_ins (ins);
13904 if (ins->opcode == OP_NOP)
13908 * We handle LDADDR here as well, since it can only be decomposed
13909 * when variable addresses are known.
13911 if (ins->opcode == OP_LDADDR) {
13912 MonoInst *var = ins->inst_p0;
13914 if (var->opcode == OP_VTARG_ADDR) {
13915 /* Happens on SPARC/S390 where vtypes are passed by reference */
13916 MonoInst *vtaddr = var->inst_left;
13917 if (vtaddr->opcode == OP_REGVAR) {
13918 ins->opcode = OP_MOVE;
13919 ins->sreg1 = vtaddr->dreg;
13921 else if (var->inst_left->opcode == OP_REGOFFSET) {
13922 ins->opcode = OP_LOAD_MEMBASE;
13923 ins->inst_basereg = vtaddr->inst_basereg;
13924 ins->inst_offset = vtaddr->inst_offset;
13927 } else if (cfg->gsharedvt && gsharedvt_vreg_to_idx [var->dreg] < 0) {
13928 /* gsharedvt arg passed by ref */
13929 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13931 ins->opcode = OP_LOAD_MEMBASE;
13932 ins->inst_basereg = var->inst_basereg;
13933 ins->inst_offset = var->inst_offset;
13934 } else if (cfg->gsharedvt && gsharedvt_vreg_to_idx [var->dreg]) {
13935 MonoInst *load, *load2, *load3;
13936 int idx = gsharedvt_vreg_to_idx [var->dreg] - 1;
13937 int reg1, reg2, reg3;
13938 MonoInst *info_var = cfg->gsharedvt_info_var;
13939 MonoInst *locals_var = cfg->gsharedvt_locals_var;
13943 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13946 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13948 g_assert (info_var);
13949 g_assert (locals_var);
13951 /* Mark the instruction used to compute the locals var as used */
13952 cfg->gsharedvt_locals_var_ins = NULL;
13954 /* Load the offset */
13955 if (info_var->opcode == OP_REGOFFSET) {
13956 reg1 = alloc_ireg (cfg);
13957 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13958 } else if (info_var->opcode == OP_REGVAR) {
13960 reg1 = info_var->dreg;
13962 g_assert_not_reached ();
13964 reg2 = alloc_ireg (cfg);
13965 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13966 /* Load the locals area address */
13967 reg3 = alloc_ireg (cfg);
13968 if (locals_var->opcode == OP_REGOFFSET) {
13969 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
13970 } else if (locals_var->opcode == OP_REGVAR) {
13971 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
13973 g_assert_not_reached ();
13975 /* Compute the address */
13976 ins->opcode = OP_PADD;
13980 mono_bblock_insert_before_ins (bb, ins, load3);
13981 mono_bblock_insert_before_ins (bb, load3, load2);
13983 mono_bblock_insert_before_ins (bb, load2, load);
13985 g_assert (var->opcode == OP_REGOFFSET);
13987 ins->opcode = OP_ADD_IMM;
13988 ins->sreg1 = var->inst_basereg;
13989 ins->inst_imm = var->inst_offset;
13992 *need_local_opts = TRUE;
13993 spec = INS_INFO (ins->opcode);
13996 if (ins->opcode < MONO_CEE_LAST) {
13997 mono_print_ins (ins);
13998 g_assert_not_reached ();
14002 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14006 if (MONO_IS_STORE_MEMBASE (ins)) {
14007 tmp_reg = ins->dreg;
14008 ins->dreg = ins->sreg2;
14009 ins->sreg2 = tmp_reg;
14012 spec2 [MONO_INST_DEST] = ' ';
14013 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14014 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14015 spec2 [MONO_INST_SRC3] = ' ';
14017 } else if (MONO_IS_STORE_MEMINDEX (ins))
14018 g_assert_not_reached ();
14023 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14024 printf ("\t %.3s %d", spec, ins->dreg);
14025 num_sregs = mono_inst_get_src_registers (ins, sregs);
14026 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14027 printf (" %d", sregs [srcindex]);
14034 regtype = spec [MONO_INST_DEST];
14035 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14038 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14039 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14040 MonoInst *store_ins;
14042 MonoInst *def_ins = ins;
14043 int dreg = ins->dreg; /* The original vreg */
14045 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14047 if (var->opcode == OP_REGVAR) {
14048 ins->dreg = var->dreg;
14049 } 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)) {
14051 * Instead of emitting a load+store, use a _membase opcode.
14053 g_assert (var->opcode == OP_REGOFFSET);
14054 if (ins->opcode == OP_MOVE) {
14058 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14059 ins->inst_basereg = var->inst_basereg;
14060 ins->inst_offset = var->inst_offset;
14063 spec = INS_INFO (ins->opcode);
14067 g_assert (var->opcode == OP_REGOFFSET);
14069 prev_dreg = ins->dreg;
14071 /* Invalidate any previous lvreg for this vreg */
14072 vreg_to_lvreg [ins->dreg] = 0;
14076 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14078 store_opcode = OP_STOREI8_MEMBASE_REG;
14081 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14083 #if SIZEOF_REGISTER != 8
14084 if (regtype == 'l') {
14085 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, ins->dreg + 1);
14086 mono_bblock_insert_after_ins (bb, ins, store_ins);
14087 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, ins->dreg + 2);
14088 mono_bblock_insert_after_ins (bb, ins, store_ins);
14089 def_ins = store_ins;
14094 g_assert (store_opcode != OP_STOREV_MEMBASE);
14096 /* Try to fuse the store into the instruction itself */
14097 /* FIXME: Add more instructions */
14098 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14099 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14100 ins->inst_imm = ins->inst_c0;
14101 ins->inst_destbasereg = var->inst_basereg;
14102 ins->inst_offset = var->inst_offset;
14103 spec = INS_INFO (ins->opcode);
14104 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14105 ins->opcode = store_opcode;
14106 ins->inst_destbasereg = var->inst_basereg;
14107 ins->inst_offset = var->inst_offset;
14111 tmp_reg = ins->dreg;
14112 ins->dreg = ins->sreg2;
14113 ins->sreg2 = tmp_reg;
14116 spec2 [MONO_INST_DEST] = ' ';
14117 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14118 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14119 spec2 [MONO_INST_SRC3] = ' ';
14121 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14122 // FIXME: The backends expect the base reg to be in inst_basereg
14123 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14125 ins->inst_basereg = var->inst_basereg;
14126 ins->inst_offset = var->inst_offset;
14127 spec = INS_INFO (ins->opcode);
14129 /* printf ("INS: "); mono_print_ins (ins); */
14130 /* Create a store instruction */
14131 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14133 /* Insert it after the instruction */
14134 mono_bblock_insert_after_ins (bb, ins, store_ins);
14136 def_ins = store_ins;
14139 * We can't assign ins->dreg to var->dreg here, since the
14140 * sregs could use it. So set a flag, and do it after
14143 if ((!MONO_ARCH_USE_FPSTACK || ((store_opcode != OP_STORER8_MEMBASE_REG) && (store_opcode != OP_STORER4_MEMBASE_REG))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
14144 dest_has_lvreg = TRUE;
14149 if (def_ins && !live_range_start [dreg]) {
14150 live_range_start [dreg] = def_ins;
14151 live_range_start_bb [dreg] = bb;
14154 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14157 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14158 tmp->inst_c1 = dreg;
14159 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14166 num_sregs = mono_inst_get_src_registers (ins, sregs);
14167 for (srcindex = 0; srcindex < 3; ++srcindex) {
14168 regtype = spec [MONO_INST_SRC1 + srcindex];
14169 sreg = sregs [srcindex];
14171 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14172 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14173 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14174 MonoInst *use_ins = ins;
14175 MonoInst *load_ins;
14176 guint32 load_opcode;
14178 if (var->opcode == OP_REGVAR) {
14179 sregs [srcindex] = var->dreg;
14180 //mono_inst_set_src_registers (ins, sregs);
14181 live_range_end [sreg] = use_ins;
14182 live_range_end_bb [sreg] = bb;
14184 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14187 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14188 /* var->dreg is a hreg */
14189 tmp->inst_c1 = sreg;
14190 mono_bblock_insert_after_ins (bb, ins, tmp);
14196 g_assert (var->opcode == OP_REGOFFSET);
14198 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14200 g_assert (load_opcode != OP_LOADV_MEMBASE);
14202 if (vreg_to_lvreg [sreg]) {
14203 g_assert (vreg_to_lvreg [sreg] != -1);
14205 /* The variable is already loaded to an lvreg */
14206 if (G_UNLIKELY (cfg->verbose_level > 2))
14207 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14208 sregs [srcindex] = vreg_to_lvreg [sreg];
14209 //mono_inst_set_src_registers (ins, sregs);
14213 /* Try to fuse the load into the instruction */
14214 if ((srcindex == 0) && (op_to_op_src1_membase (load_opcode, ins->opcode) != -1)) {
14215 ins->opcode = op_to_op_src1_membase (load_opcode, ins->opcode);
14216 sregs [0] = var->inst_basereg;
14217 //mono_inst_set_src_registers (ins, sregs);
14218 ins->inst_offset = var->inst_offset;
14219 } else if ((srcindex == 1) && (op_to_op_src2_membase (load_opcode, ins->opcode) != -1)) {
14220 ins->opcode = op_to_op_src2_membase (load_opcode, ins->opcode);
14221 sregs [1] = var->inst_basereg;
14222 //mono_inst_set_src_registers (ins, sregs);
14223 ins->inst_offset = var->inst_offset;
14225 if (MONO_IS_REAL_MOVE (ins)) {
14226 ins->opcode = OP_NOP;
14229 //printf ("%d ", srcindex); mono_print_ins (ins);
14231 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14233 if ((!MONO_ARCH_USE_FPSTACK || ((load_opcode != OP_LOADR8_MEMBASE) && (load_opcode != OP_LOADR4_MEMBASE))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && !no_lvreg) {
14234 if (var->dreg == prev_dreg) {
14236 * sreg refers to the value loaded by the load
14237 * emitted below, but we need to use ins->dreg
14238 * since it refers to the store emitted earlier.
14242 g_assert (sreg != -1);
14243 vreg_to_lvreg [var->dreg] = sreg;
14244 g_assert (lvregs_len < 1024);
14245 lvregs [lvregs_len ++] = var->dreg;
14249 sregs [srcindex] = sreg;
14250 //mono_inst_set_src_registers (ins, sregs);
14252 #if SIZEOF_REGISTER != 8
14253 if (regtype == 'l') {
14254 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 2, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14255 mono_bblock_insert_before_ins (bb, ins, load_ins);
14256 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 1, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14257 mono_bblock_insert_before_ins (bb, ins, load_ins);
14258 use_ins = load_ins;
14263 #if SIZEOF_REGISTER == 4
14264 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14266 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14267 mono_bblock_insert_before_ins (bb, ins, load_ins);
14268 use_ins = load_ins;
14272 if (var->dreg < orig_next_vreg) {
14273 live_range_end [var->dreg] = use_ins;
14274 live_range_end_bb [var->dreg] = bb;
14277 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14280 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14281 tmp->inst_c1 = var->dreg;
14282 mono_bblock_insert_after_ins (bb, ins, tmp);
14286 mono_inst_set_src_registers (ins, sregs);
14288 if (dest_has_lvreg) {
14289 g_assert (ins->dreg != -1);
14290 vreg_to_lvreg [prev_dreg] = ins->dreg;
14291 g_assert (lvregs_len < 1024);
14292 lvregs [lvregs_len ++] = prev_dreg;
14293 dest_has_lvreg = FALSE;
14297 tmp_reg = ins->dreg;
14298 ins->dreg = ins->sreg2;
14299 ins->sreg2 = tmp_reg;
14302 if (MONO_IS_CALL (ins)) {
14303 /* Clear vreg_to_lvreg array */
14304 for (i = 0; i < lvregs_len; i++)
14305 vreg_to_lvreg [lvregs [i]] = 0;
14307 } else if (ins->opcode == OP_NOP) {
14309 MONO_INST_NULLIFY_SREGS (ins);
14312 if (cfg->verbose_level > 2)
14313 mono_print_ins_index (1, ins);
14316 /* Extend the live range based on the liveness info */
14317 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14318 for (i = 0; i < cfg->num_varinfo; i ++) {
14319 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14321 if (vreg_is_volatile (cfg, vi->vreg))
14322 /* The liveness info is incomplete */
14325 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14326 /* Live from at least the first ins of this bb */
14327 live_range_start [vi->vreg] = bb->code;
14328 live_range_start_bb [vi->vreg] = bb;
14331 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14332 /* Live at least until the last ins of this bb */
14333 live_range_end [vi->vreg] = bb->last_ins;
14334 live_range_end_bb [vi->vreg] = bb;
14340 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
14342 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14343 * by storing the current native offset into MonoMethodVar->live_range_start/end.
14345 if (cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14346 for (i = 0; i < cfg->num_varinfo; ++i) {
14347 int vreg = MONO_VARINFO (cfg, i)->vreg;
14350 if (live_range_start [vreg]) {
14351 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14353 ins->inst_c1 = vreg;
14354 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14356 if (live_range_end [vreg]) {
14357 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14359 ins->inst_c1 = vreg;
14360 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14361 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14363 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14369 if (cfg->gsharedvt_locals_var_ins) {
14370 /* Nullify if unused */
14371 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14372 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14375 g_free (live_range_start);
14376 g_free (live_range_end);
14377 g_free (live_range_start_bb);
14378 g_free (live_range_end_bb);
14383 * - use 'iadd' instead of 'int_add'
14384 * - handling ovf opcodes: decompose in method_to_ir.
14385 * - unify iregs/fregs
14386 * -> partly done, the missing parts are:
14387 * - a more complete unification would involve unifying the hregs as well, so
14388 * code wouldn't need if (fp) all over the place. but that would mean the hregs
14389 * would no longer map to the machine hregs, so the code generators would need to
14390 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14391 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14392 * fp/non-fp branches speeds it up by about 15%.
14393 * - use sext/zext opcodes instead of shifts
14395 * - get rid of TEMPLOADs if possible and use vregs instead
14396 * - clean up usage of OP_P/OP_ opcodes
14397 * - cleanup usage of DUMMY_USE
14398 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
14400 * - set the stack type and allocate a dreg in the EMIT_NEW macros
14401 * - get rid of all the <foo>2 stuff when the new JIT is ready.
14402 * - make sure handle_stack_args () is called before the branch is emitted
14403 * - when the new IR is done, get rid of all unused stuff
14404 * - COMPARE/BEQ as separate instructions or unify them ?
14405 * - keeping them separate allows specialized compare instructions like
14406 * compare_imm, compare_membase
14407 * - most back ends unify fp compare+branch, fp compare+ceq
14408 * - integrate mono_save_args into inline_method
14409 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14410 * - handle long shift opts on 32 bit platforms somehow: they require
14411 * 3 sregs (2 for arg1 and 1 for arg2)
14412 * - make byref a 'normal' type.
14413 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14414 * variable if needed.
14415 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14416 * like inline_method.
14417 * - remove inlining restrictions
14418 * - fix LNEG and enable cfold of INEG
14419 * - generalize x86 optimizations like ldelema as a peephole optimization
14420 * - add store_mem_imm for amd64
14421 * - optimize the loading of the interruption flag in the managed->native wrappers
14422 * - avoid special handling of OP_NOP in passes
14423 * - move code inserting instructions into one function/macro.
14424 * - try a coalescing phase after liveness analysis
14425 * - add float -> vreg conversion + local optimizations on !x86
14426 * - figure out how to handle decomposed branches during optimizations, ie.
14427 * compare+branch, op_jump_table+op_br etc.
14428 * - promote RuntimeXHandles to vregs
14429 * - vtype cleanups:
14430 * - add a NEW_VARLOADA_VREG macro
14431 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
14432 * accessing vtype fields.
14433 * - get rid of I8CONST on 64 bit platforms
14434 * - dealing with the increase in code size due to branches created during opcode
14436 * - use extended basic blocks
14437 * - all parts of the JIT
14438 * - handle_global_vregs () && local regalloc
14439 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14440 * - sources of increase in code size:
14443 * - isinst and castclass
14444 * - lvregs not allocated to global registers even if used multiple times
14445 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14447 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14448 * - add all micro optimizations from the old JIT
14449 * - put tree optimizations into the deadce pass
14450 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14451 * specific function.
14452 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14453 * fcompare + branchCC.
14454 * - create a helper function for allocating a stack slot, taking into account
14455 * MONO_CFG_HAS_SPILLUP.
14457 * - merge the ia64 switch changes.
14458 * - optimize mono_regstate2_alloc_int/float.
14459 * - fix the pessimistic handling of variables accessed in exception handler blocks.
14460 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14461 * parts of the tree could be separated by other instructions, killing the tree
14462 * arguments, or stores killing loads etc. Also, should we fold loads into other
14463 * instructions if the result of the load is used multiple times ?
14464 * - make the REM_IMM optimization in mini-x86.c arch-independent.
14465 * - LAST MERGE: 108395.
14466 * - when returning vtypes in registers, generate IR and append it to the end of the
14467 * last bb instead of doing it in the epilog.
14468 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14476 - When to decompose opcodes:
14477 - earlier: this makes some optimizations hard to implement, since the low level IR
14478 no longer contains the neccessary information. But it is easier to do.
14479 - later: harder to implement, enables more optimizations.
14480 - Branches inside bblocks:
14481 - created when decomposing complex opcodes.
14482 - branches to another bblock: harmless, but not tracked by the branch
14483 optimizations, so need to branch to a label at the start of the bblock.
14484 - branches to inside the same bblock: very problematic, trips up the local
14485 reg allocator. Can be fixed by spitting the current bblock, but that is a
14486 complex operation, since some local vregs can become global vregs etc.
14487 - Local/global vregs:
14488 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14489 local register allocator.
14490 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14491 structure, created by mono_create_var (). Assigned to hregs or the stack by
14492 the global register allocator.
14493 - When to do optimizations like alu->alu_imm:
14494 - earlier -> saves work later on since the IR will be smaller/simpler
14495 - later -> can work on more instructions
14496 - Handling of valuetypes:
14497 - When a vtype is pushed on the stack, a new temporary is created, an
14498 instruction computing its address (LDADDR) is emitted and pushed on
14499 the stack. Need to optimize cases when the vtype is used immediately as in
14500 argument passing, stloc etc.
14501 - Instead of the to_end stuff in the old JIT, simply call the function handling
14502 the values on the stack before emitting the last instruction of the bb.
14505 #endif /* DISABLE_JIT */