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
117 * defaults to FALSE if ENABLE_LLVM is not defined, i.e. the runtime is only capable of
118 * running AOT code compiled by LLVM.
119 * Changes when this flag is set include:
120 * - a per method vtable trampoline is used to handle virtual calls, instead of only
122 * - fast generic virtual calls are not supported.
125 gboolean mono_use_llvm
= TRUE
;
127 gboolean mono_use_llvm
= FALSE
;
130 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
131 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
132 static CRITICAL_SECTION jit_mutex
;
134 static MonoCodeManager
*global_codeman
= NULL
;
136 static GHashTable
*jit_icall_name_hash
= NULL
;
138 static MonoDebugOptions debug_options
;
140 #ifdef VALGRIND_JIT_REGISTER_MAP
141 static int valgrind_register
= 0;
145 * Table written to by the debugger with a 1-based index into the
146 * mono_breakpoint_info table, which contains changes made to
147 * the JIT instructions by the debugger.
150 mono_breakpoint_info_index
[MONO_BREAKPOINT_ARRAY_SIZE
];
152 /* Whenever to check for pending exceptions in managed-to-native wrappers */
153 gboolean check_for_pending_exc
= TRUE
;
155 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
156 gboolean disable_vtypes_in_regs
= FALSE
;
158 gboolean mono_dont_free_global_codeman
;
161 mono_running_on_valgrind (void)
163 if (RUNNING_ON_VALGRIND
){
164 #ifdef VALGRIND_JIT_REGISTER_MAP
165 valgrind_register
= TRUE
;
173 MonoExceptionClause
*clause
;
174 MonoBasicBlock
*basic_block
;
184 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
186 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
189 ud
->method
= (MonoMethod
*)key
;
193 G_GNUC_UNUSED
static char*
194 get_method_from_ip (void *ip
)
199 MonoDomain
*domain
= mono_domain_get ();
200 MonoDebugSourceLocation
*location
;
201 FindTrampUserData user_data
;
203 ji
= mono_jit_info_table_find (domain
, ip
);
206 user_data
.method
= NULL
;
207 mono_domain_lock (domain
);
208 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
209 mono_domain_unlock (domain
);
210 if (user_data
.method
) {
211 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
212 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
219 method
= mono_method_full_name (ji
->method
, TRUE
);
220 /* FIXME: unused ? */
221 location
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
223 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
);
225 mono_debug_free_source_location (location
);
233 * @ip: an instruction pointer address
235 * This method is used from a debugger to get the name of the
236 * method at address @ip. This routine is typically invoked from
237 * a debugger like this:
239 * (gdb) print mono_pmip ($pc)
241 * Returns: the name of the method at address @ip.
246 return get_method_from_ip (ip
);
250 * mono_print_method_from_ip
251 * @ip: an instruction pointer address
253 * This method is used from a debugger to get the name of the
254 * method at address @ip.
256 * This prints the name of the method at address @ip in the standard
257 * output. Unlike mono_pmip which returns a string, this routine
258 * prints the value on the standard output.
261 mono_print_method_from_ip (void *ip
)
265 MonoDebugSourceLocation
*source
;
266 MonoDomain
*domain
= mono_domain_get ();
267 MonoDomain
*target_domain
= mono_domain_get ();
268 FindTrampUserData user_data
;
270 ji
= mini_jit_info_table_find (domain
, ip
, &target_domain
);
273 user_data
.method
= NULL
;
274 mono_domain_lock (domain
);
275 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
276 mono_domain_unlock (domain
);
277 if (user_data
.method
) {
278 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
279 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
283 g_print ("No method at %p\n", ip
);
286 method
= mono_method_full_name (ji
->method
, TRUE
);
287 source
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
289 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
);
292 g_print ("%s:%d\n", source
->source_file
, source
->row
);
294 mono_debug_free_source_location (source
);
299 * mono_method_same_domain:
301 * Determine whenever two compiled methods are in the same domain, thus
302 * the address of the callee can be embedded in the caller.
304 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
306 if (!caller
|| !callee
)
310 * If the call was made from domain-neutral to domain-specific
311 * code, we can't patch the call site.
313 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
316 if ((caller
->method
->klass
== mono_defaults
.appdomain_class
) &&
317 (strstr (caller
->method
->name
, "InvokeInDomain"))) {
318 /* The InvokeInDomain methods change the current appdomain */
326 * mono_global_codeman_reserve:
328 * Allocate code memory from the global code manager.
330 void *mono_global_codeman_reserve (int size
)
335 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
337 if (!global_codeman
) {
338 /* This can happen during startup */
339 global_codeman
= mono_code_manager_new ();
340 return mono_code_manager_reserve (global_codeman
, size
);
344 ptr
= mono_code_manager_reserve (global_codeman
, size
);
351 * mono_create_unwind_op:
353 * Create an unwind op with the given parameters.
356 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
358 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
369 * mono_emit_unwind_op:
371 * Add an unwind op with the given parameters for the list of unwind ops stored in
375 mono_emit_unwind_op (MonoCompile
*cfg
, int when
, int tag
, int reg
, int val
)
377 MonoUnwindOp
*op
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoUnwindOp
));
384 cfg
->unwind_ops
= g_slist_append_mempool (cfg
->mempool
, cfg
->unwind_ops
, op
);
388 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
390 MonoJumpInfoToken
*res
= mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
393 res
->has_context
= context
!= NULL
;
395 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
401 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
403 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
406 #define MONO_INIT_VARINFO(vi,id) do { \
407 (vi)->range.first_use.pos.bid = 0xffff; \
413 * mono_unlink_bblock:
415 * Unlink two basic blocks.
418 mono_unlink_bblock (MonoCompile
*cfg
, MonoBasicBlock
*from
, MonoBasicBlock
* to
)
424 for (i
= 0; i
< from
->out_count
; ++i
) {
425 if (to
== from
->out_bb
[i
]) {
432 for (i
= 0; i
< from
->out_count
; ++i
) {
433 if (from
->out_bb
[i
] != to
)
434 from
->out_bb
[pos
++] = from
->out_bb
[i
];
436 g_assert (pos
== from
->out_count
- 1);
441 for (i
= 0; i
< to
->in_count
; ++i
) {
442 if (from
== to
->in_bb
[i
]) {
449 for (i
= 0; i
< to
->in_count
; ++i
) {
450 if (to
->in_bb
[i
] != from
)
451 to
->in_bb
[pos
++] = to
->in_bb
[i
];
453 g_assert (pos
== to
->in_count
- 1);
459 * mono_bblocks_linked:
461 * Return whenever BB1 and BB2 are linked in the CFG.
464 mono_bblocks_linked (MonoBasicBlock
*bb1
, MonoBasicBlock
*bb2
)
468 for (i
= 0; i
< bb1
->out_count
; ++i
) {
469 if (bb1
->out_bb
[i
] == bb2
)
477 mono_find_block_region_notry (MonoCompile
*cfg
, int offset
)
479 MonoMethodHeader
*header
= cfg
->header
;
480 MonoExceptionClause
*clause
;
483 for (i
= 0; i
< header
->num_clauses
; ++i
) {
484 clause
= &header
->clauses
[i
];
485 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) && (offset
>= clause
->data
.filter_offset
) &&
486 (offset
< (clause
->handler_offset
)))
487 return ((i
+ 1) << 8) | MONO_REGION_FILTER
| clause
->flags
;
489 if (MONO_OFFSET_IN_HANDLER (clause
, offset
)) {
490 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
491 return ((i
+ 1) << 8) | MONO_REGION_FINALLY
| clause
->flags
;
492 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
)
493 return ((i
+ 1) << 8) | MONO_REGION_FAULT
| clause
->flags
;
495 return ((i
+ 1) << 8) | MONO_REGION_CATCH
| clause
->flags
;
503 * mono_get_block_region_notry:
505 * Return the region corresponding to REGION, ignoring try clauses nested inside
509 mono_get_block_region_notry (MonoCompile
*cfg
, int region
)
511 if ((region
& (0xf << 4)) == MONO_REGION_TRY
) {
512 MonoMethodHeader
*header
= cfg
->header
;
515 * This can happen if a try clause is nested inside a finally clause.
517 int clause_index
= (region
>> 8) - 1;
518 g_assert (clause_index
>= 0 && clause_index
< header
->num_clauses
);
520 region
= mono_find_block_region_notry (cfg
, header
->clauses
[clause_index
].try_offset
);
527 mono_find_spvar_for_region (MonoCompile
*cfg
, int region
)
529 region
= mono_get_block_region_notry (cfg
, region
);
531 return g_hash_table_lookup (cfg
->spvars
, GINT_TO_POINTER (region
));
535 df_visit (MonoBasicBlock
*start
, int *dfn
, MonoBasicBlock
**array
)
539 array
[*dfn
] = start
;
540 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
541 for (i
= 0; i
< start
->out_count
; ++i
) {
542 if (start
->out_bb
[i
]->dfn
)
545 start
->out_bb
[i
]->dfn
= *dfn
;
546 start
->out_bb
[i
]->df_parent
= start
;
547 array
[*dfn
] = start
->out_bb
[i
];
548 df_visit (start
->out_bb
[i
], dfn
, array
);
553 mono_reverse_branch_op (guint32 opcode
)
555 static const int reverse_map
[] = {
556 CEE_BNE_UN
, CEE_BLT
, CEE_BLE
, CEE_BGT
, CEE_BGE
,
557 CEE_BEQ
, CEE_BLT_UN
, CEE_BLE_UN
, CEE_BGT_UN
, CEE_BGE_UN
559 static const int reverse_fmap
[] = {
560 OP_FBNE_UN
, OP_FBLT
, OP_FBLE
, OP_FBGT
, OP_FBGE
,
561 OP_FBEQ
, OP_FBLT_UN
, OP_FBLE_UN
, OP_FBGT_UN
, OP_FBGE_UN
563 static const int reverse_lmap
[] = {
564 OP_LBNE_UN
, OP_LBLT
, OP_LBLE
, OP_LBGT
, OP_LBGE
,
565 OP_LBEQ
, OP_LBLT_UN
, OP_LBLE_UN
, OP_LBGT_UN
, OP_LBGE_UN
567 static const int reverse_imap
[] = {
568 OP_IBNE_UN
, OP_IBLT
, OP_IBLE
, OP_IBGT
, OP_IBGE
,
569 OP_IBEQ
, OP_IBLT_UN
, OP_IBLE_UN
, OP_IBGT_UN
, OP_IBGE_UN
572 if (opcode
>= CEE_BEQ
&& opcode
<= CEE_BLT_UN
) {
573 opcode
= reverse_map
[opcode
- CEE_BEQ
];
574 } else if (opcode
>= OP_FBEQ
&& opcode
<= OP_FBLT_UN
) {
575 opcode
= reverse_fmap
[opcode
- OP_FBEQ
];
576 } else if (opcode
>= OP_LBEQ
&& opcode
<= OP_LBLT_UN
) {
577 opcode
= reverse_lmap
[opcode
- OP_LBEQ
];
578 } else if (opcode
>= OP_IBEQ
&& opcode
<= OP_IBLT_UN
) {
579 opcode
= reverse_imap
[opcode
- OP_IBEQ
];
581 g_assert_not_reached ();
587 mono_type_to_store_membase (MonoCompile
*cfg
, MonoType
*type
)
590 return OP_STORE_MEMBASE_REG
;
593 switch (type
->type
) {
596 case MONO_TYPE_BOOLEAN
:
597 return OP_STOREI1_MEMBASE_REG
;
601 return OP_STOREI2_MEMBASE_REG
;
604 return OP_STOREI4_MEMBASE_REG
;
608 case MONO_TYPE_FNPTR
:
609 return OP_STORE_MEMBASE_REG
;
610 case MONO_TYPE_CLASS
:
611 case MONO_TYPE_STRING
:
612 case MONO_TYPE_OBJECT
:
613 case MONO_TYPE_SZARRAY
:
614 case MONO_TYPE_ARRAY
:
615 return OP_STORE_MEMBASE_REG
;
618 return OP_STOREI8_MEMBASE_REG
;
620 return OP_STORER4_MEMBASE_REG
;
622 return OP_STORER8_MEMBASE_REG
;
623 case MONO_TYPE_VALUETYPE
:
624 if (type
->data
.klass
->enumtype
) {
625 type
= mono_class_enum_basetype (type
->data
.klass
);
628 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
629 return OP_STOREX_MEMBASE
;
630 return OP_STOREV_MEMBASE
;
631 case MONO_TYPE_TYPEDBYREF
:
632 return OP_STOREV_MEMBASE
;
633 case MONO_TYPE_GENERICINST
:
634 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
638 /* FIXME: all the arguments must be references for now,
639 * later look inside cfg and see if the arg num is
642 g_assert (cfg
->generic_sharing_context
);
643 return OP_STORE_MEMBASE_REG
;
645 g_error ("unknown type 0x%02x in type_to_store_membase", type
->type
);
651 mono_type_to_load_membase (MonoCompile
*cfg
, MonoType
*type
)
654 return OP_LOAD_MEMBASE
;
656 type
= mono_type_get_underlying_type (type
);
658 switch (type
->type
) {
660 return OP_LOADI1_MEMBASE
;
662 case MONO_TYPE_BOOLEAN
:
663 return OP_LOADU1_MEMBASE
;
665 return OP_LOADI2_MEMBASE
;
668 return OP_LOADU2_MEMBASE
;
670 return OP_LOADI4_MEMBASE
;
672 return OP_LOADU4_MEMBASE
;
676 case MONO_TYPE_FNPTR
:
677 return OP_LOAD_MEMBASE
;
678 case MONO_TYPE_CLASS
:
679 case MONO_TYPE_STRING
:
680 case MONO_TYPE_OBJECT
:
681 case MONO_TYPE_SZARRAY
:
682 case MONO_TYPE_ARRAY
:
683 return OP_LOAD_MEMBASE
;
686 return OP_LOADI8_MEMBASE
;
688 return OP_LOADR4_MEMBASE
;
690 return OP_LOADR8_MEMBASE
;
691 case MONO_TYPE_VALUETYPE
:
692 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
693 return OP_LOADX_MEMBASE
;
694 case MONO_TYPE_TYPEDBYREF
:
695 return OP_LOADV_MEMBASE
;
696 case MONO_TYPE_GENERICINST
:
697 if (mono_type_generic_inst_is_valuetype (type
))
698 return OP_LOADV_MEMBASE
;
700 return OP_LOAD_MEMBASE
;
704 /* FIXME: all the arguments must be references for now,
705 * later look inside cfg and see if the arg num is
708 g_assert (cfg
->generic_sharing_context
);
709 return OP_LOAD_MEMBASE
;
711 g_error ("unknown type 0x%02x in type_to_load_membase", type
->type
);
717 mini_type_to_ldind (MonoCompile
* cfg
, MonoType
*type
)
719 if (cfg
->generic_sharing_context
&& !type
->byref
) {
720 /* FIXME: all the arguments must be references for now,
721 * later look inside cfg and see if the arg num is
724 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
725 return CEE_LDIND_REF
;
727 return mono_type_to_ldind (type
);
731 mini_type_to_stind (MonoCompile
* cfg
, MonoType
*type
)
733 if (cfg
->generic_sharing_context
&& !type
->byref
) {
734 /* FIXME: all the arguments must be references for now,
735 * later look inside cfg and see if the arg num is
738 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
739 return CEE_STIND_REF
;
741 return mono_type_to_stind (type
);
745 mono_op_imm_to_op (int opcode
)
749 #if SIZEOF_REGISTER == 4
765 #if SIZEOF_REGISTER == 4
771 #if SIZEOF_REGISTER == 4
777 #if SIZEOF_REGISTER == 4
815 #if SIZEOF_REGISTER == 4
821 #if SIZEOF_REGISTER == 4
840 case OP_ICOMPARE_IMM
:
842 case OP_LOCALLOC_IMM
:
845 printf ("%s\n", mono_inst_name (opcode
));
846 g_assert_not_reached ();
852 * mono_decompose_op_imm:
854 * Replace the OP_.._IMM INS with its non IMM variant.
857 mono_decompose_op_imm (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
861 MONO_INST_NEW (cfg
, temp
, OP_ICONST
);
862 temp
->inst_c0
= ins
->inst_imm
;
863 temp
->dreg
= mono_alloc_ireg (cfg
);
864 mono_bblock_insert_before_ins (bb
, ins
, temp
);
865 ins
->opcode
= mono_op_imm_to_op (ins
->opcode
);
866 if (ins
->opcode
== OP_LOCALLOC
)
867 ins
->sreg1
= temp
->dreg
;
869 ins
->sreg2
= temp
->dreg
;
871 bb
->max_vreg
= MAX (bb
->max_vreg
, cfg
->next_vreg
);
875 set_vreg_to_inst (MonoCompile
*cfg
, int vreg
, MonoInst
*inst
)
877 if (vreg
>= cfg
->vreg_to_inst_len
) {
878 MonoInst
**tmp
= cfg
->vreg_to_inst
;
879 int size
= cfg
->vreg_to_inst_len
;
881 while (vreg
>= cfg
->vreg_to_inst_len
)
882 cfg
->vreg_to_inst_len
= cfg
->vreg_to_inst_len
? cfg
->vreg_to_inst_len
* 2 : 32;
883 cfg
->vreg_to_inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
*) * cfg
->vreg_to_inst_len
);
885 memcpy (cfg
->vreg_to_inst
, tmp
, size
* sizeof (MonoInst
*));
887 cfg
->vreg_to_inst
[vreg
] = inst
;
890 #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)))
891 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
896 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
904 mono_compile_create_var_for_vreg (MonoCompile
*cfg
, MonoType
*type
, int opcode
, int vreg
)
907 int num
= cfg
->num_varinfo
;
910 if ((num
+ 1) >= cfg
->varinfo_count
) {
911 int orig_count
= cfg
->varinfo_count
;
912 cfg
->varinfo_count
= cfg
->varinfo_count
? (cfg
->varinfo_count
* 2) : 64;
913 cfg
->varinfo
= (MonoInst
**)g_realloc (cfg
->varinfo
, sizeof (MonoInst
*) * cfg
->varinfo_count
);
914 cfg
->vars
= (MonoMethodVar
*)g_realloc (cfg
->vars
, sizeof (MonoMethodVar
) * cfg
->varinfo_count
);
915 memset (&cfg
->vars
[orig_count
], 0, (cfg
->varinfo_count
- orig_count
) * sizeof (MonoMethodVar
));
918 mono_jit_stats
.allocate_var
++;
920 MONO_INST_NEW (cfg
, inst
, opcode
);
922 inst
->inst_vtype
= type
;
923 inst
->klass
= mono_class_from_mono_type (type
);
924 type_to_eval_stack_type (cfg
, type
, inst
);
925 /* if set to 1 the variable is native */
926 inst
->backend
.is_pinvoke
= 0;
929 cfg
->varinfo
[num
] = inst
;
931 MONO_INIT_VARINFO (&cfg
->vars
[num
], num
);
932 MONO_VARINFO (cfg
, num
)->vreg
= vreg
;
935 set_vreg_to_inst (cfg
, vreg
, inst
);
937 #if SIZEOF_REGISTER == 4
938 #ifdef MONO_ARCH_SOFT_FLOAT
939 regpair
= mono_type_is_long (type
) || mono_type_is_float (type
);
941 regpair
= mono_type_is_long (type
);
951 * These two cannot be allocated using create_var_for_vreg since that would
952 * put it into the cfg->varinfo array, confusing many parts of the JIT.
956 * Set flags to VOLATILE so SSA skips it.
959 if (cfg
->verbose_level
>= 4) {
960 printf (" Create LVAR R%d (R%d, R%d)\n", inst
->dreg
, inst
->dreg
+ 1, inst
->dreg
+ 2);
963 #ifdef MONO_ARCH_SOFT_FLOAT
964 if (cfg
->opt
& MONO_OPT_SSA
) {
965 if (mono_type_is_float (type
))
966 inst
->flags
= MONO_INST_VOLATILE
;
970 /* Allocate a dummy MonoInst for the first vreg */
971 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
972 tree
->dreg
= inst
->dreg
+ 1;
973 if (cfg
->opt
& MONO_OPT_SSA
)
974 tree
->flags
= MONO_INST_VOLATILE
;
976 tree
->type
= STACK_I4
;
977 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
978 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
980 set_vreg_to_inst (cfg
, inst
->dreg
+ 1, tree
);
982 /* Allocate a dummy MonoInst for the second vreg */
983 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
984 tree
->dreg
= inst
->dreg
+ 2;
985 if (cfg
->opt
& MONO_OPT_SSA
)
986 tree
->flags
= MONO_INST_VOLATILE
;
988 tree
->type
= STACK_I4
;
989 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
990 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
992 set_vreg_to_inst (cfg
, inst
->dreg
+ 2, tree
);
996 if (cfg
->verbose_level
> 2)
997 g_print ("created temp %d (R%d) of type %s\n", num
, vreg
, mono_type_get_name (type
));
1002 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1006 if (mono_type_is_long (type
))
1007 dreg
= mono_alloc_dreg (cfg
, STACK_I8
);
1008 #ifdef MONO_ARCH_SOFT_FLOAT
1009 else if (mono_type_is_float (type
))
1010 dreg
= mono_alloc_dreg (cfg
, STACK_R8
);
1013 /* All the others are unified */
1014 dreg
= mono_alloc_preg (cfg
);
1016 return mono_compile_create_var_for_vreg (cfg
, type
, opcode
, dreg
);
1020 * Transform a MonoInst into a load from the variable of index var_index.
1023 mono_compile_make_var_load (MonoCompile
*cfg
, MonoInst
*dest
, gssize var_index
) {
1024 memset (dest
, 0, sizeof (MonoInst
));
1025 dest
->inst_i0
= cfg
->varinfo
[var_index
];
1026 dest
->opcode
= mini_type_to_ldind (cfg
, dest
->inst_i0
->inst_vtype
);
1027 type_to_eval_stack_type (cfg
, dest
->inst_i0
->inst_vtype
, dest
);
1028 dest
->klass
= dest
->inst_i0
->klass
;
1034 type_from_stack_type (MonoInst
*ins
) {
1035 switch (ins
->type
) {
1036 case STACK_I4
: return &mono_defaults
.int32_class
->byval_arg
;
1037 case STACK_I8
: return &mono_defaults
.int64_class
->byval_arg
;
1038 case STACK_PTR
: return &mono_defaults
.int_class
->byval_arg
;
1039 case STACK_R8
: return &mono_defaults
.double_class
->byval_arg
;
1042 * this if used to be commented without any specific reason, but
1043 * it breaks #80235 when commented
1046 return &ins
->klass
->this_arg
;
1048 return &mono_defaults
.object_class
->this_arg
;
1050 /* ins->klass may not be set for ldnull.
1051 * Also, if we have a boxed valuetype, we want an object lass,
1052 * not the valuetype class
1054 if (ins
->klass
&& !ins
->klass
->valuetype
)
1055 return &ins
->klass
->byval_arg
;
1056 return &mono_defaults
.object_class
->byval_arg
;
1057 case STACK_VTYPE
: return &ins
->klass
->byval_arg
;
1059 g_error ("stack type %d to montype not handled\n", ins
->type
);
1065 mono_type_from_stack_type (MonoInst
*ins
) {
1066 return type_from_stack_type (ins
);
1070 * mono_add_ins_to_end:
1072 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
1075 mono_add_ins_to_end (MonoBasicBlock
*bb
, MonoInst
*inst
)
1080 MONO_ADD_INS (bb
, inst
);
1084 switch (bb
->last_ins
->opcode
) {
1098 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1101 if (MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1102 /* Need to insert the ins before the compare */
1103 if (bb
->code
== bb
->last_ins
) {
1104 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1108 if (bb
->code
->next
== bb
->last_ins
) {
1109 /* Only two instructions */
1110 opcode
= bb
->code
->opcode
;
1112 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
)) {
1114 mono_bblock_insert_before_ins (bb
, bb
->code
, inst
);
1116 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1119 opcode
= bb
->last_ins
->prev
->opcode
;
1121 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
)) {
1123 mono_bblock_insert_before_ins (bb
, bb
->last_ins
->prev
, inst
);
1125 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1130 MONO_ADD_INS (bb
, inst
);
1136 mono_create_jump_table (MonoCompile
*cfg
, MonoInst
*label
, MonoBasicBlock
**bbs
, int num_blocks
)
1138 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
1139 MonoJumpInfoBBTable
*table
;
1141 table
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfoBBTable
));
1143 table
->table_size
= num_blocks
;
1145 ji
->ip
.label
= label
;
1146 ji
->type
= MONO_PATCH_INFO_SWITCH
;
1147 ji
->data
.table
= table
;
1148 ji
->next
= cfg
->patch_info
;
1149 cfg
->patch_info
= ji
;
1152 static MonoMethodSignature
*
1153 mono_get_array_new_va_signature (int arity
)
1155 static GHashTable
*sighash
= NULL
;
1156 MonoMethodSignature
*res
;
1161 sighash
= g_hash_table_new (NULL
, NULL
);
1163 else if ((res
= g_hash_table_lookup (sighash
, GINT_TO_POINTER (arity
)))) {
1168 res
= mono_metadata_signature_alloc (mono_defaults
.corlib
, arity
+ 1);
1171 #ifdef MONO_ARCH_VARARG_ICALLS
1172 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1173 res
->call_convention
= MONO_CALL_VARARG
;
1177 res
->call_convention
= MONO_CALL_C
;
1180 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
1181 for (i
= 0; i
< arity
; i
++)
1182 res
->params
[i
+ 1] = &mono_defaults
.int_class
->byval_arg
;
1184 res
->ret
= &mono_defaults
.object_class
->byval_arg
;
1186 g_hash_table_insert (sighash
, GINT_TO_POINTER (arity
), res
);
1193 mono_get_array_new_va_icall (int rank
)
1195 MonoMethodSignature
*esig
;
1196 char icall_name
[256];
1198 MonoJitICallInfo
*info
;
1200 /* Need to register the icall so it gets an icall wrapper */
1201 sprintf (icall_name
, "ves_array_new_va_%d", rank
);
1204 info
= mono_find_jit_icall_by_name (icall_name
);
1206 esig
= mono_get_array_new_va_signature (rank
);
1207 name
= g_strdup (icall_name
);
1208 info
= mono_register_jit_icall (mono_array_new_va
, name
, esig
, FALSE
);
1210 g_hash_table_insert (jit_icall_name_hash
, name
, name
);
1218 mini_class_is_system_array (MonoClass
*klass
)
1220 if (klass
->parent
== mono_defaults
.array_class
)
1227 mini_assembly_can_skip_verification (MonoDomain
*domain
, MonoMethod
*method
)
1229 MonoAssembly
*assembly
= method
->klass
->image
->assembly
;
1230 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1232 if (assembly
->in_gac
|| assembly
->image
== mono_defaults
.corlib
)
1234 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE
)
1236 return mono_assembly_has_skip_verification (assembly
);
1240 * mini_method_verify:
1242 * Verify the method using the new verfier.
1244 * Returns true if the method is invalid.
1247 mini_method_verify (MonoCompile
*cfg
, MonoMethod
*method
)
1250 gboolean is_fulltrust
;
1251 MonoLoaderError
*error
;
1253 if (method
->verification_success
)
1256 is_fulltrust
= mono_verifier_is_method_full_trust (method
);
1258 if (!mono_verifier_is_enabled_for_method (method
))
1261 res
= mono_method_verify_with_current_settings (method
, cfg
->skip_visibility
);
1263 if ((error
= mono_loader_get_last_error ())) {
1264 cfg
->exception_type
= error
->exception_type
;
1266 mono_free_verify_list (res
);
1271 for (tmp
= res
; tmp
; tmp
= tmp
->next
) {
1272 MonoVerifyInfoExtended
*info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1273 if (info
->info
.status
== MONO_VERIFY_ERROR
) {
1274 char *method_name
= mono_method_full_name (method
, TRUE
);
1275 cfg
->exception_type
= info
->exception_type
;
1276 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1277 mono_free_verify_list (res
);
1278 g_free (method_name
);
1281 if (info
->info
.status
== MONO_VERIFY_NOT_VERIFIABLE
&& (!is_fulltrust
|| info
->exception_type
== MONO_EXCEPTION_METHOD_ACCESS
|| info
->exception_type
== MONO_EXCEPTION_FIELD_ACCESS
)) {
1282 char *method_name
= mono_method_full_name (method
, TRUE
);
1283 cfg
->exception_type
= info
->exception_type
;
1284 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1285 mono_free_verify_list (res
);
1286 g_free (method_name
);
1290 mono_free_verify_list (res
);
1292 method
->verification_success
= 1;
1296 /*Returns true is something went wrong*/
1298 mono_compile_is_broken (MonoCompile
*cfg
)
1300 MonoMethod
*method
= cfg
->method
;
1301 MonoMethod
*method_definition
= method
;
1302 gboolean dont_verify
= mini_assembly_can_skip_verification (cfg
->domain
, method
);
1303 dont_verify
|= method
->klass
->image
->assembly
->corlib_internal
;
1305 while (method_definition
->is_inflated
) {
1306 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method_definition
;
1307 method_definition
= imethod
->declaring
;
1310 return !dont_verify
&& mini_method_verify (cfg
, method_definition
);
1314 create_helper_signature (void)
1316 helper_sig_domain_get
= mono_create_icall_signature ("ptr");
1317 helper_sig_class_init_trampoline
= mono_create_icall_signature ("void");
1318 helper_sig_generic_class_init_trampoline
= mono_create_icall_signature ("void");
1319 helper_sig_rgctx_lazy_fetch_trampoline
= mono_create_icall_signature ("ptr ptr");
1320 helper_sig_monitor_enter_exit_trampoline
= mono_create_icall_signature ("void");
1321 helper_sig_monitor_enter_exit_trampoline_llvm
= mono_create_icall_signature ("void object");
1324 static gconstpointer
1325 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
1328 MonoMethod
*wrapper
;
1329 gconstpointer trampoline
;
1330 MonoDomain
*domain
= mono_get_root_domain ();
1332 if (callinfo
->wrapper
) {
1333 return callinfo
->wrapper
;
1336 if (callinfo
->trampoline
)
1337 return callinfo
->trampoline
;
1340 * We use the lock on the root domain instead of the JIT lock to protect
1341 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
1343 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
1344 mono_domain_lock (domain
);
1346 if (callinfo
->trampoline
) {
1347 mono_domain_unlock (domain
);
1348 mono_loader_unlock ();
1349 return callinfo
->trampoline
;
1352 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
1353 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_for_pending_exc
);
1357 trampoline
= mono_compile_method (wrapper
);
1359 trampoline
= mono_create_ftnptr (domain
, mono_create_jit_trampoline_in_domain (domain
, wrapper
));
1360 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
1362 callinfo
->trampoline
= trampoline
;
1364 mono_domain_unlock (domain
);
1365 mono_loader_unlock ();
1367 return callinfo
->trampoline
;
1371 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
1373 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
1377 mono_dynamic_code_hash_insert (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*ji
)
1379 if (!domain_jit_info (domain
)->dynamic_code_hash
)
1380 domain_jit_info (domain
)->dynamic_code_hash
= g_hash_table_new (NULL
, NULL
);
1381 g_hash_table_insert (domain_jit_info (domain
)->dynamic_code_hash
, method
, ji
);
1384 static MonoJitDynamicMethodInfo
*
1385 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
1387 MonoJitDynamicMethodInfo
*res
;
1389 if (domain_jit_info (domain
)->dynamic_code_hash
)
1390 res
= g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1398 GList
*active
, *inactive
;
1403 compare_by_interval_start_pos_func (gconstpointer a
, gconstpointer b
)
1405 MonoMethodVar
*v1
= (MonoMethodVar
*)a
;
1406 MonoMethodVar
*v2
= (MonoMethodVar
*)b
;
1410 else if (v1
->interval
->range
&& v2
->interval
->range
)
1411 return v1
->interval
->range
->from
- v2
->interval
->range
->from
;
1412 else if (v1
->interval
->range
)
1421 #define LSCAN_DEBUG(a) do { a; } while (0)
1423 #define LSCAN_DEBUG(a)
1427 mono_allocate_stack_slots_full2 (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1429 int i
, slot
, offset
, size
;
1434 GList
*vars
= NULL
, *l
, *unhandled
;
1435 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1438 gboolean reuse_slot
;
1440 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg
->method
, TRUE
)));
1442 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1443 vtype_stack_slots
= NULL
;
1446 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1447 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1450 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1451 inst
= cfg
->varinfo
[i
];
1452 vmv
= MONO_VARINFO (cfg
, i
);
1454 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1457 vars
= g_list_prepend (vars
, vmv
);
1460 vars
= g_list_sort (g_list_copy (vars
), compare_by_interval_start_pos_func
);
1465 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1466 MonoMethodVar *current = unhandled->data;
1468 if (current->interval->range) {
1469 g_assert (current->interval->range->from >= i);
1470 i = current->interval->range->from;
1477 for (unhandled
= vars
; unhandled
; unhandled
= unhandled
->next
) {
1478 MonoMethodVar
*current
= unhandled
->data
;
1481 inst
= cfg
->varinfo
[vmv
->idx
];
1483 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1484 * pinvoke wrappers when they call functions returning structures */
1485 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1486 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1491 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1494 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1499 if (cfg
->disable_reuse_stack_slots
)
1502 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1504 case MONO_TYPE_GENERICINST
:
1505 if (!mono_type_generic_inst_is_valuetype (t
)) {
1506 slot_info
= &scalar_stack_slots
[t
->type
];
1510 case MONO_TYPE_VALUETYPE
:
1511 if (!vtype_stack_slots
)
1512 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1513 for (i
= 0; i
< nvtypes
; ++i
)
1514 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1517 slot_info
= &vtype_stack_slots
[i
];
1519 g_assert (nvtypes
< 256);
1520 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1521 slot_info
= &vtype_stack_slots
[nvtypes
];
1524 if (cfg
->disable_reuse_ref_stack_slots
)
1531 #if SIZEOF_REGISTER == 4
1537 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1543 case MONO_TYPE_CLASS
:
1544 case MONO_TYPE_OBJECT
:
1545 case MONO_TYPE_ARRAY
:
1546 case MONO_TYPE_SZARRAY
:
1547 case MONO_TYPE_STRING
:
1548 /* Share non-float stack slots of the same size */
1549 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1550 if (cfg
->disable_reuse_ref_stack_slots
)
1555 slot_info
= &scalar_stack_slots
[t
->type
];
1559 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1563 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1565 if (!current
->interval
->range
) {
1566 if (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
1570 inst
->flags
|= MONO_INST_IS_DEAD
;
1575 pos
= current
->interval
->range
->from
;
1577 LSCAN_DEBUG (printf ("process R%d ", inst
->dreg
));
1578 if (current
->interval
->range
)
1579 LSCAN_DEBUG (mono_linterval_print (current
->interval
));
1580 LSCAN_DEBUG (printf ("\n"));
1582 /* Check for intervals in active which expired or inactive */
1584 /* FIXME: Optimize this */
1587 for (l
= slot_info
->active
; l
!= NULL
; l
= l
->next
) {
1588 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1590 if (v
->interval
->last_range
->to
< pos
) {
1591 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1592 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[v
->idx
]));
1593 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1597 else if (!mono_linterval_covers (v
->interval
, pos
)) {
1598 slot_info
->inactive
= g_list_append (slot_info
->inactive
, v
);
1599 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1600 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg
->varinfo
[v
->idx
]->dreg
));
1607 /* Check for intervals in inactive which expired or active */
1609 /* FIXME: Optimize this */
1612 for (l
= slot_info
->inactive
; l
!= NULL
; l
= l
->next
) {
1613 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1615 if (v
->interval
->last_range
->to
< pos
) {
1616 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1617 // FIXME: Enabling this seems to cause impossible to debug crashes
1618 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1619 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1623 else if (mono_linterval_covers (v
->interval
, pos
)) {
1624 slot_info
->active
= g_list_append (slot_info
->active
, v
);
1625 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1626 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg
->varinfo
[v
->idx
]->dreg
));
1634 * This also handles the case when the variable is used in an
1635 * exception region, as liveness info is not computed there.
1638 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1641 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1642 if (slot_info
->slots
) {
1643 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1645 slot_info
->slots
= slot_info
->slots
->next
;
1648 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1650 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1656 static int count
= 0;
1659 if (count
== atoi (getenv ("COUNT3")))
1660 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1661 if (count
> atoi (getenv ("COUNT3")))
1664 mono_print_ins (inst
);
1669 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst
->dreg
, mono_type_full_name (t
), slot
));
1674 if (slot
== 0xffffff) {
1676 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1677 * efficient copying (and to work around the fact that OP_MEMCPY
1678 * and OP_MEMSET ignores alignment).
1680 if (MONO_TYPE_ISSTRUCT (t
)) {
1681 align
= MAX (align
, sizeof (gpointer
));
1682 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1687 offset
+= align
- 1;
1688 offset
&= ~(align
- 1);
1692 offset
+= align
- 1;
1693 offset
&= ~(align
- 1);
1698 if (*stack_align
== 0)
1699 *stack_align
= align
;
1702 offsets
[vmv
->idx
] = slot
;
1705 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1706 if (scalar_stack_slots
[i
].active
)
1707 g_list_free (scalar_stack_slots
[i
].active
);
1709 for (i
= 0; i
< nvtypes
; ++i
) {
1710 if (vtype_stack_slots
[i
].active
)
1711 g_list_free (vtype_stack_slots
[i
].active
);
1714 mono_jit_stats
.locals_stack_size
+= offset
;
1716 *stack_size
= offset
;
1721 * mono_allocate_stack_slots_full:
1723 * Allocate stack slots for all non register allocated variables using a
1724 * linear scan algorithm.
1725 * Returns: an array of stack offsets.
1726 * STACK_SIZE is set to the amount of stack space needed.
1727 * STACK_ALIGN is set to the alignment needed by the locals area.
1730 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1732 int i
, slot
, offset
, size
;
1737 GList
*vars
= NULL
, *l
;
1738 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1741 gboolean reuse_slot
;
1743 if ((cfg
->num_varinfo
> 0) && MONO_VARINFO (cfg
, 0)->interval
)
1744 return mono_allocate_stack_slots_full2 (cfg
, backward
, stack_size
, stack_align
);
1746 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1747 vtype_stack_slots
= NULL
;
1750 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1751 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1754 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1755 inst
= cfg
->varinfo
[i
];
1756 vmv
= MONO_VARINFO (cfg
, i
);
1758 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1761 vars
= g_list_prepend (vars
, vmv
);
1764 vars
= mono_varlist_sort (cfg
, vars
, 0);
1766 *stack_align
= sizeof (gpointer
);
1767 for (l
= vars
; l
; l
= l
->next
) {
1769 inst
= cfg
->varinfo
[vmv
->idx
];
1771 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1772 * pinvoke wrappers when they call functions returning structures */
1773 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1774 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1778 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1781 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1786 if (cfg
->disable_reuse_stack_slots
)
1789 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1791 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1794 case MONO_TYPE_GENERICINST
:
1795 if (!mono_type_generic_inst_is_valuetype (t
)) {
1796 slot_info
= &scalar_stack_slots
[t
->type
];
1800 case MONO_TYPE_VALUETYPE
:
1801 if (!vtype_stack_slots
)
1802 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1803 for (i
= 0; i
< nvtypes
; ++i
)
1804 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1807 slot_info
= &vtype_stack_slots
[i
];
1809 g_assert (nvtypes
< 256);
1810 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1811 slot_info
= &vtype_stack_slots
[nvtypes
];
1814 if (cfg
->disable_reuse_ref_stack_slots
)
1821 #if SIZEOF_REGISTER == 4
1827 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1833 case MONO_TYPE_CLASS
:
1834 case MONO_TYPE_OBJECT
:
1835 case MONO_TYPE_ARRAY
:
1836 case MONO_TYPE_SZARRAY
:
1837 case MONO_TYPE_STRING
:
1838 /* Share non-float stack slots of the same size */
1839 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1840 if (cfg
->disable_reuse_ref_stack_slots
)
1845 slot_info
= &scalar_stack_slots
[t
->type
];
1850 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1851 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1853 /* expire old intervals in active */
1854 while (slot_info
->active
) {
1855 MonoMethodVar
*amv
= (MonoMethodVar
*)slot_info
->active
->data
;
1857 if (amv
->range
.last_use
.abs_pos
> vmv
->range
.first_use
.abs_pos
)
1860 //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);
1862 slot_info
->active
= g_list_delete_link (slot_info
->active
, slot_info
->active
);
1863 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[amv
->idx
]));
1867 * This also handles the case when the variable is used in an
1868 * exception region, as liveness info is not computed there.
1871 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1874 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1875 if (slot_info
->slots
) {
1876 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1878 slot_info
->slots
= slot_info
->slots
->next
;
1881 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1886 static int count
= 0;
1890 if (count == atoi (getenv ("COUNT")))
1891 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1892 if (count > atoi (getenv ("COUNT")))
1895 mono_print_ins (inst);
1903 if (slot
== 0xffffff) {
1905 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1906 * efficient copying (and to work around the fact that OP_MEMCPY
1907 * and OP_MEMSET ignores alignment).
1909 if (MONO_TYPE_ISSTRUCT (t
)) {
1910 align
= MAX (align
, sizeof (gpointer
));
1911 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1913 * Align the size too so the code generated for passing vtypes in
1914 * registers doesn't overwrite random locals.
1916 size
= (size
+ (align
- 1)) & ~(align
-1);
1921 offset
+= align
- 1;
1922 offset
&= ~(align
- 1);
1926 offset
+= align
- 1;
1927 offset
&= ~(align
- 1);
1932 *stack_align
= MAX (*stack_align
, align
);
1935 offsets
[vmv
->idx
] = slot
;
1938 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1939 if (scalar_stack_slots
[i
].active
)
1940 g_list_free (scalar_stack_slots
[i
].active
);
1942 for (i
= 0; i
< nvtypes
; ++i
) {
1943 if (vtype_stack_slots
[i
].active
)
1944 g_list_free (vtype_stack_slots
[i
].active
);
1947 mono_jit_stats
.locals_stack_size
+= offset
;
1949 *stack_size
= offset
;
1956 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1958 g_assert_not_reached ();
1962 #endif /* DISABLE_JIT */
1965 mono_allocate_stack_slots (MonoCompile
*m
, guint32
*stack_size
, guint32
*stack_align
)
1967 return mono_allocate_stack_slots_full (m
, TRUE
, stack_size
, stack_align
);
1970 #define EMUL_HIT_SHIFT 3
1971 #define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
1972 /* small hit bitmap cache */
1973 static mono_byte emul_opcode_hit_cache
[(OP_LAST
>>EMUL_HIT_SHIFT
) + 1] = {0};
1974 static short emul_opcode_num
= 0;
1975 static short emul_opcode_alloced
= 0;
1976 static short *emul_opcode_opcodes
= NULL
;
1977 static MonoJitICallInfo
**emul_opcode_map
= NULL
;
1980 mono_find_jit_opcode_emulation (int opcode
)
1982 g_assert (opcode
>= 0 && opcode
<= OP_LAST
);
1983 if (emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] & (1 << (opcode
& EMUL_HIT_MASK
))) {
1985 for (i
= 0; i
< emul_opcode_num
; ++i
) {
1986 if (emul_opcode_opcodes
[i
] == opcode
)
1987 return emul_opcode_map
[i
];
1994 mono_register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, gboolean no_throw
)
1996 MonoJitICallInfo
*info
;
1997 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
1999 g_assert (!sig
->hasthis
);
2000 g_assert (sig
->param_count
< 3);
2002 info
= mono_register_jit_icall (func
, name
, sig
, no_throw
);
2004 if (emul_opcode_num
>= emul_opcode_alloced
) {
2005 int incr
= emul_opcode_alloced
? emul_opcode_alloced
/2: 16;
2006 emul_opcode_alloced
+= incr
;
2007 emul_opcode_map
= g_realloc (emul_opcode_map
, sizeof (emul_opcode_map
[0]) * emul_opcode_alloced
);
2008 emul_opcode_opcodes
= g_realloc (emul_opcode_opcodes
, sizeof (emul_opcode_opcodes
[0]) * emul_opcode_alloced
);
2010 emul_opcode_map
[emul_opcode_num
] = info
;
2011 emul_opcode_opcodes
[emul_opcode_num
] = opcode
;
2013 emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] |= (1 << (opcode
& EMUL_HIT_MASK
));
2017 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
2019 MonoMethodSignature
*sig
;
2022 sig
= mono_create_icall_signature (sigstr
);
2026 mono_register_jit_icall (func
, name
, sig
, save
);
2030 print_dfn (MonoCompile
*cfg
) {
2036 g_print ("IR code for method %s\n", mono_method_full_name (cfg
->method
, TRUE
));
2038 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
2039 bb
= cfg
->bblocks
[i
];
2040 /*if (bb->cil_code) {
2041 char* code1, *code2;
2042 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
2043 if (bb->last_ins->cil_code)
2044 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
2046 code2 = g_strdup ("");
2048 code1 [strlen (code1) - 1] = 0;
2049 code = g_strdup_printf ("%s -> %s", code1, code2);
2053 code
= g_strdup ("\n");
2054 g_print ("\nBB%d (%d) (len: %d): %s", bb
->block_num
, i
, bb
->cil_length
, code
);
2055 MONO_BB_FOR_EACH_INS (bb
, c
) {
2056 mono_print_ins_index (-1, c
);
2059 g_print ("\tprev:");
2060 for (j
= 0; j
< bb
->in_count
; ++j
) {
2061 g_print (" BB%d", bb
->in_bb
[j
]->block_num
);
2063 g_print ("\t\tsucc:");
2064 for (j
= 0; j
< bb
->out_count
; ++j
) {
2065 g_print (" BB%d", bb
->out_bb
[j
]->block_num
);
2067 g_print ("\n\tidom: BB%d\n", bb
->idom
? bb
->idom
->block_num
: -1);
2070 g_assert (mono_bitset_test_fast (bb
->dominators
, bb
->idom
->dfn
));
2073 mono_blockset_print (cfg
, bb
->dominators
, "\tdominators", bb
->idom
? bb
->idom
->dfn
: -1);
2075 mono_blockset_print (cfg
, bb
->dfrontier
, "\tdfrontier", -1);
2083 mono_bblock_add_inst (MonoBasicBlock
*bb
, MonoInst
*inst
)
2085 MONO_ADD_INS (bb
, inst
);
2089 mono_bblock_insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2093 bb
->code
= ins_to_insert
;
2095 /* Link with next */
2096 ins_to_insert
->next
= ins
;
2098 ins
->prev
= ins_to_insert
;
2100 if (bb
->last_ins
== NULL
)
2101 bb
->last_ins
= ins_to_insert
;
2103 /* Link with next */
2104 ins_to_insert
->next
= ins
->next
;
2106 ins
->next
->prev
= ins_to_insert
;
2108 /* Link with previous */
2109 ins
->next
= ins_to_insert
;
2110 ins_to_insert
->prev
= ins
;
2112 if (bb
->last_ins
== ins
)
2113 bb
->last_ins
= ins_to_insert
;
2118 mono_bblock_insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2122 bb
->code
= ins_to_insert
;
2123 ins_to_insert
->next
= ins
;
2124 if (bb
->last_ins
== NULL
)
2125 bb
->last_ins
= ins_to_insert
;
2127 /* Link with previous */
2129 ins
->prev
->next
= ins_to_insert
;
2130 ins_to_insert
->prev
= ins
->prev
;
2132 /* Link with next */
2133 ins
->prev
= ins_to_insert
;
2134 ins_to_insert
->next
= ins
;
2136 if (bb
->code
== ins
)
2137 bb
->code
= ins_to_insert
;
2142 * mono_verify_bblock:
2144 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2147 mono_verify_bblock (MonoBasicBlock
*bb
)
2149 MonoInst
*ins
, *prev
;
2152 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2153 g_assert (ins
->prev
== prev
);
2157 g_assert (!bb
->last_ins
->next
);
2163 * Perform consistency checks on the JIT data structures and the IR
2166 mono_verify_cfg (MonoCompile
*cfg
)
2170 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2171 mono_verify_bblock (bb
);
2175 mono_destroy_compile (MonoCompile
*cfg
)
2178 mono_metadata_free_mh (cfg
->header
);
2179 //mono_mempool_stats (cfg->mempool);
2180 mono_free_loop_info (cfg
);
2182 mono_regstate_free (cfg
->rs
);
2184 g_hash_table_destroy (cfg
->spvars
);
2186 g_hash_table_destroy (cfg
->exvars
);
2187 mono_mempool_destroy (cfg
->mempool
);
2188 g_list_free (cfg
->ldstr_list
);
2189 g_hash_table_destroy (cfg
->token_info_hash
);
2190 if (cfg
->abs_patches
)
2191 g_hash_table_destroy (cfg
->abs_patches
);
2193 g_free (cfg
->varinfo
);
2195 g_free (cfg
->exception_message
);
2199 #ifdef HAVE_KW_THREAD
2200 static __thread gpointer mono_lmf_addr MONO_TLS_FAST
;
2201 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
2203 * When this is defined, the current lmf is stored in this tls variable instead of in
2206 static __thread gpointer mono_lmf MONO_TLS_FAST
;
2211 mono_get_jit_tls_key (void)
2213 return mono_jit_tls_id
;
2217 mono_get_jit_tls_offset (void)
2219 #ifdef HAVE_KW_THREAD
2221 MONO_THREAD_VAR_OFFSET (mono_jit_tls
, offset
);
2229 mono_get_lmf_tls_offset (void)
2231 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2233 MONO_THREAD_VAR_OFFSET(mono_lmf
,offset
);
2241 mono_get_lmf_addr_tls_offset (void)
2244 MONO_THREAD_VAR_OFFSET(mono_lmf_addr
,offset
);
2251 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2254 MonoJitTlsData
*jit_tls
;
2256 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2257 return jit_tls
->lmf
;
2259 g_assert_not_reached ();
2265 mono_get_lmf_addr (void)
2267 #ifdef HAVE_KW_THREAD
2268 return mono_lmf_addr
;
2270 MonoJitTlsData
*jit_tls
;
2272 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2273 return &jit_tls
->lmf
;
2276 * When resolving the call to mono_jit_thread_attach full-aot will look
2277 * in the plt, which causes a call into the generic trampoline, which in turn
2278 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
2279 * call mono_jit_thread_attach from the native-to-managed wrapper, without
2280 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
2283 mono_jit_thread_attach (NULL
);
2285 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2286 return &jit_tls
->lmf
;
2288 g_assert_not_reached ();
2294 mono_set_lmf (MonoLMF
*lmf
)
2296 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2300 (*mono_get_lmf_addr ()) = lmf
;
2303 /* Called by native->managed wrappers */
2305 mono_jit_thread_attach (MonoDomain
*domain
)
2309 * Happens when called from AOTed code which is only used in the root
2312 domain
= mono_get_root_domain ();
2314 #ifdef HAVE_KW_THREAD
2315 if (!mono_lmf_addr
) {
2316 mono_thread_attach (domain
);
2319 if (!TlsGetValue (mono_jit_tls_id
))
2320 mono_thread_attach (domain
);
2322 if (mono_domain_get () != domain
)
2323 mono_domain_set (domain
, TRUE
);
2327 * mono_thread_abort:
2328 * @obj: exception object
2330 * abort the thread, print exception information and stack trace
2333 mono_thread_abort (MonoObject
*obj
)
2335 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
2337 /* handle_remove should be eventually called for this thread, too
2340 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
2341 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
2342 mono_thread_exit ();
2344 exit (mono_environment_exitcode_get ());
2349 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
2351 MonoJitTlsData
*jit_tls
;
2354 jit_tls
= TlsGetValue (mono_jit_tls_id
);
2358 jit_tls
= g_new0 (MonoJitTlsData
, 1);
2360 TlsSetValue (mono_jit_tls_id
, jit_tls
);
2362 #ifdef HAVE_KW_THREAD
2363 mono_jit_tls
= jit_tls
;
2366 jit_tls
->abort_func
= abort_func
;
2367 jit_tls
->end_of_stack
= stack_start
;
2369 lmf
= g_new0 (MonoLMF
, 1);
2370 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
2371 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
2376 jit_tls
->first_lmf
= lmf
;
2378 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2379 /* jit_tls->lmf is unused */
2381 mono_lmf_addr
= &mono_lmf
;
2383 #if defined(HAVE_KW_THREAD)
2384 mono_lmf_addr
= &jit_tls
->lmf
;
2390 mono_arch_setup_jit_tls_data (jit_tls
);
2391 mono_setup_altstack (jit_tls
);
2397 mono_thread_start_cb (intptr_t tid
, gpointer stack_start
, gpointer func
)
2399 MonoInternalThread
*thread
;
2400 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
2401 thread
= mono_thread_internal_current ();
2402 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, jit_tls
, func
);
2404 thread
->jit_data
= jit_tls
;
2407 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
2410 mono_thread_abort_dummy (MonoObject
*obj
)
2412 if (mono_thread_attach_aborted_cb
)
2413 mono_thread_attach_aborted_cb (obj
);
2415 mono_thread_abort (obj
);
2419 mono_thread_attach_cb (intptr_t tid
, gpointer stack_start
)
2421 MonoInternalThread
*thread
;
2422 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
2423 thread
= mono_thread_internal_current ();
2424 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, (MonoJitTlsData
*) jit_tls
, NULL
);
2426 thread
->jit_data
= jit_tls
;
2427 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
2428 mono_runtime_setup_stat_profiler ();
2432 mini_thread_cleanup (MonoThread
*thread
)
2434 MonoInternalThread
*internal
= thread
->internal_thread
;
2435 MonoJitTlsData
*jit_tls
= internal
->jit_data
;
2438 mono_debugger_thread_cleanup (jit_tls
);
2439 mono_arch_free_jit_tls_data (jit_tls
);
2441 mono_free_altstack (jit_tls
);
2442 g_free (jit_tls
->first_lmf
);
2444 internal
->jit_data
= NULL
;
2446 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
2447 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
2448 * not a trivial thing.
2450 * The current offender is mono_thread_manage which cleanup threads from the outside.
2452 if (internal
== mono_thread_internal_current ()) {
2453 TlsSetValue (mono_jit_tls_id
, NULL
);
2455 #ifdef HAVE_KW_THREAD
2456 mono_jit_tls
= NULL
;
2457 mono_lmf_addr
= NULL
;
2458 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2467 mono_create_tls_get (MonoCompile
*cfg
, int offset
)
2469 #ifdef MONO_ARCH_HAVE_TLS_GET
2470 if (MONO_ARCH_HAVE_TLS_GET
) {
2476 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2477 ins
->dreg
= mono_alloc_preg (cfg
);
2478 ins
->inst_offset
= offset
;
2486 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2488 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2492 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2494 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2498 mono_get_thread_intrinsic (MonoCompile
* cfg
)
2500 return mono_create_tls_get (cfg
, mono_thread_get_tls_offset ());
2504 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2506 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2510 ji
->data
.target
= target
;
2511 ji
->next
= cfg
->patch_info
;
2513 cfg
->patch_info
= ji
;
2517 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2519 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2523 ji
->data
.target
= target
;
2530 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2532 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2535 if ((*ji
)->ip
.i
== ip
)
2538 ji
= &((*ji
)->next
);
2543 * mono_patch_info_dup_mp:
2545 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2548 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2550 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2551 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2553 switch (patch_info
->type
) {
2554 case MONO_PATCH_INFO_RVA
:
2555 case MONO_PATCH_INFO_LDSTR
:
2556 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2557 case MONO_PATCH_INFO_LDTOKEN
:
2558 case MONO_PATCH_INFO_DECLSEC
:
2559 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2560 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2562 case MONO_PATCH_INFO_SWITCH
:
2563 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2564 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2565 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2566 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2568 case MONO_PATCH_INFO_RGCTX_FETCH
:
2569 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2570 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2571 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2581 mono_patch_info_hash (gconstpointer data
)
2583 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2586 case MONO_PATCH_INFO_RVA
:
2587 case MONO_PATCH_INFO_LDSTR
:
2588 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2589 case MONO_PATCH_INFO_LDTOKEN
:
2590 case MONO_PATCH_INFO_DECLSEC
:
2591 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2592 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2593 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2594 case MONO_PATCH_INFO_VTABLE
:
2595 case MONO_PATCH_INFO_CLASS
:
2596 case MONO_PATCH_INFO_IID
:
2597 case MONO_PATCH_INFO_ADJUSTED_IID
:
2598 case MONO_PATCH_INFO_CLASS_INIT
:
2599 case MONO_PATCH_INFO_METHODCONST
:
2600 case MONO_PATCH_INFO_METHOD
:
2601 case MONO_PATCH_INFO_METHOD_JUMP
:
2602 case MONO_PATCH_INFO_IMAGE
:
2603 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2604 case MONO_PATCH_INFO_FIELD
:
2605 case MONO_PATCH_INFO_SFLDA
:
2606 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2607 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2609 return (ji
->type
<< 8);
2614 * mono_patch_info_equal:
2616 * This might fail to recognize equivalent patches, i.e. floats, so its only
2617 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2621 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2623 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2624 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2626 if (ji1
->type
!= ji2
->type
)
2629 switch (ji1
->type
) {
2630 case MONO_PATCH_INFO_RVA
:
2631 case MONO_PATCH_INFO_LDSTR
:
2632 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2633 case MONO_PATCH_INFO_LDTOKEN
:
2634 case MONO_PATCH_INFO_DECLSEC
:
2635 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2636 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2637 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2638 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2639 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2642 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2643 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2645 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2646 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
2647 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
2649 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
);
2652 if (ji1
->data
.target
!= ji2
->data
.target
)
2661 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
2663 unsigned char *ip
= patch_info
->ip
.i
+ code
;
2664 gconstpointer target
= NULL
;
2666 switch (patch_info
->type
) {
2667 case MONO_PATCH_INFO_BB
:
2669 * FIXME: This could be hit for methods without a prolog. Should use -1
2670 * but too much code depends on a 0 initial value.
2672 //g_assert (patch_info->data.bb->native_offset);
2673 target
= patch_info
->data
.bb
->native_offset
+ code
;
2675 case MONO_PATCH_INFO_ABS
:
2676 target
= patch_info
->data
.target
;
2678 case MONO_PATCH_INFO_LABEL
:
2679 target
= patch_info
->data
.inst
->inst_c0
+ code
;
2681 case MONO_PATCH_INFO_IP
:
2684 case MONO_PATCH_INFO_METHOD_REL
:
2685 target
= code
+ patch_info
->data
.offset
;
2687 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
2688 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2690 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
2691 g_assert_not_reached ();
2693 target
= mono_icall_get_wrapper (mi
);
2696 case MONO_PATCH_INFO_METHOD_JUMP
:
2697 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
2699 case MONO_PATCH_INFO_METHOD
:
2700 if (patch_info
->data
.method
== method
) {
2703 /* get the trampoline to the method from the domain */
2704 target
= mono_create_jit_trampoline (patch_info
->data
.method
);
2707 case MONO_PATCH_INFO_SWITCH
: {
2708 gpointer
*jump_table
;
2711 if (method
&& method
->dynamic
) {
2712 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2714 if (mono_aot_only
) {
2715 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2717 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2721 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++)
2722 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
2723 target
= jump_table
;
2726 case MONO_PATCH_INFO_METHODCONST
:
2727 case MONO_PATCH_INFO_CLASS
:
2728 case MONO_PATCH_INFO_IMAGE
:
2729 case MONO_PATCH_INFO_FIELD
:
2730 target
= patch_info
->data
.target
;
2732 case MONO_PATCH_INFO_IID
:
2733 mono_class_init (patch_info
->data
.klass
);
2734 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
2736 case MONO_PATCH_INFO_ADJUSTED_IID
:
2737 mono_class_init (patch_info
->data
.klass
);
2738 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
2740 case MONO_PATCH_INFO_VTABLE
:
2741 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2744 case MONO_PATCH_INFO_CLASS_INIT
: {
2745 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2748 target
= mono_create_class_init_trampoline (vtable
);
2751 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
2752 target
= mono_create_delegate_trampoline (patch_info
->data
.klass
);
2754 case MONO_PATCH_INFO_SFLDA
: {
2755 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
2757 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
2758 gpointer addr
= NULL
;
2760 mono_domain_lock (domain
);
2761 if (domain
->special_static_fields
)
2762 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
2763 mono_domain_unlock (domain
);
2769 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
2770 /* Done by the generated code */
2774 mono_runtime_class_init (vtable
);
2776 target
= (char*)vtable
->data
+ patch_info
->data
.field
->offset
;
2779 case MONO_PATCH_INFO_RVA
: {
2780 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
2783 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
2784 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
2787 case MONO_PATCH_INFO_R4
:
2788 case MONO_PATCH_INFO_R8
:
2789 target
= patch_info
->data
.target
;
2791 case MONO_PATCH_INFO_EXC_NAME
:
2792 target
= patch_info
->data
.name
;
2794 case MONO_PATCH_INFO_LDSTR
:
2796 mono_ldstr (domain
, patch_info
->data
.token
->image
,
2797 mono_metadata_token_index (patch_info
->data
.token
->token
));
2799 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
2801 MonoClass
*handle_class
;
2803 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2804 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
2805 mono_class_init (handle_class
);
2806 mono_class_init (mono_class_from_mono_type (handle
));
2809 mono_type_get_object (domain
, handle
);
2812 case MONO_PATCH_INFO_LDTOKEN
: {
2814 MonoClass
*handle_class
;
2816 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2817 patch_info
->data
.token
->token
, &handle_class
, NULL
);
2818 mono_class_init (handle_class
);
2823 case MONO_PATCH_INFO_DECLSEC
:
2824 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
2826 case MONO_PATCH_INFO_ICALL_ADDR
:
2827 /* run_cctors == 0 -> AOT */
2828 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2830 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, NULL
, NULL
);
2832 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
));
2837 target
= mono_lookup_internal_call (patch_info
->data
.method
);
2839 if (!target
&& run_cctors
)
2840 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2843 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
2844 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2846 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
2847 g_assert_not_reached ();
2852 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
2853 target
= mono_thread_interruption_request_flag ();
2855 case MONO_PATCH_INFO_METHOD_RGCTX
: {
2856 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
2859 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
2862 case MONO_PATCH_INFO_BB_OVF
:
2863 case MONO_PATCH_INFO_EXC_OVF
:
2864 case MONO_PATCH_INFO_GOT_OFFSET
:
2865 case MONO_PATCH_INFO_NONE
:
2867 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2868 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
2871 switch (entry
->data
->type
) {
2872 case MONO_PATCH_INFO_CLASS
:
2873 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
));
2875 case MONO_PATCH_INFO_METHOD
:
2876 case MONO_PATCH_INFO_METHODCONST
:
2877 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
));
2879 case MONO_PATCH_INFO_FIELD
:
2880 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
));
2883 g_assert_not_reached ();
2887 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
2890 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
2891 target
= mono_create_generic_class_init_trampoline ();
2893 case MONO_PATCH_INFO_MONITOR_ENTER
:
2894 target
= mono_create_monitor_enter_trampoline ();
2896 case MONO_PATCH_INFO_MONITOR_EXIT
:
2897 target
= mono_create_monitor_exit_trampoline ();
2899 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2900 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2902 /* AOT, not needed */
2905 target
= mono_arch_get_seq_point_info (domain
, code
);
2908 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
2909 #ifdef MONO_ARCH_LLVM_SUPPORTED
2910 g_assert (mono_use_llvm
);
2911 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
2913 g_assert_not_reached ();
2917 g_assert_not_reached ();
2920 return (gpointer
)target
;
2924 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
2926 ins
->inst_offset
= native_offset
;
2927 g_ptr_array_add (cfg
->seq_points
, ins
);
2928 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
2929 bb
->last_seq_point
= ins
;
2935 mono_compile_create_vars (MonoCompile
*cfg
)
2937 MonoMethodSignature
*sig
;
2938 MonoMethodHeader
*header
;
2941 header
= cfg
->header
;
2943 sig
= mono_method_signature (cfg
->method
);
2945 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
2946 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
2947 /* Inhibit optimizations */
2948 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
2950 if (cfg
->verbose_level
> 2)
2951 g_print ("creating vars\n");
2953 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
2956 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
2958 for (i
= 0; i
< sig
->param_count
; ++i
) {
2959 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
2962 if (cfg
->verbose_level
> 2) {
2964 printf ("\treturn : ");
2965 mono_print_ins (cfg
->ret
);
2969 printf ("\tthis: ");
2970 mono_print_ins (cfg
->args
[0]);
2973 for (i
= 0; i
< sig
->param_count
; ++i
) {
2974 printf ("\targ [%d]: ", i
);
2975 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
2979 cfg
->locals_start
= cfg
->num_varinfo
;
2980 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
2982 if (cfg
->verbose_level
> 2)
2983 g_print ("creating locals\n");
2985 for (i
= 0; i
< header
->num_locals
; ++i
)
2986 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
2988 if (cfg
->verbose_level
> 2)
2989 g_print ("locals done\n");
2991 mono_arch_create_vars (cfg
);
2994 #endif /* #ifndef DISABLE_JIT */
2997 mono_print_code (MonoCompile
*cfg
, const char* msg
)
3001 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3002 mono_print_bb (bb
, msg
);
3008 mono_postprocess_patches (MonoCompile
*cfg
)
3010 MonoJumpInfo
*patch_info
;
3013 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3014 switch (patch_info
->type
) {
3015 case MONO_PATCH_INFO_ABS
: {
3016 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
3019 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
3023 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
3024 // FIXME: CLEAN UP THIS MESS.
3025 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
3026 strstr (cfg
->method
->name
, info
->name
)) {
3028 * This is an icall wrapper, and this is a call to the
3031 if (cfg
->compile_aot
) {
3032 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
3033 patch_info
->data
.name
= info
->name
;
3036 /* for these array methods we currently register the same function pointer
3037 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
3038 * will return the incorrect one depending on the order they are registered.
3039 * See tests/test-arr.cs
3041 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
3042 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3043 patch_info
->data
.name
= info
->name
;
3048 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
3049 if (cfg
->abs_patches
) {
3050 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
3052 patch_info
->type
= abs_ji
->type
;
3053 patch_info
->data
.target
= abs_ji
->data
.target
;
3060 case MONO_PATCH_INFO_SWITCH
: {
3062 if (cfg
->method
->dynamic
) {
3063 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3065 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3068 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3069 /* Might be NULL if the switch is eliminated */
3070 if (patch_info
->data
.table
->table
[i
]) {
3071 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
3072 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
3077 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
3080 case MONO_PATCH_INFO_METHOD_JUMP
: {
3082 MonoDomain
*domain
= cfg
->domain
;
3083 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
3085 mono_domain_lock (domain
);
3086 if (!domain_jit_info (domain
)->jump_target_hash
)
3087 domain_jit_info (domain
)->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3088 list
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3089 list
= g_slist_prepend (list
, ip
);
3090 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, list
);
3091 mono_domain_unlock (domain
);
3102 collect_pred_seq_points (MonoBasicBlock
*bb
, MonoInst
*ins
, GSList
**next
, int depth
)
3105 MonoBasicBlock
*in_bb
;
3107 for (i
= 0; i
< bb
->in_count
; ++i
) {
3108 in_bb
= bb
->in_bb
[i
];
3110 if (in_bb
->last_seq_point
) {
3111 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
));
3113 /* Have to look at its predecessors */
3115 collect_pred_seq_points (in_bb
, ins
, next
, depth
+ 1);
3121 mono_save_seq_point_info (MonoCompile
*cfg
)
3124 GSList
*bb_seq_points
, *l
;
3126 MonoDomain
*domain
= cfg
->domain
;
3128 MonoSeqPointInfo
*info
;
3131 if (!cfg
->seq_points
)
3134 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3135 info
->len
= cfg
->seq_points
->len
;
3136 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3137 SeqPoint
*sp
= &info
->seq_points
[i
];
3138 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3140 sp
->il_offset
= ins
->inst_imm
;
3141 sp
->native_offset
= ins
->inst_offset
;
3144 ins
->backend
.size
= i
;
3148 * For each sequence point, compute the list of sequence points immediately
3149 * following it, this is needed to implement 'step over' in the debugger agent.
3151 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3152 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3153 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3155 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3156 MonoInst
*ins
= l
->data
;
3158 if (!(ins
->flags
& MONO_INST_SINGLE_STEP_LOC
))
3162 /* Link with the previous seq point in the same bb */
3163 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3165 /* Link with the last bb in the previous bblocks */
3166 collect_pred_seq_points (bb
, ins
, next
, 0);
3173 if (cfg
->verbose_level
> 2) {
3174 printf ("\nSEQ POINT MAP: \n");
3177 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3178 SeqPoint
*sp
= &info
->seq_points
[i
];
3182 sp
->next_len
= g_slist_length (next
[i
]);
3183 sp
->next
= g_new (int, sp
->next_len
);
3185 if (cfg
->verbose_level
> 2 && next
[i
]) {
3186 printf ("\t0x%x ->", sp
->il_offset
);
3187 for (l
= next
[i
]; l
; l
= l
->next
) {
3188 next_index
= GPOINTER_TO_UINT (l
->data
);
3189 printf (" 0x%x", info
->seq_points
[next_index
].il_offset
);
3193 for (l
= next
[i
]; l
; l
= l
->next
) {
3194 next_index
= GPOINTER_TO_UINT (l
->data
);
3195 sp
->next
[j
++] = next_index
;
3197 g_slist_free (next
[i
]);
3201 cfg
->seq_point_info
= info
;
3203 // FIXME: dynamic methods
3204 mono_domain_lock (domain
);
3205 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3206 mono_domain_unlock (domain
);
3208 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3209 cfg
->seq_points
= NULL
;
3213 mono_codegen (MonoCompile
*cfg
)
3216 int max_epilog_size
;
3219 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3220 cfg
->spill_count
= 0;
3221 /* we reuse dfn here */
3222 /* bb->dfn = bb_count++; */
3224 mono_arch_lowering_pass (cfg
, bb
);
3226 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3227 mono_arch_peephole_pass_1 (cfg
, bb
);
3230 mono_local_regalloc (cfg
, bb
);
3232 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3233 mono_arch_peephole_pass_2 (cfg
, bb
);
3236 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3237 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3239 code
= mono_arch_emit_prolog (cfg
);
3241 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3242 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3244 cfg
->code_len
= code
- cfg
->native_code
;
3245 cfg
->prolog_end
= cfg
->code_len
;
3247 mono_debug_open_method (cfg
);
3249 /* emit code all basic blocks */
3250 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3251 bb
->native_offset
= cfg
->code_len
;
3252 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3253 mono_arch_output_basic_block (cfg
, bb
);
3254 bb
->native_length
= cfg
->code_len
- bb
->native_offset
;
3256 if (bb
== cfg
->bb_exit
) {
3257 cfg
->epilog_begin
= cfg
->code_len
;
3259 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3260 code
= cfg
->native_code
+ cfg
->code_len
;
3261 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3262 cfg
->code_len
= code
- cfg
->native_code
;
3263 g_assert (cfg
->code_len
< cfg
->code_size
);
3266 mono_arch_emit_epilog (cfg
);
3270 mono_arch_emit_exceptions (cfg
);
3272 max_epilog_size
= 0;
3274 code
= cfg
->native_code
+ cfg
->code_len
;
3276 /* we always allocate code in cfg->domain->code_mp to increase locality */
3277 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3278 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3280 if (cfg
->method
->dynamic
) {
3281 guint unwindlen
= 0;
3282 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3283 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3285 /* Allocate the code into a separate memory pool so it can be freed */
3286 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3287 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3288 mono_domain_lock (cfg
->domain
);
3289 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3290 mono_domain_unlock (cfg
->domain
);
3292 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3294 guint unwindlen
= 0;
3295 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3296 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3298 code
= mono_domain_code_reserve (cfg
->domain
, cfg
->code_size
+ unwindlen
);
3301 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3302 g_free (cfg
->native_code
);
3303 cfg
->native_code
= code
;
3304 code
= cfg
->native_code
+ cfg
->code_len
;
3306 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3307 mono_postprocess_patches (cfg
);
3309 #ifdef VALGRIND_JIT_REGISTER_MAP
3310 if (valgrind_register
){
3311 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3312 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3317 if (cfg
->verbose_level
> 0) {
3318 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3319 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3321 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3326 gboolean is_generic
= FALSE
;
3328 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3329 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3333 if (cfg
->generic_sharing_context
)
3334 g_assert (is_generic
);
3337 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3338 mono_arch_save_unwind_info (cfg
);
3341 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->run_cctors
);
3343 if (cfg
->method
->dynamic
) {
3344 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3346 mono_domain_code_commit (cfg
->domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3348 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3350 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3352 mono_debug_close_method (cfg
);
3354 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3355 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3360 compute_reachable (MonoBasicBlock
*bb
)
3364 if (!(bb
->flags
& BB_VISITED
)) {
3365 bb
->flags
|= BB_VISITED
;
3366 for (i
= 0; i
< bb
->out_count
; ++i
)
3367 compute_reachable (bb
->out_bb
[i
]);
3372 create_jit_info (MonoCompile
*cfg
, MonoMethod
*method_to_compile
)
3375 MonoMethodHeader
*header
;
3378 int generic_info_size
;
3379 int holes_size
= 0, num_holes
= 0;
3381 g_assert (method_to_compile
== cfg
->method
);
3382 header
= cfg
->header
;
3384 if (cfg
->generic_sharing_context
)
3385 generic_info_size
= sizeof (MonoGenericJitInfo
);
3387 generic_info_size
= 0;
3389 if (cfg
->try_block_holes
) {
3390 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3391 TryBlockHole
*hole
= tmp
->data
;
3392 MonoExceptionClause
*ec
= hole
->clause
;
3393 int hole_end
= hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
;
3394 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3395 g_assert (clause_last_bb
);
3397 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3398 if (clause_last_bb
->native_offset
!= hole_end
)
3402 holes_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3403 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3404 printf ("Number of try block holes %d\n", num_holes
);
3407 if (COMPILE_LLVM (cfg
))
3408 num_clauses
= cfg
->llvm_ex_info_len
;
3410 num_clauses
= header
->num_clauses
;
3412 if (cfg
->method
->dynamic
) {
3413 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3414 generic_info_size
+ holes_size
);
3416 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
3417 (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3418 generic_info_size
+ holes_size
);
3421 jinfo
->method
= cfg
->method_to_register
;
3422 jinfo
->code_start
= cfg
->native_code
;
3423 jinfo
->code_size
= cfg
->code_len
;
3424 jinfo
->used_regs
= cfg
->used_int_regs
;
3425 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
3426 jinfo
->cas_inited
= FALSE
; /* initialization delayed at the first stalk walk using this method */
3427 jinfo
->num_clauses
= num_clauses
;
3428 if (COMPILE_LLVM (cfg
))
3429 jinfo
->from_llvm
= TRUE
;
3431 if (cfg
->generic_sharing_context
) {
3433 MonoGenericJitInfo
*gi
;
3435 jinfo
->has_generic_jit_info
= 1;
3437 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3440 gi
->generic_sharing_context
= cfg
->generic_sharing_context
;
3442 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3443 mini_method_get_context (method_to_compile
)->method_inst
||
3444 method_to_compile
->klass
->valuetype
) {
3445 g_assert (cfg
->rgctx_var
);
3450 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3451 mini_method_get_context (method_to_compile
)->method_inst
||
3452 method_to_compile
->klass
->valuetype
) {
3453 inst
= cfg
->rgctx_var
;
3454 g_assert (inst
->opcode
== OP_REGOFFSET
);
3456 inst
= cfg
->args
[0];
3459 if (inst
->opcode
== OP_REGVAR
) {
3460 gi
->this_in_reg
= 1;
3461 gi
->this_reg
= inst
->dreg
;
3463 g_assert (inst
->opcode
== OP_REGOFFSET
);
3465 g_assert (inst
->inst_basereg
== X86_EBP
);
3466 #elif defined(TARGET_AMD64)
3467 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
3469 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
3471 gi
->this_in_reg
= 0;
3472 gi
->this_reg
= inst
->inst_basereg
;
3473 gi
->this_offset
= inst
->inst_offset
;
3478 MonoTryBlockHoleTableJitInfo
*table
;
3481 jinfo
->has_try_block_holes
= 1;
3482 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3483 table
->num_holes
= (guint16
)num_holes
;
3485 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3486 guint32 start_bb_offset
;
3487 MonoTryBlockHoleJitInfo
*hole
;
3488 TryBlockHole
*hole_data
= tmp
->data
;
3489 MonoExceptionClause
*ec
= hole_data
->clause
;
3490 int hole_end
= hole_data
->basic_block
->native_offset
+ hole_data
->basic_block
->native_length
;
3491 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3492 g_assert (clause_last_bb
);
3494 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3495 if (clause_last_bb
->native_offset
== hole_end
)
3498 start_bb_offset
= hole_data
->start_offset
- hole_data
->basic_block
->native_offset
;
3499 hole
= &table
->holes
[i
++];
3500 hole
->clause
= hole_data
->clause
- &header
->clauses
[0];
3501 hole
->offset
= (guint32
)hole_data
->start_offset
;
3502 hole
->length
= (guint16
)(hole_data
->basic_block
->native_length
- start_bb_offset
);
3504 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3505 printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole
->clause
, hole
->offset
, hole
->length
);
3507 g_assert (i
== num_holes
);
3510 if (COMPILE_LLVM (cfg
)) {
3512 memcpy (&jinfo
->clauses
[0], &cfg
->llvm_ex_info
[0], num_clauses
* sizeof (MonoJitExceptionInfo
));
3513 } else if (header
->num_clauses
) {
3516 for (i
= 0; i
< header
->num_clauses
; i
++) {
3517 MonoExceptionClause
*ec
= &header
->clauses
[i
];
3518 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3519 MonoBasicBlock
*tblock
;
3522 ei
->flags
= ec
->flags
;
3524 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
3525 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
3527 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
3528 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
3530 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
3532 ei
->data
.catch_class
= ec
->data
.catch_class
;
3535 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
3537 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
3538 g_assert (tblock
->native_offset
);
3539 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3541 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
3542 g_assert (tblock
->native_offset
);
3543 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
3545 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
3547 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3548 TryBlockHole
*hole
= tmp
->data
;
3549 gpointer hole_end
= cfg
->native_code
+ (hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
3550 if (hole
->clause
== ec
&& hole_end
== ei
->try_end
) {
3551 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3552 printf ("\tShortening try block %d from %x to %x\n", i
, (int)((guint8
*)ei
->try_end
- cfg
->native_code
), hole
->start_offset
);
3554 ei
->try_end
= cfg
->native_code
+ hole
->start_offset
;
3559 if (ec
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
3561 if (ec
->handler_offset
+ ec
->handler_len
< header
->code_size
) {
3562 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
+ ec
->handler_len
];
3564 end_offset
= tblock
->native_offset
;
3566 end_offset
= cfg
->epilog_begin
;
3568 ei
->data
.handler_end
= cfg
->native_code
+ end_offset
;
3572 if (G_UNLIKELY (cfg
->verbose_level
>= 4)) {
3573 for (i
= 0; i
< jinfo
->num_clauses
; i
++) {
3574 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3575 int start
= (guint8
*)ei
->try_start
- cfg
->native_code
;
3576 int end
= (guint8
*)ei
->try_end
- cfg
->native_code
;
3577 int handler
= (guint8
*)ei
->handler_start
- cfg
->native_code
;
3579 printf ("JitInfo EH clause %d flags %x try %x-%x handler %x\n", i
, ei
->flags
, start
, end
, handler
);
3586 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
3587 * using it during runtime, hence the define.
3589 #ifdef MONO_ARCH_HAVE_XP_UNWIND
3590 if (cfg
->encoded_unwind_ops
) {
3591 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
3592 g_free (cfg
->encoded_unwind_ops
);
3593 } else if (cfg
->unwind_ops
) {
3595 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
3597 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
3598 g_free (unwind_info
);
3607 * mini_get_shared_method:
3609 * Return the method which is actually compiled/registered when doing generic sharing.
3612 mini_get_shared_method (MonoMethod
*method
)
3614 MonoGenericContext shared_context
;
3615 MonoMethod
*declaring_method
, *res
;
3617 gboolean partial
= FALSE
;
3619 if (method
->is_generic
|| method
->klass
->generic_container
)
3620 declaring_method
= method
;
3622 declaring_method
= mono_method_get_declaring_generic_method (method
);
3624 if (declaring_method
->is_generic
)
3625 shared_context
= mono_method_get_generic_container (declaring_method
)->context
;
3627 shared_context
= declaring_method
->klass
->generic_container
->context
;
3629 /* Handle partial sharing */
3630 if (method
!= declaring_method
&& method
->is_inflated
&& !mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
)) {
3631 MonoGenericContext
*context
= mono_method_get_context (method
);
3632 MonoGenericInst
*inst
;
3633 MonoType
**type_argv
;
3636 * Create the shared context by replacing the ref type arguments with
3637 * type parameters, and keeping the rest.
3640 inst
= context
->class_inst
;
3642 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3643 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3644 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
)
3645 type_argv
[i
] = shared_context
.class_inst
->type_argv
[i
];
3647 type_argv
[i
] = inst
->type_argv
[i
];
3650 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3654 inst
= context
->method_inst
;
3656 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3657 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3658 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
)
3659 type_argv
[i
] = shared_context
.method_inst
->type_argv
[i
];
3661 type_argv
[i
] = inst
->type_argv
[i
];
3664 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3669 res
= mono_class_inflate_generic_method (declaring_method
, &shared_context
);
3671 /* The result should be an inflated method whose parent is not inflated */
3672 g_assert (!res
->klass
->is_inflated
);
3679 * mini_method_compile:
3680 * @method: the method to compile
3681 * @opts: the optimization flags to use
3682 * @domain: the domain where the method will be compiled in
3683 * @run_cctors: whether we should run type ctors if possible
3684 * @compile_aot: whether this is an AOT compilation
3685 * @parts: debug flag
3687 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3688 * field in the returned struct to see if compilation succeded.
3691 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
3693 MonoMethodHeader
*header
;
3694 MonoMethodSignature
*sig
;
3698 int dfn
, i
, code_size_ratio
;
3699 gboolean deadce_has_run
= FALSE
;
3700 gboolean try_generic_shared
, try_llvm
;
3701 MonoMethod
*method_to_compile
, *method_to_register
;
3703 mono_jit_stats
.methods_compiled
++;
3704 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
3705 mono_profiler_method_jit (method
);
3706 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
3707 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
3711 * We might get passed the original generic method definition or
3712 * instances with type parameters.
3713 * FIXME: Remove the method->klass->generic_class limitation.
3715 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3716 (opts
& MONO_OPT_GSHARED
) && ((method
->is_generic
|| method
->klass
->generic_container
) || (!method
->klass
->generic_class
&& mono_method_is_generic_sharable_impl (method
, TRUE
)));
3718 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3719 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl (method
, FALSE
);
3721 if (opts
& MONO_OPT_GSHARED
) {
3722 if (try_generic_shared
)
3723 mono_stats
.generics_sharable_methods
++;
3724 else if (mono_method_is_generic_impl (method
))
3725 mono_stats
.generics_unsharable_methods
++;
3735 if (try_generic_shared
) {
3736 method_to_compile
= mini_get_shared_method (method
);
3737 g_assert (method_to_compile
);
3739 method_to_compile
= method
;
3742 cfg
= g_new0 (MonoCompile
, 1);
3743 cfg
->method
= method_to_compile
;
3744 cfg
->header
= mono_method_get_header (cfg
->method
);
3745 cfg
->mempool
= mono_mempool_new ();
3747 cfg
->prof_options
= mono_profiler_get_events ();
3748 cfg
->run_cctors
= run_cctors
;
3749 cfg
->domain
= domain
;
3750 cfg
->verbose_level
= mini_verbose
;
3751 cfg
->compile_aot
= compile_aot
;
3752 cfg
->skip_visibility
= method
->skip_visibility
;
3753 cfg
->orig_method
= method
;
3754 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
3755 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
3756 if (try_generic_shared
)
3757 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->generic_sharing_context
;
3758 cfg
->compile_llvm
= try_llvm
;
3759 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
3761 if (cfg
->gen_seq_points
)
3762 cfg
->seq_points
= g_ptr_array_new ();
3764 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
)) {
3765 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
3769 if (cfg
->generic_sharing_context
) {
3770 method_to_register
= method_to_compile
;
3772 g_assert (method
== method_to_compile
);
3773 method_to_register
= method
;
3775 cfg
->method_to_register
= method_to_register
;
3777 if (cfg
->compile_llvm
) {
3778 /* No way to obtain the location info for 'this' */
3779 if (try_generic_shared
) {
3780 cfg
->exception_message
= g_strdup ("gshared");
3781 cfg
->disable_llvm
= TRUE
;
3784 if (cfg
->method
->save_lmf
) {
3785 cfg
->exception_message
= g_strdup ("lmf");
3786 cfg
->disable_llvm
= TRUE
;
3790 if (cfg
->method
->dynamic
) {
3791 cfg
->exception_message
= g_strdup ("dynamic.");
3792 cfg
->disable_llvm
= TRUE
;
3796 mono_error_init (&err
);
3797 sig
= mono_method_signature_checked (cfg
->method
, &err
);
3799 cfg
->exception_type
= MONO_EXCEPTION_TYPE_LOAD
;
3800 cfg
->exception_message
= g_strdup (mono_error_get_message (&err
));
3801 mono_error_cleanup (&err
);
3802 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3803 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3807 header
= cfg
->header
;
3809 MonoLoaderError
*error
;
3811 if ((error
= mono_loader_get_last_error ())) {
3812 cfg
->exception_type
= error
->exception_type
;
3814 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
3815 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
3817 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3818 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3822 if (FALSE
&& header
->clauses
) {
3824 * Cannot be enabled until LLVM supports implicit exceptions, or we use
3825 * explicit checks, or we disable this for methods which might throw implicit
3826 * exceptions inside clauses.
3828 cfg
->exception_message
= g_strdup ("clauses");
3829 cfg
->disable_llvm
= TRUE
;
3834 static gboolean inited
;
3837 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_with_llvm
);
3838 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_without_llvm
);
3843 * Check for methods which cannot be compiled by LLVM early, to avoid
3844 * the extra compilation pass.
3846 if (COMPILE_LLVM (cfg
) && cfg
->disable_llvm
) {
3847 if (cfg
->verbose_level
>= 1) {
3848 //nm = mono_method_full_name (cfg->method, TRUE);
3849 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3852 InterlockedIncrement (&methods_without_llvm
);
3853 mono_destroy_compile (cfg
);
3855 goto restart_compile
;
3860 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3861 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
3862 cfg
->disable_reuse_registers
= TRUE
;
3863 cfg
->disable_reuse_stack_slots
= TRUE
;
3865 * This decreases the change the debugger will read registers/stack slots which are
3866 * not yet initialized.
3868 cfg
->disable_initlocals_opt
= TRUE
;
3870 cfg
->extend_live_ranges
= TRUE
;
3872 /* Temporarily disable this when running in the debugger until we have support
3873 * for this in the debugger. */
3874 cfg
->disable_omit_fp
= TRUE
;
3876 /* The debugger needs all locals to be on the stack or in a global register */
3877 cfg
->disable_vreg_to_lvreg
= TRUE
;
3879 /* Don't remove unused variables when running inside the debugger since the user
3880 * may still want to view them. */
3881 cfg
->disable_deadce_vars
= TRUE
;
3883 // cfg->opt |= MONO_OPT_SHARED;
3884 cfg
->opt
&= ~MONO_OPT_DEADCE
;
3885 cfg
->opt
&= ~MONO_OPT_INLINE
;
3886 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
3887 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
3888 cfg
->opt
&= ~MONO_OPT_GSHARED
;
3890 /* This is needed for the soft debugger, which doesn't like code after the epilog */
3891 cfg
->disable_out_of_line_bblocks
= TRUE
;
3894 if (mono_using_xdebug
) {
3896 * Make each variable use its own register/stack slot and extend
3897 * their liveness to cover the whole method, making them displayable
3898 * in gdb even after they are dead.
3900 cfg
->disable_reuse_registers
= TRUE
;
3901 cfg
->disable_reuse_stack_slots
= TRUE
;
3902 cfg
->extend_live_ranges
= TRUE
;
3903 cfg
->compute_precise_live_ranges
= TRUE
;
3906 mini_gc_init_gc_map (cfg
);
3908 if (COMPILE_LLVM (cfg
)) {
3909 cfg
->opt
|= MONO_OPT_ABCREM
;
3912 if (getenv ("MONO_VERBOSE_METHOD")) {
3913 char *name
= getenv ("MONO_VERBOSE_METHOD");
3915 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
3916 MonoMethodDesc
*desc
;
3918 desc
= mono_method_desc_new (name
, TRUE
);
3919 if (mono_method_desc_full_match (desc
, cfg
->method
)) {
3920 cfg
->verbose_level
= 4;
3922 mono_method_desc_free (desc
);
3924 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
3925 cfg
->verbose_level
= 4;
3929 ip
= (guint8
*)header
->code
;
3931 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
3933 if (cfg
->verbose_level
> 0) {
3934 if (COMPILE_LLVM (cfg
))
3935 g_print ("converting llvm method %s\n", mono_method_full_name (method
, TRUE
));
3936 else if (cfg
->generic_sharing_context
)
3937 g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile
, TRUE
));
3939 g_print ("converting method %s\n", mono_method_full_name (method
, TRUE
));
3942 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
3943 cfg
->opt
|= MONO_OPT_SSA
;
3946 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")))
3947 cfg->globalra = TRUE;
3950 //cfg->globalra = TRUE;
3952 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3953 // cfg->globalra = TRUE;
3956 static int count
= 0;
3960 if (getenv ("COUNT2")) {
3961 cfg->globalra = TRUE;
3962 if (count == atoi (getenv ("COUNT2")))
3963 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3964 if (count > atoi (getenv ("COUNT2")))
3965 cfg->globalra = FALSE;
3970 if (header
->clauses
)
3971 cfg
->globalra
= FALSE
;
3973 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
3974 /* The code in the prolog clobbers caller saved registers */
3975 cfg
->globalra
= FALSE
;
3977 // FIXME: Disable globalra in case of tracing/profiling
3979 if (cfg
->method
->save_lmf
)
3980 /* The LMF saving code might clobber caller saved registers */
3981 cfg
->globalra
= FALSE
;
3983 if (header
->code_size
> 5000)
3985 /* Too large bblocks could overflow the ins positions */
3986 cfg
->globalra
= FALSE
;
3988 cfg
->rs
= mono_regstate_new ();
3990 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
3991 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
3993 /* FIXME: Fix SSA to handle branches inside bblocks */
3994 if (cfg
->opt
& MONO_OPT_SSA
)
3995 cfg
->enable_extended_bblocks
= FALSE
;
3998 * FIXME: This confuses liveness analysis because variables which are assigned after
3999 * a branch inside a bblock become part of the kill set, even though the assignment
4000 * might not get executed. This causes the optimize_initlocals pass to delete some
4001 * assignments which are needed.
4002 * Also, the mono_if_conversion pass needs to be modified to recognize the code
4005 //cfg->enable_extended_bblocks = TRUE;
4007 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
4008 if (mono_compile_is_broken (cfg
))
4012 * create MonoInst* which represents arguments and local variables
4014 mono_compile_create_vars (cfg
);
4016 /* SSAPRE is not supported on linear IR */
4017 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
4019 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
4022 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
4024 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4025 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4028 mono_destroy_compile (cfg
);
4029 try_generic_shared
= FALSE
;
4030 goto restart_compile
;
4032 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
4034 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4035 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4036 /* cfg contains the details of the failure, so let the caller cleanup */
4040 mono_jit_stats
.basic_blocks
+= cfg
->num_bblocks
;
4041 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->num_bblocks
, mono_jit_stats
.max_basic_blocks
);
4043 if (COMPILE_LLVM (cfg
)) {
4046 /* The IR has to be in SSA form for LLVM */
4047 cfg
->opt
|= MONO_OPT_SSA
;
4051 // Allow SSA on the result value
4052 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
4054 // Add an explicit return instruction referencing the return value
4055 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
4056 ins
->sreg1
= cfg
->ret
->dreg
;
4058 MONO_ADD_INS (cfg
->bb_exit
, ins
);
4061 cfg
->opt
&= ~MONO_OPT_LINEARS
;
4064 cfg
->opt
&= ~MONO_OPT_BRANCH
;
4067 /* todo: remove code when we have verified that the liveness for try/catch blocks
4071 * Currently, this can't be commented out since exception blocks are not
4072 * processed during liveness analysis.
4073 * It is also needed, because otherwise the local optimization passes would
4074 * delete assignments in cases like this:
4076 * <something which throws>
4078 * This also allows SSA to be run on methods containing exception clauses, since
4079 * SSA will ignore variables marked VOLATILE.
4081 mono_liveness_handle_exception_clauses (cfg
);
4083 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
4085 if (!COMPILE_LLVM (cfg
))
4086 mono_decompose_long_opts (cfg
);
4088 /* Should be done before branch opts */
4089 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
4090 mono_local_cprop (cfg
);
4092 if (cfg
->opt
& MONO_OPT_BRANCH
)
4093 mono_optimize_branches (cfg
);
4095 /* This must be done _before_ global reg alloc and _after_ decompose */
4096 mono_handle_global_vregs (cfg
);
4097 if (cfg
->opt
& MONO_OPT_DEADCE
)
4098 mono_local_deadce (cfg
);
4099 /* Disable this for LLVM to make the IR easier to handle */
4100 if (!COMPILE_LLVM (cfg
))
4101 mono_if_conversion (cfg
);
4103 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
4104 mono_remove_critical_edges (cfg
);
4106 /* Depth-first ordering on basic blocks */
4107 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4109 cfg
->max_block_num
= cfg
->num_bblocks
;
4112 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4113 if (cfg
->num_bblocks
!= dfn
+ 1) {
4116 cfg
->num_bblocks
= dfn
+ 1;
4118 /* remove unreachable code, because the code in them may be
4119 * inconsistent (access to dead variables for example) */
4120 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4121 bb
->flags
&= ~BB_VISITED
;
4122 compute_reachable (cfg
->bb_entry
);
4123 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4124 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
4125 compute_reachable (bb
);
4126 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4127 if (!(bb
->flags
& BB_VISITED
)) {
4128 if (cfg
->verbose_level
> 1)
4129 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
4130 bb
->code
= bb
->last_ins
= NULL
;
4131 while (bb
->out_count
)
4132 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
4135 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4136 bb
->flags
&= ~BB_VISITED
;
4139 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
4141 * we disable some optimizations if there are too many variables
4142 * because JIT time may become too expensive. The actual number needs
4143 * to be tweaked and eventually the non-linear algorithms should be fixed.
4145 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
4146 cfg
->disable_ssa
= TRUE
;
4149 if (cfg
->opt
& MONO_OPT_LOOP
) {
4150 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
4151 mono_compute_natural_loops (cfg
);
4154 /* after method_to_ir */
4156 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4157 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4162 if (header->num_clauses)
4163 cfg->disable_ssa = TRUE;
4166 //#define DEBUGSSA "logic_run"
4167 #define DEBUGSSA_CLASS "Tests"
4170 if (!cfg
->disable_ssa
) {
4171 mono_local_cprop (cfg
);
4174 mono_ssa_compute (cfg
);
4178 if (cfg
->opt
& MONO_OPT_SSA
) {
4179 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
4181 mono_ssa_compute (cfg
);
4184 if (cfg
->verbose_level
>= 2) {
4191 /* after SSA translation */
4193 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4194 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4198 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
4199 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4201 mono_ssa_cprop (cfg
);
4207 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4208 //mono_ssa_strength_reduction (cfg);
4210 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
4211 mono_perform_ssapre (cfg
);
4212 //mono_local_cprop (cfg);
4215 if (cfg
->opt
& MONO_OPT_DEADCE
) {
4216 mono_ssa_deadce (cfg
);
4217 deadce_has_run
= TRUE
;
4220 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4221 mono_perform_abc_removal (cfg
);
4223 mono_ssa_remove (cfg
);
4224 mono_local_cprop (cfg
);
4225 mono_handle_global_vregs (cfg
);
4226 if (cfg
->opt
& MONO_OPT_DEADCE
)
4227 mono_local_deadce (cfg
);
4229 if (cfg
->opt
& MONO_OPT_BRANCH
) {
4232 mono_optimize_branches (cfg
);
4234 /* Have to recompute cfg->bblocks and bb->dfn */
4235 if (cfg
->globalra
) {
4236 mono_remove_critical_edges (cfg
);
4238 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4241 /* Depth-first ordering on basic blocks */
4242 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4245 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4246 cfg
->num_bblocks
= dfn
+ 1;
4252 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
4253 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4254 mono_perform_abc_removal (cfg
);
4257 /* after SSA removal */
4259 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4260 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4264 #ifdef MONO_ARCH_SOFT_FLOAT
4265 if (!COMPILE_LLVM (cfg
))
4266 mono_decompose_soft_float (cfg
);
4268 if (!COMPILE_LLVM (cfg
))
4269 mono_decompose_vtype_opts (cfg
);
4270 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4271 mono_decompose_array_access_opts (cfg
);
4276 g_assert (cfg
->got_var_allocated
);
4279 * Allways allocate the GOT var to a register, because keeping it
4280 * in memory will increase the number of live temporaries in some
4281 * code created by inssel.brg, leading to the well known spills+
4282 * branches problem. Testcase: mcs crash in
4283 * System.MonoCustomAttrs:GetCustomAttributes.
4285 regs
= mono_arch_get_global_int_regs (cfg
);
4287 cfg
->got_var
->opcode
= OP_REGVAR
;
4288 cfg
->got_var
->dreg
= GPOINTER_TO_INT (regs
->data
);
4289 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
4295 * Have to call this again to process variables added since the first call.
4297 mono_liveness_handle_exception_clauses (cfg
);
4299 if (cfg
->globalra
) {
4302 /* Have to do this before regalloc since it can create vregs */
4303 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4304 mono_arch_lowering_pass (cfg
, bb
);
4306 mono_global_regalloc (cfg
);
4309 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
4312 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
4313 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
4314 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
4315 mono_analyze_liveness (cfg
);
4317 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
4318 regs
= mono_arch_get_global_int_regs (cfg
);
4320 regs
= g_list_delete_link (regs
, regs
);
4321 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
4325 //mono_print_code (cfg, "");
4329 /* variables are allocated after decompose, since decompose could create temps */
4330 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4331 mono_arch_allocate_vars (cfg
);
4332 if (cfg
->exception_type
)
4338 gboolean need_local_opts
;
4340 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4341 mono_spill_global_vars (cfg
, &need_local_opts
);
4343 if (need_local_opts
|| cfg
->compile_aot
) {
4344 /* To optimize code created by spill_global_vars */
4345 mono_local_cprop (cfg
);
4346 if (cfg
->opt
& MONO_OPT_DEADCE
)
4347 mono_local_deadce (cfg
);
4351 /* Add branches between non-consecutive bblocks */
4352 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4353 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
4354 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
4355 /* we are careful when inverting, since bugs like #59580
4356 * could show up when dealing with NaNs.
4358 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
4359 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
4360 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
4361 bb
->last_ins
->inst_false_bb
= tmp
;
4363 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
4365 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
4366 inst
->opcode
= OP_BR
;
4367 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
4368 mono_bblock_add_inst (bb
, inst
);
4373 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
4374 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4375 MonoInst
*tree
= bb
->code
;
4376 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
4379 for (; tree
; tree
= tree
->next
) {
4380 mono_print_ins_index (-1, tree
);
4386 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4387 bb
->max_vreg
= cfg
->next_vreg
;
4391 if (COMPILE_LLVM (cfg
)) {
4395 /* The IR has to be in SSA form for LLVM */
4396 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
4397 cfg
->exception_message
= g_strdup ("SSA disabled.");
4398 cfg
->disable_llvm
= TRUE
;
4401 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4402 mono_decompose_array_access_opts (cfg
);
4404 if (!cfg
->disable_llvm
)
4405 mono_llvm_emit_method (cfg
);
4406 if (cfg
->disable_llvm
) {
4407 if (cfg
->verbose_level
>= 1) {
4408 //nm = mono_method_full_name (cfg->method, TRUE);
4409 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
4412 InterlockedIncrement (&methods_without_llvm
);
4413 mono_destroy_compile (cfg
);
4415 goto restart_compile
;
4418 InterlockedIncrement (&methods_with_llvm
);
4420 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
4421 nm
= mono_method_full_name (cfg
->method
, TRUE
);
4422 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
4424 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
4432 if (cfg
->verbose_level
>= 2) {
4433 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
4434 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
4438 cfg
->jit_info
= create_jit_info (cfg
, method_to_compile
);
4440 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
4441 if (cfg
->extend_live_ranges
) {
4442 /* Extend live ranges to cover the whole method */
4443 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
4444 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
4448 mono_save_xdebug_info (cfg
);
4450 mini_gc_create_gc_map (cfg
);
4452 mono_save_seq_point_info (cfg
);
4454 if (!cfg
->compile_aot
) {
4455 mono_domain_lock (cfg
->domain
);
4456 mono_jit_info_table_add (cfg
->domain
, cfg
->jit_info
);
4458 if (cfg
->method
->dynamic
)
4459 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= cfg
->jit_info
;
4460 mono_domain_unlock (cfg
->domain
);
4463 /* collect statistics */
4464 mono_perfcounters
->jit_methods
++;
4465 mono_perfcounters
->jit_bytes
+= header
->code_size
;
4466 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
4467 code_size_ratio
= cfg
->code_len
;
4468 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
4469 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
4470 g_free (mono_jit_stats
.biggest_method
);
4471 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4473 code_size_ratio
= (code_size_ratio
* 100) / header
->code_size
;
4474 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
4475 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
4476 g_free (mono_jit_stats
.max_ratio_method
);
4477 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4479 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
4481 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4482 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4490 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4492 g_assert_not_reached ();
4496 #endif /* DISABLE_JIT */
4499 mono_domain_lookup_shared_generic (MonoDomain
*domain
, MonoMethod
*method
)
4501 static gboolean inited
= FALSE
;
4502 static int lookups
= 0;
4503 static int failed_lookups
= 0;
4506 ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, mini_get_shared_method (method
));
4507 if (ji
&& !ji
->has_generic_jit_info
)
4511 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
4512 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
4524 * LOCKING: Assumes domain->jit_code_hash_lock is held.
4527 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
4529 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
4534 if (!mono_method_is_generic_sharable_impl (method
, FALSE
))
4536 return mono_domain_lookup_shared_generic (domain
, method
);
4540 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
4544 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
4545 mono_domain_jit_code_hash_lock (domain
);
4546 info
= lookup_method_inner (domain
, method
);
4547 mono_domain_jit_code_hash_unlock (domain
);
4548 mono_loader_unlock ();
4554 static FILE* perf_map_file
= NULL
;
4557 mono_enable_jit_map (void)
4559 if (!perf_map_file
) {
4561 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
4563 perf_map_file
= fopen (name
, "w");
4568 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
4571 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
4575 mono_emit_jit_map (MonoJitInfo
*jinfo
)
4577 if (perf_map_file
) {
4578 char *name
= mono_method_full_name (jinfo
->method
, TRUE
);
4579 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
4586 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
4589 gpointer code
= NULL
;
4590 MonoJitInfo
*jinfo
, *info
;
4592 MonoException
*ex
= NULL
;
4593 guint32 prof_options
;
4595 #ifdef MONO_USE_AOT_COMPILER
4596 if (opt
& MONO_OPT_AOT
) {
4597 MonoDomain
*domain
= mono_domain_get ();
4599 mono_class_init (method
->klass
);
4601 if ((code
= mono_aot_get_method (domain
, method
))) {
4602 vtable
= mono_class_vtable (domain
, method
->klass
);
4604 mono_runtime_class_init (vtable
);
4611 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4612 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
4614 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
4616 if (!piinfo
->addr
) {
4617 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
4618 piinfo
->addr
= mono_lookup_internal_call (method
);
4619 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
4621 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
);
4623 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
);
4626 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
4628 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
4629 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4631 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
4632 //mono_debug_add_wrapper (method, nm);
4633 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
4634 const char *name
= method
->name
;
4637 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
4638 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
4639 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
4642 * We need to make sure this wrapper
4643 * is compiled because it might end up
4644 * in an (M)RGCTX if generic sharing
4645 * is enabled, and would be called
4646 * indirectly. If it were a
4647 * trampoline we'd try to patch that
4648 * indirect call, which is not
4651 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
4652 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
4653 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4654 return mono_create_delegate_trampoline (method
->klass
);
4656 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
4657 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4659 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
4660 nm
= mono_marshal_get_delegate_begin_invoke (method
);
4661 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4662 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
4663 nm
= mono_marshal_get_delegate_end_invoke (method
);
4664 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4670 if (mono_aot_only
) {
4671 char *fullname
= mono_method_full_name (method
, TRUE
);
4672 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only.\n", fullname
);
4674 *jit_ex
= mono_get_exception_execution_engine (msg
);
4681 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
4683 switch (cfg
->exception_type
) {
4684 case MONO_EXCEPTION_NONE
:
4686 case MONO_EXCEPTION_TYPE_LOAD
:
4687 case MONO_EXCEPTION_MISSING_FIELD
:
4688 case MONO_EXCEPTION_MISSING_METHOD
:
4689 case MONO_EXCEPTION_FILE_NOT_FOUND
:
4690 case MONO_EXCEPTION_BAD_IMAGE
: {
4691 /* Throw a type load exception if needed */
4692 MonoLoaderError
*error
= mono_loader_get_last_error ();
4695 ex
= mono_loader_error_prepare_exception (error
);
4697 if (cfg
->exception_ptr
) {
4698 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
4700 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
4701 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
4702 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
4703 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
4704 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
4705 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
4706 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
4707 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
4708 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
4709 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
4711 g_assert_not_reached ();
4716 case MONO_EXCEPTION_INVALID_PROGRAM
:
4717 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
4719 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
4720 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
4722 case MONO_EXCEPTION_METHOD_ACCESS
:
4723 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
4725 case MONO_EXCEPTION_FIELD_ACCESS
:
4726 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
4728 /* this can only be set if the security manager is active */
4729 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
4730 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
4731 MonoObject
*exc
= NULL
;
4734 args
[0] = &cfg
->exception_data
;
4736 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
4738 ex
= (MonoException
*)exc
;
4741 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
4742 MonoException
*exp
= cfg
->exception_ptr
;
4743 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
4749 g_assert_not_reached ();
4753 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4754 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
4756 mono_destroy_compile (cfg
);
4762 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
4763 mono_domain_lock (target_domain
);
4765 /* Check if some other thread already did the job. In this case, we can
4766 discard the code this thread generated. */
4768 mono_domain_jit_code_hash_lock (target_domain
);
4770 info
= lookup_method_inner (target_domain
, method
);
4772 /* We can't use a domain specific method in another domain */
4773 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
4774 code
= info
->code_start
;
4775 // printf("Discarding code for method %s\n", method->name);
4780 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
4781 mono_domain_jit_code_hash_unlock (target_domain
);
4782 code
= cfg
->native_code
;
4784 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl (method
, FALSE
))
4785 mono_stats
.generics_shared_methods
++;
4787 mono_domain_jit_code_hash_unlock (target_domain
);
4790 jinfo
= cfg
->jit_info
;
4792 prof_options
= cfg
->prof_options
;
4794 mono_destroy_compile (cfg
);
4797 if (domain_jit_info (target_domain
)->jump_target_hash
) {
4798 MonoJumpInfo patch_info
;
4800 list
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4802 patch_info
.next
= NULL
;
4803 patch_info
.ip
.i
= 0;
4804 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
4805 patch_info
.data
.method
= method
;
4806 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4808 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
4809 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, TRUE
);
4810 g_slist_free (list
);
4813 mono_emit_jit_map (jinfo
);
4815 mono_domain_unlock (target_domain
);
4816 mono_loader_unlock ();
4818 vtable
= mono_class_vtable (target_domain
, method
->klass
);
4821 exc
= mono_class_get_exception_for_failure (method
->klass
);
4823 mono_raise_exception (exc
);
4826 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
) {
4827 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
4828 /* The profiler doesn't know about wrappers, so pass the original icall method */
4829 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method
), jinfo
, MONO_PROFILE_OK
);
4831 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
4834 mono_runtime_class_init (vtable
);
4839 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
4841 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
4844 MonoJitICallInfo
*callinfo
= NULL
;
4847 * ICALL wrappers are handled specially, since there is only one copy of them
4848 * shared by all appdomains.
4850 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
4851 const char *icall_name
;
4853 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
4854 g_assert (icall_name
);
4855 callinfo
= mono_find_jit_icall_by_name (icall_name
);
4856 g_assert (callinfo
);
4858 /* Must be domain neutral since there is only one copy */
4859 opt
|= MONO_OPT_SHARED
;
4862 if (opt
& MONO_OPT_SHARED
)
4863 target_domain
= mono_get_root_domain ();
4865 target_domain
= domain
;
4867 info
= lookup_method (target_domain
, method
);
4869 /* We can't use a domain specific method in another domain */
4870 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4873 mono_jit_stats
.methods_lookups
++;
4874 vtable
= mono_class_vtable (domain
, method
->klass
);
4876 mono_runtime_class_init (vtable
);
4877 return mono_create_ftnptr (target_domain
, info
->code_start
);
4881 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
4885 p
= mono_create_ftnptr (target_domain
, code
);
4889 if (!callinfo
->wrapper
) {
4890 callinfo
->wrapper
= p
;
4891 mono_register_jit_icall_wrapper (callinfo
, p
);
4892 mono_debug_add_icall_wrapper (method
, callinfo
);
4901 mono_jit_compile_method (MonoMethod
*method
)
4903 MonoException
*ex
= NULL
;
4906 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
4909 mono_raise_exception (ex
);
4915 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4917 invalidated_delegate_trampoline (char *desc
)
4919 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
4920 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
4926 * mono_jit_free_method:
4928 * Free all memory allocated by the JIT for METHOD.
4931 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
4933 MonoJitDynamicMethodInfo
*ji
;
4934 gboolean destroy
= TRUE
;
4936 g_assert (method
->dynamic
);
4938 mono_domain_lock (domain
);
4939 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
4940 mono_domain_unlock (domain
);
4944 mono_domain_lock (domain
);
4945 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
4946 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
4947 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
4948 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
4949 mono_domain_unlock (domain
);
4951 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4952 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
4954 * Instead of freeing the code, change it to call an error routine
4955 * so people can fix their code.
4957 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
4958 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
4961 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
4967 * This needs to be done before freeing code_mp, since the code address is the
4968 * key in the table, so if we free the code_mp first, another thread can grab the
4969 * same code address and replace our entry in the table.
4971 mono_jit_info_table_remove (domain
, ji
->ji
);
4974 mono_code_manager_destroy (ji
->code_mp
);
4979 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
4981 MonoDomain
*target_domain
;
4984 if (default_opt
& MONO_OPT_SHARED
)
4985 target_domain
= mono_get_root_domain ();
4987 target_domain
= domain
;
4989 info
= lookup_method (target_domain
, method
);
4991 /* We can't use a domain specific method in another domain */
4992 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4993 mono_jit_stats
.methods_lookups
++;
4996 return info
->code_start
;
5006 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
5008 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
5013 gpointer compiled_method
;
5014 gpointer runtime_invoke
;
5016 MonoDynCallInfo
*dyn_call_info
;
5017 MonoClass
*ret_box_class
;
5018 } RuntimeInvokeInfo
;
5021 * mono_jit_runtime_invoke:
5022 * @method: the method to invoke
5023 * @obj: this pointer
5024 * @params: array of parameter values.
5025 * @exc: used to catch exceptions objects
5028 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
5031 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
5032 MonoDomain
*domain
= mono_domain_get ();
5033 MonoJitDomainInfo
*domain_info
;
5034 RuntimeInvokeInfo
*info
, *info2
;
5036 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
5037 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
5041 domain_info
= domain_jit_info (domain
);
5043 mono_domain_lock (domain
);
5044 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5045 mono_domain_unlock (domain
);
5048 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
5050 * This might be redundant since mono_class_vtable () already does this,
5051 * but keep it just in case for moonlight.
5053 mono_class_setup_vtable (method
->klass
);
5054 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
5056 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
5058 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
5063 info
= g_new0 (RuntimeInvokeInfo
, 1);
5065 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
5066 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
5067 g_assert (info
->vtable
);
5069 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
5070 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
5072 * Array Get/Set/Address methods. The JIT implements them using inline code
5073 * inside the runtime invoke wrappers, so no need to compile them.
5075 info
->compiled_method
= NULL
;
5077 MonoException
*jit_ex
= NULL
;
5079 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
5080 if (!info
->compiled_method
) {
5084 *exc
= (MonoObject
*)jit_ex
;
5087 mono_raise_exception (jit_ex
);
5091 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
5092 info
->compiled_method
= mono_create_static_rgctx_trampoline (method
, info
->compiled_method
);
5096 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
5097 * in full-aot mode, so we use a slower, but more generic wrapper if
5098 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
5100 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5101 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
5102 MonoMethodSignature
*sig
= mono_method_signature (method
);
5103 gboolean supported
= TRUE
;
5106 if (method
->string_ctor
)
5107 sig
= mono_marshal_get_string_ctor_signature (method
);
5109 for (i
= 0; i
< sig
->param_count
; ++i
) {
5110 MonoType
*t
= sig
->params
[i
];
5112 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
5116 if (method
->klass
->contextbound
|| !info
->compiled_method
)
5120 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
5122 if (info
->dyn_call_info
) {
5123 switch (sig
->ret
->type
) {
5124 case MONO_TYPE_VOID
:
5136 case MONO_TYPE_BOOLEAN
:
5137 case MONO_TYPE_CHAR
:
5140 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5143 info
->ret_box_class
= mono_defaults
.int_class
;
5145 case MONO_TYPE_STRING
:
5146 case MONO_TYPE_CLASS
:
5147 case MONO_TYPE_ARRAY
:
5148 case MONO_TYPE_SZARRAY
:
5149 case MONO_TYPE_OBJECT
:
5151 case MONO_TYPE_GENERICINST
:
5152 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
5153 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5155 case MONO_TYPE_VALUETYPE
:
5156 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5159 g_assert_not_reached ();
5166 if (!info
->dyn_call_info
)
5167 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
5169 mono_domain_lock (domain
);
5170 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5175 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
5177 mono_domain_unlock (domain
);
5180 runtime_invoke
= info
->runtime_invoke
;
5183 * We need this here because mono_marshal_get_runtime_invoke can place
5184 * the helper method in System.Object and not the target class.
5186 mono_runtime_class_init (info
->vtable
);
5188 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5189 if (info
->dyn_call_info
) {
5190 MonoMethodSignature
*sig
= mono_method_signature (method
);
5192 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
5195 guint8 retval
[128];
5197 if (!dyn_runtime_invoke
) {
5198 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
5199 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
5202 /* Convert the arguments to the format expected by start_dyn_call () */
5203 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
5206 args
[pindex
++] = &obj
;
5207 for (i
= 0; i
< sig
->param_count
; ++i
) {
5208 MonoType
*t
= sig
->params
[i
];
5211 args
[pindex
++] = ¶ms
[i
];
5212 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
5213 args
[pindex
++] = ¶ms
[i
];
5215 args
[pindex
++] = params
[i
];
5219 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
5221 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
5223 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
5225 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
5227 if (info
->ret_box_class
)
5228 return mono_value_box (domain
, info
->ret_box_class
, retval
);
5230 return *(MonoObject
**)retval
;
5234 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
5238 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
5240 MonoException
*exc
= NULL
;
5242 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
5247 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5249 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
5250 if (mono_arch_is_int_overflow (ctx
, info
))
5251 exc
= mono_get_exception_arithmetic ();
5253 exc
= mono_get_exception_divide_by_zero ();
5255 exc
= mono_get_exception_divide_by_zero ();
5259 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5262 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5265 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5269 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
5274 exc
= mono_get_exception_execution_engine ("SIGILL");
5276 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5279 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
5280 #define HAVE_SIG_INFO
5284 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
5287 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
5291 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
5292 if (mono_arch_is_single_step_event (info
, ctx
)) {
5293 mono_debugger_agent_single_step_event (ctx
);
5295 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
5296 mono_debugger_agent_breakpoint_hit (ctx
);
5301 #if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
5302 if (mono_aot_is_pagefault (info
->si_addr
)) {
5303 mono_aot_handle_pagefault (info
->si_addr
);
5308 /* The thread might no be registered with the runtime */
5309 if (!mono_domain_get () || !jit_tls
) {
5310 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5312 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5315 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5317 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
5318 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
5321 /* The hard-guard page has been hit: there is not much we can do anymore
5322 * Print a hopefully clear message and abort.
5324 if (jit_tls
->stack_size
&&
5325 ABS ((guint8
*)info
->si_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 32768) {
5327 /* we don't do much now, but we can warn the user with a useful message */
5328 fprintf (stderr
, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx
), (gpointer
)info
->si_addr
);
5329 if (ji
&& ji
->method
)
5330 method
= mono_method_full_name (ji
->method
, TRUE
);
5332 method
= "Unmanaged";
5333 fprintf (stderr
, "At %s\n", method
);
5336 /* The original handler might not like that it is executed on an altstack... */
5337 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
5340 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
5345 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5348 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5351 mono_arch_handle_exception (ctx
, NULL
, FALSE
);
5356 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
5361 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
5363 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5366 /* mono_jit_create_remoting_trampoline:
5367 * @method: pointer to the method info
5369 * Creates a trampoline which calls the remoting functions. This
5370 * is used in the vtable of transparent proxies.
5372 * Returns: a pointer to the newly created code
5375 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
5378 guint8
*addr
= NULL
;
5380 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
5381 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
5385 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
5386 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
5387 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
5388 addr
= mono_compile_method (nm
);
5390 addr
= mono_compile_method (method
);
5392 return mono_get_addr_from_ftnptr (addr
);
5395 #ifdef MONO_ARCH_HAVE_IMT
5396 static G_GNUC_UNUSED gpointer
5397 mini_get_imt_trampoline (void)
5399 static gpointer tramp
= NULL
;
5401 tramp
= mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5407 mini_get_vtable_trampoline (int slot_index
)
5409 static gpointer tramp
= NULL
;
5412 tramp
= mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5417 mini_parse_debug_options (void)
5419 char *options
= getenv ("MONO_DEBUG");
5420 gchar
**args
, **ptr
;
5425 args
= g_strsplit (options
, ",", -1);
5427 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
5428 const char *arg
= *ptr
;
5430 if (!strcmp (arg
, "handle-sigint"))
5431 debug_options
.handle_sigint
= TRUE
;
5432 else if (!strcmp (arg
, "keep-delegates"))
5433 debug_options
.keep_delegates
= TRUE
;
5434 else if (!strcmp (arg
, "collect-pagefault-stats"))
5435 debug_options
.collect_pagefault_stats
= TRUE
;
5436 else if (!strcmp (arg
, "break-on-unverified"))
5437 debug_options
.break_on_unverified
= TRUE
;
5438 else if (!strcmp (arg
, "no-gdb-backtrace"))
5439 debug_options
.no_gdb_backtrace
= TRUE
;
5440 else if (!strcmp (arg
, "suspend-on-sigsegv"))
5441 debug_options
.suspend_on_sigsegv
= TRUE
;
5442 else if (!strcmp (arg
, "dont-free-domains"))
5443 mono_dont_free_domains
= TRUE
;
5444 else if (!strcmp (arg
, "dyn-runtime-invoke"))
5445 debug_options
.dyn_runtime_invoke
= TRUE
;
5446 else if (!strcmp (arg
, "gdb"))
5447 debug_options
.gdb
= TRUE
;
5448 else if (!strcmp (arg
, "explicit-null-checks"))
5449 debug_options
.explicit_null_checks
= TRUE
;
5450 else if (!strcmp (arg
, "gen-seq-points"))
5451 debug_options
.gen_seq_points
= TRUE
;
5452 else if (!strcmp (arg
, "init-stacks"))
5453 debug_options
.init_stacks
= TRUE
;
5455 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
5456 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");
5465 mini_get_debug_options (void)
5467 return &debug_options
;
5471 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
5476 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
5482 #elif defined(__ppc64__) || defined(__powerpc64__)
5485 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
5498 mini_get_addr_from_ftnptr (gpointer descr
)
5500 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
5501 return *(gpointer
*)descr
;
5507 static void runtime_invoke_info_free (gpointer value
);
5510 mini_create_jit_domain_info (MonoDomain
*domain
)
5512 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
5514 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5515 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5516 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5517 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5518 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5519 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
5520 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
5521 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5523 domain
->runtime_info
= info
;
5527 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
5529 g_slist_free (value
);
5533 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
5535 MonoJitDynamicMethodInfo
*di
= value
;
5536 mono_code_manager_destroy (di
->code_mp
);
5541 runtime_invoke_info_free (gpointer value
)
5543 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
5545 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5546 if (info
->dyn_call_info
)
5547 mono_arch_dyn_call_free (info
->dyn_call_info
);
5553 mini_free_jit_domain_info (MonoDomain
*domain
)
5555 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
5557 if (info
->jump_target_hash
) {
5558 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
5559 g_hash_table_destroy (info
->jump_target_hash
);
5561 if (info
->jump_target_got_slot_hash
) {
5562 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
5563 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
5565 if (info
->dynamic_code_hash
) {
5566 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
5567 g_hash_table_destroy (info
->dynamic_code_hash
);
5569 if (info
->method_code_hash
)
5570 g_hash_table_destroy (info
->method_code_hash
);
5571 g_hash_table_destroy (info
->class_init_trampoline_hash
);
5572 g_hash_table_destroy (info
->jump_trampoline_hash
);
5573 g_hash_table_destroy (info
->jit_trampoline_hash
);
5574 g_hash_table_destroy (info
->delegate_trampoline_hash
);
5575 if (info
->static_rgctx_trampoline_hash
)
5576 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
5577 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
5578 g_hash_table_destroy (info
->runtime_invoke_hash
);
5579 g_hash_table_destroy (info
->seq_points
);
5580 g_hash_table_destroy (info
->arch_seq_points
);
5582 if (info
->agent_info
)
5583 mono_debugger_agent_free_domain_info (domain
);
5585 g_free (domain
->runtime_info
);
5586 domain
->runtime_info
= NULL
;
5590 mini_init (const char *filename
, const char *runtime_version
)
5593 MonoRuntimeCallbacks callbacks
;
5595 MONO_PROBE_VES_INIT_BEGIN ();
5598 if (access ("/proc/self/maps", F_OK
) != 0) {
5599 g_print ("Mono requires /proc to be mounted.\n");
5604 /* Happens when using the embedding interface */
5605 if (!default_opt_set
)
5606 default_opt
= mono_parse_default_optimizations (NULL
);
5608 InitializeCriticalSection (&jit_mutex
);
5610 #ifdef MONO_DEBUGGER_SUPPORTED
5611 if (mini_debug_running_inside_mdb ())
5612 mini_debugger_init ();
5615 #ifdef MONO_ARCH_HAVE_TLS_GET
5616 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET
);
5618 mono_runtime_set_has_tls_get (FALSE
);
5621 if (!global_codeman
)
5622 global_codeman
= mono_code_manager_new ();
5623 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
5625 memset (&callbacks
, 0, sizeof (callbacks
));
5626 callbacks
.create_ftnptr
= mini_create_ftnptr
;
5627 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
5628 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
5630 #ifdef MONO_ARCH_HAVE_IMT
5632 if (!mono_use_llvm
) {
5633 /* LLVM needs a per-method vtable trampoline */
5634 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
5639 mono_install_callbacks (&callbacks
);
5641 mono_arch_cpu_init ();
5645 mono_unwind_init ();
5649 if (getenv ("MONO_DEBUG") != NULL
)
5650 mini_parse_debug_options ();
5652 if (getenv ("MONO_XDEBUG")) {
5653 char *xdebug_opts
= getenv ("MONO_XDEBUG");
5654 mono_xdebug_init (xdebug_opts
);
5655 /* So methods for multiple domains don't have the same address */
5656 mono_dont_free_domains
= TRUE
;
5657 mono_using_xdebug
= TRUE
;
5658 } else if (mini_get_debug_options ()->gdb
) {
5659 mono_xdebug_init ((char*)"gdb");
5660 mono_dont_free_domains
= TRUE
;
5661 mono_using_xdebug
= TRUE
;
5668 mono_trampolines_init ();
5670 if (!g_thread_supported ())
5671 g_thread_init (NULL
);
5673 mono_jit_tls_id
= TlsAlloc ();
5674 setup_jit_tls_data ((gpointer
)-1, mono_thread_abort
);
5676 if (default_opt
& MONO_OPT_AOT
)
5679 mono_debugger_agent_init ();
5681 #ifdef MONO_ARCH_GSHARED_SUPPORTED
5682 mono_set_generic_sharing_supported (TRUE
);
5685 #ifndef MONO_CROSS_COMPILE
5686 mono_runtime_install_handlers ();
5688 mono_threads_install_cleanup (mini_thread_cleanup
);
5690 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
5691 // This is experimental code so provide an env var to switch it off
5692 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
5693 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
5695 check_for_pending_exc
= FALSE
;
5696 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
5700 #define JIT_TRAMPOLINES_WORK
5701 #ifdef JIT_TRAMPOLINES_WORK
5702 mono_install_compile_method (mono_jit_compile_method
);
5703 mono_install_free_method (mono_jit_free_method
);
5704 #ifdef MONO_ARCH_LLVM_SUPPORTED
5706 /* The runtime currently only uses this for filling out vtables */
5707 mono_install_trampoline (mono_create_llvm_vcall_trampoline
);
5709 mono_install_trampoline (mono_create_jit_trampoline
);
5711 mono_install_trampoline (mono_create_jit_trampoline
);
5713 mono_install_jump_trampoline (mono_create_jump_trampoline
);
5714 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
5715 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
5716 mono_install_create_domain_hook (mini_create_jit_domain_info
);
5717 mono_install_free_domain_hook (mini_free_jit_domain_info
);
5719 #define JIT_INVOKE_WORKS
5720 #ifdef JIT_INVOKE_WORKS
5721 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
5723 mono_install_stack_walk (mono_jit_walk_stack
);
5724 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
5725 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
5726 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
5728 if (debug_options
.collect_pagefault_stats
) {
5729 mono_aot_set_make_unreadable (TRUE
);
5732 if (runtime_version
)
5733 domain
= mono_init_version (filename
, runtime_version
);
5735 domain
= mono_init_from_assembly (filename
, filename
);
5737 if (mono_aot_only
) {
5738 /* This helps catch code allocation requests */
5739 mono_code_manager_set_read_only (domain
->code_mp
);
5742 #ifdef MONO_ARCH_HAVE_IMT
5745 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
5747 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
5748 if (!mono_use_llvm
) {
5750 * The imt code in mono_magic_trampoline () can't handle LLVM code. By disabling
5751 * this, we force iface calls to go through the llvm vcall trampoline.
5753 mono_install_imt_trampoline (mini_get_imt_trampoline ());
5758 /* This must come after mono_init () in the aot-only case */
5759 mono_exceptions_init ();
5760 mono_install_handler (mono_get_throw_exception ());
5764 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
5765 ves_icall_get_frame_info
);
5766 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
5767 ves_icall_get_trace
);
5768 mono_add_internal_call ("System.Exception::get_trace",
5769 ves_icall_System_Exception_get_trace
);
5770 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
5771 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
5772 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
5773 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
5774 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
5775 mono_runtime_install_handlers
);
5778 create_helper_signature ();
5780 #define JIT_CALLS_WORK
5781 #ifdef JIT_CALLS_WORK
5782 /* Needs to be called here since register_jit_icall depends on it */
5783 mono_marshal_init ();
5785 mono_arch_register_lowlevel_calls ();
5786 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
5787 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
5788 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
5789 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
5790 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
5791 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "void", TRUE
);
5792 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
5794 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
5795 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
5796 #ifdef MONO_ARCH_HAVE_THROW_EXCEPTION_BY_NAME
5797 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE
);
5799 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
5800 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
5803 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
5804 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
5805 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
5806 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
5807 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
5810 * NOTE, NOTE, NOTE, NOTE:
5811 * when adding emulation for some opcodes, remember to also add a dummy
5812 * rule to the burg files, because we need the arity information to be correct.
5814 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
5815 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
5816 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
5817 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
5818 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
5819 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
5820 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
5821 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
5824 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
5825 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
5826 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
5827 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
5830 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
5831 mono_register_opcode_emulation (CEE_DIV
, "__emul_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5832 mono_register_opcode_emulation (CEE_DIV_UN
, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5833 mono_register_opcode_emulation (CEE_REM
, "__emul_irem", "int32 int32 int32", mono_irem
, FALSE
);
5834 mono_register_opcode_emulation (CEE_REM_UN
, "__emul_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5835 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5836 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5837 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
5838 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5841 #ifdef MONO_ARCH_EMULATE_MUL_DIV
5842 mono_register_opcode_emulation (CEE_MUL
, "__emul_imul", "int32 int32 int32", mono_imul
, TRUE
);
5843 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
5846 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
5847 mono_register_opcode_emulation (CEE_MUL_OVF
, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5848 mono_register_opcode_emulation (CEE_MUL_OVF_UN
, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5849 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5850 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5853 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
5854 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
5857 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
5858 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
5859 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
5860 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
5862 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
5863 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
5865 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
5866 mono_register_opcode_emulation (CEE_CONV_R_UN
, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5867 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5869 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
5870 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
5872 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
5873 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
5875 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
5876 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
5878 #ifdef MONO_ARCH_EMULATE_FREM
5879 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
5882 #ifdef MONO_ARCH_SOFT_FLOAT
5883 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
5884 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
5885 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
5886 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
5887 mono_register_opcode_emulation (CEE_CONV_R8
, "__emul_conv_r8", "double int32", mono_conv_to_r8
, FALSE
);
5888 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
5889 mono_register_opcode_emulation (CEE_CONV_R4
, "__emul_conv_r4", "double int32", mono_conv_to_r4
, FALSE
);
5890 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
5891 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
5892 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
5893 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
5894 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
5895 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
5896 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
5897 #if SIZEOF_VOID_P == 4
5898 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
5901 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
5902 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
5903 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
5904 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
5905 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
5906 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
5907 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
5908 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
5909 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
5910 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
5912 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
5913 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
5914 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
5915 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
5916 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
5918 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
5919 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
5920 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
5921 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
5924 #ifdef COMPRESSED_INTERFACE_BITMAP
5925 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
5928 #if SIZEOF_REGISTER == 4
5929 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
5932 /* other jit icalls */
5933 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
5934 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
5935 "ptr ptr ptr", FALSE
);
5936 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
5937 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
5938 "ptr ptr ptr ptr", FALSE
);
5939 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
5940 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
5941 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
5942 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
5943 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
5944 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
5945 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
5946 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
5947 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
5948 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
5949 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
5950 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
5951 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
5952 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
5953 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
5954 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
5955 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
5956 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
5957 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
5958 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
5959 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
5960 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
5961 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
5962 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
5963 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
5966 mono_generic_sharing_init ();
5968 #ifdef MONO_ARCH_SIMD_INTRINSICS
5969 mono_simd_intrinsics_init ();
5972 #if MONO_SUPPORT_TASKLETS
5973 mono_tasklets_init ();
5976 if (mono_compile_aot
)
5978 * Avoid running managed code when AOT compiling, since the platform
5979 * might only support aot-only execution.
5981 mono_runtime_set_no_exec (TRUE
);
5983 #define JIT_RUNTIME_WORKS
5984 #ifdef JIT_RUNTIME_WORKS
5985 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
5986 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
5987 mono_thread_attach (domain
);
5990 mono_profiler_runtime_initialized ();
5992 MONO_PROBE_VES_INIT_END ();
5997 MonoJitStats mono_jit_stats
= {0};
6000 print_jit_stats (void)
6002 if (mono_jit_stats
.enabled
) {
6003 g_print ("Mono Jit statistics\n");
6004 g_print ("Compiled methods: %ld\n", mono_jit_stats
.methods_compiled
);
6005 g_print ("Methods from AOT: %ld\n", mono_jit_stats
.methods_aot
);
6006 g_print ("Methods cache lookup: %ld\n", mono_jit_stats
.methods_lookups
);
6007 g_print ("Method trampolines: %ld\n", mono_jit_stats
.method_trampolines
);
6008 g_print ("Basic blocks: %ld\n", mono_jit_stats
.basic_blocks
);
6009 g_print ("Max basic blocks: %ld\n", mono_jit_stats
.max_basic_blocks
);
6010 g_print ("Allocated vars: %ld\n", mono_jit_stats
.allocate_var
);
6011 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats
.cil_code_size
);
6012 g_print ("Native code size: %ld\n", mono_jit_stats
.native_code_size
);
6013 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
6014 mono_jit_stats
.max_ratio_method
);
6015 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
6016 mono_jit_stats
.biggest_method
);
6017 g_print ("Code reallocs: %ld\n", mono_jit_stats
.code_reallocs
);
6018 g_print ("Allocated code size: %ld\n", mono_jit_stats
.allocated_code_size
);
6019 g_print ("Inlineable methods: %ld\n", mono_jit_stats
.inlineable_methods
);
6020 g_print ("Inlined methods: %ld\n", mono_jit_stats
.inlined_methods
);
6021 g_print ("Regvars: %ld\n", mono_jit_stats
.regvars
);
6022 g_print ("Locals stack size: %ld\n", mono_jit_stats
.locals_stack_size
);
6024 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
6025 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
6026 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
6027 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
6028 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
6029 g_print ("Methods: %ld\n", mono_stats
.method_count
);
6030 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
6031 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
6032 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
6034 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
6035 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
6036 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
6038 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
6039 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
6040 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
6042 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
6043 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
6044 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
6046 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
6047 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
6048 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
6049 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
6050 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
6051 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
6052 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
6053 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
6055 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
6056 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
6057 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
6059 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
6061 g_print ("Minor GC collections: %ld\n", mono_stats
.minor_gc_count
);
6063 g_print ("Major GC collections: %ld\n", mono_stats
.major_gc_count
);
6065 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats
.minor_gc_time_usecs
/ 1000.0);
6067 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats
.major_gc_time_usecs
/ 1000.0);
6068 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
6069 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
6070 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
6071 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
6072 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
6073 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
6074 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
6077 g_free (mono_jit_stats
.max_ratio_method
);
6078 mono_jit_stats
.max_ratio_method
= NULL
;
6079 g_free (mono_jit_stats
.biggest_method
);
6080 mono_jit_stats
.biggest_method
= NULL
;
6085 mini_cleanup (MonoDomain
*domain
)
6087 mono_runtime_shutdown_stat_profiler ();
6090 cominterop_release_all_rcws ();
6093 #ifndef MONO_CROSS_COMPILE
6095 * mono_runtime_cleanup() and mono_domain_finalize () need to
6096 * be called early since they need the execution engine still
6097 * fully working (mono_domain_finalize may invoke managed finalizers
6098 * and mono_runtime_cleanup will wait for other threads to finish).
6100 mono_domain_finalize (domain
, 2000);
6103 /* This accesses metadata so needs to be called before runtime shutdown */
6106 mono_profiler_shutdown ();
6108 #ifndef MONO_CROSS_COMPILE
6109 mono_runtime_cleanup (domain
);
6112 mono_icall_cleanup ();
6114 mono_runtime_cleanup_handlers ();
6116 mono_domain_free (domain
, TRUE
);
6118 mono_debugger_cleanup ();
6121 mono_llvm_cleanup ();
6124 mono_trampolines_cleanup ();
6126 mono_unwind_cleanup ();
6128 if (!mono_dont_free_global_codeman
)
6129 mono_code_manager_destroy (global_codeman
);
6130 g_hash_table_destroy (jit_icall_name_hash
);
6131 g_free (emul_opcode_map
);
6132 g_free (emul_opcode_opcodes
);
6134 mono_arch_cleanup ();
6138 mono_trace_cleanup ();
6140 mono_counters_dump (-1, stdout
);
6142 if (mono_inject_async_exc_method
)
6143 mono_method_desc_free (mono_inject_async_exc_method
);
6145 TlsFree(mono_jit_tls_id
);
6147 DeleteCriticalSection (&jit_mutex
);
6149 DeleteCriticalSection (&mono_delegate_section
);
6153 mono_set_defaults (int verbose_level
, guint32 opts
)
6155 mini_verbose
= verbose_level
;
6157 default_opt_set
= TRUE
;
6161 mono_disable_optimizations (guint32 opts
)
6163 default_opt
&= ~opts
;
6167 * mono_get_runtime_build_info:
6169 * Return the runtime version + build date in string format.
6170 * The returned string is owned by the caller.
6173 mono_get_runtime_build_info (void)
6175 if (mono_build_date
)
6176 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
6178 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
6182 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
6184 GHashTable
*assemblies
= (GHashTable
*)user_data
;
6185 MonoImage
*image
= mono_assembly_get_image (ass
);
6186 MonoMethod
*method
, *invoke
;
6189 if (g_hash_table_lookup (assemblies
, ass
))
6192 g_hash_table_insert (assemblies
, ass
, ass
);
6194 if (mini_verbose
> 0)
6195 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
6197 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
6198 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
6199 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
6203 if (mini_verbose
> 1) {
6204 char * desc
= mono_method_full_name (method
, TRUE
);
6205 g_print ("Compiling %d %s\n", count
, desc
);
6208 mono_compile_method (method
);
6209 if (strcmp (method
->name
, "Finalize") == 0) {
6210 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
6211 mono_compile_method (invoke
);
6213 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
6214 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
6215 mono_compile_method (invoke
);
6219 /* Load and precompile referenced assemblies as well */
6220 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
6221 mono_assembly_load_reference (image
, i
);
6222 if (image
->references
[i
])
6223 mono_precompile_assembly (image
->references
[i
], assemblies
);
6227 void mono_precompile_assemblies ()
6229 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
6231 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
6233 g_hash_table_destroy (assemblies
);
6239 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
6240 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);
6244 mono_cfg_add_try_hole (MonoCompile
*cfg
, MonoExceptionClause
*clause
, guint8
*start
, MonoBasicBlock
*bb
)
6246 TryBlockHole
*hole
= mono_mempool_alloc (cfg
->mempool
, sizeof (TryBlockHole
));
6247 hole
->clause
= clause
;
6248 hole
->start_offset
= start
- cfg
->native_code
;
6249 hole
->basic_block
= bb
;
6251 cfg
->try_block_holes
= g_slist_append_mempool (cfg
->mempool
, cfg
->try_block_holes
, hole
);