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.
20 #ifdef HAVE_SYS_TIME_H
24 #ifdef HAVE_VALGRIND_MEMCHECK_H
25 #include <valgrind/memcheck.h>
28 #include <mono/metadata/assembly.h>
29 #include <mono/metadata/loader.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/class.h>
32 #include <mono/metadata/object.h>
33 #include <mono/metadata/exception.h>
34 #include <mono/metadata/opcodes.h>
35 #include <mono/metadata/mono-endian.h>
36 #include <mono/metadata/tokentype.h>
37 #include <mono/metadata/tabledefs.h>
38 #include <mono/metadata/threads.h>
39 #include <mono/metadata/marshal.h>
40 #include <mono/metadata/socket-io.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/debug-helpers.h>
43 #include <mono/io-layer/io-layer.h>
44 #include "mono/metadata/profiler.h"
45 #include <mono/metadata/profiler-private.h>
46 #include <mono/metadata/mono-config.h>
47 #include <mono/metadata/environment.h>
48 #include <mono/metadata/mono-debug.h>
49 #include <mono/metadata/mono-debug-debugger.h>
50 #include <mono/metadata/monitor.h>
51 #include <mono/metadata/gc-internal.h>
52 #include <mono/metadata/security-manager.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/metadata/rawbuffer.h>
55 #include <mono/metadata/security-core-clr.h>
56 #include <mono/utils/mono-math.h>
57 #include <mono/utils/mono-compiler.h>
58 #include <mono/os/gc_wrapper.h>
68 #include "jit-icalls.h"
72 #define BRANCH_COST 100
73 #define INLINE_LENGTH_LIMIT 20
74 #define INLINE_FAILURE do {\
75 if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
78 #define CHECK_CFG_EXCEPTION do {\
79 if (cfg->exception_type != MONO_EXCEPTION_NONE)\
82 #define METHOD_ACCESS_FAILURE do { \
83 char *method_fname = mono_method_full_name (method, TRUE); \
84 char *cil_method_fname = mono_method_full_name (cil_method, TRUE); \
85 cfg->exception_type = MONO_EXCEPTION_METHOD_ACCESS; \
86 cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname); \
87 g_free (method_fname); \
88 g_free (cil_method_fname); \
89 goto exception_exit; \
91 #define FIELD_ACCESS_FAILURE do { \
92 char *method_fname = mono_method_full_name (method, TRUE); \
93 char *field_fname = mono_field_full_name (field); \
94 cfg->exception_type = MONO_EXCEPTION_FIELD_ACCESS; \
95 cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname); \
96 g_free (method_fname); \
97 g_free (field_fname); \
98 goto exception_exit; \
100 #define GENERIC_SHARING_FAILURE(opcode) do { \
101 if (cfg->generic_sharing_context) { \
102 if (cfg->verbose_level > 1) \
103 printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", method->klass->name_space, method->klass->name, method->name, method->signature->param_count, mono_opcode_name ((opcode)), __LINE__); \
104 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED; \
105 goto exception_exit; \
108 #define GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(opcode) do { \
109 if (method->klass->valuetype) \
110 GENERIC_SHARING_FAILURE ((opcode)); \
113 /* Determine whenever 'ins' represents a load of the 'this' argument */
114 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
116 static int ldind_to_load_membase (int opcode);
117 static int stind_to_store_membase (int opcode);
119 int mono_op_to_op_imm (int opcode);
120 int mono_op_to_op_imm_noemul (int opcode);
122 MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
123 void mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native);
124 void mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass);
126 int mono_method_to_ir2 (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
127 MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
128 guint inline_offset, gboolean is_virtual_call);
130 /* helper methods signature */
131 extern MonoMethodSignature *helper_sig_class_init_trampoline;
132 extern MonoMethodSignature *helper_sig_domain_get;
133 extern MonoMethodSignature *helper_sig_generic_class_init_trampoline;
134 extern MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
137 * Instruction metadata
142 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2,
147 #if SIZEOF_VOID_P == 8
152 /* keep in sync with the enum in mini.h */
155 #include "mini-ops.h"
159 extern GHashTable *jit_icall_name_hash;
161 #define MONO_INIT_VARINFO(vi,id) do { \
162 (vi)->range.first_use.pos.bid = 0xffff; \
168 mono_alloc_ireg (MonoCompile *cfg)
170 return alloc_ireg (cfg);
174 mono_alloc_freg (MonoCompile *cfg)
176 return alloc_freg (cfg);
180 mono_alloc_preg (MonoCompile *cfg)
182 return alloc_preg (cfg);
186 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
188 return alloc_dreg (cfg, stack_type);
192 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
198 switch (type->type) {
201 case MONO_TYPE_BOOLEAN:
213 case MONO_TYPE_FNPTR:
215 case MONO_TYPE_CLASS:
216 case MONO_TYPE_STRING:
217 case MONO_TYPE_OBJECT:
218 case MONO_TYPE_SZARRAY:
219 case MONO_TYPE_ARRAY:
223 #if SIZEOF_VOID_P == 8
232 case MONO_TYPE_VALUETYPE:
233 if (type->data.klass->enumtype) {
234 type = type->data.klass->enum_basetype;
238 case MONO_TYPE_TYPEDBYREF:
240 case MONO_TYPE_GENERICINST:
241 type = &type->data.generic_class->container_class->byval_arg;
245 g_assert (cfg->generic_sharing_context);
248 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
254 mono_print_bb (MonoBasicBlock *bb, const char *msg)
259 printf ("\n%s %d: [IN: ", msg, bb->block_num);
260 for (i = 0; i < bb->in_count; ++i)
261 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
263 for (i = 0; i < bb->out_count; ++i)
264 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
266 for (tree = bb->code; tree; tree = tree->next)
267 mono_print_ins_index (-1, tree);
270 #define UNVERIFIED do { if (mini_get_debug_options ()->break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
272 #define GET_BBLOCK(cfg,tblock,ip) do { \
273 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
275 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
276 NEW_BBLOCK (cfg, (tblock)); \
277 (tblock)->cil_code = (ip); \
278 ADD_BBLOCK (cfg, (tblock)); \
282 #define CHECK_BBLOCK(target,ip,tblock) do { \
283 if ((target) < (ip) && !(tblock)->code) { \
284 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
285 if (cfg->verbose_level > 2) printf ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (int)((target) - header->code), (int)((ip) - header->code)); \
289 #ifndef MONO_ARCH_EMIT_BOUNDS_CHECK
290 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
291 int _length_reg = alloc_ireg (cfg); \
292 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
293 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
294 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
298 #define MONO_EMIT_BOUNDS_CHECK(cfg, array_reg, array_type, array_length_field, index_reg) do { \
299 if (!(cfg->opt & MONO_OPT_ABCREM)) { \
300 MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
303 MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK); \
304 ins->sreg1 = array_reg; \
305 ins->sreg2 = index_reg; \
306 ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
307 MONO_ADD_INS ((cfg)->cbb, ins); \
308 (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS; \
309 (cfg)->cbb->has_array_access = TRUE; \
313 #if defined(__i386__) || defined(__x86_64__)
314 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
315 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
316 (dest)->dreg = alloc_preg ((cfg)); \
317 (dest)->sreg1 = (sr1); \
318 (dest)->sreg2 = (sr2); \
319 (dest)->inst_imm = (imm); \
320 (dest)->backend.shift_amount = (shift); \
321 MONO_ADD_INS ((cfg)->cbb, (dest)); \
325 #if SIZEOF_VOID_P == 8
326 #define ADD_WIDEN_OP(ins, arg1, arg2) do { \
327 /* FIXME: Need to add many more cases */ \
328 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) { \
330 int dr = alloc_preg (cfg); \
331 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg); \
332 (ins)->sreg2 = widen->dreg; \
336 #define ADD_WIDEN_OP(ins, arg1, arg2)
339 #define ADD_BINOP(op) do { \
340 MONO_INST_NEW (cfg, ins, (op)); \
342 ins->sreg1 = sp [0]->dreg; \
343 ins->sreg2 = sp [1]->dreg; \
344 type_from_op (ins, sp [0], sp [1]); \
346 /* Have to insert a widening op */ \
347 ADD_WIDEN_OP (ins, sp [0], sp [1]); \
348 ins->dreg = alloc_dreg ((cfg), (ins)->type); \
349 MONO_ADD_INS ((cfg)->cbb, (ins)); \
351 mono_decompose_opcode ((cfg), (ins)); \
354 #define ADD_UNOP(op) do { \
355 MONO_INST_NEW (cfg, ins, (op)); \
357 ins->sreg1 = sp [0]->dreg; \
358 type_from_op (ins, sp [0], NULL); \
360 (ins)->dreg = alloc_dreg ((cfg), (ins)->type); \
361 MONO_ADD_INS ((cfg)->cbb, (ins)); \
363 mono_decompose_opcode (cfg, ins); \
366 #define ADD_BINCOND(next_block) do { \
369 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
370 cmp->sreg1 = sp [0]->dreg; \
371 cmp->sreg2 = sp [1]->dreg; \
372 type_from_op (cmp, sp [0], sp [1]); \
374 type_from_op (ins, sp [0], sp [1]); \
375 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
376 GET_BBLOCK (cfg, tblock, target); \
377 link_bblock (cfg, bblock, tblock); \
378 ins->inst_true_bb = tblock; \
379 CHECK_BBLOCK (target, ip, tblock); \
380 if ((next_block)) { \
381 link_bblock (cfg, bblock, (next_block)); \
382 ins->inst_false_bb = (next_block); \
383 start_new_bblock = 1; \
385 GET_BBLOCK (cfg, tblock, ip); \
386 link_bblock (cfg, bblock, tblock); \
387 ins->inst_false_bb = tblock; \
388 start_new_bblock = 2; \
390 if (sp != stack_start) { \
391 handle_stack_args (cfg, stack_start, sp - stack_start); \
392 CHECK_UNVERIFIABLE (cfg); \
394 MONO_ADD_INS (bblock, cmp); \
395 MONO_ADD_INS (bblock, ins); \
399 * link_bblock: Links two basic blocks
401 * links two basic blocks in the control flow graph, the 'from'
402 * argument is the starting block and the 'to' argument is the block
403 * the control flow ends to after 'from'.
406 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
408 MonoBasicBlock **newa;
412 if (from->cil_code) {
414 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
416 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
419 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
421 printf ("edge from entry to exit\n");
426 for (i = 0; i < from->out_count; ++i) {
427 if (to == from->out_bb [i]) {
433 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
434 for (i = 0; i < from->out_count; ++i) {
435 newa [i] = from->out_bb [i];
443 for (i = 0; i < to->in_count; ++i) {
444 if (from == to->in_bb [i]) {
450 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
451 for (i = 0; i < to->in_count; ++i) {
452 newa [i] = to->in_bb [i];
461 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
463 link_bblock (cfg, from, to);
467 * mono_find_block_region:
469 * We mark each basic block with a region ID. We use that to avoid BB
470 * optimizations when blocks are in different regions.
473 * A region token that encodes where this region is, and information
474 * about the clause owner for this block.
476 * The region encodes the try/catch/filter clause that owns this block
477 * as well as the type. -1 is a special value that represents a block
478 * that is in none of try/catch/filter.
481 mono_find_block_region (MonoCompile *cfg, int offset)
483 MonoMethod *method = cfg->method;
484 MonoMethodHeader *header = mono_method_get_header (method);
485 MonoExceptionClause *clause;
488 /* first search for handlers and filters */
489 for (i = 0; i < header->num_clauses; ++i) {
490 clause = &header->clauses [i];
491 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
492 (offset < (clause->handler_offset)))
493 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
495 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
496 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
497 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
498 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
499 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
501 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
505 /* search the try blocks */
506 for (i = 0; i < header->num_clauses; ++i) {
507 clause = &header->clauses [i];
508 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
509 return ((i + 1) << 8) | clause->flags;
516 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
518 MonoMethod *method = cfg->method;
519 MonoMethodHeader *header = mono_method_get_header (method);
520 MonoExceptionClause *clause;
521 MonoBasicBlock *handler;
525 for (i = 0; i < header->num_clauses; ++i) {
526 clause = &header->clauses [i];
527 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
528 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
529 if (clause->flags == type) {
530 handler = cfg->cil_offset_to_bb [clause->handler_offset];
532 res = g_list_append (res, handler);
540 mono_create_spvar_for_region (MonoCompile *cfg, int region)
544 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
548 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
549 /* prevent it from being register allocated */
550 var->flags |= MONO_INST_INDIRECT;
552 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
556 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
558 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
562 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
566 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
570 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
571 /* prevent it from being register allocated */
572 var->flags |= MONO_INST_INDIRECT;
574 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
579 static MonoBasicBlock*
580 find_previous (MonoBasicBlock **bblocks, guint32 n_bblocks, MonoBasicBlock *start, const guchar *code)
582 MonoBasicBlock *best = start;
585 for (i = 0; i < n_bblocks; ++i) {
587 MonoBasicBlock *bb = bblocks [i];
589 if (bb->cil_code && bb->cil_code < code && bb->cil_code > best->cil_code)
598 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
607 * FIXME: take into account all the details:
608 * second may have been the target of more than one bblock
610 second->out_count = first->out_count;
611 second->out_bb = first->out_bb;
613 for (i = 0; i < first->out_count; ++i) {
614 bb = first->out_bb [i];
615 for (j = 0; j < bb->in_count; ++j) {
616 if (bb->in_bb [j] == first)
617 bb->in_bb [j] = second;
621 first->out_count = 0;
622 first->out_bb = NULL;
623 link_bblock (cfg, first, second);
625 second->last_ins = first->last_ins;
627 /*printf ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
628 for (inst = first->code; inst && inst->next; inst = inst->next) {
629 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
630 printf ("found %p: %s", inst->next->cil_code, code);
632 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
633 second->code = inst->next;
635 first->last_ins = inst;
636 second->next_bb = first->next_bb;
637 first->next_bb = second;
642 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
648 * Returns the type used in the eval stack when @type is loaded.
649 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
652 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
657 inst->type = STACK_MP;
658 inst->klass = mono_defaults.object_class;
662 inst->klass = klass = mono_class_from_mono_type (type);
665 switch (type->type) {
667 inst->type = STACK_INV;
671 case MONO_TYPE_BOOLEAN:
677 inst->type = STACK_I4;
682 case MONO_TYPE_FNPTR:
683 inst->type = STACK_PTR;
685 case MONO_TYPE_CLASS:
686 case MONO_TYPE_STRING:
687 case MONO_TYPE_OBJECT:
688 case MONO_TYPE_SZARRAY:
689 case MONO_TYPE_ARRAY:
690 inst->type = STACK_OBJ;
694 inst->type = STACK_I8;
698 inst->type = STACK_R8;
700 case MONO_TYPE_VALUETYPE:
701 if (type->data.klass->enumtype) {
702 type = type->data.klass->enum_basetype;
706 inst->type = STACK_VTYPE;
709 case MONO_TYPE_TYPEDBYREF:
710 inst->klass = mono_defaults.typed_reference_class;
711 inst->type = STACK_VTYPE;
713 case MONO_TYPE_GENERICINST:
714 type = &type->data.generic_class->container_class->byval_arg;
717 case MONO_TYPE_MVAR :
718 /* FIXME: all the arguments must be references for now,
719 * later look inside cfg and see if the arg num is
722 g_assert (cfg->generic_sharing_context);
723 inst->type = STACK_OBJ;
726 g_error ("unknown type 0x%02x in eval stack type", type->type);
731 * The following tables are used to quickly validate the IL code in type_from_op ().
734 bin_num_table [STACK_MAX] [STACK_MAX] = {
735 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
736 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
737 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
738 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
739 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
740 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
741 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
742 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
747 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
750 /* reduce the size of this table */
752 bin_int_table [STACK_MAX] [STACK_MAX] = {
753 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
754 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
755 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
756 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
757 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
758 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
759 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
760 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
764 bin_comp_table [STACK_MAX] [STACK_MAX] = {
765 /* Inv i L p F & O vt */
767 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
768 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
769 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
770 {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
771 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
772 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
773 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
776 /* reduce the size of this table */
778 shift_table [STACK_MAX] [STACK_MAX] = {
779 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
780 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
781 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
782 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
783 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
784 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
785 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
786 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
790 * Tables to map from the non-specific opcode to the matching
791 * type-specific opcode.
793 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
795 binops_op_map [STACK_MAX] = {
796 0, OP_IADD-CEE_ADD, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
799 /* handles from CEE_NEG to CEE_CONV_U8 */
801 unops_op_map [STACK_MAX] = {
802 0, OP_INEG-CEE_NEG, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
805 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
807 ovfops_op_map [STACK_MAX] = {
808 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
811 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
813 ovf2ops_op_map [STACK_MAX] = {
814 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
817 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
819 ovf3ops_op_map [STACK_MAX] = {
820 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
823 /* handles from CEE_BEQ to CEE_BLT_UN */
825 beqops_op_map [STACK_MAX] = {
826 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
829 /* handles from CEE_CEQ to CEE_CLT_UN */
831 ceqops_op_map [STACK_MAX] = {
832 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
836 * Sets ins->type (the type on the eval stack) according to the
837 * type of the opcode and the arguments to it.
838 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
840 * FIXME: this function sets ins->type unconditionally in some cases, but
841 * it should set it to invalid for some types (a conv.x on an object)
844 type_from_op (MonoInst *ins, MonoInst *src1, MonoInst *src2) {
846 switch (ins->opcode) {
853 /* FIXME: check unverifiable args for STACK_MP */
854 ins->type = bin_num_table [src1->type] [src2->type];
855 ins->opcode += binops_op_map [ins->type];
862 ins->type = bin_int_table [src1->type] [src2->type];
863 ins->opcode += binops_op_map [ins->type];
868 ins->type = shift_table [src1->type] [src2->type];
869 ins->opcode += binops_op_map [ins->type];
874 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
875 if ((src1->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
876 ins->opcode = OP_LCOMPARE;
877 else if (src1->type == STACK_R8)
878 ins->opcode = OP_FCOMPARE;
880 ins->opcode = OP_ICOMPARE;
882 case OP_ICOMPARE_IMM:
883 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
884 if ((src1->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
885 ins->opcode = OP_LCOMPARE_IMM;
897 ins->opcode += beqops_op_map [src1->type];
900 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
901 ins->opcode += ceqops_op_map [src1->type];
907 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
908 ins->opcode += ceqops_op_map [src1->type];
912 ins->type = neg_table [src1->type];
913 ins->opcode += unops_op_map [ins->type];
916 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
917 ins->type = src1->type;
919 ins->type = STACK_INV;
920 ins->opcode += unops_op_map [ins->type];
926 ins->type = STACK_I4;
927 ins->opcode += unops_op_map [src1->type];
930 ins->type = STACK_R8;
931 switch (src1->type) {
934 ins->opcode = OP_ICONV_TO_R_UN;
937 ins->opcode = OP_LCONV_TO_R_UN;
941 case CEE_CONV_OVF_I1:
942 case CEE_CONV_OVF_U1:
943 case CEE_CONV_OVF_I2:
944 case CEE_CONV_OVF_U2:
945 case CEE_CONV_OVF_I4:
946 case CEE_CONV_OVF_U4:
947 ins->type = STACK_I4;
948 ins->opcode += ovf3ops_op_map [src1->type];
950 case CEE_CONV_OVF_I_UN:
951 case CEE_CONV_OVF_U_UN:
952 ins->type = STACK_PTR;
953 ins->opcode += ovf2ops_op_map [src1->type];
955 case CEE_CONV_OVF_I1_UN:
956 case CEE_CONV_OVF_I2_UN:
957 case CEE_CONV_OVF_I4_UN:
958 case CEE_CONV_OVF_U1_UN:
959 case CEE_CONV_OVF_U2_UN:
960 case CEE_CONV_OVF_U4_UN:
961 ins->type = STACK_I4;
962 ins->opcode += ovf2ops_op_map [src1->type];
965 ins->type = STACK_PTR;
966 switch (src1->type) {
968 ins->opcode = OP_MOVE;
972 #if SIZEOF_VOID_P == 8
973 ins->opcode = OP_LCONV_TO_U;
975 ins->opcode = OP_MOVE;
979 ins->opcode = OP_LCONV_TO_U;
982 ins->opcode = OP_FCONV_TO_U;
988 ins->type = STACK_I8;
989 ins->opcode += unops_op_map [src1->type];
991 case CEE_CONV_OVF_I8:
992 case CEE_CONV_OVF_U8:
993 ins->type = STACK_I8;
994 ins->opcode += ovf3ops_op_map [src1->type];
996 case CEE_CONV_OVF_U8_UN:
997 case CEE_CONV_OVF_I8_UN:
998 ins->type = STACK_I8;
999 ins->opcode += ovf2ops_op_map [src1->type];
1003 ins->type = STACK_R8;
1004 ins->opcode += unops_op_map [src1->type];
1007 ins->type = STACK_R8;
1011 ins->type = STACK_I4;
1012 ins->opcode += ovfops_op_map [src1->type];
1015 case CEE_CONV_OVF_I:
1016 case CEE_CONV_OVF_U:
1017 ins->type = STACK_PTR;
1018 ins->opcode += ovfops_op_map [src1->type];
1021 case CEE_ADD_OVF_UN:
1023 case CEE_MUL_OVF_UN:
1025 case CEE_SUB_OVF_UN:
1026 ins->type = bin_num_table [src1->type] [src2->type];
1027 ins->opcode += ovfops_op_map [src1->type];
1028 if (ins->type == STACK_R8)
1029 ins->type = STACK_INV;
1031 case OP_LOAD_MEMBASE:
1032 ins->type = STACK_PTR;
1034 case OP_LOADI1_MEMBASE:
1035 case OP_LOADU1_MEMBASE:
1036 case OP_LOADI2_MEMBASE:
1037 case OP_LOADU2_MEMBASE:
1038 case OP_LOADI4_MEMBASE:
1039 case OP_LOADU4_MEMBASE:
1040 ins->type = STACK_PTR;
1042 case OP_LOADI8_MEMBASE:
1043 ins->type = STACK_I8;
1045 case OP_LOADR4_MEMBASE:
1046 case OP_LOADR8_MEMBASE:
1047 ins->type = STACK_R8;
1050 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1054 if (ins->type == STACK_MP)
1055 ins->klass = mono_defaults.object_class;
1060 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1066 param_table [STACK_MAX] [STACK_MAX] = {
1071 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1075 switch (args->type) {
1085 for (i = 0; i < sig->param_count; ++i) {
1086 switch (args [i].type) {
1090 if (!sig->params [i]->byref)
1094 if (sig->params [i]->byref)
1096 switch (sig->params [i]->type) {
1097 case MONO_TYPE_CLASS:
1098 case MONO_TYPE_STRING:
1099 case MONO_TYPE_OBJECT:
1100 case MONO_TYPE_SZARRAY:
1101 case MONO_TYPE_ARRAY:
1108 if (sig->params [i]->byref)
1110 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1119 /*if (!param_table [args [i].type] [sig->params [i]->type])
1127 * When we need a pointer to the current domain many times in a method, we
1128 * call mono_domain_get() once and we store the result in a local variable.
1129 * This function returns the variable that represents the MonoDomain*.
1131 inline static MonoInst *
1132 mono_get_domainvar (MonoCompile *cfg)
1134 if (!cfg->domainvar)
1135 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1136 return cfg->domainvar;
1140 * The got_var contains the address of the Global Offset Table when AOT
1143 inline static MonoInst *
1144 mono_get_got_var (MonoCompile *cfg)
1146 #ifdef MONO_ARCH_NEED_GOT_VAR
1147 if (!cfg->compile_aot)
1149 if (!cfg->got_var) {
1150 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1152 return cfg->got_var;
1159 mono_get_vtable_var (MonoCompile *cfg)
1161 g_assert (cfg->generic_sharing_context);
1163 if (!cfg->rgctx_var) {
1164 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1165 /* force the var to be stack allocated */
1166 cfg->rgctx_var->flags |= MONO_INST_INDIRECT;
1169 return cfg->rgctx_var;
1173 type_from_stack_type (MonoInst *ins) {
1174 switch (ins->type) {
1175 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1176 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1177 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1178 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1180 return &ins->klass->this_arg;
1181 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1182 case STACK_VTYPE: return &ins->klass->byval_arg;
1184 g_error ("stack type %d to montype not handled\n", ins->type);
1189 static G_GNUC_UNUSED int
1190 type_to_stack_type (MonoType *t)
1192 switch (mono_type_get_underlying_type (t)->type) {
1195 case MONO_TYPE_BOOLEAN:
1198 case MONO_TYPE_CHAR:
1205 case MONO_TYPE_FNPTR:
1207 case MONO_TYPE_CLASS:
1208 case MONO_TYPE_STRING:
1209 case MONO_TYPE_OBJECT:
1210 case MONO_TYPE_SZARRAY:
1211 case MONO_TYPE_ARRAY:
1219 case MONO_TYPE_VALUETYPE:
1220 case MONO_TYPE_TYPEDBYREF:
1222 case MONO_TYPE_GENERICINST:
1223 if (mono_type_generic_inst_is_valuetype (t))
1229 g_assert_not_reached ();
1236 array_access_to_klass (int opcode)
1240 return mono_defaults.byte_class;
1242 return mono_defaults.uint16_class;
1245 return mono_defaults.int_class;
1248 return mono_defaults.sbyte_class;
1251 return mono_defaults.int16_class;
1254 return mono_defaults.int32_class;
1256 return mono_defaults.uint32_class;
1259 return mono_defaults.int64_class;
1262 return mono_defaults.single_class;
1265 return mono_defaults.double_class;
1266 case CEE_LDELEM_REF:
1267 case CEE_STELEM_REF:
1268 return mono_defaults.object_class;
1270 g_assert_not_reached ();
1276 * We try to share variables when possible
1279 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1284 /* inlining can result in deeper stacks */
1285 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1286 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1288 pos = ins->type - 1 + slot * STACK_MAX;
1290 switch (ins->type) {
1297 if ((vnum = cfg->intvars [pos]))
1298 return cfg->varinfo [vnum];
1299 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1300 cfg->intvars [pos] = res->inst_c0;
1303 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1309 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1311 if (cfg->compile_aot) {
1312 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1313 jump_info_token->image = image;
1314 jump_info_token->token = token;
1315 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1320 * This function is called to handle items that are left on the evaluation stack
1321 * at basic block boundaries. What happens is that we save the values to local variables
1322 * and we reload them later when first entering the target basic block (with the
1323 * handle_loaded_temps () function).
1324 * A single joint point will use the same variables (stored in the array bb->out_stack or
1325 * bb->in_stack, if the basic block is before or after the joint point).
1327 * This function needs to be called _before_ emitting the last instruction of
1328 * the bb (i.e. before emitting a branch).
1329 * If the stack merge fails at a join point, cfg->unverifiable is set.
1332 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1335 MonoBasicBlock *bb = cfg->cbb;
1336 MonoBasicBlock *outb;
1337 MonoInst *inst, **locals;
1342 if (cfg->verbose_level > 3)
1343 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1344 if (!bb->out_scount) {
1345 bb->out_scount = count;
1346 //printf ("bblock %d has out:", bb->block_num);
1348 for (i = 0; i < bb->out_count; ++i) {
1349 outb = bb->out_bb [i];
1350 /* exception handlers are linked, but they should not be considered for stack args */
1351 if (outb->flags & BB_EXCEPTION_HANDLER)
1353 //printf (" %d", outb->block_num);
1354 if (outb->in_stack) {
1356 bb->out_stack = outb->in_stack;
1362 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1363 for (i = 0; i < count; ++i) {
1365 * try to reuse temps already allocated for this purpouse, if they occupy the same
1366 * stack slot and if they are of the same type.
1367 * This won't cause conflicts since if 'local' is used to
1368 * store one of the values in the in_stack of a bblock, then
1369 * the same variable will be used for the same outgoing stack
1371 * This doesn't work when inlining methods, since the bblocks
1372 * in the inlined methods do not inherit their in_stack from
1373 * the bblock they are inlined to. See bug #58863 for an
1376 if (cfg->inlined_method)
1377 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1379 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1384 for (i = 0; i < bb->out_count; ++i) {
1385 outb = bb->out_bb [i];
1386 /* exception handlers are linked, but they should not be considered for stack args */
1387 if (outb->flags & BB_EXCEPTION_HANDLER)
1389 if (outb->in_scount) {
1390 if (outb->in_scount != bb->out_scount) {
1391 cfg->unverifiable = TRUE;
1394 continue; /* check they are the same locals */
1396 outb->in_scount = count;
1397 outb->in_stack = bb->out_stack;
1400 locals = bb->out_stack;
1402 for (i = 0; i < count; ++i) {
1403 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1404 inst->cil_code = sp [i]->cil_code;
1405 sp [i] = locals [i];
1406 if (cfg->verbose_level > 3)
1407 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1411 * It is possible that the out bblocks already have in_stack assigned, and
1412 * the in_stacks differ. In this case, we will store to all the different
1419 /* Find a bblock which has a different in_stack */
1421 while (bindex < bb->out_count) {
1422 outb = bb->out_bb [bindex];
1423 /* exception handlers are linked, but they should not be considered for stack args */
1424 if (outb->flags & BB_EXCEPTION_HANDLER) {
1428 if (outb->in_stack != locals) {
1429 for (i = 0; i < count; ++i) {
1430 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1431 inst->cil_code = sp [i]->cil_code;
1432 sp [i] = locals [i];
1433 if (cfg->verbose_level > 3)
1434 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1436 locals = outb->in_stack;
1445 /* Emit code which loads interface_offsets [klass->interface_id]
1446 * The array is stored in memory before vtable.
1449 mini_emit_load_intf_reg_vtable (MonoCompile *cfg, int intf_reg, int vtable_reg, MonoClass *klass)
1451 if (cfg->compile_aot) {
1452 int ioffset_reg = alloc_preg (cfg);
1453 int iid_reg = alloc_preg (cfg);
1455 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_ADJUSTED_IID);
1456 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ioffset_reg, iid_reg, vtable_reg);
1457 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, intf_reg, ioffset_reg, 0);
1460 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, intf_reg, vtable_reg, -((klass->interface_id + 1) * SIZEOF_VOID_P));
1465 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1466 * stored in "klass_reg" implements the interface "klass".
1469 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1471 int ibitmap_reg = alloc_preg (cfg);
1472 int ibitmap_byte_reg = alloc_preg (cfg);
1474 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, interface_bitmap));
1476 if (cfg->compile_aot) {
1477 int iid_reg = alloc_preg (cfg);
1478 int shifted_iid_reg = alloc_preg (cfg);
1479 int ibitmap_byte_address_reg = alloc_preg (cfg);
1480 int masked_iid_reg = alloc_preg (cfg);
1481 int iid_one_bit_reg = alloc_preg (cfg);
1482 int iid_bit_reg = alloc_preg (cfg);
1483 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1484 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1485 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1486 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1487 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1488 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1489 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1490 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1492 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1493 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1498 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1499 * stored in "vtable_reg" implements the interface "klass".
1502 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1504 int ibitmap_reg = alloc_preg (cfg);
1505 int ibitmap_byte_reg = alloc_preg (cfg);
1507 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, interface_bitmap));
1509 if (cfg->compile_aot) {
1510 int iid_reg = alloc_preg (cfg);
1511 int shifted_iid_reg = alloc_preg (cfg);
1512 int ibitmap_byte_address_reg = alloc_preg (cfg);
1513 int masked_iid_reg = alloc_preg (cfg);
1514 int iid_one_bit_reg = alloc_preg (cfg);
1515 int iid_bit_reg = alloc_preg (cfg);
1516 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1517 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_IMM, shifted_iid_reg, iid_reg, 3);
1518 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1519 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1520 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, masked_iid_reg, iid_reg, 7);
1521 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1522 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1523 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1525 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1526 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1531 * Emit code which checks whenever the interface id of @klass is smaller than
1532 * than the value given by max_iid_reg.
1535 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1536 MonoBasicBlock *false_target)
1538 if (cfg->compile_aot) {
1539 int iid_reg = alloc_preg (cfg);
1540 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1541 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1544 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1546 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1548 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1551 /* Same as above, but obtains max_iid from a vtable */
1553 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1554 MonoBasicBlock *false_target)
1556 int max_iid_reg = alloc_preg (cfg);
1558 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, max_interface_id));
1559 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1562 /* Same as above, but obtains max_iid from a klass */
1564 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1565 MonoBasicBlock *false_target)
1567 int max_iid_reg = alloc_preg (cfg);
1569 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, max_interface_id));
1570 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1574 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1576 int idepth_reg = alloc_preg (cfg);
1577 int stypes_reg = alloc_preg (cfg);
1578 int stype = alloc_preg (cfg);
1580 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1581 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, idepth));
1582 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1583 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1585 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, supertypes));
1586 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1587 if (cfg->compile_aot) {
1588 int const_reg = alloc_preg (cfg);
1589 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1590 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1592 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1594 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1598 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1600 int intf_reg = alloc_preg (cfg);
1602 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1603 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1604 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1606 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1608 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1612 * Variant of the above that takes a register to the class, not the vtable.
1615 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1617 int intf_bit_reg = alloc_preg (cfg);
1619 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1620 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1621 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1623 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1625 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1629 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1631 if (cfg->compile_aot) {
1632 int const_reg = alloc_preg (cfg);
1633 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1634 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1636 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1638 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1642 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1644 if (cfg->compile_aot) {
1645 int const_reg = alloc_preg (cfg);
1646 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1647 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1649 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1651 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1655 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1658 int rank_reg = alloc_preg (cfg);
1659 int eclass_reg = alloc_preg (cfg);
1661 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, rank));
1662 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1663 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1664 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
1665 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, cast_class));
1666 if (klass->cast_class == mono_defaults.object_class) {
1667 int parent_reg = alloc_preg (cfg);
1668 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, G_STRUCT_OFFSET (MonoClass, parent));
1669 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1670 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1671 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1672 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1673 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1674 } else if (klass->cast_class == mono_defaults.enum_class) {
1675 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1676 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1677 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1679 mini_emit_castclass (cfg, obj_reg, eclass_reg, klass->cast_class, object_is_null);
1682 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
1683 /* Check that the object is a vector too */
1684 int bounds_reg = alloc_preg (cfg);
1685 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, G_STRUCT_OFFSET (MonoArray, bounds));
1686 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1687 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1690 int idepth_reg = alloc_preg (cfg);
1691 int stypes_reg = alloc_preg (cfg);
1692 int stype = alloc_preg (cfg);
1694 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1695 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, idepth));
1696 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1697 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1699 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, supertypes));
1700 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1701 mini_emit_class_check (cfg, stype, klass);
1706 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1710 g_assert (val == 0);
1712 if ((size <= 4) && (size <= align)) {
1715 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1718 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1721 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1723 #if SIZEOF_VOID_P == 8
1725 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1731 val_reg = alloc_preg (cfg);
1733 if (sizeof (gpointer) == 8)
1734 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1736 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1739 /* This could be optimized further if neccesary */
1741 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1748 #if !NO_UNALIGNED_ACCESS
1749 if (sizeof (gpointer) == 8) {
1751 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1756 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1764 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1769 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1774 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1781 mini_emit_memcpy2 (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1786 /* This could be optimized further if neccesary */
1788 cur_reg = alloc_preg (cfg);
1789 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1790 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1797 #if !NO_UNALIGNED_ACCESS
1798 if (sizeof (gpointer) == 8) {
1800 cur_reg = alloc_preg (cfg);
1801 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1802 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1811 cur_reg = alloc_preg (cfg);
1812 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1813 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1819 cur_reg = alloc_preg (cfg);
1820 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1821 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1827 cur_reg = alloc_preg (cfg);
1828 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1829 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1837 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
1839 int vtable_reg = alloc_preg (cfg);
1841 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj->dreg, G_STRUCT_OFFSET (MonoObject, vtable));
1843 if (cfg->opt & MONO_OPT_SHARED) {
1844 int class_reg = alloc_preg (cfg);
1845 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
1846 if (cfg->compile_aot) {
1847 int klass_reg = alloc_preg (cfg);
1848 MONO_EMIT_NEW_CLASSCONST (cfg, klass_reg, array_class);
1849 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, klass_reg);
1851 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, class_reg, array_class);
1854 if (cfg->compile_aot) {
1855 int vt_reg = alloc_preg (cfg);
1856 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, mono_class_vtable (cfg->domain, array_class));
1857 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
1859 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, mono_class_vtable (cfg->domain, array_class));
1863 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
1867 ret_type_to_call_opcode (MonoType *type, int calli, int virt, MonoGenericSharingContext *gsctx)
1870 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
1873 type = mini_get_basic_type_from_generic (gsctx, type);
1874 switch (type->type) {
1875 case MONO_TYPE_VOID:
1876 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1879 case MONO_TYPE_BOOLEAN:
1882 case MONO_TYPE_CHAR:
1885 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
1889 case MONO_TYPE_FNPTR:
1890 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
1891 case MONO_TYPE_CLASS:
1892 case MONO_TYPE_STRING:
1893 case MONO_TYPE_OBJECT:
1894 case MONO_TYPE_SZARRAY:
1895 case MONO_TYPE_ARRAY:
1896 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
1899 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1902 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1903 case MONO_TYPE_VALUETYPE:
1904 if (type->data.klass->enumtype) {
1905 type = type->data.klass->enum_basetype;
1908 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1909 case MONO_TYPE_TYPEDBYREF:
1910 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1911 case MONO_TYPE_GENERICINST:
1912 type = &type->data.generic_class->container_class->byval_arg;
1915 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1921 * target_type_is_incompatible:
1922 * @cfg: MonoCompile context
1924 * Check that the item @arg on the evaluation stack can be stored
1925 * in the target type (can be a local, or field, etc).
1926 * The cfg arg can be used to check if we need verification or just
1929 * Returns: non-0 value if arg can't be stored on a target.
1932 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
1934 MonoType *simple_type;
1937 if (target->byref) {
1938 /* FIXME: check that the pointed to types match */
1939 if (arg->type == STACK_MP)
1940 return arg->klass != mono_class_from_mono_type (target);
1941 if (arg->type == STACK_PTR)
1946 simple_type = mono_type_get_underlying_type (target);
1947 switch (simple_type->type) {
1948 case MONO_TYPE_VOID:
1952 case MONO_TYPE_BOOLEAN:
1955 case MONO_TYPE_CHAR:
1958 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
1962 /* STACK_MP is needed when setting pinned locals */
1963 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1968 case MONO_TYPE_FNPTR:
1969 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
1972 case MONO_TYPE_CLASS:
1973 case MONO_TYPE_STRING:
1974 case MONO_TYPE_OBJECT:
1975 case MONO_TYPE_SZARRAY:
1976 case MONO_TYPE_ARRAY:
1977 if (arg->type != STACK_OBJ)
1979 /* FIXME: check type compatibility */
1983 if (arg->type != STACK_I8)
1988 if (arg->type != STACK_R8)
1991 case MONO_TYPE_VALUETYPE:
1992 if (arg->type != STACK_VTYPE)
1994 klass = mono_class_from_mono_type (simple_type);
1995 if (klass != arg->klass)
1998 case MONO_TYPE_TYPEDBYREF:
1999 if (arg->type != STACK_VTYPE)
2001 klass = mono_class_from_mono_type (simple_type);
2002 if (klass != arg->klass)
2005 case MONO_TYPE_GENERICINST:
2006 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2007 if (arg->type != STACK_VTYPE)
2009 klass = mono_class_from_mono_type (simple_type);
2010 if (klass != arg->klass)
2014 if (arg->type != STACK_OBJ)
2016 /* FIXME: check type compatibility */
2020 case MONO_TYPE_MVAR:
2021 /* FIXME: all the arguments must be references for now,
2022 * later look inside cfg and see if the arg num is
2023 * really a reference
2025 g_assert (cfg->generic_sharing_context);
2026 if (arg->type != STACK_OBJ)
2030 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2036 * Prepare arguments for passing to a function call.
2037 * Return a non-zero value if the arguments can't be passed to the given
2039 * The type checks are not yet complete and some conversions may need
2040 * casts on 32 or 64 bit architectures.
2042 * FIXME: implement this using target_type_is_incompatible ()
2045 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2047 MonoType *simple_type;
2051 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2055 for (i = 0; i < sig->param_count; ++i) {
2056 if (sig->params [i]->byref) {
2057 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2061 simple_type = sig->params [i];
2062 simple_type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, simple_type);
2064 switch (simple_type->type) {
2065 case MONO_TYPE_VOID:
2070 case MONO_TYPE_BOOLEAN:
2073 case MONO_TYPE_CHAR:
2076 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2082 case MONO_TYPE_FNPTR:
2083 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2086 case MONO_TYPE_CLASS:
2087 case MONO_TYPE_STRING:
2088 case MONO_TYPE_OBJECT:
2089 case MONO_TYPE_SZARRAY:
2090 case MONO_TYPE_ARRAY:
2091 if (args [i]->type != STACK_OBJ)
2096 if (args [i]->type != STACK_I8)
2101 if (args [i]->type != STACK_R8)
2104 case MONO_TYPE_VALUETYPE:
2105 if (simple_type->data.klass->enumtype) {
2106 simple_type = simple_type->data.klass->enum_basetype;
2109 if (args [i]->type != STACK_VTYPE)
2112 case MONO_TYPE_TYPEDBYREF:
2113 if (args [i]->type != STACK_VTYPE)
2116 case MONO_TYPE_GENERICINST:
2117 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2121 g_error ("unknown type 0x%02x in check_call_signature",
2129 callvirt_to_call (int opcode)
2134 case OP_VOIDCALLVIRT:
2143 g_assert_not_reached ();
2150 callvirt_to_call_membase (int opcode)
2154 return OP_CALL_MEMBASE;
2155 case OP_VOIDCALLVIRT:
2156 return OP_VOIDCALL_MEMBASE;
2158 return OP_FCALL_MEMBASE;
2160 return OP_LCALL_MEMBASE;
2162 return OP_VCALL_MEMBASE;
2164 g_assert_not_reached ();
2170 #ifdef MONO_ARCH_HAVE_IMT
2172 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
2174 #ifdef MONO_ARCH_IMT_REG
2175 int method_reg = alloc_preg (cfg);
2178 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2179 } else if (cfg->compile_aot) {
2180 MONO_EMIT_NEW_AOTCONST (cfg, method_reg, call->method, MONO_PATCH_INFO_METHODCONST);
2183 MONO_INST_NEW (cfg, ins, OP_PCONST);
2184 ins->inst_p0 = call->method;
2185 ins->dreg = method_reg;
2186 MONO_ADD_INS (cfg->cbb, ins);
2189 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2191 mono_arch_emit_imt_argument (cfg, call);
2196 static MonoJumpInfo *
2197 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2199 MonoJumpInfo *ji = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2203 ji->data.target = target;
2208 inline static MonoInst*
2209 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args);
2211 inline static MonoCallInst *
2212 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2213 MonoInst **args, int calli, int virtual)
2216 #ifdef MONO_ARCH_SOFT_FLOAT
2220 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual, cfg->generic_sharing_context));
2223 call->signature = sig;
2225 type_to_eval_stack_type ((cfg), sig->ret, &call->inst);
2227 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
2228 MonoInst *temp = mono_compile_create_var (cfg, sig->ret, OP_LOCAL);
2231 temp->backend.is_pinvoke = sig->pinvoke;
2234 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2235 * address of return value to increase optimization opportunities.
2236 * Before vtype decomposition, the dreg of the call ins itself represents the
2237 * fact the call modifies the return value. After decomposition, the call will
2238 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2239 * will be transformed into an LDADDR.
2241 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2242 loada->dreg = alloc_preg (cfg);
2243 loada->inst_p0 = temp;
2244 /* We reference the call too since call->dreg could change during optimization */
2245 loada->inst_p1 = call;
2246 MONO_ADD_INS (cfg->cbb, loada);
2248 call->inst.dreg = temp->dreg;
2250 call->vret_var = loada;
2251 } else if (!MONO_TYPE_IS_VOID (sig->ret))
2252 call->inst.dreg = alloc_dreg (cfg, call->inst.type);
2254 #ifdef MONO_ARCH_SOFT_FLOAT
2256 * If the call has a float argument, we would need to do an r8->r4 conversion using
2257 * an icall, but that cannot be done during the call sequence since it would clobber
2258 * the call registers + the stack. So we do it before emitting the call.
2260 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2262 MonoInst *in = call->args [i];
2264 if (i >= sig->hasthis)
2265 t = sig->params [i - sig->hasthis];
2267 t = &mono_defaults.int_class->byval_arg;
2268 t = mono_type_get_underlying_type (t);
2270 if (!t->byref && t->type == MONO_TYPE_R4) {
2271 MonoInst *iargs [1];
2275 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2277 /* The result will be in an int vreg */
2278 call->args [i] = conv;
2283 mono_arch_emit_call (cfg, call);
2285 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2286 cfg->flags |= MONO_CFG_HAS_CALLS;
2291 inline static MonoInst*
2292 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr)
2294 MonoCallInst *call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE);
2296 call->inst.sreg1 = addr->dreg;
2298 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2300 return (MonoInst*)call;
2303 inline static MonoInst*
2304 mono_emit_rgctx_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg)
2306 #ifdef MONO_ARCH_RGCTX_REG
2308 int rgctx_reg = mono_alloc_preg (cfg);
2310 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2311 call = (MonoCallInst*)mono_emit_calli (cfg, sig, args, addr);
2312 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2313 cfg->uses_rgctx_reg = TRUE;
2314 return (MonoInst*)call;
2316 g_assert_not_reached ();
2322 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig,
2323 MonoInst **args, MonoInst *this, MonoInst *imt_arg)
2325 gboolean virtual = this != NULL;
2326 gboolean enable_for_aot = TRUE;
2329 if (method->string_ctor) {
2330 /* Create the real signature */
2331 /* FIXME: Cache these */
2332 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_full (cfg->mempool, sig);
2333 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2338 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual);
2340 if (this && sig->hasthis &&
2341 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2342 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2343 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2345 call->method = method;
2347 call->inst.flags |= MONO_INST_HAS_METHOD;
2348 call->inst.inst_left = this;
2351 int vtable_reg, slot_reg, this_reg;
2353 this_reg = this->dreg;
2355 if ((!cfg->compile_aot || enable_for_aot) &&
2356 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2357 ((method->flags & METHOD_ATTRIBUTE_FINAL) &&
2358 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))) {
2360 * the method is not virtual, we just need to ensure this is not null
2361 * and then we can call the method directly.
2363 if (method->klass->marshalbyref || method->klass == mono_defaults.object_class) {
2364 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2367 if (!method->string_ctor) {
2368 cfg->flags |= MONO_CFG_HAS_CHECK_THIS;
2369 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, this_reg);
2370 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, this_reg);
2373 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2375 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2377 return (MonoInst*)call;
2380 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
2381 if ((method->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (method->name, "Invoke"))) {
2382 /* Make a call to delegate->invoke_impl */
2383 call->inst.opcode = callvirt_to_call_membase (call->inst.opcode);
2384 call->inst.inst_basereg = this_reg;
2385 call->inst.inst_offset = G_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2386 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2388 return (MonoInst*)call;
2392 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
2393 ((method->flags & METHOD_ATTRIBUTE_FINAL) ||
2394 (method->klass && method->klass->flags & TYPE_ATTRIBUTE_SEALED))) {
2396 * the method is virtual, but we can statically dispatch since either
2397 * it's class or the method itself are sealed.
2398 * But first we need to ensure it's not a null reference.
2400 cfg->flags |= MONO_CFG_HAS_CHECK_THIS;
2401 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, this_reg);
2402 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, this_reg);
2404 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2405 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2407 return (MonoInst*)call;
2410 call->inst.opcode = callvirt_to_call_membase (call->inst.opcode);
2412 /* Initialize method->slot */
2413 mono_class_setup_vtable (method->klass);
2415 vtable_reg = alloc_preg (cfg);
2416 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, this_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2417 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2419 #ifdef MONO_ARCH_HAVE_IMT
2421 guint32 imt_slot = mono_method_get_imt_slot (method);
2422 emit_imt_argument (cfg, call, imt_arg);
2423 slot_reg = vtable_reg;
2424 call->inst.inst_offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2427 if (slot_reg == -1) {
2428 slot_reg = alloc_preg (cfg);
2429 mini_emit_load_intf_reg_vtable (cfg, slot_reg, vtable_reg, method->klass);
2430 call->inst.inst_offset = method->slot * SIZEOF_VOID_P;
2433 slot_reg = vtable_reg;
2434 call->inst.inst_offset = G_STRUCT_OFFSET (MonoVTable, vtable) + (method->slot * SIZEOF_VOID_P);
2437 call->inst.sreg1 = slot_reg;
2438 call->virtual = TRUE;
2441 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2443 return (MonoInst*)call;
2446 static inline MonoInst*
2447 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this)
2449 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), args, this, NULL);
2453 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2460 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE);
2463 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2465 return (MonoInst*)call;
2468 inline static MonoInst*
2469 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2471 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2475 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2479 * mono_emit_abs_call:
2481 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2483 inline static MonoInst*
2484 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2485 MonoMethodSignature *sig, MonoInst **args)
2487 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2491 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2494 if (cfg->abs_patches == NULL)
2495 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2496 g_hash_table_insert (cfg->abs_patches, ji, ji);
2497 ins = mono_emit_native_call (cfg, ji, sig, args);
2498 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2503 get_memcpy_method (void)
2505 static MonoMethod *memcpy_method = NULL;
2506 if (!memcpy_method) {
2507 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2509 g_error ("Old corlib found. Install a new one");
2511 return memcpy_method;
2515 * Emit code to copy a valuetype of type @klass whose address is stored in
2516 * @src->dreg to memory whose address is stored at @dest->dreg.
2519 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
2521 MonoInst *iargs [3];
2524 MonoMethod *memcpy_method;
2528 * This check breaks with spilled vars... need to handle it during verification anyway.
2529 * g_assert (klass && klass == src->klass && klass == dest->klass);
2533 n = mono_class_native_size (klass, &align);
2535 n = mono_class_value_size (klass, &align);
2537 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
2538 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
2539 mini_emit_memcpy2 (cfg, dest->dreg, 0, src->dreg, 0, n, align);
2543 EMIT_NEW_ICONST (cfg, iargs [2], n);
2545 memcpy_method = get_memcpy_method ();
2546 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
2551 get_memset_method (void)
2553 static MonoMethod *memset_method = NULL;
2554 if (!memset_method) {
2555 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2557 g_error ("Old corlib found. Install a new one");
2559 return memset_method;
2563 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
2565 MonoInst *iargs [3];
2568 MonoMethod *memset_method;
2570 /* FIXME: Optimize this for the case when dest is an LDADDR */
2572 mono_class_init (klass);
2573 n = mono_class_value_size (klass, &align);
2575 if (n <= sizeof (gpointer) * 5) {
2576 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
2579 memset_method = get_memset_method ();
2581 EMIT_NEW_ICONST (cfg, iargs [1], 0);
2582 EMIT_NEW_ICONST (cfg, iargs [2], n);
2583 mono_emit_method_call (cfg, memset_method, iargs, NULL);
2588 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
2590 MonoInst *this = NULL;
2592 g_assert (!method->klass->valuetype);
2594 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) && !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD))
2595 EMIT_NEW_ARGLOAD (cfg, this, 0);
2597 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
2598 MonoInst *mrgctx_loc, *mrgctx_var;
2601 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
2603 mrgctx_loc = mono_get_vtable_var (cfg);
2604 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
2607 } else if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2608 MonoInst *vtable_loc, *vtable_var;
2612 vtable_loc = mono_get_vtable_var (cfg);
2613 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
2615 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
2616 MonoInst *mrgctx_var = vtable_var;
2619 vtable_reg = alloc_preg (cfg);
2620 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, G_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
2621 vtable_var->type = STACK_PTR;
2627 int vtable_reg, res_reg;
2629 vtable_reg = alloc_preg (cfg);
2630 res_reg = alloc_preg (cfg);
2631 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this->dreg, G_STRUCT_OFFSET (MonoObject, vtable));
2636 #define EMIT_GET_RGCTX(rgctx, context_used) do { \
2637 GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(*ip); \
2638 (rgctx) = emit_get_rgctx (cfg, method, (context_used)); \
2641 static MonoJumpInfoRgctxEntry *
2642 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, int info_type)
2644 MonoJumpInfoRgctxEntry *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2645 res->method = method;
2646 res->in_mrgctx = in_mrgctx;
2647 res->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2648 res->data->type = patch_type;
2649 res->data->data.target = patch_data;
2650 res->info_type = info_type;
2655 static inline MonoInst*
2656 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
2658 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
2662 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
2663 MonoInst *rgctx, MonoClass *klass, int rgctx_type)
2665 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);
2667 return emit_rgctx_fetch (cfg, rgctx, entry);
2671 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
2672 MonoInst *rgctx, MonoMethod *cmethod, int rgctx_type)
2674 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);
2676 return emit_rgctx_fetch (cfg, rgctx, entry);
2680 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
2681 MonoInst *rgctx, MonoClassField *field, int rgctx_type)
2683 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);
2685 return emit_rgctx_fetch (cfg, rgctx, entry);
2689 * Handles unbox of a Nullable<T>. If a rgctx is passed, then shared generic code
2693 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used, MonoInst *rgctx)
2695 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
2698 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, rgctx, method,
2699 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
2701 return mono_emit_rgctx_calli (cfg, mono_method_signature (method), &val, addr, rgctx);
2703 return mono_emit_method_call (cfg, method, &val, NULL);
2708 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used, MonoInst *rgctx)
2712 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
2713 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
2714 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
2715 int rank_reg = alloc_dreg (cfg ,STACK_I4);
2717 obj_reg = sp [0]->dreg;
2718 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2719 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, rank));
2721 /* FIXME: generics */
2722 g_assert (klass->rank == 0);
2725 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
2726 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
2728 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2729 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, element_class));
2732 MonoInst *element_class;
2734 /* This assertion is from the unboxcast insn */
2735 g_assert (klass->rank == 0);
2737 element_class = emit_get_rgctx_klass (cfg, context_used, rgctx,
2738 klass->element_class, MONO_RGCTX_INFO_KLASS);
2740 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
2741 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
2743 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
2746 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_PTR), obj_reg, sizeof (MonoObject));
2747 MONO_ADD_INS (cfg->cbb, add);
2748 add->type = STACK_MP;
2755 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box)
2757 MonoInst *iargs [2];
2760 if (cfg->opt & MONO_OPT_SHARED) {
2761 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
2762 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
2764 alloc_ftn = mono_object_new;
2765 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
2766 /* This happens often in argument checking code, eg. throw new FooException... */
2767 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
2768 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
2769 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
2771 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2772 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
2775 if (managed_alloc) {
2776 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
2777 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
2779 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2781 guint32 lw = vtable->klass->instance_size;
2782 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2783 EMIT_NEW_ICONST (cfg, iargs [0], lw);
2784 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
2787 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
2791 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
2795 handle_alloc_from_inst (MonoCompile *cfg, MonoClass *klass, MonoInst *data_inst,
2798 MonoInst *iargs [2];
2799 MonoMethod *managed_alloc = NULL;
2803 FIXME: we cannot get managed_alloc here because we can't get
2804 the class's vtable (because it's not a closed class)
2806 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2807 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
2810 if (cfg->opt & MONO_OPT_SHARED) {
2811 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
2812 iargs [1] = data_inst;
2813 alloc_ftn = mono_object_new;
2815 if (managed_alloc) {
2816 iargs [0] = data_inst;
2817 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
2820 iargs [0] = data_inst;
2821 alloc_ftn = mono_object_new_specific;
2824 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
2828 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass)
2830 MonoInst *alloc, *ins;
2832 if (mono_class_is_nullable (klass)) {
2833 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
2834 return mono_emit_method_call (cfg, method, &val, NULL);
2837 alloc = handle_alloc (cfg, klass, TRUE);
2839 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
2845 handle_box_from_inst (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used, MonoInst *rgctx, MonoInst *data_inst)
2847 MonoInst *alloc, *ins;
2849 if (mono_class_is_nullable (klass)) {
2850 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
2851 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, rgctx, method,
2852 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
2854 return mono_emit_rgctx_calli (cfg, mono_method_signature (method), &val, addr, rgctx);
2856 alloc = handle_alloc_from_inst (cfg, klass, data_inst, TRUE);
2858 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
2865 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
2867 MonoBasicBlock *is_null_bb;
2868 int obj_reg = src->dreg;
2869 int vtable_reg = alloc_preg (cfg);
2871 NEW_BBLOCK (cfg, is_null_bb);
2873 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
2874 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
2876 if (mini_get_debug_options ()->better_cast_details) {
2877 int to_klass_reg = alloc_preg (cfg);
2878 int klass_reg = alloc_preg (cfg);
2879 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
2882 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
2886 MONO_ADD_INS (cfg->cbb, tls_get);
2887 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2888 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2890 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, G_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
2891 MONO_EMIT_NEW_PCONST (cfg, to_klass_reg, klass);
2892 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, G_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
2895 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2896 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2897 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
2899 int klass_reg = alloc_preg (cfg);
2901 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2903 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2904 /* the remoting code is broken, access the class for now */
2906 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
2907 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
2909 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2910 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
2912 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
2914 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2915 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, is_null_bb);
2919 MONO_START_BB (cfg, is_null_bb);
2921 /* Reset the variables holding the cast details */
2922 if (mini_get_debug_options ()->better_cast_details) {
2923 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
2925 MONO_ADD_INS (cfg->cbb, tls_get);
2926 /* It is enough to reset the from field */
2927 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, G_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
2934 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
2937 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
2938 int obj_reg = src->dreg;
2939 int vtable_reg = alloc_preg (cfg);
2940 int res_reg = alloc_preg (cfg);
2942 NEW_BBLOCK (cfg, is_null_bb);
2943 NEW_BBLOCK (cfg, false_bb);
2944 NEW_BBLOCK (cfg, end_bb);
2946 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
2947 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
2949 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2950 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2951 /* the is_null_bb target simply copies the input register to the output */
2952 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
2954 int klass_reg = alloc_preg (cfg);
2956 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
2959 int rank_reg = alloc_preg (cfg);
2960 int eclass_reg = alloc_preg (cfg);
2962 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, rank));
2963 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
2964 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
2965 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2966 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, cast_class));
2967 if (klass->cast_class == mono_defaults.object_class) {
2968 int parent_reg = alloc_preg (cfg);
2969 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, G_STRUCT_OFFSET (MonoClass, parent));
2970 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
2971 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
2972 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
2973 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
2974 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
2975 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
2976 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
2977 } else if (klass->cast_class == mono_defaults.enum_class) {
2978 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
2979 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
2980 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
2981 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
2983 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
2984 /* Check that the object is a vector too */
2985 int bounds_reg = alloc_preg (cfg);
2986 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, G_STRUCT_OFFSET (MonoArray, bounds));
2987 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
2988 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
2991 /* the is_null_bb target simply copies the input register to the output */
2992 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
2994 } else if (mono_class_is_nullable (klass)) {
2995 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
2996 /* the is_null_bb target simply copies the input register to the output */
2997 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
2999 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
3000 /* the remoting code is broken, access the class for now */
3002 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
3003 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
3005 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3006 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
3008 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
3009 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
3011 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3012 /* the is_null_bb target simply copies the input register to the output */
3013 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, is_null_bb);
3018 MONO_START_BB (cfg, false_bb);
3020 MONO_EMIT_NEW_ICONST (cfg, res_reg, 0);
3021 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3023 MONO_START_BB (cfg, is_null_bb);
3025 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
3026 ins->type = STACK_OBJ;
3029 MONO_START_BB (cfg, end_bb);
3035 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
3037 /* This opcode takes as input an object reference and a class, and returns:
3038 0) if the object is an instance of the class,
3039 1) if the object is not instance of the class,
3040 2) if the object is a proxy whose type cannot be determined */
3043 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
3044 int obj_reg = src->dreg;
3045 int dreg = alloc_ireg (cfg);
3047 int klass_reg = alloc_preg (cfg);
3049 NEW_BBLOCK (cfg, true_bb);
3050 NEW_BBLOCK (cfg, false_bb);
3051 NEW_BBLOCK (cfg, false2_bb);
3052 NEW_BBLOCK (cfg, end_bb);
3053 NEW_BBLOCK (cfg, no_proxy_bb);
3055 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3056 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
3058 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3059 NEW_BBLOCK (cfg, interface_fail_bb);
3061 tmp_reg = alloc_preg (cfg);
3062 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
3063 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
3064 MONO_START_BB (cfg, interface_fail_bb);
3065 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3067 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
3069 tmp_reg = alloc_preg (cfg);
3070 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
3071 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
3072 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
3074 tmp_reg = alloc_preg (cfg);
3075 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
3076 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3078 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
3079 tmp_reg = alloc_preg (cfg);
3080 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
3081 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
3083 tmp_reg = alloc_preg (cfg);
3084 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
3085 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
3086 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
3088 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
3089 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
3091 MONO_START_BB (cfg, no_proxy_bb);
3093 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
3096 MONO_START_BB (cfg, false_bb);
3098 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
3099 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3101 MONO_START_BB (cfg, false2_bb);
3103 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
3104 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3106 MONO_START_BB (cfg, true_bb);
3108 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
3110 MONO_START_BB (cfg, end_bb);
3113 MONO_INST_NEW (cfg, ins, OP_ICONST);
3115 ins->type = STACK_I4;
3121 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
3123 /* This opcode takes as input an object reference and a class, and returns:
3124 0) if the object is an instance of the class,
3125 1) if the object is a proxy whose type cannot be determined
3126 an InvalidCastException exception is thrown otherwhise*/
3129 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
3130 int obj_reg = src->dreg;
3131 int dreg = alloc_ireg (cfg);
3132 int tmp_reg = alloc_preg (cfg);
3133 int klass_reg = alloc_preg (cfg);
3135 NEW_BBLOCK (cfg, end_bb);
3136 NEW_BBLOCK (cfg, ok_result_bb);
3138 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3139 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
3141 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3142 NEW_BBLOCK (cfg, interface_fail_bb);
3144 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
3145 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
3146 MONO_START_BB (cfg, interface_fail_bb);
3147 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3149 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
3151 tmp_reg = alloc_preg (cfg);
3152 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
3153 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
3154 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
3156 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
3157 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3160 NEW_BBLOCK (cfg, no_proxy_bb);
3162 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
3163 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
3164 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
3166 tmp_reg = alloc_preg (cfg);
3167 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
3168 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
3170 tmp_reg = alloc_preg (cfg);
3171 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
3172 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
3173 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
3175 NEW_BBLOCK (cfg, fail_1_bb);
3177 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
3179 MONO_START_BB (cfg, fail_1_bb);
3181 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
3182 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3184 MONO_START_BB (cfg, no_proxy_bb);
3186 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
3189 MONO_START_BB (cfg, ok_result_bb);
3191 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
3193 MONO_START_BB (cfg, end_bb);
3196 MONO_INST_NEW (cfg, ins, OP_ICONST);
3198 ins->type = STACK_I4;
3203 static G_GNUC_UNUSED MonoInst*
3204 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method)
3206 gpointer *trampoline;
3207 MonoInst *obj, *method_ins, *tramp_ins;
3211 obj = handle_alloc (cfg, klass, FALSE);
3213 /* Inline the contents of mono_delegate_ctor */
3215 /* Set target field */
3216 /* Optimize away setting of NULL target */
3217 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0))
3218 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, G_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
3220 /* Set method field */
3221 EMIT_NEW_METHODCONST (cfg, method_ins, method);
3222 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, G_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
3225 * To avoid looking up the compiled code belonging to the target method
3226 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
3227 * store it, and we fill it after the method has been compiled.
3229 if (!cfg->compile_aot && !method->dynamic) {
3230 MonoInst *code_slot_ins;
3232 domain = mono_domain_get ();
3233 mono_domain_lock (domain);
3234 if (!domain->method_code_hash)
3235 domain->method_code_hash = g_hash_table_new (NULL, NULL);
3236 code_slot = g_hash_table_lookup (domain->method_code_hash, method);
3238 code_slot = mono_mempool_alloc0 (domain->mp, sizeof (gpointer));
3239 g_hash_table_insert (domain->method_code_hash, method, code_slot);
3241 mono_domain_unlock (domain);
3243 EMIT_NEW_PCONST (cfg, code_slot_ins, code_slot);
3244 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, G_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
3247 /* Set invoke_impl field */
3248 trampoline = mono_create_delegate_trampoline (klass);
3249 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_ABS, trampoline);
3250 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, G_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
3252 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
3258 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
3260 MonoJitICallInfo *info;
3262 /* Need to register the icall so it gets an icall wrapper */
3263 info = mono_get_array_new_va_icall (rank);
3265 cfg->flags |= MONO_CFG_HAS_VARARGS;
3267 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
3268 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
3272 mono_emit_load_got_addr (MonoCompile *cfg)
3274 MonoInst *getaddr, *dummy_use;
3276 if (!cfg->got_var || cfg->got_var_allocated)
3279 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
3280 getaddr->dreg = cfg->got_var->dreg;
3282 /* Add it to the start of the first bblock */
3283 if (cfg->bb_entry->code) {
3284 getaddr->next = cfg->bb_entry->code;
3285 cfg->bb_entry->code = getaddr;
3288 MONO_ADD_INS (cfg->bb_entry, getaddr);
3290 cfg->got_var_allocated = TRUE;
3293 * Add a dummy use to keep the got_var alive, since real uses might
3294 * only be generated by the back ends.
3295 * Add it to end_bblock, so the variable's lifetime covers the whole
3297 * It would be better to make the usage of the got var explicit in all
3298 * cases when the backend needs it (i.e. calls, throw etc.), so this
3299 * wouldn't be needed.
3301 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
3302 MONO_ADD_INS (cfg->bb_exit, dummy_use);
3306 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
3308 MonoMethodHeader *header = mono_method_get_header (method);
3310 #ifdef MONO_ARCH_SOFT_FLOAT
3311 MonoMethodSignature *sig = mono_method_signature (method);
3315 if (cfg->generic_sharing_context)
3318 #ifdef MONO_ARCH_HAVE_LMF_OPS
3319 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3320 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
3321 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
3325 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3326 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3327 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
3328 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
3329 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3330 (method->klass->marshalbyref) ||
3331 !header || header->num_clauses)
3334 /* also consider num_locals? */
3335 /* Do the size check early to avoid creating vtables */
3336 if (getenv ("MONO_INLINELIMIT")) {
3337 if (header->code_size >= atoi (getenv ("MONO_INLINELIMIT"))) {
3340 } else if (header->code_size >= INLINE_LENGTH_LIMIT)
3344 * if we can initialize the class of the method right away, we do,
3345 * otherwise we don't allow inlining if the class needs initialization,
3346 * since it would mean inserting a call to mono_runtime_class_init()
3347 * inside the inlined code
3349 if (!(cfg->opt & MONO_OPT_SHARED)) {
3350 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
3351 if (cfg->run_cctors && method->klass->has_cctor) {
3352 if (!method->klass->runtime_info)
3353 /* No vtable created yet */
3355 vtable = mono_class_vtable (cfg->domain, method->klass);
3358 /* This makes so that inline cannot trigger */
3359 /* .cctors: too many apps depend on them */
3360 /* running with a specific order... */
3361 if (! vtable->initialized)
3363 mono_runtime_class_init (vtable);
3365 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
3366 if (!method->klass->runtime_info)
3367 /* No vtable created yet */
3369 vtable = mono_class_vtable (cfg->domain, method->klass);
3372 if (!vtable->initialized)
3377 * If we're compiling for shared code
3378 * the cctor will need to be run at aot method load time, for example,
3379 * or at the end of the compilation of the inlining method.
3381 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3386 * CAS - do not inline methods with declarative security
3387 * Note: this has to be before any possible return TRUE;
3389 if (mono_method_has_declsec (method))
3392 #ifdef MONO_ARCH_SOFT_FLOAT
3394 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
3396 for (i = 0; i < sig->param_count; ++i)
3397 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
3405 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
3407 if (vtable->initialized && !cfg->compile_aot)
3410 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3413 if (!mono_class_needs_cctor_run (vtable->klass, method))
3416 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3417 /* The initialization is already done before the method is called */
3424 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index)
3428 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
3430 mono_class_init (klass);
3431 size = mono_class_array_element_size (klass);
3433 mult_reg = alloc_preg (cfg);
3434 array_reg = arr->dreg;
3435 index_reg = index->dreg;
3437 #if SIZEOF_VOID_P == 8
3438 /* The array reg is 64 bits but the index reg is only 32 */
3439 index2_reg = alloc_preg (cfg);
3440 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
3442 index2_reg = index_reg;
3445 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
3447 #if defined(__i386__) || defined(__x86_64__)
3448 if (size == 1 || size == 2 || size == 4 || size == 8) {
3449 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
3451 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], G_STRUCT_OFFSET (MonoArray, vector));
3452 ins->type = STACK_PTR;
3458 add_reg = alloc_preg (cfg);
3460 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
3461 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
3462 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, G_STRUCT_OFFSET (MonoArray, vector));
3463 ins->type = STACK_PTR;
3464 MONO_ADD_INS (cfg->cbb, ins);
3469 #ifndef MONO_ARCH_EMULATE_MUL_DIV
3471 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
3473 int bounds_reg = alloc_preg (cfg);
3474 int add_reg = alloc_preg (cfg);
3475 int mult_reg = alloc_preg (cfg);
3476 int mult2_reg = alloc_preg (cfg);
3477 int low1_reg = alloc_preg (cfg);
3478 int low2_reg = alloc_preg (cfg);
3479 int high1_reg = alloc_preg (cfg);
3480 int high2_reg = alloc_preg (cfg);
3481 int realidx1_reg = alloc_preg (cfg);
3482 int realidx2_reg = alloc_preg (cfg);
3483 int sum_reg = alloc_preg (cfg);
3488 mono_class_init (klass);
3489 size = mono_class_array_element_size (klass);
3491 index1 = index_ins1->dreg;
3492 index2 = index_ins2->dreg;
3494 /* range checking */
3495 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
3496 arr->dreg, G_STRUCT_OFFSET (MonoArray, bounds));
3498 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
3499 bounds_reg, G_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
3500 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
3501 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
3502 bounds_reg, G_STRUCT_OFFSET (MonoArrayBounds, length));
3503 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
3504 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
3506 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
3507 bounds_reg, sizeof (MonoArrayBounds) + G_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
3508 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
3509 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
3510 bounds_reg, sizeof (MonoArrayBounds) + G_STRUCT_OFFSET (MonoArrayBounds, length));
3511 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
3512 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
3514 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
3515 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
3516 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
3517 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
3518 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, G_STRUCT_OFFSET (MonoArray, vector));
3520 ins->type = STACK_MP;
3522 MONO_ADD_INS (cfg->cbb, ins);
3529 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3533 MonoMethod *addr_method;
3536 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3539 return mini_emit_ldelema_1_ins (cfg, cmethod->klass->element_class, sp [0], sp [1]);
3541 #ifndef MONO_ARCH_EMULATE_MUL_DIV
3542 /* emit_ldelema_2 depends on OP_LMUL */
3543 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3544 return mini_emit_ldelema_2_ins (cfg, cmethod->klass->element_class, sp [0], sp [1], sp [2]);
3548 element_size = mono_class_array_element_size (cmethod->klass->element_class);
3549 addr_method = mono_marshal_get_array_address (rank, element_size);
3550 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
3556 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3558 MonoInst *ins = NULL;
3560 static MonoClass *runtime_helpers_class = NULL;
3561 if (! runtime_helpers_class)
3562 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
3563 "System.Runtime.CompilerServices", "RuntimeHelpers");
3565 if (cmethod->klass == mono_defaults.string_class) {
3566 if (strcmp (cmethod->name, "get_Chars") == 0) {
3567 int dreg = alloc_ireg (cfg);
3568 int index_reg = alloc_preg (cfg);
3569 int mult_reg = alloc_preg (cfg);
3570 int add_reg = alloc_preg (cfg);
3572 #if SIZEOF_VOID_P == 8
3573 /* The array reg is 64 bits but the index reg is only 32 */
3574 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
3576 index_reg = args [1]->dreg;
3578 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
3580 #if defined(__i386__) || defined(__x86_64__)
3581 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, G_STRUCT_OFFSET (MonoString, chars));
3582 add_reg = ins->dreg;
3583 /* Avoid a warning */
3585 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
3588 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
3589 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
3590 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
3591 add_reg, G_STRUCT_OFFSET (MonoString, chars));
3593 type_from_op (ins, NULL, NULL);
3595 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3596 int dreg = alloc_ireg (cfg);
3597 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
3598 args [0]->dreg, G_STRUCT_OFFSET (MonoString, length));
3599 type_from_op (ins, NULL, NULL);
3602 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
3603 int mult_reg = alloc_preg (cfg);
3604 int add_reg = alloc_preg (cfg);
3606 /* The corlib functions check for oob already. */
3607 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, args [1]->dreg, 1);
3608 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
3609 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, add_reg, G_STRUCT_OFFSET (MonoString, chars), args [2]->dreg);
3612 } else if (cmethod->klass == mono_defaults.object_class) {
3614 if (strcmp (cmethod->name, "GetType") == 0) {
3615 int dreg = alloc_preg (cfg);
3616 int vt_reg = alloc_preg (cfg);
3617 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vt_reg, args [0]->dreg, G_STRUCT_OFFSET (MonoObject, vtable));
3618 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, G_STRUCT_OFFSET (MonoVTable, type));
3619 type_from_op (ins, NULL, NULL);
3622 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
3623 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
3624 int dreg = alloc_ireg (cfg);
3625 int t1 = alloc_ireg (cfg);
3627 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
3628 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
3629 ins->type = STACK_I4;
3633 } else if (strcmp (cmethod->name, ".ctor") == 0) {
3634 MONO_INST_NEW (cfg, ins, OP_NOP);
3635 MONO_ADD_INS (cfg->cbb, ins);
3639 } else if (cmethod->klass == mono_defaults.array_class) {
3640 if (cmethod->name [0] != 'g')
3643 if (strcmp (cmethod->name, "get_Rank") == 0) {
3644 int dreg = alloc_ireg (cfg);
3645 int vtable_reg = alloc_preg (cfg);
3646 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, vtable_reg,
3647 args [0]->dreg, G_STRUCT_OFFSET (MonoObject, vtable));
3648 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
3649 vtable_reg, G_STRUCT_OFFSET (MonoVTable, rank));
3650 type_from_op (ins, NULL, NULL);
3653 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3654 int dreg = alloc_ireg (cfg);
3656 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
3657 args [0]->dreg, G_STRUCT_OFFSET (MonoArray, max_length));
3658 type_from_op (ins, NULL, NULL);
3663 } else if (cmethod->klass == runtime_helpers_class) {
3665 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
3666 EMIT_NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
3670 } else if (cmethod->klass == mono_defaults.thread_class) {
3671 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg))) {
3672 ins->dreg = alloc_preg (cfg);
3673 ins->type = STACK_OBJ;
3674 MONO_ADD_INS (cfg->cbb, ins);
3676 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0) {
3677 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3678 MONO_ADD_INS (cfg->cbb, ins);
3681 } else if (mini_class_is_system_array (cmethod->klass) &&
3682 strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
3683 MonoInst *addr, *store, *load;
3684 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
3686 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1]);
3687 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
3688 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
3690 } else if (cmethod->klass->image == mono_defaults.corlib &&
3691 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
3692 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
3695 #if SIZEOF_VOID_P == 8
3696 if (strcmp (cmethod->name, "Read") == 0 && (fsig->params [0]->type == MONO_TYPE_I8)) {
3697 /* 64 bit reads are already atomic */
3698 MONO_INST_NEW (cfg, ins, OP_LOADI8_MEMBASE);
3699 ins->dreg = mono_alloc_preg (cfg);
3700 ins->inst_basereg = args [0]->dreg;
3701 ins->inst_offset = 0;
3702 MONO_ADD_INS (cfg->cbb, ins);
3706 #ifdef MONO_ARCH_HAVE_ATOMIC_ADD
3707 if (strcmp (cmethod->name, "Increment") == 0) {
3708 MonoInst *ins_iconst;
3711 if (fsig->params [0]->type == MONO_TYPE_I4)
3712 opcode = OP_ATOMIC_ADD_NEW_I4;
3713 #if SIZEOF_VOID_P == 8
3714 else if (fsig->params [0]->type == MONO_TYPE_I8)
3715 opcode = OP_ATOMIC_ADD_NEW_I8;
3718 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3719 ins_iconst->inst_c0 = 1;
3720 ins_iconst->dreg = mono_alloc_ireg (cfg);
3721 MONO_ADD_INS (cfg->cbb, ins_iconst);
3723 MONO_INST_NEW (cfg, ins, opcode);
3724 ins->dreg = mono_alloc_ireg (cfg);
3725 ins->inst_basereg = args [0]->dreg;
3726 ins->inst_offset = 0;
3727 ins->sreg2 = ins_iconst->dreg;
3728 ins->type = (opcode == OP_ATOMIC_ADD_NEW_I4) ? STACK_I4 : STACK_I8;
3729 MONO_ADD_INS (cfg->cbb, ins);
3731 } else if (strcmp (cmethod->name, "Decrement") == 0) {
3732 MonoInst *ins_iconst;
3735 if (fsig->params [0]->type == MONO_TYPE_I4)
3736 opcode = OP_ATOMIC_ADD_NEW_I4;
3737 #if SIZEOF_VOID_P == 8
3738 else if (fsig->params [0]->type == MONO_TYPE_I8)
3739 opcode = OP_ATOMIC_ADD_NEW_I8;
3742 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3743 ins_iconst->inst_c0 = -1;
3744 ins_iconst->dreg = mono_alloc_ireg (cfg);
3745 MONO_ADD_INS (cfg->cbb, ins_iconst);
3747 MONO_INST_NEW (cfg, ins, opcode);
3748 ins->dreg = mono_alloc_ireg (cfg);
3749 ins->inst_basereg = args [0]->dreg;
3750 ins->inst_offset = 0;
3751 ins->sreg2 = ins_iconst->dreg;
3752 ins->type = (opcode == OP_ATOMIC_ADD_NEW_I4) ? STACK_I4 : STACK_I8;
3753 MONO_ADD_INS (cfg->cbb, ins);
3755 } else if (strcmp (cmethod->name, "Add") == 0) {
3758 if (fsig->params [0]->type == MONO_TYPE_I4)
3759 opcode = OP_ATOMIC_ADD_NEW_I4;
3760 #if SIZEOF_VOID_P == 8
3761 else if (fsig->params [0]->type == MONO_TYPE_I8)
3762 opcode = OP_ATOMIC_ADD_NEW_I8;
3766 MONO_INST_NEW (cfg, ins, opcode);
3767 ins->dreg = mono_alloc_ireg (cfg);
3768 ins->inst_basereg = args [0]->dreg;
3769 ins->inst_offset = 0;
3770 ins->sreg2 = args [1]->dreg;
3771 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
3772 MONO_ADD_INS (cfg->cbb, ins);
3775 #endif /* MONO_ARCH_HAVE_ATOMIC_ADD */
3777 #ifdef MONO_ARCH_HAVE_ATOMIC_EXCHANGE
3778 if (strcmp (cmethod->name, "Exchange") == 0) {
3781 if (fsig->params [0]->type == MONO_TYPE_I4)
3782 opcode = OP_ATOMIC_EXCHANGE_I4;
3783 #if SIZEOF_VOID_P == 8
3784 else if ((fsig->params [0]->type == MONO_TYPE_I8) ||
3785 (fsig->params [0]->type == MONO_TYPE_I) ||
3786 (fsig->params [0]->type == MONO_TYPE_OBJECT))
3787 opcode = OP_ATOMIC_EXCHANGE_I8;
3789 else if ((fsig->params [0]->type == MONO_TYPE_I) ||
3790 (fsig->params [0]->type == MONO_TYPE_OBJECT))
3791 opcode = OP_ATOMIC_EXCHANGE_I4;
3796 MONO_INST_NEW (cfg, ins, opcode);
3797 ins->dreg = mono_alloc_ireg (cfg);
3798 ins->inst_basereg = args [0]->dreg;
3799 ins->inst_offset = 0;
3800 ins->sreg2 = args [1]->dreg;
3801 MONO_ADD_INS (cfg->cbb, ins);
3803 switch (fsig->params [0]->type) {
3805 ins->type = STACK_I4;
3809 ins->type = STACK_I8;
3811 case MONO_TYPE_OBJECT:
3812 ins->type = STACK_OBJ;
3815 g_assert_not_reached ();
3818 #endif /* MONO_ARCH_HAVE_ATOMIC_EXCHANGE */
3820 #ifdef MONO_ARCH_HAVE_ATOMIC_CAS_IMM
3822 * Can't implement CompareExchange methods this way since they have
3823 * three arguments. We can implement one of the common cases, where the new
3824 * value is a constant.
3826 if ((strcmp (cmethod->name, "CompareExchange") == 0)) {
3827 if (fsig->params [1]->type == MONO_TYPE_I4 && args [2]->opcode == OP_ICONST) {
3828 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_IMM_I4);
3829 ins->dreg = alloc_ireg (cfg);
3830 ins->sreg1 = args [0]->dreg;
3831 ins->sreg2 = args [1]->dreg;
3832 ins->backend.data = GINT_TO_POINTER (args [2]->inst_c0);
3833 ins->type = STACK_I4;
3834 MONO_ADD_INS (cfg->cbb, ins);
3836 /* The I8 case is hard to detect, since the arg might be a conv.i8 (iconst) tree */
3838 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS_IMM */
3842 } else if (cmethod->klass->image == mono_defaults.corlib) {
3843 if (cmethod->name [0] == 'B' && strcmp (cmethod->name, "Break") == 0
3844 && strcmp (cmethod->klass->name, "Debugger") == 0) {
3845 MONO_INST_NEW (cfg, ins, OP_BREAK);
3846 MONO_ADD_INS (cfg->cbb, ins);
3849 if (cmethod->name [0] == 'g' && strcmp (cmethod->name, "get_IsRunningOnWindows") == 0
3850 && strcmp (cmethod->klass->name, "Environment") == 0) {
3851 #ifdef PLATFORM_WIN32
3852 EMIT_NEW_ICONST (cfg, ins, 1);
3854 EMIT_NEW_ICONST (cfg, ins, 0);
3858 } else if (cmethod->klass == mono_defaults.math_class) {
3860 * There is general branches code for Min/Max, but it does not work for
3862 * http://everything2.com/?node_id=1051618
3866 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
3870 * This entry point could be used later for arbitrary method
3873 inline static MonoInst*
3874 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
3875 MonoMethodSignature *signature, MonoInst **args, MonoInst *this)
3877 if (method->klass == mono_defaults.string_class) {
3878 /* managed string allocation support */
3879 if (strcmp (method->name, "InternalAllocateStr") == 0) {
3880 MonoInst *iargs [2];
3881 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3882 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, FALSE);
3885 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
3886 iargs [1] = args [0];
3887 return mono_emit_method_call (cfg, managed_alloc, iargs, this);
3894 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
3896 MonoInst *store, *temp;
3899 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3900 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
3903 * FIXME: We should use *args++ = sp [0], but that would mean the arg
3904 * would be different than the MonoInst's used to represent arguments, and
3905 * the ldelema implementation can't deal with that.
3906 * Solution: When ldelema is used on an inline argument, create a var for
3907 * it, emit ldelema on that var, and emit the saving code below in
3908 * inline_method () if needed.
3910 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
3912 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
3913 store->cil_code = sp [0]->cil_code;
3918 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
3919 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
3921 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
3923 check_inline_called_method_name_limit (MonoMethod *called_method)
3926 static char *limit = NULL;
3928 if (limit == NULL) {
3929 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
3931 if (limit_string != NULL)
3932 limit = limit_string;
3934 limit = (char *) "";
3937 if (limit [0] != '\0') {
3938 char *called_method_name = mono_method_full_name (called_method, TRUE);
3940 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
3941 g_free (called_method_name);
3943 //return (strncmp_result <= 0);
3944 return (strncmp_result == 0);
3951 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
3953 check_inline_caller_method_name_limit (MonoMethod *caller_method)
3956 static char *limit = NULL;
3958 if (limit == NULL) {
3959 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
3960 if (limit_string != NULL) {
3961 limit = limit_string;
3963 limit = (char *) "";
3967 if (limit [0] != '\0') {
3968 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
3970 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
3971 g_free (caller_method_name);
3973 //return (strncmp_result <= 0);
3974 return (strncmp_result == 0);
3982 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
3983 guchar *ip, guint real_offset, GList *dont_inline, gboolean inline_allways)
3985 MonoInst *ins, *rvar = NULL;
3986 MonoMethodHeader *cheader;
3987 MonoBasicBlock *ebblock, *sbblock;
3989 MonoMethod *prev_inlined_method;
3990 MonoInst **prev_locals, **prev_args;
3991 MonoType **prev_arg_types;
3992 guint prev_real_offset;
3993 GHashTable *prev_cbb_hash;
3994 MonoBasicBlock **prev_cil_offset_to_bb;
3995 MonoBasicBlock *prev_cbb;
3996 unsigned char* prev_cil_start;
3997 guint32 prev_cil_offset_to_bb_len;
3998 MonoMethod *prev_current_method;
3999 MonoGenericContext *prev_generic_context;
4001 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
4003 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4004 if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
4007 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4008 if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
4012 if (cfg->verbose_level > 2)
4013 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4015 if (!cmethod->inline_info) {
4016 mono_jit_stats.inlineable_methods++;
4017 cmethod->inline_info = 1;
4019 /* allocate space to store the return value */
4020 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4021 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4024 /* allocate local variables */
4025 cheader = mono_method_get_header (cmethod);
4026 prev_locals = cfg->locals;
4027 cfg->locals = mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
4028 for (i = 0; i < cheader->num_locals; ++i)
4029 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
4031 /* allocate start and end blocks */
4032 /* This is needed so if the inline is aborted, we can clean up */
4033 NEW_BBLOCK (cfg, sbblock);
4034 sbblock->real_offset = real_offset;
4036 NEW_BBLOCK (cfg, ebblock);
4037 ebblock->block_num = cfg->num_bblocks++;
4038 ebblock->real_offset = real_offset;
4040 prev_args = cfg->args;
4041 prev_arg_types = cfg->arg_types;
4042 prev_inlined_method = cfg->inlined_method;
4043 cfg->inlined_method = cmethod;
4044 cfg->ret_var_set = FALSE;
4045 prev_real_offset = cfg->real_offset;
4046 prev_cbb_hash = cfg->cbb_hash;
4047 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
4048 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
4049 prev_cil_start = cfg->cil_start;
4050 prev_cbb = cfg->cbb;
4051 prev_current_method = cfg->current_method;
4052 prev_generic_context = cfg->generic_context;
4054 costs = mono_method_to_ir2 (cfg, cmethod, sbblock, ebblock, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
4056 cfg->inlined_method = prev_inlined_method;
4057 cfg->real_offset = prev_real_offset;
4058 cfg->cbb_hash = prev_cbb_hash;
4059 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
4060 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
4061 cfg->cil_start = prev_cil_start;
4062 cfg->locals = prev_locals;
4063 cfg->args = prev_args;
4064 cfg->arg_types = prev_arg_types;
4065 cfg->current_method = prev_current_method;
4066 cfg->generic_context = prev_generic_context;
4068 if ((costs >= 0 && costs < 60) || inline_allways) {
4069 if (cfg->verbose_level > 2)
4070 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4072 mono_jit_stats.inlined_methods++;
4074 /* always add some code to avoid block split failures */
4075 MONO_INST_NEW (cfg, ins, OP_NOP);
4076 MONO_ADD_INS (prev_cbb, ins);
4078 prev_cbb->next_bb = sbblock;
4079 link_bblock (cfg, prev_cbb, sbblock);
4082 * Get rid of the begin and end bblocks if possible to aid local
4085 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
4087 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
4088 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
4090 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
4091 MonoBasicBlock *prev = ebblock->in_bb [0];
4092 mono_merge_basic_blocks (cfg, prev, ebblock);
4100 * If the inlined method contains only a throw, then the ret var is not
4101 * set, so set it to a dummy value.
4103 if (!cfg->ret_var_set) {
4104 static double r8_0 = 0.0;
4106 switch (rvar->type) {
4108 MONO_EMIT_NEW_ICONST (cfg, rvar->dreg, 0);
4111 MONO_EMIT_NEW_I8CONST (cfg, rvar->dreg, 0);
4116 MONO_EMIT_NEW_PCONST (cfg, rvar->dreg, 0);
4119 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4120 ins->type = STACK_R8;
4121 ins->inst_p0 = (void*)&r8_0;
4122 ins->dreg = rvar->dreg;
4123 MONO_ADD_INS (cfg->cbb, ins);
4126 MONO_EMIT_NEW_VZERO (cfg, rvar->dreg, mono_class_from_mono_type (fsig->ret));
4129 g_assert_not_reached ();
4133 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
4138 if (cfg->verbose_level > 2)
4139 printf ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
4140 cfg->exception_type = MONO_EXCEPTION_NONE;
4141 mono_loader_clear_error ();
4143 /* This gets rid of the newly added bblocks */
4144 cfg->cbb = prev_cbb;
4150 * Some of these comments may well be out-of-date.
4151 * Design decisions: we do a single pass over the IL code (and we do bblock
4152 * splitting/merging in the few cases when it's required: a back jump to an IL
4153 * address that was not already seen as bblock starting point).
4154 * Code is validated as we go (full verification is still better left to metadata/verify.c).
4155 * Complex operations are decomposed in simpler ones right away. We need to let the
4156 * arch-specific code peek and poke inside this process somehow (except when the
4157 * optimizations can take advantage of the full semantic info of coarse opcodes).
4158 * All the opcodes of the form opcode.s are 'normalized' to opcode.
4159 * MonoInst->opcode initially is the IL opcode or some simplification of that
4160 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
4161 * opcode with value bigger than OP_LAST.
4162 * At this point the IR can be handed over to an interpreter, a dumb code generator
4163 * or to the optimizing code generator that will translate it to SSA form.
4165 * Profiling directed optimizations.
4166 * We may compile by default with few or no optimizations and instrument the code
4167 * or the user may indicate what methods to optimize the most either in a config file
4168 * or through repeated runs where the compiler applies offline the optimizations to
4169 * each method and then decides if it was worth it.
4172 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
4173 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
4174 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
4175 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
4176 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
4177 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
4178 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
4179 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) {cfg->exception_ptr = klass; goto load_error;}
4181 /* offset from br.s -> br like opcodes */
4182 #define BIG_BRANCH_OFFSET 13
4185 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
4187 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
4189 return b == NULL || b == bb;
4193 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
4195 unsigned char *ip = start;
4196 unsigned char *target;
4199 MonoBasicBlock *bblock;
4200 const MonoOpcode *opcode;
4203 cli_addr = ip - start;
4204 i = mono_opcode_value ((const guint8 **)&ip, end);
4207 opcode = &mono_opcodes [i];
4208 switch (opcode->argument) {
4209 case MonoInlineNone:
4212 case MonoInlineString:
4213 case MonoInlineType:
4214 case MonoInlineField:
4215 case MonoInlineMethod:
4218 case MonoShortInlineR:
4225 case MonoShortInlineVar:
4226 case MonoShortInlineI:
4229 case MonoShortInlineBrTarget:
4230 target = start + cli_addr + 2 + (signed char)ip [1];
4231 GET_BBLOCK (cfg, bblock, target);
4234 GET_BBLOCK (cfg, bblock, ip);
4236 case MonoInlineBrTarget:
4237 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
4238 GET_BBLOCK (cfg, bblock, target);
4241 GET_BBLOCK (cfg, bblock, ip);
4243 case MonoInlineSwitch: {
4244 guint32 n = read32 (ip + 1);
4247 cli_addr += 5 + 4 * n;
4248 target = start + cli_addr;
4249 GET_BBLOCK (cfg, bblock, target);
4251 for (j = 0; j < n; ++j) {
4252 target = start + cli_addr + (gint32)read32 (ip);
4253 GET_BBLOCK (cfg, bblock, target);
4263 g_assert_not_reached ();
4266 if (i == CEE_THROW) {
4267 unsigned char *bb_start = ip - 1;
4269 /* Find the start of the bblock containing the throw */
4271 while ((bb_start >= start) && !bblock) {
4272 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
4276 bblock->out_of_line = 1;
4285 static inline MonoMethod *
4286 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4290 if (m->wrapper_type != MONO_WRAPPER_NONE)
4291 return mono_method_get_wrapper_data (m, token);
4293 method = mono_get_method_full (m->klass->image, token, klass, context);
4298 static inline MonoMethod *
4299 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4301 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
4303 if (method && cfg && !cfg->generic_sharing_context && mono_class_is_open_constructed_type (&method->klass->byval_arg))
4309 static inline MonoClass*
4310 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
4314 if (method->wrapper_type != MONO_WRAPPER_NONE)
4315 klass = mono_method_get_wrapper_data (method, token);
4317 klass = mono_class_get_full (method->klass->image, token, context);
4319 mono_class_init (klass);
4324 * Returns TRUE if the JIT should abort inlining because "callee"
4325 * is influenced by security attributes.
4328 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
4332 if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
4336 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
4337 if (result == MONO_JIT_SECURITY_OK)
4340 if (result == MONO_JIT_LINKDEMAND_ECMA) {
4341 /* Generate code to throw a SecurityException before the actual call/link */
4342 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4345 NEW_ICONST (cfg, args [0], 4);
4346 NEW_METHODCONST (cfg, args [1], caller);
4347 mono_emit_method_call (cfg, secman->linkdemandsecurityexception, args, NULL);
4348 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
4349 /* don't hide previous results */
4350 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
4351 cfg->exception_data = result;
4359 method_access_exception (void)
4361 static MonoMethod *method = NULL;
4364 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4365 method = mono_class_get_method_from_name (secman->securitymanager,
4366 "MethodAccessException", 2);
4373 emit_throw_method_access_exception (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4374 MonoBasicBlock *bblock, unsigned char *ip)
4376 MonoMethod *thrower = method_access_exception ();
4379 EMIT_NEW_METHODCONST (cfg, args [0], caller);
4380 EMIT_NEW_METHODCONST (cfg, args [1], callee);
4381 mono_emit_method_call (cfg, thrower, args, NULL);
4385 verification_exception (void)
4387 static MonoMethod *method = NULL;
4390 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4391 method = mono_class_get_method_from_name (secman->securitymanager,
4392 "VerificationException", 0);
4399 emit_throw_verification_exception (MonoCompile *cfg, MonoBasicBlock *bblock, unsigned char *ip)
4401 MonoMethod *thrower = verification_exception ();
4403 mono_emit_method_call (cfg, thrower, NULL, NULL);
4407 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4408 MonoBasicBlock *bblock, unsigned char *ip)
4410 MonoSecurityCoreCLRLevel caller_level = mono_security_core_clr_method_level (caller, TRUE);
4411 MonoSecurityCoreCLRLevel callee_level = mono_security_core_clr_method_level (callee, TRUE);
4412 gboolean is_safe = TRUE;
4414 if (!(caller_level >= callee_level ||
4415 caller_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL ||
4416 callee_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL)) {
4421 emit_throw_method_access_exception (cfg, caller, callee, bblock, ip);
4425 method_is_safe (MonoMethod *method)
4428 if (strcmp (method->name, "unsafeMethod") == 0)
4435 * Check that the IL instructions at ip are the array initialization
4436 * sequence and return the pointer to the data and the size.
4439 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size)
4442 * newarr[System.Int32]
4444 * ldtoken field valuetype ...
4445 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
4447 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
4448 guint32 token = read32 (ip + 7);
4449 guint32 field_token = read32 (ip + 2);
4450 guint32 field_index = field_token & 0xffffff;
4452 const char *data_ptr;
4454 MonoMethod *cmethod;
4455 MonoClass *dummy_class;
4456 MonoClassField *field = mono_field_from_token (method->klass->image, field_token, &dummy_class, NULL);
4462 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
4465 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
4467 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
4468 case MONO_TYPE_BOOLEAN:
4472 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
4473 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
4474 case MONO_TYPE_CHAR:
4484 return NULL; /* stupid ARM FP swapped format */
4494 if (size > mono_type_size (field->type, &dummy_align))
4497 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
4498 field_index = read32 (ip + 2) & 0xffffff;
4499 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
4500 data_ptr = mono_image_rva_map (method->klass->image, rva);
4501 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
4502 /* for aot code we do the lookup on load */
4503 if (aot && data_ptr)
4504 return GUINT_TO_POINTER (rva);
4511 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
4513 char *method_fname = mono_method_full_name (method, TRUE);
4516 if (mono_method_get_header (method)->code_size == 0)
4517 method_code = g_strdup ("method body is empty.");
4519 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
4520 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
4521 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
4522 g_free (method_fname);
4523 g_free (method_code);
4527 set_exception_object (MonoCompile *cfg, MonoException *exception)
4529 cfg->exception_type = MONO_EXCEPTION_OBJECT_SUPPLIED;
4530 MONO_GC_REGISTER_ROOT (cfg->exception_ptr);
4531 cfg->exception_ptr = exception;
4535 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4539 if (cfg->generic_sharing_context)
4540 type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, &klass->byval_arg);
4542 type = &klass->byval_arg;
4543 return MONO_TYPE_IS_REFERENCE (type);
4547 * mono_decompose_array_access_opts:
4549 * Decompose array access opcodes.
4552 mono_decompose_array_access_opts (MonoCompile *cfg)
4554 MonoBasicBlock *bb, *first_bb;
4557 * Unlike decompose_long_opts, this pass does not alter the CFG of the method so it
4558 * can be executed anytime. It should be run before decompose_long
4562 * Create a dummy bblock and emit code into it so we can use the normal
4563 * code generation macros.
4565 cfg->cbb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
4566 first_bb = cfg->cbb;
4568 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4570 MonoInst *prev = NULL;
4572 MonoInst *iargs [3];
4575 if (!bb->has_array_access)
4578 if (cfg->verbose_level > 3) mono_print_bb (bb, "BEFORE DECOMPOSE-ARRAY-ACCESS-OPTS ");
4580 cfg->cbb->code = cfg->cbb->last_ins = NULL;
4586 for (ins = bb->code; ins; ins = ins->next) {
4587 switch (ins->opcode) {
4589 NEW_LOAD_MEMBASE (cfg, dest, OP_LOADI4_MEMBASE, ins->dreg, ins->sreg1,
4590 G_STRUCT_OFFSET (MonoArray, max_length));
4591 MONO_ADD_INS (cfg->cbb, dest);
4593 case OP_BOUNDS_CHECK:
4594 MONO_ARCH_EMIT_BOUNDS_CHECK (cfg, ins->sreg1, ins->inst_imm, ins->sreg2);
4597 if (cfg->opt & MONO_OPT_SHARED) {
4598 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4599 EMIT_NEW_CLASSCONST (cfg, iargs [1], ins->inst_newa_class);
4600 MONO_INST_NEW (cfg, iargs [2], OP_MOVE);
4601 iargs [2]->dreg = ins->sreg1;
4603 dest = mono_emit_jit_icall (cfg, mono_array_new, iargs);
4604 dest->dreg = ins->dreg;
4606 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (ins->inst_newa_class, 1));
4609 NEW_VTABLECONST (cfg, iargs [0], vtable);
4610 MONO_ADD_INS (cfg->cbb, iargs [0]);
4611 MONO_INST_NEW (cfg, iargs [1], OP_MOVE);
4612 iargs [1]->dreg = ins->sreg1;
4614 dest = mono_emit_jit_icall (cfg, mono_array_new_specific, iargs);
4615 dest->dreg = ins->dreg;
4622 g_assert (cfg->cbb == first_bb);
4624 if (cfg->cbb->code || (cfg->cbb != first_bb)) {
4625 /* Replace the original instruction with the new code sequence */
4627 mono_replace_ins (cfg, bb, ins, &prev, first_bb, cfg->cbb);
4628 first_bb->code = first_bb->last_ins = NULL;
4629 first_bb->in_count = first_bb->out_count = 0;
4630 cfg->cbb = first_bb;
4637 if (cfg->verbose_level > 3) mono_print_bb (bb, "AFTER DECOMPOSE-ARRAY-ACCESS-OPTS ");
4647 #ifdef MONO_ARCH_SOFT_FLOAT
4650 * mono_handle_soft_float:
4652 * Soft float support on ARM. We store each double value in a pair of integer vregs,
4653 * similar to long support on 32 bit platforms. 32 bit float values require special
4654 * handling when used as locals, arguments, and in calls.
4655 * One big problem with soft-float is that there are few r4 test cases in our test suite.
4658 mono_handle_soft_float (MonoCompile *cfg)
4660 MonoBasicBlock *bb, *first_bb;
4663 * This pass creates long opcodes, so it should be run before decompose_long_opts ().
4667 * Create a dummy bblock and emit code into it so we can use the normal
4668 * code generation macros.
4670 cfg->cbb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
4671 first_bb = cfg->cbb;
4673 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4675 MonoInst *prev = NULL;
4678 if (cfg->verbose_level > 3) mono_print_bb (bb, "BEFORE HANDLE-SOFT-FLOAT ");
4680 cfg->cbb->code = cfg->cbb->last_ins = NULL;
4686 for (ins = bb->code; ins; ins = ins->next) {
4687 const char *spec = INS_INFO (ins->opcode);
4689 /* Most fp operations are handled automatically by opcode emulation */
4691 switch (ins->opcode) {
4694 d.vald = *(double*)ins->inst_p0;
4695 MONO_EMIT_NEW_I8CONST (cfg, ins->dreg, d.vall);
4700 /* We load the r8 value */
4701 d.vald = *(float*)ins->inst_p0;
4702 MONO_EMIT_NEW_I8CONST (cfg, ins->dreg, d.vall);
4706 ins->opcode = OP_LMOVE;
4709 ins->opcode = OP_MOVE;
4710 ins->sreg1 = ins->sreg1 + 1;
4713 ins->opcode = OP_MOVE;
4714 ins->sreg1 = ins->sreg1 + 2;
4717 int reg = ins->sreg1;
4719 ins->opcode = OP_SETLRET;
4721 ins->sreg1 = reg + 1;
4722 ins->sreg2 = reg + 2;
4725 case OP_LOADR8_MEMBASE:
4726 ins->opcode = OP_LOADI8_MEMBASE;
4728 case OP_STORER8_MEMBASE_REG:
4729 ins->opcode = OP_STOREI8_MEMBASE_REG;
4731 case OP_STORER4_MEMBASE_REG: {
4732 MonoInst *iargs [2];
4735 /* Arg 1 is the double value */
4736 MONO_INST_NEW (cfg, iargs [0], OP_ARG);
4737 iargs [0]->dreg = ins->sreg1;
4739 /* Arg 2 is the address to store to */
4740 addr_reg = mono_alloc_preg (cfg);
4741 EMIT_NEW_BIALU_IMM (cfg, iargs [1], OP_PADD_IMM, addr_reg, ins->inst_destbasereg, ins->inst_offset);
4742 mono_emit_jit_icall (cfg, mono_fstore_r4, iargs);
4746 case OP_LOADR4_MEMBASE: {
4747 MonoInst *iargs [1];
4751 addr_reg = mono_alloc_preg (cfg);
4752 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, addr_reg, ins->inst_basereg, ins->inst_offset);
4753 conv = mono_emit_jit_icall (cfg, mono_fload_r4, iargs);
4754 conv->dreg = ins->dreg;
4759 case OP_FCALL_MEMBASE: {
4760 MonoCallInst *call = (MonoCallInst*)ins;
4761 if (call->signature->ret->type == MONO_TYPE_R4) {
4762 MonoCallInst *call2;
4763 MonoInst *iargs [1];
4766 /* Convert the call into a call returning an int */
4767 MONO_INST_NEW_CALL (cfg, call2, OP_CALL);
4768 memcpy (call2, call, sizeof (MonoCallInst));
4769 switch (ins->opcode) {
4771 call2->inst.opcode = OP_CALL;
4774 call2->inst.opcode = OP_CALL_REG;
4776 case OP_FCALL_MEMBASE:
4777 call2->inst.opcode = OP_CALL_MEMBASE;
4780 g_assert_not_reached ();
4782 call2->inst.dreg = mono_alloc_ireg (cfg);
4783 MONO_ADD_INS (cfg->cbb, (MonoInst*)call2);
4785 /* FIXME: Optimize this */
4787 /* Emit an r4->r8 conversion */
4788 EMIT_NEW_VARLOADA_VREG (cfg, iargs [0], call2->inst.dreg, &mono_defaults.int32_class->byval_arg);
4789 conv = mono_emit_jit_icall (cfg, mono_fload_r4, iargs);
4790 conv->dreg = ins->dreg;
4792 switch (ins->opcode) {
4794 ins->opcode = OP_LCALL;
4797 ins->opcode = OP_LCALL_REG;
4799 case OP_FCALL_MEMBASE:
4800 ins->opcode = OP_LCALL_MEMBASE;
4803 g_assert_not_reached ();
4809 MonoJitICallInfo *info;
4810 MonoInst *iargs [2];
4811 MonoInst *call, *cmp, *br;
4813 /* Convert fcompare+fbcc to icall+icompare+beq */
4815 info = mono_find_jit_opcode_emulation (ins->next->opcode);
4818 /* Create dummy MonoInst's for the arguments */
4819 MONO_INST_NEW (cfg, iargs [0], OP_ARG);
4820 iargs [0]->dreg = ins->sreg1;
4821 MONO_INST_NEW (cfg, iargs [1], OP_ARG);
4822 iargs [1]->dreg = ins->sreg2;
4824 call = mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, iargs);
4826 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE_IMM);
4827 cmp->sreg1 = call->dreg;
4829 MONO_ADD_INS (cfg->cbb, cmp);
4831 MONO_INST_NEW (cfg, br, OP_IBNE_UN);
4832 br->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * 2);
4833 br->inst_true_bb = ins->next->inst_true_bb;
4834 br->inst_false_bb = ins->next->inst_false_bb;
4835 MONO_ADD_INS (cfg->cbb, br);
4837 /* The call sequence might include fp ins */
4840 /* Skip fbcc or fccc */
4841 NULLIFY_INS (ins->next);
4849 MonoJitICallInfo *info;
4850 MonoInst *iargs [2];
4853 /* Convert fccc to icall+icompare+iceq */
4855 info = mono_find_jit_opcode_emulation (ins->opcode);
4858 /* Create dummy MonoInst's for the arguments */
4859 MONO_INST_NEW (cfg, iargs [0], OP_ARG);
4860 iargs [0]->dreg = ins->sreg1;
4861 MONO_INST_NEW (cfg, iargs [1], OP_ARG);
4862 iargs [1]->dreg = ins->sreg2;
4864 call = mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, iargs);
4866 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, call->dreg, 1);
4867 MONO_EMIT_NEW_UNALU (cfg, OP_ICEQ, ins->dreg, -1);
4869 /* The call sequence might include fp ins */
4874 if (spec [MONO_INST_SRC1] == 'f' || spec [MONO_INST_SRC2] == 'f' || spec [MONO_INST_DEST] == 'f') {
4875 mono_print_ins (ins);
4876 g_assert_not_reached ();
4881 g_assert (cfg->cbb == first_bb);
4883 if (cfg->cbb->code || (cfg->cbb != first_bb)) {
4884 /* Replace the original instruction with the new code sequence */
4886 mono_replace_ins (cfg, bb, ins, &prev, first_bb, cfg->cbb);
4887 first_bb->code = first_bb->last_ins = NULL;
4888 first_bb->in_count = first_bb->out_count = 0;
4889 cfg->cbb = first_bb;
4896 if (cfg->verbose_level > 3) mono_print_bb (bb, "AFTER HANDLE-SOFT-FLOAT ");
4899 mono_decompose_long_opts (cfg);
4905 * mono_method_to_ir: translates IL into basic blocks containing trees
4908 mono_method_to_ir2 (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
4909 MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
4910 guint inline_offset, gboolean is_virtual_call)
4912 MonoInst *ins, **sp, **stack_start;
4913 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
4914 MonoMethod *cmethod, *method_definition;
4915 MonoInst **arg_array;
4916 MonoMethodHeader *header;
4918 guint32 token, ins_flag;
4920 MonoClass *constrained_call = NULL;
4921 unsigned char *ip, *end, *target, *err_pos;
4922 static double r8_0 = 0.0;
4923 MonoMethodSignature *sig;
4924 MonoGenericContext *generic_context = NULL;
4925 MonoGenericContainer *generic_container = NULL;
4926 MonoType **param_types;
4927 GList *bb_recheck = NULL, *tmp;
4928 int i, n, start_new_bblock, dreg;
4929 int num_calls = 0, inline_costs = 0;
4930 int breakpoint_id = 0;
4932 MonoBoolean security, pinvoke;
4933 MonoSecurityManager* secman = NULL;
4934 MonoDeclSecurityActions actions;
4935 GSList *class_inits = NULL;
4936 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
4939 /* serialization and xdomain stuff may need access to private fields and methods */
4940 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
4941 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
4942 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
4943 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
4944 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
4945 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
4947 dont_verify |= mono_security_get_mode () == MONO_SECURITY_MODE_SMCS_HACK;
4949 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
4950 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
4951 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
4952 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
4954 image = method->klass->image;
4955 header = mono_method_get_header (method);
4956 generic_container = mono_method_get_generic_container (method);
4957 sig = mono_method_signature (method);
4958 num_args = sig->hasthis + sig->param_count;
4959 ip = (unsigned char*)header->code;
4960 cfg->cil_start = ip;
4961 end = ip + header->code_size;
4962 mono_jit_stats.cil_code_size += header->code_size;
4964 method_definition = method;
4965 while (method_definition->is_inflated) {
4966 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
4967 method_definition = imethod->declaring;
4970 /* SkipVerification is not allowed if core-clr is enabled */
4971 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
4973 dont_verify_stloc = TRUE;
4976 if (!dont_verify && mini_method_verify (cfg, method_definition))
4977 goto exception_exit;
4979 if (sig->is_inflated)
4980 generic_context = mono_method_get_context (method);
4981 else if (generic_container)
4982 generic_context = &generic_container->context;
4983 cfg->generic_context = generic_context;
4985 if (!cfg->generic_sharing_context)
4986 g_assert (!sig->has_type_parameters);
4988 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
4989 g_assert (method->is_inflated);
4990 g_assert (mono_method_get_context (method)->method_inst);
4992 if (method->is_inflated && mono_method_get_context (method)->method_inst)
4993 g_assert (sig->generic_param_count);
4995 if (cfg->method == method) {
4996 cfg->real_offset = 0;
4998 cfg->real_offset = inline_offset;
5001 cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
5002 cfg->cil_offset_to_bb_len = header->code_size;
5004 cfg->current_method = method;
5006 if (cfg->verbose_level > 2)
5007 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
5009 dont_inline = g_list_prepend (dont_inline, method);
5010 if (cfg->method == method) {
5012 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
5013 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
5016 NEW_BBLOCK (cfg, start_bblock);
5017 cfg->bb_entry = start_bblock;
5018 start_bblock->cil_code = NULL;
5019 start_bblock->cil_length = 0;
5022 NEW_BBLOCK (cfg, end_bblock);
5023 cfg->bb_exit = end_bblock;
5024 end_bblock->cil_code = NULL;
5025 end_bblock->cil_length = 0;
5026 g_assert (cfg->num_bblocks == 2);
5028 arg_array = cfg->args;
5030 if (header->num_clauses) {
5031 cfg->spvars = g_hash_table_new (NULL, NULL);
5032 cfg->exvars = g_hash_table_new (NULL, NULL);
5034 /* handle exception clauses */
5035 for (i = 0; i < header->num_clauses; ++i) {
5036 MonoBasicBlock *try_bb;
5037 MonoExceptionClause *clause = &header->clauses [i];
5038 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
5039 try_bb->real_offset = clause->try_offset;
5040 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
5041 tblock->real_offset = clause->handler_offset;
5042 tblock->flags |= BB_EXCEPTION_HANDLER;
5044 link_bblock (cfg, try_bb, tblock);
5046 if (*(ip + clause->handler_offset) == CEE_POP)
5047 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
5049 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
5050 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
5051 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
5052 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5053 MONO_ADD_INS (tblock, ins);
5055 /* todo: is a fault block unsafe to optimize? */
5056 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
5057 tblock->flags |= BB_EXCEPTION_UNSAFE;
5061 /*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);
5063 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
5065 /* catch and filter blocks get the exception object on the stack */
5066 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
5067 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5068 MonoInst *dummy_use;
5070 /* mostly like handle_stack_args (), but just sets the input args */
5071 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
5072 tblock->in_scount = 1;
5073 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5074 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5077 * Add a dummy use for the exvar so its liveness info will be
5081 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
5083 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5084 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
5085 tblock->real_offset = clause->data.filter_offset;
5086 tblock->in_scount = 1;
5087 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5088 /* The filter block shares the exvar with the handler block */
5089 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5090 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5091 MONO_ADD_INS (tblock, ins);
5095 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
5096 clause->data.catch_class &&
5097 cfg->generic_sharing_context &&
5098 mono_class_check_context_used (clause->data.catch_class)) {
5099 if (mono_method_get_context (method)->method_inst)
5100 GENERIC_SHARING_FAILURE (CEE_NOP);
5103 * In shared generic code with catch
5104 * clauses containing type variables
5105 * the exception handling code has to
5106 * be able to get to the rgctx.
5107 * Therefore we have to make sure that
5108 * the vtable/mrgctx argument (for
5109 * static or generic methods) or the
5110 * "this" argument (for non-static
5111 * methods) are live.
5113 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
5114 mini_method_get_context (method)->method_inst) {
5115 mono_get_vtable_var (cfg);
5117 MonoInst *dummy_use;
5119 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
5124 arg_array = alloca (sizeof (MonoInst *) * num_args);
5125 cfg->cbb = start_bblock;
5126 mono_save_args (cfg, sig, inline_args, arg_array);
5127 cfg->args = arg_array;
5130 /* FIRST CODE BLOCK */
5131 NEW_BBLOCK (cfg, bblock);
5132 bblock->cil_code = ip;
5136 ADD_BBLOCK (cfg, bblock);
5138 if (cfg->method == method) {
5139 breakpoint_id = mono_debugger_method_has_breakpoint (method);
5140 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
5141 MONO_INST_NEW (cfg, ins, OP_BREAK);
5142 MONO_ADD_INS (bblock, ins);
5146 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
5147 secman = mono_security_manager_get_methods ();
5149 security = (secman && mono_method_has_declsec (method));
5150 /* at this point having security doesn't mean we have any code to generate */
5151 if (security && (cfg->method == method)) {
5152 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
5153 * And we do not want to enter the next section (with allocation) if we
5154 * have nothing to generate */
5155 security = mono_declsec_get_demands (method, &actions);
5158 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
5159 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
5161 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5162 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5163 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
5165 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
5166 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5170 mono_custom_attrs_free (custom);
5173 custom = mono_custom_attrs_from_class (wrapped->klass);
5174 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5178 mono_custom_attrs_free (custom);
5181 /* not a P/Invoke after all */
5186 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || cfg->compile_aot || security || pinvoke) {
5187 /* we use a separate basic block for the initialization code */
5188 NEW_BBLOCK (cfg, init_localsbb);
5189 cfg->bb_init = init_localsbb;
5190 init_localsbb->real_offset = cfg->real_offset;
5191 start_bblock->next_bb = init_localsbb;
5192 init_localsbb->next_bb = bblock;
5193 link_bblock (cfg, start_bblock, init_localsbb);
5194 link_bblock (cfg, init_localsbb, bblock);
5196 cfg->cbb = init_localsbb;
5198 start_bblock->next_bb = bblock;
5199 link_bblock (cfg, start_bblock, bblock);
5202 /* at this point we know, if security is TRUE, that some code needs to be generated */
5203 if (security && (cfg->method == method)) {
5206 mono_jit_stats.cas_demand_generation++;
5208 if (actions.demand.blob) {
5209 /* Add code for SecurityAction.Demand */
5210 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
5211 EMIT_NEW_ICONST (cfg, args [1], actions.demand.size);
5212 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5213 mono_emit_method_call (cfg, secman->demand, args, NULL);
5215 if (actions.noncasdemand.blob) {
5216 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
5217 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
5218 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
5219 EMIT_NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
5220 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5221 mono_emit_method_call (cfg, secman->demand, args, NULL);
5223 if (actions.demandchoice.blob) {
5224 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
5225 EMIT_NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
5226 EMIT_NEW_ICONST (cfg, args [1], actions.demandchoice.size);
5227 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
5228 mono_emit_method_call (cfg, secman->demandchoice, args, NULL);
5232 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
5234 mono_emit_method_call (cfg, secman->demandunmanaged, NULL, NULL);
5237 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
5238 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
5239 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5240 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5241 if (!(method->klass && method->klass->image &&
5242 mono_security_core_clr_is_platform_image (method->klass->image))) {
5243 emit_throw_method_access_exception (cfg, method, wrapped, bblock, ip);
5247 if (!method_is_safe (method))
5248 emit_throw_verification_exception (cfg, bblock, ip);
5251 if (header->code_size == 0)
5254 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
5259 if (cfg->method == method)
5260 mono_debug_init_method (cfg, bblock, breakpoint_id);
5262 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
5264 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
5265 for (n = 0; n < sig->param_count; ++n)
5266 param_types [n + sig->hasthis] = sig->params [n];
5267 cfg->arg_types = param_types;
5268 for (n = 0; n < header->num_locals; ++n) {
5269 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
5274 /* add a check for this != NULL to inlined methods */
5275 if (is_virtual_call) {
5278 NEW_ARGLOAD (cfg, arg_ins, 0);
5279 MONO_ADD_INS (cfg->cbb, arg_ins);
5280 cfg->flags |= MONO_CFG_HAS_CHECK_THIS;
5281 MONO_EMIT_NEW_UNALU (cfg, OP_CHECK_THIS, -1, arg_ins->dreg);
5282 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, arg_ins->dreg);
5285 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
5286 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
5289 start_new_bblock = 0;
5293 if (cfg->method == method)
5294 cfg->real_offset = ip - header->code;
5296 cfg->real_offset = inline_offset;
5301 if (start_new_bblock) {
5302 bblock->cil_length = ip - bblock->cil_code;
5303 if (start_new_bblock == 2) {
5304 g_assert (ip == tblock->cil_code);
5306 GET_BBLOCK (cfg, tblock, ip);
5308 bblock->next_bb = tblock;
5311 start_new_bblock = 0;
5312 for (i = 0; i < bblock->in_scount; ++i) {
5313 if (cfg->verbose_level > 3)
5314 printf ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5315 EMIT_NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5319 g_slist_free (class_inits);
5322 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
5323 link_bblock (cfg, bblock, tblock);
5324 if (sp != stack_start) {
5325 handle_stack_args (cfg, stack_start, sp - stack_start);
5327 CHECK_UNVERIFIABLE (cfg);
5329 bblock->next_bb = tblock;
5332 for (i = 0; i < bblock->in_scount; ++i) {
5333 if (cfg->verbose_level > 3)
5334 printf ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5335 EMIT_NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5338 g_slist_free (class_inits);
5343 bblock->real_offset = cfg->real_offset;
5345 if ((cfg->method == method) && cfg->coverage_info) {
5346 guint32 cil_offset = ip - header->code;
5347 cfg->coverage_info->data [cil_offset].cil_code = ip;
5349 /* TODO: Use an increment here */
5350 #if defined(__i386__)
5351 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
5352 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
5354 MONO_ADD_INS (cfg->cbb, ins);
5356 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
5357 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
5361 if (cfg->verbose_level > 3)
5362 printf ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
5367 MONO_INST_NEW (cfg, ins, (*ip) == CEE_NOP ? OP_NOP : OP_BREAK);
5369 MONO_ADD_INS (bblock, ins);
5375 CHECK_STACK_OVF (1);
5376 n = (*ip)-CEE_LDARG_0;
5378 EMIT_NEW_ARGLOAD (cfg, ins, n);
5386 CHECK_STACK_OVF (1);
5387 n = (*ip)-CEE_LDLOC_0;
5389 EMIT_NEW_LOCLOAD (cfg, ins, n);
5400 n = (*ip)-CEE_STLOC_0;
5403 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
5406 opcode = mono_type_to_regmove (cfg, header->locals [n]);
5407 if ((opcode == OP_MOVE) && ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
5408 /* Optimize reg-reg moves away */
5410 * Can't optimize other opcodes, since sp[0] might point to
5411 * the last ins of a decomposed opcode.
5413 sp [0]->dreg = (cfg)->locals [n]->dreg;
5415 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
5423 CHECK_STACK_OVF (1);
5426 EMIT_NEW_ARGLOAD (cfg, ins, n);
5432 CHECK_STACK_OVF (1);
5435 NEW_ARGLOADA (cfg, ins, n);
5436 MONO_ADD_INS (cfg->cbb, ins);
5446 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
5448 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
5453 CHECK_STACK_OVF (1);
5456 EMIT_NEW_LOCLOAD (cfg, ins, n);
5462 CHECK_STACK_OVF (1);
5463 CHECK_LOCAL (ip [1]);
5466 * ldloca inhibits many optimizations so try to get rid of it in common
5469 if (ip + 8 < end && (ip [2] == CEE_PREFIX1) && (ip [3] == CEE_INITOBJ) && ip_in_bb (cfg, bblock, ip + 3)) {
5470 gboolean skip = FALSE;
5472 /* From the INITOBJ case */
5473 token = read32 (ip + 4);
5474 klass = mini_get_class (method, token, generic_context);
5475 CHECK_TYPELOAD (klass);
5476 if (generic_class_is_reference_type (cfg, klass)) {
5477 MONO_EMIT_NEW_PCONST (cfg, cfg->locals [ip [1]]->dreg, NULL);
5478 } else if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5479 MONO_EMIT_NEW_PCONST (cfg, cfg->locals [ip [1]]->dreg, NULL);
5480 } else if (MONO_TYPE_ISSTRUCT (&klass->byval_arg)) {
5481 MONO_EMIT_NEW_VZERO (cfg, cfg->locals [ip [1]]->dreg, klass);
5493 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
5501 CHECK_LOCAL (ip [1]);
5502 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
5504 EMIT_NEW_LOCSTORE (cfg, ins, ip [1], *sp);
5509 CHECK_STACK_OVF (1);
5510 EMIT_NEW_PCONST (cfg, ins, NULL);
5511 ins->type = STACK_OBJ;
5516 CHECK_STACK_OVF (1);
5517 EMIT_NEW_ICONST (cfg, ins, -1);
5530 CHECK_STACK_OVF (1);
5531 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
5537 CHECK_STACK_OVF (1);
5539 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
5545 CHECK_STACK_OVF (1);
5546 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
5552 CHECK_STACK_OVF (1);
5553 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5554 ins->type = STACK_I8;
5555 ins->dreg = alloc_dreg (cfg, STACK_I8);
5557 ins->inst_l = (gint64)read64 (ip);
5558 MONO_ADD_INS (bblock, ins);
5564 /* FIXME: we should really allocate this only late in the compilation process */
5565 mono_domain_lock (cfg->domain);
5566 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
5567 mono_domain_unlock (cfg->domain);
5569 CHECK_STACK_OVF (1);
5570 MONO_INST_NEW (cfg, ins, OP_R4CONST);
5571 ins->type = STACK_R8;
5572 ins->dreg = alloc_dreg (cfg, STACK_R8);
5576 MONO_ADD_INS (bblock, ins);
5584 /* FIXME: we should really allocate this only late in the compilation process */
5585 mono_domain_lock (cfg->domain);
5586 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
5587 mono_domain_unlock (cfg->domain);
5589 CHECK_STACK_OVF (1);
5590 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5591 ins->type = STACK_R8;
5592 ins->dreg = alloc_dreg (cfg, STACK_R8);
5596 MONO_ADD_INS (bblock, ins);
5603 MonoInst *temp, *store;
5605 CHECK_STACK_OVF (1);
5609 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5610 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5612 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5615 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5628 if (sp [0]->type == STACK_R8)
5629 /* we need to pop the value from the x86 FP stack */
5630 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
5637 if (stack_start != sp)
5639 token = read32 (ip + 1);
5640 /* FIXME: check the signature matches */
5641 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
5646 if (cfg->generic_sharing_context && mono_method_check_context_used (cmethod))
5647 GENERIC_SHARING_FAILURE (CEE_JMP);
5649 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
5650 if (check_linkdemand (cfg, method, cmethod))
5652 CHECK_CFG_EXCEPTION;
5657 MonoMethodSignature *fsig = mono_method_signature (cmethod);
5660 /* FIXME: Remove OP_JMP from mini-amd64.c when the old JIT is removed */
5662 /* Handle tail calls similarly to calls */
5663 n = fsig->param_count + fsig->hasthis;
5665 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
5666 call->method = cmethod;
5667 call->tail_call = TRUE;
5668 call->signature = mono_method_signature (cmethod);
5669 call->args = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
5670 call->inst.inst_p0 = cmethod;
5671 for (i = 0; i < n; ++i)
5672 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
5674 mono_arch_emit_call (cfg, call);
5675 MONO_ADD_INS (bblock, (MonoInst*)call);
5678 for (i = 0; i < num_args; ++i)
5679 /* Prevent arguments from being optimized away */
5680 arg_array [i]->flags |= MONO_INST_VOLATILE;
5682 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
5683 ins = (MonoInst*)call;
5684 ins->inst_p0 = cmethod;
5685 MONO_ADD_INS (bblock, ins);
5689 start_new_bblock = 1;
5694 case CEE_CALLVIRT: {
5695 MonoInst *addr = NULL;
5696 MonoMethodSignature *fsig = NULL;
5698 int virtual = *ip == CEE_CALLVIRT;
5699 int calli = *ip == CEE_CALLI;
5700 gboolean pass_imt_from_rgctx = FALSE;
5701 MonoInst *imt_arg = NULL;
5702 gboolean pass_vtable = FALSE;
5703 gboolean pass_mrgctx = FALSE;
5704 MonoInst *vtable_arg = NULL;
5705 gboolean check_this = FALSE;
5708 token = read32 (ip + 1);
5715 if (method->wrapper_type != MONO_WRAPPER_NONE)
5716 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
5718 fsig = mono_metadata_parse_signature (image, token);
5720 n = fsig->param_count + fsig->hasthis;
5722 MonoMethod *cil_method;
5724 if (method->wrapper_type != MONO_WRAPPER_NONE) {
5725 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
5726 cil_method = cmethod;
5727 } else if (constrained_call) {
5728 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
5730 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
5731 cil_method = cmethod;
5736 if (!dont_verify && !cfg->skip_visibility) {
5737 MonoMethod *target_method = cil_method;
5738 if (method->is_inflated) {
5739 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
5741 if (!mono_method_can_access_method (method_definition, target_method) &&
5742 !mono_method_can_access_method (method, cil_method))
5743 METHOD_ACCESS_FAILURE;
5746 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
5747 ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
5749 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
5750 /* MS.NET seems to silently convert this to a callvirt */
5753 if (!cmethod->klass->inited)
5754 if (!mono_class_init (cmethod->klass))
5757 if (mono_method_signature (cmethod)->pinvoke) {
5758 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
5759 fsig = mono_method_signature (wrapper);
5760 } else if (constrained_call) {
5761 fsig = mono_method_signature (cmethod);
5763 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
5766 mono_save_token_info (cfg, image, token, cmethod);
5768 n = fsig->param_count + fsig->hasthis;
5770 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
5771 if (check_linkdemand (cfg, method, cmethod))
5773 CHECK_CFG_EXCEPTION;
5776 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
5777 mini_class_is_system_array (cmethod->klass)) {
5778 array_rank = cmethod->klass->rank;
5781 if (cmethod->string_ctor)
5782 g_assert_not_reached ();
5785 if (!cfg->generic_sharing_context && cmethod && cmethod->klass->generic_container)
5788 if (!cfg->generic_sharing_context && cmethod)
5789 g_assert (!mono_method_check_context_used (cmethod));
5793 //g_assert (!virtual || fsig->hasthis);
5797 if (constrained_call) {
5799 * We have the `constrained.' prefix opcode.
5801 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
5805 * The type parameter is instantiated as a valuetype,
5806 * but that type doesn't override the method we're
5807 * calling, so we need to box `this'.
5809 dreg = alloc_dreg (cfg, STACK_VTYPE);
5810 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADV_MEMBASE, dreg, sp [0]->dreg, 0);
5811 ins->klass = constrained_call;
5812 sp [0] = handle_box (cfg, ins, constrained_call);
5813 } else if (!constrained_call->valuetype) {
5814 int dreg = alloc_preg (cfg);
5817 * The type parameter is instantiated as a reference
5818 * type. We have a managed pointer on the stack, so
5819 * we need to dereference it here.
5821 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
5822 ins->type = STACK_OBJ;
5824 } else if (cmethod->klass->valuetype)
5826 constrained_call = NULL;
5829 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
5833 if (cmethod && (cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
5834 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
5835 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
5836 MonoGenericContext *context = mini_class_get_context (cmethod->klass);
5837 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
5840 * Pass vtable iff target method might
5841 * be shared, which means that sharing
5842 * is enabled for its class and its
5843 * context is sharable (and it's not a
5846 if (sharing_enabled && context_sharable &&
5847 !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
5851 if (cmethod && mini_method_get_context (cmethod) &&
5852 mini_method_get_context (cmethod)->method_inst) {
5853 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
5854 MonoGenericContext *context = mini_method_get_context (cmethod);
5855 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
5857 g_assert (!pass_vtable);
5859 if (sharing_enabled && context_sharable)
5863 if (cfg->generic_sharing_context && cmethod) {
5864 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
5866 context_used = mono_method_check_context_used (cmethod);
5868 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
5869 /* Generic method interface
5870 calls are resolved via a
5871 helper function and don't
5873 if (!cmethod_context || !cmethod_context->method_inst)
5874 pass_imt_from_rgctx = TRUE;
5878 * If a shared method calls another
5879 * shared method then the caller must
5880 * have a generic sharing context
5881 * because the magic trampoline
5882 * requires it. FIXME: We shouldn't
5883 * have to force the vtable/mrgctx
5884 * variable here. Instead there
5885 * should be a flag in the cfg to
5886 * request a generic sharing context.
5888 if (context_used && method->flags & METHOD_ATTRIBUTE_STATIC)
5889 mono_get_vtable_var (cfg);
5896 EMIT_GET_RGCTX (rgctx, context_used);
5897 vtable_arg = emit_get_rgctx_klass (cfg, context_used, rgctx, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
5899 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
5901 CHECK_TYPELOAD (cmethod->klass);
5902 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
5907 g_assert (!vtable_arg);
5912 EMIT_GET_RGCTX (rgctx, context_used);
5913 vtable_arg = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
5915 EMIT_NEW_METHOD_RGCTX_CONST (cfg, vtable_arg, cmethod);
5918 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
5919 (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) {
5926 if (pass_imt_from_rgctx) {
5929 g_assert (!pass_vtable);
5932 EMIT_GET_RGCTX (rgctx, context_used);
5933 imt_arg = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod,
5934 MONO_RGCTX_INFO_METHOD);
5940 MONO_INST_NEW (cfg, check, OP_CHECK_THIS);
5941 check->sreg1 = sp [0]->dreg;
5942 MONO_ADD_INS (cfg->cbb, check);
5945 /* Calling virtual generic methods */
5946 if (cmethod && virtual &&
5947 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
5948 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
5949 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
5950 mono_method_signature (cmethod)->generic_param_count) {
5951 MonoInst *this_temp, *this_arg_temp, *store;
5952 MonoInst *iargs [4];
5954 g_assert (mono_method_signature (cmethod)->is_inflated);
5956 /* Prevent inlining of methods that contain indirect calls */
5959 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
5960 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
5961 MONO_ADD_INS (bblock, store);
5963 /* FIXME: This should be a managed pointer */
5964 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
5966 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
5970 EMIT_GET_RGCTX (rgctx, context_used);
5971 iargs [1] = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod, MONO_RGCTX_INFO_METHOD);
5972 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
5973 addr = mono_emit_jit_icall (cfg,
5974 mono_helper_compile_generic_method, iargs);
5976 EMIT_NEW_METHODCONST (cfg, iargs [1], cmethod);
5977 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
5978 addr = mono_emit_jit_icall (cfg, mono_helper_compile_generic_method, iargs);
5981 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
5983 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr);
5984 if (!MONO_TYPE_IS_VOID (fsig->ret))
5993 /* FIXME: runtime generic context pointer for jumps? */
5994 /* FIXME: handle this for generic sharing eventually */
5995 if ((ins_flag & MONO_INST_TAILCALL) && !cfg->generic_sharing_context && !vtable_arg && cmethod && (*ip == CEE_CALL) &&
5996 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
5999 /* FIXME: runtime generic context pointer for jumps? */
6000 GENERIC_SHARING_FAILURE (*ip);
6002 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6005 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
6006 call->tail_call = TRUE;
6007 call->method = cmethod;
6008 call->signature = mono_method_signature (cmethod);
6011 /* Handle tail calls similarly to calls */
6012 call->inst.opcode = OP_TAILCALL;
6014 mono_arch_emit_call (cfg, call);
6017 * We implement tail calls by storing the actual arguments into the
6018 * argument variables, then emitting a CEE_JMP.
6020 for (i = 0; i < n; ++i) {
6021 /* Prevent argument from being register allocated */
6022 arg_array [i]->flags |= MONO_INST_VOLATILE;
6023 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
6027 ins = (MonoInst*)call;
6028 ins->inst_p0 = cmethod;
6029 ins->inst_p1 = arg_array [0];
6030 MONO_ADD_INS (bblock, ins);
6031 link_bblock (cfg, bblock, end_bblock);
6032 start_new_bblock = 1;
6033 /* skip CEE_RET as well */
6039 /* Conversion to a JIT intrinsic */
6040 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
6041 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6042 type_to_eval_stack_type ((cfg), fsig->ret, ins);
6053 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
6054 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
6055 mono_method_check_inlining (cfg, cmethod) &&
6056 !g_list_find (dont_inline, cmethod)) {
6058 gboolean allways = FALSE;
6060 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6061 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6062 /* Prevent inlining of methods that call wrappers */
6064 cmethod = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6068 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, dont_inline, allways))) {
6070 cfg->real_offset += 5;
6073 if (!MONO_TYPE_IS_VOID (fsig->ret))
6074 /* *sp is already set by inline_method */
6077 inline_costs += costs;
6083 inline_costs += 10 * num_calls++;
6085 /* Tail recursion elimination */
6086 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
6087 gboolean has_vtargs = FALSE;
6090 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6093 /* keep it simple */
6094 for (i = fsig->param_count - 1; i >= 0; i--) {
6095 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
6100 for (i = 0; i < n; ++i)
6101 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
6102 MONO_INST_NEW (cfg, ins, OP_BR);
6103 MONO_ADD_INS (bblock, ins);
6104 tblock = start_bblock->out_bb [0];
6105 link_bblock (cfg, bblock, tblock);
6106 ins->inst_target_bb = tblock;
6107 start_new_bblock = 1;
6109 /* skip the CEE_RET, too */
6110 if (ip_in_bb (cfg, bblock, ip + 5))
6120 /* Generic sharing */
6121 /* FIXME: only do this for generic methods if
6122 they are not shared! */
6124 (cmethod->klass->valuetype ||
6125 (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst && !pass_mrgctx) ||
6126 ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
6127 mono_class_generic_sharing_enabled (cmethod->klass)) ||
6128 (!imt_arg && !mono_method_is_generic_sharable_impl (cmethod, TRUE) &&
6129 (!virtual || cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6130 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))))) {
6135 g_assert (cfg->generic_sharing_context && cmethod);
6139 * We are compiling a call to a
6140 * generic method from shared code,
6141 * which means that we have to look up
6142 * the method in the rgctx and do an
6146 EMIT_GET_RGCTX (rgctx, context_used);
6147 addr = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
6150 /* Indirect calls */
6152 g_assert (!imt_arg);
6154 if (*ip == CEE_CALL)
6155 g_assert (context_used);
6156 else if (*ip == CEE_CALLI)
6157 g_assert (!vtable_arg);
6159 g_assert (cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6160 !(cmethod->flags & METHOD_ATTRIBUTE_FINAL));
6162 /* Prevent inlining of methods with indirect calls */
6166 #ifdef MONO_ARCH_RGCTX_REG
6168 int rgctx_reg = mono_alloc_preg (cfg);
6170 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, vtable_arg->dreg);
6171 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr);
6172 call = (MonoCallInst*)ins;
6173 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
6174 cfg->uses_rgctx_reg = TRUE;
6179 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr);
6181 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6182 if (fsig->pinvoke && !fsig->ret->byref) {
6186 * Native code might return non register sized integers
6187 * without initializing the upper bits.
6189 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
6190 case OP_LOADI1_MEMBASE:
6191 widen_op = OP_ICONV_TO_I1;
6193 case OP_LOADU1_MEMBASE:
6194 widen_op = OP_ICONV_TO_U1;
6196 case OP_LOADI2_MEMBASE:
6197 widen_op = OP_ICONV_TO_I2;
6199 case OP_LOADU2_MEMBASE:
6200 widen_op = OP_ICONV_TO_U2;
6206 if (widen_op != -1) {
6207 int dreg = alloc_preg (cfg);
6210 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
6211 widen->type = ins->type;
6228 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
6229 if (sp [fsig->param_count]->type == STACK_OBJ) {
6230 MonoInst *iargs [2];
6233 iargs [1] = sp [fsig->param_count];
6235 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
6238 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
6239 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, sp [fsig->param_count]->dreg);
6240 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
6241 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
6243 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
6246 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
6247 if (!cmethod->klass->element_class->valuetype && !readonly)
6248 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
6251 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
6254 g_assert_not_reached ();
6262 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL);
6264 if (!MONO_TYPE_IS_VOID (fsig->ret))
6275 #ifdef MONO_ARCH_RGCTX_REG
6277 int rgctx_reg = mono_alloc_preg (cfg);
6279 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, vtable_arg->dreg);
6280 ins = (MonoInst*)mono_emit_method_call_full (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL, NULL);
6281 call = (MonoCallInst*)ins;
6282 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
6283 cfg->uses_rgctx_reg = TRUE;
6287 } else if (imt_arg) {
6288 ins = (MonoInst*)mono_emit_method_call_full (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL, imt_arg);
6290 ins = (MonoInst*)mono_emit_method_call_full (cfg, cmethod, fsig, sp, virtual ? sp [0] : NULL, NULL);
6293 if (!MONO_TYPE_IS_VOID (fsig->ret))
6301 if (cfg->method != method) {
6302 /* return from inlined method */
6304 * If in_count == 0, that means the ret is unreachable due to
6305 * being preceeded by a throw. In that case, inline_method () will
6306 * handle setting the return value
6307 * (test case: test_0_inline_throw ()).
6309 if (return_var && cfg->cbb->in_count) {
6313 //g_assert (returnvar != -1);
6314 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
6315 cfg->ret_var_set = TRUE;
6319 MonoType *ret_type = mono_method_signature (method)->ret;
6321 g_assert (!return_var);
6324 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
6327 if (!cfg->vret_addr) {
6330 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, (*sp));
6332 EMIT_NEW_RETLOADA (cfg, ret_addr);
6334 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, (*sp)->dreg);
6335 ins->klass = mono_class_from_mono_type (ret_type);
6338 #ifdef MONO_ARCH_SOFT_FLOAT
6339 if (!ret_type->byref && ret_type->type == MONO_TYPE_R4) {
6340 MonoInst *iargs [1];
6344 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
6345 mono_arch_emit_setret (cfg, method, conv);
6347 mono_arch_emit_setret (cfg, method, *sp);
6350 mono_arch_emit_setret (cfg, method, *sp);
6355 if (sp != stack_start)
6357 MONO_INST_NEW (cfg, ins, OP_BR);
6359 ins->inst_target_bb = end_bblock;
6360 MONO_ADD_INS (bblock, ins);
6361 link_bblock (cfg, bblock, end_bblock);
6362 start_new_bblock = 1;
6366 MONO_INST_NEW (cfg, ins, OP_BR);
6368 target = ip + 1 + (signed char)(*ip);
6370 GET_BBLOCK (cfg, tblock, target);
6371 link_bblock (cfg, bblock, tblock);
6372 CHECK_BBLOCK (target, ip, tblock);
6373 ins->inst_target_bb = tblock;
6374 if (sp != stack_start) {
6375 handle_stack_args (cfg, stack_start, sp - stack_start);
6377 CHECK_UNVERIFIABLE (cfg);
6379 MONO_ADD_INS (bblock, ins);
6380 start_new_bblock = 1;
6381 inline_costs += BRANCH_COST;
6395 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6397 target = ip + 1 + *(signed char*)ip;
6403 inline_costs += BRANCH_COST;
6407 MONO_INST_NEW (cfg, ins, OP_BR);
6410 target = ip + 4 + (gint32)read32(ip);
6412 GET_BBLOCK (cfg, tblock, target);
6413 link_bblock (cfg, bblock, tblock);
6414 CHECK_BBLOCK (target, ip, tblock);
6415 ins->inst_target_bb = tblock;
6416 if (sp != stack_start) {
6417 handle_stack_args (cfg, stack_start, sp - stack_start);
6419 CHECK_UNVERIFIABLE (cfg);
6422 MONO_ADD_INS (bblock, ins);
6424 start_new_bblock = 1;
6425 inline_costs += BRANCH_COST;
6432 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
6433 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
6434 guint32 opsize = is_short ? 1 : 4;
6436 CHECK_OPSIZE (opsize);
6438 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6441 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
6446 GET_BBLOCK (cfg, tblock, target);
6447 link_bblock (cfg, bblock, tblock);
6448 CHECK_BBLOCK (target, ip, tblock);
6449 GET_BBLOCK (cfg, tblock, ip);
6450 link_bblock (cfg, bblock, tblock);
6452 if (sp != stack_start) {
6453 handle_stack_args (cfg, stack_start, sp - stack_start);
6454 CHECK_UNVERIFIABLE (cfg);
6457 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
6458 cmp->sreg1 = sp [0]->dreg;
6459 type_from_op (cmp, sp [0], NULL);
6462 #if SIZEOF_VOID_P == 4
6463 if (cmp->opcode == OP_LCOMPARE_IMM) {
6464 /* Convert it to OP_LCOMPARE */
6465 MONO_INST_NEW (cfg, ins, OP_I8CONST);
6466 ins->type = STACK_I8;
6467 ins->dreg = alloc_dreg (cfg, STACK_I8);
6469 MONO_ADD_INS (bblock, ins);
6470 cmp->opcode = OP_LCOMPARE;
6471 cmp->sreg2 = ins->dreg;
6474 MONO_ADD_INS (bblock, cmp);
6476 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
6477 type_from_op (ins, sp [0], NULL);
6478 MONO_ADD_INS (bblock, ins);
6479 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
6480 GET_BBLOCK (cfg, tblock, target);
6481 ins->inst_true_bb = tblock;
6482 GET_BBLOCK (cfg, tblock, ip);
6483 ins->inst_false_bb = tblock;
6484 start_new_bblock = 2;
6487 inline_costs += BRANCH_COST;
6502 MONO_INST_NEW (cfg, ins, *ip);
6504 target = ip + 4 + (gint32)read32(ip);
6510 inline_costs += BRANCH_COST;
6514 MonoBasicBlock **targets;
6515 MonoBasicBlock *default_bblock;
6516 MonoJumpInfoBBTable *table;
6518 int offset_reg = alloc_preg (cfg);
6519 int target_reg = alloc_preg (cfg);
6520 int table_reg = alloc_preg (cfg);
6521 int sum_reg = alloc_preg (cfg);
6526 n = read32 (ip + 1);
6529 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
6533 CHECK_OPSIZE (n * sizeof (guint32));
6534 target = ip + n * sizeof (guint32);
6536 GET_BBLOCK (cfg, default_bblock, target);
6538 targets = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
6539 for (i = 0; i < n; ++i) {
6540 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
6541 targets [i] = tblock;
6545 if (sp != stack_start) {
6547 * Link the current bb with the targets as well, so handle_stack_args
6548 * will set their in_stack correctly.
6550 link_bblock (cfg, bblock, default_bblock);
6551 for (i = 0; i < n; ++i)
6552 link_bblock (cfg, bblock, targets [i]);
6554 handle_stack_args (cfg, stack_start, sp - stack_start);
6556 CHECK_UNVERIFIABLE (cfg);
6559 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
6560 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
6563 for (i = 0; i < n; ++i)
6564 link_bblock (cfg, bblock, targets [i]);
6566 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
6567 table->table = targets;
6568 table->table_size = n;
6571 /* ARM implements SWITCH statements differently */
6572 /* FIXME: Make it use the generic implementation */
6573 /* the backend code will deal with aot vs normal case */
6574 MONO_INST_NEW (cfg, ins, OP_SWITCH);
6575 ins->sreg1 = src1->dreg;
6576 ins->inst_p0 = table;
6577 ins->inst_many_bb = targets;
6578 ins->klass = GUINT_TO_POINTER (n);
6579 MONO_ADD_INS (cfg->cbb, ins);
6581 if (sizeof (gpointer) == 8)
6582 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
6584 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
6586 #if SIZEOF_VOID_P == 8
6587 /* The upper word might not be zero, and we add it to a 64 bit address later */
6588 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
6591 if (cfg->compile_aot) {
6592 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
6594 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
6595 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
6596 ins->inst_p0 = table;
6597 ins->dreg = table_reg;
6598 MONO_ADD_INS (cfg->cbb, ins);
6601 /* FIXME: Use load_memindex */
6602 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
6603 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
6604 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
6606 start_new_bblock = 1;
6607 inline_costs += (BRANCH_COST * 2);
6627 dreg = alloc_freg (cfg);
6630 dreg = alloc_lreg (cfg);
6633 dreg = alloc_preg (cfg);
6636 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
6637 ins->type = ldind_type [*ip - CEE_LDIND_I1];
6638 ins->flags |= ins_flag;
6640 MONO_ADD_INS (bblock, ins);
6655 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
6656 ins->flags |= ins_flag;
6658 MONO_ADD_INS (bblock, ins);
6666 MONO_INST_NEW (cfg, ins, (*ip));
6668 ins->sreg1 = sp [0]->dreg;
6669 ins->sreg2 = sp [1]->dreg;
6670 type_from_op (ins, sp [0], sp [1]);
6672 ins->dreg = alloc_dreg ((cfg), (ins)->type);
6674 /* Use the immediate opcodes if possible */
6675 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
6676 int imm_opcode = mono_op_to_op_imm (ins->opcode);
6677 if (imm_opcode != -1) {
6678 ins->opcode = imm_opcode;
6679 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
6682 sp [1]->opcode = OP_NOP;
6686 MONO_ADD_INS ((cfg)->cbb, (ins));
6689 mono_decompose_opcode (cfg, ins);
6706 MONO_INST_NEW (cfg, ins, (*ip));
6708 ins->sreg1 = sp [0]->dreg;
6709 ins->sreg2 = sp [1]->dreg;
6710 type_from_op (ins, sp [0], sp [1]);
6712 ADD_WIDEN_OP (ins, sp [0], sp [1]);
6713 ins->dreg = alloc_dreg ((cfg), (ins)->type);
6715 /* FIXME: Pass opcode to is_inst_imm */
6717 /* Use the immediate opcodes if possible */
6718 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)) {
6721 imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
6722 if (imm_opcode != -1) {
6723 ins->opcode = imm_opcode;
6724 if (sp [1]->opcode == OP_I8CONST) {
6725 #if SIZEOF_VOID_P == 8
6726 ins->inst_imm = sp [1]->inst_l;
6728 ins->inst_ls_word = sp [1]->inst_ls_word;
6729 ins->inst_ms_word = sp [1]->inst_ms_word;
6733 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
6736 sp [1]->opcode = OP_NOP;
6739 MONO_ADD_INS ((cfg)->cbb, (ins));
6742 mono_decompose_opcode (cfg, ins);
6755 case CEE_CONV_OVF_I8:
6756 case CEE_CONV_OVF_U8:
6760 /* Special case this earlier so we have long constants in the IR */
6761 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
6762 int data = sp [-1]->inst_c0;
6763 sp [-1]->opcode = OP_I8CONST;
6764 sp [-1]->type = STACK_I8;
6765 #if SIZEOF_VOID_P == 8
6766 if ((*ip) == CEE_CONV_U8)
6767 sp [-1]->inst_c0 = (guint32)data;
6769 sp [-1]->inst_c0 = data;
6771 sp [-1]->inst_ls_word = data;
6772 if ((*ip) == CEE_CONV_U8)
6773 sp [-1]->inst_ms_word = 0;
6775 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
6777 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
6784 case CEE_CONV_OVF_I4:
6785 case CEE_CONV_OVF_I1:
6786 case CEE_CONV_OVF_I2:
6787 case CEE_CONV_OVF_I:
6788 case CEE_CONV_OVF_U:
6791 if (sp [-1]->type == STACK_R8) {
6792 ADD_UNOP (CEE_CONV_OVF_I8);
6799 case CEE_CONV_OVF_U1:
6800 case CEE_CONV_OVF_U2:
6801 case CEE_CONV_OVF_U4:
6804 if (sp [-1]->type == STACK_R8) {
6805 ADD_UNOP (CEE_CONV_OVF_U8);
6812 case CEE_CONV_OVF_I1_UN:
6813 case CEE_CONV_OVF_I2_UN:
6814 case CEE_CONV_OVF_I4_UN:
6815 case CEE_CONV_OVF_I8_UN:
6816 case CEE_CONV_OVF_U1_UN:
6817 case CEE_CONV_OVF_U2_UN:
6818 case CEE_CONV_OVF_U4_UN:
6819 case CEE_CONV_OVF_U8_UN:
6820 case CEE_CONV_OVF_I_UN:
6821 case CEE_CONV_OVF_U_UN:
6831 case CEE_ADD_OVF_UN:
6833 case CEE_MUL_OVF_UN:
6835 case CEE_SUB_OVF_UN:
6843 token = read32 (ip + 1);
6844 klass = mini_get_class (method, token, generic_context);
6845 CHECK_TYPELOAD (klass);
6847 if (generic_class_is_reference_type (cfg, klass)) {
6848 MonoInst *store, *load;
6849 int dreg = alloc_preg (cfg);
6851 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
6852 load->flags |= ins_flag;
6853 MONO_ADD_INS (cfg->cbb, load);
6855 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
6856 store->flags |= ins_flag;
6857 MONO_ADD_INS (cfg->cbb, store);
6859 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
6871 token = read32 (ip + 1);
6872 klass = mini_get_class (method, token, generic_context);
6873 CHECK_TYPELOAD (klass);
6875 /* Optimize the common ldobj+stloc combination */
6885 loc_index = ip [5] - CEE_STLOC_0;
6892 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
6893 CHECK_LOCAL (loc_index);
6895 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
6896 ins->dreg = cfg->locals [loc_index]->dreg;
6902 /* Optimize the ldobj+stobj combination */
6903 /* The reference case ends up being a load+store anyway */
6904 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, bblock, ip + 9) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass)) {
6909 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
6916 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
6925 CHECK_STACK_OVF (1);
6927 n = read32 (ip + 1);
6929 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
6930 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
6931 ins->type = STACK_OBJ;
6934 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
6935 MonoInst *iargs [1];
6937 EMIT_NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
6938 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
6940 if (cfg->opt & MONO_OPT_SHARED) {
6941 MonoInst *iargs [3];
6943 if (cfg->compile_aot) {
6944 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
6946 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
6947 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
6948 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
6949 *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
6950 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
6952 if (bblock->out_of_line) {
6953 MonoInst *iargs [2];
6955 if (cfg->method->klass->image == mono_defaults.corlib) {
6957 * Avoid relocations in AOT and save some space by using a
6958 * version of helper_ldstr specialized to mscorlib.
6960 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
6961 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
6963 /* Avoid creating the string object */
6964 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
6965 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
6966 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
6970 if (cfg->compile_aot) {
6971 NEW_LDSTRCONST (cfg, ins, image, n);
6973 MONO_ADD_INS (bblock, ins);
6976 NEW_PCONST (cfg, ins, NULL);
6977 ins->type = STACK_OBJ;
6978 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
6980 MONO_ADD_INS (bblock, ins);
6989 MonoInst *iargs [2];
6990 MonoMethodSignature *fsig;
6995 token = read32 (ip + 1);
6996 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
6999 fsig = mono_method_get_signature (cmethod, image, token);
7001 mono_save_token_info (cfg, image, token, cmethod);
7003 if (!mono_class_init (cmethod->klass))
7006 if (cfg->generic_sharing_context)
7007 context_used = mono_method_check_context_used (cmethod);
7009 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
7010 if (check_linkdemand (cfg, method, cmethod))
7012 CHECK_CFG_EXCEPTION;
7013 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
7014 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
7017 n = fsig->param_count;
7021 * Generate smaller code for the common newobj <exception> instruction in
7022 * argument checking code.
7024 if (bblock->out_of_line && cmethod->klass->image == mono_defaults.corlib && n <= 2 &&
7025 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
7026 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
7027 MonoInst *iargs [3];
7031 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
7034 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
7038 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
7043 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
7046 g_assert_not_reached ();
7054 /* move the args to allow room for 'this' in the first position */
7060 /* check_call_signature () requires sp[0] to be set */
7061 this_ins.type = STACK_OBJ;
7063 if (check_call_signature (cfg, fsig, sp))
7068 if (mini_class_is_system_array (cmethod->klass)) {
7069 g_assert (!context_used);
7070 EMIT_NEW_METHODCONST (cfg, *sp, cmethod);
7072 /* Avoid varargs in the common case */
7073 if (fsig->param_count == 1)
7074 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
7075 else if (fsig->param_count == 2)
7076 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
7078 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
7079 } else if (cmethod->string_ctor) {
7080 g_assert (!context_used);
7081 /* we simply pass a null pointer */
7082 EMIT_NEW_PCONST (cfg, *sp, NULL);
7083 /* now call the string ctor */
7084 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, sp, NULL, NULL);
7086 MonoInst* callvirt_this_arg = NULL;
7088 if (cmethod->klass->valuetype) {
7089 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
7090 MONO_EMIT_NEW_VZERO (cfg, iargs [0]->dreg, cmethod->klass);
7091 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
7096 * The code generated by mini_emit_virtual_call () expects
7097 * iargs [0] to be a boxed instance, but luckily the vcall
7098 * will be transformed into a normal call there.
7100 } else if (context_used) {
7101 MonoInst *rgctx, *data;
7104 EMIT_GET_RGCTX (rgctx, context_used);
7105 if (cfg->opt & MONO_OPT_SHARED)
7106 rgctx_info = MONO_RGCTX_INFO_KLASS;
7108 rgctx_info = MONO_RGCTX_INFO_VTABLE;
7109 data = emit_get_rgctx_klass (cfg, context_used, rgctx, cmethod->klass, rgctx_info);
7111 alloc = handle_alloc_from_inst (cfg, cmethod->klass, data, FALSE);
7114 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7116 CHECK_TYPELOAD (cmethod->klass);
7119 * TypeInitializationExceptions thrown from the mono_runtime_class_init
7120 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
7121 * As a workaround, we call class cctors before allocating objects.
7123 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
7124 mono_emit_abs_call (cfg, MONO_PATCH_INFO_CLASS_INIT, vtable->klass, helper_sig_class_init_trampoline, NULL);
7125 if (cfg->verbose_level > 2)
7126 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
7127 class_inits = g_slist_prepend (class_inits, vtable);
7130 alloc = handle_alloc (cfg, cmethod->klass, FALSE);
7135 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
7137 /* Now call the actual ctor */
7138 /* Avoid virtual calls to ctors if possible */
7139 if (cmethod->klass->marshalbyref)
7140 callvirt_this_arg = sp [0];
7142 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used &&
7143 mono_method_check_inlining (cfg, cmethod) &&
7144 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
7145 !g_list_find (dont_inline, cmethod)) {
7148 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, dont_inline, FALSE))) {
7149 cfg->real_offset += 5;
7152 inline_costs += costs - 5;
7155 mono_emit_method_call_full (cfg, cmethod, fsig, sp, callvirt_this_arg, NULL);
7157 } else if (context_used &&
7158 (cmethod->klass->valuetype ||
7159 !mono_method_is_generic_sharable_impl (cmethod, TRUE))) {
7160 MonoInst *rgctx, *cmethod_addr;
7162 g_assert (!callvirt_this_arg);
7164 EMIT_GET_RGCTX (rgctx, context_used);
7165 cmethod_addr = emit_get_rgctx_method (cfg, context_used, rgctx,
7166 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7168 mono_emit_calli (cfg, fsig, sp, cmethod_addr);
7171 mono_emit_method_call_full (cfg, cmethod, fsig, sp, callvirt_this_arg, NULL);
7175 if (alloc == NULL) {
7177 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
7178 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
7192 token = read32 (ip + 1);
7193 klass = mini_get_class (method, token, generic_context);
7194 CHECK_TYPELOAD (klass);
7195 if (sp [0]->type != STACK_OBJ)
7198 if (cfg->generic_sharing_context)
7199 context_used = mono_class_check_context_used (klass);
7202 MonoInst *rgctx, *args [2];
7204 g_assert (!method->klass->valuetype);
7210 EMIT_GET_RGCTX (rgctx, context_used);
7211 args [1] = emit_get_rgctx_klass (cfg, context_used, rgctx, klass,
7212 MONO_RGCTX_INFO_KLASS);
7214 ins = mono_emit_jit_icall (cfg, mono_object_castclass, args);
7218 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7219 MonoMethod *mono_castclass;
7220 MonoInst *iargs [1];
7223 mono_castclass = mono_marshal_get_castclass (klass);
7226 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
7227 iargs, ip, cfg->real_offset, dont_inline, TRUE);
7228 g_assert (costs > 0);
7231 cfg->real_offset += 5;
7236 inline_costs += costs;
7239 ins = handle_castclass (cfg, klass, *sp);
7249 token = read32 (ip + 1);
7250 klass = mini_get_class (method, token, generic_context);
7251 CHECK_TYPELOAD (klass);
7252 if (sp [0]->type != STACK_OBJ)
7255 if (cfg->generic_sharing_context)
7256 context_used = mono_class_check_context_used (klass);
7259 MonoInst *rgctx, *args [2];
7265 EMIT_GET_RGCTX (rgctx, context_used);
7266 args [1] = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_KLASS);
7268 *sp = mono_emit_jit_icall (cfg, mono_object_isinst, args);
7272 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7273 MonoMethod *mono_isinst;
7274 MonoInst *iargs [1];
7277 mono_isinst = mono_marshal_get_isinst (klass);
7280 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst),
7281 iargs, ip, cfg->real_offset, dont_inline, TRUE);
7282 g_assert (costs > 0);
7285 cfg->real_offset += 5;
7290 inline_costs += costs;
7293 ins = handle_isinst (cfg, klass, *sp);
7300 case CEE_UNBOX_ANY: {
7301 MonoInst *rgctx = NULL;
7306 token = read32 (ip + 1);
7307 klass = mini_get_class (method, token, generic_context);
7308 CHECK_TYPELOAD (klass);
7310 mono_save_token_info (cfg, image, token, klass);
7312 if (cfg->generic_sharing_context)
7313 context_used = mono_class_check_context_used (klass);
7315 if (generic_class_is_reference_type (cfg, klass)) {
7318 MonoInst *iargs [2];
7320 g_assert (!method->klass->valuetype);
7325 EMIT_GET_RGCTX (rgctx, context_used);
7326 iargs [1] = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_KLASS);
7327 ins = mono_emit_jit_icall (cfg, mono_object_castclass, iargs);
7331 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7332 MonoMethod *mono_castclass;
7333 MonoInst *iargs [1];
7336 mono_castclass = mono_marshal_get_castclass (klass);
7339 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
7340 iargs, ip, cfg->real_offset, dont_inline, TRUE);
7342 g_assert (costs > 0);
7345 cfg->real_offset += 5;
7349 inline_costs += costs;
7351 ins = handle_castclass (cfg, klass, *sp);
7360 EMIT_GET_RGCTX (rgctx, context_used);
7362 if (mono_class_is_nullable (klass)) {
7363 ins = handle_unbox_nullable (cfg, *sp, klass, context_used, rgctx);
7370 ins = handle_unbox (cfg, klass, sp, context_used, rgctx);
7376 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
7389 token = read32 (ip + 1);
7390 klass = mini_get_class (method, token, generic_context);
7391 CHECK_TYPELOAD (klass);
7393 mono_save_token_info (cfg, image, token, klass);
7395 if (cfg->generic_sharing_context)
7396 context_used = mono_class_check_context_used (klass);
7398 if (generic_class_is_reference_type (cfg, klass)) {
7404 if (klass == mono_defaults.void_class)
7406 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
7408 /* frequent check in generic code: box (struct), brtrue */
7409 if (!mono_class_is_nullable (klass) &&
7410 ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
7411 /*printf ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
7413 MONO_INST_NEW (cfg, ins, OP_BR);
7414 if (*ip == CEE_BRTRUE_S) {
7417 target = ip + 1 + (signed char)(*ip);
7422 target = ip + 4 + (gint)(read32 (ip));
7425 GET_BBLOCK (cfg, tblock, target);
7426 link_bblock (cfg, bblock, tblock);
7427 CHECK_BBLOCK (target, ip, tblock);
7428 ins->inst_target_bb = tblock;
7429 GET_BBLOCK (cfg, tblock, ip);
7431 * This leads to some inconsistency, since the two bblocks are not
7432 * really connected, but it is needed for handling stack arguments
7433 * correct (See test_0_box_brtrue_opt_regress_81102).
7435 link_bblock (cfg, bblock, tblock);
7436 if (sp != stack_start) {
7437 handle_stack_args (cfg, stack_start, sp - stack_start);
7439 CHECK_UNVERIFIABLE (cfg);
7441 MONO_ADD_INS (bblock, ins);
7442 start_new_bblock = 1;
7451 EMIT_GET_RGCTX (rgctx, context_used);
7452 if (cfg->opt & MONO_OPT_SHARED)
7453 rgctx_info = MONO_RGCTX_INFO_KLASS;
7455 rgctx_info = MONO_RGCTX_INFO_VTABLE;
7456 data = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, rgctx_info);
7457 *sp++ = handle_box_from_inst (cfg, val, klass, context_used, rgctx, data);
7459 *sp++ = handle_box (cfg, val, klass);
7467 MonoInst *rgctx = NULL;
7472 token = read32 (ip + 1);
7473 klass = mini_get_class (method, token, generic_context);
7474 CHECK_TYPELOAD (klass);
7476 mono_save_token_info (cfg, image, token, klass);
7478 if (cfg->generic_sharing_context)
7479 context_used = mono_class_check_context_used (klass);
7482 EMIT_GET_RGCTX (rgctx, context_used);
7484 if (mono_class_is_nullable (klass)) {
7487 val = handle_unbox_nullable (cfg, *sp, klass, context_used, rgctx);
7488 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
7492 ins = handle_unbox (cfg, klass, sp, context_used, rgctx);
7502 MonoClassField *field;
7506 if (*ip == CEE_STFLD) {
7513 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
7515 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
7518 token = read32 (ip + 1);
7519 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7520 field = mono_method_get_wrapper_data (method, token);
7521 klass = field->parent;
7524 field = mono_field_from_token (image, token, &klass, generic_context);
7528 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7529 FIELD_ACCESS_FAILURE;
7530 mono_class_init (klass);
7532 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
7533 if (*ip == CEE_STFLD) {
7534 if (target_type_is_incompatible (cfg, field->type, sp [1]))
7536 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7537 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
7538 MonoInst *iargs [5];
7541 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
7542 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
7543 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
7547 if (cfg->opt & MONO_OPT_INLINE) {
7548 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
7549 iargs, ip, cfg->real_offset, dont_inline, TRUE);
7550 g_assert (costs > 0);
7553 cfg->real_offset += 5;
7556 inline_costs += costs;
7559 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
7564 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
7566 store->flags |= ins_flag;
7573 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7574 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
7575 MonoInst *iargs [4];
7578 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
7579 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
7580 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
7581 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
7582 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
7583 iargs, ip, cfg->real_offset, dont_inline, TRUE);
7585 g_assert (costs > 0);
7588 cfg->real_offset += 5;
7592 inline_costs += costs;
7595 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
7599 if (sp [0]->type == STACK_VTYPE) {
7602 /* Have to compute the address of the variable */
7604 var = get_vreg_to_inst (cfg, sp [0]->dreg);
7606 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
7608 g_assert (var->klass == klass);
7610 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
7614 if (*ip == CEE_LDFLDA) {
7615 dreg = alloc_preg (cfg);
7617 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
7618 ins->klass = mono_class_from_mono_type (field->type);
7619 ins->type = STACK_MP;
7624 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
7625 load->flags |= ins_flag;
7636 MonoClassField *field;
7637 gpointer addr = NULL;
7638 gboolean is_special_static;
7641 token = read32 (ip + 1);
7643 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7644 field = mono_method_get_wrapper_data (method, token);
7645 klass = field->parent;
7648 field = mono_field_from_token (image, token, &klass, generic_context);
7651 mono_class_init (klass);
7652 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7653 FIELD_ACCESS_FAILURE;
7656 * We can only support shared generic static
7657 * field access on architectures where the
7658 * trampoline code has been extended to handle
7659 * the generic class init.
7661 #ifndef MONO_ARCH_VTABLE_REG
7662 GENERIC_SHARING_FAILURE (*ip);
7665 if (cfg->generic_sharing_context)
7666 context_used = mono_class_check_context_used (klass);
7668 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
7670 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
7671 * to be called here.
7673 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
7674 mono_class_vtable (cfg->domain, klass);
7675 CHECK_TYPELOAD (klass);
7677 mono_domain_lock (cfg->domain);
7678 if (cfg->domain->special_static_fields)
7679 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
7680 mono_domain_unlock (cfg->domain);
7682 is_special_static = mono_class_field_is_special_static (field);
7684 /* Generate IR to compute the field address */
7686 if ((cfg->opt & MONO_OPT_SHARED) ||
7687 (cfg->compile_aot && is_special_static) ||
7688 (context_used && is_special_static)) {
7689 MonoInst *iargs [2];
7691 g_assert (field->parent);
7692 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
7696 EMIT_GET_RGCTX (rgctx, context_used);
7697 iargs [1] = emit_get_rgctx_field (cfg, context_used, rgctx, field, MONO_RGCTX_INFO_CLASS_FIELD);
7699 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
7701 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
7702 } else if (context_used) {
7703 MonoInst *rgctx, *static_data;
7706 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
7707 method->klass->name_space, method->klass->name, method->name,
7708 depth, field->offset);
7711 if (mono_class_needs_cctor_run (klass, method)) {
7713 MonoInst *vtable, *rgctx;
7715 EMIT_GET_RGCTX (rgctx, context_used);
7716 vtable = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_VTABLE);
7718 // FIXME: This doesn't work since it tries to pass the argument
7719 // in the normal way, instead of using MONO_ARCH_VTABLE_REG
7721 * The vtable pointer is always passed in a register regardless of
7722 * the calling convention, so assign it manually, and make a call
7723 * using a signature without parameters.
7725 call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline, &vtable);
7726 #ifdef MONO_ARCH_VTABLE_REG
7727 mono_call_inst_add_outarg_reg (cfg, call, vtable->dreg, MONO_ARCH_VTABLE_REG, FALSE);
7728 cfg->uses_vtable_reg = TRUE;
7735 * The pointer we're computing here is
7737 * super_info.static_data + field->offset
7739 EMIT_GET_RGCTX (rgctx, context_used);
7740 static_data = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_STATIC_DATA);
7742 if (field->offset == 0) {
7745 int addr_reg = mono_alloc_preg (cfg);
7746 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
7748 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
7749 MonoInst *iargs [2];
7751 g_assert (field->parent);
7752 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
7753 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
7754 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
7756 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
7758 CHECK_TYPELOAD (klass);
7760 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
7761 mono_emit_abs_call (cfg, MONO_PATCH_INFO_CLASS_INIT, vtable->klass, helper_sig_class_init_trampoline, NULL);
7762 if (cfg->verbose_level > 2)
7763 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
7764 class_inits = g_slist_prepend (class_inits, vtable);
7766 if (cfg->run_cctors) {
7768 /* This makes so that inline cannot trigger */
7769 /* .cctors: too many apps depend on them */
7770 /* running with a specific order... */
7771 if (! vtable->initialized)
7773 ex = mono_runtime_class_init_full (vtable, FALSE);
7775 set_exception_object (cfg, ex);
7776 goto exception_exit;
7780 addr = (char*)vtable->data + field->offset;
7782 if (cfg->compile_aot)
7783 EMIT_NEW_SFLDACONST (cfg, ins, field);
7785 EMIT_NEW_PCONST (cfg, ins, addr);
7788 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
7789 * This could be later optimized to do just a couple of
7790 * memory dereferences with constant offsets.
7792 MonoInst *iargs [1];
7793 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
7794 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
7798 /* Generate IR to do the actual load/store operation */
7800 if (*ip == CEE_LDSFLDA) {
7801 ins->klass = mono_class_from_mono_type (field->type);
7803 } else if (*ip == CEE_STSFLD) {
7808 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, ins->dreg, 0, sp [0]->dreg);
7809 store->flags |= ins_flag;
7812 gboolean is_const = FALSE;
7813 MonoVTable *vtable = NULL;
7815 if (!context_used) {
7816 vtable = mono_class_vtable (cfg->domain, klass);
7817 CHECK_TYPELOAD (klass);
7819 if (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
7820 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
7821 gpointer addr = (char*)vtable->data + field->offset;
7822 int ro_type = field->type->type;
7823 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
7824 ro_type = field->type->data.klass->enum_basetype->type;
7826 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
7829 case MONO_TYPE_BOOLEAN:
7831 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
7835 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
7838 case MONO_TYPE_CHAR:
7840 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
7844 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
7849 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
7853 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
7858 case MONO_TYPE_STRING:
7859 case MONO_TYPE_OBJECT:
7860 case MONO_TYPE_CLASS:
7861 case MONO_TYPE_SZARRAY:
7863 case MONO_TYPE_FNPTR:
7864 case MONO_TYPE_ARRAY:
7865 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
7866 type_to_eval_stack_type ((cfg), field->type, *sp);
7871 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
7876 case MONO_TYPE_VALUETYPE:
7886 CHECK_STACK_OVF (1);
7888 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
7889 load->flags |= ins_flag;
7901 token = read32 (ip + 1);
7902 klass = mini_get_class (method, token, generic_context);
7903 CHECK_TYPELOAD (klass);
7904 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
7905 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
7916 const char *data_ptr;
7923 token = read32 (ip + 1);
7925 klass = mini_get_class (method, token, generic_context);
7926 CHECK_TYPELOAD (klass);
7928 if (cfg->generic_sharing_context)
7929 context_used = mono_class_check_context_used (klass);
7935 /* FIXME: Decompose later to help abcrem */
7938 EMIT_GET_RGCTX (rgctx, context_used);
7939 args [0] = emit_get_rgctx_klass (cfg, context_used, rgctx, mono_array_class_get (klass, 1), MONO_RGCTX_INFO_VTABLE);
7944 ins = mono_emit_jit_icall (cfg, mono_array_new_specific, args);
7946 if (cfg->opt & MONO_OPT_SHARED) {
7947 /* Decompose now to avoid problems with references to the domainvar */
7948 MonoInst *iargs [3];
7950 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
7951 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
7954 ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
7956 /* Decompose later since it is needed by abcrem */
7957 MONO_INST_NEW (cfg, ins, OP_NEWARR);
7958 ins->dreg = alloc_preg (cfg);
7959 ins->sreg1 = sp [0]->dreg;
7960 ins->inst_newa_class = klass;
7961 ins->type = STACK_OBJ;
7963 MONO_ADD_INS (cfg->cbb, ins);
7964 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
7965 cfg->cbb->has_array_access = TRUE;
7967 /* Needed so mono_emit_load_get_addr () gets called */
7968 mono_get_got_var (cfg);
7978 * we inline/optimize the initialization sequence if possible.
7979 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
7980 * for small sizes open code the memcpy
7981 * ensure the rva field is big enough
7983 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))) {
7984 MonoMethod *memcpy_method = get_memcpy_method ();
7985 MonoInst *iargs [3];
7986 int add_reg = alloc_preg (cfg);
7988 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, G_STRUCT_OFFSET (MonoArray, vector));
7989 if (cfg->compile_aot) {
7990 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(data_ptr), STACK_PTR, NULL);
7992 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
7994 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
7995 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
8004 if (sp [0]->type != STACK_OBJ)
8007 dreg = alloc_preg (cfg);
8008 MONO_INST_NEW (cfg, ins, OP_LDLEN);
8009 ins->dreg = alloc_preg (cfg);
8010 ins->sreg1 = sp [0]->dreg;
8011 ins->type = STACK_I4;
8012 MONO_ADD_INS (cfg->cbb, ins);
8013 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
8014 cfg->cbb->has_array_access = TRUE;
8022 if (sp [0]->type != STACK_OBJ)
8025 cfg->flags |= MONO_CFG_HAS_LDELEMA;
8027 klass = mini_get_class (method, read32 (ip + 1), generic_context);
8028 CHECK_TYPELOAD (klass);
8029 /* we need to make sure that this array is exactly the type it needs
8030 * to be for correctness. the wrappers are lax with their usage
8031 * so we need to ignore them here
8033 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly)
8034 mini_emit_check_array_type (cfg, sp [0], mono_array_class_get (klass, 1));
8037 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1]);
8041 case CEE_LDELEM_ANY:
8052 case CEE_LDELEM_REF: {
8058 if (*ip == CEE_LDELEM_ANY) {
8060 token = read32 (ip + 1);
8061 klass = mini_get_class (method, token, generic_context);
8062 CHECK_TYPELOAD (klass);
8063 mono_class_init (klass);
8066 klass = array_access_to_klass (*ip);
8068 if (sp [0]->type != STACK_OBJ)
8071 cfg->flags |= MONO_CFG_HAS_LDELEMA;
8073 if (sp [1]->opcode == OP_ICONST) {
8074 int array_reg = sp [0]->dreg;
8075 int index_reg = sp [1]->dreg;
8076 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + G_STRUCT_OFFSET (MonoArray, vector);
8078 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
8079 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
8081 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1]);
8082 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
8085 if (*ip == CEE_LDELEM_ANY)
8098 case CEE_STELEM_REF:
8099 case CEE_STELEM_ANY: {
8105 cfg->flags |= MONO_CFG_HAS_LDELEMA;
8107 if (*ip == CEE_STELEM_ANY) {
8109 token = read32 (ip + 1);
8110 klass = mini_get_class (method, token, generic_context);
8111 CHECK_TYPELOAD (klass);
8112 mono_class_init (klass);
8115 klass = array_access_to_klass (*ip);
8117 if (sp [0]->type != STACK_OBJ)
8120 /* storing a NULL doesn't need any of the complex checks in stelemref */
8121 if (generic_class_is_reference_type (cfg, klass) &&
8122 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
8123 MonoMethod* helper = mono_marshal_get_stelemref ();
8124 MonoInst *iargs [3];
8126 if (sp [0]->type != STACK_OBJ)
8128 if (sp [2]->type != STACK_OBJ)
8135 mono_emit_method_call (cfg, helper, iargs, NULL);
8137 if (sp [1]->opcode == OP_ICONST) {
8138 int array_reg = sp [0]->dreg;
8139 int index_reg = sp [1]->dreg;
8140 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + G_STRUCT_OFFSET (MonoArray, vector);
8142 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
8143 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
8145 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1]);
8146 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
8150 if (*ip == CEE_STELEM_ANY)
8157 case CEE_CKFINITE: {
8161 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
8162 ins->sreg1 = sp [0]->dreg;
8163 ins->dreg = alloc_freg (cfg);
8164 ins->type = STACK_R8;
8165 MONO_ADD_INS (bblock, ins);
8168 mono_decompose_opcode (cfg, ins);
8173 case CEE_REFANYVAL: {
8174 MonoInst *src_var, *src;
8176 int klass_reg = alloc_preg (cfg);
8177 int dreg = alloc_preg (cfg);
8180 MONO_INST_NEW (cfg, ins, *ip);
8183 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
8184 CHECK_TYPELOAD (klass);
8185 mono_class_init (klass);
8187 if (cfg->generic_sharing_context)
8188 context_used = mono_class_check_context_used (klass);
8191 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
8193 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
8194 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
8195 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, G_STRUCT_OFFSET (MonoTypedRef, klass));
8198 MonoInst *rgctx, *klass_ins;
8200 EMIT_GET_RGCTX (rgctx, context_used);
8201 klass_ins = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_KLASS);
8204 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
8205 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
8207 mini_emit_class_check (cfg, klass_reg, klass);
8209 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, G_STRUCT_OFFSET (MonoTypedRef, value));
8210 ins->type = STACK_MP;
8215 case CEE_MKREFANY: {
8216 MonoInst *loc, *addr;
8219 MONO_INST_NEW (cfg, ins, *ip);
8222 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
8223 CHECK_TYPELOAD (klass);
8224 mono_class_init (klass);
8226 if (cfg->generic_sharing_context)
8227 context_used = mono_class_check_context_used (klass);
8229 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
8230 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
8233 MonoInst *rgctx, *const_ins;
8234 int type_reg = alloc_preg (cfg);
8236 EMIT_GET_RGCTX (rgctx, context_used);
8237 const_ins = emit_get_rgctx_klass (cfg, context_used, rgctx, klass, MONO_RGCTX_INFO_KLASS);
8238 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
8239 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, G_STRUCT_OFFSET (MonoClass, byval_arg));
8240 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
8241 } else if (cfg->compile_aot) {
8242 int const_reg = alloc_preg (cfg);
8243 int type_reg = alloc_preg (cfg);
8245 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
8246 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
8247 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, G_STRUCT_OFFSET (MonoClass, byval_arg));
8248 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
8250 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
8251 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, klass), klass);
8253 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, G_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
8255 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
8256 ins->type = STACK_VTYPE;
8257 ins->klass = mono_defaults.typed_reference_class;
8264 MonoClass *handle_class;
8266 CHECK_STACK_OVF (1);
8269 n = read32 (ip + 1);
8271 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
8272 handle = mono_method_get_wrapper_data (method, n);
8273 handle_class = mono_method_get_wrapper_data (method, n + 1);
8274 if (handle_class == mono_defaults.typehandle_class)
8275 handle = &((MonoClass*)handle)->byval_arg;
8278 handle = mono_ldtoken (image, n, &handle_class, generic_context);
8282 mono_class_init (handle_class);
8283 if (cfg->generic_sharing_context) {
8284 if (handle_class == mono_defaults.typehandle_class) {
8285 /* If we get a MONO_TYPE_CLASS
8286 then we need to provide the
8288 instantiation of it. */
8289 if (mono_type_get_type (handle) == MONO_TYPE_CLASS)
8292 context_used = mono_class_check_context_used (mono_class_from_mono_type (handle));
8293 } else if (handle_class == mono_defaults.fieldhandle_class)
8294 context_used = mono_class_check_context_used (((MonoClassField*)handle)->parent);
8295 else if (handle_class == mono_defaults.methodhandle_class)
8296 context_used = mono_method_check_context_used (handle);
8298 g_assert_not_reached ();
8301 if (cfg->opt & MONO_OPT_SHARED) {
8302 MonoInst *addr, *vtvar, *iargs [3];
8303 int method_context_used;
8305 if (cfg->generic_sharing_context)
8306 method_context_used = mono_method_check_context_used (method);
8308 method_context_used = 0;
8310 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8312 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
8313 EMIT_NEW_ICONST (cfg, iargs [1], n);
8314 if (method_context_used) {
8317 EMIT_GET_RGCTX (rgctx, method_context_used);
8318 iargs [2] = emit_get_rgctx_method (cfg, method_context_used, rgctx, method, MONO_RGCTX_INFO_METHOD);
8319 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
8321 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
8322 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
8324 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8326 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
8328 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8330 if ((ip + 5 < end) && ip_in_bb (cfg, bblock, ip + 5) &&
8331 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
8332 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
8333 (cmethod->klass == mono_defaults.monotype_class->parent) &&
8334 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
8335 MonoClass *tclass = mono_class_from_mono_type (handle);
8337 mono_class_init (tclass);
8341 EMIT_GET_RGCTX (rgctx, context_used);
8342 ins = emit_get_rgctx_klass (cfg, context_used, rgctx, tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
8343 } else if (cfg->compile_aot) {
8344 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
8346 EMIT_NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
8348 ins->type = STACK_OBJ;
8349 ins->klass = cmethod->klass;
8352 MonoInst *addr, *vtvar;
8354 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8359 EMIT_GET_RGCTX (rgctx, context_used);
8360 if (handle_class == mono_defaults.typehandle_class) {
8361 ins = emit_get_rgctx_klass (cfg, context_used, rgctx,
8362 mono_class_from_mono_type (handle),
8363 MONO_RGCTX_INFO_TYPE);
8364 } else if (handle_class == mono_defaults.methodhandle_class) {
8365 ins = emit_get_rgctx_method (cfg, context_used, rgctx,
8366 handle, MONO_RGCTX_INFO_METHOD);
8367 } else if (handle_class == mono_defaults.fieldhandle_class) {
8368 ins = emit_get_rgctx_field (cfg, context_used, rgctx,
8369 handle, MONO_RGCTX_INFO_CLASS_FIELD);
8371 g_assert_not_reached ();
8373 } else if (cfg->compile_aot) {
8374 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n);
8376 EMIT_NEW_PCONST (cfg, ins, handle);
8378 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8379 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
8380 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8390 MONO_INST_NEW (cfg, ins, OP_THROW);
8392 ins->sreg1 = sp [0]->dreg;
8394 bblock->out_of_line = TRUE;
8395 MONO_ADD_INS (bblock, ins);
8396 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
8397 MONO_ADD_INS (bblock, ins);
8400 link_bblock (cfg, bblock, end_bblock);
8401 start_new_bblock = 1;
8403 case CEE_ENDFINALLY:
8404 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
8405 MONO_ADD_INS (bblock, ins);
8407 start_new_bblock = 1;
8410 * Control will leave the method so empty the stack, otherwise
8411 * the next basic block will start with a nonempty stack.
8413 while (sp != stack_start) {
8421 if (*ip == CEE_LEAVE) {
8423 target = ip + 5 + (gint32)read32(ip + 1);
8426 target = ip + 2 + (signed char)(ip [1]);
8429 /* empty the stack */
8430 while (sp != stack_start) {
8435 * If this leave statement is in a catch block, check for a
8436 * pending exception, and rethrow it if necessary.
8438 for (i = 0; i < header->num_clauses; ++i) {
8439 MonoExceptionClause *clause = &header->clauses [i];
8442 * Use <= in the final comparison to handle clauses with multiple
8443 * leave statements, like in bug #78024.
8444 * The ordering of the exception clauses guarantees that we find the
8447 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)) {
8449 MonoBasicBlock *dont_throw;
8454 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
8457 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
8459 NEW_BBLOCK (cfg, dont_throw);
8462 * Currently, we allways rethrow the abort exception, despite the
8463 * fact that this is not correct. See thread6.cs for an example.
8464 * But propagating the abort exception is more important than
8465 * getting the sematics right.
8467 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
8468 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
8469 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
8471 MONO_START_BB (cfg, dont_throw);
8476 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
8478 for (tmp = handlers; tmp; tmp = tmp->next) {
8480 link_bblock (cfg, bblock, tblock);
8481 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
8482 ins->inst_target_bb = tblock;
8483 MONO_ADD_INS (bblock, ins);
8485 g_list_free (handlers);
8488 MONO_INST_NEW (cfg, ins, OP_BR);
8489 MONO_ADD_INS (bblock, ins);
8490 GET_BBLOCK (cfg, tblock, target);
8491 link_bblock (cfg, bblock, tblock);
8492 CHECK_BBLOCK (target, ip, tblock);
8493 ins->inst_target_bb = tblock;
8494 start_new_bblock = 1;
8496 if (*ip == CEE_LEAVE)
8505 * Mono specific opcodes
8507 case MONO_CUSTOM_PREFIX: {
8509 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
8513 case CEE_MONO_ICALL: {
8515 MonoJitICallInfo *info;
8517 token = read32 (ip + 2);
8518 func = mono_method_get_wrapper_data (method, token);
8519 info = mono_find_jit_icall_by_addr (func);
8522 CHECK_STACK (info->sig->param_count);
8523 sp -= info->sig->param_count;
8525 ins = mono_emit_jit_icall (cfg, info->func, sp);
8526 if (!MONO_TYPE_IS_VOID (info->sig->ret))
8530 inline_costs += 10 * num_calls++;
8534 case CEE_MONO_LDPTR: {
8537 CHECK_STACK_OVF (1);
8539 token = read32 (ip + 2);
8541 ptr = mono_method_get_wrapper_data (method, token);
8542 if (cfg->compile_aot && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
8543 MonoJitICallInfo *callinfo;
8544 const char *icall_name;
8546 icall_name = method->name + strlen ("__icall_wrapper_");
8547 g_assert (icall_name);
8548 callinfo = mono_find_jit_icall_by_name (icall_name);
8549 g_assert (callinfo);
8551 if (ptr == callinfo->func) {
8552 /* Will be transformed into an AOTCONST later */
8553 EMIT_NEW_PCONST (cfg, ins, ptr);
8559 /* FIXME: Generalize this */
8560 if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
8561 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
8566 EMIT_NEW_PCONST (cfg, ins, ptr);
8569 inline_costs += 10 * num_calls++;
8570 /* Can't embed random pointers into AOT code */
8571 cfg->disable_aot = 1;
8574 case CEE_MONO_ICALL_ADDR: {
8575 MonoMethod *cmethod;
8578 CHECK_STACK_OVF (1);
8580 token = read32 (ip + 2);
8582 cmethod = mono_method_get_wrapper_data (method, token);
8584 if (cfg->compile_aot) {
8585 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
8587 ptr = mono_lookup_internal_call (cmethod);
8589 EMIT_NEW_PCONST (cfg, ins, ptr);
8595 case CEE_MONO_VTADDR: {
8596 MonoInst *src_var, *src;
8602 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
8603 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
8608 case CEE_MONO_NEWOBJ: {
8609 MonoInst *iargs [2];
8611 CHECK_STACK_OVF (1);
8613 token = read32 (ip + 2);
8614 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
8615 mono_class_init (klass);
8616 NEW_DOMAINCONST (cfg, iargs [0]);
8617 MONO_ADD_INS (cfg->cbb, iargs [0]);
8618 NEW_CLASSCONST (cfg, iargs [1], klass);
8619 MONO_ADD_INS (cfg->cbb, iargs [1]);
8620 *sp++ = mono_emit_jit_icall (cfg, mono_object_new, iargs);
8622 inline_costs += 10 * num_calls++;
8625 case CEE_MONO_OBJADDR:
8628 MONO_INST_NEW (cfg, ins, OP_MOVE);
8629 ins->dreg = alloc_preg (cfg);
8630 ins->sreg1 = sp [0]->dreg;
8631 ins->type = STACK_MP;
8632 MONO_ADD_INS (cfg->cbb, ins);
8636 case CEE_MONO_LDNATIVEOBJ:
8638 * Similar to LDOBJ, but instead load the unmanaged
8639 * representation of the vtype to the stack.
8644 token = read32 (ip + 2);
8645 klass = mono_method_get_wrapper_data (method, token);
8646 g_assert (klass->valuetype);
8647 mono_class_init (klass);
8650 MonoInst *src, *dest, *temp;
8653 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
8654 temp->backend.is_pinvoke = 1;
8655 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
8656 mini_emit_stobj (cfg, dest, src, klass, TRUE);
8658 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
8659 dest->type = STACK_VTYPE;
8660 dest->klass = klass;
8666 case CEE_MONO_RETOBJ: {
8668 * Same as RET, but return the native representation of a vtype
8671 g_assert (cfg->ret);
8672 g_assert (mono_method_signature (method)->pinvoke);
8677 token = read32 (ip + 2);
8678 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
8680 if (!cfg->vret_addr) {
8681 g_assert (cfg->ret_var_is_local);
8683 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
8685 EMIT_NEW_RETLOADA (cfg, ins);
8687 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
8689 if (sp != stack_start)
8692 MONO_INST_NEW (cfg, ins, OP_BR);
8693 ins->inst_target_bb = end_bblock;
8694 MONO_ADD_INS (bblock, ins);
8695 link_bblock (cfg, bblock, end_bblock);
8696 start_new_bblock = 1;
8700 case CEE_MONO_CISINST:
8701 case CEE_MONO_CCASTCLASS: {
8706 token = read32 (ip + 2);
8707 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
8708 if (ip [1] == CEE_MONO_CISINST)
8709 ins = handle_cisinst (cfg, klass, sp [0]);
8711 ins = handle_ccastclass (cfg, klass, sp [0]);
8717 case CEE_MONO_SAVE_LMF:
8718 case CEE_MONO_RESTORE_LMF:
8719 #ifdef MONO_ARCH_HAVE_LMF_OPS
8720 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
8721 MONO_ADD_INS (bblock, ins);
8722 cfg->need_lmf_area = TRUE;
8726 case CEE_MONO_CLASSCONST:
8727 CHECK_STACK_OVF (1);
8729 token = read32 (ip + 2);
8730 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
8733 inline_costs += 10 * num_calls++;
8735 case CEE_MONO_NOT_TAKEN:
8736 bblock->out_of_line = TRUE;
8740 CHECK_STACK_OVF (1);
8742 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
8743 ins->dreg = alloc_preg (cfg);
8744 ins->inst_offset = (gint32)read32 (ip + 2);
8745 ins->type = STACK_PTR;
8746 MONO_ADD_INS (bblock, ins);
8751 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
8761 /* somewhat similar to LDTOKEN */
8762 MonoInst *addr, *vtvar;
8763 CHECK_STACK_OVF (1);
8764 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
8766 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8767 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
8769 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8770 ins->type = STACK_VTYPE;
8771 ins->klass = mono_defaults.argumenthandle_class;
8784 * The following transforms:
8785 * CEE_CEQ into OP_CEQ
8786 * CEE_CGT into OP_CGT
8787 * CEE_CGT_UN into OP_CGT_UN
8788 * CEE_CLT into OP_CLT
8789 * CEE_CLT_UN into OP_CLT_UN
8791 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
8793 MONO_INST_NEW (cfg, ins, cmp->opcode);
8795 cmp->sreg1 = sp [0]->dreg;
8796 cmp->sreg2 = sp [1]->dreg;
8797 type_from_op (cmp, sp [0], sp [1]);
8799 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
8800 cmp->opcode = OP_LCOMPARE;
8801 else if (sp [0]->type == STACK_R8)
8802 cmp->opcode = OP_FCOMPARE;
8804 cmp->opcode = OP_ICOMPARE;
8805 MONO_ADD_INS (bblock, cmp);
8806 ins->type = STACK_I4;
8807 ins->dreg = alloc_dreg (cfg, ins->type);
8808 type_from_op (ins, sp [0], sp [1]);
8810 if (cmp->opcode == OP_FCOMPARE) {
8812 * The backends expect the fceq opcodes to do the
8815 cmp->opcode = OP_NOP;
8816 ins->sreg1 = cmp->sreg1;
8817 ins->sreg2 = cmp->sreg2;
8819 MONO_ADD_INS (bblock, ins);
8826 MonoMethod *cil_method, *ctor_method;
8827 gboolean is_shared = FALSE;
8829 CHECK_STACK_OVF (1);
8831 n = read32 (ip + 2);
8832 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
8835 mono_class_init (cmethod->klass);
8837 mono_save_token_info (cfg, image, n, cmethod);
8839 if (cfg->generic_sharing_context)
8840 context_used = mono_method_check_context_used (cmethod);
8842 if (mono_class_generic_sharing_enabled (cmethod->klass)) {
8843 if ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
8844 (cmethod->klass->generic_class ||
8845 cmethod->klass->generic_container)) {
8848 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst)
8852 cil_method = cmethod;
8853 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
8854 METHOD_ACCESS_FAILURE;
8856 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
8857 if (check_linkdemand (cfg, method, cmethod))
8859 CHECK_CFG_EXCEPTION;
8860 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
8861 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
8865 * Optimize the common case of ldftn+delegate creation
8867 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE) && !defined(HAVE_WRITE_BARRIERS)
8868 /* FIXME: SGEN support */
8869 /* FIXME: handle shared static generic methods */
8870 /* FIXME: handle this in shared code */
8871 if (!is_shared && !context_used && (sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, bblock, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context)) && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
8872 MonoInst *target_ins;
8875 if (cfg->verbose_level > 3)
8876 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8877 target_ins = sp [-1];
8879 *sp = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod);
8890 cmethod = mono_marshal_get_static_rgctx_invoke (cmethod);
8892 EMIT_GET_RGCTX (rgctx, context_used);
8893 argconst = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod, MONO_RGCTX_INFO_METHOD);
8894 } else if (is_shared) {
8895 EMIT_NEW_METHODCONST (cfg, argconst, mono_marshal_get_static_rgctx_invoke (cmethod));
8897 EMIT_NEW_METHODCONST (cfg, argconst, cmethod);
8899 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
8903 inline_costs += 10 * num_calls++;
8906 case CEE_LDVIRTFTN: {
8911 n = read32 (ip + 2);
8912 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
8915 mono_class_init (cmethod->klass);
8917 if (cfg->generic_sharing_context)
8918 context_used = mono_method_check_context_used (cmethod);
8920 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
8921 if (check_linkdemand (cfg, method, cmethod))
8923 CHECK_CFG_EXCEPTION;
8924 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
8925 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
8934 EMIT_GET_RGCTX (rgctx, context_used);
8935 args [1] = emit_get_rgctx_method (cfg, context_used, rgctx, cmethod, MONO_RGCTX_INFO_METHOD);
8936 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
8938 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
8939 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
8943 inline_costs += 10 * num_calls++;
8947 CHECK_STACK_OVF (1);
8949 n = read16 (ip + 2);
8951 EMIT_NEW_ARGLOAD (cfg, ins, n);
8956 CHECK_STACK_OVF (1);
8958 n = read16 (ip + 2);
8960 NEW_ARGLOADA (cfg, ins, n);
8961 MONO_ADD_INS (cfg->cbb, ins);
8969 n = read16 (ip + 2);
8971 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
8973 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8977 CHECK_STACK_OVF (1);
8979 n = read16 (ip + 2);
8981 EMIT_NEW_LOCLOAD (cfg, ins, n);
8986 CHECK_STACK_OVF (1);
8988 n = read16 (ip + 2);
8990 EMIT_NEW_LOCLOADA (cfg, ins, n);
8998 n = read16 (ip + 2);
9000 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
9002 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
9009 if (sp != stack_start)
9011 if (cfg->method != method)
9013 * Inlining this into a loop in a parent could lead to
9014 * stack overflows which is different behavior than the
9015 * non-inlined case, thus disable inlining in this case.
9017 goto inline_failure;
9019 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
9020 ins->dreg = alloc_preg (cfg);
9021 ins->sreg1 = sp [0]->dreg;
9022 ins->type = STACK_PTR;
9023 MONO_ADD_INS (cfg->cbb, ins);
9025 cfg->flags |= MONO_CFG_HAS_ALLOCA;
9026 if (header->init_locals)
9027 ins->flags |= MONO_INST_INIT;
9032 case CEE_ENDFILTER: {
9033 MonoExceptionClause *clause, *nearest;
9034 int cc, nearest_num;
9038 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
9040 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
9041 ins->sreg1 = (*sp)->dreg;
9042 MONO_ADD_INS (bblock, ins);
9043 start_new_bblock = 1;
9048 for (cc = 0; cc < header->num_clauses; ++cc) {
9049 clause = &header->clauses [cc];
9050 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
9051 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
9052 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
9058 if ((ip - header->code) != nearest->handler_offset)
9063 case CEE_UNALIGNED_:
9064 ins_flag |= MONO_INST_UNALIGNED;
9065 /* FIXME: record alignment? we can assume 1 for now */
9070 ins_flag |= MONO_INST_VOLATILE;
9074 ins_flag |= MONO_INST_TAILCALL;
9075 cfg->flags |= MONO_CFG_HAS_TAIL;
9076 /* Can't inline tail calls at this time */
9077 inline_costs += 100000;
9084 token = read32 (ip + 2);
9085 klass = mini_get_class (method, token, generic_context);
9086 CHECK_TYPELOAD (klass);
9087 if (generic_class_is_reference_type (cfg, klass)) {
9088 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
9090 GENERIC_SHARING_FAILURE (CEE_INITOBJ);
9091 mini_emit_initobj (cfg, *sp, NULL, klass);
9096 case CEE_CONSTRAINED_:
9098 token = read32 (ip + 2);
9099 constrained_call = mono_class_get_full (image, token, generic_context);
9100 CHECK_TYPELOAD (constrained_call);
9105 MonoInst *iargs [3];
9109 if ((ip [1] == CEE_CPBLK) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
9110 mini_emit_memcpy2 (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
9111 } else if ((ip [1] == CEE_INITBLK) && (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)) {
9112 /* emit_memset only works when val == 0 */
9113 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
9118 if (ip [1] == CEE_CPBLK) {
9119 MonoMethod *memcpy_method = get_memcpy_method ();
9120 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
9122 MonoMethod *memset_method = get_memset_method ();
9123 mono_emit_method_call (cfg, memset_method, iargs, NULL);
9133 ins_flag |= MONO_INST_NOTYPECHECK;
9135 ins_flag |= MONO_INST_NORANGECHECK;
9136 /* we ignore the no-nullcheck for now since we
9137 * really do it explicitly only when doing callvirt->call
9143 int handler_offset = -1;
9145 for (i = 0; i < header->num_clauses; ++i) {
9146 MonoExceptionClause *clause = &header->clauses [i];
9147 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
9148 handler_offset = clause->handler_offset;
9151 bblock->flags |= BB_EXCEPTION_UNSAFE;
9153 g_assert (handler_offset != -1);
9155 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
9156 MONO_INST_NEW (cfg, ins, OP_RETHROW);
9157 ins->sreg1 = load->dreg;
9158 MONO_ADD_INS (bblock, ins);
9160 link_bblock (cfg, bblock, end_bblock);
9161 start_new_bblock = 1;
9169 GENERIC_SHARING_FAILURE (CEE_SIZEOF);
9171 CHECK_STACK_OVF (1);
9173 token = read32 (ip + 2);
9174 /* FIXXME: handle generics. */
9175 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
9176 MonoType *type = mono_type_create_from_typespec (image, token);
9177 token = mono_type_size (type, &ialign);
9179 MonoClass *klass = mono_class_get_full (image, token, generic_context);
9180 CHECK_TYPELOAD (klass);
9181 mono_class_init (klass);
9182 token = mono_class_value_size (klass, &align);
9184 EMIT_NEW_ICONST (cfg, ins, token);
9189 case CEE_REFANYTYPE: {
9190 MonoInst *src_var, *src;
9196 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
9198 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
9199 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
9200 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typed_reference_class->byval_arg, src->dreg, G_STRUCT_OFFSET (MonoTypedRef, type));
9210 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
9215 g_error ("opcode 0x%02x not handled", *ip);
9218 if (start_new_bblock != 1)
9221 bblock->cil_length = ip - bblock->cil_code;
9222 bblock->next_bb = end_bblock;
9224 if (cfg->method == method && cfg->domainvar) {
9226 MonoInst *get_domain;
9228 cfg->cbb = init_localsbb;
9230 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
9231 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
9234 get_domain->dreg = alloc_preg (cfg);
9235 MONO_ADD_INS (cfg->cbb, get_domain);
9237 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
9238 MONO_ADD_INS (cfg->cbb, store);
9241 if (cfg->method == method && cfg->got_var)
9242 mono_emit_load_got_addr (cfg);
9244 if (header->init_locals) {
9247 cfg->cbb = init_localsbb;
9248 cfg->ip = header->code;
9249 for (i = 0; i < header->num_locals; ++i) {
9250 MonoType *ptype = header->locals [i];
9251 int t = ptype->type;
9252 dreg = cfg->locals [i]->dreg;
9254 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
9255 t = ptype->data.klass->enum_basetype->type;
9257 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
9258 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
9259 MONO_EMIT_NEW_ICONST (cfg, cfg->locals [i]->dreg, 0);
9260 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
9261 MONO_EMIT_NEW_I8CONST (cfg, cfg->locals [i]->dreg, 0);
9262 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
9263 MONO_INST_NEW (cfg, ins, OP_R8CONST);
9264 ins->type = STACK_R8;
9265 ins->inst_p0 = (void*)&r8_0;
9266 ins->dreg = alloc_dreg (cfg, STACK_R8);
9267 MONO_ADD_INS (init_localsbb, ins);
9268 EMIT_NEW_LOCSTORE (cfg, store, i, ins);
9269 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
9270 + ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
9271 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (ptype));
9273 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
9280 /* resolve backward branches in the middle of an existing basic block */
9281 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
9283 /*printf ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
9284 tblock = find_previous (cfg->cil_offset_to_bb, header->code_size, start_bblock, bblock->cil_code);
9285 if (tblock != start_bblock) {
9287 split_bblock (cfg, tblock, bblock);
9288 l = bblock->cil_code - header->code;
9289 bblock->cil_length = tblock->cil_length - l;
9290 tblock->cil_length = l;
9292 printf ("recheck failed.\n");
9296 if (cfg->method == method) {
9298 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9299 bb->region = mono_find_block_region (cfg, bb->real_offset);
9301 mono_create_spvar_for_region (cfg, bb->region);
9302 if (cfg->verbose_level > 2)
9303 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
9307 g_slist_free (class_inits);
9308 dont_inline = g_list_remove (dont_inline, method);
9310 if (inline_costs < 0) {
9313 /* Method is too large */
9314 mname = mono_method_full_name (method, TRUE);
9315 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
9316 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
9321 if ((cfg->verbose_level > 1) && (cfg->method == method))
9322 mono_print_code (cfg, "AFTER METHOD-TO-IR");
9324 return inline_costs;
9327 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
9328 g_slist_free (class_inits);
9329 dont_inline = g_list_remove (dont_inline, method);
9333 g_slist_free (class_inits);
9334 dont_inline = g_list_remove (dont_inline, method);
9338 g_slist_free (class_inits);
9339 dont_inline = g_list_remove (dont_inline, method);
9340 cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
9344 g_slist_free (class_inits);
9345 dont_inline = g_list_remove (dont_inline, method);
9346 set_exception_type_from_invalid_il (cfg, method, ip);
9351 store_membase_reg_to_store_membase_imm (int opcode)
9354 case OP_STORE_MEMBASE_REG:
9355 return OP_STORE_MEMBASE_IMM;
9356 case OP_STOREI1_MEMBASE_REG:
9357 return OP_STOREI1_MEMBASE_IMM;
9358 case OP_STOREI2_MEMBASE_REG:
9359 return OP_STOREI2_MEMBASE_IMM;
9360 case OP_STOREI4_MEMBASE_REG:
9361 return OP_STOREI4_MEMBASE_IMM;
9362 case OP_STOREI8_MEMBASE_REG:
9363 return OP_STOREI8_MEMBASE_IMM;
9365 g_assert_not_reached ();
9372 mono_op_to_op_imm (int opcode)
9382 return OP_IDIV_UN_IMM;
9386 return OP_IREM_UN_IMM;
9400 return OP_ISHR_UN_IMM;
9417 return OP_LSHR_UN_IMM;
9420 return OP_COMPARE_IMM;
9422 return OP_ICOMPARE_IMM;
9424 return OP_LCOMPARE_IMM;
9426 case OP_STORE_MEMBASE_REG:
9427 return OP_STORE_MEMBASE_IMM;
9428 case OP_STOREI1_MEMBASE_REG:
9429 return OP_STOREI1_MEMBASE_IMM;
9430 case OP_STOREI2_MEMBASE_REG:
9431 return OP_STOREI2_MEMBASE_IMM;
9432 case OP_STOREI4_MEMBASE_REG:
9433 return OP_STOREI4_MEMBASE_IMM;
9435 #if defined(__i386__) || defined (__x86_64__)
9437 return OP_X86_PUSH_IMM;
9438 case OP_X86_COMPARE_MEMBASE_REG:
9439 return OP_X86_COMPARE_MEMBASE_IMM;
9441 #if defined(__x86_64__)
9442 case OP_AMD64_ICOMPARE_MEMBASE_REG:
9443 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
9445 case OP_VOIDCALL_REG:
9454 return OP_LOCALLOC_IMM;
9461 ldind_to_load_membase (int opcode)
9465 return OP_LOADI1_MEMBASE;
9467 return OP_LOADU1_MEMBASE;
9469 return OP_LOADI2_MEMBASE;
9471 return OP_LOADU2_MEMBASE;
9473 return OP_LOADI4_MEMBASE;
9475 return OP_LOADU4_MEMBASE;
9477 return OP_LOAD_MEMBASE;
9479 return OP_LOAD_MEMBASE;
9481 return OP_LOADI8_MEMBASE;
9483 return OP_LOADR4_MEMBASE;
9485 return OP_LOADR8_MEMBASE;
9487 g_assert_not_reached ();
9494 stind_to_store_membase (int opcode)
9498 return OP_STOREI1_MEMBASE_REG;
9500 return OP_STOREI2_MEMBASE_REG;
9502 return OP_STOREI4_MEMBASE_REG;
9505 return OP_STORE_MEMBASE_REG;
9507 return OP_STOREI8_MEMBASE_REG;
9509 return OP_STORER4_MEMBASE_REG;
9511 return OP_STORER8_MEMBASE_REG;
9513 g_assert_not_reached ();
9520 mono_load_membase_to_load_mem (int opcode)
9522 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
9523 #if defined(__i386__) || defined(__x86_64__)
9525 case OP_LOAD_MEMBASE:
9527 case OP_LOADU1_MEMBASE:
9528 return OP_LOADU1_MEM;
9529 case OP_LOADU2_MEMBASE:
9530 return OP_LOADU2_MEM;
9531 case OP_LOADI4_MEMBASE:
9532 return OP_LOADI4_MEM;
9533 case OP_LOADU4_MEMBASE:
9534 return OP_LOADU4_MEM;
9535 #if SIZEOF_VOID_P == 8
9536 case OP_LOADI8_MEMBASE:
9537 return OP_LOADI8_MEM;
9546 op_to_op_dest_membase (int store_opcode, int opcode)
9548 #if defined(__i386__)
9549 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
9554 return OP_X86_ADD_MEMBASE_REG;
9556 return OP_X86_SUB_MEMBASE_REG;
9558 return OP_X86_AND_MEMBASE_REG;
9560 return OP_X86_OR_MEMBASE_REG;
9562 return OP_X86_XOR_MEMBASE_REG;
9565 return OP_X86_ADD_MEMBASE_IMM;
9568 return OP_X86_SUB_MEMBASE_IMM;
9571 return OP_X86_AND_MEMBASE_IMM;
9574 return OP_X86_OR_MEMBASE_IMM;
9577 return OP_X86_XOR_MEMBASE_IMM;
9583 #if defined(__x86_64__)
9584 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
9589 return OP_X86_ADD_MEMBASE_REG;
9591 return OP_X86_SUB_MEMBASE_REG;
9593 return OP_X86_AND_MEMBASE_REG;
9595 return OP_X86_OR_MEMBASE_REG;
9597 return OP_X86_XOR_MEMBASE_REG;
9599 return OP_X86_ADD_MEMBASE_IMM;
9601 return OP_X86_SUB_MEMBASE_IMM;
9603 return OP_X86_AND_MEMBASE_IMM;
9605 return OP_X86_OR_MEMBASE_IMM;
9607 return OP_X86_XOR_MEMBASE_IMM;
9609 return OP_AMD64_ADD_MEMBASE_REG;
9611 return OP_AMD64_SUB_MEMBASE_REG;
9613 return OP_AMD64_AND_MEMBASE_REG;
9615 return OP_AMD64_OR_MEMBASE_REG;
9617 return OP_AMD64_XOR_MEMBASE_REG;
9620 return OP_AMD64_ADD_MEMBASE_IMM;
9623 return OP_AMD64_SUB_MEMBASE_IMM;
9626 return OP_AMD64_AND_MEMBASE_IMM;
9629 return OP_AMD64_OR_MEMBASE_IMM;
9632 return OP_AMD64_XOR_MEMBASE_IMM;
9642 op_to_op_store_membase (int store_opcode, int opcode)
9644 #if defined(__i386__) || defined(__x86_64__)
9647 if (store_opcode == OP_STOREI1_MEMBASE_REG)
9648 return OP_X86_SETEQ_MEMBASE;
9650 if (store_opcode == OP_STOREI1_MEMBASE_REG)
9651 return OP_X86_SETNE_MEMBASE;
9659 op_to_op_src1_membase (int load_opcode, int opcode)
9662 /* FIXME: This has sign extension issues */
9664 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
9665 return OP_X86_COMPARE_MEMBASE8_IMM;
9668 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
9673 return OP_X86_PUSH_MEMBASE;
9674 case OP_COMPARE_IMM:
9675 case OP_ICOMPARE_IMM:
9676 return OP_X86_COMPARE_MEMBASE_IMM;
9679 return OP_X86_COMPARE_MEMBASE_REG;
9684 /* FIXME: This has sign extension issues */
9686 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
9687 return OP_X86_COMPARE_MEMBASE8_IMM;
9692 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
9693 return OP_X86_PUSH_MEMBASE;
9695 /* FIXME: This only works for 32 bit immediates
9696 case OP_COMPARE_IMM:
9697 case OP_LCOMPARE_IMM:
9698 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
9699 return OP_AMD64_COMPARE_MEMBASE_IMM;
9701 case OP_ICOMPARE_IMM:
9702 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9703 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
9707 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
9708 return OP_AMD64_COMPARE_MEMBASE_REG;
9711 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9712 return OP_AMD64_ICOMPARE_MEMBASE_REG;
9721 op_to_op_src2_membase (int load_opcode, int opcode)
9724 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
9730 return OP_X86_COMPARE_REG_MEMBASE;
9732 return OP_X86_ADD_REG_MEMBASE;
9734 return OP_X86_SUB_REG_MEMBASE;
9736 return OP_X86_AND_REG_MEMBASE;
9738 return OP_X86_OR_REG_MEMBASE;
9740 return OP_X86_XOR_REG_MEMBASE;
9747 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9748 return OP_AMD64_ICOMPARE_REG_MEMBASE;
9752 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9753 return OP_AMD64_COMPARE_REG_MEMBASE;
9756 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9757 return OP_X86_ADD_REG_MEMBASE;
9759 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9760 return OP_X86_SUB_REG_MEMBASE;
9762 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9763 return OP_X86_AND_REG_MEMBASE;
9765 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9766 return OP_X86_OR_REG_MEMBASE;
9768 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
9769 return OP_X86_XOR_REG_MEMBASE;
9771 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9772 return OP_AMD64_ADD_REG_MEMBASE;
9774 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9775 return OP_AMD64_SUB_REG_MEMBASE;
9777 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9778 return OP_AMD64_AND_REG_MEMBASE;
9780 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9781 return OP_AMD64_OR_REG_MEMBASE;
9783 if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE))
9784 return OP_AMD64_XOR_REG_MEMBASE;
9792 mono_op_to_op_imm_noemul (int opcode)
9795 #if SIZEOF_VOID_P == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
9800 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
9808 return mono_op_to_op_imm (opcode);
9813 * mono_handle_global_vregs:
9815 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
9819 mono_handle_global_vregs (MonoCompile *cfg)
9825 vreg_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
9827 /* Find local vregs used in more than one bb */
9828 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9829 MonoInst *ins = bb->code;
9830 int block_num = bb->block_num;
9832 if (cfg->verbose_level > 1)
9833 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
9836 for (; ins; ins = ins->next) {
9837 const char *spec = INS_INFO (ins->opcode);
9838 int regtype, regindex;
9841 if (G_UNLIKELY (cfg->verbose_level > 1))
9842 mono_print_ins (ins);
9844 g_assert (ins->opcode >= MONO_CEE_LAST);
9846 for (regindex = 0; regindex < 3; regindex ++) {
9849 if (regindex == 0) {
9850 regtype = spec [MONO_INST_DEST];
9854 } else if (regindex == 1) {
9855 regtype = spec [MONO_INST_SRC1];
9860 regtype = spec [MONO_INST_SRC2];
9866 #if SIZEOF_VOID_P == 4
9867 if (regtype == 'l') {
9869 * Since some instructions reference the original long vreg,
9870 * and some reference the two component vregs, it is quite hard
9871 * to determine when it needs to be global. So be conservative.
9873 if (!get_vreg_to_inst (cfg, vreg)) {
9874 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
9876 if (cfg->verbose_level > 1)
9877 printf ("LONG VREG R%d made global.\n", vreg);
9881 * Make the component vregs volatile since the optimizations can
9882 * get confused otherwise.
9884 get_vreg_to_inst (cfg, vreg + 1)->flags |= MONO_INST_VOLATILE;
9885 get_vreg_to_inst (cfg, vreg + 2)->flags |= MONO_INST_VOLATILE;
9889 g_assert (vreg != -1);
9891 prev_bb = vreg_to_bb [vreg];
9893 /* 0 is a valid block num */
9894 vreg_to_bb [vreg] = block_num + 1;
9895 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
9896 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
9899 if (!get_vreg_to_inst (cfg, vreg)) {
9900 if (G_UNLIKELY (cfg->verbose_level > 1))
9901 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
9905 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
9908 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
9911 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
9914 g_assert_not_reached ();
9918 /* Flag as having been used in more than one bb */
9919 vreg_to_bb [vreg] = -1;
9925 /* If a variable is used in only one bblock, convert it into a local vreg */
9926 for (i = 0; i < cfg->num_varinfo; i++) {
9927 MonoInst *var = cfg->varinfo [i];
9928 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
9930 switch (var->type) {
9936 #if SIZEOF_VOID_P == 8
9939 #if !defined(__i386__) && !defined(MONO_ARCH_SOFT_FLOAT)
9940 /* Enabling this screws up the fp stack on x86 */
9943 /* Arguments are implicitly global */
9944 /* Putting R4 vars into registers doesn't work currently */
9945 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)) {
9947 * Make that the variable's liveness interval doesn't contain a call, since
9948 * that would cause the lvreg to be spilled, making the whole optimization
9951 /* This is too slow for JIT compilation */
9953 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
9955 int def_index, call_index, ins_index;
9956 gboolean spilled = FALSE;
9961 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
9962 const char *spec = INS_INFO (ins->opcode);
9964 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
9965 def_index = ins_index;
9967 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
9968 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
9969 if (call_index > def_index) {
9975 if (MONO_IS_CALL (ins))
9976 call_index = ins_index;
9986 if (G_UNLIKELY (cfg->verbose_level > 2))
9987 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
9988 var->flags |= MONO_INST_IS_DEAD;
9989 cfg->vreg_to_inst [var->dreg] = NULL;
9996 * Compress the varinfo and vars tables so the liveness computation is faster and
9997 * takes up less space.
10000 for (i = 0; i < cfg->num_varinfo; ++i) {
10001 MonoInst *var = cfg->varinfo [i];
10002 if (pos < i && cfg->locals_start == i)
10003 cfg->locals_start = pos;
10004 if (!(var->flags & MONO_INST_IS_DEAD)) {
10006 cfg->varinfo [pos] = cfg->varinfo [i];
10007 cfg->varinfo [pos]->inst_c0 = pos;
10008 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
10009 cfg->vars [pos].idx = pos;
10010 #if SIZEOF_VOID_P == 4
10011 if (cfg->varinfo [pos]->type == STACK_I8) {
10012 /* Modify the two component vars too */
10015 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 1);
10016 var1->inst_c0 = pos;
10017 var1 = get_vreg_to_inst (cfg, cfg->varinfo [pos]->dreg + 2);
10018 var1->inst_c0 = pos;
10025 cfg->num_varinfo = pos;
10026 if (cfg->locals_start > cfg->num_varinfo)
10027 cfg->locals_start = cfg->num_varinfo;
10031 * mono_spill_global_vars:
10033 * Generate spill code for variables which are not allocated to registers,
10034 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
10035 * code is generated which could be optimized by the local optimization passes.
10038 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
10040 MonoBasicBlock *bb;
10042 int orig_next_vreg;
10043 guint32 *vreg_to_lvreg;
10045 guint32 i, lvregs_len;
10046 gboolean dest_has_lvreg = FALSE;
10047 guint32 stacktypes [128];
10049 *need_local_opts = FALSE;
10051 memset (spec2, 0, sizeof (spec2));
10053 /* FIXME: Move this function to mini.c */
10054 stacktypes ['i'] = STACK_PTR;
10055 stacktypes ['l'] = STACK_I8;
10056 stacktypes ['f'] = STACK_R8;
10058 #if SIZEOF_VOID_P == 4
10059 /* Create MonoInsts for longs */
10060 for (i = 0; i < cfg->num_varinfo; i++) {
10061 MonoInst *ins = cfg->varinfo [i];
10063 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
10064 switch (ins->type) {
10065 #ifdef MONO_ARCH_SOFT_FLOAT
10071 g_assert (ins->opcode == OP_REGOFFSET);
10073 tree = get_vreg_to_inst (cfg, ins->dreg + 1);
10075 tree->opcode = OP_REGOFFSET;
10076 tree->inst_basereg = ins->inst_basereg;
10077 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
10079 tree = get_vreg_to_inst (cfg, ins->dreg + 2);
10081 tree->opcode = OP_REGOFFSET;
10082 tree->inst_basereg = ins->inst_basereg;
10083 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
10093 /* FIXME: widening and truncation */
10096 * As an optimization, when a variable allocated to the stack is first loaded into
10097 * an lvreg, we will remember the lvreg and use it the next time instead of loading
10098 * the variable again.
10100 orig_next_vreg = cfg->next_vreg;
10101 vreg_to_lvreg = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
10102 lvregs = mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
10105 /* Add spill loads/stores */
10106 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
10109 if (cfg->verbose_level > 1)
10110 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
10112 /* Clear vreg_to_lvreg array */
10113 for (i = 0; i < lvregs_len; i++)
10114 vreg_to_lvreg [lvregs [i]] = 0;
10118 MONO_BB_FOR_EACH_INS (bb, ins) {
10119 const char *spec = INS_INFO (ins->opcode);
10120 int regtype, srcindex, sreg, tmp_reg, prev_dreg;
10121 gboolean store, no_lvreg;
10123 if (G_UNLIKELY (cfg->verbose_level > 1))
10124 mono_print_ins (ins);
10126 if (ins->opcode == OP_NOP)
10130 * We handle LDADDR here as well, since it can only be decomposed
10131 * when variable addresses are known.
10133 if (ins->opcode == OP_LDADDR) {
10134 MonoInst *var = ins->inst_p0;
10136 if (var->opcode == OP_VTARG_ADDR) {
10137 /* Happens on SPARC/S390 where vtypes are passed by reference */
10138 MonoInst *vtaddr = var->inst_left;
10139 if (vtaddr->opcode == OP_REGVAR) {
10140 ins->opcode = OP_MOVE;
10141 ins->sreg1 = vtaddr->dreg;
10143 else if (var->inst_left->opcode == OP_REGOFFSET) {
10144 ins->opcode = OP_LOAD_MEMBASE;
10145 ins->inst_basereg = vtaddr->inst_basereg;
10146 ins->inst_offset = vtaddr->inst_offset;
10150 g_assert (var->opcode == OP_REGOFFSET);
10152 ins->opcode = OP_ADD_IMM;
10153 ins->sreg1 = var->inst_basereg;
10154 ins->inst_imm = var->inst_offset;
10157 *need_local_opts = TRUE;
10158 spec = INS_INFO (ins->opcode);
10161 if (ins->opcode < MONO_CEE_LAST) {
10162 mono_print_ins (ins);
10163 g_assert_not_reached ();
10167 * Store opcodes have destbasereg in the dreg, but in reality, it is an
10171 if (MONO_IS_STORE_MEMBASE (ins)) {
10172 tmp_reg = ins->dreg;
10173 ins->dreg = ins->sreg2;
10174 ins->sreg2 = tmp_reg;
10177 spec2 [MONO_INST_DEST] = ' ';
10178 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
10179 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
10181 } else if (MONO_IS_STORE_MEMINDEX (ins))
10182 g_assert_not_reached ();
10187 if (G_UNLIKELY (cfg->verbose_level > 1))
10188 printf ("\t %.3s %d %d %d\n", spec, ins->dreg, ins->sreg1, ins->sreg2);
10193 regtype = spec [MONO_INST_DEST];
10194 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
10197 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
10198 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
10199 MonoInst *store_ins;
10202 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
10204 if (var->opcode == OP_REGVAR) {
10205 ins->dreg = var->dreg;
10206 } 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)) {
10208 * Instead of emitting a load+store, use a _membase opcode.
10210 g_assert (var->opcode == OP_REGOFFSET);
10211 if (ins->opcode == OP_MOVE) {
10214 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
10215 ins->inst_basereg = var->inst_basereg;
10216 ins->inst_offset = var->inst_offset;
10219 spec = INS_INFO (ins->opcode);
10223 g_assert (var->opcode == OP_REGOFFSET);
10225 prev_dreg = ins->dreg;
10227 /* Invalidate any previous lvreg for this vreg */
10228 vreg_to_lvreg [ins->dreg] = 0;
10232 #ifdef MONO_ARCH_SOFT_FLOAT
10233 if (store_opcode == OP_STORER8_MEMBASE_REG) {
10235 store_opcode = OP_STOREI8_MEMBASE_REG;
10239 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
10241 if (regtype == 'l') {
10242 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, ins->dreg + 1);
10243 mono_bblock_insert_after_ins (bb, ins, store_ins);
10244 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, ins->dreg + 2);
10245 mono_bblock_insert_after_ins (bb, ins, store_ins);
10248 g_assert (store_opcode != OP_STOREV_MEMBASE);
10250 /* Try to fuse the store into the instruction itself */
10251 /* FIXME: Add more instructions */
10252 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
10253 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
10254 ins->inst_imm = ins->inst_c0;
10255 ins->inst_destbasereg = var->inst_basereg;
10256 ins->inst_offset = var->inst_offset;
10257 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE))) {
10258 ins->opcode = store_opcode;
10259 ins->inst_destbasereg = var->inst_basereg;
10260 ins->inst_offset = var->inst_offset;
10264 tmp_reg = ins->dreg;
10265 ins->dreg = ins->sreg2;
10266 ins->sreg2 = tmp_reg;
10269 spec2 [MONO_INST_DEST] = ' ';
10270 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
10271 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
10273 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
10274 // FIXME: The backends expect the base reg to be in inst_basereg
10275 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
10277 ins->inst_basereg = var->inst_basereg;
10278 ins->inst_offset = var->inst_offset;
10279 spec = INS_INFO (ins->opcode);
10281 /* printf ("INS: "); mono_print_ins (ins); */
10282 /* Create a store instruction */
10283 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
10285 /* Insert it after the instruction */
10286 mono_bblock_insert_after_ins (bb, ins, store_ins);
10289 * We can't assign ins->dreg to var->dreg here, since the
10290 * sregs could use it. So set a flag, and do it after
10293 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)))
10294 dest_has_lvreg = TRUE;
10303 for (srcindex = 0; srcindex < 2; ++srcindex) {
10304 regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
10305 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
10307 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
10308 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
10309 MonoInst *var = get_vreg_to_inst (cfg, sreg);
10310 MonoInst *load_ins;
10311 guint32 load_opcode;
10313 if (var->opcode == OP_REGVAR) {
10315 ins->sreg1 = var->dreg;
10317 ins->sreg2 = var->dreg;
10321 g_assert (var->opcode == OP_REGOFFSET);
10323 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
10325 g_assert (load_opcode != OP_LOADV_MEMBASE);
10327 if (vreg_to_lvreg [sreg]) {
10328 /* The variable is already loaded to an lvreg */
10329 if (G_UNLIKELY (cfg->verbose_level > 1))
10330 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
10332 ins->sreg1 = vreg_to_lvreg [sreg];
10334 ins->sreg2 = vreg_to_lvreg [sreg];
10338 /* Try to fuse the load into the instruction */
10339 if ((srcindex == 0) && (op_to_op_src1_membase (load_opcode, ins->opcode) != -1)) {
10340 ins->opcode = op_to_op_src1_membase (load_opcode, ins->opcode);
10341 ins->inst_basereg = var->inst_basereg;
10342 ins->inst_offset = var->inst_offset;
10343 } else if ((srcindex == 1) && (op_to_op_src2_membase (load_opcode, ins->opcode) != -1)) {
10344 ins->opcode = op_to_op_src2_membase (load_opcode, ins->opcode);
10345 ins->sreg2 = var->inst_basereg;
10346 ins->inst_offset = var->inst_offset;
10348 if ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE)) {
10349 ins->opcode = OP_NOP;
10352 //printf ("%d ", srcindex); mono_print_ins (ins);
10354 sreg = alloc_dreg (cfg, stacktypes [regtype]);
10356 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) {
10357 if (var->dreg == prev_dreg) {
10359 * sreg refers to the value loaded by the load
10360 * emitted below, but we need to use ins->dreg
10361 * since it refers to the store emitted earlier.
10365 vreg_to_lvreg [var->dreg] = sreg;
10366 g_assert (lvregs_len < 1024);
10367 lvregs [lvregs_len ++] = var->dreg;
10376 if (regtype == 'l') {
10377 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 2, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
10378 mono_bblock_insert_before_ins (bb, ins, load_ins);
10379 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 1, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
10380 mono_bblock_insert_before_ins (bb, ins, load_ins);
10383 #if SIZEOF_VOID_P == 4
10384 g_assert (load_opcode != OP_LOADI8_MEMBASE);
10386 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
10387 mono_bblock_insert_before_ins (bb, ins, load_ins);
10393 if (dest_has_lvreg) {
10394 vreg_to_lvreg [prev_dreg] = ins->dreg;
10395 g_assert (lvregs_len < 1024);
10396 lvregs [lvregs_len ++] = prev_dreg;
10397 dest_has_lvreg = FALSE;
10401 tmp_reg = ins->dreg;
10402 ins->dreg = ins->sreg2;
10403 ins->sreg2 = tmp_reg;
10406 if (MONO_IS_CALL (ins)) {
10407 /* Clear vreg_to_lvreg array */
10408 for (i = 0; i < lvregs_len; i++)
10409 vreg_to_lvreg [lvregs [i]] = 0;
10413 if (cfg->verbose_level > 1)
10414 mono_print_ins_index (1, ins);
10421 * - use 'iadd' instead of 'int_add'
10422 * - handling ovf opcodes: decompose in method_to_ir.
10423 * - unify iregs/fregs
10424 * -> partly done, the missing parts are:
10425 * - a more complete unification would involve unifying the hregs as well, so
10426 * code wouldn't need if (fp) all over the place. but that would mean the hregs
10427 * would no longer map to the machine hregs, so the code generators would need to
10428 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
10429 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
10430 * fp/non-fp branches speeds it up by about 15%.
10431 * - use sext/zext opcodes instead of shifts
10433 * - get rid of TEMPLOADs if possible and use vregs instead
10434 * - clean up usage of OP_P/OP_ opcodes
10435 * - cleanup usage of DUMMY_USE
10436 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
10438 * - set the stack type and allocate a dreg in the EMIT_NEW macros
10439 * - get rid of all the <foo>2 stuff when the new JIT is ready.
10440 * - make sure handle_stack_args () is called before the branch is emitted
10441 * - when the new IR is done, get rid of all unused stuff
10442 * - COMPARE/BEQ as separate instructions or unify them ?
10443 * - keeping them separate allows specialized compare instructions like
10444 * compare_imm, compare_membase
10445 * - most back ends unify fp compare+branch, fp compare+ceq
10446 * - integrate handle_stack_args into inline_method
10447 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
10448 * - Things to backport to the old JIT:
10449 * - op_atomic_exchange fix for amd64
10450 * - localloc fix for amd64
10451 * - x86 type_token change
10453 * - long eq/ne optimizations
10454 * - handle long shift opts on 32 bit platforms somehow: they require
10455 * 3 sregs (2 for arg1 and 1 for arg2)
10456 * - make byref a 'normal' type.
10457 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
10458 * variable if needed.
10459 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
10460 * like inline_method.
10461 * - remove inlining restrictions
10462 * - remove mono_save_args.
10463 * - add 'introduce a new optimization to simplify some range checks'
10464 * - fix LNEG and enable cfold of INEG
10465 * - generalize x86 optimizations like ldelema as a peephole optimization
10466 * - add store_mem_imm for amd64
10467 * - optimize the loading of the interruption flag in the managed->native wrappers
10468 * - avoid special handling of OP_NOP in passes
10469 * - move code inserting instructions into one function/macro.
10470 * - cleanup the code replacement in decompose_long_opts ()
10471 * - try a coalescing phase after liveness analysis
10472 * - add float -> vreg conversion + local optimizations on !x86
10473 * - figure out how to handle decomposed branches during optimizations, ie.
10474 * compare+branch, op_jump_table+op_br etc.
10475 * - promote RuntimeXHandles to vregs
10476 * - vtype cleanups:
10477 * - add a NEW_VARLOADA_VREG macro
10478 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
10479 * accessing vtype fields.
10480 * - get rid of I8CONST on 64 bit platforms
10481 * - dealing with the increase in code size due to branches created during opcode
10483 * - use extended basic blocks
10484 * - all parts of the JIT
10485 * - handle_global_vregs () && local regalloc
10486 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
10487 * - sources of increase in code size:
10490 * - isinst and castclass
10491 * - lvregs not allocated to global registers even if used multiple times
10492 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
10494 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
10495 * - add all micro optimizations from the old JIT
10496 * - put tree optimizations into the deadce pass
10497 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
10498 * specific function.
10499 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
10500 * fcompare + branchCC.
10501 * - sig->ret->byref seems to be set for some calls made from ldfld wrappers when
10502 * running generics.exe.
10503 * - create a helper function for allocating a stack slot, taking into account
10504 * MONO_CFG_HAS_SPILLUP.
10505 * - merge new GC changes in mini.c.
10507 * - merge the ia64 switch changes.
10508 * - merge the mips conditional changes.
10509 * - remove unused opcodes from mini-ops.h, remove "op_" from the opcode names,
10510 * remove the op_ opcodes from the cpu-..md files, clean up the cpu-..md files.
10511 * - make the cpu_ tables smaller when the usage of the cee_ opcodes is removed.
10512 * - optimize mono_regstate2_alloc_int/float.
10513 * - fix the pessimistic handling of variables accessed in exception handler blocks.
10514 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
10515 * parts of the tree could be separated by other instructions, killing the tree
10516 * arguments, or stores killing loads etc. Also, should we fold loads into other
10517 * instructions if the result of the load is used multiple times ?
10518 * - make the REM_IMM optimization in mini-x86.c arch-independent.
10519 * - LAST MERGE: 108395.
10520 * - when returning vtypes in registers, generate IR and append it to the end of the
10521 * last bb instead of doing it in the epilog.
10522 * - when the new JIT is done, use the ins emission macros in ir-emit.h instead of the
10523 * ones in inssel.h.
10524 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
10532 - When to decompose opcodes:
10533 - earlier: this makes some optimizations hard to implement, since the low level IR
10534 no longer contains the neccessary information. But it is easier to do.
10535 - later: harder to implement, enables more optimizations.
10536 - Branches inside bblocks:
10537 - created when decomposing complex opcodes.
10538 - branches to another bblock: harmless, but not tracked by the branch
10539 optimizations, so need to branch to a label at the start of the bblock.
10540 - branches to inside the same bblock: very problematic, trips up the local
10541 reg allocator. Can be fixed by spitting the current bblock, but that is a
10542 complex operation, since some local vregs can become global vregs etc.
10543 - Local/global vregs:
10544 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
10545 local register allocator.
10546 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
10547 structure, created by mono_create_var (). Assigned to hregs or the stack by
10548 the global register allocator.
10549 - When to do optimizations like alu->alu_imm:
10550 - earlier -> saves work later on since the IR will be smaller/simpler
10551 - later -> can work on more instructions
10552 - Handling of valuetypes:
10553 - When a vtype is pushed on the stack, a new tempotary is created, an
10554 instruction computing its address (LDADDR) is emitted and pushed on
10555 the stack. Need to optimize cases when the vtype is used immediately as in
10556 argument passing, stloc etc.
10557 - Instead of the to_end stuff in the old JIT, simply call the function handling
10558 the values on the stack before emitting the last instruction of the bb.