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
2475 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2476 ins
->dreg
= mono_alloc_preg (cfg
);
2477 ins
->inst_offset
= offset
;
2485 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2487 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2491 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2493 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2497 mono_get_thread_intrinsic (MonoCompile
* cfg
)
2499 return mono_create_tls_get (cfg
, mono_thread_get_tls_offset ());
2503 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2505 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2509 ji
->data
.target
= target
;
2510 ji
->next
= cfg
->patch_info
;
2512 cfg
->patch_info
= ji
;
2516 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2518 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2522 ji
->data
.target
= target
;
2529 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2531 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2534 if ((*ji
)->ip
.i
== ip
)
2537 ji
= &((*ji
)->next
);
2542 * mono_patch_info_dup_mp:
2544 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2547 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2549 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2550 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2552 switch (patch_info
->type
) {
2553 case MONO_PATCH_INFO_RVA
:
2554 case MONO_PATCH_INFO_LDSTR
:
2555 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2556 case MONO_PATCH_INFO_LDTOKEN
:
2557 case MONO_PATCH_INFO_DECLSEC
:
2558 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2559 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2561 case MONO_PATCH_INFO_SWITCH
:
2562 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2563 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2564 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2565 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2567 case MONO_PATCH_INFO_RGCTX_FETCH
:
2568 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2569 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2570 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2580 mono_patch_info_hash (gconstpointer data
)
2582 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2585 case MONO_PATCH_INFO_RVA
:
2586 case MONO_PATCH_INFO_LDSTR
:
2587 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2588 case MONO_PATCH_INFO_LDTOKEN
:
2589 case MONO_PATCH_INFO_DECLSEC
:
2590 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2591 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2592 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2593 case MONO_PATCH_INFO_VTABLE
:
2594 case MONO_PATCH_INFO_CLASS
:
2595 case MONO_PATCH_INFO_IID
:
2596 case MONO_PATCH_INFO_ADJUSTED_IID
:
2597 case MONO_PATCH_INFO_CLASS_INIT
:
2598 case MONO_PATCH_INFO_METHODCONST
:
2599 case MONO_PATCH_INFO_METHOD
:
2600 case MONO_PATCH_INFO_METHOD_JUMP
:
2601 case MONO_PATCH_INFO_IMAGE
:
2602 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2603 case MONO_PATCH_INFO_FIELD
:
2604 case MONO_PATCH_INFO_SFLDA
:
2605 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2606 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2608 return (ji
->type
<< 8);
2613 * mono_patch_info_equal:
2615 * This might fail to recognize equivalent patches, i.e. floats, so its only
2616 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2620 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2622 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2623 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2625 if (ji1
->type
!= ji2
->type
)
2628 switch (ji1
->type
) {
2629 case MONO_PATCH_INFO_RVA
:
2630 case MONO_PATCH_INFO_LDSTR
:
2631 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2632 case MONO_PATCH_INFO_LDTOKEN
:
2633 case MONO_PATCH_INFO_DECLSEC
:
2634 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2635 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2636 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2637 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2638 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2641 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2642 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2644 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2645 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
2646 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
2648 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
);
2651 if (ji1
->data
.target
!= ji2
->data
.target
)
2660 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
2662 unsigned char *ip
= patch_info
->ip
.i
+ code
;
2663 gconstpointer target
= NULL
;
2665 switch (patch_info
->type
) {
2666 case MONO_PATCH_INFO_BB
:
2668 * FIXME: This could be hit for methods without a prolog. Should use -1
2669 * but too much code depends on a 0 initial value.
2671 //g_assert (patch_info->data.bb->native_offset);
2672 target
= patch_info
->data
.bb
->native_offset
+ code
;
2674 case MONO_PATCH_INFO_ABS
:
2675 target
= patch_info
->data
.target
;
2677 case MONO_PATCH_INFO_LABEL
:
2678 target
= patch_info
->data
.inst
->inst_c0
+ code
;
2680 case MONO_PATCH_INFO_IP
:
2683 case MONO_PATCH_INFO_METHOD_REL
:
2684 target
= code
+ patch_info
->data
.offset
;
2686 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
2687 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2689 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
2690 g_assert_not_reached ();
2692 target
= mono_icall_get_wrapper (mi
);
2695 case MONO_PATCH_INFO_METHOD_JUMP
:
2696 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
2698 case MONO_PATCH_INFO_METHOD
:
2699 if (patch_info
->data
.method
== method
) {
2702 /* get the trampoline to the method from the domain */
2703 target
= mono_create_jit_trampoline (patch_info
->data
.method
);
2706 case MONO_PATCH_INFO_SWITCH
: {
2707 gpointer
*jump_table
;
2710 if (method
&& method
->dynamic
) {
2711 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2713 if (mono_aot_only
) {
2714 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2716 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2720 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++)
2721 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
2722 target
= jump_table
;
2725 case MONO_PATCH_INFO_METHODCONST
:
2726 case MONO_PATCH_INFO_CLASS
:
2727 case MONO_PATCH_INFO_IMAGE
:
2728 case MONO_PATCH_INFO_FIELD
:
2729 target
= patch_info
->data
.target
;
2731 case MONO_PATCH_INFO_IID
:
2732 mono_class_init (patch_info
->data
.klass
);
2733 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
2735 case MONO_PATCH_INFO_ADJUSTED_IID
:
2736 mono_class_init (patch_info
->data
.klass
);
2737 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
2739 case MONO_PATCH_INFO_VTABLE
:
2740 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2743 case MONO_PATCH_INFO_CLASS_INIT
: {
2744 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2747 target
= mono_create_class_init_trampoline (vtable
);
2750 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
2751 target
= mono_create_delegate_trampoline (patch_info
->data
.klass
);
2753 case MONO_PATCH_INFO_SFLDA
: {
2754 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
2756 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
2757 gpointer addr
= NULL
;
2759 mono_domain_lock (domain
);
2760 if (domain
->special_static_fields
)
2761 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
2762 mono_domain_unlock (domain
);
2768 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
2769 /* Done by the generated code */
2773 mono_runtime_class_init (vtable
);
2775 target
= (char*)vtable
->data
+ patch_info
->data
.field
->offset
;
2778 case MONO_PATCH_INFO_RVA
: {
2779 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
2782 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
2783 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
2786 case MONO_PATCH_INFO_R4
:
2787 case MONO_PATCH_INFO_R8
:
2788 target
= patch_info
->data
.target
;
2790 case MONO_PATCH_INFO_EXC_NAME
:
2791 target
= patch_info
->data
.name
;
2793 case MONO_PATCH_INFO_LDSTR
:
2795 mono_ldstr (domain
, patch_info
->data
.token
->image
,
2796 mono_metadata_token_index (patch_info
->data
.token
->token
));
2798 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
2800 MonoClass
*handle_class
;
2802 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2803 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
2804 mono_class_init (handle_class
);
2805 mono_class_init (mono_class_from_mono_type (handle
));
2808 mono_type_get_object (domain
, handle
);
2811 case MONO_PATCH_INFO_LDTOKEN
: {
2813 MonoClass
*handle_class
;
2815 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2816 patch_info
->data
.token
->token
, &handle_class
, NULL
);
2817 mono_class_init (handle_class
);
2822 case MONO_PATCH_INFO_DECLSEC
:
2823 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
2825 case MONO_PATCH_INFO_ICALL_ADDR
:
2826 /* run_cctors == 0 -> AOT */
2827 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2829 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, NULL
, NULL
);
2831 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
));
2836 target
= mono_lookup_internal_call (patch_info
->data
.method
);
2838 if (!target
&& run_cctors
)
2839 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2842 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
2843 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2845 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
2846 g_assert_not_reached ();
2851 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
2852 target
= mono_thread_interruption_request_flag ();
2854 case MONO_PATCH_INFO_METHOD_RGCTX
: {
2855 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
2858 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
2861 case MONO_PATCH_INFO_BB_OVF
:
2862 case MONO_PATCH_INFO_EXC_OVF
:
2863 case MONO_PATCH_INFO_GOT_OFFSET
:
2864 case MONO_PATCH_INFO_NONE
:
2866 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2867 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
2870 switch (entry
->data
->type
) {
2871 case MONO_PATCH_INFO_CLASS
:
2872 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
));
2874 case MONO_PATCH_INFO_METHOD
:
2875 case MONO_PATCH_INFO_METHODCONST
:
2876 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
));
2878 case MONO_PATCH_INFO_FIELD
:
2879 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
));
2882 g_assert_not_reached ();
2886 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
2889 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
2890 target
= mono_create_generic_class_init_trampoline ();
2892 case MONO_PATCH_INFO_MONITOR_ENTER
:
2893 target
= mono_create_monitor_enter_trampoline ();
2895 case MONO_PATCH_INFO_MONITOR_EXIT
:
2896 target
= mono_create_monitor_exit_trampoline ();
2898 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2899 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2901 /* AOT, not needed */
2904 target
= mono_arch_get_seq_point_info (domain
, code
);
2907 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
2908 #ifdef MONO_ARCH_LLVM_SUPPORTED
2909 g_assert (mono_use_llvm
);
2910 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
2912 g_assert_not_reached ();
2916 g_assert_not_reached ();
2919 return (gpointer
)target
;
2923 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
2925 ins
->inst_offset
= native_offset
;
2926 g_ptr_array_add (cfg
->seq_points
, ins
);
2927 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
2928 bb
->last_seq_point
= ins
;
2934 mono_compile_create_vars (MonoCompile
*cfg
)
2936 MonoMethodSignature
*sig
;
2937 MonoMethodHeader
*header
;
2940 header
= cfg
->header
;
2942 sig
= mono_method_signature (cfg
->method
);
2944 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
2945 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
2946 /* Inhibit optimizations */
2947 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
2949 if (cfg
->verbose_level
> 2)
2950 g_print ("creating vars\n");
2952 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
2955 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
2957 for (i
= 0; i
< sig
->param_count
; ++i
) {
2958 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
2961 if (cfg
->verbose_level
> 2) {
2963 printf ("\treturn : ");
2964 mono_print_ins (cfg
->ret
);
2968 printf ("\tthis: ");
2969 mono_print_ins (cfg
->args
[0]);
2972 for (i
= 0; i
< sig
->param_count
; ++i
) {
2973 printf ("\targ [%d]: ", i
);
2974 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
2978 cfg
->locals_start
= cfg
->num_varinfo
;
2979 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
2981 if (cfg
->verbose_level
> 2)
2982 g_print ("creating locals\n");
2984 for (i
= 0; i
< header
->num_locals
; ++i
)
2985 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
2987 if (cfg
->verbose_level
> 2)
2988 g_print ("locals done\n");
2990 mono_arch_create_vars (cfg
);
2993 #endif /* #ifndef DISABLE_JIT */
2996 mono_print_code (MonoCompile
*cfg
, const char* msg
)
3000 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3001 mono_print_bb (bb
, msg
);
3007 mono_postprocess_patches (MonoCompile
*cfg
)
3009 MonoJumpInfo
*patch_info
;
3012 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3013 switch (patch_info
->type
) {
3014 case MONO_PATCH_INFO_ABS
: {
3015 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
3018 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
3022 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
3023 // FIXME: CLEAN UP THIS MESS.
3024 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
3025 strstr (cfg
->method
->name
, info
->name
)) {
3027 * This is an icall wrapper, and this is a call to the
3030 if (cfg
->compile_aot
) {
3031 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
3032 patch_info
->data
.name
= info
->name
;
3035 /* for these array methods we currently register the same function pointer
3036 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
3037 * will return the incorrect one depending on the order they are registered.
3038 * See tests/test-arr.cs
3040 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
3041 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3042 patch_info
->data
.name
= info
->name
;
3047 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
3048 if (cfg
->abs_patches
) {
3049 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
3051 patch_info
->type
= abs_ji
->type
;
3052 patch_info
->data
.target
= abs_ji
->data
.target
;
3059 case MONO_PATCH_INFO_SWITCH
: {
3061 if (cfg
->method
->dynamic
) {
3062 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3064 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3067 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3068 /* Might be NULL if the switch is eliminated */
3069 if (patch_info
->data
.table
->table
[i
]) {
3070 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
3071 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
3076 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
3079 case MONO_PATCH_INFO_METHOD_JUMP
: {
3081 MonoDomain
*domain
= cfg
->domain
;
3082 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
3084 mono_domain_lock (domain
);
3085 if (!domain_jit_info (domain
)->jump_target_hash
)
3086 domain_jit_info (domain
)->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3087 list
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3088 list
= g_slist_prepend (list
, ip
);
3089 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, list
);
3090 mono_domain_unlock (domain
);
3101 collect_pred_seq_points (MonoBasicBlock
*bb
, MonoInst
*ins
, GSList
**next
, int depth
)
3104 MonoBasicBlock
*in_bb
;
3106 for (i
= 0; i
< bb
->in_count
; ++i
) {
3107 in_bb
= bb
->in_bb
[i
];
3109 if (in_bb
->last_seq_point
) {
3110 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
));
3112 /* Have to look at its predecessors */
3114 collect_pred_seq_points (in_bb
, ins
, next
, depth
+ 1);
3120 mono_save_seq_point_info (MonoCompile
*cfg
)
3123 GSList
*bb_seq_points
, *l
;
3125 MonoDomain
*domain
= cfg
->domain
;
3127 MonoSeqPointInfo
*info
;
3130 if (!cfg
->seq_points
)
3133 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3134 info
->len
= cfg
->seq_points
->len
;
3135 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3136 SeqPoint
*sp
= &info
->seq_points
[i
];
3137 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3139 sp
->il_offset
= ins
->inst_imm
;
3140 sp
->native_offset
= ins
->inst_offset
;
3143 ins
->backend
.size
= i
;
3147 * For each sequence point, compute the list of sequence points immediately
3148 * following it, this is needed to implement 'step over' in the debugger agent.
3150 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3151 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3152 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3154 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3155 MonoInst
*ins
= l
->data
;
3157 if (!(ins
->flags
& MONO_INST_SINGLE_STEP_LOC
))
3161 /* Link with the previous seq point in the same bb */
3162 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3164 /* Link with the last bb in the previous bblocks */
3165 collect_pred_seq_points (bb
, ins
, next
, 0);
3172 if (cfg
->verbose_level
> 2) {
3173 printf ("\nSEQ POINT MAP: \n");
3176 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3177 SeqPoint
*sp
= &info
->seq_points
[i
];
3181 sp
->next_len
= g_slist_length (next
[i
]);
3182 sp
->next
= g_new (int, sp
->next_len
);
3184 if (cfg
->verbose_level
> 2 && next
[i
]) {
3185 printf ("\t0x%x ->", sp
->il_offset
);
3186 for (l
= next
[i
]; l
; l
= l
->next
) {
3187 next_index
= GPOINTER_TO_UINT (l
->data
);
3188 printf (" 0x%x", info
->seq_points
[next_index
].il_offset
);
3192 for (l
= next
[i
]; l
; l
= l
->next
) {
3193 next_index
= GPOINTER_TO_UINT (l
->data
);
3194 sp
->next
[j
++] = next_index
;
3196 g_slist_free (next
[i
]);
3200 cfg
->seq_point_info
= info
;
3202 // FIXME: dynamic methods
3203 mono_domain_lock (domain
);
3204 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3205 mono_domain_unlock (domain
);
3207 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3208 cfg
->seq_points
= NULL
;
3212 mono_codegen (MonoCompile
*cfg
)
3215 int max_epilog_size
;
3218 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3219 cfg
->spill_count
= 0;
3220 /* we reuse dfn here */
3221 /* bb->dfn = bb_count++; */
3223 mono_arch_lowering_pass (cfg
, bb
);
3225 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3226 mono_arch_peephole_pass_1 (cfg
, bb
);
3229 mono_local_regalloc (cfg
, bb
);
3231 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3232 mono_arch_peephole_pass_2 (cfg
, bb
);
3235 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3236 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3238 code
= mono_arch_emit_prolog (cfg
);
3240 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3241 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3243 cfg
->code_len
= code
- cfg
->native_code
;
3244 cfg
->prolog_end
= cfg
->code_len
;
3246 mono_debug_open_method (cfg
);
3248 /* emit code all basic blocks */
3249 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3250 bb
->native_offset
= cfg
->code_len
;
3251 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3252 mono_arch_output_basic_block (cfg
, bb
);
3253 bb
->native_length
= cfg
->code_len
- bb
->native_offset
;
3255 if (bb
== cfg
->bb_exit
) {
3256 cfg
->epilog_begin
= cfg
->code_len
;
3258 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3259 code
= cfg
->native_code
+ cfg
->code_len
;
3260 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3261 cfg
->code_len
= code
- cfg
->native_code
;
3262 g_assert (cfg
->code_len
< cfg
->code_size
);
3265 mono_arch_emit_epilog (cfg
);
3269 mono_arch_emit_exceptions (cfg
);
3271 max_epilog_size
= 0;
3273 code
= cfg
->native_code
+ cfg
->code_len
;
3275 /* we always allocate code in cfg->domain->code_mp to increase locality */
3276 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3277 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3279 if (cfg
->method
->dynamic
) {
3280 guint unwindlen
= 0;
3281 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3282 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3284 /* Allocate the code into a separate memory pool so it can be freed */
3285 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3286 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3287 mono_domain_lock (cfg
->domain
);
3288 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3289 mono_domain_unlock (cfg
->domain
);
3291 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3293 guint unwindlen
= 0;
3294 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3295 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3297 code
= mono_domain_code_reserve (cfg
->domain
, cfg
->code_size
+ unwindlen
);
3300 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3301 g_free (cfg
->native_code
);
3302 cfg
->native_code
= code
;
3303 code
= cfg
->native_code
+ cfg
->code_len
;
3305 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3306 mono_postprocess_patches (cfg
);
3308 #ifdef VALGRIND_JIT_REGISTER_MAP
3309 if (valgrind_register
){
3310 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3311 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3316 if (cfg
->verbose_level
> 0) {
3317 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3318 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3320 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3325 gboolean is_generic
= FALSE
;
3327 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3328 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3332 if (cfg
->generic_sharing_context
)
3333 g_assert (is_generic
);
3336 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3337 mono_arch_save_unwind_info (cfg
);
3340 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->run_cctors
);
3342 if (cfg
->method
->dynamic
) {
3343 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3345 mono_domain_code_commit (cfg
->domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3347 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3349 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3351 mono_debug_close_method (cfg
);
3353 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3354 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3359 compute_reachable (MonoBasicBlock
*bb
)
3363 if (!(bb
->flags
& BB_VISITED
)) {
3364 bb
->flags
|= BB_VISITED
;
3365 for (i
= 0; i
< bb
->out_count
; ++i
)
3366 compute_reachable (bb
->out_bb
[i
]);
3371 create_jit_info (MonoCompile
*cfg
, MonoMethod
*method_to_compile
)
3374 MonoMethodHeader
*header
;
3377 int generic_info_size
;
3378 int holes_size
= 0, num_holes
= 0;
3380 g_assert (method_to_compile
== cfg
->method
);
3381 header
= cfg
->header
;
3383 if (cfg
->generic_sharing_context
)
3384 generic_info_size
= sizeof (MonoGenericJitInfo
);
3386 generic_info_size
= 0;
3388 if (cfg
->try_block_holes
) {
3389 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3390 TryBlockHole
*hole
= tmp
->data
;
3391 MonoExceptionClause
*ec
= hole
->clause
;
3392 int hole_end
= hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
;
3393 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3394 g_assert (clause_last_bb
);
3396 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3397 if (clause_last_bb
->native_offset
!= hole_end
)
3401 holes_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3402 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3403 printf ("Number of try block holes %d\n", num_holes
);
3406 if (COMPILE_LLVM (cfg
))
3407 num_clauses
= cfg
->llvm_ex_info_len
;
3409 num_clauses
= header
->num_clauses
;
3411 if (cfg
->method
->dynamic
) {
3412 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3413 generic_info_size
+ holes_size
);
3415 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
3416 (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3417 generic_info_size
+ holes_size
);
3420 jinfo
->method
= cfg
->method_to_register
;
3421 jinfo
->code_start
= cfg
->native_code
;
3422 jinfo
->code_size
= cfg
->code_len
;
3423 jinfo
->used_regs
= cfg
->used_int_regs
;
3424 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
3425 jinfo
->cas_inited
= FALSE
; /* initialization delayed at the first stalk walk using this method */
3426 jinfo
->num_clauses
= num_clauses
;
3427 if (COMPILE_LLVM (cfg
))
3428 jinfo
->from_llvm
= TRUE
;
3430 if (cfg
->generic_sharing_context
) {
3432 MonoGenericJitInfo
*gi
;
3434 jinfo
->has_generic_jit_info
= 1;
3436 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3439 gi
->generic_sharing_context
= cfg
->generic_sharing_context
;
3441 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3442 mini_method_get_context (method_to_compile
)->method_inst
||
3443 method_to_compile
->klass
->valuetype
) {
3444 g_assert (cfg
->rgctx_var
);
3449 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3450 mini_method_get_context (method_to_compile
)->method_inst
||
3451 method_to_compile
->klass
->valuetype
) {
3452 inst
= cfg
->rgctx_var
;
3453 g_assert (inst
->opcode
== OP_REGOFFSET
);
3455 inst
= cfg
->args
[0];
3458 if (inst
->opcode
== OP_REGVAR
) {
3459 gi
->this_in_reg
= 1;
3460 gi
->this_reg
= inst
->dreg
;
3462 g_assert (inst
->opcode
== OP_REGOFFSET
);
3464 g_assert (inst
->inst_basereg
== X86_EBP
);
3465 #elif defined(TARGET_AMD64)
3466 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
3468 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
3470 gi
->this_in_reg
= 0;
3471 gi
->this_reg
= inst
->inst_basereg
;
3472 gi
->this_offset
= inst
->inst_offset
;
3477 MonoTryBlockHoleTableJitInfo
*table
;
3480 jinfo
->has_try_block_holes
= 1;
3481 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3482 table
->num_holes
= (guint16
)num_holes
;
3484 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3485 MonoTryBlockHoleJitInfo
*hole
;
3486 TryBlockHole
*hole_data
= tmp
->data
;
3487 MonoExceptionClause
*ec
= hole_data
->clause
;
3488 int hole_end
= hole_data
->basic_block
->native_offset
+ hole_data
->basic_block
->native_length
;
3489 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3490 g_assert (clause_last_bb
);
3492 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3493 if (clause_last_bb
->native_offset
== hole_end
)
3496 hole
= &table
->holes
[i
++];
3497 hole
->clause
= hole_data
->clause
- &header
->clauses
[0];
3498 hole
->offset
= (guint32
)hole_data
->start_offset
;
3499 hole
->length
= (guint16
)(hole_data
->basic_block
->native_length
- hole_data
->start_offset
);
3501 g_assert (i
== num_holes
);
3504 if (G_UNLIKELY (header
->num_clauses
&& cfg
->verbose_level
>= 4)) {
3507 printf ("\nException Handling Data\n");
3508 for (i
= 0; i
< header
->num_clauses
; i
++) {
3509 MonoExceptionClause
*ec
= &header
->clauses
[i
];
3510 int try_start
= cfg
->cil_offset_to_bb
[ec
->try_offset
]->native_offset
;
3511 int try_end
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
]->native_offset
;
3512 int handler_start
= cfg
->cil_offset_to_bb
[ec
->handler_offset
]->native_offset
;
3514 if (ec
->handler_offset
+ ec
->handler_len
< header
->code_size
)
3515 handler_end
= cfg
->cil_offset_to_bb
[ec
->handler_offset
+ ec
->handler_len
]->native_offset
;
3517 handler_end
= cfg
->epilog_begin
;
3519 printf ("EH clause %d flags %x try IL %x-%x NATIVE %x-%x handler IL %x-%x NATIVE %x-%x\n",
3521 ec
->try_offset
, ec
->try_offset
+ ec
->try_len
, try_start
, try_end
,
3522 ec
->handler_offset
, ec
->handler_offset
+ ec
->handler_len
, handler_start
, handler_end
);
3524 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3525 TryBlockHole
*hole
= tmp
->data
;
3526 int block
= hole
->clause
- &header
->clauses
[0];
3527 printf ("try block hole at eh clause %d range %x-%x\n",
3528 block
, hole
->start_offset
, hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
3532 if (COMPILE_LLVM (cfg
)) {
3534 memcpy (&jinfo
->clauses
[0], &cfg
->llvm_ex_info
[0], num_clauses
* sizeof (MonoJitExceptionInfo
));
3535 } else if (header
->num_clauses
) {
3538 for (i
= 0; i
< header
->num_clauses
; i
++) {
3539 MonoExceptionClause
*ec
= &header
->clauses
[i
];
3540 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3541 MonoBasicBlock
*tblock
;
3544 ei
->flags
= ec
->flags
;
3546 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
3547 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
3549 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
3550 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
3552 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
3554 ei
->data
.catch_class
= ec
->data
.catch_class
;
3557 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
3559 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
3560 g_assert (tblock
->native_offset
);
3561 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3563 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
3564 g_assert (tblock
->native_offset
);
3565 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
3567 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
3569 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3570 TryBlockHole
*hole
= tmp
->data
;
3571 gpointer hole_end
= cfg
->native_code
+ (hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
3572 if (hole
->clause
== ec
&& hole_end
== ei
->try_end
) {
3573 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3574 printf ("\tShortening try block %d from %p to %p\n", i
, ei
->try_end
, cfg
->native_code
+ hole
->start_offset
);
3575 ei
->try_end
= cfg
->native_code
+ hole
->start_offset
;
3581 if (G_UNLIKELY (cfg
->verbose_level
>= 4)) {
3582 for (i
= 0; i
< jinfo
->num_clauses
; i
++) {
3583 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3584 printf ("JitInfo EH clause %d flags %x try %p-%p handler %p\n", i
, ei
->flags
, ei
->try_start
, ei
->try_end
, ei
->handler_start
);
3591 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
3592 * using it during runtime, hence the define.
3594 #ifdef MONO_ARCH_HAVE_XP_UNWIND
3595 if (cfg
->encoded_unwind_ops
) {
3596 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
3597 g_free (cfg
->encoded_unwind_ops
);
3598 } else if (cfg
->unwind_ops
) {
3600 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
3602 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
3603 g_free (unwind_info
);
3611 * mini_get_shared_method:
3613 * Return the method which is actually compiled/registered when doing generic sharing.
3616 mini_get_shared_method (MonoMethod
*method
)
3618 MonoGenericContext shared_context
;
3619 MonoMethod
*declaring_method
, *res
;
3621 gboolean partial
= FALSE
;
3623 if (method
->is_generic
|| method
->klass
->generic_container
)
3624 declaring_method
= method
;
3626 declaring_method
= mono_method_get_declaring_generic_method (method
);
3628 if (declaring_method
->is_generic
)
3629 shared_context
= mono_method_get_generic_container (declaring_method
)->context
;
3631 shared_context
= declaring_method
->klass
->generic_container
->context
;
3633 /* Handle partial sharing */
3634 if (method
!= declaring_method
&& method
->is_inflated
&& !mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
)) {
3635 MonoGenericContext
*context
= mono_method_get_context (method
);
3636 MonoGenericInst
*inst
;
3637 MonoType
**type_argv
;
3640 * Create the shared context by replacing the ref type arguments with
3641 * type parameters, and keeping the rest.
3644 inst
= context
->class_inst
;
3646 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3647 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3648 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
)
3649 type_argv
[i
] = shared_context
.class_inst
->type_argv
[i
];
3651 type_argv
[i
] = inst
->type_argv
[i
];
3654 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3658 inst
= context
->method_inst
;
3660 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3661 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3662 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
)
3663 type_argv
[i
] = shared_context
.method_inst
->type_argv
[i
];
3665 type_argv
[i
] = inst
->type_argv
[i
];
3668 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3673 res
= mono_class_inflate_generic_method (declaring_method
, &shared_context
);
3675 /* The result should be an inflated method whose parent is not inflated */
3676 g_assert (!res
->klass
->is_inflated
);
3682 * mini_method_compile:
3683 * @method: the method to compile
3684 * @opts: the optimization flags to use
3685 * @domain: the domain where the method will be compiled in
3686 * @run_cctors: whether we should run type ctors if possible
3687 * @compile_aot: whether this is an AOT compilation
3688 * @parts: debug flag
3690 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3691 * field in the returned struct to see if compilation succeded.
3694 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
3696 MonoMethodHeader
*header
;
3699 int dfn
, i
, code_size_ratio
;
3700 gboolean deadce_has_run
= FALSE
;
3701 gboolean try_generic_shared
, try_llvm
;
3702 MonoMethod
*method_to_compile
, *method_to_register
;
3704 mono_jit_stats
.methods_compiled
++;
3705 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
3706 mono_profiler_method_jit (method
);
3707 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
3708 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
3712 * We might get passed the original generic method definition or
3713 * instances with type parameters.
3714 * FIXME: Remove the method->klass->generic_class limitation.
3716 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3717 (opts
& MONO_OPT_GSHARED
) && ((method
->is_generic
|| method
->klass
->generic_container
) || (!method
->klass
->generic_class
&& mono_method_is_generic_sharable_impl (method
, TRUE
)));
3719 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3720 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl (method
, FALSE
);
3722 if (opts
& MONO_OPT_GSHARED
) {
3723 if (try_generic_shared
)
3724 mono_stats
.generics_sharable_methods
++;
3725 else if (mono_method_is_generic_impl (method
))
3726 mono_stats
.generics_unsharable_methods
++;
3736 if (try_generic_shared
) {
3737 method_to_compile
= mini_get_shared_method (method
);
3738 g_assert (method_to_compile
);
3740 method_to_compile
= method
;
3743 cfg
= g_new0 (MonoCompile
, 1);
3744 cfg
->method
= method_to_compile
;
3745 cfg
->header
= mono_method_get_header (cfg
->method
);
3746 cfg
->mempool
= mono_mempool_new ();
3748 cfg
->prof_options
= mono_profiler_get_events ();
3749 cfg
->run_cctors
= run_cctors
;
3750 cfg
->domain
= domain
;
3751 cfg
->verbose_level
= mini_verbose
;
3752 cfg
->compile_aot
= compile_aot
;
3753 cfg
->skip_visibility
= method
->skip_visibility
;
3754 cfg
->orig_method
= method
;
3755 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
3756 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
3757 if (try_generic_shared
)
3758 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->generic_sharing_context
;
3759 cfg
->compile_llvm
= try_llvm
;
3760 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
3762 if (cfg
->gen_seq_points
)
3763 cfg
->seq_points
= g_ptr_array_new ();
3765 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
)) {
3766 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
3770 if (cfg
->generic_sharing_context
) {
3771 method_to_register
= method_to_compile
;
3773 g_assert (method
== method_to_compile
);
3774 method_to_register
= method
;
3776 cfg
->method_to_register
= method_to_register
;
3778 if (cfg
->compile_llvm
) {
3779 /* No way to obtain the location info for 'this' */
3780 if (try_generic_shared
) {
3781 cfg
->exception_message
= g_strdup ("gshared");
3782 cfg
->disable_llvm
= TRUE
;
3785 if (cfg
->method
->save_lmf
) {
3786 cfg
->exception_message
= g_strdup ("lmf");
3787 cfg
->disable_llvm
= TRUE
;
3791 if (cfg
->method
->dynamic
) {
3792 cfg
->exception_message
= g_strdup ("dynamic.");
3793 cfg
->disable_llvm
= TRUE
;
3797 header
= cfg
->header
;
3799 MonoLoaderError
*error
;
3801 if ((error
= mono_loader_get_last_error ())) {
3802 cfg
->exception_type
= error
->exception_type
;
3804 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
3805 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
3807 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3808 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3812 if (FALSE
&& header
->clauses
) {
3814 * Cannot be enabled until LLVM supports implicit exceptions, or we use
3815 * explicit checks, or we disable this for methods which might throw implicit
3816 * exceptions inside clauses.
3818 cfg
->exception_message
= g_strdup ("clauses");
3819 cfg
->disable_llvm
= TRUE
;
3824 static gboolean inited
;
3827 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_with_llvm
);
3828 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_without_llvm
);
3833 * Check for methods which cannot be compiled by LLVM early, to avoid
3834 * the extra compilation pass.
3836 if (COMPILE_LLVM (cfg
) && cfg
->disable_llvm
) {
3837 if (cfg
->verbose_level
>= 1) {
3838 //nm = mono_method_full_name (cfg->method, TRUE);
3839 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3842 InterlockedIncrement (&methods_without_llvm
);
3843 mono_destroy_compile (cfg
);
3845 goto restart_compile
;
3850 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3851 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
3852 cfg
->disable_reuse_registers
= TRUE
;
3853 cfg
->disable_reuse_stack_slots
= TRUE
;
3855 * This decreases the change the debugger will read registers/stack slots which are
3856 * not yet initialized.
3858 cfg
->disable_initlocals_opt
= TRUE
;
3860 cfg
->extend_live_ranges
= TRUE
;
3862 /* Temporarily disable this when running in the debugger until we have support
3863 * for this in the debugger. */
3864 cfg
->disable_omit_fp
= TRUE
;
3866 /* The debugger needs all locals to be on the stack or in a global register */
3867 cfg
->disable_vreg_to_lvreg
= TRUE
;
3869 /* Don't remove unused variables when running inside the debugger since the user
3870 * may still want to view them. */
3871 cfg
->disable_deadce_vars
= TRUE
;
3873 // cfg->opt |= MONO_OPT_SHARED;
3874 cfg
->opt
&= ~MONO_OPT_DEADCE
;
3875 cfg
->opt
&= ~MONO_OPT_INLINE
;
3876 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
3877 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
3878 cfg
->opt
&= ~MONO_OPT_GSHARED
;
3880 /* This is needed for the soft debugger, which doesn't like code after the epilog */
3881 cfg
->disable_out_of_line_bblocks
= TRUE
;
3884 if (mono_using_xdebug
) {
3886 * Make each variable use its own register/stack slot and extend
3887 * their liveness to cover the whole method, making them displayable
3888 * in gdb even after they are dead.
3890 cfg
->disable_reuse_registers
= TRUE
;
3891 cfg
->disable_reuse_stack_slots
= TRUE
;
3892 cfg
->extend_live_ranges
= TRUE
;
3893 cfg
->compute_precise_live_ranges
= TRUE
;
3896 mini_gc_init_gc_map (cfg
);
3898 if (COMPILE_LLVM (cfg
)) {
3899 cfg
->opt
|= MONO_OPT_ABCREM
;
3902 if (getenv ("MONO_VERBOSE_METHOD")) {
3903 char *name
= getenv ("MONO_VERBOSE_METHOD");
3905 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
3906 MonoMethodDesc
*desc
;
3908 desc
= mono_method_desc_new (name
, TRUE
);
3909 if (mono_method_desc_full_match (desc
, cfg
->method
)) {
3910 cfg
->verbose_level
= 4;
3912 mono_method_desc_free (desc
);
3914 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
3915 cfg
->verbose_level
= 4;
3919 ip
= (guint8
*)header
->code
;
3921 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
3923 if (cfg
->verbose_level
> 0) {
3924 if (COMPILE_LLVM (cfg
))
3925 g_print ("converting llvm method %s\n", mono_method_full_name (method
, TRUE
));
3926 else if (cfg
->generic_sharing_context
)
3927 g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile
, TRUE
));
3929 g_print ("converting method %s\n", mono_method_full_name (method
, TRUE
));
3932 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
3933 cfg
->opt
|= MONO_OPT_SSA
;
3936 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")))
3937 cfg->globalra = TRUE;
3940 //cfg->globalra = TRUE;
3942 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3943 // cfg->globalra = TRUE;
3946 static int count
= 0;
3950 if (getenv ("COUNT2")) {
3951 cfg->globalra = TRUE;
3952 if (count == atoi (getenv ("COUNT2")))
3953 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3954 if (count > atoi (getenv ("COUNT2")))
3955 cfg->globalra = FALSE;
3960 if (header
->clauses
)
3961 cfg
->globalra
= FALSE
;
3963 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
3964 /* The code in the prolog clobbers caller saved registers */
3965 cfg
->globalra
= FALSE
;
3967 // FIXME: Disable globalra in case of tracing/profiling
3969 if (cfg
->method
->save_lmf
)
3970 /* The LMF saving code might clobber caller saved registers */
3971 cfg
->globalra
= FALSE
;
3973 if (header
->code_size
> 5000)
3975 /* Too large bblocks could overflow the ins positions */
3976 cfg
->globalra
= FALSE
;
3978 cfg
->rs
= mono_regstate_new ();
3980 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
3981 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
3983 /* FIXME: Fix SSA to handle branches inside bblocks */
3984 if (cfg
->opt
& MONO_OPT_SSA
)
3985 cfg
->enable_extended_bblocks
= FALSE
;
3988 * FIXME: This confuses liveness analysis because variables which are assigned after
3989 * a branch inside a bblock become part of the kill set, even though the assignment
3990 * might not get executed. This causes the optimize_initlocals pass to delete some
3991 * assignments which are needed.
3992 * Also, the mono_if_conversion pass needs to be modified to recognize the code
3995 //cfg->enable_extended_bblocks = TRUE;
3997 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
3998 if (mono_compile_is_broken (cfg
))
4002 * create MonoInst* which represents arguments and local variables
4004 mono_compile_create_vars (cfg
);
4006 /* SSAPRE is not supported on linear IR */
4007 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
4009 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
4012 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
4014 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4015 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4018 mono_destroy_compile (cfg
);
4019 try_generic_shared
= FALSE
;
4020 goto restart_compile
;
4022 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
4024 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4025 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4026 /* cfg contains the details of the failure, so let the caller cleanup */
4030 mono_jit_stats
.basic_blocks
+= cfg
->num_bblocks
;
4031 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->num_bblocks
, mono_jit_stats
.max_basic_blocks
);
4033 if (COMPILE_LLVM (cfg
)) {
4036 /* The IR has to be in SSA form for LLVM */
4037 cfg
->opt
|= MONO_OPT_SSA
;
4041 // Allow SSA on the result value
4042 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
4044 // Add an explicit return instruction referencing the return value
4045 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
4046 ins
->sreg1
= cfg
->ret
->dreg
;
4048 MONO_ADD_INS (cfg
->bb_exit
, ins
);
4051 cfg
->opt
&= ~MONO_OPT_LINEARS
;
4054 cfg
->opt
&= ~MONO_OPT_BRANCH
;
4057 /* todo: remove code when we have verified that the liveness for try/catch blocks
4061 * Currently, this can't be commented out since exception blocks are not
4062 * processed during liveness analysis.
4063 * It is also needed, because otherwise the local optimization passes would
4064 * delete assignments in cases like this:
4066 * <something which throws>
4068 * This also allows SSA to be run on methods containing exception clauses, since
4069 * SSA will ignore variables marked VOLATILE.
4071 mono_liveness_handle_exception_clauses (cfg
);
4073 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
4075 if (!COMPILE_LLVM (cfg
))
4076 mono_decompose_long_opts (cfg
);
4078 /* Should be done before branch opts */
4079 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
4080 mono_local_cprop (cfg
);
4082 if (cfg
->opt
& MONO_OPT_BRANCH
)
4083 mono_optimize_branches (cfg
);
4085 /* This must be done _before_ global reg alloc and _after_ decompose */
4086 mono_handle_global_vregs (cfg
);
4087 if (cfg
->opt
& MONO_OPT_DEADCE
)
4088 mono_local_deadce (cfg
);
4089 /* Disable this for LLVM to make the IR easier to handle */
4090 if (!COMPILE_LLVM (cfg
))
4091 mono_if_conversion (cfg
);
4093 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
4094 mono_remove_critical_edges (cfg
);
4096 /* Depth-first ordering on basic blocks */
4097 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4099 cfg
->max_block_num
= cfg
->num_bblocks
;
4102 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4103 if (cfg
->num_bblocks
!= dfn
+ 1) {
4106 cfg
->num_bblocks
= dfn
+ 1;
4108 /* remove unreachable code, because the code in them may be
4109 * inconsistent (access to dead variables for example) */
4110 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4111 bb
->flags
&= ~BB_VISITED
;
4112 compute_reachable (cfg
->bb_entry
);
4113 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4114 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
4115 compute_reachable (bb
);
4116 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4117 if (!(bb
->flags
& BB_VISITED
)) {
4118 if (cfg
->verbose_level
> 1)
4119 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
4120 bb
->code
= bb
->last_ins
= NULL
;
4121 while (bb
->out_count
)
4122 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
4125 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4126 bb
->flags
&= ~BB_VISITED
;
4129 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
4131 * we disable some optimizations if there are too many variables
4132 * because JIT time may become too expensive. The actual number needs
4133 * to be tweaked and eventually the non-linear algorithms should be fixed.
4135 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
4136 cfg
->disable_ssa
= TRUE
;
4139 if (cfg
->opt
& MONO_OPT_LOOP
) {
4140 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
4141 mono_compute_natural_loops (cfg
);
4144 /* after method_to_ir */
4146 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4147 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4152 if (header->num_clauses)
4153 cfg->disable_ssa = TRUE;
4156 //#define DEBUGSSA "logic_run"
4157 #define DEBUGSSA_CLASS "Tests"
4160 if (!cfg
->disable_ssa
) {
4161 mono_local_cprop (cfg
);
4164 mono_ssa_compute (cfg
);
4168 if (cfg
->opt
& MONO_OPT_SSA
) {
4169 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
4171 mono_ssa_compute (cfg
);
4174 if (cfg
->verbose_level
>= 2) {
4181 /* after SSA translation */
4183 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4184 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4188 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
4189 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4191 mono_ssa_cprop (cfg
);
4197 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4198 //mono_ssa_strength_reduction (cfg);
4200 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
4201 mono_perform_ssapre (cfg
);
4202 //mono_local_cprop (cfg);
4205 if (cfg
->opt
& MONO_OPT_DEADCE
) {
4206 mono_ssa_deadce (cfg
);
4207 deadce_has_run
= TRUE
;
4210 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4211 mono_perform_abc_removal (cfg
);
4213 mono_ssa_remove (cfg
);
4214 mono_local_cprop (cfg
);
4215 mono_handle_global_vregs (cfg
);
4216 if (cfg
->opt
& MONO_OPT_DEADCE
)
4217 mono_local_deadce (cfg
);
4219 if (cfg
->opt
& MONO_OPT_BRANCH
) {
4222 mono_optimize_branches (cfg
);
4224 /* Have to recompute cfg->bblocks and bb->dfn */
4225 if (cfg
->globalra
) {
4226 mono_remove_critical_edges (cfg
);
4228 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4231 /* Depth-first ordering on basic blocks */
4232 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4235 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4236 cfg
->num_bblocks
= dfn
+ 1;
4242 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
4243 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4244 mono_perform_abc_removal (cfg
);
4247 /* after SSA removal */
4249 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4250 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4254 #ifdef MONO_ARCH_SOFT_FLOAT
4255 if (!COMPILE_LLVM (cfg
))
4256 mono_decompose_soft_float (cfg
);
4258 if (!COMPILE_LLVM (cfg
))
4259 mono_decompose_vtype_opts (cfg
);
4260 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4261 mono_decompose_array_access_opts (cfg
);
4266 g_assert (cfg
->got_var_allocated
);
4269 * Allways allocate the GOT var to a register, because keeping it
4270 * in memory will increase the number of live temporaries in some
4271 * code created by inssel.brg, leading to the well known spills+
4272 * branches problem. Testcase: mcs crash in
4273 * System.MonoCustomAttrs:GetCustomAttributes.
4275 regs
= mono_arch_get_global_int_regs (cfg
);
4277 cfg
->got_var
->opcode
= OP_REGVAR
;
4278 cfg
->got_var
->dreg
= GPOINTER_TO_INT (regs
->data
);
4279 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
4285 * Have to call this again to process variables added since the first call.
4287 mono_liveness_handle_exception_clauses (cfg
);
4289 if (cfg
->globalra
) {
4292 /* Have to do this before regalloc since it can create vregs */
4293 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4294 mono_arch_lowering_pass (cfg
, bb
);
4296 mono_global_regalloc (cfg
);
4299 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
4302 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
4303 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
4304 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
4305 mono_analyze_liveness (cfg
);
4307 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
4308 regs
= mono_arch_get_global_int_regs (cfg
);
4310 regs
= g_list_delete_link (regs
, regs
);
4311 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
4315 //mono_print_code (cfg, "");
4319 /* variables are allocated after decompose, since decompose could create temps */
4320 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4321 mono_arch_allocate_vars (cfg
);
4322 if (cfg
->exception_type
)
4328 gboolean need_local_opts
;
4330 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4331 mono_spill_global_vars (cfg
, &need_local_opts
);
4333 if (need_local_opts
|| cfg
->compile_aot
) {
4334 /* To optimize code created by spill_global_vars */
4335 mono_local_cprop (cfg
);
4336 if (cfg
->opt
& MONO_OPT_DEADCE
)
4337 mono_local_deadce (cfg
);
4341 /* Add branches between non-consecutive bblocks */
4342 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4343 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
4344 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
4345 /* we are careful when inverting, since bugs like #59580
4346 * could show up when dealing with NaNs.
4348 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
4349 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
4350 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
4351 bb
->last_ins
->inst_false_bb
= tmp
;
4353 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
4355 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
4356 inst
->opcode
= OP_BR
;
4357 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
4358 mono_bblock_add_inst (bb
, inst
);
4363 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
4364 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4365 MonoInst
*tree
= bb
->code
;
4366 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
4369 for (; tree
; tree
= tree
->next
) {
4370 mono_print_ins_index (-1, tree
);
4376 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4377 bb
->max_vreg
= cfg
->next_vreg
;
4381 if (COMPILE_LLVM (cfg
)) {
4385 /* The IR has to be in SSA form for LLVM */
4386 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
4387 cfg
->exception_message
= g_strdup ("SSA disabled.");
4388 cfg
->disable_llvm
= TRUE
;
4391 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4392 mono_decompose_array_access_opts (cfg
);
4394 if (!cfg
->disable_llvm
)
4395 mono_llvm_emit_method (cfg
);
4396 if (cfg
->disable_llvm
) {
4397 if (cfg
->verbose_level
>= 1) {
4398 //nm = mono_method_full_name (cfg->method, TRUE);
4399 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
4402 InterlockedIncrement (&methods_without_llvm
);
4403 mono_destroy_compile (cfg
);
4405 goto restart_compile
;
4408 InterlockedIncrement (&methods_with_llvm
);
4410 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
4411 nm
= mono_method_full_name (cfg
->method
, TRUE
);
4412 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
4414 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
4422 if (cfg
->verbose_level
>= 2) {
4423 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
4424 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
4428 cfg
->jit_info
= create_jit_info (cfg
, method_to_compile
);
4430 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
4431 if (cfg
->extend_live_ranges
) {
4432 /* Extend live ranges to cover the whole method */
4433 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
4434 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
4438 mono_save_xdebug_info (cfg
);
4440 mini_gc_create_gc_map (cfg
);
4442 mono_save_seq_point_info (cfg
);
4444 if (!cfg
->compile_aot
) {
4445 mono_domain_lock (cfg
->domain
);
4446 mono_jit_info_table_add (cfg
->domain
, cfg
->jit_info
);
4448 if (cfg
->method
->dynamic
)
4449 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= cfg
->jit_info
;
4450 mono_domain_unlock (cfg
->domain
);
4453 /* collect statistics */
4454 mono_perfcounters
->jit_methods
++;
4455 mono_perfcounters
->jit_bytes
+= header
->code_size
;
4456 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
4457 code_size_ratio
= cfg
->code_len
;
4458 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
4459 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
4460 g_free (mono_jit_stats
.biggest_method
);
4461 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4463 code_size_ratio
= (code_size_ratio
* 100) / header
->code_size
;
4464 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
4465 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
4466 g_free (mono_jit_stats
.max_ratio_method
);
4467 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4469 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
4471 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4472 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4480 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4482 g_assert_not_reached ();
4486 #endif /* DISABLE_JIT */
4489 mono_domain_lookup_shared_generic (MonoDomain
*domain
, MonoMethod
*method
)
4491 static gboolean inited
= FALSE
;
4492 static int lookups
= 0;
4493 static int failed_lookups
= 0;
4496 ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, mini_get_shared_method (method
));
4497 if (ji
&& !ji
->has_generic_jit_info
)
4501 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
4502 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
4514 * LOCKING: Assumes domain->jit_code_hash_lock is held.
4517 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
4519 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
4524 if (!mono_method_is_generic_sharable_impl (method
, FALSE
))
4526 return mono_domain_lookup_shared_generic (domain
, method
);
4530 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
4534 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
4535 mono_domain_jit_code_hash_lock (domain
);
4536 info
= lookup_method_inner (domain
, method
);
4537 mono_domain_jit_code_hash_unlock (domain
);
4538 mono_loader_unlock ();
4544 static FILE* perf_map_file
= NULL
;
4547 mono_enable_jit_map (void)
4549 if (!perf_map_file
) {
4551 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
4553 perf_map_file
= fopen (name
, "w");
4558 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
4561 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
4565 mono_emit_jit_map (MonoJitInfo
*jinfo
)
4567 if (perf_map_file
) {
4568 char *name
= mono_method_full_name (jinfo
->method
, TRUE
);
4569 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
4576 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
4579 gpointer code
= NULL
;
4580 MonoJitInfo
*jinfo
, *info
;
4582 MonoException
*ex
= NULL
;
4583 guint32 prof_options
;
4585 #ifdef MONO_USE_AOT_COMPILER
4586 if (opt
& MONO_OPT_AOT
) {
4587 MonoDomain
*domain
= mono_domain_get ();
4589 mono_class_init (method
->klass
);
4591 if ((code
= mono_aot_get_method (domain
, method
))) {
4592 vtable
= mono_class_vtable (domain
, method
->klass
);
4594 mono_runtime_class_init (vtable
);
4601 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4602 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
4604 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
4606 if (!piinfo
->addr
) {
4607 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
4608 piinfo
->addr
= mono_lookup_internal_call (method
);
4609 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
4611 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
);
4613 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
);
4616 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
4618 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
4619 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4621 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
4622 //mono_debug_add_wrapper (method, nm);
4623 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
4624 const char *name
= method
->name
;
4627 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
4628 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
4629 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
4632 * We need to make sure this wrapper
4633 * is compiled because it might end up
4634 * in an (M)RGCTX if generic sharing
4635 * is enabled, and would be called
4636 * indirectly. If it were a
4637 * trampoline we'd try to patch that
4638 * indirect call, which is not
4641 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
4642 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
4643 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4644 return mono_create_delegate_trampoline (method
->klass
);
4646 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
4647 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4649 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
4650 nm
= mono_marshal_get_delegate_begin_invoke (method
);
4651 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4652 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
4653 nm
= mono_marshal_get_delegate_end_invoke (method
);
4654 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4660 if (mono_aot_only
) {
4661 char *fullname
= mono_method_full_name (method
, TRUE
);
4662 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only.\n", fullname
);
4664 *jit_ex
= mono_get_exception_execution_engine (msg
);
4671 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
4673 switch (cfg
->exception_type
) {
4674 case MONO_EXCEPTION_NONE
:
4676 case MONO_EXCEPTION_TYPE_LOAD
:
4677 case MONO_EXCEPTION_MISSING_FIELD
:
4678 case MONO_EXCEPTION_MISSING_METHOD
:
4679 case MONO_EXCEPTION_FILE_NOT_FOUND
:
4680 case MONO_EXCEPTION_BAD_IMAGE
: {
4681 /* Throw a type load exception if needed */
4682 MonoLoaderError
*error
= mono_loader_get_last_error ();
4685 ex
= mono_loader_error_prepare_exception (error
);
4687 if (cfg
->exception_ptr
) {
4688 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
4690 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
4691 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
4692 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
4693 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
4694 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
4695 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
4696 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
4697 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
4698 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
4699 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
4701 g_assert_not_reached ();
4706 case MONO_EXCEPTION_INVALID_PROGRAM
:
4707 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
4709 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
4710 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
4712 case MONO_EXCEPTION_METHOD_ACCESS
:
4713 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
4715 case MONO_EXCEPTION_FIELD_ACCESS
:
4716 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
4718 /* this can only be set if the security manager is active */
4719 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
4720 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
4721 MonoObject
*exc
= NULL
;
4724 args
[0] = &cfg
->exception_data
;
4726 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
4728 ex
= (MonoException
*)exc
;
4731 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
4732 MonoException
*exp
= cfg
->exception_ptr
;
4733 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
4739 g_assert_not_reached ();
4743 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4744 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
4746 mono_destroy_compile (cfg
);
4752 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
4753 mono_domain_lock (target_domain
);
4755 /* Check if some other thread already did the job. In this case, we can
4756 discard the code this thread generated. */
4758 mono_domain_jit_code_hash_lock (target_domain
);
4760 info
= lookup_method_inner (target_domain
, method
);
4762 /* We can't use a domain specific method in another domain */
4763 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
4764 code
= info
->code_start
;
4765 // printf("Discarding code for method %s\n", method->name);
4770 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
4771 mono_domain_jit_code_hash_unlock (target_domain
);
4772 code
= cfg
->native_code
;
4774 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl (method
, FALSE
))
4775 mono_stats
.generics_shared_methods
++;
4777 mono_domain_jit_code_hash_unlock (target_domain
);
4780 jinfo
= cfg
->jit_info
;
4782 prof_options
= cfg
->prof_options
;
4784 mono_destroy_compile (cfg
);
4786 if (domain_jit_info (target_domain
)->jump_target_hash
) {
4787 MonoJumpInfo patch_info
;
4789 list
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4791 patch_info
.next
= NULL
;
4792 patch_info
.ip
.i
= 0;
4793 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
4794 patch_info
.data
.method
= method
;
4795 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4797 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
4798 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, TRUE
);
4799 g_slist_free (list
);
4802 mono_emit_jit_map (jinfo
);
4803 mono_domain_unlock (target_domain
);
4804 mono_loader_unlock ();
4806 vtable
= mono_class_vtable (target_domain
, method
->klass
);
4809 exc
= mono_class_get_exception_for_failure (method
->klass
);
4811 mono_raise_exception (exc
);
4814 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
) {
4815 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
4816 /* The profiler doesn't know about wrappers, so pass the original icall method */
4817 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method
), jinfo
, MONO_PROFILE_OK
);
4819 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
4822 mono_runtime_class_init (vtable
);
4827 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
4829 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
4832 MonoJitICallInfo
*callinfo
= NULL
;
4835 * ICALL wrappers are handled specially, since there is only one copy of them
4836 * shared by all appdomains.
4838 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
4839 const char *icall_name
;
4841 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
4842 g_assert (icall_name
);
4843 callinfo
= mono_find_jit_icall_by_name (icall_name
);
4844 g_assert (callinfo
);
4846 /* Must be domain neutral since there is only one copy */
4847 opt
|= MONO_OPT_SHARED
;
4850 if (opt
& MONO_OPT_SHARED
)
4851 target_domain
= mono_get_root_domain ();
4853 target_domain
= domain
;
4855 info
= lookup_method (target_domain
, method
);
4857 /* We can't use a domain specific method in another domain */
4858 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4861 mono_jit_stats
.methods_lookups
++;
4862 vtable
= mono_class_vtable (domain
, method
->klass
);
4864 mono_runtime_class_init (vtable
);
4865 return mono_create_ftnptr (target_domain
, info
->code_start
);
4869 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
4873 p
= mono_create_ftnptr (target_domain
, code
);
4877 if (!callinfo
->wrapper
) {
4878 callinfo
->wrapper
= p
;
4879 mono_register_jit_icall_wrapper (callinfo
, p
);
4880 mono_debug_add_icall_wrapper (method
, callinfo
);
4889 mono_jit_compile_method (MonoMethod
*method
)
4891 MonoException
*ex
= NULL
;
4894 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
4897 mono_raise_exception (ex
);
4903 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4905 invalidated_delegate_trampoline (char *desc
)
4907 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
4908 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
4914 * mono_jit_free_method:
4916 * Free all memory allocated by the JIT for METHOD.
4919 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
4921 MonoJitDynamicMethodInfo
*ji
;
4922 gboolean destroy
= TRUE
;
4924 g_assert (method
->dynamic
);
4926 mono_domain_lock (domain
);
4927 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
4928 mono_domain_unlock (domain
);
4932 mono_domain_lock (domain
);
4933 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
4934 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
4935 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
4936 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
4937 mono_domain_unlock (domain
);
4939 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4940 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
4942 * Instead of freeing the code, change it to call an error routine
4943 * so people can fix their code.
4945 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
4946 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
4949 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
4955 * This needs to be done before freeing code_mp, since the code address is the
4956 * key in the table, so if we free the code_mp first, another thread can grab the
4957 * same code address and replace our entry in the table.
4959 mono_jit_info_table_remove (domain
, ji
->ji
);
4962 mono_code_manager_destroy (ji
->code_mp
);
4967 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
4969 MonoDomain
*target_domain
;
4972 if (default_opt
& MONO_OPT_SHARED
)
4973 target_domain
= mono_get_root_domain ();
4975 target_domain
= domain
;
4977 info
= lookup_method (target_domain
, method
);
4979 /* We can't use a domain specific method in another domain */
4980 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4981 mono_jit_stats
.methods_lookups
++;
4984 return info
->code_start
;
4994 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
4996 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
5001 gpointer compiled_method
;
5002 gpointer runtime_invoke
;
5004 MonoDynCallInfo
*dyn_call_info
;
5005 MonoClass
*ret_box_class
;
5006 } RuntimeInvokeInfo
;
5009 * mono_jit_runtime_invoke:
5010 * @method: the method to invoke
5011 * @obj: this pointer
5012 * @params: array of parameter values.
5013 * @exc: used to catch exceptions objects
5016 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
5019 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
5020 MonoDomain
*domain
= mono_domain_get ();
5021 MonoJitDomainInfo
*domain_info
;
5022 RuntimeInvokeInfo
*info
, *info2
;
5024 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
5025 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
5029 domain_info
= domain_jit_info (domain
);
5031 mono_domain_lock (domain
);
5032 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5033 mono_domain_unlock (domain
);
5036 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
5038 * This might be redundant since mono_class_vtable () already does this,
5039 * but keep it just in case for moonlight.
5041 mono_class_setup_vtable (method
->klass
);
5042 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
5044 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
5046 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
5051 info
= g_new0 (RuntimeInvokeInfo
, 1);
5053 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
5054 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
5055 g_assert (info
->vtable
);
5057 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
5058 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
5060 * Array Get/Set/Address methods. The JIT implements them using inline code
5061 * inside the runtime invoke wrappers, so no need to compile them.
5063 info
->compiled_method
= NULL
;
5065 MonoException
*jit_ex
= NULL
;
5067 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
5068 if (!info
->compiled_method
) {
5072 *exc
= (MonoObject
*)jit_ex
;
5075 mono_raise_exception (jit_ex
);
5079 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
5080 info
->compiled_method
= mono_create_static_rgctx_trampoline (method
, info
->compiled_method
);
5084 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
5085 * in full-aot mode, so we use a slower, but more generic wrapper if
5086 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
5088 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5089 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
5090 MonoMethodSignature
*sig
= mono_method_signature (method
);
5091 gboolean supported
= TRUE
;
5094 if (method
->string_ctor
)
5095 sig
= mono_marshal_get_string_ctor_signature (method
);
5097 for (i
= 0; i
< sig
->param_count
; ++i
) {
5098 MonoType
*t
= sig
->params
[i
];
5100 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
5104 if (method
->klass
->contextbound
|| !info
->compiled_method
)
5108 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
5110 if (info
->dyn_call_info
) {
5111 switch (sig
->ret
->type
) {
5112 case MONO_TYPE_VOID
:
5124 case MONO_TYPE_BOOLEAN
:
5125 case MONO_TYPE_CHAR
:
5128 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5131 info
->ret_box_class
= mono_defaults
.int_class
;
5133 case MONO_TYPE_STRING
:
5134 case MONO_TYPE_CLASS
:
5135 case MONO_TYPE_ARRAY
:
5136 case MONO_TYPE_SZARRAY
:
5137 case MONO_TYPE_OBJECT
:
5139 case MONO_TYPE_GENERICINST
:
5140 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
5141 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5143 case MONO_TYPE_VALUETYPE
:
5144 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5147 g_assert_not_reached ();
5154 if (!info
->dyn_call_info
)
5155 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
5157 mono_domain_lock (domain
);
5158 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5163 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
5165 mono_domain_unlock (domain
);
5168 runtime_invoke
= info
->runtime_invoke
;
5171 * We need this here because mono_marshal_get_runtime_invoke can place
5172 * the helper method in System.Object and not the target class.
5174 mono_runtime_class_init (info
->vtable
);
5176 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5177 if (info
->dyn_call_info
) {
5178 MonoMethodSignature
*sig
= mono_method_signature (method
);
5180 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
5183 guint8 retval
[128];
5185 if (!dyn_runtime_invoke
) {
5186 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
5187 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
5190 /* Convert the arguments to the format expected by start_dyn_call () */
5191 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
5194 args
[pindex
++] = &obj
;
5195 for (i
= 0; i
< sig
->param_count
; ++i
) {
5196 MonoType
*t
= sig
->params
[i
];
5199 args
[pindex
++] = ¶ms
[i
];
5200 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
5201 args
[pindex
++] = ¶ms
[i
];
5203 args
[pindex
++] = params
[i
];
5207 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
5209 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
5211 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
5213 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
5215 if (info
->ret_box_class
)
5216 return mono_value_box (domain
, info
->ret_box_class
, retval
);
5218 return *(MonoObject
**)retval
;
5222 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
5226 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
5228 MonoException
*exc
= NULL
;
5230 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
5235 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5237 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
5238 if (mono_arch_is_int_overflow (ctx
, info
))
5239 exc
= mono_get_exception_arithmetic ();
5241 exc
= mono_get_exception_divide_by_zero ();
5243 exc
= mono_get_exception_divide_by_zero ();
5247 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5250 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5253 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5257 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
5262 exc
= mono_get_exception_execution_engine ("SIGILL");
5264 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5267 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
5268 #define HAVE_SIG_INFO
5272 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
5275 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
5279 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
5280 if (mono_arch_is_single_step_event (info
, ctx
)) {
5281 mono_debugger_agent_single_step_event (ctx
);
5283 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
5284 mono_debugger_agent_breakpoint_hit (ctx
);
5289 #if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
5290 if (mono_aot_is_pagefault (info
->si_addr
)) {
5291 mono_aot_handle_pagefault (info
->si_addr
);
5296 /* The thread might no be registered with the runtime */
5297 if (!mono_domain_get () || !jit_tls
) {
5298 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5300 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5303 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5305 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
5306 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
5309 /* The hard-guard page has been hit: there is not much we can do anymore
5310 * Print a hopefully clear message and abort.
5312 if (jit_tls
->stack_size
&&
5313 ABS ((guint8
*)info
->si_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 32768) {
5315 /* we don't do much now, but we can warn the user with a useful message */
5316 fprintf (stderr
, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx
), (gpointer
)info
->si_addr
);
5317 if (ji
&& ji
->method
)
5318 method
= mono_method_full_name (ji
->method
, TRUE
);
5320 method
= "Unmanaged";
5321 fprintf (stderr
, "At %s\n", method
);
5324 /* The original handler might not like that it is executed on an altstack... */
5325 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
5328 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
5333 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5336 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5339 mono_arch_handle_exception (ctx
, NULL
, FALSE
);
5344 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
5349 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
5351 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5354 /* mono_jit_create_remoting_trampoline:
5355 * @method: pointer to the method info
5357 * Creates a trampoline which calls the remoting functions. This
5358 * is used in the vtable of transparent proxies.
5360 * Returns: a pointer to the newly created code
5363 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
5366 guint8
*addr
= NULL
;
5368 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
5369 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
5373 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
5374 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
5375 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
5376 addr
= mono_compile_method (nm
);
5378 addr
= mono_compile_method (method
);
5380 return mono_get_addr_from_ftnptr (addr
);
5383 #ifdef MONO_ARCH_HAVE_IMT
5384 static G_GNUC_UNUSED gpointer
5385 mini_get_imt_trampoline (void)
5387 static gpointer tramp
= NULL
;
5389 tramp
= mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5395 mini_get_vtable_trampoline (int slot_index
)
5397 static gpointer tramp
= NULL
;
5400 tramp
= mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5405 mini_parse_debug_options (void)
5407 char *options
= getenv ("MONO_DEBUG");
5408 gchar
**args
, **ptr
;
5413 args
= g_strsplit (options
, ",", -1);
5415 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
5416 const char *arg
= *ptr
;
5418 if (!strcmp (arg
, "handle-sigint"))
5419 debug_options
.handle_sigint
= TRUE
;
5420 else if (!strcmp (arg
, "keep-delegates"))
5421 debug_options
.keep_delegates
= TRUE
;
5422 else if (!strcmp (arg
, "collect-pagefault-stats"))
5423 debug_options
.collect_pagefault_stats
= TRUE
;
5424 else if (!strcmp (arg
, "break-on-unverified"))
5425 debug_options
.break_on_unverified
= TRUE
;
5426 else if (!strcmp (arg
, "no-gdb-backtrace"))
5427 debug_options
.no_gdb_backtrace
= TRUE
;
5428 else if (!strcmp (arg
, "suspend-on-sigsegv"))
5429 debug_options
.suspend_on_sigsegv
= TRUE
;
5430 else if (!strcmp (arg
, "dont-free-domains"))
5431 mono_dont_free_domains
= TRUE
;
5432 else if (!strcmp (arg
, "dyn-runtime-invoke"))
5433 debug_options
.dyn_runtime_invoke
= TRUE
;
5434 else if (!strcmp (arg
, "gdb"))
5435 debug_options
.gdb
= TRUE
;
5436 else if (!strcmp (arg
, "explicit-null-checks"))
5437 debug_options
.explicit_null_checks
= TRUE
;
5438 else if (!strcmp (arg
, "gen-seq-points"))
5439 debug_options
.gen_seq_points
= TRUE
;
5440 else if (!strcmp (arg
, "init-stacks"))
5441 debug_options
.init_stacks
= TRUE
;
5443 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
5444 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");
5453 mini_get_debug_options (void)
5455 return &debug_options
;
5459 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
5464 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
5470 #elif defined(__ppc64__) || defined(__powerpc64__)
5473 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
5486 mini_get_addr_from_ftnptr (gpointer descr
)
5488 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
5489 return *(gpointer
*)descr
;
5495 static void runtime_invoke_info_free (gpointer value
);
5498 mini_create_jit_domain_info (MonoDomain
*domain
)
5500 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
5502 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5503 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5504 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5505 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5506 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5507 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
5508 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
5509 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5511 domain
->runtime_info
= info
;
5515 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
5517 g_slist_free (value
);
5521 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
5523 MonoJitDynamicMethodInfo
*di
= value
;
5524 mono_code_manager_destroy (di
->code_mp
);
5529 runtime_invoke_info_free (gpointer value
)
5531 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
5533 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5534 if (info
->dyn_call_info
)
5535 mono_arch_dyn_call_free (info
->dyn_call_info
);
5541 mini_free_jit_domain_info (MonoDomain
*domain
)
5543 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
5545 if (info
->jump_target_hash
) {
5546 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
5547 g_hash_table_destroy (info
->jump_target_hash
);
5549 if (info
->jump_target_got_slot_hash
) {
5550 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
5551 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
5553 if (info
->dynamic_code_hash
) {
5554 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
5555 g_hash_table_destroy (info
->dynamic_code_hash
);
5557 if (info
->method_code_hash
)
5558 g_hash_table_destroy (info
->method_code_hash
);
5559 g_hash_table_destroy (info
->class_init_trampoline_hash
);
5560 g_hash_table_destroy (info
->jump_trampoline_hash
);
5561 g_hash_table_destroy (info
->jit_trampoline_hash
);
5562 g_hash_table_destroy (info
->delegate_trampoline_hash
);
5563 if (info
->static_rgctx_trampoline_hash
)
5564 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
5565 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
5566 g_hash_table_destroy (info
->runtime_invoke_hash
);
5567 g_hash_table_destroy (info
->seq_points
);
5568 g_hash_table_destroy (info
->arch_seq_points
);
5570 if (info
->agent_info
)
5571 mono_debugger_agent_free_domain_info (domain
);
5573 g_free (domain
->runtime_info
);
5574 domain
->runtime_info
= NULL
;
5578 mini_init (const char *filename
, const char *runtime_version
)
5581 MonoRuntimeCallbacks callbacks
;
5583 MONO_PROBE_VES_INIT_BEGIN ();
5586 if (access ("/proc/self/maps", F_OK
) != 0) {
5587 g_print ("Mono requires /proc to be mounted.\n");
5592 /* Happens when using the embedding interface */
5593 if (!default_opt_set
)
5594 default_opt
= mono_parse_default_optimizations (NULL
);
5596 InitializeCriticalSection (&jit_mutex
);
5598 #ifdef MONO_DEBUGGER_SUPPORTED
5599 if (mini_debug_running_inside_mdb ())
5600 mini_debugger_init ();
5603 #ifdef MONO_ARCH_HAVE_TLS_GET
5604 mono_runtime_set_has_tls_get (TRUE
);
5606 mono_runtime_set_has_tls_get (FALSE
);
5609 if (!global_codeman
)
5610 global_codeman
= mono_code_manager_new ();
5611 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
5613 memset (&callbacks
, 0, sizeof (callbacks
));
5614 callbacks
.create_ftnptr
= mini_create_ftnptr
;
5615 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
5616 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
5618 #ifdef MONO_ARCH_HAVE_IMT
5620 if (!mono_use_llvm
) {
5621 /* LLVM needs a per-method vtable trampoline */
5622 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
5627 mono_install_callbacks (&callbacks
);
5629 mono_arch_cpu_init ();
5633 mono_unwind_init ();
5637 if (getenv ("MONO_DEBUG") != NULL
)
5638 mini_parse_debug_options ();
5640 if (getenv ("MONO_XDEBUG")) {
5641 char *xdebug_opts
= getenv ("MONO_XDEBUG");
5642 mono_xdebug_init (xdebug_opts
);
5643 /* So methods for multiple domains don't have the same address */
5644 mono_dont_free_domains
= TRUE
;
5645 mono_using_xdebug
= TRUE
;
5646 } else if (mini_get_debug_options ()->gdb
) {
5647 mono_xdebug_init ((char*)"gdb");
5648 mono_dont_free_domains
= TRUE
;
5649 mono_using_xdebug
= TRUE
;
5656 mono_trampolines_init ();
5658 if (!g_thread_supported ())
5659 g_thread_init (NULL
);
5661 mono_gc_base_init ();
5663 mono_jit_tls_id
= TlsAlloc ();
5664 setup_jit_tls_data ((gpointer
)-1, mono_thread_abort
);
5666 if (default_opt
& MONO_OPT_AOT
)
5669 mono_debugger_agent_init ();
5671 #ifdef MONO_ARCH_GSHARED_SUPPORTED
5672 mono_set_generic_sharing_supported (TRUE
);
5675 #ifndef MONO_CROSS_COMPILE
5676 mono_runtime_install_handlers ();
5678 mono_threads_install_cleanup (mini_thread_cleanup
);
5680 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
5681 // This is experimental code so provide an env var to switch it off
5682 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
5683 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
5685 check_for_pending_exc
= FALSE
;
5686 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
5690 #define JIT_TRAMPOLINES_WORK
5691 #ifdef JIT_TRAMPOLINES_WORK
5692 mono_install_compile_method (mono_jit_compile_method
);
5693 mono_install_free_method (mono_jit_free_method
);
5694 #ifdef MONO_ARCH_LLVM_SUPPORTED
5696 /* The runtime currently only uses this for filling out vtables */
5697 mono_install_trampoline (mono_create_llvm_vcall_trampoline
);
5699 mono_install_trampoline (mono_create_jit_trampoline
);
5701 mono_install_trampoline (mono_create_jit_trampoline
);
5703 mono_install_jump_trampoline (mono_create_jump_trampoline
);
5704 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
5705 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
5706 mono_install_create_domain_hook (mini_create_jit_domain_info
);
5707 mono_install_free_domain_hook (mini_free_jit_domain_info
);
5709 #define JIT_INVOKE_WORKS
5710 #ifdef JIT_INVOKE_WORKS
5711 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
5713 mono_install_stack_walk (mono_jit_walk_stack
);
5714 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
5715 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
5716 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
5718 if (debug_options
.collect_pagefault_stats
) {
5719 mono_aot_set_make_unreadable (TRUE
);
5722 if (runtime_version
)
5723 domain
= mono_init_version (filename
, runtime_version
);
5725 domain
= mono_init_from_assembly (filename
, filename
);
5727 if (mono_aot_only
) {
5728 /* This helps catch code allocation requests */
5729 mono_code_manager_set_read_only (domain
->code_mp
);
5732 #ifdef MONO_ARCH_HAVE_IMT
5735 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
5737 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
5738 if (!mono_use_llvm
) {
5740 * The imt code in mono_magic_trampoline () can't handle LLVM code. By disabling
5741 * this, we force iface calls to go through the llvm vcall trampoline.
5743 mono_install_imt_trampoline (mini_get_imt_trampoline ());
5748 /* This must come after mono_init () in the aot-only case */
5749 mono_exceptions_init ();
5750 mono_install_handler (mono_get_throw_exception ());
5754 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
5755 ves_icall_get_frame_info
);
5756 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
5757 ves_icall_get_trace
);
5758 mono_add_internal_call ("System.Exception::get_trace",
5759 ves_icall_System_Exception_get_trace
);
5760 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
5761 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
5762 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
5763 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
5764 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
5765 mono_runtime_install_handlers
);
5768 create_helper_signature ();
5770 #define JIT_CALLS_WORK
5771 #ifdef JIT_CALLS_WORK
5772 /* Needs to be called here since register_jit_icall depends on it */
5773 mono_marshal_init ();
5775 mono_arch_register_lowlevel_calls ();
5776 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
5777 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
5778 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
5779 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
5780 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
5781 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "void", TRUE
);
5782 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
5784 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
5785 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
5786 #ifdef MONO_ARCH_HAVE_THROW_EXCEPTION_BY_NAME
5787 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE
);
5789 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
5790 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
5793 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
5794 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
5795 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
5796 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
5797 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
5800 * NOTE, NOTE, NOTE, NOTE:
5801 * when adding emulation for some opcodes, remember to also add a dummy
5802 * rule to the burg files, because we need the arity information to be correct.
5804 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
5805 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
5806 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
5807 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
5808 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
5809 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
5810 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
5811 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
5814 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
5815 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
5816 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
5817 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
5820 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
5821 mono_register_opcode_emulation (CEE_DIV
, "__emul_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5822 mono_register_opcode_emulation (CEE_DIV_UN
, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5823 mono_register_opcode_emulation (CEE_REM
, "__emul_irem", "int32 int32 int32", mono_irem
, FALSE
);
5824 mono_register_opcode_emulation (CEE_REM_UN
, "__emul_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5825 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5826 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5827 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
5828 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5831 #ifdef MONO_ARCH_EMULATE_MUL_DIV
5832 mono_register_opcode_emulation (CEE_MUL
, "__emul_imul", "int32 int32 int32", mono_imul
, TRUE
);
5833 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
5836 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
5837 mono_register_opcode_emulation (CEE_MUL_OVF
, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5838 mono_register_opcode_emulation (CEE_MUL_OVF_UN
, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5839 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5840 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5843 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
5844 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
5847 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
5848 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
5849 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
5850 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
5852 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
5853 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
5855 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
5856 mono_register_opcode_emulation (CEE_CONV_R_UN
, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5857 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5859 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
5860 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
5862 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
5863 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
5865 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
5866 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
5868 #ifdef MONO_ARCH_EMULATE_FREM
5869 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
5872 #ifdef MONO_ARCH_SOFT_FLOAT
5873 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
5874 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
5875 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
5876 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
5877 mono_register_opcode_emulation (CEE_CONV_R8
, "__emul_conv_r8", "double int32", mono_conv_to_r8
, FALSE
);
5878 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
5879 mono_register_opcode_emulation (CEE_CONV_R4
, "__emul_conv_r4", "double int32", mono_conv_to_r4
, FALSE
);
5880 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
5881 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
5882 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
5883 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
5884 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
5885 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
5886 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
5887 #if SIZEOF_VOID_P == 4
5888 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
5891 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
5892 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
5893 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
5894 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
5895 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
5896 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
5897 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
5898 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
5899 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
5900 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
5902 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
5903 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
5904 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
5905 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
5906 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
5908 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
5909 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
5910 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
5911 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
5914 #ifdef COMPRESSED_INTERFACE_BITMAP
5915 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
5918 #if SIZEOF_REGISTER == 4
5919 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
5922 /* other jit icalls */
5923 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
5924 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
5925 "ptr ptr ptr", FALSE
);
5926 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
5927 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
5928 "ptr ptr ptr ptr", FALSE
);
5929 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
5930 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
5931 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
5932 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
5933 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
5934 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
5935 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
5936 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
5937 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
5938 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
5939 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
5940 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
5941 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
5942 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
5943 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
5944 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
5945 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
5946 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
5947 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
5948 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
5949 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
5950 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
5951 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
5952 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
5953 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
5956 mono_generic_sharing_init ();
5958 #ifdef MONO_ARCH_SIMD_INTRINSICS
5959 mono_simd_intrinsics_init ();
5962 #if MONO_SUPPORT_TASKLETS
5963 mono_tasklets_init ();
5966 if (mono_compile_aot
)
5968 * Avoid running managed code when AOT compiling, since the platform
5969 * might only support aot-only execution.
5971 mono_runtime_set_no_exec (TRUE
);
5973 #define JIT_RUNTIME_WORKS
5974 #ifdef JIT_RUNTIME_WORKS
5975 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
5976 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
5977 mono_thread_attach (domain
);
5980 mono_profiler_runtime_initialized ();
5982 MONO_PROBE_VES_INIT_END ();
5987 MonoJitStats mono_jit_stats
= {0};
5990 print_jit_stats (void)
5992 if (mono_jit_stats
.enabled
) {
5993 g_print ("Mono Jit statistics\n");
5994 g_print ("Compiled methods: %ld\n", mono_jit_stats
.methods_compiled
);
5995 g_print ("Methods from AOT: %ld\n", mono_jit_stats
.methods_aot
);
5996 g_print ("Methods cache lookup: %ld\n", mono_jit_stats
.methods_lookups
);
5997 g_print ("Method trampolines: %ld\n", mono_jit_stats
.method_trampolines
);
5998 g_print ("Basic blocks: %ld\n", mono_jit_stats
.basic_blocks
);
5999 g_print ("Max basic blocks: %ld\n", mono_jit_stats
.max_basic_blocks
);
6000 g_print ("Allocated vars: %ld\n", mono_jit_stats
.allocate_var
);
6001 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats
.cil_code_size
);
6002 g_print ("Native code size: %ld\n", mono_jit_stats
.native_code_size
);
6003 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
6004 mono_jit_stats
.max_ratio_method
);
6005 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
6006 mono_jit_stats
.biggest_method
);
6007 g_print ("Code reallocs: %ld\n", mono_jit_stats
.code_reallocs
);
6008 g_print ("Allocated code size: %ld\n", mono_jit_stats
.allocated_code_size
);
6009 g_print ("Inlineable methods: %ld\n", mono_jit_stats
.inlineable_methods
);
6010 g_print ("Inlined methods: %ld\n", mono_jit_stats
.inlined_methods
);
6011 g_print ("Regvars: %ld\n", mono_jit_stats
.regvars
);
6012 g_print ("Locals stack size: %ld\n", mono_jit_stats
.locals_stack_size
);
6014 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
6015 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
6016 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
6017 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
6018 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
6019 g_print ("Methods: %ld\n", mono_stats
.method_count
);
6020 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
6021 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
6022 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
6024 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
6025 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
6026 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
6028 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
6029 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
6030 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
6032 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
6033 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
6034 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
6036 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
6037 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
6038 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
6039 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
6040 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
6041 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
6042 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
6043 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
6045 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
6046 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
6047 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
6049 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
6051 g_print ("Minor GC collections: %ld\n", mono_stats
.minor_gc_count
);
6053 g_print ("Major GC collections: %ld\n", mono_stats
.major_gc_count
);
6055 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats
.minor_gc_time_usecs
/ 1000.0);
6057 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats
.major_gc_time_usecs
/ 1000.0);
6058 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
6059 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
6060 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
6061 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
6062 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
6063 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
6064 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
6067 g_free (mono_jit_stats
.max_ratio_method
);
6068 mono_jit_stats
.max_ratio_method
= NULL
;
6069 g_free (mono_jit_stats
.biggest_method
);
6070 mono_jit_stats
.biggest_method
= NULL
;
6075 mini_cleanup (MonoDomain
*domain
)
6077 mono_runtime_shutdown_stat_profiler ();
6080 cominterop_release_all_rcws ();
6083 #ifndef MONO_CROSS_COMPILE
6085 * mono_runtime_cleanup() and mono_domain_finalize () need to
6086 * be called early since they need the execution engine still
6087 * fully working (mono_domain_finalize may invoke managed finalizers
6088 * and mono_runtime_cleanup will wait for other threads to finish).
6090 mono_domain_finalize (domain
, 2000);
6093 /* This accesses metadata so needs to be called before runtime shutdown */
6096 mono_profiler_shutdown ();
6098 #ifndef MONO_CROSS_COMPILE
6099 mono_runtime_cleanup (domain
);
6102 mono_icall_cleanup ();
6104 mono_runtime_cleanup_handlers ();
6106 mono_domain_free (domain
, TRUE
);
6108 mono_debugger_cleanup ();
6111 mono_llvm_cleanup ();
6114 mono_trampolines_cleanup ();
6116 mono_unwind_cleanup ();
6118 if (!mono_dont_free_global_codeman
)
6119 mono_code_manager_destroy (global_codeman
);
6120 g_hash_table_destroy (jit_icall_name_hash
);
6121 g_free (emul_opcode_map
);
6122 g_free (emul_opcode_opcodes
);
6124 mono_arch_cleanup ();
6128 mono_trace_cleanup ();
6130 mono_counters_dump (-1, stdout
);
6132 if (mono_inject_async_exc_method
)
6133 mono_method_desc_free (mono_inject_async_exc_method
);
6135 TlsFree(mono_jit_tls_id
);
6137 DeleteCriticalSection (&jit_mutex
);
6139 DeleteCriticalSection (&mono_delegate_section
);
6143 mono_set_defaults (int verbose_level
, guint32 opts
)
6145 mini_verbose
= verbose_level
;
6147 default_opt_set
= TRUE
;
6151 mono_disable_optimizations (guint32 opts
)
6153 default_opt
&= ~opts
;
6157 * mono_get_runtime_build_info:
6159 * Return the runtime version + build date in string format.
6160 * The returned string is owned by the caller.
6163 mono_get_runtime_build_info (void)
6165 if (mono_build_date
)
6166 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
6168 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
6172 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
6174 GHashTable
*assemblies
= (GHashTable
*)user_data
;
6175 MonoImage
*image
= mono_assembly_get_image (ass
);
6176 MonoMethod
*method
, *invoke
;
6179 if (g_hash_table_lookup (assemblies
, ass
))
6182 g_hash_table_insert (assemblies
, ass
, ass
);
6184 if (mini_verbose
> 0)
6185 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
6187 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
6188 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
6189 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
6193 if (mini_verbose
> 1) {
6194 char * desc
= mono_method_full_name (method
, TRUE
);
6195 g_print ("Compiling %d %s\n", count
, desc
);
6198 mono_compile_method (method
);
6199 if (strcmp (method
->name
, "Finalize") == 0) {
6200 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
6201 mono_compile_method (invoke
);
6203 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
6204 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
6205 mono_compile_method (invoke
);
6209 /* Load and precompile referenced assemblies as well */
6210 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
6211 mono_assembly_load_reference (image
, i
);
6212 if (image
->references
[i
])
6213 mono_precompile_assembly (image
->references
[i
], assemblies
);
6217 void mono_precompile_assemblies ()
6219 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
6221 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
6223 g_hash_table_destroy (assemblies
);
6229 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
6230 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);
6234 mono_cfg_add_try_hole (MonoCompile
*cfg
, MonoExceptionClause
*clause
, guint8
*start
, MonoBasicBlock
*bb
)
6236 TryBlockHole
*hole
= mono_mempool_alloc (cfg
->mempool
, sizeof (TryBlockHole
));
6237 hole
->clause
= clause
;
6238 hole
->start_offset
= start
- cfg
->native_code
;
6239 hole
->basic_block
= bb
;
6241 cfg
->try_block_holes
= g_slist_append_mempool (cfg
->mempool
, cfg
->try_block_holes
, hole
);