2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
20 #ifdef HAVE_SYS_TIME_H
24 #include <mono/utils/memcheck.h>
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/threads.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.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/utils/mono-math.h>
49 #include <mono/utils/mono-compiler.h>
50 #include <mono/utils/mono-counters.h>
51 #include <mono/utils/mono-logger-internal.h>
52 #include <mono/utils/mono-mmap.h>
53 #include <mono/utils/dtrace.h>
62 #include "jit-icalls.h"
64 #include "debug-mini.h"
66 #include "debugger-agent.h"
68 static gpointer
mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
);
70 /* helper methods signature */
71 /* FIXME: Make these static again */
72 MonoMethodSignature
*helper_sig_class_init_trampoline
= NULL
;
73 MonoMethodSignature
*helper_sig_domain_get
= NULL
;
74 MonoMethodSignature
*helper_sig_generic_class_init_trampoline
= NULL
;
75 MonoMethodSignature
*helper_sig_rgctx_lazy_fetch_trampoline
= NULL
;
76 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline
= NULL
;
77 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline_llvm
= NULL
;
79 static guint32 default_opt
= 0;
80 static gboolean default_opt_set
= FALSE
;
82 guint32 mono_jit_tls_id
= -1;
85 static __thread gpointer mono_jit_tls MONO_TLS_FAST
;
88 MonoTraceSpec
*mono_jit_trace_calls
= NULL
;
89 gboolean mono_break_on_exc
= FALSE
;
90 gboolean mono_compile_aot
= FALSE
;
91 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
92 gboolean mono_aot_only
= FALSE
;
93 /* Whenever to use IMT */
94 #ifdef MONO_ARCH_HAVE_IMT
95 gboolean mono_use_imt
= TRUE
;
97 gboolean mono_use_imt
= FALSE
;
99 MonoMethodDesc
*mono_inject_async_exc_method
= NULL
;
100 int mono_inject_async_exc_pos
;
101 MonoMethodDesc
*mono_break_at_bb_method
= NULL
;
102 int mono_break_at_bb_bb_num
;
103 gboolean mono_do_x86_stack_align
= TRUE
;
104 const char *mono_build_date
;
105 gboolean mono_do_signal_chaining
;
106 static gboolean mono_using_xdebug
;
107 static int mini_verbose
= 0;
111 static int methods_with_llvm
, methods_without_llvm
;
115 * This flag controls whenever the runtime uses LLVM compiled code.
116 * Enabling this causes different/slower code paths to be used, which is why it
118 * Changes when this flag is set include:
119 * - a per method vtable trampoline is used to handle virtual calls, instead of only
121 * - fast generic virtual calls are not supported.
124 gboolean mono_use_llvm
= TRUE
;
126 gboolean mono_use_llvm
= FALSE
;
129 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
130 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
131 static CRITICAL_SECTION jit_mutex
;
133 static MonoCodeManager
*global_codeman
= NULL
;
135 static GHashTable
*jit_icall_name_hash
= NULL
;
137 static MonoDebugOptions debug_options
;
139 #ifdef VALGRIND_JIT_REGISTER_MAP
140 static int valgrind_register
= 0;
144 * Table written to by the debugger with a 1-based index into the
145 * mono_breakpoint_info table, which contains changes made to
146 * the JIT instructions by the debugger.
149 mono_breakpoint_info_index
[MONO_BREAKPOINT_ARRAY_SIZE
];
151 /* Whenever to check for pending exceptions in managed-to-native wrappers */
152 gboolean check_for_pending_exc
= TRUE
;
154 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
155 gboolean disable_vtypes_in_regs
= FALSE
;
157 gboolean mono_dont_free_global_codeman
;
160 mono_running_on_valgrind (void)
162 if (RUNNING_ON_VALGRIND
){
163 #ifdef VALGRIND_JIT_REGISTER_MAP
164 valgrind_register
= TRUE
;
172 MonoExceptionClause
*clause
;
173 MonoBasicBlock
*basic_block
;
183 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
185 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
188 ud
->method
= (MonoMethod
*)key
;
192 G_GNUC_UNUSED
static char*
193 get_method_from_ip (void *ip
)
198 MonoDomain
*domain
= mono_domain_get ();
199 MonoDebugSourceLocation
*location
;
200 FindTrampUserData user_data
;
202 ji
= mono_jit_info_table_find (domain
, ip
);
205 user_data
.method
= NULL
;
206 mono_domain_lock (domain
);
207 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
208 mono_domain_unlock (domain
);
209 if (user_data
.method
) {
210 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
211 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
218 method
= mono_method_full_name (ji
->method
, TRUE
);
219 /* FIXME: unused ? */
220 location
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
222 res
= g_strdup_printf (" %s + 0x%x (%p %p) [%p - %s]", method
, (int)((char*)ip
- (char*)ji
->code_start
), ji
->code_start
, (char*)ji
->code_start
+ ji
->code_size
, domain
, domain
->friendly_name
);
224 mono_debug_free_source_location (location
);
232 * @ip: an instruction pointer address
234 * This method is used from a debugger to get the name of the
235 * method at address @ip. This routine is typically invoked from
236 * a debugger like this:
238 * (gdb) print mono_pmip ($pc)
240 * Returns: the name of the method at address @ip.
245 return get_method_from_ip (ip
);
249 * mono_print_method_from_ip
250 * @ip: an instruction pointer address
252 * This method is used from a debugger to get the name of the
253 * method at address @ip.
255 * This prints the name of the method at address @ip in the standard
256 * output. Unlike mono_pmip which returns a string, this routine
257 * prints the value on the standard output.
260 mono_print_method_from_ip (void *ip
)
264 MonoDebugSourceLocation
*source
;
265 MonoDomain
*domain
= mono_domain_get ();
266 MonoDomain
*target_domain
= mono_domain_get ();
267 FindTrampUserData user_data
;
269 ji
= mini_jit_info_table_find (domain
, ip
, &target_domain
);
272 user_data
.method
= NULL
;
273 mono_domain_lock (domain
);
274 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
275 mono_domain_unlock (domain
);
276 if (user_data
.method
) {
277 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
278 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
282 g_print ("No method at %p\n", ip
);
285 method
= mono_method_full_name (ji
->method
, TRUE
);
286 source
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
288 g_print ("IP %p at offset 0x%x of method %s (%p %p)[domain %p - %s]\n", ip
, (int)((char*)ip
- (char*)ji
->code_start
), method
, ji
->code_start
, (char*)ji
->code_start
+ ji
->code_size
, target_domain
, target_domain
->friendly_name
);
291 g_print ("%s:%d\n", source
->source_file
, source
->row
);
293 mono_debug_free_source_location (source
);
298 * mono_method_same_domain:
300 * Determine whenever two compiled methods are in the same domain, thus
301 * the address of the callee can be embedded in the caller.
303 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
305 if (!caller
|| !callee
)
309 * If the call was made from domain-neutral to domain-specific
310 * code, we can't patch the call site.
312 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
315 if ((caller
->method
->klass
== mono_defaults
.appdomain_class
) &&
316 (strstr (caller
->method
->name
, "InvokeInDomain"))) {
317 /* The InvokeInDomain methods change the current appdomain */
325 * mono_global_codeman_reserve:
327 * Allocate code memory from the global code manager.
329 void *mono_global_codeman_reserve (int size
)
334 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
336 if (!global_codeman
) {
337 /* This can happen during startup */
338 global_codeman
= mono_code_manager_new ();
339 return mono_code_manager_reserve (global_codeman
, size
);
343 ptr
= mono_code_manager_reserve (global_codeman
, size
);
350 * mono_create_unwind_op:
352 * Create an unwind op with the given parameters.
355 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
357 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
368 * mono_emit_unwind_op:
370 * Add an unwind op with the given parameters for the list of unwind ops stored in
374 mono_emit_unwind_op (MonoCompile
*cfg
, int when
, int tag
, int reg
, int val
)
376 MonoUnwindOp
*op
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoUnwindOp
));
383 cfg
->unwind_ops
= g_slist_append_mempool (cfg
->mempool
, cfg
->unwind_ops
, op
);
387 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
389 MonoJumpInfoToken
*res
= mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
392 res
->has_context
= context
!= NULL
;
394 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
400 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
402 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
406 * mono_tramp_info_create:
408 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
409 * of NAME, JI, and UNWIND_OPS.
412 mono_tramp_info_create (const char *name
, guint8
*code
, guint32 code_size
, MonoJumpInfo
*ji
, GSList
*unwind_ops
)
414 MonoTrampInfo
*info
= g_new0 (MonoTrampInfo
, 1);
416 info
->name
= (char*)name
;
418 info
->code_size
= code_size
;
420 info
->unwind_ops
= unwind_ops
;
426 mono_tramp_info_free (MonoTrampInfo
*info
)
430 // FIXME: ji + unwind_ops
433 #define MONO_INIT_VARINFO(vi,id) do { \
434 (vi)->range.first_use.pos.bid = 0xffff; \
440 * mono_unlink_bblock:
442 * Unlink two basic blocks.
445 mono_unlink_bblock (MonoCompile
*cfg
, MonoBasicBlock
*from
, MonoBasicBlock
* to
)
451 for (i
= 0; i
< from
->out_count
; ++i
) {
452 if (to
== from
->out_bb
[i
]) {
459 for (i
= 0; i
< from
->out_count
; ++i
) {
460 if (from
->out_bb
[i
] != to
)
461 from
->out_bb
[pos
++] = from
->out_bb
[i
];
463 g_assert (pos
== from
->out_count
- 1);
468 for (i
= 0; i
< to
->in_count
; ++i
) {
469 if (from
== to
->in_bb
[i
]) {
476 for (i
= 0; i
< to
->in_count
; ++i
) {
477 if (to
->in_bb
[i
] != from
)
478 to
->in_bb
[pos
++] = to
->in_bb
[i
];
480 g_assert (pos
== to
->in_count
- 1);
486 * mono_bblocks_linked:
488 * Return whenever BB1 and BB2 are linked in the CFG.
491 mono_bblocks_linked (MonoBasicBlock
*bb1
, MonoBasicBlock
*bb2
)
495 for (i
= 0; i
< bb1
->out_count
; ++i
) {
496 if (bb1
->out_bb
[i
] == bb2
)
504 mono_find_block_region_notry (MonoCompile
*cfg
, int offset
)
506 MonoMethodHeader
*header
= cfg
->header
;
507 MonoExceptionClause
*clause
;
510 for (i
= 0; i
< header
->num_clauses
; ++i
) {
511 clause
= &header
->clauses
[i
];
512 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) && (offset
>= clause
->data
.filter_offset
) &&
513 (offset
< (clause
->handler_offset
)))
514 return ((i
+ 1) << 8) | MONO_REGION_FILTER
| clause
->flags
;
516 if (MONO_OFFSET_IN_HANDLER (clause
, offset
)) {
517 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
518 return ((i
+ 1) << 8) | MONO_REGION_FINALLY
| clause
->flags
;
519 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
)
520 return ((i
+ 1) << 8) | MONO_REGION_FAULT
| clause
->flags
;
522 return ((i
+ 1) << 8) | MONO_REGION_CATCH
| clause
->flags
;
530 * mono_get_block_region_notry:
532 * Return the region corresponding to REGION, ignoring try clauses nested inside
536 mono_get_block_region_notry (MonoCompile
*cfg
, int region
)
538 if ((region
& (0xf << 4)) == MONO_REGION_TRY
) {
539 MonoMethodHeader
*header
= cfg
->header
;
542 * This can happen if a try clause is nested inside a finally clause.
544 int clause_index
= (region
>> 8) - 1;
545 g_assert (clause_index
>= 0 && clause_index
< header
->num_clauses
);
547 region
= mono_find_block_region_notry (cfg
, header
->clauses
[clause_index
].try_offset
);
554 mono_find_spvar_for_region (MonoCompile
*cfg
, int region
)
556 region
= mono_get_block_region_notry (cfg
, region
);
558 return g_hash_table_lookup (cfg
->spvars
, GINT_TO_POINTER (region
));
562 df_visit (MonoBasicBlock
*start
, int *dfn
, MonoBasicBlock
**array
)
566 array
[*dfn
] = start
;
567 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
568 for (i
= 0; i
< start
->out_count
; ++i
) {
569 if (start
->out_bb
[i
]->dfn
)
572 start
->out_bb
[i
]->dfn
= *dfn
;
573 start
->out_bb
[i
]->df_parent
= start
;
574 array
[*dfn
] = start
->out_bb
[i
];
575 df_visit (start
->out_bb
[i
], dfn
, array
);
580 mono_reverse_branch_op (guint32 opcode
)
582 static const int reverse_map
[] = {
583 CEE_BNE_UN
, CEE_BLT
, CEE_BLE
, CEE_BGT
, CEE_BGE
,
584 CEE_BEQ
, CEE_BLT_UN
, CEE_BLE_UN
, CEE_BGT_UN
, CEE_BGE_UN
586 static const int reverse_fmap
[] = {
587 OP_FBNE_UN
, OP_FBLT
, OP_FBLE
, OP_FBGT
, OP_FBGE
,
588 OP_FBEQ
, OP_FBLT_UN
, OP_FBLE_UN
, OP_FBGT_UN
, OP_FBGE_UN
590 static const int reverse_lmap
[] = {
591 OP_LBNE_UN
, OP_LBLT
, OP_LBLE
, OP_LBGT
, OP_LBGE
,
592 OP_LBEQ
, OP_LBLT_UN
, OP_LBLE_UN
, OP_LBGT_UN
, OP_LBGE_UN
594 static const int reverse_imap
[] = {
595 OP_IBNE_UN
, OP_IBLT
, OP_IBLE
, OP_IBGT
, OP_IBGE
,
596 OP_IBEQ
, OP_IBLT_UN
, OP_IBLE_UN
, OP_IBGT_UN
, OP_IBGE_UN
599 if (opcode
>= CEE_BEQ
&& opcode
<= CEE_BLT_UN
) {
600 opcode
= reverse_map
[opcode
- CEE_BEQ
];
601 } else if (opcode
>= OP_FBEQ
&& opcode
<= OP_FBLT_UN
) {
602 opcode
= reverse_fmap
[opcode
- OP_FBEQ
];
603 } else if (opcode
>= OP_LBEQ
&& opcode
<= OP_LBLT_UN
) {
604 opcode
= reverse_lmap
[opcode
- OP_LBEQ
];
605 } else if (opcode
>= OP_IBEQ
&& opcode
<= OP_IBLT_UN
) {
606 opcode
= reverse_imap
[opcode
- OP_IBEQ
];
608 g_assert_not_reached ();
614 mono_type_to_store_membase (MonoCompile
*cfg
, MonoType
*type
)
617 return OP_STORE_MEMBASE_REG
;
620 switch (type
->type
) {
623 case MONO_TYPE_BOOLEAN
:
624 return OP_STOREI1_MEMBASE_REG
;
628 return OP_STOREI2_MEMBASE_REG
;
631 return OP_STOREI4_MEMBASE_REG
;
635 case MONO_TYPE_FNPTR
:
636 return OP_STORE_MEMBASE_REG
;
637 case MONO_TYPE_CLASS
:
638 case MONO_TYPE_STRING
:
639 case MONO_TYPE_OBJECT
:
640 case MONO_TYPE_SZARRAY
:
641 case MONO_TYPE_ARRAY
:
642 return OP_STORE_MEMBASE_REG
;
645 return OP_STOREI8_MEMBASE_REG
;
647 return OP_STORER4_MEMBASE_REG
;
649 return OP_STORER8_MEMBASE_REG
;
650 case MONO_TYPE_VALUETYPE
:
651 if (type
->data
.klass
->enumtype
) {
652 type
= mono_class_enum_basetype (type
->data
.klass
);
655 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
656 return OP_STOREX_MEMBASE
;
657 return OP_STOREV_MEMBASE
;
658 case MONO_TYPE_TYPEDBYREF
:
659 return OP_STOREV_MEMBASE
;
660 case MONO_TYPE_GENERICINST
:
661 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
665 /* FIXME: all the arguments must be references for now,
666 * later look inside cfg and see if the arg num is
669 g_assert (cfg
->generic_sharing_context
);
670 return OP_STORE_MEMBASE_REG
;
672 g_error ("unknown type 0x%02x in type_to_store_membase", type
->type
);
678 mono_type_to_load_membase (MonoCompile
*cfg
, MonoType
*type
)
681 return OP_LOAD_MEMBASE
;
683 type
= mono_type_get_underlying_type (type
);
685 switch (type
->type
) {
687 return OP_LOADI1_MEMBASE
;
689 case MONO_TYPE_BOOLEAN
:
690 return OP_LOADU1_MEMBASE
;
692 return OP_LOADI2_MEMBASE
;
695 return OP_LOADU2_MEMBASE
;
697 return OP_LOADI4_MEMBASE
;
699 return OP_LOADU4_MEMBASE
;
703 case MONO_TYPE_FNPTR
:
704 return OP_LOAD_MEMBASE
;
705 case MONO_TYPE_CLASS
:
706 case MONO_TYPE_STRING
:
707 case MONO_TYPE_OBJECT
:
708 case MONO_TYPE_SZARRAY
:
709 case MONO_TYPE_ARRAY
:
710 return OP_LOAD_MEMBASE
;
713 return OP_LOADI8_MEMBASE
;
715 return OP_LOADR4_MEMBASE
;
717 return OP_LOADR8_MEMBASE
;
718 case MONO_TYPE_VALUETYPE
:
719 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
720 return OP_LOADX_MEMBASE
;
721 case MONO_TYPE_TYPEDBYREF
:
722 return OP_LOADV_MEMBASE
;
723 case MONO_TYPE_GENERICINST
:
724 if (mono_type_generic_inst_is_valuetype (type
))
725 return OP_LOADV_MEMBASE
;
727 return OP_LOAD_MEMBASE
;
731 /* FIXME: all the arguments must be references for now,
732 * later look inside cfg and see if the arg num is
735 g_assert (cfg
->generic_sharing_context
);
736 return OP_LOAD_MEMBASE
;
738 g_error ("unknown type 0x%02x in type_to_load_membase", type
->type
);
744 mini_type_to_ldind (MonoCompile
* cfg
, MonoType
*type
)
746 if (cfg
->generic_sharing_context
&& !type
->byref
) {
747 /* FIXME: all the arguments must be references for now,
748 * later look inside cfg and see if the arg num is
751 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
752 return CEE_LDIND_REF
;
754 return mono_type_to_ldind (type
);
758 mini_type_to_stind (MonoCompile
* cfg
, MonoType
*type
)
760 if (cfg
->generic_sharing_context
&& !type
->byref
) {
761 /* FIXME: all the arguments must be references for now,
762 * later look inside cfg and see if the arg num is
765 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
766 return CEE_STIND_REF
;
768 return mono_type_to_stind (type
);
772 mono_op_imm_to_op (int opcode
)
776 #if SIZEOF_REGISTER == 4
792 #if SIZEOF_REGISTER == 4
798 #if SIZEOF_REGISTER == 4
804 #if SIZEOF_REGISTER == 4
842 #if SIZEOF_REGISTER == 4
848 #if SIZEOF_REGISTER == 4
867 case OP_ICOMPARE_IMM
:
869 case OP_LOCALLOC_IMM
:
872 printf ("%s\n", mono_inst_name (opcode
));
873 g_assert_not_reached ();
879 * mono_decompose_op_imm:
881 * Replace the OP_.._IMM INS with its non IMM variant.
884 mono_decompose_op_imm (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
888 MONO_INST_NEW (cfg
, temp
, OP_ICONST
);
889 temp
->inst_c0
= ins
->inst_imm
;
890 temp
->dreg
= mono_alloc_ireg (cfg
);
891 mono_bblock_insert_before_ins (bb
, ins
, temp
);
892 ins
->opcode
= mono_op_imm_to_op (ins
->opcode
);
893 if (ins
->opcode
== OP_LOCALLOC
)
894 ins
->sreg1
= temp
->dreg
;
896 ins
->sreg2
= temp
->dreg
;
898 bb
->max_vreg
= MAX (bb
->max_vreg
, cfg
->next_vreg
);
902 set_vreg_to_inst (MonoCompile
*cfg
, int vreg
, MonoInst
*inst
)
904 if (vreg
>= cfg
->vreg_to_inst_len
) {
905 MonoInst
**tmp
= cfg
->vreg_to_inst
;
906 int size
= cfg
->vreg_to_inst_len
;
908 while (vreg
>= cfg
->vreg_to_inst_len
)
909 cfg
->vreg_to_inst_len
= cfg
->vreg_to_inst_len
? cfg
->vreg_to_inst_len
* 2 : 32;
910 cfg
->vreg_to_inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
*) * cfg
->vreg_to_inst_len
);
912 memcpy (cfg
->vreg_to_inst
, tmp
, size
* sizeof (MonoInst
*));
914 cfg
->vreg_to_inst
[vreg
] = inst
;
917 #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)))
918 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
923 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
931 mono_compile_create_var_for_vreg (MonoCompile
*cfg
, MonoType
*type
, int opcode
, int vreg
)
934 int num
= cfg
->num_varinfo
;
937 if ((num
+ 1) >= cfg
->varinfo_count
) {
938 int orig_count
= cfg
->varinfo_count
;
939 cfg
->varinfo_count
= cfg
->varinfo_count
? (cfg
->varinfo_count
* 2) : 64;
940 cfg
->varinfo
= (MonoInst
**)g_realloc (cfg
->varinfo
, sizeof (MonoInst
*) * cfg
->varinfo_count
);
941 cfg
->vars
= (MonoMethodVar
*)g_realloc (cfg
->vars
, sizeof (MonoMethodVar
) * cfg
->varinfo_count
);
942 memset (&cfg
->vars
[orig_count
], 0, (cfg
->varinfo_count
- orig_count
) * sizeof (MonoMethodVar
));
945 mono_jit_stats
.allocate_var
++;
947 MONO_INST_NEW (cfg
, inst
, opcode
);
949 inst
->inst_vtype
= type
;
950 inst
->klass
= mono_class_from_mono_type (type
);
951 type_to_eval_stack_type (cfg
, type
, inst
);
952 /* if set to 1 the variable is native */
953 inst
->backend
.is_pinvoke
= 0;
956 cfg
->varinfo
[num
] = inst
;
958 MONO_INIT_VARINFO (&cfg
->vars
[num
], num
);
959 MONO_VARINFO (cfg
, num
)->vreg
= vreg
;
962 set_vreg_to_inst (cfg
, vreg
, inst
);
964 #if SIZEOF_REGISTER == 4
965 #ifdef MONO_ARCH_SOFT_FLOAT
966 regpair
= mono_type_is_long (type
) || mono_type_is_float (type
);
968 regpair
= mono_type_is_long (type
);
978 * These two cannot be allocated using create_var_for_vreg since that would
979 * put it into the cfg->varinfo array, confusing many parts of the JIT.
983 * Set flags to VOLATILE so SSA skips it.
986 if (cfg
->verbose_level
>= 4) {
987 printf (" Create LVAR R%d (R%d, R%d)\n", inst
->dreg
, inst
->dreg
+ 1, inst
->dreg
+ 2);
990 #ifdef MONO_ARCH_SOFT_FLOAT
991 if (cfg
->opt
& MONO_OPT_SSA
) {
992 if (mono_type_is_float (type
))
993 inst
->flags
= MONO_INST_VOLATILE
;
997 /* Allocate a dummy MonoInst for the first vreg */
998 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
999 tree
->dreg
= inst
->dreg
+ 1;
1000 if (cfg
->opt
& MONO_OPT_SSA
)
1001 tree
->flags
= MONO_INST_VOLATILE
;
1002 tree
->inst_c0
= num
;
1003 tree
->type
= STACK_I4
;
1004 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1005 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1007 set_vreg_to_inst (cfg
, inst
->dreg
+ 1, tree
);
1009 /* Allocate a dummy MonoInst for the second vreg */
1010 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
1011 tree
->dreg
= inst
->dreg
+ 2;
1012 if (cfg
->opt
& MONO_OPT_SSA
)
1013 tree
->flags
= MONO_INST_VOLATILE
;
1014 tree
->inst_c0
= num
;
1015 tree
->type
= STACK_I4
;
1016 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1017 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1019 set_vreg_to_inst (cfg
, inst
->dreg
+ 2, tree
);
1023 if (cfg
->verbose_level
> 2)
1024 g_print ("created temp %d (R%d) of type %s\n", num
, vreg
, mono_type_get_name (type
));
1029 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1033 if (mono_type_is_long (type
))
1034 dreg
= mono_alloc_dreg (cfg
, STACK_I8
);
1035 #ifdef MONO_ARCH_SOFT_FLOAT
1036 else if (mono_type_is_float (type
))
1037 dreg
= mono_alloc_dreg (cfg
, STACK_R8
);
1040 /* All the others are unified */
1041 dreg
= mono_alloc_preg (cfg
);
1043 return mono_compile_create_var_for_vreg (cfg
, type
, opcode
, dreg
);
1047 * Transform a MonoInst into a load from the variable of index var_index.
1050 mono_compile_make_var_load (MonoCompile
*cfg
, MonoInst
*dest
, gssize var_index
) {
1051 memset (dest
, 0, sizeof (MonoInst
));
1052 dest
->inst_i0
= cfg
->varinfo
[var_index
];
1053 dest
->opcode
= mini_type_to_ldind (cfg
, dest
->inst_i0
->inst_vtype
);
1054 type_to_eval_stack_type (cfg
, dest
->inst_i0
->inst_vtype
, dest
);
1055 dest
->klass
= dest
->inst_i0
->klass
;
1061 type_from_stack_type (MonoInst
*ins
) {
1062 switch (ins
->type
) {
1063 case STACK_I4
: return &mono_defaults
.int32_class
->byval_arg
;
1064 case STACK_I8
: return &mono_defaults
.int64_class
->byval_arg
;
1065 case STACK_PTR
: return &mono_defaults
.int_class
->byval_arg
;
1066 case STACK_R8
: return &mono_defaults
.double_class
->byval_arg
;
1069 * this if used to be commented without any specific reason, but
1070 * it breaks #80235 when commented
1073 return &ins
->klass
->this_arg
;
1075 return &mono_defaults
.object_class
->this_arg
;
1077 /* ins->klass may not be set for ldnull.
1078 * Also, if we have a boxed valuetype, we want an object lass,
1079 * not the valuetype class
1081 if (ins
->klass
&& !ins
->klass
->valuetype
)
1082 return &ins
->klass
->byval_arg
;
1083 return &mono_defaults
.object_class
->byval_arg
;
1084 case STACK_VTYPE
: return &ins
->klass
->byval_arg
;
1086 g_error ("stack type %d to montype not handled\n", ins
->type
);
1092 mono_type_from_stack_type (MonoInst
*ins
) {
1093 return type_from_stack_type (ins
);
1097 * mono_add_ins_to_end:
1099 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
1102 mono_add_ins_to_end (MonoBasicBlock
*bb
, MonoInst
*inst
)
1107 MONO_ADD_INS (bb
, inst
);
1111 switch (bb
->last_ins
->opcode
) {
1125 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1128 if (MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1129 /* Need to insert the ins before the compare */
1130 if (bb
->code
== bb
->last_ins
) {
1131 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1135 if (bb
->code
->next
== bb
->last_ins
) {
1136 /* Only two instructions */
1137 opcode
= bb
->code
->opcode
;
1139 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
)) {
1141 mono_bblock_insert_before_ins (bb
, bb
->code
, inst
);
1143 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1146 opcode
= bb
->last_ins
->prev
->opcode
;
1148 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
)) {
1150 mono_bblock_insert_before_ins (bb
, bb
->last_ins
->prev
, inst
);
1152 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1157 MONO_ADD_INS (bb
, inst
);
1163 mono_create_jump_table (MonoCompile
*cfg
, MonoInst
*label
, MonoBasicBlock
**bbs
, int num_blocks
)
1165 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
1166 MonoJumpInfoBBTable
*table
;
1168 table
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfoBBTable
));
1170 table
->table_size
= num_blocks
;
1172 ji
->ip
.label
= label
;
1173 ji
->type
= MONO_PATCH_INFO_SWITCH
;
1174 ji
->data
.table
= table
;
1175 ji
->next
= cfg
->patch_info
;
1176 cfg
->patch_info
= ji
;
1179 static MonoMethodSignature
*
1180 mono_get_array_new_va_signature (int arity
)
1182 static GHashTable
*sighash
= NULL
;
1183 MonoMethodSignature
*res
;
1188 sighash
= g_hash_table_new (NULL
, NULL
);
1190 else if ((res
= g_hash_table_lookup (sighash
, GINT_TO_POINTER (arity
)))) {
1195 res
= mono_metadata_signature_alloc (mono_defaults
.corlib
, arity
+ 1);
1198 #ifdef MONO_ARCH_VARARG_ICALLS
1199 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1200 res
->call_convention
= MONO_CALL_VARARG
;
1204 res
->call_convention
= MONO_CALL_C
;
1207 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
1208 for (i
= 0; i
< arity
; i
++)
1209 res
->params
[i
+ 1] = &mono_defaults
.int_class
->byval_arg
;
1211 res
->ret
= &mono_defaults
.object_class
->byval_arg
;
1213 g_hash_table_insert (sighash
, GINT_TO_POINTER (arity
), res
);
1220 mono_get_array_new_va_icall (int rank
)
1222 MonoMethodSignature
*esig
;
1223 char icall_name
[256];
1225 MonoJitICallInfo
*info
;
1227 /* Need to register the icall so it gets an icall wrapper */
1228 sprintf (icall_name
, "ves_array_new_va_%d", rank
);
1231 info
= mono_find_jit_icall_by_name (icall_name
);
1233 esig
= mono_get_array_new_va_signature (rank
);
1234 name
= g_strdup (icall_name
);
1235 info
= mono_register_jit_icall (mono_array_new_va
, name
, esig
, FALSE
);
1237 g_hash_table_insert (jit_icall_name_hash
, name
, name
);
1245 mini_class_is_system_array (MonoClass
*klass
)
1247 if (klass
->parent
== mono_defaults
.array_class
)
1254 mini_assembly_can_skip_verification (MonoDomain
*domain
, MonoMethod
*method
)
1256 MonoAssembly
*assembly
= method
->klass
->image
->assembly
;
1257 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1259 if (assembly
->in_gac
|| assembly
->image
== mono_defaults
.corlib
)
1261 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE
)
1263 return mono_assembly_has_skip_verification (assembly
);
1267 * mini_method_verify:
1269 * Verify the method using the new verfier.
1271 * Returns true if the method is invalid.
1274 mini_method_verify (MonoCompile
*cfg
, MonoMethod
*method
)
1277 gboolean is_fulltrust
;
1278 MonoLoaderError
*error
;
1280 if (method
->verification_success
)
1283 is_fulltrust
= mono_verifier_is_method_full_trust (method
);
1285 if (!mono_verifier_is_enabled_for_method (method
))
1288 res
= mono_method_verify_with_current_settings (method
, cfg
->skip_visibility
);
1290 if ((error
= mono_loader_get_last_error ())) {
1291 cfg
->exception_type
= error
->exception_type
;
1293 mono_free_verify_list (res
);
1298 for (tmp
= res
; tmp
; tmp
= tmp
->next
) {
1299 MonoVerifyInfoExtended
*info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1300 if (info
->info
.status
== MONO_VERIFY_ERROR
) {
1301 char *method_name
= mono_method_full_name (method
, TRUE
);
1302 cfg
->exception_type
= info
->exception_type
;
1303 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1304 mono_free_verify_list (res
);
1305 g_free (method_name
);
1308 if (info
->info
.status
== MONO_VERIFY_NOT_VERIFIABLE
&& (!is_fulltrust
|| info
->exception_type
== MONO_EXCEPTION_METHOD_ACCESS
|| info
->exception_type
== MONO_EXCEPTION_FIELD_ACCESS
)) {
1309 char *method_name
= mono_method_full_name (method
, TRUE
);
1310 cfg
->exception_type
= info
->exception_type
;
1311 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1312 mono_free_verify_list (res
);
1313 g_free (method_name
);
1317 mono_free_verify_list (res
);
1319 method
->verification_success
= 1;
1323 /*Returns true is something went wrong*/
1325 mono_compile_is_broken (MonoCompile
*cfg
)
1327 MonoMethod
*method
= cfg
->method
;
1328 MonoMethod
*method_definition
= method
;
1329 gboolean dont_verify
= mini_assembly_can_skip_verification (cfg
->domain
, method
);
1330 dont_verify
|= method
->klass
->image
->assembly
->corlib_internal
;
1332 while (method_definition
->is_inflated
) {
1333 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method_definition
;
1334 method_definition
= imethod
->declaring
;
1337 return !dont_verify
&& mini_method_verify (cfg
, method_definition
);
1341 create_helper_signature (void)
1343 helper_sig_domain_get
= mono_create_icall_signature ("ptr");
1344 helper_sig_class_init_trampoline
= mono_create_icall_signature ("void");
1345 helper_sig_generic_class_init_trampoline
= mono_create_icall_signature ("void");
1346 helper_sig_rgctx_lazy_fetch_trampoline
= mono_create_icall_signature ("ptr ptr");
1347 helper_sig_monitor_enter_exit_trampoline
= mono_create_icall_signature ("void");
1348 helper_sig_monitor_enter_exit_trampoline_llvm
= mono_create_icall_signature ("void object");
1351 static gconstpointer
1352 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
1355 MonoMethod
*wrapper
;
1356 gconstpointer trampoline
;
1357 MonoDomain
*domain
= mono_get_root_domain ();
1359 if (callinfo
->wrapper
) {
1360 return callinfo
->wrapper
;
1363 if (callinfo
->trampoline
)
1364 return callinfo
->trampoline
;
1367 * We use the lock on the root domain instead of the JIT lock to protect
1368 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
1370 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
1371 mono_domain_lock (domain
);
1373 if (callinfo
->trampoline
) {
1374 mono_domain_unlock (domain
);
1375 mono_loader_unlock ();
1376 return callinfo
->trampoline
;
1379 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
1380 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_for_pending_exc
);
1384 trampoline
= mono_compile_method (wrapper
);
1386 trampoline
= mono_create_ftnptr (domain
, mono_create_jit_trampoline_in_domain (domain
, wrapper
));
1387 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
1389 callinfo
->trampoline
= trampoline
;
1391 mono_domain_unlock (domain
);
1392 mono_loader_unlock ();
1394 return callinfo
->trampoline
;
1398 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
1400 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
1404 mono_dynamic_code_hash_insert (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*ji
)
1406 if (!domain_jit_info (domain
)->dynamic_code_hash
)
1407 domain_jit_info (domain
)->dynamic_code_hash
= g_hash_table_new (NULL
, NULL
);
1408 g_hash_table_insert (domain_jit_info (domain
)->dynamic_code_hash
, method
, ji
);
1411 static MonoJitDynamicMethodInfo
*
1412 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
1414 MonoJitDynamicMethodInfo
*res
;
1416 if (domain_jit_info (domain
)->dynamic_code_hash
)
1417 res
= g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1425 GList
*active
, *inactive
;
1430 compare_by_interval_start_pos_func (gconstpointer a
, gconstpointer b
)
1432 MonoMethodVar
*v1
= (MonoMethodVar
*)a
;
1433 MonoMethodVar
*v2
= (MonoMethodVar
*)b
;
1437 else if (v1
->interval
->range
&& v2
->interval
->range
)
1438 return v1
->interval
->range
->from
- v2
->interval
->range
->from
;
1439 else if (v1
->interval
->range
)
1448 #define LSCAN_DEBUG(a) do { a; } while (0)
1450 #define LSCAN_DEBUG(a)
1454 mono_allocate_stack_slots_full2 (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1456 int i
, slot
, offset
, size
;
1461 GList
*vars
= NULL
, *l
, *unhandled
;
1462 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1465 gboolean reuse_slot
;
1467 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg
->method
, TRUE
)));
1469 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1470 vtype_stack_slots
= NULL
;
1473 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1474 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1477 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1478 inst
= cfg
->varinfo
[i
];
1479 vmv
= MONO_VARINFO (cfg
, i
);
1481 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1484 vars
= g_list_prepend (vars
, vmv
);
1487 vars
= g_list_sort (g_list_copy (vars
), compare_by_interval_start_pos_func
);
1492 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1493 MonoMethodVar *current = unhandled->data;
1495 if (current->interval->range) {
1496 g_assert (current->interval->range->from >= i);
1497 i = current->interval->range->from;
1504 for (unhandled
= vars
; unhandled
; unhandled
= unhandled
->next
) {
1505 MonoMethodVar
*current
= unhandled
->data
;
1508 inst
= cfg
->varinfo
[vmv
->idx
];
1510 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1511 * pinvoke wrappers when they call functions returning structures */
1512 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1513 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1518 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1521 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1526 if (cfg
->disable_reuse_stack_slots
)
1529 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1531 case MONO_TYPE_GENERICINST
:
1532 if (!mono_type_generic_inst_is_valuetype (t
)) {
1533 slot_info
= &scalar_stack_slots
[t
->type
];
1537 case MONO_TYPE_VALUETYPE
:
1538 if (!vtype_stack_slots
)
1539 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1540 for (i
= 0; i
< nvtypes
; ++i
)
1541 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1544 slot_info
= &vtype_stack_slots
[i
];
1546 g_assert (nvtypes
< 256);
1547 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1548 slot_info
= &vtype_stack_slots
[nvtypes
];
1551 if (cfg
->disable_reuse_ref_stack_slots
)
1558 #if SIZEOF_REGISTER == 4
1564 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1570 case MONO_TYPE_CLASS
:
1571 case MONO_TYPE_OBJECT
:
1572 case MONO_TYPE_ARRAY
:
1573 case MONO_TYPE_SZARRAY
:
1574 case MONO_TYPE_STRING
:
1575 /* Share non-float stack slots of the same size */
1576 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1577 if (cfg
->disable_reuse_ref_stack_slots
)
1582 slot_info
= &scalar_stack_slots
[t
->type
];
1586 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1590 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1592 if (!current
->interval
->range
) {
1593 if (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
1597 inst
->flags
|= MONO_INST_IS_DEAD
;
1602 pos
= current
->interval
->range
->from
;
1604 LSCAN_DEBUG (printf ("process R%d ", inst
->dreg
));
1605 if (current
->interval
->range
)
1606 LSCAN_DEBUG (mono_linterval_print (current
->interval
));
1607 LSCAN_DEBUG (printf ("\n"));
1609 /* Check for intervals in active which expired or inactive */
1611 /* FIXME: Optimize this */
1614 for (l
= slot_info
->active
; l
!= NULL
; l
= l
->next
) {
1615 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1617 if (v
->interval
->last_range
->to
< pos
) {
1618 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1619 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[v
->idx
]));
1620 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1624 else if (!mono_linterval_covers (v
->interval
, pos
)) {
1625 slot_info
->inactive
= g_list_append (slot_info
->inactive
, v
);
1626 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1627 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg
->varinfo
[v
->idx
]->dreg
));
1634 /* Check for intervals in inactive which expired or active */
1636 /* FIXME: Optimize this */
1639 for (l
= slot_info
->inactive
; l
!= NULL
; l
= l
->next
) {
1640 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1642 if (v
->interval
->last_range
->to
< pos
) {
1643 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1644 // FIXME: Enabling this seems to cause impossible to debug crashes
1645 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1646 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1650 else if (mono_linterval_covers (v
->interval
, pos
)) {
1651 slot_info
->active
= g_list_append (slot_info
->active
, v
);
1652 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1653 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg
->varinfo
[v
->idx
]->dreg
));
1661 * This also handles the case when the variable is used in an
1662 * exception region, as liveness info is not computed there.
1665 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1668 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1669 if (slot_info
->slots
) {
1670 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1672 slot_info
->slots
= slot_info
->slots
->next
;
1675 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1677 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1683 static int count
= 0;
1686 if (count
== atoi (getenv ("COUNT3")))
1687 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1688 if (count
> atoi (getenv ("COUNT3")))
1691 mono_print_ins (inst
);
1696 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst
->dreg
, mono_type_full_name (t
), slot
));
1701 if (slot
== 0xffffff) {
1703 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1704 * efficient copying (and to work around the fact that OP_MEMCPY
1705 * and OP_MEMSET ignores alignment).
1707 if (MONO_TYPE_ISSTRUCT (t
)) {
1708 align
= MAX (align
, sizeof (gpointer
));
1709 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1714 offset
+= align
- 1;
1715 offset
&= ~(align
- 1);
1719 offset
+= align
- 1;
1720 offset
&= ~(align
- 1);
1725 if (*stack_align
== 0)
1726 *stack_align
= align
;
1729 offsets
[vmv
->idx
] = slot
;
1732 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1733 if (scalar_stack_slots
[i
].active
)
1734 g_list_free (scalar_stack_slots
[i
].active
);
1736 for (i
= 0; i
< nvtypes
; ++i
) {
1737 if (vtype_stack_slots
[i
].active
)
1738 g_list_free (vtype_stack_slots
[i
].active
);
1741 mono_jit_stats
.locals_stack_size
+= offset
;
1743 *stack_size
= offset
;
1748 * mono_allocate_stack_slots_full:
1750 * Allocate stack slots for all non register allocated variables using a
1751 * linear scan algorithm.
1752 * Returns: an array of stack offsets.
1753 * STACK_SIZE is set to the amount of stack space needed.
1754 * STACK_ALIGN is set to the alignment needed by the locals area.
1757 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1759 int i
, slot
, offset
, size
;
1764 GList
*vars
= NULL
, *l
;
1765 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1768 gboolean reuse_slot
;
1770 if ((cfg
->num_varinfo
> 0) && MONO_VARINFO (cfg
, 0)->interval
)
1771 return mono_allocate_stack_slots_full2 (cfg
, backward
, stack_size
, stack_align
);
1773 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1774 vtype_stack_slots
= NULL
;
1777 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1778 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1781 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1782 inst
= cfg
->varinfo
[i
];
1783 vmv
= MONO_VARINFO (cfg
, i
);
1785 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1788 vars
= g_list_prepend (vars
, vmv
);
1791 vars
= mono_varlist_sort (cfg
, vars
, 0);
1793 *stack_align
= sizeof (gpointer
);
1794 for (l
= vars
; l
; l
= l
->next
) {
1796 inst
= cfg
->varinfo
[vmv
->idx
];
1798 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1799 * pinvoke wrappers when they call functions returning structures */
1800 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1801 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1805 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1808 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1813 if (cfg
->disable_reuse_stack_slots
)
1816 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1818 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1821 case MONO_TYPE_GENERICINST
:
1822 if (!mono_type_generic_inst_is_valuetype (t
)) {
1823 slot_info
= &scalar_stack_slots
[t
->type
];
1827 case MONO_TYPE_VALUETYPE
:
1828 if (!vtype_stack_slots
)
1829 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1830 for (i
= 0; i
< nvtypes
; ++i
)
1831 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1834 slot_info
= &vtype_stack_slots
[i
];
1836 g_assert (nvtypes
< 256);
1837 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1838 slot_info
= &vtype_stack_slots
[nvtypes
];
1841 if (cfg
->disable_reuse_ref_stack_slots
)
1848 #if SIZEOF_REGISTER == 4
1854 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1860 case MONO_TYPE_CLASS
:
1861 case MONO_TYPE_OBJECT
:
1862 case MONO_TYPE_ARRAY
:
1863 case MONO_TYPE_SZARRAY
:
1864 case MONO_TYPE_STRING
:
1865 /* Share non-float stack slots of the same size */
1866 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1867 if (cfg
->disable_reuse_ref_stack_slots
)
1872 slot_info
= &scalar_stack_slots
[t
->type
];
1877 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1878 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1880 /* expire old intervals in active */
1881 while (slot_info
->active
) {
1882 MonoMethodVar
*amv
= (MonoMethodVar
*)slot_info
->active
->data
;
1884 if (amv
->range
.last_use
.abs_pos
> vmv
->range
.first_use
.abs_pos
)
1887 //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);
1889 slot_info
->active
= g_list_delete_link (slot_info
->active
, slot_info
->active
);
1890 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[amv
->idx
]));
1894 * This also handles the case when the variable is used in an
1895 * exception region, as liveness info is not computed there.
1898 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1901 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1902 if (slot_info
->slots
) {
1903 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1905 slot_info
->slots
= slot_info
->slots
->next
;
1908 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1913 static int count
= 0;
1917 if (count == atoi (getenv ("COUNT")))
1918 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1919 if (count > atoi (getenv ("COUNT")))
1922 mono_print_ins (inst);
1930 if (slot
== 0xffffff) {
1932 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1933 * efficient copying (and to work around the fact that OP_MEMCPY
1934 * and OP_MEMSET ignores alignment).
1936 if (MONO_TYPE_ISSTRUCT (t
)) {
1937 align
= MAX (align
, sizeof (gpointer
));
1938 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1940 * Align the size too so the code generated for passing vtypes in
1941 * registers doesn't overwrite random locals.
1943 size
= (size
+ (align
- 1)) & ~(align
-1);
1948 offset
+= align
- 1;
1949 offset
&= ~(align
- 1);
1953 offset
+= align
- 1;
1954 offset
&= ~(align
- 1);
1959 *stack_align
= MAX (*stack_align
, align
);
1962 offsets
[vmv
->idx
] = slot
;
1965 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1966 if (scalar_stack_slots
[i
].active
)
1967 g_list_free (scalar_stack_slots
[i
].active
);
1969 for (i
= 0; i
< nvtypes
; ++i
) {
1970 if (vtype_stack_slots
[i
].active
)
1971 g_list_free (vtype_stack_slots
[i
].active
);
1974 mono_jit_stats
.locals_stack_size
+= offset
;
1976 *stack_size
= offset
;
1983 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1985 g_assert_not_reached ();
1989 #endif /* DISABLE_JIT */
1992 mono_allocate_stack_slots (MonoCompile
*m
, guint32
*stack_size
, guint32
*stack_align
)
1994 return mono_allocate_stack_slots_full (m
, TRUE
, stack_size
, stack_align
);
1997 #define EMUL_HIT_SHIFT 3
1998 #define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
1999 /* small hit bitmap cache */
2000 static mono_byte emul_opcode_hit_cache
[(OP_LAST
>>EMUL_HIT_SHIFT
) + 1] = {0};
2001 static short emul_opcode_num
= 0;
2002 static short emul_opcode_alloced
= 0;
2003 static short *emul_opcode_opcodes
= NULL
;
2004 static MonoJitICallInfo
**emul_opcode_map
= NULL
;
2007 mono_find_jit_opcode_emulation (int opcode
)
2009 g_assert (opcode
>= 0 && opcode
<= OP_LAST
);
2010 if (emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] & (1 << (opcode
& EMUL_HIT_MASK
))) {
2012 for (i
= 0; i
< emul_opcode_num
; ++i
) {
2013 if (emul_opcode_opcodes
[i
] == opcode
)
2014 return emul_opcode_map
[i
];
2021 mono_register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, gboolean no_throw
)
2023 MonoJitICallInfo
*info
;
2024 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
2026 g_assert (!sig
->hasthis
);
2027 g_assert (sig
->param_count
< 3);
2029 info
= mono_register_jit_icall (func
, name
, sig
, no_throw
);
2031 if (emul_opcode_num
>= emul_opcode_alloced
) {
2032 int incr
= emul_opcode_alloced
? emul_opcode_alloced
/2: 16;
2033 emul_opcode_alloced
+= incr
;
2034 emul_opcode_map
= g_realloc (emul_opcode_map
, sizeof (emul_opcode_map
[0]) * emul_opcode_alloced
);
2035 emul_opcode_opcodes
= g_realloc (emul_opcode_opcodes
, sizeof (emul_opcode_opcodes
[0]) * emul_opcode_alloced
);
2037 emul_opcode_map
[emul_opcode_num
] = info
;
2038 emul_opcode_opcodes
[emul_opcode_num
] = opcode
;
2040 emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] |= (1 << (opcode
& EMUL_HIT_MASK
));
2044 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
2046 MonoMethodSignature
*sig
;
2049 sig
= mono_create_icall_signature (sigstr
);
2053 mono_register_jit_icall (func
, name
, sig
, save
);
2057 print_dfn (MonoCompile
*cfg
) {
2063 g_print ("IR code for method %s\n", mono_method_full_name (cfg
->method
, TRUE
));
2065 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
2066 bb
= cfg
->bblocks
[i
];
2067 /*if (bb->cil_code) {
2068 char* code1, *code2;
2069 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
2070 if (bb->last_ins->cil_code)
2071 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
2073 code2 = g_strdup ("");
2075 code1 [strlen (code1) - 1] = 0;
2076 code = g_strdup_printf ("%s -> %s", code1, code2);
2080 code
= g_strdup ("\n");
2081 g_print ("\nBB%d (%d) (len: %d): %s", bb
->block_num
, i
, bb
->cil_length
, code
);
2082 MONO_BB_FOR_EACH_INS (bb
, c
) {
2083 mono_print_ins_index (-1, c
);
2086 g_print ("\tprev:");
2087 for (j
= 0; j
< bb
->in_count
; ++j
) {
2088 g_print (" BB%d", bb
->in_bb
[j
]->block_num
);
2090 g_print ("\t\tsucc:");
2091 for (j
= 0; j
< bb
->out_count
; ++j
) {
2092 g_print (" BB%d", bb
->out_bb
[j
]->block_num
);
2094 g_print ("\n\tidom: BB%d\n", bb
->idom
? bb
->idom
->block_num
: -1);
2097 g_assert (mono_bitset_test_fast (bb
->dominators
, bb
->idom
->dfn
));
2100 mono_blockset_print (cfg
, bb
->dominators
, "\tdominators", bb
->idom
? bb
->idom
->dfn
: -1);
2102 mono_blockset_print (cfg
, bb
->dfrontier
, "\tdfrontier", -1);
2110 mono_bblock_add_inst (MonoBasicBlock
*bb
, MonoInst
*inst
)
2112 MONO_ADD_INS (bb
, inst
);
2116 mono_bblock_insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2120 bb
->code
= ins_to_insert
;
2122 /* Link with next */
2123 ins_to_insert
->next
= ins
;
2125 ins
->prev
= ins_to_insert
;
2127 if (bb
->last_ins
== NULL
)
2128 bb
->last_ins
= ins_to_insert
;
2130 /* Link with next */
2131 ins_to_insert
->next
= ins
->next
;
2133 ins
->next
->prev
= ins_to_insert
;
2135 /* Link with previous */
2136 ins
->next
= ins_to_insert
;
2137 ins_to_insert
->prev
= ins
;
2139 if (bb
->last_ins
== ins
)
2140 bb
->last_ins
= ins_to_insert
;
2145 mono_bblock_insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2149 bb
->code
= ins_to_insert
;
2150 ins_to_insert
->next
= ins
;
2151 if (bb
->last_ins
== NULL
)
2152 bb
->last_ins
= ins_to_insert
;
2154 /* Link with previous */
2156 ins
->prev
->next
= ins_to_insert
;
2157 ins_to_insert
->prev
= ins
->prev
;
2159 /* Link with next */
2160 ins
->prev
= ins_to_insert
;
2161 ins_to_insert
->next
= ins
;
2163 if (bb
->code
== ins
)
2164 bb
->code
= ins_to_insert
;
2169 * mono_verify_bblock:
2171 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2174 mono_verify_bblock (MonoBasicBlock
*bb
)
2176 MonoInst
*ins
, *prev
;
2179 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2180 g_assert (ins
->prev
== prev
);
2184 g_assert (!bb
->last_ins
->next
);
2190 * Perform consistency checks on the JIT data structures and the IR
2193 mono_verify_cfg (MonoCompile
*cfg
)
2197 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2198 mono_verify_bblock (bb
);
2202 mono_destroy_compile (MonoCompile
*cfg
)
2205 mono_metadata_free_mh (cfg
->header
);
2206 //mono_mempool_stats (cfg->mempool);
2207 mono_free_loop_info (cfg
);
2209 mono_regstate_free (cfg
->rs
);
2211 g_hash_table_destroy (cfg
->spvars
);
2213 g_hash_table_destroy (cfg
->exvars
);
2214 mono_mempool_destroy (cfg
->mempool
);
2215 g_list_free (cfg
->ldstr_list
);
2216 g_hash_table_destroy (cfg
->token_info_hash
);
2217 if (cfg
->abs_patches
)
2218 g_hash_table_destroy (cfg
->abs_patches
);
2220 g_free (cfg
->varinfo
);
2222 g_free (cfg
->exception_message
);
2226 #ifdef HAVE_KW_THREAD
2227 static __thread gpointer mono_lmf_addr MONO_TLS_FAST
;
2228 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
2230 * When this is defined, the current lmf is stored in this tls variable instead of in
2233 static __thread gpointer mono_lmf MONO_TLS_FAST
;
2238 mono_get_jit_tls_key (void)
2240 return mono_jit_tls_id
;
2244 mono_get_jit_tls_offset (void)
2246 #ifdef HAVE_KW_THREAD
2248 MONO_THREAD_VAR_OFFSET (mono_jit_tls
, offset
);
2256 mono_get_lmf_tls_offset (void)
2258 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2260 MONO_THREAD_VAR_OFFSET(mono_lmf
,offset
);
2268 mono_get_lmf_addr_tls_offset (void)
2271 MONO_THREAD_VAR_OFFSET(mono_lmf_addr
,offset
);
2278 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2281 MonoJitTlsData
*jit_tls
;
2283 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2284 return jit_tls
->lmf
;
2286 g_assert_not_reached ();
2292 mono_get_lmf_addr (void)
2294 #ifdef HAVE_KW_THREAD
2295 return mono_lmf_addr
;
2297 MonoJitTlsData
*jit_tls
;
2299 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2300 return &jit_tls
->lmf
;
2303 * When resolving the call to mono_jit_thread_attach full-aot will look
2304 * in the plt, which causes a call into the generic trampoline, which in turn
2305 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
2306 * call mono_jit_thread_attach from the native-to-managed wrapper, without
2307 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
2310 mono_jit_thread_attach (NULL
);
2312 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2313 return &jit_tls
->lmf
;
2315 g_assert_not_reached ();
2321 mono_set_lmf (MonoLMF
*lmf
)
2323 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2327 (*mono_get_lmf_addr ()) = lmf
;
2330 /* Called by native->managed wrappers */
2332 mono_jit_thread_attach (MonoDomain
*domain
)
2336 * Happens when called from AOTed code which is only used in the root
2339 domain
= mono_get_root_domain ();
2341 #ifdef HAVE_KW_THREAD
2342 if (!mono_lmf_addr
) {
2343 mono_thread_attach (domain
);
2346 if (!TlsGetValue (mono_jit_tls_id
))
2347 mono_thread_attach (domain
);
2349 if (mono_domain_get () != domain
)
2350 mono_domain_set (domain
, TRUE
);
2354 * mono_thread_abort:
2355 * @obj: exception object
2357 * abort the thread, print exception information and stack trace
2360 mono_thread_abort (MonoObject
*obj
)
2362 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
2364 /* handle_remove should be eventually called for this thread, too
2367 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
2368 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
2369 mono_thread_exit ();
2371 exit (mono_environment_exitcode_get ());
2376 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
2378 MonoJitTlsData
*jit_tls
;
2381 jit_tls
= TlsGetValue (mono_jit_tls_id
);
2385 jit_tls
= g_new0 (MonoJitTlsData
, 1);
2387 TlsSetValue (mono_jit_tls_id
, jit_tls
);
2389 #ifdef HAVE_KW_THREAD
2390 mono_jit_tls
= jit_tls
;
2393 jit_tls
->abort_func
= abort_func
;
2394 jit_tls
->end_of_stack
= stack_start
;
2396 lmf
= g_new0 (MonoLMF
, 1);
2397 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
2398 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
2403 jit_tls
->first_lmf
= lmf
;
2405 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2406 /* jit_tls->lmf is unused */
2408 mono_lmf_addr
= &mono_lmf
;
2410 #if defined(HAVE_KW_THREAD)
2411 mono_lmf_addr
= &jit_tls
->lmf
;
2417 mono_arch_setup_jit_tls_data (jit_tls
);
2418 mono_setup_altstack (jit_tls
);
2424 mono_thread_start_cb (intptr_t tid
, gpointer stack_start
, gpointer func
)
2426 MonoInternalThread
*thread
;
2427 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
2428 thread
= mono_thread_internal_current ();
2429 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, jit_tls
, func
);
2431 thread
->jit_data
= jit_tls
;
2434 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
2437 mono_thread_abort_dummy (MonoObject
*obj
)
2439 if (mono_thread_attach_aborted_cb
)
2440 mono_thread_attach_aborted_cb (obj
);
2442 mono_thread_abort (obj
);
2446 mono_thread_attach_cb (intptr_t tid
, gpointer stack_start
)
2448 MonoInternalThread
*thread
;
2449 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
2450 thread
= mono_thread_internal_current ();
2451 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, (MonoJitTlsData
*) jit_tls
, NULL
);
2453 thread
->jit_data
= jit_tls
;
2454 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
2455 mono_runtime_setup_stat_profiler ();
2459 mini_thread_cleanup (MonoThread
*thread
)
2461 MonoInternalThread
*internal
= thread
->internal_thread
;
2462 MonoJitTlsData
*jit_tls
= internal
->jit_data
;
2465 mono_debugger_thread_cleanup (jit_tls
);
2466 mono_arch_free_jit_tls_data (jit_tls
);
2468 mono_free_altstack (jit_tls
);
2469 g_free (jit_tls
->first_lmf
);
2471 internal
->jit_data
= NULL
;
2473 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
2474 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
2475 * not a trivial thing.
2477 * The current offender is mono_thread_manage which cleanup threads from the outside.
2479 if (internal
== mono_thread_internal_current ()) {
2480 TlsSetValue (mono_jit_tls_id
, NULL
);
2482 #ifdef HAVE_KW_THREAD
2483 mono_jit_tls
= NULL
;
2484 mono_lmf_addr
= NULL
;
2485 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2494 mono_create_tls_get (MonoCompile
*cfg
, int offset
)
2496 #ifdef MONO_ARCH_HAVE_TLS_GET
2497 if (MONO_ARCH_HAVE_TLS_GET
) {
2503 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2504 ins
->dreg
= mono_alloc_preg (cfg
);
2505 ins
->inst_offset
= offset
;
2513 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2515 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2519 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2521 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2525 mono_get_thread_intrinsic (MonoCompile
* cfg
)
2527 return mono_create_tls_get (cfg
, mono_thread_get_tls_offset ());
2531 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2533 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2537 ji
->data
.target
= target
;
2538 ji
->next
= cfg
->patch_info
;
2540 cfg
->patch_info
= ji
;
2544 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2546 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2550 ji
->data
.target
= target
;
2557 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2559 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2562 if ((*ji
)->ip
.i
== ip
)
2565 ji
= &((*ji
)->next
);
2570 * mono_patch_info_dup_mp:
2572 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2575 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2577 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2578 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2580 switch (patch_info
->type
) {
2581 case MONO_PATCH_INFO_RVA
:
2582 case MONO_PATCH_INFO_LDSTR
:
2583 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2584 case MONO_PATCH_INFO_LDTOKEN
:
2585 case MONO_PATCH_INFO_DECLSEC
:
2586 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2587 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2589 case MONO_PATCH_INFO_SWITCH
:
2590 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2591 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2592 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2593 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2595 case MONO_PATCH_INFO_RGCTX_FETCH
:
2596 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2597 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2598 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2608 mono_patch_info_hash (gconstpointer data
)
2610 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2613 case MONO_PATCH_INFO_RVA
:
2614 case MONO_PATCH_INFO_LDSTR
:
2615 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2616 case MONO_PATCH_INFO_LDTOKEN
:
2617 case MONO_PATCH_INFO_DECLSEC
:
2618 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2619 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2620 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2621 case MONO_PATCH_INFO_VTABLE
:
2622 case MONO_PATCH_INFO_CLASS
:
2623 case MONO_PATCH_INFO_IID
:
2624 case MONO_PATCH_INFO_ADJUSTED_IID
:
2625 case MONO_PATCH_INFO_CLASS_INIT
:
2626 case MONO_PATCH_INFO_METHODCONST
:
2627 case MONO_PATCH_INFO_METHOD
:
2628 case MONO_PATCH_INFO_METHOD_JUMP
:
2629 case MONO_PATCH_INFO_IMAGE
:
2630 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2631 case MONO_PATCH_INFO_FIELD
:
2632 case MONO_PATCH_INFO_SFLDA
:
2633 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2634 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2636 return (ji
->type
<< 8);
2641 * mono_patch_info_equal:
2643 * This might fail to recognize equivalent patches, i.e. floats, so its only
2644 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2648 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2650 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2651 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2653 if (ji1
->type
!= ji2
->type
)
2656 switch (ji1
->type
) {
2657 case MONO_PATCH_INFO_RVA
:
2658 case MONO_PATCH_INFO_LDSTR
:
2659 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2660 case MONO_PATCH_INFO_LDTOKEN
:
2661 case MONO_PATCH_INFO_DECLSEC
:
2662 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2663 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2664 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2665 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2666 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2669 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2670 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2672 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2673 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
2674 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
2676 return e1
->method
== e2
->method
&& e1
->in_mrgctx
== e2
->in_mrgctx
&& e1
->info_type
== e2
->info_type
&& mono_patch_info_equal (e1
->data
, e2
->data
);
2679 if (ji1
->data
.target
!= ji2
->data
.target
)
2688 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
2690 unsigned char *ip
= patch_info
->ip
.i
+ code
;
2691 gconstpointer target
= NULL
;
2693 switch (patch_info
->type
) {
2694 case MONO_PATCH_INFO_BB
:
2696 * FIXME: This could be hit for methods without a prolog. Should use -1
2697 * but too much code depends on a 0 initial value.
2699 //g_assert (patch_info->data.bb->native_offset);
2700 target
= patch_info
->data
.bb
->native_offset
+ code
;
2702 case MONO_PATCH_INFO_ABS
:
2703 target
= patch_info
->data
.target
;
2705 case MONO_PATCH_INFO_LABEL
:
2706 target
= patch_info
->data
.inst
->inst_c0
+ code
;
2708 case MONO_PATCH_INFO_IP
:
2711 case MONO_PATCH_INFO_METHOD_REL
:
2712 target
= code
+ patch_info
->data
.offset
;
2714 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
2715 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2717 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
2718 g_assert_not_reached ();
2720 target
= mono_icall_get_wrapper (mi
);
2723 case MONO_PATCH_INFO_METHOD_JUMP
:
2724 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
2726 case MONO_PATCH_INFO_METHOD
:
2727 if (patch_info
->data
.method
== method
) {
2730 /* get the trampoline to the method from the domain */
2731 target
= mono_create_jit_trampoline (patch_info
->data
.method
);
2734 case MONO_PATCH_INFO_SWITCH
: {
2735 gpointer
*jump_table
;
2738 if (method
&& method
->dynamic
) {
2739 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2741 if (mono_aot_only
) {
2742 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2744 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2748 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++)
2749 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
2750 target
= jump_table
;
2753 case MONO_PATCH_INFO_METHODCONST
:
2754 case MONO_PATCH_INFO_CLASS
:
2755 case MONO_PATCH_INFO_IMAGE
:
2756 case MONO_PATCH_INFO_FIELD
:
2757 target
= patch_info
->data
.target
;
2759 case MONO_PATCH_INFO_IID
:
2760 mono_class_init (patch_info
->data
.klass
);
2761 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
2763 case MONO_PATCH_INFO_ADJUSTED_IID
:
2764 mono_class_init (patch_info
->data
.klass
);
2765 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
2767 case MONO_PATCH_INFO_VTABLE
:
2768 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2771 case MONO_PATCH_INFO_CLASS_INIT
: {
2772 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2775 target
= mono_create_class_init_trampoline (vtable
);
2778 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
2779 target
= mono_create_delegate_trampoline (patch_info
->data
.klass
);
2781 case MONO_PATCH_INFO_SFLDA
: {
2782 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
2784 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
2785 gpointer addr
= NULL
;
2787 mono_domain_lock (domain
);
2788 if (domain
->special_static_fields
)
2789 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
2790 mono_domain_unlock (domain
);
2796 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
2797 /* Done by the generated code */
2801 mono_runtime_class_init (vtable
);
2803 target
= (char*)vtable
->data
+ patch_info
->data
.field
->offset
;
2806 case MONO_PATCH_INFO_RVA
: {
2807 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
2810 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
2811 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
2814 case MONO_PATCH_INFO_R4
:
2815 case MONO_PATCH_INFO_R8
:
2816 target
= patch_info
->data
.target
;
2818 case MONO_PATCH_INFO_EXC_NAME
:
2819 target
= patch_info
->data
.name
;
2821 case MONO_PATCH_INFO_LDSTR
:
2823 mono_ldstr (domain
, patch_info
->data
.token
->image
,
2824 mono_metadata_token_index (patch_info
->data
.token
->token
));
2826 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
2828 MonoClass
*handle_class
;
2830 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2831 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
2832 mono_class_init (handle_class
);
2833 mono_class_init (mono_class_from_mono_type (handle
));
2836 mono_type_get_object (domain
, handle
);
2839 case MONO_PATCH_INFO_LDTOKEN
: {
2841 MonoClass
*handle_class
;
2843 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2844 patch_info
->data
.token
->token
, &handle_class
, NULL
);
2845 mono_class_init (handle_class
);
2850 case MONO_PATCH_INFO_DECLSEC
:
2851 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
2853 case MONO_PATCH_INFO_ICALL_ADDR
:
2854 /* run_cctors == 0 -> AOT */
2855 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2857 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, NULL
, NULL
);
2859 g_error ("Unable to resolve pinvoke method '%s' Re-run with MONO_LOG_LEVEL=debug for more information.\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2864 target
= mono_lookup_internal_call (patch_info
->data
.method
);
2866 if (!target
&& run_cctors
)
2867 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2870 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
2871 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2873 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
2874 g_assert_not_reached ();
2879 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
2880 target
= mono_thread_interruption_request_flag ();
2882 case MONO_PATCH_INFO_METHOD_RGCTX
: {
2883 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
2886 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
2889 case MONO_PATCH_INFO_BB_OVF
:
2890 case MONO_PATCH_INFO_EXC_OVF
:
2891 case MONO_PATCH_INFO_GOT_OFFSET
:
2892 case MONO_PATCH_INFO_NONE
:
2894 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2895 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
2898 switch (entry
->data
->type
) {
2899 case MONO_PATCH_INFO_CLASS
:
2900 slot
= mono_method_lookup_or_register_other_info (entry
->method
, entry
->in_mrgctx
, &entry
->data
->data
.klass
->byval_arg
, entry
->info_type
, mono_method_get_context (entry
->method
));
2902 case MONO_PATCH_INFO_METHOD
:
2903 case MONO_PATCH_INFO_METHODCONST
:
2904 slot
= mono_method_lookup_or_register_other_info (entry
->method
, entry
->in_mrgctx
, entry
->data
->data
.method
, entry
->info_type
, mono_method_get_context (entry
->method
));
2906 case MONO_PATCH_INFO_FIELD
:
2907 slot
= mono_method_lookup_or_register_other_info (entry
->method
, entry
->in_mrgctx
, entry
->data
->data
.field
, entry
->info_type
, mono_method_get_context (entry
->method
));
2910 g_assert_not_reached ();
2914 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
2917 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
2918 target
= mono_create_generic_class_init_trampoline ();
2920 case MONO_PATCH_INFO_MONITOR_ENTER
:
2921 target
= mono_create_monitor_enter_trampoline ();
2923 case MONO_PATCH_INFO_MONITOR_EXIT
:
2924 target
= mono_create_monitor_exit_trampoline ();
2926 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2927 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2929 /* AOT, not needed */
2932 target
= mono_arch_get_seq_point_info (domain
, code
);
2935 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
2936 #ifdef MONO_ARCH_LLVM_SUPPORTED
2937 g_assert (mono_use_llvm
);
2938 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
2940 g_assert_not_reached ();
2944 g_assert_not_reached ();
2947 return (gpointer
)target
;
2951 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
2953 ins
->inst_offset
= native_offset
;
2954 g_ptr_array_add (cfg
->seq_points
, ins
);
2955 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
2956 bb
->last_seq_point
= ins
;
2962 mono_compile_create_vars (MonoCompile
*cfg
)
2964 MonoMethodSignature
*sig
;
2965 MonoMethodHeader
*header
;
2968 header
= cfg
->header
;
2970 sig
= mono_method_signature (cfg
->method
);
2972 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
2973 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
2974 /* Inhibit optimizations */
2975 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
2977 if (cfg
->verbose_level
> 2)
2978 g_print ("creating vars\n");
2980 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
2983 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
2985 for (i
= 0; i
< sig
->param_count
; ++i
) {
2986 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
2989 if (cfg
->verbose_level
> 2) {
2991 printf ("\treturn : ");
2992 mono_print_ins (cfg
->ret
);
2996 printf ("\tthis: ");
2997 mono_print_ins (cfg
->args
[0]);
3000 for (i
= 0; i
< sig
->param_count
; ++i
) {
3001 printf ("\targ [%d]: ", i
);
3002 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
3006 cfg
->locals_start
= cfg
->num_varinfo
;
3007 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
3009 if (cfg
->verbose_level
> 2)
3010 g_print ("creating locals\n");
3012 for (i
= 0; i
< header
->num_locals
; ++i
)
3013 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
3015 if (cfg
->verbose_level
> 2)
3016 g_print ("locals done\n");
3018 mono_arch_create_vars (cfg
);
3021 #endif /* #ifndef DISABLE_JIT */
3024 mono_print_code (MonoCompile
*cfg
, const char* msg
)
3028 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3029 mono_print_bb (bb
, msg
);
3035 mono_postprocess_patches (MonoCompile
*cfg
)
3037 MonoJumpInfo
*patch_info
;
3040 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3041 switch (patch_info
->type
) {
3042 case MONO_PATCH_INFO_ABS
: {
3043 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
3046 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
3050 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
3051 // FIXME: CLEAN UP THIS MESS.
3052 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
3053 strstr (cfg
->method
->name
, info
->name
)) {
3055 * This is an icall wrapper, and this is a call to the
3058 if (cfg
->compile_aot
) {
3059 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
3060 patch_info
->data
.name
= info
->name
;
3063 /* for these array methods we currently register the same function pointer
3064 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
3065 * will return the incorrect one depending on the order they are registered.
3066 * See tests/test-arr.cs
3068 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
3069 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3070 patch_info
->data
.name
= info
->name
;
3075 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
3076 if (cfg
->abs_patches
) {
3077 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
3079 patch_info
->type
= abs_ji
->type
;
3080 patch_info
->data
.target
= abs_ji
->data
.target
;
3087 case MONO_PATCH_INFO_SWITCH
: {
3089 if (cfg
->method
->dynamic
) {
3090 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3092 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3095 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3096 /* Might be NULL if the switch is eliminated */
3097 if (patch_info
->data
.table
->table
[i
]) {
3098 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
3099 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
3104 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
3107 case MONO_PATCH_INFO_METHOD_JUMP
: {
3109 MonoDomain
*domain
= cfg
->domain
;
3110 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
3112 mono_domain_lock (domain
);
3113 if (!domain_jit_info (domain
)->jump_target_hash
)
3114 domain_jit_info (domain
)->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3115 list
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3116 list
= g_slist_prepend (list
, ip
);
3117 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, list
);
3118 mono_domain_unlock (domain
);
3129 collect_pred_seq_points (MonoBasicBlock
*bb
, MonoInst
*ins
, GSList
**next
, int depth
)
3132 MonoBasicBlock
*in_bb
;
3134 for (i
= 0; i
< bb
->in_count
; ++i
) {
3135 in_bb
= bb
->in_bb
[i
];
3137 if (in_bb
->last_seq_point
) {
3138 next
[in_bb
->last_seq_point
->backend
.size
] = g_slist_append (next
[in_bb
->last_seq_point
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3140 /* Have to look at its predecessors */
3142 collect_pred_seq_points (in_bb
, ins
, next
, depth
+ 1);
3148 mono_save_seq_point_info (MonoCompile
*cfg
)
3151 GSList
*bb_seq_points
, *l
;
3153 MonoDomain
*domain
= cfg
->domain
;
3155 MonoSeqPointInfo
*info
;
3158 if (!cfg
->seq_points
)
3161 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3162 info
->len
= cfg
->seq_points
->len
;
3163 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3164 SeqPoint
*sp
= &info
->seq_points
[i
];
3165 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3167 sp
->il_offset
= ins
->inst_imm
;
3168 sp
->native_offset
= ins
->inst_offset
;
3171 ins
->backend
.size
= i
;
3175 * For each sequence point, compute the list of sequence points immediately
3176 * following it, this is needed to implement 'step over' in the debugger agent.
3178 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3179 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3180 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3182 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3183 MonoInst
*ins
= l
->data
;
3185 if (!(ins
->flags
& MONO_INST_SINGLE_STEP_LOC
))
3189 /* Link with the previous seq point in the same bb */
3190 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3192 /* Link with the last bb in the previous bblocks */
3193 collect_pred_seq_points (bb
, ins
, next
, 0);
3200 if (cfg
->verbose_level
> 2) {
3201 printf ("\nSEQ POINT MAP: \n");
3204 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3205 SeqPoint
*sp
= &info
->seq_points
[i
];
3209 sp
->next_len
= g_slist_length (next
[i
]);
3210 sp
->next
= g_new (int, sp
->next_len
);
3212 if (cfg
->verbose_level
> 2 && next
[i
]) {
3213 printf ("\t0x%x ->", sp
->il_offset
);
3214 for (l
= next
[i
]; l
; l
= l
->next
) {
3215 next_index
= GPOINTER_TO_UINT (l
->data
);
3216 printf (" 0x%x", info
->seq_points
[next_index
].il_offset
);
3220 for (l
= next
[i
]; l
; l
= l
->next
) {
3221 next_index
= GPOINTER_TO_UINT (l
->data
);
3222 sp
->next
[j
++] = next_index
;
3224 g_slist_free (next
[i
]);
3228 cfg
->seq_point_info
= info
;
3230 // FIXME: dynamic methods
3231 mono_domain_lock (domain
);
3232 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3233 mono_domain_unlock (domain
);
3235 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3236 cfg
->seq_points
= NULL
;
3240 mono_codegen (MonoCompile
*cfg
)
3243 int max_epilog_size
;
3246 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3247 cfg
->spill_count
= 0;
3248 /* we reuse dfn here */
3249 /* bb->dfn = bb_count++; */
3251 mono_arch_lowering_pass (cfg
, bb
);
3253 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3254 mono_arch_peephole_pass_1 (cfg
, bb
);
3257 mono_local_regalloc (cfg
, bb
);
3259 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3260 mono_arch_peephole_pass_2 (cfg
, bb
);
3263 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3264 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3266 code
= mono_arch_emit_prolog (cfg
);
3268 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3269 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3271 cfg
->code_len
= code
- cfg
->native_code
;
3272 cfg
->prolog_end
= cfg
->code_len
;
3274 mono_debug_open_method (cfg
);
3276 /* emit code all basic blocks */
3277 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3278 bb
->native_offset
= cfg
->code_len
;
3279 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3280 mono_arch_output_basic_block (cfg
, bb
);
3281 bb
->native_length
= cfg
->code_len
- bb
->native_offset
;
3283 if (bb
== cfg
->bb_exit
) {
3284 cfg
->epilog_begin
= cfg
->code_len
;
3286 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3287 code
= cfg
->native_code
+ cfg
->code_len
;
3288 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3289 cfg
->code_len
= code
- cfg
->native_code
;
3290 g_assert (cfg
->code_len
< cfg
->code_size
);
3293 mono_arch_emit_epilog (cfg
);
3297 mono_arch_emit_exceptions (cfg
);
3299 max_epilog_size
= 0;
3301 code
= cfg
->native_code
+ cfg
->code_len
;
3303 /* we always allocate code in cfg->domain->code_mp to increase locality */
3304 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3305 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3307 if (cfg
->method
->dynamic
) {
3308 guint unwindlen
= 0;
3309 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3310 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3312 /* Allocate the code into a separate memory pool so it can be freed */
3313 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3314 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3315 mono_domain_lock (cfg
->domain
);
3316 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3317 mono_domain_unlock (cfg
->domain
);
3319 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3321 guint unwindlen
= 0;
3322 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3323 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3325 code
= mono_domain_code_reserve (cfg
->domain
, cfg
->code_size
+ unwindlen
);
3328 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3329 g_free (cfg
->native_code
);
3330 cfg
->native_code
= code
;
3331 code
= cfg
->native_code
+ cfg
->code_len
;
3333 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3334 mono_postprocess_patches (cfg
);
3336 #ifdef VALGRIND_JIT_REGISTER_MAP
3337 if (valgrind_register
){
3338 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3339 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3344 if (cfg
->verbose_level
> 0) {
3345 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3346 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3348 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3353 gboolean is_generic
= FALSE
;
3355 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3356 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3360 if (cfg
->generic_sharing_context
)
3361 g_assert (is_generic
);
3364 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3365 mono_arch_save_unwind_info (cfg
);
3368 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->run_cctors
);
3370 if (cfg
->method
->dynamic
) {
3371 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3373 mono_domain_code_commit (cfg
->domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3375 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3377 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3379 mono_debug_close_method (cfg
);
3381 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3382 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3387 compute_reachable (MonoBasicBlock
*bb
)
3391 if (!(bb
->flags
& BB_VISITED
)) {
3392 bb
->flags
|= BB_VISITED
;
3393 for (i
= 0; i
< bb
->out_count
; ++i
)
3394 compute_reachable (bb
->out_bb
[i
]);
3399 create_jit_info (MonoCompile
*cfg
, MonoMethod
*method_to_compile
)
3402 MonoMethodHeader
*header
;
3405 int generic_info_size
;
3406 int holes_size
= 0, num_holes
= 0;
3408 g_assert (method_to_compile
== cfg
->method
);
3409 header
= cfg
->header
;
3411 if (cfg
->generic_sharing_context
)
3412 generic_info_size
= sizeof (MonoGenericJitInfo
);
3414 generic_info_size
= 0;
3416 if (cfg
->try_block_holes
) {
3417 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3418 TryBlockHole
*hole
= tmp
->data
;
3419 MonoExceptionClause
*ec
= hole
->clause
;
3420 int hole_end
= hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
;
3421 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3422 g_assert (clause_last_bb
);
3424 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3425 if (clause_last_bb
->native_offset
!= hole_end
)
3429 holes_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3430 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3431 printf ("Number of try block holes %d\n", num_holes
);
3434 if (COMPILE_LLVM (cfg
))
3435 num_clauses
= cfg
->llvm_ex_info_len
;
3437 num_clauses
= header
->num_clauses
;
3439 if (cfg
->method
->dynamic
) {
3440 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3441 generic_info_size
+ holes_size
);
3443 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
3444 (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3445 generic_info_size
+ holes_size
);
3448 jinfo
->method
= cfg
->method_to_register
;
3449 jinfo
->code_start
= cfg
->native_code
;
3450 jinfo
->code_size
= cfg
->code_len
;
3451 jinfo
->used_regs
= cfg
->used_int_regs
;
3452 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
3453 jinfo
->cas_inited
= FALSE
; /* initialization delayed at the first stalk walk using this method */
3454 jinfo
->num_clauses
= num_clauses
;
3455 if (COMPILE_LLVM (cfg
))
3456 jinfo
->from_llvm
= TRUE
;
3458 if (cfg
->generic_sharing_context
) {
3460 MonoGenericJitInfo
*gi
;
3462 jinfo
->has_generic_jit_info
= 1;
3464 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3467 gi
->generic_sharing_context
= cfg
->generic_sharing_context
;
3469 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3470 mini_method_get_context (method_to_compile
)->method_inst
||
3471 method_to_compile
->klass
->valuetype
) {
3472 g_assert (cfg
->rgctx_var
);
3477 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3478 mini_method_get_context (method_to_compile
)->method_inst
||
3479 method_to_compile
->klass
->valuetype
) {
3480 inst
= cfg
->rgctx_var
;
3481 g_assert (inst
->opcode
== OP_REGOFFSET
);
3483 inst
= cfg
->args
[0];
3486 if (inst
->opcode
== OP_REGVAR
) {
3487 gi
->this_in_reg
= 1;
3488 gi
->this_reg
= inst
->dreg
;
3490 g_assert (inst
->opcode
== OP_REGOFFSET
);
3492 g_assert (inst
->inst_basereg
== X86_EBP
);
3493 #elif defined(TARGET_AMD64)
3494 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
3496 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
3498 gi
->this_in_reg
= 0;
3499 gi
->this_reg
= inst
->inst_basereg
;
3500 gi
->this_offset
= inst
->inst_offset
;
3505 MonoTryBlockHoleTableJitInfo
*table
;
3508 jinfo
->has_try_block_holes
= 1;
3509 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3510 table
->num_holes
= (guint16
)num_holes
;
3512 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3513 guint32 start_bb_offset
;
3514 MonoTryBlockHoleJitInfo
*hole
;
3515 TryBlockHole
*hole_data
= tmp
->data
;
3516 MonoExceptionClause
*ec
= hole_data
->clause
;
3517 int hole_end
= hole_data
->basic_block
->native_offset
+ hole_data
->basic_block
->native_length
;
3518 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3519 g_assert (clause_last_bb
);
3521 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3522 if (clause_last_bb
->native_offset
== hole_end
)
3525 start_bb_offset
= hole_data
->start_offset
- hole_data
->basic_block
->native_offset
;
3526 hole
= &table
->holes
[i
++];
3527 hole
->clause
= hole_data
->clause
- &header
->clauses
[0];
3528 hole
->offset
= (guint32
)hole_data
->start_offset
;
3529 hole
->length
= (guint16
)(hole_data
->basic_block
->native_length
- start_bb_offset
);
3531 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3532 printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole
->clause
, hole
->offset
, hole
->length
);
3534 g_assert (i
== num_holes
);
3537 if (COMPILE_LLVM (cfg
)) {
3539 memcpy (&jinfo
->clauses
[0], &cfg
->llvm_ex_info
[0], num_clauses
* sizeof (MonoJitExceptionInfo
));
3540 } else if (header
->num_clauses
) {
3543 for (i
= 0; i
< header
->num_clauses
; i
++) {
3544 MonoExceptionClause
*ec
= &header
->clauses
[i
];
3545 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3546 MonoBasicBlock
*tblock
;
3549 ei
->flags
= ec
->flags
;
3551 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
3552 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
3554 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
3555 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
3557 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
3559 ei
->data
.catch_class
= ec
->data
.catch_class
;
3562 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
3564 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
3565 g_assert (tblock
->native_offset
);
3566 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3568 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
3569 g_assert (tblock
->native_offset
);
3570 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
3572 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
3574 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3575 TryBlockHole
*hole
= tmp
->data
;
3576 gpointer hole_end
= cfg
->native_code
+ (hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
3577 if (hole
->clause
== ec
&& hole_end
== ei
->try_end
) {
3578 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3579 printf ("\tShortening try block %d from %x to %x\n", i
, (int)((guint8
*)ei
->try_end
- cfg
->native_code
), hole
->start_offset
);
3581 ei
->try_end
= cfg
->native_code
+ hole
->start_offset
;
3586 if (ec
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
3588 if (ec
->handler_offset
+ ec
->handler_len
< header
->code_size
) {
3589 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
+ ec
->handler_len
];
3591 end_offset
= tblock
->native_offset
;
3593 end_offset
= cfg
->epilog_begin
;
3595 ei
->data
.handler_end
= cfg
->native_code
+ end_offset
;
3599 if (G_UNLIKELY (cfg
->verbose_level
>= 4)) {
3600 for (i
= 0; i
< jinfo
->num_clauses
; i
++) {
3601 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3602 int start
= (guint8
*)ei
->try_start
- cfg
->native_code
;
3603 int end
= (guint8
*)ei
->try_end
- cfg
->native_code
;
3604 int handler
= (guint8
*)ei
->handler_start
- cfg
->native_code
;
3606 printf ("JitInfo EH clause %d flags %x try %x-%x handler %x\n", i
, ei
->flags
, start
, end
, handler
);
3613 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
3614 * using it during runtime, hence the define.
3616 #ifdef MONO_ARCH_HAVE_XP_UNWIND
3617 if (cfg
->encoded_unwind_ops
) {
3618 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
3619 g_free (cfg
->encoded_unwind_ops
);
3620 } else if (cfg
->unwind_ops
) {
3622 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
3624 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
3625 g_free (unwind_info
);
3634 * mini_get_shared_method:
3636 * Return the method which is actually compiled/registered when doing generic sharing.
3639 mini_get_shared_method (MonoMethod
*method
)
3641 MonoGenericContext shared_context
;
3642 MonoMethod
*declaring_method
, *res
;
3644 gboolean partial
= FALSE
;
3646 if (method
->is_generic
|| method
->klass
->generic_container
)
3647 declaring_method
= method
;
3649 declaring_method
= mono_method_get_declaring_generic_method (method
);
3651 if (declaring_method
->is_generic
)
3652 shared_context
= mono_method_get_generic_container (declaring_method
)->context
;
3654 shared_context
= declaring_method
->klass
->generic_container
->context
;
3656 /* Handle partial sharing */
3657 if (method
!= declaring_method
&& method
->is_inflated
&& !mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
)) {
3658 MonoGenericContext
*context
= mono_method_get_context (method
);
3659 MonoGenericInst
*inst
;
3660 MonoType
**type_argv
;
3663 * Create the shared context by replacing the ref type arguments with
3664 * type parameters, and keeping the rest.
3667 inst
= context
->class_inst
;
3669 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3670 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3671 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
3672 type_argv
[i
] = shared_context
.class_inst
->type_argv
[i
];
3674 type_argv
[i
] = inst
->type_argv
[i
];
3677 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3681 inst
= context
->method_inst
;
3683 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3684 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3685 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
3686 type_argv
[i
] = shared_context
.method_inst
->type_argv
[i
];
3688 type_argv
[i
] = inst
->type_argv
[i
];
3691 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3696 res
= mono_class_inflate_generic_method (declaring_method
, &shared_context
);
3698 /* The result should be an inflated method whose parent is not inflated */
3699 g_assert (!res
->klass
->is_inflated
);
3706 * mini_method_compile:
3707 * @method: the method to compile
3708 * @opts: the optimization flags to use
3709 * @domain: the domain where the method will be compiled in
3710 * @run_cctors: whether we should run type ctors if possible
3711 * @compile_aot: whether this is an AOT compilation
3712 * @parts: debug flag
3714 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3715 * field in the returned struct to see if compilation succeded.
3718 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
3720 MonoMethodHeader
*header
;
3721 MonoMethodSignature
*sig
;
3725 int dfn
, i
, code_size_ratio
;
3726 gboolean deadce_has_run
= FALSE
;
3727 gboolean try_generic_shared
, try_llvm
;
3728 MonoMethod
*method_to_compile
, *method_to_register
;
3730 mono_jit_stats
.methods_compiled
++;
3731 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
3732 mono_profiler_method_jit (method
);
3733 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
3734 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
3738 * We might get passed the original generic method definition or
3739 * instances with type parameters.
3740 * FIXME: Remove the method->klass->generic_class limitation.
3742 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3743 (opts
& MONO_OPT_GSHARED
) && ((method
->is_generic
|| method
->klass
->generic_container
) || (!method
->klass
->generic_class
&& mono_method_is_generic_sharable_impl (method
, TRUE
)));
3745 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3746 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl (method
, FALSE
);
3748 if (opts
& MONO_OPT_GSHARED
) {
3749 if (try_generic_shared
)
3750 mono_stats
.generics_sharable_methods
++;
3751 else if (mono_method_is_generic_impl (method
))
3752 mono_stats
.generics_unsharable_methods
++;
3762 if (try_generic_shared
) {
3763 method_to_compile
= mini_get_shared_method (method
);
3764 g_assert (method_to_compile
);
3766 method_to_compile
= method
;
3769 cfg
= g_new0 (MonoCompile
, 1);
3770 cfg
->method
= method_to_compile
;
3771 cfg
->header
= mono_method_get_header (cfg
->method
);
3772 cfg
->mempool
= mono_mempool_new ();
3774 cfg
->prof_options
= mono_profiler_get_events ();
3775 cfg
->run_cctors
= run_cctors
;
3776 cfg
->domain
= domain
;
3777 cfg
->verbose_level
= mini_verbose
;
3778 cfg
->compile_aot
= compile_aot
;
3779 cfg
->skip_visibility
= method
->skip_visibility
;
3780 cfg
->orig_method
= method
;
3781 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
3782 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
3783 if (try_generic_shared
)
3784 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->generic_sharing_context
;
3785 cfg
->compile_llvm
= try_llvm
;
3786 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
3788 if (cfg
->gen_seq_points
)
3789 cfg
->seq_points
= g_ptr_array_new ();
3791 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
)) {
3792 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
3796 if (cfg
->generic_sharing_context
) {
3797 method_to_register
= method_to_compile
;
3799 g_assert (method
== method_to_compile
);
3800 method_to_register
= method
;
3802 cfg
->method_to_register
= method_to_register
;
3804 if (cfg
->compile_llvm
) {
3805 /* No way to obtain the location info for 'this' */
3806 if (try_generic_shared
) {
3807 cfg
->exception_message
= g_strdup ("gshared");
3808 cfg
->disable_llvm
= TRUE
;
3811 if (cfg
->method
->save_lmf
) {
3812 cfg
->exception_message
= g_strdup ("lmf");
3813 cfg
->disable_llvm
= TRUE
;
3817 if (cfg
->method
->dynamic
) {
3818 cfg
->exception_message
= g_strdup ("dynamic.");
3819 cfg
->disable_llvm
= TRUE
;
3823 mono_error_init (&err
);
3824 sig
= mono_method_signature_checked (cfg
->method
, &err
);
3826 cfg
->exception_type
= MONO_EXCEPTION_TYPE_LOAD
;
3827 cfg
->exception_message
= g_strdup (mono_error_get_message (&err
));
3828 mono_error_cleanup (&err
);
3829 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3830 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3834 header
= cfg
->header
;
3836 MonoLoaderError
*error
;
3838 if ((error
= mono_loader_get_last_error ())) {
3839 cfg
->exception_type
= error
->exception_type
;
3841 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
3842 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
3844 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3845 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3849 if (header
->clauses
) {
3851 * FIXME: LLLVM 2.6/SVN no longer seems to generate correct exception info
3854 cfg
->exception_message
= g_strdup ("clauses");
3855 cfg
->disable_llvm
= TRUE
;
3860 static gboolean inited
;
3863 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_with_llvm
);
3864 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_without_llvm
);
3869 * Check for methods which cannot be compiled by LLVM early, to avoid
3870 * the extra compilation pass.
3872 if (COMPILE_LLVM (cfg
) && cfg
->disable_llvm
) {
3873 if (cfg
->verbose_level
>= 1) {
3874 //nm = mono_method_full_name (cfg->method, TRUE);
3875 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3878 InterlockedIncrement (&methods_without_llvm
);
3879 mono_destroy_compile (cfg
);
3881 goto restart_compile
;
3886 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3887 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
3888 cfg
->disable_reuse_registers
= TRUE
;
3889 cfg
->disable_reuse_stack_slots
= TRUE
;
3891 * This decreases the change the debugger will read registers/stack slots which are
3892 * not yet initialized.
3894 cfg
->disable_initlocals_opt
= TRUE
;
3896 cfg
->extend_live_ranges
= TRUE
;
3898 /* Temporarily disable this when running in the debugger until we have support
3899 * for this in the debugger. */
3900 cfg
->disable_omit_fp
= TRUE
;
3902 /* The debugger needs all locals to be on the stack or in a global register */
3903 cfg
->disable_vreg_to_lvreg
= TRUE
;
3905 /* Don't remove unused variables when running inside the debugger since the user
3906 * may still want to view them. */
3907 cfg
->disable_deadce_vars
= TRUE
;
3909 // cfg->opt |= MONO_OPT_SHARED;
3910 cfg
->opt
&= ~MONO_OPT_DEADCE
;
3911 cfg
->opt
&= ~MONO_OPT_INLINE
;
3912 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
3913 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
3914 cfg
->opt
&= ~MONO_OPT_GSHARED
;
3916 /* This is needed for the soft debugger, which doesn't like code after the epilog */
3917 cfg
->disable_out_of_line_bblocks
= TRUE
;
3920 if (mono_using_xdebug
) {
3922 * Make each variable use its own register/stack slot and extend
3923 * their liveness to cover the whole method, making them displayable
3924 * in gdb even after they are dead.
3926 cfg
->disable_reuse_registers
= TRUE
;
3927 cfg
->disable_reuse_stack_slots
= TRUE
;
3928 cfg
->extend_live_ranges
= TRUE
;
3929 cfg
->compute_precise_live_ranges
= TRUE
;
3932 mini_gc_init_gc_map (cfg
);
3934 if (COMPILE_LLVM (cfg
)) {
3935 cfg
->opt
|= MONO_OPT_ABCREM
;
3938 if (getenv ("MONO_VERBOSE_METHOD")) {
3939 char *name
= getenv ("MONO_VERBOSE_METHOD");
3941 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
3942 MonoMethodDesc
*desc
;
3944 desc
= mono_method_desc_new (name
, TRUE
);
3945 if (mono_method_desc_full_match (desc
, cfg
->method
)) {
3946 cfg
->verbose_level
= 4;
3948 mono_method_desc_free (desc
);
3950 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
3951 cfg
->verbose_level
= 4;
3955 ip
= (guint8
*)header
->code
;
3957 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
3959 if (cfg
->verbose_level
> 0) {
3960 if (COMPILE_LLVM (cfg
))
3961 g_print ("converting llvm method %s\n", mono_method_full_name (method
, TRUE
));
3962 else if (cfg
->generic_sharing_context
)
3963 g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile
, TRUE
));
3965 g_print ("converting method %s\n", mono_method_full_name (method
, TRUE
));
3968 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
3969 cfg
->opt
|= MONO_OPT_SSA
;
3972 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")))
3973 cfg->globalra = TRUE;
3976 //cfg->globalra = TRUE;
3978 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3979 // cfg->globalra = TRUE;
3982 static int count
= 0;
3986 if (getenv ("COUNT2")) {
3987 cfg->globalra = TRUE;
3988 if (count == atoi (getenv ("COUNT2")))
3989 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3990 if (count > atoi (getenv ("COUNT2")))
3991 cfg->globalra = FALSE;
3996 if (header
->clauses
)
3997 cfg
->globalra
= FALSE
;
3999 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
4000 /* The code in the prolog clobbers caller saved registers */
4001 cfg
->globalra
= FALSE
;
4003 // FIXME: Disable globalra in case of tracing/profiling
4005 if (cfg
->method
->save_lmf
)
4006 /* The LMF saving code might clobber caller saved registers */
4007 cfg
->globalra
= FALSE
;
4009 if (header
->code_size
> 5000)
4011 /* Too large bblocks could overflow the ins positions */
4012 cfg
->globalra
= FALSE
;
4014 cfg
->rs
= mono_regstate_new ();
4016 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
4017 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
4019 /* FIXME: Fix SSA to handle branches inside bblocks */
4020 if (cfg
->opt
& MONO_OPT_SSA
)
4021 cfg
->enable_extended_bblocks
= FALSE
;
4024 * FIXME: This confuses liveness analysis because variables which are assigned after
4025 * a branch inside a bblock become part of the kill set, even though the assignment
4026 * might not get executed. This causes the optimize_initlocals pass to delete some
4027 * assignments which are needed.
4028 * Also, the mono_if_conversion pass needs to be modified to recognize the code
4031 //cfg->enable_extended_bblocks = TRUE;
4033 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
4034 if (mono_compile_is_broken (cfg
))
4038 * create MonoInst* which represents arguments and local variables
4040 mono_compile_create_vars (cfg
);
4042 /* SSAPRE is not supported on linear IR */
4043 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
4045 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
4048 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
4050 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4051 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4054 mono_destroy_compile (cfg
);
4055 try_generic_shared
= FALSE
;
4056 goto restart_compile
;
4058 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
4060 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4061 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4062 /* cfg contains the details of the failure, so let the caller cleanup */
4066 mono_jit_stats
.basic_blocks
+= cfg
->num_bblocks
;
4067 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->num_bblocks
, mono_jit_stats
.max_basic_blocks
);
4069 if (COMPILE_LLVM (cfg
)) {
4072 /* The IR has to be in SSA form for LLVM */
4073 cfg
->opt
|= MONO_OPT_SSA
;
4077 // Allow SSA on the result value
4078 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
4080 // Add an explicit return instruction referencing the return value
4081 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
4082 ins
->sreg1
= cfg
->ret
->dreg
;
4084 MONO_ADD_INS (cfg
->bb_exit
, ins
);
4087 cfg
->opt
&= ~MONO_OPT_LINEARS
;
4090 cfg
->opt
&= ~MONO_OPT_BRANCH
;
4093 /* todo: remove code when we have verified that the liveness for try/catch blocks
4097 * Currently, this can't be commented out since exception blocks are not
4098 * processed during liveness analysis.
4099 * It is also needed, because otherwise the local optimization passes would
4100 * delete assignments in cases like this:
4102 * <something which throws>
4104 * This also allows SSA to be run on methods containing exception clauses, since
4105 * SSA will ignore variables marked VOLATILE.
4107 mono_liveness_handle_exception_clauses (cfg
);
4109 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
4111 if (!COMPILE_LLVM (cfg
))
4112 mono_decompose_long_opts (cfg
);
4114 /* Should be done before branch opts */
4115 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
4116 mono_local_cprop (cfg
);
4118 if (cfg
->opt
& MONO_OPT_BRANCH
)
4119 mono_optimize_branches (cfg
);
4121 /* This must be done _before_ global reg alloc and _after_ decompose */
4122 mono_handle_global_vregs (cfg
);
4123 if (cfg
->opt
& MONO_OPT_DEADCE
)
4124 mono_local_deadce (cfg
);
4125 /* Disable this for LLVM to make the IR easier to handle */
4126 if (!COMPILE_LLVM (cfg
))
4127 mono_if_conversion (cfg
);
4129 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
4130 mono_remove_critical_edges (cfg
);
4132 /* Depth-first ordering on basic blocks */
4133 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4135 cfg
->max_block_num
= cfg
->num_bblocks
;
4138 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4139 if (cfg
->num_bblocks
!= dfn
+ 1) {
4142 cfg
->num_bblocks
= dfn
+ 1;
4144 /* remove unreachable code, because the code in them may be
4145 * inconsistent (access to dead variables for example) */
4146 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4147 bb
->flags
&= ~BB_VISITED
;
4148 compute_reachable (cfg
->bb_entry
);
4149 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4150 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
4151 compute_reachable (bb
);
4152 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4153 if (!(bb
->flags
& BB_VISITED
)) {
4154 if (cfg
->verbose_level
> 1)
4155 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
4156 bb
->code
= bb
->last_ins
= NULL
;
4157 while (bb
->out_count
)
4158 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
4161 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4162 bb
->flags
&= ~BB_VISITED
;
4165 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
4167 * we disable some optimizations if there are too many variables
4168 * because JIT time may become too expensive. The actual number needs
4169 * to be tweaked and eventually the non-linear algorithms should be fixed.
4171 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
4172 cfg
->disable_ssa
= TRUE
;
4175 if (cfg
->opt
& MONO_OPT_LOOP
) {
4176 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
4177 mono_compute_natural_loops (cfg
);
4180 /* after method_to_ir */
4182 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4183 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4188 if (header->num_clauses)
4189 cfg->disable_ssa = TRUE;
4192 //#define DEBUGSSA "logic_run"
4193 #define DEBUGSSA_CLASS "Tests"
4196 if (!cfg
->disable_ssa
) {
4197 mono_local_cprop (cfg
);
4200 mono_ssa_compute (cfg
);
4204 if (cfg
->opt
& MONO_OPT_SSA
) {
4205 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
4207 mono_ssa_compute (cfg
);
4210 if (cfg
->verbose_level
>= 2) {
4217 /* after SSA translation */
4219 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4220 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4224 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
4225 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4227 mono_ssa_cprop (cfg
);
4233 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4234 //mono_ssa_strength_reduction (cfg);
4236 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
4237 mono_perform_ssapre (cfg
);
4238 //mono_local_cprop (cfg);
4241 if (cfg
->opt
& MONO_OPT_DEADCE
) {
4242 mono_ssa_deadce (cfg
);
4243 deadce_has_run
= TRUE
;
4246 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4247 mono_perform_abc_removal (cfg
);
4249 mono_ssa_remove (cfg
);
4250 mono_local_cprop (cfg
);
4251 mono_handle_global_vregs (cfg
);
4252 if (cfg
->opt
& MONO_OPT_DEADCE
)
4253 mono_local_deadce (cfg
);
4255 if (cfg
->opt
& MONO_OPT_BRANCH
) {
4258 mono_optimize_branches (cfg
);
4260 /* Have to recompute cfg->bblocks and bb->dfn */
4261 if (cfg
->globalra
) {
4262 mono_remove_critical_edges (cfg
);
4264 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4267 /* Depth-first ordering on basic blocks */
4268 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4271 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4272 cfg
->num_bblocks
= dfn
+ 1;
4278 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
4279 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4280 mono_perform_abc_removal (cfg
);
4283 /* after SSA removal */
4285 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4286 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4290 #ifdef MONO_ARCH_SOFT_FLOAT
4291 if (!COMPILE_LLVM (cfg
))
4292 mono_decompose_soft_float (cfg
);
4294 if (!COMPILE_LLVM (cfg
))
4295 mono_decompose_vtype_opts (cfg
);
4296 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4297 mono_decompose_array_access_opts (cfg
);
4300 #ifndef MONO_ARCH_GOT_REG
4305 g_assert (cfg
->got_var_allocated
);
4308 * Allways allocate the GOT var to a register, because keeping it
4309 * in memory will increase the number of live temporaries in some
4310 * code created by inssel.brg, leading to the well known spills+
4311 * branches problem. Testcase: mcs crash in
4312 * System.MonoCustomAttrs:GetCustomAttributes.
4314 #ifdef MONO_ARCH_GOT_REG
4315 got_reg
= MONO_ARCH_GOT_REG
;
4317 regs
= mono_arch_get_global_int_regs (cfg
);
4319 got_reg
= GPOINTER_TO_INT (regs
->data
);
4322 cfg
->got_var
->opcode
= OP_REGVAR
;
4323 cfg
->got_var
->dreg
= got_reg
;
4324 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
4328 * Have to call this again to process variables added since the first call.
4330 mono_liveness_handle_exception_clauses (cfg
);
4332 if (cfg
->globalra
) {
4335 /* Have to do this before regalloc since it can create vregs */
4336 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4337 mono_arch_lowering_pass (cfg
, bb
);
4339 mono_global_regalloc (cfg
);
4342 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
4343 GList
*vars
, *regs
, *l
;
4345 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
4346 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
4347 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
4348 mono_analyze_liveness (cfg
);
4350 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
4351 regs
= mono_arch_get_global_int_regs (cfg
);
4352 /* Remove the reg reserved for holding the GOT address */
4354 for (l
= regs
; l
; l
= l
->next
) {
4355 if (GPOINTER_TO_UINT (l
->data
) == cfg
->got_var
->dreg
) {
4356 regs
= g_list_delete_link (regs
, l
);
4361 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
4365 //mono_print_code (cfg, "");
4369 /* variables are allocated after decompose, since decompose could create temps */
4370 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4371 mono_arch_allocate_vars (cfg
);
4372 if (cfg
->exception_type
)
4378 gboolean need_local_opts
;
4380 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4381 mono_spill_global_vars (cfg
, &need_local_opts
);
4383 if (need_local_opts
|| cfg
->compile_aot
) {
4384 /* To optimize code created by spill_global_vars */
4385 mono_local_cprop (cfg
);
4386 if (cfg
->opt
& MONO_OPT_DEADCE
)
4387 mono_local_deadce (cfg
);
4391 /* Add branches between non-consecutive bblocks */
4392 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4393 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
4394 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
4395 /* we are careful when inverting, since bugs like #59580
4396 * could show up when dealing with NaNs.
4398 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
4399 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
4400 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
4401 bb
->last_ins
->inst_false_bb
= tmp
;
4403 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
4405 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
4406 inst
->opcode
= OP_BR
;
4407 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
4408 mono_bblock_add_inst (bb
, inst
);
4413 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
4414 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4415 MonoInst
*tree
= bb
->code
;
4416 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
4419 for (; tree
; tree
= tree
->next
) {
4420 mono_print_ins_index (-1, tree
);
4426 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4427 bb
->max_vreg
= cfg
->next_vreg
;
4431 if (COMPILE_LLVM (cfg
)) {
4435 /* The IR has to be in SSA form for LLVM */
4436 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
4437 cfg
->exception_message
= g_strdup ("SSA disabled.");
4438 cfg
->disable_llvm
= TRUE
;
4441 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4442 mono_decompose_array_access_opts (cfg
);
4444 if (!cfg
->disable_llvm
)
4445 mono_llvm_emit_method (cfg
);
4446 if (cfg
->disable_llvm
) {
4447 if (cfg
->verbose_level
>= 1) {
4448 //nm = mono_method_full_name (cfg->method, TRUE);
4449 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
4452 InterlockedIncrement (&methods_without_llvm
);
4453 mono_destroy_compile (cfg
);
4455 goto restart_compile
;
4458 InterlockedIncrement (&methods_with_llvm
);
4460 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
4461 nm
= mono_method_full_name (cfg
->method
, TRUE
);
4462 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
4464 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
4472 if (cfg
->verbose_level
>= 2) {
4473 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
4474 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
4478 cfg
->jit_info
= create_jit_info (cfg
, method_to_compile
);
4480 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
4481 if (cfg
->extend_live_ranges
) {
4482 /* Extend live ranges to cover the whole method */
4483 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
4484 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
4488 mono_save_xdebug_info (cfg
);
4490 mini_gc_create_gc_map (cfg
);
4492 mono_save_seq_point_info (cfg
);
4494 if (!cfg
->compile_aot
) {
4495 mono_domain_lock (cfg
->domain
);
4496 mono_jit_info_table_add (cfg
->domain
, cfg
->jit_info
);
4498 if (cfg
->method
->dynamic
)
4499 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= cfg
->jit_info
;
4500 mono_domain_unlock (cfg
->domain
);
4503 /* collect statistics */
4504 mono_perfcounters
->jit_methods
++;
4505 mono_perfcounters
->jit_bytes
+= header
->code_size
;
4506 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
4507 code_size_ratio
= cfg
->code_len
;
4508 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
4509 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
4510 g_free (mono_jit_stats
.biggest_method
);
4511 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4513 code_size_ratio
= (code_size_ratio
* 100) / header
->code_size
;
4514 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
4515 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
4516 g_free (mono_jit_stats
.max_ratio_method
);
4517 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4519 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
4521 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4522 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4530 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4532 g_assert_not_reached ();
4536 #endif /* DISABLE_JIT */
4539 mono_domain_lookup_shared_generic (MonoDomain
*domain
, MonoMethod
*method
)
4541 static gboolean inited
= FALSE
;
4542 static int lookups
= 0;
4543 static int failed_lookups
= 0;
4546 ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, mini_get_shared_method (method
));
4547 if (ji
&& !ji
->has_generic_jit_info
)
4551 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
4552 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
4564 * LOCKING: Assumes domain->jit_code_hash_lock is held.
4567 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
4569 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
4574 if (!mono_method_is_generic_sharable_impl (method
, FALSE
))
4576 return mono_domain_lookup_shared_generic (domain
, method
);
4580 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
4584 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
4585 mono_domain_jit_code_hash_lock (domain
);
4586 info
= lookup_method_inner (domain
, method
);
4587 mono_domain_jit_code_hash_unlock (domain
);
4588 mono_loader_unlock ();
4594 static FILE* perf_map_file
= NULL
;
4597 mono_enable_jit_map (void)
4599 if (!perf_map_file
) {
4601 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
4603 perf_map_file
= fopen (name
, "w");
4608 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
4611 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
4615 mono_emit_jit_map (MonoJitInfo
*jinfo
)
4617 if (perf_map_file
) {
4618 char *name
= mono_method_full_name (jinfo
->method
, TRUE
);
4619 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
4626 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
4629 gpointer code
= NULL
;
4630 MonoJitInfo
*jinfo
, *info
;
4632 MonoException
*ex
= NULL
;
4633 guint32 prof_options
;
4635 #ifdef MONO_USE_AOT_COMPILER
4636 if (opt
& MONO_OPT_AOT
) {
4637 MonoDomain
*domain
= mono_domain_get ();
4639 mono_class_init (method
->klass
);
4641 if ((code
= mono_aot_get_method (domain
, method
))) {
4642 vtable
= mono_class_vtable (domain
, method
->klass
);
4644 mono_runtime_class_init (vtable
);
4652 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4653 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
4655 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
4657 if (!piinfo
->addr
) {
4658 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
4659 piinfo
->addr
= mono_lookup_internal_call (method
);
4660 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
4662 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
);
4664 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
);
4667 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
4669 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
4670 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4672 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
4673 //mono_debug_add_wrapper (method, nm);
4674 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
4675 const char *name
= method
->name
;
4678 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
4679 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
4680 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
4683 * We need to make sure this wrapper
4684 * is compiled because it might end up
4685 * in an (M)RGCTX if generic sharing
4686 * is enabled, and would be called
4687 * indirectly. If it were a
4688 * trampoline we'd try to patch that
4689 * indirect call, which is not
4692 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
4693 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
4694 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4695 return mono_create_delegate_trampoline (method
->klass
);
4697 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
4698 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4700 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
4701 nm
= mono_marshal_get_delegate_begin_invoke (method
);
4702 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4703 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
4704 nm
= mono_marshal_get_delegate_end_invoke (method
);
4705 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4712 if (mono_aot_only
) {
4713 char *fullname
= mono_method_full_name (method
, TRUE
);
4714 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only.\n", fullname
);
4716 *jit_ex
= mono_get_exception_execution_engine (msg
);
4723 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
4725 switch (cfg
->exception_type
) {
4726 case MONO_EXCEPTION_NONE
:
4728 case MONO_EXCEPTION_TYPE_LOAD
:
4729 case MONO_EXCEPTION_MISSING_FIELD
:
4730 case MONO_EXCEPTION_MISSING_METHOD
:
4731 case MONO_EXCEPTION_FILE_NOT_FOUND
:
4732 case MONO_EXCEPTION_BAD_IMAGE
: {
4733 /* Throw a type load exception if needed */
4734 MonoLoaderError
*error
= mono_loader_get_last_error ();
4737 ex
= mono_loader_error_prepare_exception (error
);
4739 if (cfg
->exception_ptr
) {
4740 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
4742 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
4743 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
4744 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
4745 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
4746 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
4747 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
4748 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
4749 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
4750 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
4751 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
4753 g_assert_not_reached ();
4758 case MONO_EXCEPTION_INVALID_PROGRAM
:
4759 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
4761 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
4762 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
4764 case MONO_EXCEPTION_METHOD_ACCESS
:
4765 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
4767 case MONO_EXCEPTION_FIELD_ACCESS
:
4768 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
4770 /* this can only be set if the security manager is active */
4771 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
4772 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
4773 MonoObject
*exc
= NULL
;
4776 args
[0] = &cfg
->exception_data
;
4778 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
4780 ex
= (MonoException
*)exc
;
4783 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
4784 MonoException
*exp
= cfg
->exception_ptr
;
4785 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
4791 g_assert_not_reached ();
4795 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4796 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
4798 mono_destroy_compile (cfg
);
4804 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
4805 mono_domain_lock (target_domain
);
4807 /* Check if some other thread already did the job. In this case, we can
4808 discard the code this thread generated. */
4810 mono_domain_jit_code_hash_lock (target_domain
);
4812 info
= lookup_method_inner (target_domain
, method
);
4814 /* We can't use a domain specific method in another domain */
4815 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
4816 code
= info
->code_start
;
4817 // printf("Discarding code for method %s\n", method->name);
4822 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
4823 mono_domain_jit_code_hash_unlock (target_domain
);
4824 code
= cfg
->native_code
;
4826 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl (method
, FALSE
))
4827 mono_stats
.generics_shared_methods
++;
4829 mono_domain_jit_code_hash_unlock (target_domain
);
4832 jinfo
= cfg
->jit_info
;
4834 prof_options
= cfg
->prof_options
;
4836 mono_destroy_compile (cfg
);
4839 if (domain_jit_info (target_domain
)->jump_target_hash
) {
4840 MonoJumpInfo patch_info
;
4842 list
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4844 patch_info
.next
= NULL
;
4845 patch_info
.ip
.i
= 0;
4846 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
4847 patch_info
.data
.method
= method
;
4848 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4850 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
4851 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, TRUE
);
4852 g_slist_free (list
);
4855 mono_emit_jit_map (jinfo
);
4857 mono_domain_unlock (target_domain
);
4858 mono_loader_unlock ();
4860 vtable
= mono_class_vtable (target_domain
, method
->klass
);
4863 exc
= mono_class_get_exception_for_failure (method
->klass
);
4865 mono_raise_exception (exc
);
4868 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
) {
4869 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
4870 if (mono_marshal_method_from_wrapper (method
)) {
4871 /* Native func wrappers have no method */
4872 /* The profiler doesn't know about wrappers, so pass the original icall method */
4873 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method
), jinfo
, MONO_PROFILE_OK
);
4876 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
4880 mono_runtime_class_init (vtable
);
4885 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
4887 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
4890 MonoJitICallInfo
*callinfo
= NULL
;
4893 * ICALL wrappers are handled specially, since there is only one copy of them
4894 * shared by all appdomains.
4896 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
4897 const char *icall_name
;
4899 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
4900 g_assert (icall_name
);
4901 callinfo
= mono_find_jit_icall_by_name (icall_name
);
4902 g_assert (callinfo
);
4904 /* Must be domain neutral since there is only one copy */
4905 opt
|= MONO_OPT_SHARED
;
4908 if (opt
& MONO_OPT_SHARED
)
4909 target_domain
= mono_get_root_domain ();
4911 target_domain
= domain
;
4913 info
= lookup_method (target_domain
, method
);
4915 /* We can't use a domain specific method in another domain */
4916 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4919 mono_jit_stats
.methods_lookups
++;
4920 vtable
= mono_class_vtable (domain
, method
->klass
);
4922 mono_runtime_class_init (vtable
);
4923 return mono_create_ftnptr (target_domain
, info
->code_start
);
4927 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
4931 p
= mono_create_ftnptr (target_domain
, code
);
4935 if (!callinfo
->wrapper
) {
4936 callinfo
->wrapper
= p
;
4937 mono_register_jit_icall_wrapper (callinfo
, p
);
4938 mono_debug_add_icall_wrapper (method
, callinfo
);
4947 mono_jit_compile_method (MonoMethod
*method
)
4949 MonoException
*ex
= NULL
;
4952 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
4955 mono_raise_exception (ex
);
4961 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4963 invalidated_delegate_trampoline (char *desc
)
4965 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
4966 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
4972 * mono_jit_free_method:
4974 * Free all memory allocated by the JIT for METHOD.
4977 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
4979 MonoJitDynamicMethodInfo
*ji
;
4980 gboolean destroy
= TRUE
;
4982 g_assert (method
->dynamic
);
4984 mono_domain_lock (domain
);
4985 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
4986 mono_domain_unlock (domain
);
4990 mono_domain_lock (domain
);
4991 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
4992 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
4993 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
4994 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
4995 mono_domain_unlock (domain
);
4997 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4998 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
5000 * Instead of freeing the code, change it to call an error routine
5001 * so people can fix their code.
5003 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
5004 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
5007 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
5013 * This needs to be done before freeing code_mp, since the code address is the
5014 * key in the table, so if we free the code_mp first, another thread can grab the
5015 * same code address and replace our entry in the table.
5017 mono_jit_info_table_remove (domain
, ji
->ji
);
5020 mono_code_manager_destroy (ji
->code_mp
);
5025 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
5027 MonoDomain
*target_domain
;
5030 if (default_opt
& MONO_OPT_SHARED
)
5031 target_domain
= mono_get_root_domain ();
5033 target_domain
= domain
;
5035 info
= lookup_method (target_domain
, method
);
5037 /* We can't use a domain specific method in another domain */
5038 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
5039 mono_jit_stats
.methods_lookups
++;
5042 return info
->code_start
;
5052 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
5054 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
5059 gpointer compiled_method
;
5060 gpointer runtime_invoke
;
5062 MonoDynCallInfo
*dyn_call_info
;
5063 MonoClass
*ret_box_class
;
5064 } RuntimeInvokeInfo
;
5067 * mono_jit_runtime_invoke:
5068 * @method: the method to invoke
5069 * @obj: this pointer
5070 * @params: array of parameter values.
5071 * @exc: used to catch exceptions objects
5074 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
5077 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
5078 MonoDomain
*domain
= mono_domain_get ();
5079 MonoJitDomainInfo
*domain_info
;
5080 RuntimeInvokeInfo
*info
, *info2
;
5082 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
5083 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
5087 domain_info
= domain_jit_info (domain
);
5089 mono_domain_lock (domain
);
5090 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5091 mono_domain_unlock (domain
);
5094 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
5096 * This might be redundant since mono_class_vtable () already does this,
5097 * but keep it just in case for moonlight.
5099 mono_class_setup_vtable (method
->klass
);
5100 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
5102 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
5104 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
5109 info
= g_new0 (RuntimeInvokeInfo
, 1);
5111 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
5112 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
5113 g_assert (info
->vtable
);
5115 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
5116 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
5118 * Array Get/Set/Address methods. The JIT implements them using inline code
5119 * inside the runtime invoke wrappers, so no need to compile them.
5121 info
->compiled_method
= NULL
;
5123 MonoException
*jit_ex
= NULL
;
5125 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
5126 if (!info
->compiled_method
) {
5130 *exc
= (MonoObject
*)jit_ex
;
5133 mono_raise_exception (jit_ex
);
5137 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
5138 info
->compiled_method
= mono_create_static_rgctx_trampoline (method
, info
->compiled_method
);
5142 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
5143 * in full-aot mode, so we use a slower, but more generic wrapper if
5144 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
5146 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5147 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
5148 MonoMethodSignature
*sig
= mono_method_signature (method
);
5149 gboolean supported
= TRUE
;
5152 if (method
->string_ctor
)
5153 sig
= mono_marshal_get_string_ctor_signature (method
);
5155 for (i
= 0; i
< sig
->param_count
; ++i
) {
5156 MonoType
*t
= sig
->params
[i
];
5158 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
5162 if (method
->klass
->contextbound
|| !info
->compiled_method
)
5166 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
5168 if (info
->dyn_call_info
) {
5169 switch (sig
->ret
->type
) {
5170 case MONO_TYPE_VOID
:
5182 case MONO_TYPE_BOOLEAN
:
5183 case MONO_TYPE_CHAR
:
5186 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5189 info
->ret_box_class
= mono_defaults
.int_class
;
5191 case MONO_TYPE_STRING
:
5192 case MONO_TYPE_CLASS
:
5193 case MONO_TYPE_ARRAY
:
5194 case MONO_TYPE_SZARRAY
:
5195 case MONO_TYPE_OBJECT
:
5197 case MONO_TYPE_GENERICINST
:
5198 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
5199 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5201 case MONO_TYPE_VALUETYPE
:
5202 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5205 g_assert_not_reached ();
5212 if (!info
->dyn_call_info
)
5213 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
5215 mono_domain_lock (domain
);
5216 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5221 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
5223 mono_domain_unlock (domain
);
5226 runtime_invoke
= info
->runtime_invoke
;
5229 * We need this here because mono_marshal_get_runtime_invoke can place
5230 * the helper method in System.Object and not the target class.
5232 mono_runtime_class_init (info
->vtable
);
5234 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5235 if (info
->dyn_call_info
) {
5236 MonoMethodSignature
*sig
= mono_method_signature (method
);
5238 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
5241 guint8 retval
[128];
5243 if (!dyn_runtime_invoke
) {
5244 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
5245 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
5248 /* Convert the arguments to the format expected by start_dyn_call () */
5249 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
5252 args
[pindex
++] = &obj
;
5253 for (i
= 0; i
< sig
->param_count
; ++i
) {
5254 MonoType
*t
= sig
->params
[i
];
5257 args
[pindex
++] = ¶ms
[i
];
5258 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
5259 args
[pindex
++] = ¶ms
[i
];
5261 args
[pindex
++] = params
[i
];
5265 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
5267 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
5269 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
5271 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
5273 if (info
->ret_box_class
)
5274 return mono_value_box (domain
, info
->ret_box_class
, retval
);
5276 return *(MonoObject
**)retval
;
5280 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
5284 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
5286 MonoException
*exc
= NULL
;
5288 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
5293 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5295 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
5296 if (mono_arch_is_int_overflow (ctx
, info
))
5297 exc
= mono_get_exception_arithmetic ();
5299 exc
= mono_get_exception_divide_by_zero ();
5301 exc
= mono_get_exception_divide_by_zero ();
5305 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5308 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5311 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5315 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
5320 exc
= mono_get_exception_execution_engine ("SIGILL");
5322 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5325 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
5326 #define HAVE_SIG_INFO
5330 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
5333 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
5337 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
5338 if (mono_arch_is_single_step_event (info
, ctx
)) {
5339 mono_debugger_agent_single_step_event (ctx
);
5341 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
5342 mono_debugger_agent_breakpoint_hit (ctx
);
5347 #if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
5348 if (mono_aot_is_pagefault (info
->si_addr
)) {
5349 mono_aot_handle_pagefault (info
->si_addr
);
5354 /* The thread might no be registered with the runtime */
5355 if (!mono_domain_get () || !jit_tls
) {
5356 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5358 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5361 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5363 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
5364 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
5367 /* The hard-guard page has been hit: there is not much we can do anymore
5368 * Print a hopefully clear message and abort.
5370 if (jit_tls
->stack_size
&&
5371 ABS ((guint8
*)info
->si_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 32768) {
5373 /* we don't do much now, but we can warn the user with a useful message */
5374 fprintf (stderr
, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx
), (gpointer
)info
->si_addr
);
5375 if (ji
&& ji
->method
)
5376 method
= mono_method_full_name (ji
->method
, TRUE
);
5378 method
= "Unmanaged";
5379 fprintf (stderr
, "At %s\n", method
);
5382 /* The original handler might not like that it is executed on an altstack... */
5383 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
5386 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
5391 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5394 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5397 mono_arch_handle_exception (ctx
, NULL
, FALSE
);
5402 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
5407 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
5409 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5412 /* mono_jit_create_remoting_trampoline:
5413 * @method: pointer to the method info
5415 * Creates a trampoline which calls the remoting functions. This
5416 * is used in the vtable of transparent proxies.
5418 * Returns: a pointer to the newly created code
5421 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
5424 guint8
*addr
= NULL
;
5426 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
5427 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
5431 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
5432 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
5433 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
5434 addr
= mono_compile_method (nm
);
5436 addr
= mono_compile_method (method
);
5438 return mono_get_addr_from_ftnptr (addr
);
5441 #ifdef MONO_ARCH_HAVE_IMT
5442 static G_GNUC_UNUSED gpointer
5443 mini_get_imt_trampoline (void)
5445 static gpointer tramp
= NULL
;
5447 tramp
= mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5453 mini_get_vtable_trampoline (int slot_index
)
5455 static gpointer tramp
= NULL
;
5458 tramp
= mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5463 mini_parse_debug_options (void)
5465 char *options
= getenv ("MONO_DEBUG");
5466 gchar
**args
, **ptr
;
5471 args
= g_strsplit (options
, ",", -1);
5473 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
5474 const char *arg
= *ptr
;
5476 if (!strcmp (arg
, "handle-sigint"))
5477 debug_options
.handle_sigint
= TRUE
;
5478 else if (!strcmp (arg
, "keep-delegates"))
5479 debug_options
.keep_delegates
= TRUE
;
5480 else if (!strcmp (arg
, "collect-pagefault-stats"))
5481 debug_options
.collect_pagefault_stats
= TRUE
;
5482 else if (!strcmp (arg
, "break-on-unverified"))
5483 debug_options
.break_on_unverified
= TRUE
;
5484 else if (!strcmp (arg
, "no-gdb-backtrace"))
5485 debug_options
.no_gdb_backtrace
= TRUE
;
5486 else if (!strcmp (arg
, "suspend-on-sigsegv"))
5487 debug_options
.suspend_on_sigsegv
= TRUE
;
5488 else if (!strcmp (arg
, "dont-free-domains"))
5489 mono_dont_free_domains
= TRUE
;
5490 else if (!strcmp (arg
, "dyn-runtime-invoke"))
5491 debug_options
.dyn_runtime_invoke
= TRUE
;
5492 else if (!strcmp (arg
, "gdb"))
5493 debug_options
.gdb
= TRUE
;
5494 else if (!strcmp (arg
, "explicit-null-checks"))
5495 debug_options
.explicit_null_checks
= TRUE
;
5496 else if (!strcmp (arg
, "gen-seq-points"))
5497 debug_options
.gen_seq_points
= TRUE
;
5498 else if (!strcmp (arg
, "init-stacks"))
5499 debug_options
.init_stacks
= TRUE
;
5501 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
5502 fprintf (stderr
, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains', 'suspend-on-sigsegv', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'init-stacks'\n");
5511 mini_get_debug_options (void)
5513 return &debug_options
;
5517 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
5522 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
5528 #elif defined(__ppc64__) || defined(__powerpc64__)
5531 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
5544 mini_get_addr_from_ftnptr (gpointer descr
)
5546 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
5547 return *(gpointer
*)descr
;
5553 static void runtime_invoke_info_free (gpointer value
);
5556 mini_create_jit_domain_info (MonoDomain
*domain
)
5558 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
5560 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5561 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5562 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5563 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5564 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5565 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
5566 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
5567 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5569 domain
->runtime_info
= info
;
5573 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
5575 g_slist_free (value
);
5579 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
5581 MonoJitDynamicMethodInfo
*di
= value
;
5582 mono_code_manager_destroy (di
->code_mp
);
5587 runtime_invoke_info_free (gpointer value
)
5589 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
5591 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5592 if (info
->dyn_call_info
)
5593 mono_arch_dyn_call_free (info
->dyn_call_info
);
5599 mini_free_jit_domain_info (MonoDomain
*domain
)
5601 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
5603 if (info
->jump_target_hash
) {
5604 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
5605 g_hash_table_destroy (info
->jump_target_hash
);
5607 if (info
->jump_target_got_slot_hash
) {
5608 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
5609 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
5611 if (info
->dynamic_code_hash
) {
5612 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
5613 g_hash_table_destroy (info
->dynamic_code_hash
);
5615 if (info
->method_code_hash
)
5616 g_hash_table_destroy (info
->method_code_hash
);
5617 g_hash_table_destroy (info
->class_init_trampoline_hash
);
5618 g_hash_table_destroy (info
->jump_trampoline_hash
);
5619 g_hash_table_destroy (info
->jit_trampoline_hash
);
5620 g_hash_table_destroy (info
->delegate_trampoline_hash
);
5621 if (info
->static_rgctx_trampoline_hash
)
5622 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
5623 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
5624 g_hash_table_destroy (info
->runtime_invoke_hash
);
5625 g_hash_table_destroy (info
->seq_points
);
5626 g_hash_table_destroy (info
->arch_seq_points
);
5628 if (info
->agent_info
)
5629 mono_debugger_agent_free_domain_info (domain
);
5631 g_free (domain
->runtime_info
);
5632 domain
->runtime_info
= NULL
;
5636 mini_init (const char *filename
, const char *runtime_version
)
5639 MonoRuntimeCallbacks callbacks
;
5641 MONO_PROBE_VES_INIT_BEGIN ();
5644 if (access ("/proc/self/maps", F_OK
) != 0) {
5645 g_print ("Mono requires /proc to be mounted.\n");
5650 /* Happens when using the embedding interface */
5651 if (!default_opt_set
)
5652 default_opt
= mono_parse_default_optimizations (NULL
);
5654 InitializeCriticalSection (&jit_mutex
);
5656 #ifdef MONO_DEBUGGER_SUPPORTED
5657 if (mini_debug_running_inside_mdb ())
5658 mini_debugger_init ();
5661 #ifdef MONO_ARCH_HAVE_TLS_GET
5662 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET
);
5664 mono_runtime_set_has_tls_get (FALSE
);
5667 if (!global_codeman
)
5668 global_codeman
= mono_code_manager_new ();
5669 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
5671 memset (&callbacks
, 0, sizeof (callbacks
));
5672 callbacks
.create_ftnptr
= mini_create_ftnptr
;
5673 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
5674 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
5676 #ifdef MONO_ARCH_HAVE_IMT
5678 if (!mono_use_llvm
) {
5679 /* LLVM needs a per-method vtable trampoline */
5680 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
5685 mono_install_callbacks (&callbacks
);
5687 mono_arch_cpu_init ();
5691 mono_unwind_init ();
5695 if (getenv ("MONO_DEBUG") != NULL
)
5696 mini_parse_debug_options ();
5698 if (getenv ("MONO_XDEBUG")) {
5699 char *xdebug_opts
= getenv ("MONO_XDEBUG");
5700 mono_xdebug_init (xdebug_opts
);
5701 /* So methods for multiple domains don't have the same address */
5702 mono_dont_free_domains
= TRUE
;
5703 mono_using_xdebug
= TRUE
;
5704 } else if (mini_get_debug_options ()->gdb
) {
5705 mono_xdebug_init ((char*)"gdb");
5706 mono_dont_free_domains
= TRUE
;
5707 mono_using_xdebug
= TRUE
;
5714 mono_trampolines_init ();
5716 if (!g_thread_supported ())
5717 g_thread_init (NULL
);
5719 mono_jit_tls_id
= TlsAlloc ();
5720 setup_jit_tls_data ((gpointer
)-1, mono_thread_abort
);
5722 if (default_opt
& MONO_OPT_AOT
)
5725 mono_debugger_agent_init ();
5727 #ifdef MONO_ARCH_GSHARED_SUPPORTED
5728 mono_set_generic_sharing_supported (TRUE
);
5731 #ifndef MONO_CROSS_COMPILE
5732 mono_runtime_install_handlers ();
5734 mono_threads_install_cleanup (mini_thread_cleanup
);
5736 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
5737 // This is experimental code so provide an env var to switch it off
5738 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
5739 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
5741 check_for_pending_exc
= FALSE
;
5742 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
5746 #define JIT_TRAMPOLINES_WORK
5747 #ifdef JIT_TRAMPOLINES_WORK
5748 mono_install_compile_method (mono_jit_compile_method
);
5749 mono_install_free_method (mono_jit_free_method
);
5750 #ifdef MONO_ARCH_LLVM_SUPPORTED
5752 /* The runtime currently only uses this for filling out vtables */
5753 mono_install_trampoline (mono_create_llvm_vcall_trampoline
);
5755 mono_install_trampoline (mono_create_jit_trampoline
);
5757 mono_install_trampoline (mono_create_jit_trampoline
);
5759 mono_install_jump_trampoline (mono_create_jump_trampoline
);
5760 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
5761 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
5762 mono_install_create_domain_hook (mini_create_jit_domain_info
);
5763 mono_install_free_domain_hook (mini_free_jit_domain_info
);
5765 #define JIT_INVOKE_WORKS
5766 #ifdef JIT_INVOKE_WORKS
5767 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
5769 mono_install_stack_walk (mono_jit_walk_stack
);
5770 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
5771 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
5772 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
5774 if (debug_options
.collect_pagefault_stats
) {
5775 mono_aot_set_make_unreadable (TRUE
);
5778 if (runtime_version
)
5779 domain
= mono_init_version (filename
, runtime_version
);
5781 domain
= mono_init_from_assembly (filename
, filename
);
5783 if (mono_aot_only
) {
5784 /* This helps catch code allocation requests */
5785 mono_code_manager_set_read_only (domain
->code_mp
);
5788 #ifdef MONO_ARCH_HAVE_IMT
5791 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
5793 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
5794 if (!mono_use_llvm
) {
5796 * The imt code in mono_magic_trampoline () can't handle LLVM code. By disabling
5797 * this, we force iface calls to go through the llvm vcall trampoline.
5799 mono_install_imt_trampoline (mini_get_imt_trampoline ());
5804 /* This must come after mono_init () in the aot-only case */
5805 mono_exceptions_init ();
5806 mono_install_handler (mono_get_throw_exception ());
5810 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
5811 ves_icall_get_frame_info
);
5812 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
5813 ves_icall_get_trace
);
5814 mono_add_internal_call ("System.Exception::get_trace",
5815 ves_icall_System_Exception_get_trace
);
5816 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
5817 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
5818 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
5819 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
5820 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
5821 mono_runtime_install_handlers
);
5824 create_helper_signature ();
5826 #define JIT_CALLS_WORK
5827 #ifdef JIT_CALLS_WORK
5828 /* Needs to be called here since register_jit_icall depends on it */
5829 mono_marshal_init ();
5831 mono_arch_register_lowlevel_calls ();
5832 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
5833 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
5834 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
5835 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
5836 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
5837 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "void", TRUE
);
5838 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
5840 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
5841 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
5842 #ifdef MONO_ARCH_HAVE_THROW_EXCEPTION_BY_NAME
5843 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE
);
5845 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
5846 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
5849 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
5850 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
5851 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
5852 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
5853 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
5856 * NOTE, NOTE, NOTE, NOTE:
5857 * when adding emulation for some opcodes, remember to also add a dummy
5858 * rule to the burg files, because we need the arity information to be correct.
5860 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
5861 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
5862 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
5863 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
5864 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
5865 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
5866 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
5867 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
5870 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
5871 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
5872 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
5873 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
5876 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
5877 mono_register_opcode_emulation (CEE_DIV
, "__emul_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5878 mono_register_opcode_emulation (CEE_DIV_UN
, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5879 mono_register_opcode_emulation (CEE_REM
, "__emul_irem", "int32 int32 int32", mono_irem
, FALSE
);
5880 mono_register_opcode_emulation (CEE_REM_UN
, "__emul_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5881 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5882 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5883 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
5884 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5887 #ifdef MONO_ARCH_EMULATE_MUL_DIV
5888 mono_register_opcode_emulation (CEE_MUL
, "__emul_imul", "int32 int32 int32", mono_imul
, TRUE
);
5889 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
5892 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
5893 mono_register_opcode_emulation (CEE_MUL_OVF
, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5894 mono_register_opcode_emulation (CEE_MUL_OVF_UN
, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5895 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5896 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5899 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
5900 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
5903 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
5904 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
5905 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
5906 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
5908 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
5909 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
5911 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
5912 mono_register_opcode_emulation (CEE_CONV_R_UN
, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5913 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5915 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
5916 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
5918 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
5919 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
5921 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
5922 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
5924 #ifdef MONO_ARCH_EMULATE_FREM
5925 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
5928 #ifdef MONO_ARCH_SOFT_FLOAT
5929 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
5930 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
5931 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
5932 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
5933 mono_register_opcode_emulation (CEE_CONV_R8
, "__emul_conv_r8", "double int32", mono_conv_to_r8
, FALSE
);
5934 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
5935 mono_register_opcode_emulation (CEE_CONV_R4
, "__emul_conv_r4", "double int32", mono_conv_to_r4
, FALSE
);
5936 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
5937 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
5938 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
5939 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
5940 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
5941 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
5942 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
5943 #if SIZEOF_VOID_P == 4
5944 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
5947 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
5948 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
5949 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
5950 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
5951 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
5952 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
5953 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
5954 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
5955 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
5956 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
5958 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
5959 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
5960 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
5961 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
5962 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
5964 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
5965 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
5966 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
5967 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
5970 #ifdef COMPRESSED_INTERFACE_BITMAP
5971 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
5974 #if SIZEOF_REGISTER == 4
5975 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
5978 /* other jit icalls */
5979 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
5980 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
5981 "ptr ptr ptr", FALSE
);
5982 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
5983 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
5984 "ptr ptr ptr ptr", FALSE
);
5985 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
5986 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
5987 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
5988 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
5989 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
5990 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
5991 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
5992 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
5993 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
5994 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
5995 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
5996 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
5997 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
5998 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
5999 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
6000 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
6001 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
6002 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
6003 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
6004 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
6005 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
6006 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
6007 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
6008 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
6009 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
6012 mono_generic_sharing_init ();
6014 #ifdef MONO_ARCH_SIMD_INTRINSICS
6015 mono_simd_intrinsics_init ();
6018 #if MONO_SUPPORT_TASKLETS
6019 mono_tasklets_init ();
6022 if (mono_compile_aot
)
6024 * Avoid running managed code when AOT compiling, since the platform
6025 * might only support aot-only execution.
6027 mono_runtime_set_no_exec (TRUE
);
6029 #define JIT_RUNTIME_WORKS
6030 #ifdef JIT_RUNTIME_WORKS
6031 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
6032 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
6033 mono_thread_attach (domain
);
6036 mono_profiler_runtime_initialized ();
6038 MONO_PROBE_VES_INIT_END ();
6043 MonoJitStats mono_jit_stats
= {0};
6046 print_jit_stats (void)
6048 if (mono_jit_stats
.enabled
) {
6049 g_print ("Mono Jit statistics\n");
6050 g_print ("Compiled methods: %ld\n", mono_jit_stats
.methods_compiled
);
6051 g_print ("Methods from AOT: %ld\n", mono_jit_stats
.methods_aot
);
6052 g_print ("Methods cache lookup: %ld\n", mono_jit_stats
.methods_lookups
);
6053 g_print ("Method trampolines: %ld\n", mono_jit_stats
.method_trampolines
);
6054 g_print ("Basic blocks: %ld\n", mono_jit_stats
.basic_blocks
);
6055 g_print ("Max basic blocks: %ld\n", mono_jit_stats
.max_basic_blocks
);
6056 g_print ("Allocated vars: %ld\n", mono_jit_stats
.allocate_var
);
6057 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats
.cil_code_size
);
6058 g_print ("Native code size: %ld\n", mono_jit_stats
.native_code_size
);
6059 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
6060 mono_jit_stats
.max_ratio_method
);
6061 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
6062 mono_jit_stats
.biggest_method
);
6063 g_print ("Code reallocs: %ld\n", mono_jit_stats
.code_reallocs
);
6064 g_print ("Allocated code size: %ld\n", mono_jit_stats
.allocated_code_size
);
6065 g_print ("Inlineable methods: %ld\n", mono_jit_stats
.inlineable_methods
);
6066 g_print ("Inlined methods: %ld\n", mono_jit_stats
.inlined_methods
);
6067 g_print ("Regvars: %ld\n", mono_jit_stats
.regvars
);
6068 g_print ("Locals stack size: %ld\n", mono_jit_stats
.locals_stack_size
);
6070 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
6071 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
6072 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
6073 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
6074 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
6075 g_print ("Methods: %ld\n", mono_stats
.method_count
);
6076 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
6077 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
6078 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
6080 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
6081 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
6082 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
6084 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
6085 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
6086 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
6088 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
6089 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
6090 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
6092 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
6093 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
6094 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
6095 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
6096 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
6097 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
6098 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
6099 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
6101 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
6102 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
6103 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
6105 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
6107 g_print ("Minor GC collections: %ld\n", mono_stats
.minor_gc_count
);
6109 g_print ("Major GC collections: %ld\n", mono_stats
.major_gc_count
);
6111 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats
.minor_gc_time_usecs
/ 1000.0);
6113 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats
.major_gc_time_usecs
/ 1000.0);
6114 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
6115 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
6116 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
6117 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
6118 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
6119 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
6120 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
6123 g_free (mono_jit_stats
.max_ratio_method
);
6124 mono_jit_stats
.max_ratio_method
= NULL
;
6125 g_free (mono_jit_stats
.biggest_method
);
6126 mono_jit_stats
.biggest_method
= NULL
;
6131 mini_cleanup (MonoDomain
*domain
)
6133 mono_runtime_shutdown_stat_profiler ();
6136 cominterop_release_all_rcws ();
6139 #ifndef MONO_CROSS_COMPILE
6141 * mono_runtime_cleanup() and mono_domain_finalize () need to
6142 * be called early since they need the execution engine still
6143 * fully working (mono_domain_finalize may invoke managed finalizers
6144 * and mono_runtime_cleanup will wait for other threads to finish).
6146 mono_domain_finalize (domain
, 2000);
6149 /* This accesses metadata so needs to be called before runtime shutdown */
6152 mono_profiler_shutdown ();
6154 #ifndef MONO_CROSS_COMPILE
6155 mono_runtime_cleanup (domain
);
6158 mono_icall_cleanup ();
6160 mono_runtime_cleanup_handlers ();
6162 mono_domain_free (domain
, TRUE
);
6164 mono_debugger_cleanup ();
6167 mono_llvm_cleanup ();
6170 mono_trampolines_cleanup ();
6172 mono_unwind_cleanup ();
6174 if (!mono_dont_free_global_codeman
)
6175 mono_code_manager_destroy (global_codeman
);
6176 g_hash_table_destroy (jit_icall_name_hash
);
6177 g_free (emul_opcode_map
);
6178 g_free (emul_opcode_opcodes
);
6180 mono_arch_cleanup ();
6184 mono_trace_cleanup ();
6186 mono_counters_dump (-1, stdout
);
6188 if (mono_inject_async_exc_method
)
6189 mono_method_desc_free (mono_inject_async_exc_method
);
6191 TlsFree(mono_jit_tls_id
);
6193 DeleteCriticalSection (&jit_mutex
);
6195 DeleteCriticalSection (&mono_delegate_section
);
6199 mono_set_defaults (int verbose_level
, guint32 opts
)
6201 mini_verbose
= verbose_level
;
6203 default_opt_set
= TRUE
;
6207 mono_disable_optimizations (guint32 opts
)
6209 default_opt
&= ~opts
;
6213 * mono_get_runtime_build_info:
6215 * Return the runtime version + build date in string format.
6216 * The returned string is owned by the caller.
6219 mono_get_runtime_build_info (void)
6221 if (mono_build_date
)
6222 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
6224 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
6228 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
6230 GHashTable
*assemblies
= (GHashTable
*)user_data
;
6231 MonoImage
*image
= mono_assembly_get_image (ass
);
6232 MonoMethod
*method
, *invoke
;
6235 if (g_hash_table_lookup (assemblies
, ass
))
6238 g_hash_table_insert (assemblies
, ass
, ass
);
6240 if (mini_verbose
> 0)
6241 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
6243 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
6244 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
6245 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
6249 if (mini_verbose
> 1) {
6250 char * desc
= mono_method_full_name (method
, TRUE
);
6251 g_print ("Compiling %d %s\n", count
, desc
);
6254 mono_compile_method (method
);
6255 if (strcmp (method
->name
, "Finalize") == 0) {
6256 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
6257 mono_compile_method (invoke
);
6259 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
6260 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
6261 mono_compile_method (invoke
);
6265 /* Load and precompile referenced assemblies as well */
6266 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
6267 mono_assembly_load_reference (image
, i
);
6268 if (image
->references
[i
])
6269 mono_precompile_assembly (image
->references
[i
], assemblies
);
6273 void mono_precompile_assemblies ()
6275 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
6277 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
6279 g_hash_table_destroy (assemblies
);
6285 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
6286 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);
6290 mono_cfg_add_try_hole (MonoCompile
*cfg
, MonoExceptionClause
*clause
, guint8
*start
, MonoBasicBlock
*bb
)
6292 TryBlockHole
*hole
= mono_mempool_alloc (cfg
->mempool
, sizeof (TryBlockHole
));
6293 hole
->clause
= clause
;
6294 hole
->start_offset
= start
- cfg
->native_code
;
6295 hole
->basic_block
= bb
;
6297 cfg
->try_block_holes
= g_slist_append_mempool (cfg
->mempool
, cfg
->try_block_holes
, hole
);