2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * Copyright 2002-2003 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc.
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
21 #ifdef HAVE_SYS_TIME_H
25 #include <mono/utils/memcheck.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/loader.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/class.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/threads.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/debug-helpers.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/gc-internal.h>
43 #include <mono/metadata/threads-types.h>
44 #include <mono/metadata/verify.h>
45 #include <mono/metadata/verify-internals.h>
46 #include <mono/metadata/mempool-internals.h>
47 #include <mono/metadata/attach.h>
48 #include <mono/metadata/runtime.h>
49 #include <mono/utils/mono-math.h>
50 #include <mono/utils/mono-compiler.h>
51 #include <mono/utils/mono-counters.h>
52 #include <mono/utils/mono-error-internals.h>
53 #include <mono/utils/mono-logger-internal.h>
54 #include <mono/utils/mono-mmap.h>
55 #include <mono/utils/mono-path.h>
56 #include <mono/utils/mono-tls.h>
57 #include <mono/utils/mono-hwcap.h>
58 #include <mono/utils/dtrace.h>
59 #include <mono/utils/mono-threads.h>
60 #include <mono/io-layer/io-layer.h>
63 #include "seq-points.h"
71 #include "jit-icalls.h"
74 #include "debugger-agent.h"
76 MonoTraceSpec *mono_jit_trace_calls;
77 MonoMethodDesc *mono_inject_async_exc_method;
78 int mono_inject_async_exc_pos;
79 MonoMethodDesc *mono_break_at_bb_method;
80 int mono_break_at_bb_bb_num;
81 gboolean mono_do_x86_stack_align = TRUE;
82 gboolean mono_using_xdebug;
84 #define mono_jit_lock() mono_mutex_lock (&jit_mutex)
85 #define mono_jit_unlock() mono_mutex_unlock (&jit_mutex)
86 static mono_mutex_t jit_mutex;
88 /* Whenever to check for pending exceptions in managed-to-native wrappers */
89 gboolean check_for_pending_exc = TRUE;
92 mono_realloc_native_code (MonoCompile *cfg)
94 #if defined(__default_codegen__)
95 return g_realloc (cfg->native_code, cfg->code_size);
96 #elif defined(__native_client_codegen__)
99 guint alignment_check;
101 /* Save the old alignment offset so we can re-align after the realloc. */
102 old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
103 cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
105 cfg->native_code_alloc = g_realloc ( cfg->native_code_alloc,
106 cfg->code_size + kNaClAlignment );
108 /* Align native_code to next nearest kNaClAlignment byte. */
109 native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
110 native_code = (guint)native_code & ~kNaClAlignmentMask;
112 /* Shift the data to be 32-byte aligned again. */
113 memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
115 alignment_check = (guint)native_code & kNaClAlignmentMask;
116 g_assert (alignment_check == 0);
119 g_assert_not_reached ();
120 return cfg->native_code;
124 #ifdef __native_client_codegen__
126 /* Prevent instructions from straddling a 32-byte alignment boundary. */
127 /* Instructions longer than 32 bytes must be aligned internally. */
128 /* IN: pcode, instlen */
130 void mono_nacl_align_inst(guint8 **pcode, int instlen) {
133 space_in_block = kNaClAlignment - ((uintptr_t)(*pcode) & kNaClAlignmentMask);
135 if (G_UNLIKELY (instlen >= kNaClAlignment)) {
136 g_assert_not_reached();
137 } else if (instlen > space_in_block) {
138 *pcode = mono_arch_nacl_pad(*pcode, space_in_block);
142 /* Move emitted call sequence to the end of a kNaClAlignment-byte block. */
143 /* IN: start pointer to start of call sequence */
144 /* IN: pcode pointer to end of call sequence (current "IP") */
145 /* OUT: start pointer to the start of the call sequence after padding */
146 /* OUT: pcode pointer to the end of the call sequence after padding */
147 void mono_nacl_align_call(guint8 **start, guint8 **pcode) {
148 const size_t MAX_NACL_CALL_LENGTH = kNaClAlignment;
149 guint8 copy_of_call[MAX_NACL_CALL_LENGTH];
152 const size_t length = (size_t)((*pcode)-(*start));
153 g_assert(length < MAX_NACL_CALL_LENGTH);
155 memcpy(copy_of_call, *start, length);
156 temp = mono_nacl_pad_call(*start, (guint8)length);
157 memcpy(temp, copy_of_call, length);
159 (*pcode) = temp + length;
162 /* mono_nacl_pad_call(): Insert padding for Native Client call instructions */
163 /* code pointer to buffer for emitting code */
164 /* ilength length of call instruction */
165 guint8 *mono_nacl_pad_call(guint8 *code, guint8 ilength) {
166 int freeSpaceInBlock = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
167 int padding = freeSpaceInBlock - ilength;
170 /* There isn't enough space in this block for the instruction. */
171 /* Fill this block and start a new one. */
172 code = mono_arch_nacl_pad(code, freeSpaceInBlock);
173 freeSpaceInBlock = kNaClAlignment;
174 padding = freeSpaceInBlock - ilength;
176 g_assert(ilength > 0);
177 g_assert(padding >= 0);
178 g_assert(padding < kNaClAlignment);
179 if (0 == padding) return code;
180 return mono_arch_nacl_pad(code, padding);
183 guint8 *mono_nacl_align(guint8 *code) {
184 int padding = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
185 if (padding != kNaClAlignment) code = mono_arch_nacl_pad(code, padding);
189 void mono_nacl_fix_patches(const guint8 *code, MonoJumpInfo *ji)
191 #ifndef USE_JUMP_TABLES
192 MonoJumpInfo *patch_info;
193 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
194 unsigned char *ip = patch_info->ip.i + code;
195 ip = mono_arch_nacl_skip_nops(ip);
196 patch_info->ip.i = ip - code;
200 #endif /* __native_client_codegen__ */
203 MonoExceptionClause *clause;
204 MonoBasicBlock *basic_block;
209 * mono_emit_unwind_op:
211 * Add an unwind op with the given parameters for the list of unwind ops stored in
215 mono_emit_unwind_op (MonoCompile *cfg, int when, int tag, int reg, int val)
217 MonoUnwindOp *op = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoUnwindOp));
224 cfg->unwind_ops = g_slist_append_mempool (cfg->mempool, cfg->unwind_ops, op);
225 if (cfg->verbose_level > 1) {
228 printf ("CFA: [%x] def_cfa: %s+0x%x\n", when, mono_arch_regname (reg), val);
230 case DW_CFA_def_cfa_register:
231 printf ("CFA: [%x] def_cfa_reg: %s\n", when, mono_arch_regname (reg));
233 case DW_CFA_def_cfa_offset:
234 printf ("CFA: [%x] def_cfa_offset: 0x%x\n", when, val);
237 printf ("CFA: [%x] offset: %s at cfa-0x%x\n", when, mono_arch_regname (reg), -val);
243 #define MONO_INIT_VARINFO(vi,id) do { \
244 (vi)->range.first_use.pos.bid = 0xffff; \
250 * mono_unlink_bblock:
252 * Unlink two basic blocks.
255 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
261 for (i = 0; i < from->out_count; ++i) {
262 if (to == from->out_bb [i]) {
269 for (i = 0; i < from->out_count; ++i) {
270 if (from->out_bb [i] != to)
271 from->out_bb [pos ++] = from->out_bb [i];
273 g_assert (pos == from->out_count - 1);
278 for (i = 0; i < to->in_count; ++i) {
279 if (from == to->in_bb [i]) {
286 for (i = 0; i < to->in_count; ++i) {
287 if (to->in_bb [i] != from)
288 to->in_bb [pos ++] = to->in_bb [i];
290 g_assert (pos == to->in_count - 1);
296 * mono_bblocks_linked:
298 * Return whenever BB1 and BB2 are linked in the CFG.
301 mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
305 for (i = 0; i < bb1->out_count; ++i) {
306 if (bb1->out_bb [i] == bb2)
314 mono_find_block_region_notry (MonoCompile *cfg, int offset)
316 MonoMethodHeader *header = cfg->header;
317 MonoExceptionClause *clause;
320 for (i = 0; i < header->num_clauses; ++i) {
321 clause = &header->clauses [i];
322 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
323 (offset < (clause->handler_offset)))
324 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
326 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
327 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
328 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
329 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
330 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
332 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
340 * mono_get_block_region_notry:
342 * Return the region corresponding to REGION, ignoring try clauses nested inside
346 mono_get_block_region_notry (MonoCompile *cfg, int region)
348 if ((region & (0xf << 4)) == MONO_REGION_TRY) {
349 MonoMethodHeader *header = cfg->header;
352 * This can happen if a try clause is nested inside a finally clause.
354 int clause_index = (region >> 8) - 1;
355 g_assert (clause_index >= 0 && clause_index < header->num_clauses);
357 region = mono_find_block_region_notry (cfg, header->clauses [clause_index].try_offset);
364 mono_find_spvar_for_region (MonoCompile *cfg, int region)
366 region = mono_get_block_region_notry (cfg, region);
368 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
372 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
376 array [*dfn] = start;
377 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
378 for (i = 0; i < start->out_count; ++i) {
379 if (start->out_bb [i]->dfn)
382 start->out_bb [i]->dfn = *dfn;
383 start->out_bb [i]->df_parent = start;
384 array [*dfn] = start->out_bb [i];
385 df_visit (start->out_bb [i], dfn, array);
390 mono_reverse_branch_op (guint32 opcode)
392 static const int reverse_map [] = {
393 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
394 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
396 static const int reverse_fmap [] = {
397 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
398 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
400 static const int reverse_lmap [] = {
401 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
402 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
404 static const int reverse_imap [] = {
405 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
406 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
409 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
410 opcode = reverse_map [opcode - CEE_BEQ];
411 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
412 opcode = reverse_fmap [opcode - OP_FBEQ];
413 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
414 opcode = reverse_lmap [opcode - OP_LBEQ];
415 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
416 opcode = reverse_imap [opcode - OP_IBEQ];
418 g_assert_not_reached ();
424 mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
426 type = mini_get_underlying_type (cfg, type);
429 switch (type->type) {
432 case MONO_TYPE_BOOLEAN:
433 return OP_STOREI1_MEMBASE_REG;
437 return OP_STOREI2_MEMBASE_REG;
440 return OP_STOREI4_MEMBASE_REG;
444 case MONO_TYPE_FNPTR:
445 return OP_STORE_MEMBASE_REG;
446 case MONO_TYPE_CLASS:
447 case MONO_TYPE_STRING:
448 case MONO_TYPE_OBJECT:
449 case MONO_TYPE_SZARRAY:
450 case MONO_TYPE_ARRAY:
451 return OP_STORE_MEMBASE_REG;
454 return OP_STOREI8_MEMBASE_REG;
456 return OP_STORER4_MEMBASE_REG;
458 return OP_STORER8_MEMBASE_REG;
459 case MONO_TYPE_VALUETYPE:
460 if (type->data.klass->enumtype) {
461 type = mono_class_enum_basetype (type->data.klass);
464 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
465 return OP_STOREX_MEMBASE;
466 return OP_STOREV_MEMBASE;
467 case MONO_TYPE_TYPEDBYREF:
468 return OP_STOREV_MEMBASE;
469 case MONO_TYPE_GENERICINST:
470 type = &type->data.generic_class->container_class->byval_arg;
474 g_assert (mini_type_var_is_vt (cfg, type));
475 return OP_STOREV_MEMBASE;
477 g_error ("unknown type 0x%02x in type_to_store_membase", type->type);
483 mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
485 type = mini_get_underlying_type (cfg, type);
487 switch (type->type) {
489 return OP_LOADI1_MEMBASE;
491 case MONO_TYPE_BOOLEAN:
492 return OP_LOADU1_MEMBASE;
494 return OP_LOADI2_MEMBASE;
497 return OP_LOADU2_MEMBASE;
499 return OP_LOADI4_MEMBASE;
501 return OP_LOADU4_MEMBASE;
505 case MONO_TYPE_FNPTR:
506 return OP_LOAD_MEMBASE;
507 case MONO_TYPE_CLASS:
508 case MONO_TYPE_STRING:
509 case MONO_TYPE_OBJECT:
510 case MONO_TYPE_SZARRAY:
511 case MONO_TYPE_ARRAY:
512 return OP_LOAD_MEMBASE;
515 return OP_LOADI8_MEMBASE;
517 return OP_LOADR4_MEMBASE;
519 return OP_LOADR8_MEMBASE;
520 case MONO_TYPE_VALUETYPE:
521 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
522 return OP_LOADX_MEMBASE;
523 case MONO_TYPE_TYPEDBYREF:
524 return OP_LOADV_MEMBASE;
525 case MONO_TYPE_GENERICINST:
526 if (mono_type_generic_inst_is_valuetype (type))
527 return OP_LOADV_MEMBASE;
529 return OP_LOAD_MEMBASE;
533 g_assert (cfg->generic_sharing_context);
534 g_assert (mini_type_var_is_vt (cfg, type));
535 return OP_LOADV_MEMBASE;
537 g_error ("unknown type 0x%02x in type_to_load_membase", type->type);
543 mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
545 type = mini_get_underlying_type (cfg, type);
546 if (cfg->generic_sharing_context && !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
547 g_assert (mini_type_var_is_vt (cfg, type));
550 return mono_type_to_ldind (type);
556 mini_type_to_stind (MonoCompile* cfg, MonoType *type)
558 type = mini_get_underlying_type (cfg, type);
559 if (cfg->generic_sharing_context && !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
560 g_assert (mini_type_var_is_vt (cfg, type));
563 return mono_type_to_stind (type);
567 mono_op_imm_to_op (int opcode)
571 #if SIZEOF_REGISTER == 4
587 #if SIZEOF_REGISTER == 4
593 #if SIZEOF_REGISTER == 4
599 #if SIZEOF_REGISTER == 4
639 #if SIZEOF_REGISTER == 4
645 #if SIZEOF_REGISTER == 4
664 case OP_ICOMPARE_IMM:
666 case OP_LOCALLOC_IMM:
669 printf ("%s\n", mono_inst_name (opcode));
670 g_assert_not_reached ();
676 * mono_decompose_op_imm:
678 * Replace the OP_.._IMM INS with its non IMM variant.
681 mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
685 MONO_INST_NEW (cfg, temp, OP_ICONST);
686 temp->inst_c0 = ins->inst_imm;
687 temp->dreg = mono_alloc_ireg (cfg);
688 mono_bblock_insert_before_ins (bb, ins, temp);
689 ins->opcode = mono_op_imm_to_op (ins->opcode);
690 if (ins->opcode == OP_LOCALLOC)
691 ins->sreg1 = temp->dreg;
693 ins->sreg2 = temp->dreg;
695 bb->max_vreg = MAX (bb->max_vreg, cfg->next_vreg);
701 set_vreg_to_inst (MonoCompile *cfg, int vreg, MonoInst *inst)
703 if (vreg >= cfg->vreg_to_inst_len) {
704 MonoInst **tmp = cfg->vreg_to_inst;
705 int size = cfg->vreg_to_inst_len;
707 while (vreg >= cfg->vreg_to_inst_len)
708 cfg->vreg_to_inst_len = cfg->vreg_to_inst_len ? cfg->vreg_to_inst_len * 2 : 32;
709 cfg->vreg_to_inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * cfg->vreg_to_inst_len);
711 memcpy (cfg->vreg_to_inst, tmp, size * sizeof (MonoInst*));
713 cfg->vreg_to_inst [vreg] = inst;
716 #define mono_type_is_long(type) (!(type)->byref && ((mono_type_get_underlying_type (type)->type == MONO_TYPE_I8) || (mono_type_get_underlying_type (type)->type == MONO_TYPE_U8)))
717 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
722 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
730 mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode, int vreg)
733 int num = cfg->num_varinfo;
736 type = mini_get_underlying_type (cfg, type);
738 if ((num + 1) >= cfg->varinfo_count) {
739 int orig_count = cfg->varinfo_count;
740 cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 32;
741 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
742 cfg->vars = (MonoMethodVar *)g_realloc (cfg->vars, sizeof (MonoMethodVar) * cfg->varinfo_count);
743 memset (&cfg->vars [orig_count], 0, (cfg->varinfo_count - orig_count) * sizeof (MonoMethodVar));
746 cfg->stat_allocate_var++;
748 MONO_INST_NEW (cfg, inst, opcode);
750 inst->inst_vtype = type;
751 inst->klass = mono_class_from_mono_type (type);
752 type_to_eval_stack_type (cfg, type, inst);
753 /* if set to 1 the variable is native */
754 inst->backend.is_pinvoke = 0;
757 if (inst->klass->exception_type)
758 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
760 if (cfg->compute_gc_maps) {
762 mono_mark_vreg_as_mp (cfg, vreg);
764 if ((MONO_TYPE_ISSTRUCT (type) && inst->klass->has_references) || mini_type_is_reference (cfg, type)) {
765 inst->flags |= MONO_INST_GC_TRACK;
766 mono_mark_vreg_as_ref (cfg, vreg);
771 cfg->varinfo [num] = inst;
773 MONO_INIT_VARINFO (&cfg->vars [num], num);
774 MONO_VARINFO (cfg, num)->vreg = vreg;
777 set_vreg_to_inst (cfg, vreg, inst);
779 #if SIZEOF_REGISTER == 4
780 if (mono_arch_is_soft_float ()) {
781 regpair = mono_type_is_long (type) || mono_type_is_float (type);
783 regpair = mono_type_is_long (type);
793 * These two cannot be allocated using create_var_for_vreg since that would
794 * put it into the cfg->varinfo array, confusing many parts of the JIT.
798 * Set flags to VOLATILE so SSA skips it.
801 if (cfg->verbose_level >= 4) {
802 printf (" Create LVAR R%d (R%d, R%d)\n", inst->dreg, inst->dreg + 1, inst->dreg + 2);
805 if (mono_arch_is_soft_float () && cfg->opt & MONO_OPT_SSA) {
806 if (mono_type_is_float (type))
807 inst->flags = MONO_INST_VOLATILE;
810 /* Allocate a dummy MonoInst for the first vreg */
811 MONO_INST_NEW (cfg, tree, OP_LOCAL);
812 tree->dreg = inst->dreg + 1;
813 if (cfg->opt & MONO_OPT_SSA)
814 tree->flags = MONO_INST_VOLATILE;
816 tree->type = STACK_I4;
817 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
818 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
820 set_vreg_to_inst (cfg, inst->dreg + 1, tree);
822 /* Allocate a dummy MonoInst for the second vreg */
823 MONO_INST_NEW (cfg, tree, OP_LOCAL);
824 tree->dreg = inst->dreg + 2;
825 if (cfg->opt & MONO_OPT_SSA)
826 tree->flags = MONO_INST_VOLATILE;
828 tree->type = STACK_I4;
829 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
830 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
832 set_vreg_to_inst (cfg, inst->dreg + 2, tree);
836 if (cfg->verbose_level > 2)
837 g_print ("created temp %d (R%d) of type %s\n", num, vreg, mono_type_get_name (type));
842 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
845 type = mini_get_underlying_type (cfg, type);
847 if (mono_type_is_long (type))
848 dreg = mono_alloc_dreg (cfg, STACK_I8);
849 else if (mono_arch_is_soft_float () && mono_type_is_float (type))
850 dreg = mono_alloc_dreg (cfg, STACK_R8);
852 /* All the others are unified */
853 dreg = mono_alloc_preg (cfg);
855 return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
859 * Transform a MonoInst into a load from the variable of index var_index.
862 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index)
864 memset (dest, 0, sizeof (MonoInst));
865 dest->inst_i0 = cfg->varinfo [var_index];
866 dest->opcode = mini_type_to_ldind (cfg, dest->inst_i0->inst_vtype);
867 type_to_eval_stack_type (cfg, dest->inst_i0->inst_vtype, dest);
868 dest->klass = dest->inst_i0->klass;
872 mini_get_int_to_float_spill_area (MonoCompile *cfg)
875 if (!cfg->iconv_raw_var) {
876 cfg->iconv_raw_var = mono_compile_create_var (cfg, &mono_defaults.int32_class->byval_arg, OP_LOCAL);
877 cfg->iconv_raw_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
879 return cfg->iconv_raw_var;
888 mono_mark_vreg_as_ref (MonoCompile *cfg, int vreg)
890 if (vreg >= cfg->vreg_is_ref_len) {
891 gboolean *tmp = cfg->vreg_is_ref;
892 int size = cfg->vreg_is_ref_len;
894 while (vreg >= cfg->vreg_is_ref_len)
895 cfg->vreg_is_ref_len = cfg->vreg_is_ref_len ? cfg->vreg_is_ref_len * 2 : 32;
896 cfg->vreg_is_ref = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * cfg->vreg_is_ref_len);
898 memcpy (cfg->vreg_is_ref, tmp, size * sizeof (gboolean));
900 cfg->vreg_is_ref [vreg] = TRUE;
904 mono_mark_vreg_as_mp (MonoCompile *cfg, int vreg)
906 if (vreg >= cfg->vreg_is_mp_len) {
907 gboolean *tmp = cfg->vreg_is_mp;
908 int size = cfg->vreg_is_mp_len;
910 while (vreg >= cfg->vreg_is_mp_len)
911 cfg->vreg_is_mp_len = cfg->vreg_is_mp_len ? cfg->vreg_is_mp_len * 2 : 32;
912 cfg->vreg_is_mp = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * cfg->vreg_is_mp_len);
914 memcpy (cfg->vreg_is_mp, tmp, size * sizeof (gboolean));
916 cfg->vreg_is_mp [vreg] = TRUE;
920 type_from_stack_type (MonoInst *ins) {
922 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
923 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
924 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
925 case STACK_R8: return &mono_defaults.double_class->byval_arg;
928 * this if used to be commented without any specific reason, but
929 * it breaks #80235 when commented
932 return &ins->klass->this_arg;
934 return &mono_defaults.object_class->this_arg;
936 /* ins->klass may not be set for ldnull.
937 * Also, if we have a boxed valuetype, we want an object lass,
938 * not the valuetype class
940 if (ins->klass && !ins->klass->valuetype)
941 return &ins->klass->byval_arg;
942 return &mono_defaults.object_class->byval_arg;
943 case STACK_VTYPE: return &ins->klass->byval_arg;
945 g_error ("stack type %d to montype not handled\n", ins->type);
951 mono_type_from_stack_type (MonoInst *ins) {
952 return type_from_stack_type (ins);
956 * mono_add_ins_to_end:
958 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
961 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
966 MONO_ADD_INS (bb, inst);
970 switch (bb->last_ins->opcode) {
984 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
987 if (MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
988 /* Need to insert the ins before the compare */
989 if (bb->code == bb->last_ins) {
990 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
994 if (bb->code->next == bb->last_ins) {
995 /* Only two instructions */
996 opcode = bb->code->opcode;
998 if ((opcode == OP_COMPARE) || (opcode == OP_COMPARE_IMM) || (opcode == OP_ICOMPARE) || (opcode == OP_ICOMPARE_IMM) || (opcode == OP_FCOMPARE) || (opcode == OP_LCOMPARE) || (opcode == OP_LCOMPARE_IMM) || (opcode == OP_RCOMPARE)) {
1000 mono_bblock_insert_before_ins (bb, bb->code, inst);
1002 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
1005 opcode = bb->last_ins->prev->opcode;
1007 if ((opcode == OP_COMPARE) || (opcode == OP_COMPARE_IMM) || (opcode == OP_ICOMPARE) || (opcode == OP_ICOMPARE_IMM) || (opcode == OP_FCOMPARE) || (opcode == OP_LCOMPARE) || (opcode == OP_LCOMPARE_IMM) || (opcode == OP_RCOMPARE)) {
1009 mono_bblock_insert_before_ins (bb, bb->last_ins->prev, inst);
1011 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
1016 MONO_ADD_INS (bb, inst);
1022 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1024 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1025 MonoJumpInfoBBTable *table;
1027 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
1029 table->table_size = num_blocks;
1031 ji->ip.label = label;
1032 ji->type = MONO_PATCH_INFO_SWITCH;
1033 ji->data.table = table;
1034 ji->next = cfg->patch_info;
1035 cfg->patch_info = ji;
1038 static MonoMethodSignature *
1039 mono_get_array_new_va_signature (int arity)
1041 static GHashTable *sighash;
1042 MonoMethodSignature *res;
1047 sighash = g_hash_table_new (NULL, NULL);
1049 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
1054 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1057 if (ARCH_VARARG_ICALLS)
1058 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1059 res->call_convention = MONO_CALL_VARARG;
1062 res->call_convention = MONO_CALL_C;
1065 res->params [0] = &mono_defaults.int_class->byval_arg;
1066 for (i = 0; i < arity; i++)
1067 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
1069 res->ret = &mono_defaults.object_class->byval_arg;
1071 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
1078 mono_get_array_new_va_icall (int rank)
1080 MonoMethodSignature *esig;
1081 char icall_name [256];
1083 MonoJitICallInfo *info;
1085 /* Need to register the icall so it gets an icall wrapper */
1086 sprintf (icall_name, "ves_array_new_va_%d", rank);
1089 info = mono_find_jit_icall_by_name (icall_name);
1091 esig = mono_get_array_new_va_signature (rank);
1092 name = g_strdup (icall_name);
1093 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
1101 mini_class_is_system_array (MonoClass *klass)
1103 if (klass->parent == mono_defaults.array_class)
1110 mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method)
1112 MonoAssembly *assembly = method->klass->image->assembly;
1113 if (method->wrapper_type != MONO_WRAPPER_NONE && method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
1115 if (assembly->in_gac || assembly->image == mono_defaults.corlib)
1117 if (mono_security_enabled ())
1119 return mono_assembly_has_skip_verification (assembly);
1123 * mini_method_verify:
1125 * Verify the method using the new verfier.
1127 * Returns true if the method is invalid.
1130 mini_method_verify (MonoCompile *cfg, MonoMethod *method, gboolean fail_compile)
1133 gboolean is_fulltrust;
1134 MonoLoaderError *error;
1136 if (method->verification_success)
1139 if (!mono_verifier_is_enabled_for_method (method))
1142 /*skip verification implies the assembly must be */
1143 is_fulltrust = mono_verifier_is_method_full_trust (method) || mini_assembly_can_skip_verification (cfg->domain, method);
1145 res = mono_method_verify_with_current_settings (method, cfg->skip_visibility, is_fulltrust);
1147 if ((error = mono_loader_get_last_error ())) {
1149 cfg->exception_type = error->exception_type;
1151 mono_loader_clear_error ();
1153 mono_free_verify_list (res);
1158 for (tmp = res; tmp; tmp = tmp->next) {
1159 MonoVerifyInfoExtended *info = (MonoVerifyInfoExtended *)tmp->data;
1160 if (info->info.status == MONO_VERIFY_ERROR) {
1162 char *method_name = mono_method_full_name (method, TRUE);
1163 cfg->exception_type = info->exception_type;
1164 cfg->exception_message = g_strdup_printf ("Error verifying %s: %s", method_name, info->info.message);
1165 g_free (method_name);
1167 mono_free_verify_list (res);
1170 if (info->info.status == MONO_VERIFY_NOT_VERIFIABLE && (!is_fulltrust || info->exception_type == MONO_EXCEPTION_METHOD_ACCESS || info->exception_type == MONO_EXCEPTION_FIELD_ACCESS)) {
1172 char *method_name = mono_method_full_name (method, TRUE);
1173 cfg->exception_type = info->exception_type;
1174 cfg->exception_message = g_strdup_printf ("Error verifying %s: %s", method_name, info->info.message);
1175 g_free (method_name);
1177 mono_free_verify_list (res);
1181 mono_free_verify_list (res);
1183 method->verification_success = 1;
1187 /*Returns true if something went wrong*/
1189 mono_compile_is_broken (MonoCompile *cfg, MonoMethod *method, gboolean fail_compile)
1191 MonoMethod *method_definition = method;
1192 gboolean dont_verify = method->klass->image->assembly->corlib_internal;
1194 while (method_definition->is_inflated) {
1195 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
1196 method_definition = imethod->declaring;
1199 return !dont_verify && mini_method_verify (cfg, method_definition, fail_compile);
1203 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
1205 if (!domain_jit_info (domain)->dynamic_code_hash)
1206 domain_jit_info (domain)->dynamic_code_hash = g_hash_table_new (NULL, NULL);
1207 g_hash_table_insert (domain_jit_info (domain)->dynamic_code_hash, method, ji);
1210 static MonoJitDynamicMethodInfo*
1211 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
1213 MonoJitDynamicMethodInfo *res;
1215 if (domain_jit_info (domain)->dynamic_code_hash)
1216 res = g_hash_table_lookup (domain_jit_info (domain)->dynamic_code_hash, method);
1224 GList *active, *inactive;
1229 compare_by_interval_start_pos_func (gconstpointer a, gconstpointer b)
1231 MonoMethodVar *v1 = (MonoMethodVar*)a;
1232 MonoMethodVar *v2 = (MonoMethodVar*)b;
1236 else if (v1->interval->range && v2->interval->range)
1237 return v1->interval->range->from - v2->interval->range->from;
1238 else if (v1->interval->range)
1247 #define LSCAN_DEBUG(a) do { a; } while (0)
1249 #define LSCAN_DEBUG(a)
1253 mono_allocate_stack_slots2 (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
1255 int i, slot, offset, size;
1260 GList *vars = NULL, *l, *unhandled;
1261 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
1264 gboolean reuse_slot;
1266 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg->method, TRUE)));
1268 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
1269 vtype_stack_slots = NULL;
1272 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
1273 for (i = 0; i < cfg->num_varinfo; ++i)
1276 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1277 inst = cfg->varinfo [i];
1278 vmv = MONO_VARINFO (cfg, i);
1280 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
1283 vars = g_list_prepend (vars, vmv);
1286 vars = g_list_sort (g_list_copy (vars), compare_by_interval_start_pos_func);
1291 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1292 MonoMethodVar *current = unhandled->data;
1294 if (current->interval->range) {
1295 g_assert (current->interval->range->from >= i);
1296 i = current->interval->range->from;
1303 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1304 MonoMethodVar *current = unhandled->data;
1307 inst = cfg->varinfo [vmv->idx];
1309 t = mono_type_get_underlying_type (inst->inst_vtype);
1310 if (cfg->gsharedvt && mini_is_gsharedvt_variable_type (cfg, t))
1313 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1314 * pinvoke wrappers when they call functions returning structures */
1315 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (t) && t->type != MONO_TYPE_TYPEDBYREF) {
1316 size = mono_class_native_size (mono_class_from_mono_type (t), &align);
1321 size = mini_type_stack_size (NULL, t, &ialign);
1324 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (t)))
1329 if (cfg->disable_reuse_stack_slots)
1333 case MONO_TYPE_GENERICINST:
1334 if (!mono_type_generic_inst_is_valuetype (t)) {
1335 slot_info = &scalar_stack_slots [t->type];
1339 case MONO_TYPE_VALUETYPE:
1340 if (!vtype_stack_slots)
1341 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
1342 for (i = 0; i < nvtypes; ++i)
1343 if (t->data.klass == vtype_stack_slots [i].vtype)
1346 slot_info = &vtype_stack_slots [i];
1348 g_assert (nvtypes < 256);
1349 vtype_stack_slots [nvtypes].vtype = t->data.klass;
1350 slot_info = &vtype_stack_slots [nvtypes];
1353 if (cfg->disable_reuse_ref_stack_slots)
1360 #if SIZEOF_VOID_P == 4
1365 if (cfg->disable_ref_noref_stack_slot_share) {
1366 slot_info = &scalar_stack_slots [MONO_TYPE_I];
1371 case MONO_TYPE_CLASS:
1372 case MONO_TYPE_OBJECT:
1373 case MONO_TYPE_ARRAY:
1374 case MONO_TYPE_SZARRAY:
1375 case MONO_TYPE_STRING:
1376 /* Share non-float stack slots of the same size */
1377 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
1378 if (cfg->disable_reuse_ref_stack_slots)
1383 slot_info = &scalar_stack_slots [t->type];
1387 if (cfg->comp_done & MONO_COMP_LIVENESS) {
1391 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1393 if (!current->interval->range) {
1394 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1398 inst->flags |= MONO_INST_IS_DEAD;
1403 pos = current->interval->range->from;
1405 LSCAN_DEBUG (printf ("process R%d ", inst->dreg));
1406 if (current->interval->range)
1407 LSCAN_DEBUG (mono_linterval_print (current->interval));
1408 LSCAN_DEBUG (printf ("\n"));
1410 /* Check for intervals in active which expired or inactive */
1412 /* FIXME: Optimize this */
1415 for (l = slot_info->active; l != NULL; l = l->next) {
1416 MonoMethodVar *v = (MonoMethodVar*)l->data;
1418 if (v->interval->last_range->to < pos) {
1419 slot_info->active = g_list_delete_link (slot_info->active, l);
1420 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1421 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
1425 else if (!mono_linterval_covers (v->interval, pos)) {
1426 slot_info->inactive = g_list_append (slot_info->inactive, v);
1427 slot_info->active = g_list_delete_link (slot_info->active, l);
1428 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg->varinfo [v->idx]->dreg));
1435 /* Check for intervals in inactive which expired or active */
1437 /* FIXME: Optimize this */
1440 for (l = slot_info->inactive; l != NULL; l = l->next) {
1441 MonoMethodVar *v = (MonoMethodVar*)l->data;
1443 if (v->interval->last_range->to < pos) {
1444 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
1445 // FIXME: Enabling this seems to cause impossible to debug crashes
1446 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1447 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
1451 else if (mono_linterval_covers (v->interval, pos)) {
1452 slot_info->active = g_list_append (slot_info->active, v);
1453 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
1454 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg->varinfo [v->idx]->dreg));
1462 * This also handles the case when the variable is used in an
1463 * exception region, as liveness info is not computed there.
1466 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1469 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
1470 if (slot_info->slots) {
1471 slot = GPOINTER_TO_INT (slot_info->slots->data);
1473 slot_info->slots = slot_info->slots->next;
1476 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1478 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
1484 static int count = 0;
1487 if (count == atoi (g_getenv ("COUNT3")))
1488 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1489 if (count > atoi (g_getenv ("COUNT3")))
1492 mono_print_ins (inst);
1497 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst->dreg, mono_type_full_name (t), slot));
1499 if (inst->flags & MONO_INST_LMF) {
1500 size = sizeof (MonoLMF);
1501 align = sizeof (mgreg_t);
1508 if (slot == 0xffffff) {
1510 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1511 * efficient copying (and to work around the fact that OP_MEMCPY
1512 * and OP_MEMSET ignores alignment).
1514 if (MONO_TYPE_ISSTRUCT (t)) {
1515 align = MAX (align, sizeof (gpointer));
1516 align = MAX (align, mono_class_min_align (mono_class_from_mono_type (t)));
1521 offset += align - 1;
1522 offset &= ~(align - 1);
1526 offset += align - 1;
1527 offset &= ~(align - 1);
1532 if (*stack_align == 0)
1533 *stack_align = align;
1536 offsets [vmv->idx] = slot;
1539 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
1540 if (scalar_stack_slots [i].active)
1541 g_list_free (scalar_stack_slots [i].active);
1543 for (i = 0; i < nvtypes; ++i) {
1544 if (vtype_stack_slots [i].active)
1545 g_list_free (vtype_stack_slots [i].active);
1548 cfg->stat_locals_stack_size += offset;
1550 *stack_size = offset;
1555 * mono_allocate_stack_slots:
1557 * Allocate stack slots for all non register allocated variables using a
1558 * linear scan algorithm.
1559 * Returns: an array of stack offsets.
1560 * STACK_SIZE is set to the amount of stack space needed.
1561 * STACK_ALIGN is set to the alignment needed by the locals area.
1564 mono_allocate_stack_slots (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
1566 int i, slot, offset, size;
1571 GList *vars = NULL, *l;
1572 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
1575 gboolean reuse_slot;
1577 if ((cfg->num_varinfo > 0) && MONO_VARINFO (cfg, 0)->interval)
1578 return mono_allocate_stack_slots2 (cfg, backward, stack_size, stack_align);
1580 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
1581 vtype_stack_slots = NULL;
1584 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
1585 for (i = 0; i < cfg->num_varinfo; ++i)
1588 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1589 inst = cfg->varinfo [i];
1590 vmv = MONO_VARINFO (cfg, i);
1592 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
1595 vars = g_list_prepend (vars, vmv);
1598 vars = mono_varlist_sort (cfg, vars, 0);
1600 *stack_align = sizeof(mgreg_t);
1601 for (l = vars; l; l = l->next) {
1603 inst = cfg->varinfo [vmv->idx];
1605 t = mono_type_get_underlying_type (inst->inst_vtype);
1606 if (cfg->gsharedvt && mini_is_gsharedvt_variable_type (cfg, t))
1609 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1610 * pinvoke wrappers when they call functions returning structures */
1611 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (t) && t->type != MONO_TYPE_TYPEDBYREF) {
1612 size = mono_class_native_size (mono_class_from_mono_type (t), &align);
1616 size = mini_type_stack_size (NULL, t, &ialign);
1619 if (mono_class_from_mono_type (t)->exception_type)
1620 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
1622 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (t)))
1627 if (cfg->disable_reuse_stack_slots)
1630 t = mini_get_underlying_type (cfg, t);
1632 case MONO_TYPE_GENERICINST:
1633 if (!mono_type_generic_inst_is_valuetype (t)) {
1634 slot_info = &scalar_stack_slots [t->type];
1638 case MONO_TYPE_VALUETYPE:
1639 if (!vtype_stack_slots)
1640 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
1641 for (i = 0; i < nvtypes; ++i)
1642 if (t->data.klass == vtype_stack_slots [i].vtype)
1645 slot_info = &vtype_stack_slots [i];
1647 g_assert (nvtypes < 256);
1648 vtype_stack_slots [nvtypes].vtype = t->data.klass;
1649 slot_info = &vtype_stack_slots [nvtypes];
1652 if (cfg->disable_reuse_ref_stack_slots)
1659 #if SIZEOF_VOID_P == 4
1664 if (cfg->disable_ref_noref_stack_slot_share) {
1665 slot_info = &scalar_stack_slots [MONO_TYPE_I];
1670 case MONO_TYPE_CLASS:
1671 case MONO_TYPE_OBJECT:
1672 case MONO_TYPE_ARRAY:
1673 case MONO_TYPE_SZARRAY:
1674 case MONO_TYPE_STRING:
1675 /* Share non-float stack slots of the same size */
1676 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
1677 if (cfg->disable_reuse_ref_stack_slots)
1681 case MONO_TYPE_MVAR:
1682 slot_info = &scalar_stack_slots [t->type];
1685 slot_info = &scalar_stack_slots [t->type];
1690 if (cfg->comp_done & MONO_COMP_LIVENESS) {
1691 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1693 /* expire old intervals in active */
1694 while (slot_info->active) {
1695 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
1697 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
1700 //printf ("EXPIR %2d %08x %08x C%d R%d\n", amv->idx, amv->range.first_use.abs_pos, amv->range.last_use.abs_pos, amv->spill_costs, amv->reg);
1702 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
1703 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
1707 * This also handles the case when the variable is used in an
1708 * exception region, as liveness info is not computed there.
1711 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1714 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
1715 if (slot_info->slots) {
1716 slot = GPOINTER_TO_INT (slot_info->slots->data);
1718 slot_info->slots = slot_info->slots->next;
1721 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
1726 static int count = 0;
1730 if (count == atoi (g_getenv ("COUNT")))
1731 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1732 if (count > atoi (g_getenv ("COUNT")))
1735 mono_print_ins (inst);
1740 if (inst->flags & MONO_INST_LMF) {
1742 * This variable represents a MonoLMF structure, which has no corresponding
1743 * CLR type, so hard-code its size/alignment.
1745 size = sizeof (MonoLMF);
1746 align = sizeof (mgreg_t);
1753 if (slot == 0xffffff) {
1755 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1756 * efficient copying (and to work around the fact that OP_MEMCPY
1757 * and OP_MEMSET ignores alignment).
1759 if (MONO_TYPE_ISSTRUCT (t)) {
1760 align = MAX (align, sizeof (gpointer));
1761 align = MAX (align, mono_class_min_align (mono_class_from_mono_type (t)));
1763 * Align the size too so the code generated for passing vtypes in
1764 * registers doesn't overwrite random locals.
1766 size = (size + (align - 1)) & ~(align -1);
1771 offset += align - 1;
1772 offset &= ~(align - 1);
1776 offset += align - 1;
1777 offset &= ~(align - 1);
1782 *stack_align = MAX (*stack_align, align);
1785 offsets [vmv->idx] = slot;
1788 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
1789 if (scalar_stack_slots [i].active)
1790 g_list_free (scalar_stack_slots [i].active);
1792 for (i = 0; i < nvtypes; ++i) {
1793 if (vtype_stack_slots [i].active)
1794 g_list_free (vtype_stack_slots [i].active);
1797 cfg->stat_locals_stack_size += offset;
1799 *stack_size = offset;
1806 mono_allocate_stack_slots (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
1808 g_assert_not_reached ();
1812 #endif /* DISABLE_JIT */
1814 #define EMUL_HIT_SHIFT 3
1815 #define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
1816 /* small hit bitmap cache */
1817 static mono_byte emul_opcode_hit_cache [(OP_LAST>>EMUL_HIT_SHIFT) + 1] = {0};
1818 static short emul_opcode_num = 0;
1819 static short emul_opcode_alloced = 0;
1820 static short *emul_opcode_opcodes;
1821 static MonoJitICallInfo **emul_opcode_map;
1824 mono_find_jit_opcode_emulation (int opcode)
1826 g_assert (opcode >= 0 && opcode <= OP_LAST);
1827 if (emul_opcode_hit_cache [opcode >> (EMUL_HIT_SHIFT + 3)] & (1 << (opcode & EMUL_HIT_MASK))) {
1829 for (i = 0; i < emul_opcode_num; ++i) {
1830 if (emul_opcode_opcodes [i] == opcode)
1831 return emul_opcode_map [i];
1838 mini_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, const char *symbol, gboolean no_throw)
1840 MonoJitICallInfo *info;
1841 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
1843 g_assert (!sig->hasthis);
1844 g_assert (sig->param_count < 3);
1846 /* Opcode emulation functions are assumed to don't call mono_raise_exception () */
1847 info = mono_register_jit_icall_full (func, name, sig, no_throw, TRUE, symbol);
1849 if (emul_opcode_num >= emul_opcode_alloced) {
1850 int incr = emul_opcode_alloced? emul_opcode_alloced/2: 16;
1851 emul_opcode_alloced += incr;
1852 emul_opcode_map = g_realloc (emul_opcode_map, sizeof (emul_opcode_map [0]) * emul_opcode_alloced);
1853 emul_opcode_opcodes = g_realloc (emul_opcode_opcodes, sizeof (emul_opcode_opcodes [0]) * emul_opcode_alloced);
1855 emul_opcode_map [emul_opcode_num] = info;
1856 emul_opcode_opcodes [emul_opcode_num] = opcode;
1858 emul_opcode_hit_cache [opcode >> (EMUL_HIT_SHIFT + 3)] |= (1 << (opcode & EMUL_HIT_MASK));
1862 * For JIT icalls implemented in C.
1863 * NAME should be the same as the name of the C function whose address is FUNC.
1864 * If SAVE is TRUE, no wrapper is generated. This is for perf critical icalls which
1865 * can't throw exceptions.
1868 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
1870 MonoMethodSignature *sig;
1873 sig = mono_create_icall_signature (sigstr);
1877 mono_register_jit_icall_full (func, name, sig, save, FALSE, save ? name : NULL);
1880 /* Register a jit icall which doesn't throw exceptions through mono_raise_exception () */
1882 register_icall_noraise (gpointer func, const char *name, const char *sigstr)
1884 MonoMethodSignature *sig;
1887 sig = mono_create_icall_signature (sigstr);
1891 mono_register_jit_icall_full (func, name, sig, TRUE, TRUE, name);
1895 register_dyn_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
1897 MonoMethodSignature *sig;
1900 sig = mono_create_icall_signature (sigstr);
1904 mono_register_jit_icall (func, name, sig, save);
1908 print_dfn (MonoCompile *cfg) {
1915 char *method_name = mono_method_full_name (cfg->method, TRUE);
1916 g_print ("IR code for method %s\n", method_name);
1917 g_free (method_name);
1920 for (i = 0; i < cfg->num_bblocks; ++i) {
1921 bb = cfg->bblocks [i];
1922 /*if (bb->cil_code) {
1923 char* code1, *code2;
1924 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
1925 if (bb->last_ins->cil_code)
1926 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
1928 code2 = g_strdup ("");
1930 code1 [strlen (code1) - 1] = 0;
1931 code = g_strdup_printf ("%s -> %s", code1, code2);
1935 code = g_strdup ("\n");
1936 g_print ("\nBB%d (%d) (len: %d): %s", bb->block_num, i, bb->cil_length, code);
1937 MONO_BB_FOR_EACH_INS (bb, c) {
1938 mono_print_ins_index (-1, c);
1941 g_print ("\tprev:");
1942 for (j = 0; j < bb->in_count; ++j) {
1943 g_print (" BB%d", bb->in_bb [j]->block_num);
1945 g_print ("\t\tsucc:");
1946 for (j = 0; j < bb->out_count; ++j) {
1947 g_print (" BB%d", bb->out_bb [j]->block_num);
1949 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
1952 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
1955 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
1957 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
1965 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
1967 MONO_ADD_INS (bb, inst);
1971 mono_bblock_insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
1975 bb->code = ins_to_insert;
1977 /* Link with next */
1978 ins_to_insert->next = ins;
1980 ins->prev = ins_to_insert;
1982 if (bb->last_ins == NULL)
1983 bb->last_ins = ins_to_insert;
1985 /* Link with next */
1986 ins_to_insert->next = ins->next;
1988 ins->next->prev = ins_to_insert;
1990 /* Link with previous */
1991 ins->next = ins_to_insert;
1992 ins_to_insert->prev = ins;
1994 if (bb->last_ins == ins)
1995 bb->last_ins = ins_to_insert;
2000 mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
2005 ins->prev = ins_to_insert;
2006 bb->code = ins_to_insert;
2007 ins_to_insert->next = ins;
2008 if (bb->last_ins == NULL)
2009 bb->last_ins = ins_to_insert;
2011 /* Link with previous */
2013 ins->prev->next = ins_to_insert;
2014 ins_to_insert->prev = ins->prev;
2016 /* Link with next */
2017 ins->prev = ins_to_insert;
2018 ins_to_insert->next = ins;
2020 if (bb->code == ins)
2021 bb->code = ins_to_insert;
2026 * mono_verify_bblock:
2028 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2031 mono_verify_bblock (MonoBasicBlock *bb)
2033 MonoInst *ins, *prev;
2036 for (ins = bb->code; ins; ins = ins->next) {
2037 g_assert (ins->prev == prev);
2041 g_assert (!bb->last_ins->next);
2047 * Perform consistency checks on the JIT data structures and the IR
2050 mono_verify_cfg (MonoCompile *cfg)
2054 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
2055 mono_verify_bblock (bb);
2059 mono_destroy_compile (MonoCompile *cfg)
2064 mono_metadata_free_mh (cfg->header);
2065 //mono_mempool_stats (cfg->mempool);
2066 mono_free_loop_info (cfg);
2068 mono_regstate_free (cfg->rs);
2070 g_hash_table_destroy (cfg->spvars);
2072 g_hash_table_destroy (cfg->exvars);
2073 for (l = cfg->headers_to_free; l; l = l->next)
2074 mono_metadata_free_mh (l->data);
2075 g_list_free (cfg->ldstr_list);
2076 g_hash_table_destroy (cfg->token_info_hash);
2077 if (cfg->abs_patches)
2078 g_hash_table_destroy (cfg->abs_patches);
2079 mono_mempool_destroy (cfg->mempool);
2081 mono_debug_free_method (cfg);
2083 g_free (cfg->varinfo);
2085 g_free (cfg->exception_message);
2092 mono_create_tls_get_offset (MonoCompile *cfg, int offset)
2096 if (!MONO_ARCH_HAVE_TLS_GET)
2102 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
2103 ins->dreg = mono_alloc_preg (cfg);
2104 ins->inst_offset = offset;
2109 mini_tls_get_supported (MonoCompile *cfg, MonoTlsKey key)
2111 if (!MONO_ARCH_HAVE_TLS_GET)
2114 if (cfg->compile_aot)
2115 return ARCH_HAVE_TLS_GET_REG;
2117 return mini_get_tls_offset (key) != -1;
2121 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
2124 * TLS offsets might be different at AOT time, so load them from a GOT slot and
2125 * use a different opcode.
2127 if (cfg->compile_aot) {
2128 if (MONO_ARCH_HAVE_TLS_GET && ARCH_HAVE_TLS_GET_REG) {
2131 EMIT_NEW_TLS_OFFSETCONST (cfg, c, key);
2132 MONO_INST_NEW (cfg, ins, OP_TLS_GET_REG);
2133 ins->dreg = mono_alloc_preg (cfg);
2134 ins->sreg1 = c->dreg;
2141 return mono_create_tls_get_offset (cfg, mini_get_tls_offset (key));
2145 mono_get_jit_tls_intrinsic (MonoCompile *cfg)
2147 return mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
2151 mono_get_domain_intrinsic (MonoCompile* cfg)
2153 return mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
2157 mono_get_thread_intrinsic (MonoCompile* cfg)
2159 return mono_create_tls_get (cfg, TLS_KEY_THREAD);
2163 mono_get_lmf_intrinsic (MonoCompile* cfg)
2165 return mono_create_tls_get (cfg, TLS_KEY_LMF);
2169 mono_get_lmf_addr_intrinsic (MonoCompile* cfg)
2171 return mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
2174 #endif /* !DISABLE_JIT */
2178 mini_tls_key_supported (MonoTlsKey key)
2180 if (!MONO_ARCH_HAVE_TLS_GET)
2183 return mini_get_tls_offset (key) != -1;
2187 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
2189 MonoJumpInfo *ji = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfo));
2193 ji->data.target = target;
2194 ji->next = cfg->patch_info;
2196 cfg->patch_info = ji;
2200 mono_add_patch_info_rel (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target, int relocation)
2202 MonoJumpInfo *ji = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfo));
2206 ji->relocation = relocation;
2207 ji->data.target = target;
2208 ji->next = cfg->patch_info;
2210 cfg->patch_info = ji;
2214 mono_remove_patch_info (MonoCompile *cfg, int ip)
2216 MonoJumpInfo **ji = &cfg->patch_info;
2219 if ((*ji)->ip.i == ip)
2222 ji = &((*ji)->next);
2227 mono_add_seq_point (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, int native_offset)
2229 ins->inst_offset = native_offset;
2230 g_ptr_array_add (cfg->seq_points, ins);
2232 bb->seq_points = g_slist_prepend_mempool (cfg->mempool, bb->seq_points, ins);
2233 bb->last_seq_point = ins;
2238 mono_add_var_location (MonoCompile *cfg, MonoInst *var, gboolean is_reg, int reg, int offset, int from, int to)
2240 MonoDwarfLocListEntry *entry = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDwarfLocListEntry));
2243 g_assert (offset == 0);
2245 entry->is_reg = is_reg;
2247 entry->offset = offset;
2251 if (var == cfg->args [0])
2252 cfg->this_loclist = g_slist_append_mempool (cfg->mempool, cfg->this_loclist, entry);
2253 else if (var == cfg->rgctx_var)
2254 cfg->rgctx_loclist = g_slist_append_mempool (cfg->mempool, cfg->rgctx_loclist, entry);
2260 mono_compile_create_vars (MonoCompile *cfg)
2262 MonoMethodSignature *sig;
2263 MonoMethodHeader *header;
2266 header = cfg->header;
2268 sig = mono_method_signature (cfg->method);
2270 if (!MONO_TYPE_IS_VOID (sig->ret)) {
2271 cfg->ret = mono_compile_create_var (cfg, sig->ret, OP_ARG);
2272 /* Inhibit optimizations */
2273 cfg->ret->flags |= MONO_INST_VOLATILE;
2275 if (cfg->verbose_level > 2)
2276 g_print ("creating vars\n");
2278 cfg->args = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + sig->hasthis) * sizeof (MonoInst*));
2281 cfg->args [0] = mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
2283 for (i = 0; i < sig->param_count; ++i) {
2284 cfg->args [i + sig->hasthis] = mono_compile_create_var (cfg, sig->params [i], OP_ARG);
2287 if (cfg->verbose_level > 2) {
2289 printf ("\treturn : ");
2290 mono_print_ins (cfg->ret);
2294 printf ("\tthis: ");
2295 mono_print_ins (cfg->args [0]);
2298 for (i = 0; i < sig->param_count; ++i) {
2299 printf ("\targ [%d]: ", i);
2300 mono_print_ins (cfg->args [i + sig->hasthis]);
2304 cfg->locals_start = cfg->num_varinfo;
2305 cfg->locals = mono_mempool_alloc0 (cfg->mempool, header->num_locals * sizeof (MonoInst*));
2307 if (cfg->verbose_level > 2)
2308 g_print ("creating locals\n");
2310 for (i = 0; i < header->num_locals; ++i)
2311 cfg->locals [i] = mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
2313 if (cfg->verbose_level > 2)
2314 g_print ("locals done\n");
2316 mono_arch_create_vars (cfg);
2318 if (cfg->method->save_lmf && cfg->create_lmf_var) {
2319 MonoInst *lmf_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2320 lmf_var->flags |= MONO_INST_VOLATILE;
2321 lmf_var->flags |= MONO_INST_LMF;
2322 cfg->lmf_var = lmf_var;
2327 mono_print_code (MonoCompile *cfg, const char* msg)
2331 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
2332 mono_print_bb (bb, msg);
2336 mono_postprocess_patches (MonoCompile *cfg)
2338 MonoJumpInfo *patch_info;
2341 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2342 switch (patch_info->type) {
2343 case MONO_PATCH_INFO_ABS: {
2344 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
2347 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
2351 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
2352 /* for these array methods we currently register the same function pointer
2353 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
2354 * will return the incorrect one depending on the order they are registered.
2355 * See tests/test-arr.cs
2357 if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
2358 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2359 patch_info->data.name = info->name;
2363 if (patch_info->type == MONO_PATCH_INFO_ABS) {
2364 if (cfg->abs_patches) {
2365 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, patch_info->data.target);
2367 patch_info->type = abs_ji->type;
2368 patch_info->data.target = abs_ji->data.target;
2375 case MONO_PATCH_INFO_SWITCH: {
2377 #if defined(__native_client__) && defined(__native_client_codegen__)
2378 /* This memory will leak. */
2379 /* TODO: can we free this when */
2380 /* making the final jump table? */
2381 table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
2383 if (cfg->method->dynamic) {
2384 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
2386 table = mono_domain_code_reserve (cfg->domain, sizeof (gpointer) * patch_info->data.table->table_size);
2390 for (i = 0; i < patch_info->data.table->table_size; i++) {
2391 /* Might be NULL if the switch is eliminated */
2392 if (patch_info->data.table->table [i]) {
2393 g_assert (patch_info->data.table->table [i]->native_offset);
2394 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
2399 patch_info->data.table->table = (MonoBasicBlock**)table;
2402 case MONO_PATCH_INFO_METHOD_JUMP: {
2403 MonoJumpList *jlist;
2404 MonoDomain *domain = cfg->domain;
2405 unsigned char *ip = cfg->native_code + patch_info->ip.i;
2406 #if defined(__native_client__) && defined(__native_client_codegen__)
2407 /* When this jump target gets evaluated, the method */
2408 /* will be installed in the dynamic code section, */
2409 /* not at the location of cfg->native_code. */
2410 ip = nacl_inverse_modify_patch_target (cfg->native_code) + patch_info->ip.i;
2413 mono_domain_lock (domain);
2414 jlist = g_hash_table_lookup (domain_jit_info (domain)->jump_target_hash, patch_info->data.method);
2416 jlist = mono_domain_alloc0 (domain, sizeof (MonoJumpList));
2417 g_hash_table_insert (domain_jit_info (domain)->jump_target_hash, patch_info->data.method, jlist);
2419 jlist->list = g_slist_prepend (jlist->list, ip);
2420 mono_domain_unlock (domain);
2431 mono_codegen (MonoCompile *cfg)
2434 int max_epilog_size;
2436 MonoDomain *code_domain;
2438 if (mono_using_xdebug)
2440 * Recent gdb versions have trouble processing symbol files containing
2441 * overlapping address ranges, so allocate all code from the code manager
2442 * of the root domain. (#666152).
2444 code_domain = mono_get_root_domain ();
2446 code_domain = cfg->domain;
2448 #if defined(__native_client_codegen__) && defined(__native_client__)
2451 /* This keeps patch targets from being transformed during
2452 * ordinary method compilation, for local branches and jumps.
2454 nacl_allow_target_modification (FALSE);
2457 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2458 cfg->spill_count = 0;
2459 /* we reuse dfn here */
2460 /* bb->dfn = bb_count++; */
2462 mono_arch_lowering_pass (cfg, bb);
2464 if (cfg->opt & MONO_OPT_PEEPHOLE)
2465 mono_arch_peephole_pass_1 (cfg, bb);
2468 mono_local_regalloc (cfg, bb);
2470 if (cfg->opt & MONO_OPT_PEEPHOLE)
2471 mono_arch_peephole_pass_2 (cfg, bb);
2473 if (cfg->gen_seq_points && !cfg->gen_seq_points_debug_data)
2474 bb_deduplicate_op_il_seq_points (cfg, bb);
2477 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2478 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
2480 code = mono_arch_emit_prolog (cfg);
2482 cfg->code_len = code - cfg->native_code;
2483 cfg->prolog_end = cfg->code_len;
2484 cfg->cfa_reg = cfg->cur_cfa_reg;
2485 cfg->cfa_offset = cfg->cur_cfa_offset;
2487 mono_debug_open_method (cfg);
2489 /* emit code all basic blocks */
2490 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2491 bb->native_offset = cfg->code_len;
2492 bb->real_native_offset = cfg->code_len;
2493 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
2494 mono_arch_output_basic_block (cfg, bb);
2495 bb->native_length = cfg->code_len - bb->native_offset;
2497 if (bb == cfg->bb_exit) {
2498 cfg->epilog_begin = cfg->code_len;
2499 mono_arch_emit_epilog (cfg);
2500 cfg->epilog_end = cfg->code_len;
2504 #ifdef __native_client_codegen__
2505 mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
2507 mono_arch_emit_exceptions (cfg);
2509 max_epilog_size = 0;
2511 /* we always allocate code in cfg->domain->code_mp to increase locality */
2512 cfg->code_size = cfg->code_len + max_epilog_size;
2513 #ifdef __native_client_codegen__
2514 cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
2516 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
2518 if (cfg->method->dynamic) {
2519 guint unwindlen = 0;
2520 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
2521 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
2523 /* Allocate the code into a separate memory pool so it can be freed */
2524 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
2525 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
2526 mono_domain_lock (cfg->domain);
2527 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
2528 mono_domain_unlock (cfg->domain);
2530 if (mono_using_xdebug)
2531 /* See the comment for cfg->code_domain */
2532 code = mono_domain_code_reserve (code_domain, cfg->code_size + unwindlen);
2534 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size + unwindlen);
2536 guint unwindlen = 0;
2537 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
2538 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
2540 code = mono_domain_code_reserve (code_domain, cfg->code_size + unwindlen);
2542 #if defined(__native_client_codegen__) && defined(__native_client__)
2543 nacl_allow_target_modification (TRUE);
2547 memcpy (code, cfg->native_code, cfg->code_len);
2548 #if defined(__default_codegen__)
2549 g_free (cfg->native_code);
2550 #elif defined(__native_client_codegen__)
2551 if (cfg->native_code_alloc) {
2552 g_free (cfg->native_code_alloc);
2553 cfg->native_code_alloc = 0;
2555 else if (cfg->native_code) {
2556 g_free (cfg->native_code);
2558 #endif /* __native_client_codegen__ */
2559 cfg->native_code = code;
2560 code = cfg->native_code + cfg->code_len;
2562 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
2563 mono_postprocess_patches (cfg);
2565 #ifdef VALGRIND_JIT_REGISTER_MAP
2566 if (valgrind_register){
2567 char* nm = mono_method_full_name (cfg->method, TRUE);
2568 VALGRIND_JIT_REGISTER_MAP (nm, cfg->native_code, cfg->native_code + cfg->code_len);
2573 if (cfg->verbose_level > 0) {
2574 char* nm = mono_method_full_name (cfg->method, TRUE);
2575 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
2577 cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
2582 gboolean is_generic = FALSE;
2584 if (cfg->method->is_inflated || mono_method_get_generic_container (cfg->method) ||
2585 cfg->method->klass->generic_container || cfg->method->klass->generic_class) {
2589 if (cfg->generic_sharing_context)
2590 g_assert (is_generic);
2593 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
2594 mono_arch_save_unwind_info (cfg);
2597 #if defined(__native_client_codegen__) && defined(__native_client__)
2598 if (!cfg->compile_aot) {
2599 if (cfg->method->dynamic) {
2600 code_dest = nacl_code_manager_get_code_dest(cfg->dynamic_info->code_mp, cfg->native_code);
2602 code_dest = nacl_domain_get_code_dest(cfg->domain, cfg->native_code);
2607 #if defined(__native_client_codegen__)
2608 mono_nacl_fix_patches (cfg->native_code, cfg->patch_info);
2611 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->dynamic_info ? cfg->dynamic_info->code_mp : NULL, cfg->run_cctors);
2613 if (cfg->method->dynamic) {
2614 if (mono_using_xdebug)
2615 mono_domain_code_commit (code_domain, cfg->native_code, cfg->code_size, cfg->code_len);
2617 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
2619 mono_domain_code_commit (code_domain, cfg->native_code, cfg->code_size, cfg->code_len);
2621 #if defined(__native_client_codegen__) && defined(__native_client__)
2622 cfg->native_code = code_dest;
2624 mono_profiler_code_buffer_new (cfg->native_code, cfg->code_len, MONO_PROFILER_CODE_BUFFER_METHOD, cfg->method);
2626 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
2628 mono_debug_close_method (cfg);
2630 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
2631 mono_arch_unwindinfo_install_unwind_info (&cfg->arch.unwindinfo, cfg->native_code, cfg->code_len);
2636 compute_reachable (MonoBasicBlock *bb)
2640 if (!(bb->flags & BB_VISITED)) {
2641 bb->flags |= BB_VISITED;
2642 for (i = 0; i < bb->out_count; ++i)
2643 compute_reachable (bb->out_bb [i]);
2648 mono_handle_out_of_line_bblock (MonoCompile *cfg)
2651 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2652 if (bb->next_bb && bb->next_bb->out_of_line && bb->last_ins && !MONO_IS_BRANCH_OP (bb->last_ins)) {
2654 MONO_INST_NEW (cfg, ins, OP_BR);
2655 MONO_ADD_INS (bb, ins);
2656 ins->inst_target_bb = bb->next_bb;
2661 #endif /* #ifndef DISABLE_JIT */
2664 create_jit_info_for_trampoline (MonoMethod *wrapper, MonoTrampInfo *info)
2666 MonoDomain *domain = mono_get_root_domain ();
2671 if (info->uw_info) {
2672 uw_info = info->uw_info;
2673 info_len = info->uw_info_len;
2675 uw_info = mono_unwind_ops_encode (info->unwind_ops, &info_len);
2678 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
2679 jinfo->d.method = wrapper;
2680 jinfo->code_start = info->code;
2681 jinfo->code_size = info->code_size;
2682 jinfo->unwind_info = mono_cache_unwind_info (uw_info, info_len);
2693 create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
2696 MonoMethodHeader *header;
2698 MonoJitInfoFlags flags = JIT_INFO_NONE;
2699 int num_clauses, num_holes = 0;
2700 guint32 stack_size = 0;
2702 g_assert (method_to_compile == cfg->method);
2703 header = cfg->header;
2705 if (cfg->generic_sharing_context)
2706 flags |= JIT_INFO_HAS_GENERIC_JIT_INFO;
2708 if (cfg->arch_eh_jit_info) {
2709 MonoJitArgumentInfo *arg_info;
2710 MonoMethodSignature *sig = mono_method_signature (cfg->method_to_register);
2713 * This cannot be computed during stack walking, as
2714 * mono_arch_get_argument_info () is not signal safe.
2716 arg_info = g_newa (MonoJitArgumentInfo, sig->param_count + 1);
2717 stack_size = mono_arch_get_argument_info (cfg->generic_sharing_context, sig, sig->param_count, arg_info);
2720 flags |= JIT_INFO_HAS_ARCH_EH_INFO;
2723 if (cfg->has_unwind_info_for_epilog && !(flags & JIT_INFO_HAS_ARCH_EH_INFO))
2724 flags |= JIT_INFO_HAS_ARCH_EH_INFO;
2726 if (cfg->try_block_holes) {
2727 for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
2728 TryBlockHole *hole = tmp->data;
2729 MonoExceptionClause *ec = hole->clause;
2730 int hole_end = hole->basic_block->native_offset + hole->basic_block->native_length;
2731 MonoBasicBlock *clause_last_bb = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
2732 g_assert (clause_last_bb);
2734 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
2735 if (clause_last_bb->native_offset != hole_end)
2739 flags |= JIT_INFO_HAS_TRY_BLOCK_HOLES;
2740 if (G_UNLIKELY (cfg->verbose_level >= 4))
2741 printf ("Number of try block holes %d\n", num_holes);
2744 if (mono_security_method_has_declsec (cfg->method_to_register))
2745 flags |= JIT_INFO_HAS_ARCH_EH_INFO;
2747 if (COMPILE_LLVM (cfg))
2748 num_clauses = cfg->llvm_ex_info_len;
2750 num_clauses = header->num_clauses;
2752 if (cfg->method->dynamic)
2753 jinfo = g_malloc0 (mono_jit_info_size (flags, num_clauses, num_holes));
2755 jinfo = mono_domain_alloc0 (cfg->domain, mono_jit_info_size (flags, num_clauses, num_holes));
2756 mono_jit_info_init (jinfo, cfg->method_to_register, cfg->native_code, cfg->code_len, flags, num_clauses, num_holes);
2757 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
2759 if (COMPILE_LLVM (cfg))
2760 jinfo->from_llvm = TRUE;
2762 if (cfg->generic_sharing_context) {
2764 MonoGenericJitInfo *gi;
2765 GSList *loclist = NULL;
2767 gi = mono_jit_info_get_generic_jit_info (jinfo);
2770 if (cfg->method->dynamic)
2771 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2773 gi->generic_sharing_context = mono_domain_alloc0 (cfg->domain, sizeof (MonoGenericSharingContext));
2774 mini_init_gsctx (cfg->method->dynamic ? NULL : cfg->domain, NULL, cfg->gsctx_context, gi->generic_sharing_context);
2776 if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
2777 mini_method_get_context (method_to_compile)->method_inst ||
2778 method_to_compile->klass->valuetype) {
2779 g_assert (cfg->rgctx_var);
2784 if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
2785 mini_method_get_context (method_to_compile)->method_inst ||
2786 method_to_compile->klass->valuetype) {
2787 inst = cfg->rgctx_var;
2788 if (!COMPILE_LLVM (cfg))
2789 g_assert (inst->opcode == OP_REGOFFSET);
2790 loclist = cfg->rgctx_loclist;
2792 inst = cfg->args [0];
2793 loclist = cfg->this_loclist;
2797 /* Needed to handle async exceptions */
2801 gi->nlocs = g_slist_length (loclist);
2802 if (cfg->method->dynamic)
2803 gi->locations = g_malloc0 (gi->nlocs * sizeof (MonoDwarfLocListEntry));
2805 gi->locations = mono_domain_alloc0 (cfg->domain, gi->nlocs * sizeof (MonoDwarfLocListEntry));
2807 for (l = loclist; l; l = l->next) {
2808 memcpy (&(gi->locations [i]), l->data, sizeof (MonoDwarfLocListEntry));
2813 if (COMPILE_LLVM (cfg)) {
2814 g_assert (cfg->llvm_this_reg != -1);
2815 gi->this_in_reg = 0;
2816 gi->this_reg = cfg->llvm_this_reg;
2817 gi->this_offset = cfg->llvm_this_offset;
2818 } else if (inst->opcode == OP_REGVAR) {
2819 gi->this_in_reg = 1;
2820 gi->this_reg = inst->dreg;
2822 g_assert (inst->opcode == OP_REGOFFSET);
2824 g_assert (inst->inst_basereg == X86_EBP);
2825 #elif defined(TARGET_AMD64)
2826 g_assert (inst->inst_basereg == X86_EBP || inst->inst_basereg == X86_ESP);
2828 g_assert (inst->inst_offset >= G_MININT32 && inst->inst_offset <= G_MAXINT32);
2830 gi->this_in_reg = 0;
2831 gi->this_reg = inst->inst_basereg;
2832 gi->this_offset = inst->inst_offset;
2837 MonoTryBlockHoleTableJitInfo *table;
2840 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
2841 table->num_holes = (guint16)num_holes;
2843 for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
2844 guint32 start_bb_offset;
2845 MonoTryBlockHoleJitInfo *hole;
2846 TryBlockHole *hole_data = tmp->data;
2847 MonoExceptionClause *ec = hole_data->clause;
2848 int hole_end = hole_data->basic_block->native_offset + hole_data->basic_block->native_length;
2849 MonoBasicBlock *clause_last_bb = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
2850 g_assert (clause_last_bb);
2852 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
2853 if (clause_last_bb->native_offset == hole_end)
2856 start_bb_offset = hole_data->start_offset - hole_data->basic_block->native_offset;
2857 hole = &table->holes [i++];
2858 hole->clause = hole_data->clause - &header->clauses [0];
2859 hole->offset = (guint32)hole_data->start_offset;
2860 hole->length = (guint16)(hole_data->basic_block->native_length - start_bb_offset);
2862 if (G_UNLIKELY (cfg->verbose_level >= 4))
2863 printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole->clause, hole->offset, hole->length);
2865 g_assert (i == num_holes);
2868 if (jinfo->has_arch_eh_info) {
2869 MonoArchEHJitInfo *info;
2871 info = mono_jit_info_get_arch_eh_info (jinfo);
2873 info->stack_size = stack_size;
2876 if (COMPILE_LLVM (cfg)) {
2878 memcpy (&jinfo->clauses [0], &cfg->llvm_ex_info [0], num_clauses * sizeof (MonoJitExceptionInfo));
2879 } else if (header->num_clauses) {
2882 for (i = 0; i < header->num_clauses; i++) {
2883 MonoExceptionClause *ec = &header->clauses [i];
2884 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2885 MonoBasicBlock *tblock;
2886 MonoInst *exvar, *spvar;
2888 ei->flags = ec->flags;
2891 * The spvars are needed by mono_arch_install_handler_block_guard ().
2893 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2896 region = ((i + 1) << 8) | MONO_REGION_FINALLY | ec->flags;
2897 spvar = mono_find_spvar_for_region (cfg, region);
2899 ei->exvar_offset = spvar->inst_offset;
2901 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
2902 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
2905 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2906 tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
2908 ei->data.filter = cfg->native_code + tblock->native_offset;
2910 ei->data.catch_class = ec->data.catch_class;
2913 tblock = cfg->cil_offset_to_bb [ec->try_offset];
2915 g_assert (tblock->native_offset);
2916 ei->try_start = cfg->native_code + tblock->native_offset;
2917 if (tblock->extend_try_block) {
2919 * Extend the try block backwards to include parts of the previous call
2922 ei->try_start = (guint8*)ei->try_start - MONO_ARCH_MONITOR_ENTER_ADJUSTMENT;
2924 tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
2926 if (!tblock->native_offset) {
2928 for (j = ec->try_offset + ec->try_len, end = ec->try_offset; j >= end; --j) {
2929 MonoBasicBlock *bb = cfg->cil_offset_to_bb [j];
2930 if (bb && bb->native_offset) {
2936 ei->try_end = cfg->native_code + tblock->native_offset;
2937 g_assert (tblock->native_offset);
2938 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
2940 ei->handler_start = cfg->native_code + tblock->native_offset;
2942 for (tmp = cfg->try_block_holes; tmp; tmp = tmp->next) {
2943 TryBlockHole *hole = tmp->data;
2944 gpointer hole_end = cfg->native_code + (hole->basic_block->native_offset + hole->basic_block->native_length);
2945 if (hole->clause == ec && hole_end == ei->try_end) {
2946 if (G_UNLIKELY (cfg->verbose_level >= 4))
2947 printf ("\tShortening try block %d from %x to %x\n", i, (int)((guint8*)ei->try_end - cfg->native_code), hole->start_offset);
2949 ei->try_end = cfg->native_code + hole->start_offset;
2954 if (ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2956 if (ec->handler_offset + ec->handler_len < header->code_size) {
2957 tblock = cfg->cil_offset_to_bb [ec->handler_offset + ec->handler_len];
2958 if (tblock->native_offset) {
2959 end_offset = tblock->native_offset;
2963 for (j = ec->handler_offset + ec->handler_len, end = ec->handler_offset; j >= end; --j) {
2964 MonoBasicBlock *bb = cfg->cil_offset_to_bb [j];
2965 if (bb && bb->native_offset) {
2970 end_offset = tblock->native_offset + tblock->native_length;
2973 end_offset = cfg->epilog_begin;
2975 ei->data.handler_end = cfg->native_code + end_offset;
2980 if (G_UNLIKELY (cfg->verbose_level >= 4)) {
2982 for (i = 0; i < jinfo->num_clauses; i++) {
2983 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2984 int start = (guint8*)ei->try_start - cfg->native_code;
2985 int end = (guint8*)ei->try_end - cfg->native_code;
2986 int handler = (guint8*)ei->handler_start - cfg->native_code;
2987 int handler_end = (guint8*)ei->data.handler_end - cfg->native_code;
2989 printf ("JitInfo EH clause %d flags %x try %x-%x handler %x-%x\n", i, ei->flags, start, end, handler, handler_end);
2993 if (cfg->encoded_unwind_ops) {
2994 /* Generated by LLVM */
2995 jinfo->unwind_info = mono_cache_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
2996 g_free (cfg->encoded_unwind_ops);
2997 } else if (cfg->unwind_ops) {
2999 guint8 *unwind_info = mono_unwind_ops_encode (cfg->unwind_ops, &info_len);
3000 guint32 unwind_desc;
3002 unwind_desc = mono_cache_unwind_info (unwind_info, info_len);
3004 if (cfg->has_unwind_info_for_epilog) {
3005 MonoArchEHJitInfo *info;
3007 info = mono_jit_info_get_arch_eh_info (jinfo);
3009 info->epilog_size = cfg->code_len - cfg->epilog_begin;
3011 jinfo->unwind_info = unwind_desc;
3012 g_free (unwind_info);
3014 jinfo->unwind_info = cfg->used_int_regs;
3022 is_gsharedvt_type (MonoType *t)
3024 return (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR) && t->data.generic_param->gshared_constraint == MONO_TYPE_VALUETYPE;
3027 /* Return whenever METHOD is a gsharedvt method */
3029 is_gsharedvt_method (MonoMethod *method)
3031 MonoGenericContext *context;
3032 MonoGenericInst *inst;
3035 if (!method->is_inflated)
3037 context = mono_method_get_context (method);
3038 inst = context->class_inst;
3040 for (i = 0; i < inst->type_argc; ++i)
3041 if (is_gsharedvt_type (inst->type_argv [i]))
3044 inst = context->method_inst;
3046 for (i = 0; i < inst->type_argc; ++i)
3047 if (is_gsharedvt_type (inst->type_argv [i]))
3054 is_open_method (MonoMethod *method)
3056 MonoGenericContext *context;
3058 if (!method->is_inflated)
3060 context = mono_method_get_context (method);
3061 if (context->class_inst && context->class_inst->is_open)
3063 if (context->method_inst && context->method_inst->is_open)
3070 * mini_method_compile:
3071 * @method: the method to compile
3072 * @opts: the optimization flags to use
3073 * @domain: the domain where the method will be compiled in
3074 * @flags: compilation flags
3075 * @parts: debug flag
3077 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3078 * field in the returned struct to see if compilation succeded.
3081 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFlags flags, int parts)
3083 MonoMethodHeader *header;
3084 MonoMethodSignature *sig;
3088 int dfn, i, code_size_ratio;
3090 gboolean deadce_has_run = FALSE;
3092 gboolean try_generic_shared, try_llvm = FALSE;
3093 MonoMethod *method_to_compile, *method_to_register;
3094 gboolean method_is_gshared = FALSE;
3095 gboolean run_cctors = (flags & JIT_FLAG_RUN_CCTORS) ? 1 : 0;
3096 gboolean compile_aot = (flags & JIT_FLAG_AOT) ? 1 : 0;
3097 gboolean full_aot = (flags & JIT_FLAG_FULL_AOT) ? 1 : 0;
3099 gboolean llvm = (flags & JIT_FLAG_LLVM) ? 1 : 0;
3101 static gboolean verbose_method_inited;
3102 static const char *verbose_method_name;
3104 InterlockedIncrement (&mono_jit_stats.methods_compiled);
3105 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
3106 mono_profiler_method_jit (method);
3107 if (MONO_METHOD_COMPILE_BEGIN_ENABLED ())
3108 MONO_PROBE_METHOD_COMPILE_BEGIN (method);
3112 * We might get passed the original generic method definition or
3113 * instances with type parameters.
3114 * FIXME: Remove the method->klass->generic_class limitation.
3116 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
3117 (opts & MONO_OPT_GSHARED) && ((method->is_generic || method->klass->generic_container) || (!method->klass->generic_class && mono_method_is_generic_sharable_full (method, TRUE, FALSE, FALSE)));
3119 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
3120 (opts & MONO_OPT_GSHARED) && mono_method_is_generic_sharable (method, FALSE);
3122 if (opts & MONO_OPT_GSHARED) {
3123 if (try_generic_shared)
3124 mono_stats.generics_sharable_methods++;
3125 else if (mono_method_is_generic_impl (method))
3126 mono_stats.generics_unsharable_methods++;
3129 if (mini_is_gsharedvt_sharable_method (method)) {
3130 if (!mono_debug_count ())
3131 try_generic_shared = FALSE;
3133 try_generic_shared = FALSE;
3136 if (is_gsharedvt_method (method) || (compile_aot && is_open_method (method))) {
3137 /* We are AOTing a gshared method directly */
3138 method_is_gshared = TRUE;
3139 g_assert (compile_aot);
3140 try_generic_shared = TRUE;
3144 try_llvm = mono_use_llvm || llvm;
3148 if (method_is_gshared) {
3149 method_to_compile = method;
3151 if (try_generic_shared) {
3152 method_to_compile = mini_get_shared_method (method);
3153 g_assert (method_to_compile);
3155 method_to_compile = method;
3159 cfg = g_new0 (MonoCompile, 1);
3160 cfg->method = method_to_compile;
3161 cfg->header = mono_method_get_header (cfg->method);
3162 cfg->mempool = mono_mempool_new ();
3164 cfg->prof_options = mono_profiler_get_events ();
3165 cfg->run_cctors = run_cctors;
3166 cfg->domain = domain;
3167 cfg->verbose_level = mini_verbose;
3168 cfg->compile_aot = compile_aot;
3169 cfg->full_aot = full_aot;
3170 cfg->skip_visibility = method->skip_visibility;
3171 cfg->orig_method = method;
3172 cfg->gen_seq_points = debug_options.gen_seq_points_compact_data || debug_options.gen_seq_points_debug_data;
3173 cfg->gen_seq_points_debug_data = debug_options.gen_seq_points_debug_data;
3175 cfg->explicit_null_checks = debug_options.explicit_null_checks;
3176 cfg->soft_breakpoints = debug_options.soft_breakpoints;
3177 cfg->check_pinvoke_callconv = debug_options.check_pinvoke_callconv;
3178 if (try_generic_shared)
3179 cfg->generic_sharing_context = (MonoGenericSharingContext*)&cfg->gsctx;
3180 cfg->compile_llvm = try_llvm;
3181 cfg->token_info_hash = g_hash_table_new (NULL, NULL);
3183 if (!mono_debug_count ())
3184 cfg->opt &= ~MONO_OPT_FLOAT32;
3185 cfg->r4fp = (cfg->opt & MONO_OPT_FLOAT32) ? 1 : 0;
3186 cfg->r4_stack_type = cfg->r4fp ? STACK_R4 : STACK_R8;
3188 if (cfg->gen_seq_points)
3189 cfg->seq_points = g_ptr_array_new ();
3190 mono_error_init (&cfg->error);
3192 if (cfg->compile_aot && !try_generic_shared && (method->is_generic || method->klass->generic_container || method_is_gshared)) {
3193 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED;
3197 if (cfg->generic_sharing_context && (mini_is_gsharedvt_sharable_method (method) || method_is_gshared)) {
3198 MonoMethodInflated *inflated;
3199 MonoGenericContext *context;
3201 if (method_is_gshared) {
3202 g_assert (method->is_inflated);
3203 inflated = (MonoMethodInflated*)method;
3204 context = &inflated->context;
3206 /* We are compiling a gsharedvt method directly */
3207 g_assert (compile_aot);
3209 g_assert (method_to_compile->is_inflated);
3210 inflated = (MonoMethodInflated*)method_to_compile;
3211 context = &inflated->context;
3214 mini_init_gsctx (NULL, cfg->mempool, context, &cfg->gsctx);
3215 cfg->gsctx_context = context;
3217 cfg->gsharedvt = TRUE;
3219 cfg->disable_llvm = TRUE;
3222 if (cfg->generic_sharing_context) {
3223 method_to_register = method_to_compile;
3224 cfg->gshared = TRUE;
3226 g_assert (method == method_to_compile);
3227 method_to_register = method;
3229 cfg->method_to_register = method_to_register;
3231 mono_error_init (&err);
3232 sig = mono_method_signature_checked (cfg->method, &err);
3234 cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
3235 cfg->exception_message = g_strdup (mono_error_get_message (&err));
3236 mono_error_cleanup (&err);
3237 if (MONO_METHOD_COMPILE_END_ENABLED ())
3238 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
3242 header = cfg->header;
3244 MonoLoaderError *error;
3246 if ((error = mono_loader_get_last_error ())) {
3247 cfg->exception_type = error->exception_type;
3249 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
3250 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
3252 if (MONO_METHOD_COMPILE_END_ENABLED ())
3253 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
3259 static gboolean inited;
3265 * Check for methods which cannot be compiled by LLVM early, to avoid
3266 * the extra compilation pass.
3268 if (COMPILE_LLVM (cfg)) {
3269 mono_llvm_check_method_supported (cfg);
3270 if (cfg->disable_llvm) {
3271 if (cfg->verbose_level >= 1) {
3272 //nm = mono_method_full_name (cfg->method, TRUE);
3273 printf ("LLVM failed for '%s': %s\n", method->name, cfg->exception_message);
3276 mono_destroy_compile (cfg);
3278 goto restart_compile;
3284 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3285 if (debug_options.mdb_optimizations) {
3286 cfg->disable_reuse_registers = TRUE;
3287 cfg->disable_reuse_stack_slots = TRUE;
3289 * This decreases the change the debugger will read registers/stack slots which are
3290 * not yet initialized.
3292 cfg->disable_initlocals_opt = TRUE;
3294 cfg->extend_live_ranges = TRUE;
3296 /* Temporarily disable this when running in the debugger until we have support
3297 * for this in the debugger. */
3298 /* This is no longer needed with sdb */
3299 //cfg->disable_omit_fp = TRUE;
3301 /* The debugger needs all locals to be on the stack or in a global register */
3302 cfg->disable_vreg_to_lvreg = TRUE;
3304 /* Don't remove unused variables when running inside the debugger since the user
3305 * may still want to view them. */
3306 cfg->disable_deadce_vars = TRUE;
3308 // cfg->opt |= MONO_OPT_SHARED;
3309 cfg->opt &= ~MONO_OPT_DEADCE;
3310 cfg->opt &= ~MONO_OPT_INLINE;
3311 cfg->opt &= ~MONO_OPT_COPYPROP;
3312 cfg->opt &= ~MONO_OPT_CONSPROP;
3313 /* This is no longer needed with sdb */
3314 //cfg->opt &= ~MONO_OPT_GSHARED;
3316 /* This is needed for the soft debugger, which doesn't like code after the epilog */
3317 cfg->disable_out_of_line_bblocks = TRUE;
3320 if (mono_using_xdebug) {
3322 * Make each variable use its own register/stack slot and extend
3323 * their liveness to cover the whole method, making them displayable
3324 * in gdb even after they are dead.
3326 cfg->disable_reuse_registers = TRUE;
3327 cfg->disable_reuse_stack_slots = TRUE;
3328 cfg->extend_live_ranges = TRUE;
3329 cfg->compute_precise_live_ranges = TRUE;
3332 mini_gc_init_cfg (cfg);
3334 if (COMPILE_LLVM (cfg)) {
3335 cfg->opt |= MONO_OPT_ABCREM;
3338 if (!verbose_method_inited) {
3339 verbose_method_name = g_getenv ("MONO_VERBOSE_METHOD");
3340 verbose_method_inited = TRUE;
3342 if (verbose_method_name) {
3343 const char *name = verbose_method_name;
3345 if ((strchr (name, '.') > name) || strchr (name, ':')) {
3346 MonoMethodDesc *desc;
3348 desc = mono_method_desc_new (name, TRUE);
3349 if (mono_method_desc_full_match (desc, cfg->method)) {
3350 cfg->verbose_level = 4;
3352 mono_method_desc_free (desc);
3354 if (strcmp (cfg->method->name, name) == 0)
3355 cfg->verbose_level = 4;
3359 ip = (guint8 *)header->code;
3361 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
3363 if (cfg->verbose_level > 0) {
3366 method_name = mono_method_full_name (method, TRUE);
3367 g_print ("converting %s%s%smethod %s\n", COMPILE_LLVM (cfg) ? "llvm " : "", cfg->gsharedvt ? "gsharedvt " : "", (cfg->generic_sharing_context && !cfg->gsharedvt) ? "gshared " : "", method_name);
3369 if (COMPILE_LLVM (cfg))
3370 g_print ("converting llvm method %s\n", method_name = mono_method_full_name (method, TRUE));
3371 else if (cfg->gsharedvt)
3372 g_print ("converting gsharedvt method %s\n", method_name = mono_method_full_name (method_to_compile, TRUE));
3373 else if (cfg->generic_sharing_context)
3374 g_print ("converting shared method %s\n", method_name = mono_method_full_name (method_to_compile, TRUE));
3376 g_print ("converting method %s\n", method_name = mono_method_full_name (method, TRUE));
3378 g_free (method_name);
3381 if (cfg->opt & (MONO_OPT_ABCREM | MONO_OPT_SSAPRE))
3382 cfg->opt |= MONO_OPT_SSA;
3385 if ((cfg->method->klass->image != mono_defaults.corlib) || (strstr (cfg->method->klass->name, "StackOverflowException") && strstr (cfg->method->name, ".ctor")) || (strstr (cfg->method->klass->name, "OutOfMemoryException") && strstr (cfg->method->name, ".ctor")))
3386 cfg->globalra = TRUE;
3389 //cfg->globalra = TRUE;
3391 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3392 // cfg->globalra = TRUE;
3395 static int count = 0;
3399 if (g_getenv ("COUNT2")) {
3400 cfg->globalra = TRUE;
3401 if (count == atoi (g_getenv ("COUNT2")))
3402 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3403 if (count > atoi (g_getenv ("COUNT2")))
3404 cfg->globalra = FALSE;
3409 if (header->clauses)
3410 cfg->globalra = FALSE;
3412 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
3413 /* The code in the prolog clobbers caller saved registers */
3414 cfg->globalra = FALSE;
3416 // FIXME: Disable globalra in case of tracing/profiling
3418 if (cfg->method->save_lmf)
3419 /* The LMF saving code might clobber caller saved registers */
3420 cfg->globalra = FALSE;
3422 if (header->code_size > 5000)
3424 /* Too large bblocks could overflow the ins positions */
3425 cfg->globalra = FALSE;
3427 cfg->rs = mono_regstate_new ();
3429 cfg->rs->next_vreg = MONO_MAX_IREGS + MONO_MAX_FREGS;
3430 cfg->next_vreg = cfg->rs->next_vreg;
3432 /* FIXME: Fix SSA to handle branches inside bblocks */
3433 if (cfg->opt & MONO_OPT_SSA)
3434 cfg->enable_extended_bblocks = FALSE;
3437 * FIXME: This confuses liveness analysis because variables which are assigned after
3438 * a branch inside a bblock become part of the kill set, even though the assignment
3439 * might not get executed. This causes the optimize_initlocals pass to delete some
3440 * assignments which are needed.
3441 * Also, the mono_if_conversion pass needs to be modified to recognize the code
3444 //cfg->enable_extended_bblocks = TRUE;
3446 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
3447 if (mono_compile_is_broken (cfg, cfg->method, TRUE)) {
3448 if (mini_get_debug_options ()->break_on_unverified)
3454 * create MonoInst* which represents arguments and local variables
3456 mono_compile_create_vars (cfg);
3458 /* SSAPRE is not supported on linear IR */
3459 cfg->opt &= ~MONO_OPT_SSAPRE;
3461 i = mono_method_to_ir (cfg, method_to_compile, NULL, NULL, NULL, NULL, 0, FALSE);
3464 if (try_generic_shared && cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
3466 if (MONO_METHOD_COMPILE_END_ENABLED ())
3467 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
3470 mono_destroy_compile (cfg);
3471 try_generic_shared = FALSE;
3472 goto restart_compile;
3474 g_assert (cfg->exception_type != MONO_EXCEPTION_GENERIC_SHARING_FAILED);
3476 if (MONO_METHOD_COMPILE_END_ENABLED ())
3477 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
3478 /* cfg contains the details of the failure, so let the caller cleanup */
3482 cfg->stat_basic_blocks += cfg->num_bblocks;
3484 if (COMPILE_LLVM (cfg)) {
3487 /* The IR has to be in SSA form for LLVM */
3488 cfg->opt |= MONO_OPT_SSA;
3492 // Allow SSA on the result value
3493 cfg->ret->flags &= ~MONO_INST_VOLATILE;
3495 // Add an explicit return instruction referencing the return value
3496 MONO_INST_NEW (cfg, ins, OP_SETRET);
3497 ins->sreg1 = cfg->ret->dreg;
3499 MONO_ADD_INS (cfg->bb_exit, ins);
3502 cfg->opt &= ~MONO_OPT_LINEARS;
3505 cfg->opt &= ~MONO_OPT_BRANCH;
3508 /* todo: remove code when we have verified that the liveness for try/catch blocks
3512 * Currently, this can't be commented out since exception blocks are not
3513 * processed during liveness analysis.
3514 * It is also needed, because otherwise the local optimization passes would
3515 * delete assignments in cases like this:
3517 * <something which throws>
3519 * This also allows SSA to be run on methods containing exception clauses, since
3520 * SSA will ignore variables marked VOLATILE.
3522 mono_liveness_handle_exception_clauses (cfg);
3524 mono_handle_out_of_line_bblock (cfg);
3526 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
3528 if (!COMPILE_LLVM (cfg))
3529 mono_decompose_long_opts (cfg);
3531 /* Should be done before branch opts */
3532 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
3533 mono_local_cprop (cfg);
3535 if (cfg->opt & MONO_OPT_BRANCH)
3536 mono_optimize_branches (cfg);
3538 /* This must be done _before_ global reg alloc and _after_ decompose */
3539 mono_handle_global_vregs (cfg);
3540 if (cfg->opt & MONO_OPT_DEADCE)
3541 mono_local_deadce (cfg);
3542 if (cfg->opt & MONO_OPT_ALIAS_ANALYSIS)
3543 mono_local_alias_analysis (cfg);
3544 /* Disable this for LLVM to make the IR easier to handle */
3545 if (!COMPILE_LLVM (cfg))
3546 mono_if_conversion (cfg);
3548 if ((cfg->opt & MONO_OPT_SSAPRE) || cfg->globalra)
3549 mono_remove_critical_edges (cfg);
3551 /* Depth-first ordering on basic blocks */
3552 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
3554 cfg->max_block_num = cfg->num_bblocks;
3557 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
3558 if (cfg->num_bblocks != dfn + 1) {
3561 cfg->num_bblocks = dfn + 1;
3563 /* remove unreachable code, because the code in them may be
3564 * inconsistent (access to dead variables for example) */
3565 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3566 bb->flags &= ~BB_VISITED;
3567 compute_reachable (cfg->bb_entry);
3568 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3569 if (bb->flags & BB_EXCEPTION_HANDLER)
3570 compute_reachable (bb);
3571 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3572 if (!(bb->flags & BB_VISITED)) {
3573 if (cfg->verbose_level > 1)
3574 g_print ("found unreachable code in BB%d\n", bb->block_num);
3575 bb->code = bb->last_ins = NULL;
3576 while (bb->out_count)
3577 mono_unlink_bblock (cfg, bb, bb->out_bb [0]);
3580 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3581 bb->flags &= ~BB_VISITED;
3584 if (((cfg->num_varinfo > 2000) || (cfg->num_bblocks > 1000)) && !cfg->compile_aot) {
3586 * we disable some optimizations if there are too many variables
3587 * because JIT time may become too expensive. The actual number needs
3588 * to be tweaked and eventually the non-linear algorithms should be fixed.
3590 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
3591 cfg->disable_ssa = TRUE;
3594 if (cfg->opt & MONO_OPT_LOOP) {
3595 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
3596 mono_compute_natural_loops (cfg);
3599 /* after method_to_ir */
3601 if (MONO_METHOD_COMPILE_END_ENABLED ())
3602 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
3607 if (header->num_clauses)
3608 cfg->disable_ssa = TRUE;
3611 //#define DEBUGSSA "logic_run"
3612 //#define DEBUGSSA_CLASS "Tests"
3615 if (!cfg->disable_ssa) {
3616 mono_local_cprop (cfg);
3619 mono_ssa_compute (cfg);
3623 if (cfg->opt & MONO_OPT_SSA) {
3624 if (!(cfg->comp_done & MONO_COMP_SSA) && !cfg->disable_ssa) {
3626 mono_ssa_compute (cfg);
3629 if (cfg->verbose_level >= 2) {
3636 /* after SSA translation */
3638 if (MONO_METHOD_COMPILE_END_ENABLED ())
3639 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
3643 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
3644 if (cfg->comp_done & MONO_COMP_SSA && !COMPILE_LLVM (cfg)) {
3646 mono_ssa_cprop (cfg);
3652 if (cfg->comp_done & MONO_COMP_SSA && !COMPILE_LLVM (cfg)) {
3653 //mono_ssa_strength_reduction (cfg);
3655 if (cfg->opt & MONO_OPT_SSAPRE) {
3656 mono_perform_ssapre (cfg);
3657 //mono_local_cprop (cfg);
3660 if (cfg->opt & MONO_OPT_DEADCE) {
3661 mono_ssa_deadce (cfg);
3662 deadce_has_run = TRUE;
3665 if ((cfg->flags & (MONO_CFG_HAS_LDELEMA|MONO_CFG_HAS_CHECK_THIS)) && (cfg->opt & MONO_OPT_ABCREM))
3666 mono_perform_abc_removal (cfg);
3668 mono_ssa_remove (cfg);
3669 mono_local_cprop (cfg);
3670 mono_handle_global_vregs (cfg);
3671 if (cfg->opt & MONO_OPT_DEADCE)
3672 mono_local_deadce (cfg);
3674 if (cfg->opt & MONO_OPT_BRANCH) {
3677 mono_optimize_branches (cfg);
3679 /* Have to recompute cfg->bblocks and bb->dfn */
3680 if (cfg->globalra) {
3681 mono_remove_critical_edges (cfg);
3683 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3686 /* Depth-first ordering on basic blocks */
3687 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
3690 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
3691 cfg->num_bblocks = dfn + 1;
3697 if (cfg->comp_done & MONO_COMP_SSA && COMPILE_LLVM (cfg)) {
3698 mono_ssa_loop_invariant_code_motion (cfg);
3699 /* This removes MONO_INST_FAULT flags too so perform it unconditionally */
3700 if (cfg->opt & MONO_OPT_ABCREM)
3701 mono_perform_abc_removal (cfg);
3704 /* after SSA removal */
3706 if (MONO_METHOD_COMPILE_END_ENABLED ())
3707 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
3711 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
3712 if (COMPILE_SOFT_FLOAT (cfg))
3713 mono_decompose_soft_float (cfg);
3715 if (COMPILE_LLVM (cfg))
3716 mono_decompose_vtype_opts_llvm (cfg);
3718 mono_decompose_vtype_opts (cfg);
3719 if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
3720 mono_decompose_array_access_opts (cfg);
3723 #ifndef MONO_ARCH_GOT_REG
3728 g_assert (cfg->got_var_allocated);
3731 * Allways allocate the GOT var to a register, because keeping it
3732 * in memory will increase the number of live temporaries in some
3733 * code created by inssel.brg, leading to the well known spills+
3734 * branches problem. Testcase: mcs crash in
3735 * System.MonoCustomAttrs:GetCustomAttributes.
3737 #ifdef MONO_ARCH_GOT_REG
3738 got_reg = MONO_ARCH_GOT_REG;
3740 regs = mono_arch_get_global_int_regs (cfg);
3742 got_reg = GPOINTER_TO_INT (regs->data);
3745 cfg->got_var->opcode = OP_REGVAR;
3746 cfg->got_var->dreg = got_reg;
3747 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
3751 * Have to call this again to process variables added since the first call.
3753 mono_liveness_handle_exception_clauses (cfg);
3755 if (cfg->globalra) {
3758 /* Have to do this before regalloc since it can create vregs */
3759 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
3760 mono_arch_lowering_pass (cfg, bb);
3762 mono_global_regalloc (cfg);
3765 if ((cfg->opt & MONO_OPT_LINEARS) && !cfg->globalra) {
3766 GList *vars, *regs, *l;
3768 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
3769 cfg->comp_done &= ~MONO_COMP_LIVENESS;
3770 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
3771 mono_analyze_liveness (cfg);
3773 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
3774 regs = mono_arch_get_global_int_regs (cfg);
3775 /* Remove the reg reserved for holding the GOT address */
3777 for (l = regs; l; l = l->next) {
3778 if (GPOINTER_TO_UINT (l->data) == cfg->got_var->dreg) {
3779 regs = g_list_delete_link (regs, l);
3784 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
3788 //mono_print_code (cfg, "");
3792 /* variables are allocated after decompose, since decompose could create temps */
3793 if (!cfg->globalra && !COMPILE_LLVM (cfg)) {
3794 mono_arch_allocate_vars (cfg);
3795 if (cfg->exception_type)
3801 gboolean need_local_opts;
3803 if (!cfg->globalra && !COMPILE_LLVM (cfg)) {
3804 mono_spill_global_vars (cfg, &need_local_opts);
3806 if (need_local_opts || cfg->compile_aot) {
3807 /* To optimize code created by spill_global_vars */
3808 mono_local_cprop (cfg);
3809 if (cfg->opt & MONO_OPT_DEADCE)
3810 mono_local_deadce (cfg);
3814 /* Add branches between non-consecutive bblocks */
3815 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3816 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
3817 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
3818 /* we are careful when inverting, since bugs like #59580
3819 * could show up when dealing with NaNs.
3821 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
3822 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
3823 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
3824 bb->last_ins->inst_false_bb = tmp;
3826 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
3828 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
3829 inst->opcode = OP_BR;
3830 inst->inst_target_bb = bb->last_ins->inst_false_bb;
3831 mono_bblock_add_inst (bb, inst);
3836 if (cfg->verbose_level >= 4 && !cfg->globalra) {
3837 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3838 MonoInst *tree = bb->code;
3839 g_print ("DUMP BLOCK %d:\n", bb->block_num);
3842 for (; tree; tree = tree->next) {
3843 mono_print_ins_index (-1, tree);
3849 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3850 bb->max_vreg = cfg->next_vreg;
3854 if (COMPILE_LLVM (cfg)) {
3858 /* The IR has to be in SSA form for LLVM */
3859 if (!(cfg->comp_done & MONO_COMP_SSA)) {
3860 cfg->exception_message = g_strdup ("SSA disabled.");
3861 cfg->disable_llvm = TRUE;
3864 if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
3865 mono_decompose_array_access_opts (cfg);
3867 if (!cfg->disable_llvm)
3868 mono_llvm_emit_method (cfg);
3869 if (cfg->disable_llvm) {
3870 if (cfg->verbose_level >= 1) {
3871 //nm = mono_method_full_name (cfg->method, TRUE);
3872 printf ("LLVM failed for '%s': %s\n", method->name, cfg->exception_message);
3875 mono_destroy_compile (cfg);
3877 goto restart_compile;
3880 if (cfg->verbose_level > 0 && !cfg->compile_aot) {
3881 nm = mono_method_full_name (cfg->method, TRUE);
3882 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
3884 cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
3892 if (COMPILE_LLVM (cfg))
3893 InterlockedIncrement (&mono_jit_stats.methods_with_llvm);
3895 InterlockedIncrement (&mono_jit_stats.methods_without_llvm);
3897 cfg->jit_info = create_jit_info (cfg, method_to_compile);
3899 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
3900 if (cfg->extend_live_ranges) {
3901 /* Extend live ranges to cover the whole method */
3902 for (i = 0; i < cfg->num_varinfo; ++i)
3903 MONO_VARINFO (cfg, i)->live_range_end = cfg->code_len;
3907 if (!cfg->compile_aot)
3908 mono_save_xdebug_info (cfg);
3910 mini_gc_create_gc_map (cfg);
3912 mono_save_seq_point_info (cfg);
3914 if (cfg->verbose_level >= 2) {
3915 char *id = mono_method_full_name (cfg->method, FALSE);
3916 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
3920 if (!cfg->compile_aot) {
3921 mono_domain_lock (cfg->domain);
3922 mono_jit_info_table_add (cfg->domain, cfg->jit_info);
3924 if (cfg->method->dynamic)
3925 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = cfg->jit_info;
3926 mono_domain_unlock (cfg->domain);
3931 printf ("GSHAREDVT: %s\n", mono_method_full_name (cfg->method, TRUE));
3934 /* collect statistics */
3935 #ifndef DISABLE_PERFCOUNTERS
3936 mono_perfcounters->jit_methods++;
3937 mono_perfcounters->jit_bytes += header->code_size;
3939 mono_jit_stats.allocated_code_size += cfg->code_len;
3940 code_size_ratio = cfg->code_len;
3941 if (code_size_ratio > mono_jit_stats.biggest_method_size && mono_jit_stats.enabled) {
3942 mono_jit_stats.biggest_method_size = code_size_ratio;
3943 g_free (mono_jit_stats.biggest_method);
3944 mono_jit_stats.biggest_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
3946 code_size_ratio = (code_size_ratio * 100) / header->code_size;
3947 if (code_size_ratio > mono_jit_stats.max_code_size_ratio && mono_jit_stats.enabled) {
3948 mono_jit_stats.max_code_size_ratio = code_size_ratio;
3949 g_free (mono_jit_stats.max_ratio_method);
3950 mono_jit_stats.max_ratio_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
3952 mono_jit_stats.native_code_size += cfg->code_len;
3954 if (MONO_METHOD_COMPILE_END_ENABLED ())
3955 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
3963 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFlags flags, int parts)
3965 g_assert_not_reached ();
3969 #endif /* DISABLE_JIT */
3972 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt, MonoException **jit_ex)
3975 gpointer code = NULL;
3976 MonoJitInfo *jinfo, *info;
3978 MonoException *ex = NULL;
3979 guint32 prof_options;
3981 MonoMethod *prof_method, *shared;
3983 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3984 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3986 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
3988 if (!piinfo->addr) {
3989 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3990 piinfo->addr = mono_lookup_internal_call (method);
3991 else if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
3993 g_warning ("Method '%s' in assembly '%s' contains native code that cannot be executed by Mono in modules loaded from byte arrays. The assembly was probably created using C++/CLI.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
3995 g_warning ("Method '%s' in assembly '%s' contains native code that cannot be executed by Mono on this platform. The assembly was probably created using C++/CLI.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
3998 mono_lookup_pinvoke_call (method, NULL, NULL);
4000 nm = mono_marshal_get_native_wrapper (method, check_for_pending_exc, mono_aot_only);
4001 code = mono_get_addr_from_ftnptr (mono_compile_method (nm));
4002 jinfo = mono_jit_info_table_find (target_domain, code);
4004 jinfo = mono_jit_info_table_find (mono_domain_get (), code);
4006 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
4008 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
4009 const char *name = method->name;
4010 char *full_name, *msg;
4013 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
4014 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
4015 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
4018 * We need to make sure this wrapper
4019 * is compiled because it might end up
4020 * in an (M)RGCTX if generic sharing
4021 * is enabled, and would be called
4022 * indirectly. If it were a
4023 * trampoline we'd try to patch that
4024 * indirect call, which is not
4027 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper_full (mi, TRUE));
4028 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
4029 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4030 return mono_create_delegate_trampoline (target_domain, method->klass);
4032 nm = mono_marshal_get_delegate_invoke (method, NULL);
4033 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
4035 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
4036 nm = mono_marshal_get_delegate_begin_invoke (method);
4037 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
4038 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
4039 nm = mono_marshal_get_delegate_end_invoke (method);
4040 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
4044 full_name = mono_method_full_name (method, TRUE);
4045 msg = g_strdup_printf ("Unrecognizable runtime implemented method '%s'", full_name);
4046 *jit_ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", msg);
4052 if (method->wrapper_type == MONO_WRAPPER_UNKNOWN) {
4053 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
4055 if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT) {
4056 static MonoTrampInfo *in_tinfo, *out_tinfo;
4057 MonoTrampInfo *tinfo;
4059 gboolean is_in = info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN;
4061 if (is_in && in_tinfo)
4062 return in_tinfo->code;
4063 else if (!is_in && out_tinfo)
4064 return out_tinfo->code;
4067 * This is a special wrapper whose body is implemented in assembly, like a trampoline. We use a wrapper so EH
4069 * FIXME: The caller signature doesn't match the callee, which might cause problems on some platforms
4072 mono_aot_get_trampoline_full (is_in ? "gsharedvt_trampoline" : "gsharedvt_out_trampoline", &tinfo);
4074 mono_arch_get_gsharedvt_trampoline (&tinfo, FALSE);
4075 jinfo = create_jit_info_for_trampoline (method, tinfo);
4076 mono_jit_info_table_add (mono_get_root_domain (), jinfo);
4085 if (mono_aot_only) {
4086 char *fullname = mono_method_full_name (method, TRUE);
4087 char *msg = g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.\n", fullname);
4089 *jit_ex = mono_get_exception_execution_engine (msg);
4096 jit_timer = g_timer_new ();
4098 cfg = mini_method_compile (method, opt, target_domain, JIT_FLAG_RUN_CCTORS, 0);
4099 prof_method = cfg->method;
4101 g_timer_stop (jit_timer);
4102 mono_jit_stats.jit_time += g_timer_elapsed (jit_timer, NULL);
4103 g_timer_destroy (jit_timer);
4105 switch (cfg->exception_type) {
4106 case MONO_EXCEPTION_NONE:
4108 case MONO_EXCEPTION_TYPE_LOAD:
4109 case MONO_EXCEPTION_MISSING_FIELD:
4110 case MONO_EXCEPTION_MISSING_METHOD:
4111 case MONO_EXCEPTION_FILE_NOT_FOUND:
4112 case MONO_EXCEPTION_BAD_IMAGE: {
4113 /* Throw a type load exception if needed */
4114 MonoLoaderError *error = mono_loader_get_last_error ();
4117 ex = mono_loader_error_prepare_exception (error);
4119 if (cfg->exception_ptr) {
4120 ex = mono_class_get_exception_for_failure (cfg->exception_ptr);
4122 if (cfg->exception_type == MONO_EXCEPTION_MISSING_FIELD)
4123 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", cfg->exception_message);
4124 else if (cfg->exception_type == MONO_EXCEPTION_MISSING_METHOD)
4125 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", cfg->exception_message);
4126 else if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD)
4127 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", cfg->exception_message);
4128 else if (cfg->exception_type == MONO_EXCEPTION_FILE_NOT_FOUND)
4129 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FileNotFoundException", cfg->exception_message);
4130 else if (cfg->exception_type == MONO_EXCEPTION_BAD_IMAGE)
4131 ex = mono_get_exception_bad_image_format (cfg->exception_message);
4133 g_assert_not_reached ();
4138 case MONO_EXCEPTION_INVALID_PROGRAM:
4139 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
4141 case MONO_EXCEPTION_UNVERIFIABLE_IL:
4142 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
4144 case MONO_EXCEPTION_METHOD_ACCESS:
4145 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", cfg->exception_message);
4147 case MONO_EXCEPTION_FIELD_ACCESS:
4148 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FieldAccessException", cfg->exception_message);
4150 #ifndef DISABLE_SECURITY
4151 /* this can only be set if the security manager is active */
4152 case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
4153 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4154 MonoObject *exc = NULL;
4157 args [0] = &cfg->exception_data;
4159 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
4161 ex = (MonoException*)exc;
4165 case MONO_EXCEPTION_OBJECT_SUPPLIED: {
4166 MonoException *exp = cfg->exception_ptr;
4167 MONO_GC_UNREGISTER_ROOT (cfg->exception_ptr);
4172 case MONO_EXCEPTION_OUT_OF_MEMORY:
4173 ex = mono_domain_get ()->out_of_memory_ex;
4175 case MONO_EXCEPTION_MONO_ERROR:
4176 g_assert (!mono_error_ok (&cfg->error));
4177 ex = mono_error_convert_to_exception (&cfg->error);
4180 g_assert_not_reached ();
4184 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
4185 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
4187 mono_destroy_compile (cfg);
4193 if (mono_method_is_generic_sharable (method, FALSE))
4194 shared = mini_get_shared_method (method);
4198 mono_domain_lock (target_domain);
4200 /* Check if some other thread already did the job. In this case, we can
4201 discard the code this thread generated. */
4203 info = mini_lookup_method (target_domain, method, shared);
4205 /* We can't use a domain specific method in another domain */
4206 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
4207 code = info->code_start;
4208 // printf("Discarding code for method %s\n", method->name);
4212 /* The lookup + insert is atomic since this is done inside the domain lock */
4213 mono_domain_jit_code_hash_lock (target_domain);
4214 mono_internal_hash_table_insert (&target_domain->jit_code_hash, cfg->jit_info->d.method, cfg->jit_info);
4215 mono_domain_jit_code_hash_unlock (target_domain);
4217 code = cfg->native_code;
4219 if (cfg->generic_sharing_context && mono_method_is_generic_sharable (method, FALSE))
4220 mono_stats.generics_shared_methods++;
4222 mono_stats.gsharedvt_methods++;
4225 jinfo = cfg->jit_info;
4227 prof_options = cfg->prof_options;
4230 * Update global stats while holding a lock, instead of doing many
4231 * InterlockedIncrement operations during JITting.
4233 mono_jit_stats.allocate_var += cfg->stat_allocate_var;
4234 mono_jit_stats.locals_stack_size += cfg->stat_locals_stack_size;
4235 mono_jit_stats.basic_blocks += cfg->stat_basic_blocks;
4236 mono_jit_stats.max_basic_blocks = MAX (cfg->stat_basic_blocks, mono_jit_stats.max_basic_blocks);
4237 mono_jit_stats.cil_code_size += cfg->stat_cil_code_size;
4238 mono_jit_stats.regvars += cfg->stat_n_regvars;
4239 mono_jit_stats.inlineable_methods += cfg->stat_inlineable_methods;
4240 mono_jit_stats.inlined_methods += cfg->stat_inlined_methods;
4241 mono_jit_stats.cas_demand_generation += cfg->stat_cas_demand_generation;
4242 mono_jit_stats.code_reallocs += cfg->stat_code_reallocs;
4244 mono_destroy_compile (cfg);
4247 if (domain_jit_info (target_domain)->jump_target_hash) {
4248 MonoJumpInfo patch_info;
4249 MonoJumpList *jlist;
4251 jlist = g_hash_table_lookup (domain_jit_info (target_domain)->jump_target_hash, method);
4253 patch_info.next = NULL;
4254 patch_info.ip.i = 0;
4255 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
4256 patch_info.data.method = method;
4257 g_hash_table_remove (domain_jit_info (target_domain)->jump_target_hash, method);
4259 #if defined(__native_client_codegen__) && defined(__native_client__)
4260 /* These patches are applied after a method has been installed, no target munging is needed. */
4261 nacl_allow_target_modification (FALSE);
4263 for (tmp = jlist->list; tmp; tmp = tmp->next)
4264 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, NULL, TRUE);
4265 #if defined(__native_client_codegen__) && defined(__native_client__)
4266 nacl_allow_target_modification (TRUE);
4271 mono_emit_jit_map (jinfo);
4273 mono_domain_unlock (target_domain);
4275 vtable = mono_class_vtable (target_domain, method->klass);
4277 ex = mono_class_get_exception_for_failure (method->klass);
4283 if (prof_options & MONO_PROFILE_JIT_COMPILATION) {
4284 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
4285 if (mono_marshal_method_from_wrapper (method)) {
4286 /* Native func wrappers have no method */
4287 /* The profiler doesn't know about wrappers, so pass the original icall method */
4288 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method), jinfo, MONO_PROFILE_OK);
4291 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
4292 if (prof_method != method) {
4293 mono_profiler_method_end_jit (prof_method, jinfo, MONO_PROFILE_OK);
4297 ex = mono_runtime_class_init_full (vtable, FALSE);
4308 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments) {
4309 return mono_arch_instrument_epilog_full (cfg, func, p, enable_arguments, FALSE);
4313 mono_cfg_add_try_hole (MonoCompile *cfg, MonoExceptionClause *clause, guint8 *start, MonoBasicBlock *bb)
4315 TryBlockHole *hole = mono_mempool_alloc (cfg->mempool, sizeof (TryBlockHole));
4316 hole->clause = clause;
4317 hole->start_offset = start - cfg->native_code;
4318 hole->basic_block = bb;
4320 cfg->try_block_holes = g_slist_append_mempool (cfg->mempool, cfg->try_block_holes, hole);
4324 mono_cfg_set_exception (MonoCompile *cfg, int type)
4326 cfg->exception_type = type;
4331 /* Dummy versions of some arch specific functions to avoid ifdefs at call sites */
4333 #ifndef MONO_ARCH_GSHAREDVT_SUPPORTED
4336 mono_arch_gsharedvt_sig_supported (MonoMethodSignature *sig)
4342 mono_arch_get_gsharedvt_call_info (gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig, MonoGenericSharingContext *gsctx, gboolean gsharedvt_in, gint32 vcall_offset, gboolean calli)
4344 g_assert_not_reached ();
4349 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
4351 g_assert_not_reached ();
4356 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
4358 g_assert_not_reached ();
4364 #if defined(MONO_ARCH_GSHAREDVT_SUPPORTED) && !defined(ENABLE_GSHAREDVT)
4367 mono_arch_gsharedvt_sig_supported (MonoMethodSignature *sig)
4373 mono_arch_get_gsharedvt_call_info (gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig, MonoGenericSharingContext *gsctx, gboolean gsharedvt_in, gint32 vcall_offset, gboolean calli)
4381 #ifdef USE_JUMP_TABLES
4382 #define DEFAULT_JUMPTABLE_CHUNK_ELEMENTS 128
4384 typedef struct MonoJumpTableChunk {
4387 struct MonoJumpTableChunk *previous;
4388 /* gpointer entries[total]; */
4389 } MonoJumpTableChunk;
4391 static MonoJumpTableChunk* g_jumptable;
4392 #define mono_jumptable_lock() mono_mutex_lock (&jumptable_mutex)
4393 #define mono_jumptable_unlock() mono_mutex_unlock (&jumptable_mutex)
4394 static mono_mutex_t jumptable_mutex;
4396 static MonoJumpTableChunk*
4397 mono_create_jumptable_chunk (guint32 max_entries)
4399 guint32 size = sizeof (MonoJumpTableChunk) + max_entries * sizeof(gpointer);
4400 MonoJumpTableChunk *chunk = (MonoJumpTableChunk*) g_new0 (guchar, size);
4401 chunk->total = max_entries;
4406 mono_jumptable_init (void)
4408 if (g_jumptable == NULL) {
4409 mono_mutex_init_recursive (&jumptable_mutex);
4410 g_jumptable = mono_create_jumptable_chunk (DEFAULT_JUMPTABLE_CHUNK_ELEMENTS);
4415 mono_jumptable_add_entry (void)
4417 return mono_jumptable_add_entries (1);
4421 mono_jumptable_add_entries (guint32 entries)
4426 mono_jumptable_init ();
4427 mono_jumptable_lock ();
4428 index = g_jumptable->active;
4429 if (index + entries >= g_jumptable->total) {
4431 * Grow jumptable, by adding one more chunk.
4432 * We cannot realloc jumptable, as there could be pointers
4433 * to existing jump table entries in the code, so instead
4434 * we just add one more chunk.
4436 guint32 max_entries = entries;
4437 MonoJumpTableChunk *new_chunk;
4439 if (max_entries < DEFAULT_JUMPTABLE_CHUNK_ELEMENTS)
4440 max_entries = DEFAULT_JUMPTABLE_CHUNK_ELEMENTS;
4441 new_chunk = mono_create_jumptable_chunk (max_entries);
4442 /* Link old jumptable, so that we could free it up later. */
4443 new_chunk->previous = g_jumptable;
4444 g_jumptable = new_chunk;
4447 g_jumptable->active = index + entries;
4448 result = (gpointer*)((guchar*)g_jumptable + sizeof(MonoJumpTableChunk)) + index;
4449 mono_jumptable_unlock();
4455 mono_jumptable_cleanup (void)
4458 MonoJumpTableChunk *current = g_jumptable, *prev;
4459 while (current != NULL) {
4460 prev = current->previous;
4465 mono_mutex_destroy (&jumptable_mutex);
4470 mono_jumptable_get_entry (guint8 *code_ptr)
4472 return mono_arch_jumptable_entry_from_code (code_ptr);
4477 * mini_replace_type:
4479 * Replace the type used in the metadata stream with what the JIT will actually use during compilation.
4482 mini_replace_type (MonoType *type)
4484 type = mono_type_get_underlying_type (type);
4485 return mini_native_type_replace_type (type);
4489 * mini_get_underlying_type:
4491 * Return the type the JIT will use during compilation.
4492 * Handles: byref, enums, native types, generic sharing.
4493 * For gsharedvt types, it will return the original VAR/MVAR.
4496 mini_get_underlying_type (MonoCompile *cfg, MonoType *type)
4498 type = mini_type_get_underlying_type (cfg->generic_sharing_context, type);
4499 return mini_native_type_replace_type (type);
4503 mini_jit_init (void)
4505 mono_mutex_init_recursive (&jit_mutex);
4509 mini_jit_cleanup (void)
4511 g_free (emul_opcode_map);
4512 g_free (emul_opcode_opcodes);