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/metadata/runtime.h>
49 #include <mono/utils/mono-math.h>
50 #include <mono/utils/mono-compiler.h>
51 #include <mono/utils/mono-counters.h>
52 #include <mono/utils/mono-logger-internal.h>
53 #include <mono/utils/mono-mmap.h>
54 #include <mono/utils/dtrace.h>
63 #include "jit-icalls.h"
65 #include "debug-mini.h"
67 #include "debugger-agent.h"
69 static gpointer
mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
);
71 /* helper methods signature */
72 /* FIXME: Make these static again */
73 MonoMethodSignature
*helper_sig_class_init_trampoline
= NULL
;
74 MonoMethodSignature
*helper_sig_domain_get
= NULL
;
75 MonoMethodSignature
*helper_sig_generic_class_init_trampoline
= NULL
;
76 MonoMethodSignature
*helper_sig_generic_class_init_trampoline_llvm
= NULL
;
77 MonoMethodSignature
*helper_sig_rgctx_lazy_fetch_trampoline
= NULL
;
78 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline
= NULL
;
79 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline_llvm
= NULL
;
81 static guint32 default_opt
= 0;
82 static gboolean default_opt_set
= FALSE
;
84 guint32 mono_jit_tls_id
= -1;
87 static __thread gpointer mono_jit_tls MONO_TLS_FAST
;
90 MonoTraceSpec
*mono_jit_trace_calls
= NULL
;
91 gboolean mono_break_on_exc
= FALSE
;
92 gboolean mono_compile_aot
= FALSE
;
93 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
94 gboolean mono_aot_only
= FALSE
;
95 /* Whenever to use IMT */
96 #ifdef MONO_ARCH_HAVE_IMT
97 gboolean mono_use_imt
= TRUE
;
99 gboolean mono_use_imt
= FALSE
;
101 MonoMethodDesc
*mono_inject_async_exc_method
= NULL
;
102 int mono_inject_async_exc_pos
;
103 MonoMethodDesc
*mono_break_at_bb_method
= NULL
;
104 int mono_break_at_bb_bb_num
;
105 gboolean mono_do_x86_stack_align
= TRUE
;
106 const char *mono_build_date
;
107 gboolean mono_do_signal_chaining
;
108 static gboolean mono_using_xdebug
;
109 static int mini_verbose
= 0;
113 static int methods_with_llvm
, methods_without_llvm
;
117 * This flag controls whenever the runtime uses LLVM compiled code.
118 * Enabling this causes different/slower code paths to be used, which is why it
120 * Changes when this flag is set include:
121 * - a per method vtable trampoline is used to handle virtual calls, instead of only
123 * - fast generic virtual calls are not supported.
126 gboolean mono_use_llvm
= TRUE
;
128 gboolean mono_use_llvm
= FALSE
;
131 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
132 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
133 static CRITICAL_SECTION jit_mutex
;
135 static MonoCodeManager
*global_codeman
= NULL
;
137 static GHashTable
*jit_icall_name_hash
= NULL
;
139 static MonoDebugOptions debug_options
;
141 #ifdef VALGRIND_JIT_REGISTER_MAP
142 static int valgrind_register
= 0;
146 * Table written to by the debugger with a 1-based index into the
147 * mono_breakpoint_info table, which contains changes made to
148 * the JIT instructions by the debugger.
151 mono_breakpoint_info_index
[MONO_BREAKPOINT_ARRAY_SIZE
];
153 /* Whenever to check for pending exceptions in managed-to-native wrappers */
154 gboolean check_for_pending_exc
= TRUE
;
156 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
157 gboolean disable_vtypes_in_regs
= FALSE
;
159 gboolean mono_dont_free_global_codeman
;
162 mono_running_on_valgrind (void)
164 if (RUNNING_ON_VALGRIND
){
165 #ifdef VALGRIND_JIT_REGISTER_MAP
166 valgrind_register
= TRUE
;
174 MonoExceptionClause
*clause
;
175 MonoBasicBlock
*basic_block
;
185 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
187 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
190 ud
->method
= (MonoMethod
*)key
;
194 G_GNUC_UNUSED
static char*
195 get_method_from_ip (void *ip
)
200 MonoDomain
*domain
= mono_domain_get ();
201 MonoDebugSourceLocation
*location
;
202 FindTrampUserData user_data
;
204 ji
= mono_jit_info_table_find (domain
, ip
);
207 user_data
.method
= NULL
;
208 mono_domain_lock (domain
);
209 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
210 mono_domain_unlock (domain
);
211 if (user_data
.method
) {
212 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
213 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
220 method
= mono_method_full_name (ji
->method
, TRUE
);
221 /* FIXME: unused ? */
222 location
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
224 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
);
226 mono_debug_free_source_location (location
);
234 * @ip: an instruction pointer address
236 * This method is used from a debugger to get the name of the
237 * method at address @ip. This routine is typically invoked from
238 * a debugger like this:
240 * (gdb) print mono_pmip ($pc)
242 * Returns: the name of the method at address @ip.
247 return get_method_from_ip (ip
);
251 * mono_print_method_from_ip
252 * @ip: an instruction pointer address
254 * This method is used from a debugger to get the name of the
255 * method at address @ip.
257 * This prints the name of the method at address @ip in the standard
258 * output. Unlike mono_pmip which returns a string, this routine
259 * prints the value on the standard output.
262 mono_print_method_from_ip (void *ip
)
266 MonoDebugSourceLocation
*source
;
267 MonoDomain
*domain
= mono_domain_get ();
268 MonoDomain
*target_domain
= mono_domain_get ();
269 FindTrampUserData user_data
;
271 ji
= mini_jit_info_table_find (domain
, ip
, &target_domain
);
274 user_data
.method
= NULL
;
275 mono_domain_lock (domain
);
276 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
277 mono_domain_unlock (domain
);
278 if (user_data
.method
) {
279 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
280 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
284 g_print ("No method at %p\n", ip
);
287 method
= mono_method_full_name (ji
->method
, TRUE
);
288 source
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
290 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
);
293 g_print ("%s:%d\n", source
->source_file
, source
->row
);
295 mono_debug_free_source_location (source
);
300 * mono_method_same_domain:
302 * Determine whenever two compiled methods are in the same domain, thus
303 * the address of the callee can be embedded in the caller.
305 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
307 if (!caller
|| !callee
)
311 * If the call was made from domain-neutral to domain-specific
312 * code, we can't patch the call site.
314 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
317 if ((caller
->method
->klass
== mono_defaults
.appdomain_class
) &&
318 (strstr (caller
->method
->name
, "InvokeInDomain"))) {
319 /* The InvokeInDomain methods change the current appdomain */
327 * mono_global_codeman_reserve:
329 * Allocate code memory from the global code manager.
331 void *mono_global_codeman_reserve (int size
)
336 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
338 if (!global_codeman
) {
339 /* This can happen during startup */
340 global_codeman
= mono_code_manager_new ();
341 return mono_code_manager_reserve (global_codeman
, size
);
345 ptr
= mono_code_manager_reserve (global_codeman
, size
);
352 * mono_create_unwind_op:
354 * Create an unwind op with the given parameters.
357 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
359 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
370 * mono_emit_unwind_op:
372 * Add an unwind op with the given parameters for the list of unwind ops stored in
376 mono_emit_unwind_op (MonoCompile
*cfg
, int when
, int tag
, int reg
, int val
)
378 MonoUnwindOp
*op
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoUnwindOp
));
385 cfg
->unwind_ops
= g_slist_append_mempool (cfg
->mempool
, cfg
->unwind_ops
, op
);
389 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
391 MonoJumpInfoToken
*res
= mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
394 res
->has_context
= context
!= NULL
;
396 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
402 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
404 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
408 * mono_tramp_info_create:
410 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
411 * of NAME, JI, and UNWIND_OPS.
414 mono_tramp_info_create (const char *name
, guint8
*code
, guint32 code_size
, MonoJumpInfo
*ji
, GSList
*unwind_ops
)
416 MonoTrampInfo
*info
= g_new0 (MonoTrampInfo
, 1);
418 info
->name
= (char*)name
;
420 info
->code_size
= code_size
;
422 info
->unwind_ops
= unwind_ops
;
428 mono_tramp_info_free (MonoTrampInfo
*info
)
432 // FIXME: ji + unwind_ops
435 #define MONO_INIT_VARINFO(vi,id) do { \
436 (vi)->range.first_use.pos.bid = 0xffff; \
442 * mono_unlink_bblock:
444 * Unlink two basic blocks.
447 mono_unlink_bblock (MonoCompile
*cfg
, MonoBasicBlock
*from
, MonoBasicBlock
* to
)
453 for (i
= 0; i
< from
->out_count
; ++i
) {
454 if (to
== from
->out_bb
[i
]) {
461 for (i
= 0; i
< from
->out_count
; ++i
) {
462 if (from
->out_bb
[i
] != to
)
463 from
->out_bb
[pos
++] = from
->out_bb
[i
];
465 g_assert (pos
== from
->out_count
- 1);
470 for (i
= 0; i
< to
->in_count
; ++i
) {
471 if (from
== to
->in_bb
[i
]) {
478 for (i
= 0; i
< to
->in_count
; ++i
) {
479 if (to
->in_bb
[i
] != from
)
480 to
->in_bb
[pos
++] = to
->in_bb
[i
];
482 g_assert (pos
== to
->in_count
- 1);
488 * mono_bblocks_linked:
490 * Return whenever BB1 and BB2 are linked in the CFG.
493 mono_bblocks_linked (MonoBasicBlock
*bb1
, MonoBasicBlock
*bb2
)
497 for (i
= 0; i
< bb1
->out_count
; ++i
) {
498 if (bb1
->out_bb
[i
] == bb2
)
506 mono_find_block_region_notry (MonoCompile
*cfg
, int offset
)
508 MonoMethodHeader
*header
= cfg
->header
;
509 MonoExceptionClause
*clause
;
512 for (i
= 0; i
< header
->num_clauses
; ++i
) {
513 clause
= &header
->clauses
[i
];
514 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) && (offset
>= clause
->data
.filter_offset
) &&
515 (offset
< (clause
->handler_offset
)))
516 return ((i
+ 1) << 8) | MONO_REGION_FILTER
| clause
->flags
;
518 if (MONO_OFFSET_IN_HANDLER (clause
, offset
)) {
519 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
520 return ((i
+ 1) << 8) | MONO_REGION_FINALLY
| clause
->flags
;
521 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
)
522 return ((i
+ 1) << 8) | MONO_REGION_FAULT
| clause
->flags
;
524 return ((i
+ 1) << 8) | MONO_REGION_CATCH
| clause
->flags
;
532 * mono_get_block_region_notry:
534 * Return the region corresponding to REGION, ignoring try clauses nested inside
538 mono_get_block_region_notry (MonoCompile
*cfg
, int region
)
540 if ((region
& (0xf << 4)) == MONO_REGION_TRY
) {
541 MonoMethodHeader
*header
= cfg
->header
;
544 * This can happen if a try clause is nested inside a finally clause.
546 int clause_index
= (region
>> 8) - 1;
547 g_assert (clause_index
>= 0 && clause_index
< header
->num_clauses
);
549 region
= mono_find_block_region_notry (cfg
, header
->clauses
[clause_index
].try_offset
);
556 mono_find_spvar_for_region (MonoCompile
*cfg
, int region
)
558 region
= mono_get_block_region_notry (cfg
, region
);
560 return g_hash_table_lookup (cfg
->spvars
, GINT_TO_POINTER (region
));
564 df_visit (MonoBasicBlock
*start
, int *dfn
, MonoBasicBlock
**array
)
568 array
[*dfn
] = start
;
569 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
570 for (i
= 0; i
< start
->out_count
; ++i
) {
571 if (start
->out_bb
[i
]->dfn
)
574 start
->out_bb
[i
]->dfn
= *dfn
;
575 start
->out_bb
[i
]->df_parent
= start
;
576 array
[*dfn
] = start
->out_bb
[i
];
577 df_visit (start
->out_bb
[i
], dfn
, array
);
582 mono_reverse_branch_op (guint32 opcode
)
584 static const int reverse_map
[] = {
585 CEE_BNE_UN
, CEE_BLT
, CEE_BLE
, CEE_BGT
, CEE_BGE
,
586 CEE_BEQ
, CEE_BLT_UN
, CEE_BLE_UN
, CEE_BGT_UN
, CEE_BGE_UN
588 static const int reverse_fmap
[] = {
589 OP_FBNE_UN
, OP_FBLT
, OP_FBLE
, OP_FBGT
, OP_FBGE
,
590 OP_FBEQ
, OP_FBLT_UN
, OP_FBLE_UN
, OP_FBGT_UN
, OP_FBGE_UN
592 static const int reverse_lmap
[] = {
593 OP_LBNE_UN
, OP_LBLT
, OP_LBLE
, OP_LBGT
, OP_LBGE
,
594 OP_LBEQ
, OP_LBLT_UN
, OP_LBLE_UN
, OP_LBGT_UN
, OP_LBGE_UN
596 static const int reverse_imap
[] = {
597 OP_IBNE_UN
, OP_IBLT
, OP_IBLE
, OP_IBGT
, OP_IBGE
,
598 OP_IBEQ
, OP_IBLT_UN
, OP_IBLE_UN
, OP_IBGT_UN
, OP_IBGE_UN
601 if (opcode
>= CEE_BEQ
&& opcode
<= CEE_BLT_UN
) {
602 opcode
= reverse_map
[opcode
- CEE_BEQ
];
603 } else if (opcode
>= OP_FBEQ
&& opcode
<= OP_FBLT_UN
) {
604 opcode
= reverse_fmap
[opcode
- OP_FBEQ
];
605 } else if (opcode
>= OP_LBEQ
&& opcode
<= OP_LBLT_UN
) {
606 opcode
= reverse_lmap
[opcode
- OP_LBEQ
];
607 } else if (opcode
>= OP_IBEQ
&& opcode
<= OP_IBLT_UN
) {
608 opcode
= reverse_imap
[opcode
- OP_IBEQ
];
610 g_assert_not_reached ();
616 mono_type_to_store_membase (MonoCompile
*cfg
, MonoType
*type
)
619 return OP_STORE_MEMBASE_REG
;
622 switch (type
->type
) {
625 case MONO_TYPE_BOOLEAN
:
626 return OP_STOREI1_MEMBASE_REG
;
630 return OP_STOREI2_MEMBASE_REG
;
633 return OP_STOREI4_MEMBASE_REG
;
637 case MONO_TYPE_FNPTR
:
638 return OP_STORE_MEMBASE_REG
;
639 case MONO_TYPE_CLASS
:
640 case MONO_TYPE_STRING
:
641 case MONO_TYPE_OBJECT
:
642 case MONO_TYPE_SZARRAY
:
643 case MONO_TYPE_ARRAY
:
644 return OP_STORE_MEMBASE_REG
;
647 return OP_STOREI8_MEMBASE_REG
;
649 return OP_STORER4_MEMBASE_REG
;
651 return OP_STORER8_MEMBASE_REG
;
652 case MONO_TYPE_VALUETYPE
:
653 if (type
->data
.klass
->enumtype
) {
654 type
= mono_class_enum_basetype (type
->data
.klass
);
657 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
658 return OP_STOREX_MEMBASE
;
659 return OP_STOREV_MEMBASE
;
660 case MONO_TYPE_TYPEDBYREF
:
661 return OP_STOREV_MEMBASE
;
662 case MONO_TYPE_GENERICINST
:
663 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
667 /* FIXME: all the arguments must be references for now,
668 * later look inside cfg and see if the arg num is
671 g_assert (cfg
->generic_sharing_context
);
672 return OP_STORE_MEMBASE_REG
;
674 g_error ("unknown type 0x%02x in type_to_store_membase", type
->type
);
680 mono_type_to_load_membase (MonoCompile
*cfg
, MonoType
*type
)
683 return OP_LOAD_MEMBASE
;
685 type
= mono_type_get_underlying_type (type
);
687 switch (type
->type
) {
689 return OP_LOADI1_MEMBASE
;
691 case MONO_TYPE_BOOLEAN
:
692 return OP_LOADU1_MEMBASE
;
694 return OP_LOADI2_MEMBASE
;
697 return OP_LOADU2_MEMBASE
;
699 return OP_LOADI4_MEMBASE
;
701 return OP_LOADU4_MEMBASE
;
705 case MONO_TYPE_FNPTR
:
706 return OP_LOAD_MEMBASE
;
707 case MONO_TYPE_CLASS
:
708 case MONO_TYPE_STRING
:
709 case MONO_TYPE_OBJECT
:
710 case MONO_TYPE_SZARRAY
:
711 case MONO_TYPE_ARRAY
:
712 return OP_LOAD_MEMBASE
;
715 return OP_LOADI8_MEMBASE
;
717 return OP_LOADR4_MEMBASE
;
719 return OP_LOADR8_MEMBASE
;
720 case MONO_TYPE_VALUETYPE
:
721 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
722 return OP_LOADX_MEMBASE
;
723 case MONO_TYPE_TYPEDBYREF
:
724 return OP_LOADV_MEMBASE
;
725 case MONO_TYPE_GENERICINST
:
726 if (mono_type_generic_inst_is_valuetype (type
))
727 return OP_LOADV_MEMBASE
;
729 return OP_LOAD_MEMBASE
;
733 /* FIXME: all the arguments must be references for now,
734 * later look inside cfg and see if the arg num is
737 g_assert (cfg
->generic_sharing_context
);
738 return OP_LOAD_MEMBASE
;
740 g_error ("unknown type 0x%02x in type_to_load_membase", type
->type
);
746 mini_type_to_ldind (MonoCompile
* cfg
, MonoType
*type
)
748 if (cfg
->generic_sharing_context
&& !type
->byref
) {
749 /* FIXME: all the arguments must be references for now,
750 * later look inside cfg and see if the arg num is
753 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
754 return CEE_LDIND_REF
;
756 return mono_type_to_ldind (type
);
760 mini_type_to_stind (MonoCompile
* cfg
, MonoType
*type
)
762 if (cfg
->generic_sharing_context
&& !type
->byref
) {
763 /* FIXME: all the arguments must be references for now,
764 * later look inside cfg and see if the arg num is
767 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
768 return CEE_STIND_REF
;
770 return mono_type_to_stind (type
);
774 mono_op_imm_to_op (int opcode
)
778 #if SIZEOF_REGISTER == 4
794 #if SIZEOF_REGISTER == 4
800 #if SIZEOF_REGISTER == 4
806 #if SIZEOF_REGISTER == 4
844 #if SIZEOF_REGISTER == 4
850 #if SIZEOF_REGISTER == 4
869 case OP_ICOMPARE_IMM
:
871 case OP_LOCALLOC_IMM
:
874 printf ("%s\n", mono_inst_name (opcode
));
875 g_assert_not_reached ();
881 * mono_decompose_op_imm:
883 * Replace the OP_.._IMM INS with its non IMM variant.
886 mono_decompose_op_imm (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
890 MONO_INST_NEW (cfg
, temp
, OP_ICONST
);
891 temp
->inst_c0
= ins
->inst_imm
;
892 temp
->dreg
= mono_alloc_ireg (cfg
);
893 mono_bblock_insert_before_ins (bb
, ins
, temp
);
894 ins
->opcode
= mono_op_imm_to_op (ins
->opcode
);
895 if (ins
->opcode
== OP_LOCALLOC
)
896 ins
->sreg1
= temp
->dreg
;
898 ins
->sreg2
= temp
->dreg
;
900 bb
->max_vreg
= MAX (bb
->max_vreg
, cfg
->next_vreg
);
904 set_vreg_to_inst (MonoCompile
*cfg
, int vreg
, MonoInst
*inst
)
906 if (vreg
>= cfg
->vreg_to_inst_len
) {
907 MonoInst
**tmp
= cfg
->vreg_to_inst
;
908 int size
= cfg
->vreg_to_inst_len
;
910 while (vreg
>= cfg
->vreg_to_inst_len
)
911 cfg
->vreg_to_inst_len
= cfg
->vreg_to_inst_len
? cfg
->vreg_to_inst_len
* 2 : 32;
912 cfg
->vreg_to_inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
*) * cfg
->vreg_to_inst_len
);
914 memcpy (cfg
->vreg_to_inst
, tmp
, size
* sizeof (MonoInst
*));
916 cfg
->vreg_to_inst
[vreg
] = inst
;
919 #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)))
920 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
925 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
933 mono_compile_create_var_for_vreg (MonoCompile
*cfg
, MonoType
*type
, int opcode
, int vreg
)
936 int num
= cfg
->num_varinfo
;
939 if ((num
+ 1) >= cfg
->varinfo_count
) {
940 int orig_count
= cfg
->varinfo_count
;
941 cfg
->varinfo_count
= cfg
->varinfo_count
? (cfg
->varinfo_count
* 2) : 64;
942 cfg
->varinfo
= (MonoInst
**)g_realloc (cfg
->varinfo
, sizeof (MonoInst
*) * cfg
->varinfo_count
);
943 cfg
->vars
= (MonoMethodVar
*)g_realloc (cfg
->vars
, sizeof (MonoMethodVar
) * cfg
->varinfo_count
);
944 memset (&cfg
->vars
[orig_count
], 0, (cfg
->varinfo_count
- orig_count
) * sizeof (MonoMethodVar
));
947 mono_jit_stats
.allocate_var
++;
949 MONO_INST_NEW (cfg
, inst
, opcode
);
951 inst
->inst_vtype
= type
;
952 inst
->klass
= mono_class_from_mono_type (type
);
953 type_to_eval_stack_type (cfg
, type
, inst
);
954 /* if set to 1 the variable is native */
955 inst
->backend
.is_pinvoke
= 0;
958 cfg
->varinfo
[num
] = inst
;
960 MONO_INIT_VARINFO (&cfg
->vars
[num
], num
);
961 MONO_VARINFO (cfg
, num
)->vreg
= vreg
;
964 set_vreg_to_inst (cfg
, vreg
, inst
);
966 #if SIZEOF_REGISTER == 4
967 #ifdef MONO_ARCH_SOFT_FLOAT
968 regpair
= mono_type_is_long (type
) || mono_type_is_float (type
);
970 regpair
= mono_type_is_long (type
);
980 * These two cannot be allocated using create_var_for_vreg since that would
981 * put it into the cfg->varinfo array, confusing many parts of the JIT.
985 * Set flags to VOLATILE so SSA skips it.
988 if (cfg
->verbose_level
>= 4) {
989 printf (" Create LVAR R%d (R%d, R%d)\n", inst
->dreg
, inst
->dreg
+ 1, inst
->dreg
+ 2);
992 #ifdef MONO_ARCH_SOFT_FLOAT
993 if (cfg
->opt
& MONO_OPT_SSA
) {
994 if (mono_type_is_float (type
))
995 inst
->flags
= MONO_INST_VOLATILE
;
999 /* Allocate a dummy MonoInst for the first vreg */
1000 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
1001 tree
->dreg
= inst
->dreg
+ 1;
1002 if (cfg
->opt
& MONO_OPT_SSA
)
1003 tree
->flags
= MONO_INST_VOLATILE
;
1004 tree
->inst_c0
= num
;
1005 tree
->type
= STACK_I4
;
1006 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1007 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1009 set_vreg_to_inst (cfg
, inst
->dreg
+ 1, tree
);
1011 /* Allocate a dummy MonoInst for the second vreg */
1012 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
1013 tree
->dreg
= inst
->dreg
+ 2;
1014 if (cfg
->opt
& MONO_OPT_SSA
)
1015 tree
->flags
= MONO_INST_VOLATILE
;
1016 tree
->inst_c0
= num
;
1017 tree
->type
= STACK_I4
;
1018 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1019 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1021 set_vreg_to_inst (cfg
, inst
->dreg
+ 2, tree
);
1025 if (cfg
->verbose_level
> 2)
1026 g_print ("created temp %d (R%d) of type %s\n", num
, vreg
, mono_type_get_name (type
));
1031 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1035 if (mono_type_is_long (type
))
1036 dreg
= mono_alloc_dreg (cfg
, STACK_I8
);
1037 #ifdef MONO_ARCH_SOFT_FLOAT
1038 else if (mono_type_is_float (type
))
1039 dreg
= mono_alloc_dreg (cfg
, STACK_R8
);
1042 /* All the others are unified */
1043 dreg
= mono_alloc_preg (cfg
);
1045 return mono_compile_create_var_for_vreg (cfg
, type
, opcode
, dreg
);
1049 * Transform a MonoInst into a load from the variable of index var_index.
1052 mono_compile_make_var_load (MonoCompile
*cfg
, MonoInst
*dest
, gssize var_index
) {
1053 memset (dest
, 0, sizeof (MonoInst
));
1054 dest
->inst_i0
= cfg
->varinfo
[var_index
];
1055 dest
->opcode
= mini_type_to_ldind (cfg
, dest
->inst_i0
->inst_vtype
);
1056 type_to_eval_stack_type (cfg
, dest
->inst_i0
->inst_vtype
, dest
);
1057 dest
->klass
= dest
->inst_i0
->klass
;
1063 type_from_stack_type (MonoInst
*ins
) {
1064 switch (ins
->type
) {
1065 case STACK_I4
: return &mono_defaults
.int32_class
->byval_arg
;
1066 case STACK_I8
: return &mono_defaults
.int64_class
->byval_arg
;
1067 case STACK_PTR
: return &mono_defaults
.int_class
->byval_arg
;
1068 case STACK_R8
: return &mono_defaults
.double_class
->byval_arg
;
1071 * this if used to be commented without any specific reason, but
1072 * it breaks #80235 when commented
1075 return &ins
->klass
->this_arg
;
1077 return &mono_defaults
.object_class
->this_arg
;
1079 /* ins->klass may not be set for ldnull.
1080 * Also, if we have a boxed valuetype, we want an object lass,
1081 * not the valuetype class
1083 if (ins
->klass
&& !ins
->klass
->valuetype
)
1084 return &ins
->klass
->byval_arg
;
1085 return &mono_defaults
.object_class
->byval_arg
;
1086 case STACK_VTYPE
: return &ins
->klass
->byval_arg
;
1088 g_error ("stack type %d to montype not handled\n", ins
->type
);
1094 mono_type_from_stack_type (MonoInst
*ins
) {
1095 return type_from_stack_type (ins
);
1099 * mono_add_ins_to_end:
1101 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
1104 mono_add_ins_to_end (MonoBasicBlock
*bb
, MonoInst
*inst
)
1109 MONO_ADD_INS (bb
, inst
);
1113 switch (bb
->last_ins
->opcode
) {
1127 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1130 if (MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1131 /* Need to insert the ins before the compare */
1132 if (bb
->code
== bb
->last_ins
) {
1133 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1137 if (bb
->code
->next
== bb
->last_ins
) {
1138 /* Only two instructions */
1139 opcode
= bb
->code
->opcode
;
1141 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
)) {
1143 mono_bblock_insert_before_ins (bb
, bb
->code
, inst
);
1145 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1148 opcode
= bb
->last_ins
->prev
->opcode
;
1150 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
)) {
1152 mono_bblock_insert_before_ins (bb
, bb
->last_ins
->prev
, inst
);
1154 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1159 MONO_ADD_INS (bb
, inst
);
1165 mono_create_jump_table (MonoCompile
*cfg
, MonoInst
*label
, MonoBasicBlock
**bbs
, int num_blocks
)
1167 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
1168 MonoJumpInfoBBTable
*table
;
1170 table
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfoBBTable
));
1172 table
->table_size
= num_blocks
;
1174 ji
->ip
.label
= label
;
1175 ji
->type
= MONO_PATCH_INFO_SWITCH
;
1176 ji
->data
.table
= table
;
1177 ji
->next
= cfg
->patch_info
;
1178 cfg
->patch_info
= ji
;
1181 static MonoMethodSignature
*
1182 mono_get_array_new_va_signature (int arity
)
1184 static GHashTable
*sighash
= NULL
;
1185 MonoMethodSignature
*res
;
1190 sighash
= g_hash_table_new (NULL
, NULL
);
1192 else if ((res
= g_hash_table_lookup (sighash
, GINT_TO_POINTER (arity
)))) {
1197 res
= mono_metadata_signature_alloc (mono_defaults
.corlib
, arity
+ 1);
1200 #ifdef MONO_ARCH_VARARG_ICALLS
1201 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1202 res
->call_convention
= MONO_CALL_VARARG
;
1206 res
->call_convention
= MONO_CALL_C
;
1209 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
1210 for (i
= 0; i
< arity
; i
++)
1211 res
->params
[i
+ 1] = &mono_defaults
.int_class
->byval_arg
;
1213 res
->ret
= &mono_defaults
.object_class
->byval_arg
;
1215 g_hash_table_insert (sighash
, GINT_TO_POINTER (arity
), res
);
1222 mono_get_array_new_va_icall (int rank
)
1224 MonoMethodSignature
*esig
;
1225 char icall_name
[256];
1227 MonoJitICallInfo
*info
;
1229 /* Need to register the icall so it gets an icall wrapper */
1230 sprintf (icall_name
, "ves_array_new_va_%d", rank
);
1233 info
= mono_find_jit_icall_by_name (icall_name
);
1235 esig
= mono_get_array_new_va_signature (rank
);
1236 name
= g_strdup (icall_name
);
1237 info
= mono_register_jit_icall (mono_array_new_va
, name
, esig
, FALSE
);
1239 g_hash_table_insert (jit_icall_name_hash
, name
, name
);
1247 mini_class_is_system_array (MonoClass
*klass
)
1249 if (klass
->parent
== mono_defaults
.array_class
)
1256 mini_assembly_can_skip_verification (MonoDomain
*domain
, MonoMethod
*method
)
1258 MonoAssembly
*assembly
= method
->klass
->image
->assembly
;
1259 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1261 if (assembly
->in_gac
|| assembly
->image
== mono_defaults
.corlib
)
1263 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE
)
1265 return mono_assembly_has_skip_verification (assembly
);
1269 * mini_method_verify:
1271 * Verify the method using the new verfier.
1273 * Returns true if the method is invalid.
1276 mini_method_verify (MonoCompile
*cfg
, MonoMethod
*method
)
1279 gboolean is_fulltrust
;
1280 MonoLoaderError
*error
;
1282 if (method
->verification_success
)
1285 is_fulltrust
= mono_verifier_is_method_full_trust (method
);
1287 if (!mono_verifier_is_enabled_for_method (method
))
1290 res
= mono_method_verify_with_current_settings (method
, cfg
->skip_visibility
);
1292 if ((error
= mono_loader_get_last_error ())) {
1293 cfg
->exception_type
= error
->exception_type
;
1295 mono_free_verify_list (res
);
1300 for (tmp
= res
; tmp
; tmp
= tmp
->next
) {
1301 MonoVerifyInfoExtended
*info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1302 if (info
->info
.status
== MONO_VERIFY_ERROR
) {
1303 char *method_name
= mono_method_full_name (method
, TRUE
);
1304 cfg
->exception_type
= info
->exception_type
;
1305 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1306 mono_free_verify_list (res
);
1307 g_free (method_name
);
1310 if (info
->info
.status
== MONO_VERIFY_NOT_VERIFIABLE
&& (!is_fulltrust
|| info
->exception_type
== MONO_EXCEPTION_METHOD_ACCESS
|| info
->exception_type
== MONO_EXCEPTION_FIELD_ACCESS
)) {
1311 char *method_name
= mono_method_full_name (method
, TRUE
);
1312 cfg
->exception_type
= info
->exception_type
;
1313 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1314 mono_free_verify_list (res
);
1315 g_free (method_name
);
1319 mono_free_verify_list (res
);
1321 method
->verification_success
= 1;
1325 /*Returns true is something went wrong*/
1327 mono_compile_is_broken (MonoCompile
*cfg
)
1329 MonoMethod
*method
= cfg
->method
;
1330 MonoMethod
*method_definition
= method
;
1331 gboolean dont_verify
= mini_assembly_can_skip_verification (cfg
->domain
, method
);
1332 dont_verify
|= method
->klass
->image
->assembly
->corlib_internal
;
1334 while (method_definition
->is_inflated
) {
1335 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method_definition
;
1336 method_definition
= imethod
->declaring
;
1339 return !dont_verify
&& mini_method_verify (cfg
, method_definition
);
1343 create_helper_signature (void)
1345 helper_sig_domain_get
= mono_create_icall_signature ("ptr");
1346 helper_sig_class_init_trampoline
= mono_create_icall_signature ("void");
1347 helper_sig_generic_class_init_trampoline
= mono_create_icall_signature ("void");
1348 helper_sig_generic_class_init_trampoline_llvm
= mono_create_icall_signature ("void ptr");
1349 helper_sig_rgctx_lazy_fetch_trampoline
= mono_create_icall_signature ("ptr ptr");
1350 helper_sig_monitor_enter_exit_trampoline
= mono_create_icall_signature ("void");
1351 helper_sig_monitor_enter_exit_trampoline_llvm
= mono_create_icall_signature ("void object");
1354 static gconstpointer
1355 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
1358 MonoMethod
*wrapper
;
1359 gconstpointer trampoline
;
1360 MonoDomain
*domain
= mono_get_root_domain ();
1362 if (callinfo
->wrapper
) {
1363 return callinfo
->wrapper
;
1366 if (callinfo
->trampoline
)
1367 return callinfo
->trampoline
;
1370 * We use the lock on the root domain instead of the JIT lock to protect
1371 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
1373 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
1374 mono_domain_lock (domain
);
1376 if (callinfo
->trampoline
) {
1377 mono_domain_unlock (domain
);
1378 mono_loader_unlock ();
1379 return callinfo
->trampoline
;
1382 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
1383 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_for_pending_exc
);
1387 trampoline
= mono_compile_method (wrapper
);
1389 trampoline
= mono_create_ftnptr (domain
, mono_create_jit_trampoline_in_domain (domain
, wrapper
));
1390 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
1392 callinfo
->trampoline
= trampoline
;
1394 mono_domain_unlock (domain
);
1395 mono_loader_unlock ();
1397 return callinfo
->trampoline
;
1401 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
1403 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
1407 mono_dynamic_code_hash_insert (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*ji
)
1409 if (!domain_jit_info (domain
)->dynamic_code_hash
)
1410 domain_jit_info (domain
)->dynamic_code_hash
= g_hash_table_new (NULL
, NULL
);
1411 g_hash_table_insert (domain_jit_info (domain
)->dynamic_code_hash
, method
, ji
);
1414 static MonoJitDynamicMethodInfo
*
1415 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
1417 MonoJitDynamicMethodInfo
*res
;
1419 if (domain_jit_info (domain
)->dynamic_code_hash
)
1420 res
= g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1428 GList
*active
, *inactive
;
1433 compare_by_interval_start_pos_func (gconstpointer a
, gconstpointer b
)
1435 MonoMethodVar
*v1
= (MonoMethodVar
*)a
;
1436 MonoMethodVar
*v2
= (MonoMethodVar
*)b
;
1440 else if (v1
->interval
->range
&& v2
->interval
->range
)
1441 return v1
->interval
->range
->from
- v2
->interval
->range
->from
;
1442 else if (v1
->interval
->range
)
1451 #define LSCAN_DEBUG(a) do { a; } while (0)
1453 #define LSCAN_DEBUG(a)
1457 mono_allocate_stack_slots_full2 (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1459 int i
, slot
, offset
, size
;
1464 GList
*vars
= NULL
, *l
, *unhandled
;
1465 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1468 gboolean reuse_slot
;
1470 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg
->method
, TRUE
)));
1472 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1473 vtype_stack_slots
= NULL
;
1476 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1477 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1480 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1481 inst
= cfg
->varinfo
[i
];
1482 vmv
= MONO_VARINFO (cfg
, i
);
1484 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1487 vars
= g_list_prepend (vars
, vmv
);
1490 vars
= g_list_sort (g_list_copy (vars
), compare_by_interval_start_pos_func
);
1495 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1496 MonoMethodVar *current = unhandled->data;
1498 if (current->interval->range) {
1499 g_assert (current->interval->range->from >= i);
1500 i = current->interval->range->from;
1507 for (unhandled
= vars
; unhandled
; unhandled
= unhandled
->next
) {
1508 MonoMethodVar
*current
= unhandled
->data
;
1511 inst
= cfg
->varinfo
[vmv
->idx
];
1513 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1514 * pinvoke wrappers when they call functions returning structures */
1515 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1516 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1521 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1524 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1529 if (cfg
->disable_reuse_stack_slots
)
1532 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1534 case MONO_TYPE_GENERICINST
:
1535 if (!mono_type_generic_inst_is_valuetype (t
)) {
1536 slot_info
= &scalar_stack_slots
[t
->type
];
1540 case MONO_TYPE_VALUETYPE
:
1541 if (!vtype_stack_slots
)
1542 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1543 for (i
= 0; i
< nvtypes
; ++i
)
1544 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1547 slot_info
= &vtype_stack_slots
[i
];
1549 g_assert (nvtypes
< 256);
1550 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1551 slot_info
= &vtype_stack_slots
[nvtypes
];
1554 if (cfg
->disable_reuse_ref_stack_slots
)
1561 #if SIZEOF_REGISTER == 4
1567 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1573 case MONO_TYPE_CLASS
:
1574 case MONO_TYPE_OBJECT
:
1575 case MONO_TYPE_ARRAY
:
1576 case MONO_TYPE_SZARRAY
:
1577 case MONO_TYPE_STRING
:
1578 /* Share non-float stack slots of the same size */
1579 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1580 if (cfg
->disable_reuse_ref_stack_slots
)
1585 slot_info
= &scalar_stack_slots
[t
->type
];
1589 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1593 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1595 if (!current
->interval
->range
) {
1596 if (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
1600 inst
->flags
|= MONO_INST_IS_DEAD
;
1605 pos
= current
->interval
->range
->from
;
1607 LSCAN_DEBUG (printf ("process R%d ", inst
->dreg
));
1608 if (current
->interval
->range
)
1609 LSCAN_DEBUG (mono_linterval_print (current
->interval
));
1610 LSCAN_DEBUG (printf ("\n"));
1612 /* Check for intervals in active which expired or inactive */
1614 /* FIXME: Optimize this */
1617 for (l
= slot_info
->active
; l
!= NULL
; l
= l
->next
) {
1618 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1620 if (v
->interval
->last_range
->to
< pos
) {
1621 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1622 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[v
->idx
]));
1623 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1627 else if (!mono_linterval_covers (v
->interval
, pos
)) {
1628 slot_info
->inactive
= g_list_append (slot_info
->inactive
, v
);
1629 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1630 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg
->varinfo
[v
->idx
]->dreg
));
1637 /* Check for intervals in inactive which expired or active */
1639 /* FIXME: Optimize this */
1642 for (l
= slot_info
->inactive
; l
!= NULL
; l
= l
->next
) {
1643 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1645 if (v
->interval
->last_range
->to
< pos
) {
1646 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1647 // FIXME: Enabling this seems to cause impossible to debug crashes
1648 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1649 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1653 else if (mono_linterval_covers (v
->interval
, pos
)) {
1654 slot_info
->active
= g_list_append (slot_info
->active
, v
);
1655 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1656 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg
->varinfo
[v
->idx
]->dreg
));
1664 * This also handles the case when the variable is used in an
1665 * exception region, as liveness info is not computed there.
1668 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1671 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1672 if (slot_info
->slots
) {
1673 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1675 slot_info
->slots
= slot_info
->slots
->next
;
1678 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1680 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1686 static int count
= 0;
1689 if (count
== atoi (getenv ("COUNT3")))
1690 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1691 if (count
> atoi (getenv ("COUNT3")))
1694 mono_print_ins (inst
);
1699 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst
->dreg
, mono_type_full_name (t
), slot
));
1704 if (slot
== 0xffffff) {
1706 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1707 * efficient copying (and to work around the fact that OP_MEMCPY
1708 * and OP_MEMSET ignores alignment).
1710 if (MONO_TYPE_ISSTRUCT (t
)) {
1711 align
= MAX (align
, sizeof (gpointer
));
1712 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1717 offset
+= align
- 1;
1718 offset
&= ~(align
- 1);
1722 offset
+= align
- 1;
1723 offset
&= ~(align
- 1);
1728 if (*stack_align
== 0)
1729 *stack_align
= align
;
1732 offsets
[vmv
->idx
] = slot
;
1735 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1736 if (scalar_stack_slots
[i
].active
)
1737 g_list_free (scalar_stack_slots
[i
].active
);
1739 for (i
= 0; i
< nvtypes
; ++i
) {
1740 if (vtype_stack_slots
[i
].active
)
1741 g_list_free (vtype_stack_slots
[i
].active
);
1744 mono_jit_stats
.locals_stack_size
+= offset
;
1746 *stack_size
= offset
;
1751 * mono_allocate_stack_slots_full:
1753 * Allocate stack slots for all non register allocated variables using a
1754 * linear scan algorithm.
1755 * Returns: an array of stack offsets.
1756 * STACK_SIZE is set to the amount of stack space needed.
1757 * STACK_ALIGN is set to the alignment needed by the locals area.
1760 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1762 int i
, slot
, offset
, size
;
1767 GList
*vars
= NULL
, *l
;
1768 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1771 gboolean reuse_slot
;
1773 if ((cfg
->num_varinfo
> 0) && MONO_VARINFO (cfg
, 0)->interval
)
1774 return mono_allocate_stack_slots_full2 (cfg
, backward
, stack_size
, stack_align
);
1776 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1777 vtype_stack_slots
= NULL
;
1780 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1781 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1784 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1785 inst
= cfg
->varinfo
[i
];
1786 vmv
= MONO_VARINFO (cfg
, i
);
1788 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1791 vars
= g_list_prepend (vars
, vmv
);
1794 vars
= mono_varlist_sort (cfg
, vars
, 0);
1796 *stack_align
= sizeof (gpointer
);
1797 for (l
= vars
; l
; l
= l
->next
) {
1799 inst
= cfg
->varinfo
[vmv
->idx
];
1801 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1802 * pinvoke wrappers when they call functions returning structures */
1803 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1804 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1808 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1811 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1816 if (cfg
->disable_reuse_stack_slots
)
1819 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1821 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1824 case MONO_TYPE_GENERICINST
:
1825 if (!mono_type_generic_inst_is_valuetype (t
)) {
1826 slot_info
= &scalar_stack_slots
[t
->type
];
1830 case MONO_TYPE_VALUETYPE
:
1831 if (!vtype_stack_slots
)
1832 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1833 for (i
= 0; i
< nvtypes
; ++i
)
1834 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1837 slot_info
= &vtype_stack_slots
[i
];
1839 g_assert (nvtypes
< 256);
1840 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1841 slot_info
= &vtype_stack_slots
[nvtypes
];
1844 if (cfg
->disable_reuse_ref_stack_slots
)
1851 #if SIZEOF_REGISTER == 4
1857 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1863 case MONO_TYPE_CLASS
:
1864 case MONO_TYPE_OBJECT
:
1865 case MONO_TYPE_ARRAY
:
1866 case MONO_TYPE_SZARRAY
:
1867 case MONO_TYPE_STRING
:
1868 /* Share non-float stack slots of the same size */
1869 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1870 if (cfg
->disable_reuse_ref_stack_slots
)
1875 slot_info
= &scalar_stack_slots
[t
->type
];
1880 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1881 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1883 /* expire old intervals in active */
1884 while (slot_info
->active
) {
1885 MonoMethodVar
*amv
= (MonoMethodVar
*)slot_info
->active
->data
;
1887 if (amv
->range
.last_use
.abs_pos
> vmv
->range
.first_use
.abs_pos
)
1890 //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);
1892 slot_info
->active
= g_list_delete_link (slot_info
->active
, slot_info
->active
);
1893 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[amv
->idx
]));
1897 * This also handles the case when the variable is used in an
1898 * exception region, as liveness info is not computed there.
1901 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1904 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1905 if (slot_info
->slots
) {
1906 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1908 slot_info
->slots
= slot_info
->slots
->next
;
1911 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1916 static int count
= 0;
1920 if (count == atoi (getenv ("COUNT")))
1921 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1922 if (count > atoi (getenv ("COUNT")))
1925 mono_print_ins (inst);
1933 if (slot
== 0xffffff) {
1935 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1936 * efficient copying (and to work around the fact that OP_MEMCPY
1937 * and OP_MEMSET ignores alignment).
1939 if (MONO_TYPE_ISSTRUCT (t
)) {
1940 align
= MAX (align
, sizeof (gpointer
));
1941 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1943 * Align the size too so the code generated for passing vtypes in
1944 * registers doesn't overwrite random locals.
1946 size
= (size
+ (align
- 1)) & ~(align
-1);
1951 offset
+= align
- 1;
1952 offset
&= ~(align
- 1);
1956 offset
+= align
- 1;
1957 offset
&= ~(align
- 1);
1962 *stack_align
= MAX (*stack_align
, align
);
1965 offsets
[vmv
->idx
] = slot
;
1968 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1969 if (scalar_stack_slots
[i
].active
)
1970 g_list_free (scalar_stack_slots
[i
].active
);
1972 for (i
= 0; i
< nvtypes
; ++i
) {
1973 if (vtype_stack_slots
[i
].active
)
1974 g_list_free (vtype_stack_slots
[i
].active
);
1977 mono_jit_stats
.locals_stack_size
+= offset
;
1979 *stack_size
= offset
;
1986 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1988 g_assert_not_reached ();
1992 #endif /* DISABLE_JIT */
1995 mono_allocate_stack_slots (MonoCompile
*m
, guint32
*stack_size
, guint32
*stack_align
)
1997 return mono_allocate_stack_slots_full (m
, TRUE
, stack_size
, stack_align
);
2000 #define EMUL_HIT_SHIFT 3
2001 #define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
2002 /* small hit bitmap cache */
2003 static mono_byte emul_opcode_hit_cache
[(OP_LAST
>>EMUL_HIT_SHIFT
) + 1] = {0};
2004 static short emul_opcode_num
= 0;
2005 static short emul_opcode_alloced
= 0;
2006 static short *emul_opcode_opcodes
= NULL
;
2007 static MonoJitICallInfo
**emul_opcode_map
= NULL
;
2010 mono_find_jit_opcode_emulation (int opcode
)
2012 g_assert (opcode
>= 0 && opcode
<= OP_LAST
);
2013 if (emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] & (1 << (opcode
& EMUL_HIT_MASK
))) {
2015 for (i
= 0; i
< emul_opcode_num
; ++i
) {
2016 if (emul_opcode_opcodes
[i
] == opcode
)
2017 return emul_opcode_map
[i
];
2024 mono_register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, gboolean no_throw
)
2026 MonoJitICallInfo
*info
;
2027 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
2029 g_assert (!sig
->hasthis
);
2030 g_assert (sig
->param_count
< 3);
2032 info
= mono_register_jit_icall (func
, name
, sig
, no_throw
);
2034 if (emul_opcode_num
>= emul_opcode_alloced
) {
2035 int incr
= emul_opcode_alloced
? emul_opcode_alloced
/2: 16;
2036 emul_opcode_alloced
+= incr
;
2037 emul_opcode_map
= g_realloc (emul_opcode_map
, sizeof (emul_opcode_map
[0]) * emul_opcode_alloced
);
2038 emul_opcode_opcodes
= g_realloc (emul_opcode_opcodes
, sizeof (emul_opcode_opcodes
[0]) * emul_opcode_alloced
);
2040 emul_opcode_map
[emul_opcode_num
] = info
;
2041 emul_opcode_opcodes
[emul_opcode_num
] = opcode
;
2043 emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] |= (1 << (opcode
& EMUL_HIT_MASK
));
2047 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
2049 MonoMethodSignature
*sig
;
2052 sig
= mono_create_icall_signature (sigstr
);
2056 mono_register_jit_icall (func
, name
, sig
, save
);
2060 print_dfn (MonoCompile
*cfg
) {
2066 g_print ("IR code for method %s\n", mono_method_full_name (cfg
->method
, TRUE
));
2068 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
2069 bb
= cfg
->bblocks
[i
];
2070 /*if (bb->cil_code) {
2071 char* code1, *code2;
2072 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
2073 if (bb->last_ins->cil_code)
2074 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
2076 code2 = g_strdup ("");
2078 code1 [strlen (code1) - 1] = 0;
2079 code = g_strdup_printf ("%s -> %s", code1, code2);
2083 code
= g_strdup ("\n");
2084 g_print ("\nBB%d (%d) (len: %d): %s", bb
->block_num
, i
, bb
->cil_length
, code
);
2085 MONO_BB_FOR_EACH_INS (bb
, c
) {
2086 mono_print_ins_index (-1, c
);
2089 g_print ("\tprev:");
2090 for (j
= 0; j
< bb
->in_count
; ++j
) {
2091 g_print (" BB%d", bb
->in_bb
[j
]->block_num
);
2093 g_print ("\t\tsucc:");
2094 for (j
= 0; j
< bb
->out_count
; ++j
) {
2095 g_print (" BB%d", bb
->out_bb
[j
]->block_num
);
2097 g_print ("\n\tidom: BB%d\n", bb
->idom
? bb
->idom
->block_num
: -1);
2100 g_assert (mono_bitset_test_fast (bb
->dominators
, bb
->idom
->dfn
));
2103 mono_blockset_print (cfg
, bb
->dominators
, "\tdominators", bb
->idom
? bb
->idom
->dfn
: -1);
2105 mono_blockset_print (cfg
, bb
->dfrontier
, "\tdfrontier", -1);
2113 mono_bblock_add_inst (MonoBasicBlock
*bb
, MonoInst
*inst
)
2115 MONO_ADD_INS (bb
, inst
);
2119 mono_bblock_insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2123 bb
->code
= ins_to_insert
;
2125 /* Link with next */
2126 ins_to_insert
->next
= ins
;
2128 ins
->prev
= ins_to_insert
;
2130 if (bb
->last_ins
== NULL
)
2131 bb
->last_ins
= ins_to_insert
;
2133 /* Link with next */
2134 ins_to_insert
->next
= ins
->next
;
2136 ins
->next
->prev
= ins_to_insert
;
2138 /* Link with previous */
2139 ins
->next
= ins_to_insert
;
2140 ins_to_insert
->prev
= ins
;
2142 if (bb
->last_ins
== ins
)
2143 bb
->last_ins
= ins_to_insert
;
2148 mono_bblock_insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2152 bb
->code
= ins_to_insert
;
2153 ins_to_insert
->next
= ins
;
2154 if (bb
->last_ins
== NULL
)
2155 bb
->last_ins
= ins_to_insert
;
2157 /* Link with previous */
2159 ins
->prev
->next
= ins_to_insert
;
2160 ins_to_insert
->prev
= ins
->prev
;
2162 /* Link with next */
2163 ins
->prev
= ins_to_insert
;
2164 ins_to_insert
->next
= ins
;
2166 if (bb
->code
== ins
)
2167 bb
->code
= ins_to_insert
;
2172 * mono_verify_bblock:
2174 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2177 mono_verify_bblock (MonoBasicBlock
*bb
)
2179 MonoInst
*ins
, *prev
;
2182 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2183 g_assert (ins
->prev
== prev
);
2187 g_assert (!bb
->last_ins
->next
);
2193 * Perform consistency checks on the JIT data structures and the IR
2196 mono_verify_cfg (MonoCompile
*cfg
)
2200 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2201 mono_verify_bblock (bb
);
2205 mono_destroy_compile (MonoCompile
*cfg
)
2210 mono_metadata_free_mh (cfg
->header
);
2211 //mono_mempool_stats (cfg->mempool);
2212 mono_free_loop_info (cfg
);
2214 mono_regstate_free (cfg
->rs
);
2216 g_hash_table_destroy (cfg
->spvars
);
2218 g_hash_table_destroy (cfg
->exvars
);
2219 for (l
= cfg
->headers_to_free
; l
; l
= l
->next
)
2220 mono_metadata_free_mh (l
->data
);
2221 g_list_free (cfg
->ldstr_list
);
2222 g_hash_table_destroy (cfg
->token_info_hash
);
2223 if (cfg
->abs_patches
)
2224 g_hash_table_destroy (cfg
->abs_patches
);
2225 mono_mempool_destroy (cfg
->mempool
);
2227 g_free (cfg
->varinfo
);
2229 g_free (cfg
->exception_message
);
2233 #ifdef HAVE_KW_THREAD
2234 static __thread gpointer mono_lmf_addr MONO_TLS_FAST
;
2235 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
2237 * When this is defined, the current lmf is stored in this tls variable instead of in
2240 static __thread gpointer mono_lmf MONO_TLS_FAST
;
2245 mono_get_jit_tls_key (void)
2247 return mono_jit_tls_id
;
2251 mono_get_jit_tls_offset (void)
2253 #ifdef HAVE_KW_THREAD
2255 MONO_THREAD_VAR_OFFSET (mono_jit_tls
, offset
);
2263 mono_get_lmf_tls_offset (void)
2265 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2267 MONO_THREAD_VAR_OFFSET(mono_lmf
,offset
);
2275 mono_get_lmf_addr_tls_offset (void)
2278 MONO_THREAD_VAR_OFFSET(mono_lmf_addr
,offset
);
2285 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2288 MonoJitTlsData
*jit_tls
;
2290 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2291 return jit_tls
->lmf
;
2293 g_assert_not_reached ();
2299 mono_get_lmf_addr (void)
2301 #ifdef HAVE_KW_THREAD
2302 return mono_lmf_addr
;
2304 MonoJitTlsData
*jit_tls
;
2306 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2307 return &jit_tls
->lmf
;
2310 * When resolving the call to mono_jit_thread_attach full-aot will look
2311 * in the plt, which causes a call into the generic trampoline, which in turn
2312 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
2313 * call mono_jit_thread_attach from the native-to-managed wrapper, without
2314 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
2317 mono_jit_thread_attach (NULL
);
2319 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2320 return &jit_tls
->lmf
;
2322 g_assert_not_reached ();
2328 mono_set_lmf (MonoLMF
*lmf
)
2330 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2334 (*mono_get_lmf_addr ()) = lmf
;
2337 /* Called by native->managed wrappers */
2339 mono_jit_thread_attach (MonoDomain
*domain
)
2343 * Happens when called from AOTed code which is only used in the root
2346 domain
= mono_get_root_domain ();
2348 #ifdef HAVE_KW_THREAD
2349 if (!mono_lmf_addr
) {
2350 mono_thread_attach (domain
);
2353 if (!TlsGetValue (mono_jit_tls_id
))
2354 mono_thread_attach (domain
);
2356 if (mono_domain_get () != domain
)
2357 mono_domain_set (domain
, TRUE
);
2361 * mono_thread_abort:
2362 * @obj: exception object
2364 * abort the thread, print exception information and stack trace
2367 mono_thread_abort (MonoObject
*obj
)
2369 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
2371 /* handle_remove should be eventually called for this thread, too
2374 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
2375 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
2376 mono_thread_exit ();
2378 exit (mono_environment_exitcode_get ());
2383 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
2385 MonoJitTlsData
*jit_tls
;
2388 jit_tls
= TlsGetValue (mono_jit_tls_id
);
2392 jit_tls
= g_new0 (MonoJitTlsData
, 1);
2394 TlsSetValue (mono_jit_tls_id
, jit_tls
);
2396 #ifdef HAVE_KW_THREAD
2397 mono_jit_tls
= jit_tls
;
2400 jit_tls
->abort_func
= abort_func
;
2401 jit_tls
->end_of_stack
= stack_start
;
2403 lmf
= g_new0 (MonoLMF
, 1);
2404 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
2405 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
2410 jit_tls
->first_lmf
= lmf
;
2412 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2413 /* jit_tls->lmf is unused */
2415 mono_lmf_addr
= &mono_lmf
;
2417 #if defined(HAVE_KW_THREAD)
2418 mono_lmf_addr
= &jit_tls
->lmf
;
2424 mono_arch_setup_jit_tls_data (jit_tls
);
2425 mono_setup_altstack (jit_tls
);
2431 mono_thread_start_cb (intptr_t tid
, gpointer stack_start
, gpointer func
)
2433 MonoInternalThread
*thread
;
2434 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
2435 thread
= mono_thread_internal_current ();
2436 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, jit_tls
, func
);
2438 thread
->jit_data
= jit_tls
;
2441 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
2444 mono_thread_abort_dummy (MonoObject
*obj
)
2446 if (mono_thread_attach_aborted_cb
)
2447 mono_thread_attach_aborted_cb (obj
);
2449 mono_thread_abort (obj
);
2453 mono_thread_attach_cb (intptr_t tid
, gpointer stack_start
)
2455 MonoInternalThread
*thread
;
2456 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
2457 thread
= mono_thread_internal_current ();
2458 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, (MonoJitTlsData
*) jit_tls
, NULL
);
2460 thread
->jit_data
= jit_tls
;
2461 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
2462 mono_runtime_setup_stat_profiler ();
2466 mini_thread_cleanup (MonoThread
*thread
)
2468 MonoInternalThread
*internal
= thread
->internal_thread
;
2469 MonoJitTlsData
*jit_tls
= internal
->jit_data
;
2472 mono_debugger_thread_cleanup (jit_tls
);
2473 mono_arch_free_jit_tls_data (jit_tls
);
2475 mono_free_altstack (jit_tls
);
2476 g_free (jit_tls
->first_lmf
);
2478 internal
->jit_data
= NULL
;
2480 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
2481 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
2482 * not a trivial thing.
2484 * The current offender is mono_thread_manage which cleanup threads from the outside.
2486 if (internal
== mono_thread_internal_current ()) {
2487 TlsSetValue (mono_jit_tls_id
, NULL
);
2489 #ifdef HAVE_KW_THREAD
2490 mono_jit_tls
= NULL
;
2491 mono_lmf_addr
= NULL
;
2492 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2501 mono_create_tls_get (MonoCompile
*cfg
, int offset
)
2503 #ifdef MONO_ARCH_HAVE_TLS_GET
2504 if (MONO_ARCH_HAVE_TLS_GET
) {
2510 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2511 ins
->dreg
= mono_alloc_preg (cfg
);
2512 ins
->inst_offset
= offset
;
2520 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2522 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2526 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2528 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2532 mono_get_thread_intrinsic (MonoCompile
* cfg
)
2534 return mono_create_tls_get (cfg
, mono_thread_get_tls_offset ());
2538 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2540 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2544 ji
->data
.target
= target
;
2545 ji
->next
= cfg
->patch_info
;
2547 cfg
->patch_info
= ji
;
2551 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2553 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2557 ji
->data
.target
= target
;
2564 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2566 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2569 if ((*ji
)->ip
.i
== ip
)
2572 ji
= &((*ji
)->next
);
2577 * mono_patch_info_dup_mp:
2579 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2582 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2584 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2585 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2587 switch (patch_info
->type
) {
2588 case MONO_PATCH_INFO_RVA
:
2589 case MONO_PATCH_INFO_LDSTR
:
2590 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2591 case MONO_PATCH_INFO_LDTOKEN
:
2592 case MONO_PATCH_INFO_DECLSEC
:
2593 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2594 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2596 case MONO_PATCH_INFO_SWITCH
:
2597 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2598 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2599 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2600 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2602 case MONO_PATCH_INFO_RGCTX_FETCH
:
2603 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2604 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2605 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2615 mono_patch_info_hash (gconstpointer data
)
2617 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2620 case MONO_PATCH_INFO_RVA
:
2621 case MONO_PATCH_INFO_LDSTR
:
2622 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2623 case MONO_PATCH_INFO_LDTOKEN
:
2624 case MONO_PATCH_INFO_DECLSEC
:
2625 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2626 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2627 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2628 case MONO_PATCH_INFO_VTABLE
:
2629 case MONO_PATCH_INFO_CLASS
:
2630 case MONO_PATCH_INFO_IID
:
2631 case MONO_PATCH_INFO_ADJUSTED_IID
:
2632 case MONO_PATCH_INFO_CLASS_INIT
:
2633 case MONO_PATCH_INFO_METHODCONST
:
2634 case MONO_PATCH_INFO_METHOD
:
2635 case MONO_PATCH_INFO_METHOD_JUMP
:
2636 case MONO_PATCH_INFO_IMAGE
:
2637 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2638 case MONO_PATCH_INFO_FIELD
:
2639 case MONO_PATCH_INFO_SFLDA
:
2640 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2641 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2643 return (ji
->type
<< 8);
2648 * mono_patch_info_equal:
2650 * This might fail to recognize equivalent patches, i.e. floats, so its only
2651 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2655 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2657 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2658 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2660 if (ji1
->type
!= ji2
->type
)
2663 switch (ji1
->type
) {
2664 case MONO_PATCH_INFO_RVA
:
2665 case MONO_PATCH_INFO_LDSTR
:
2666 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2667 case MONO_PATCH_INFO_LDTOKEN
:
2668 case MONO_PATCH_INFO_DECLSEC
:
2669 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2670 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2671 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2672 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2673 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2676 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2677 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2679 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2680 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
2681 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
2683 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
);
2686 if (ji1
->data
.target
!= ji2
->data
.target
)
2695 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
2697 unsigned char *ip
= patch_info
->ip
.i
+ code
;
2698 gconstpointer target
= NULL
;
2700 switch (patch_info
->type
) {
2701 case MONO_PATCH_INFO_BB
:
2703 * FIXME: This could be hit for methods without a prolog. Should use -1
2704 * but too much code depends on a 0 initial value.
2706 //g_assert (patch_info->data.bb->native_offset);
2707 target
= patch_info
->data
.bb
->native_offset
+ code
;
2709 case MONO_PATCH_INFO_ABS
:
2710 target
= patch_info
->data
.target
;
2712 case MONO_PATCH_INFO_LABEL
:
2713 target
= patch_info
->data
.inst
->inst_c0
+ code
;
2715 case MONO_PATCH_INFO_IP
:
2718 case MONO_PATCH_INFO_METHOD_REL
:
2719 target
= code
+ patch_info
->data
.offset
;
2721 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
2722 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2724 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
2725 g_assert_not_reached ();
2727 target
= mono_icall_get_wrapper (mi
);
2730 case MONO_PATCH_INFO_METHOD_JUMP
:
2731 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
2733 case MONO_PATCH_INFO_METHOD
:
2734 if (patch_info
->data
.method
== method
) {
2737 /* get the trampoline to the method from the domain */
2738 target
= mono_create_jit_trampoline (patch_info
->data
.method
);
2741 case MONO_PATCH_INFO_SWITCH
: {
2742 gpointer
*jump_table
;
2745 if (method
&& method
->dynamic
) {
2746 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2748 if (mono_aot_only
) {
2749 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2751 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2755 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++)
2756 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
2757 target
= jump_table
;
2760 case MONO_PATCH_INFO_METHODCONST
:
2761 case MONO_PATCH_INFO_CLASS
:
2762 case MONO_PATCH_INFO_IMAGE
:
2763 case MONO_PATCH_INFO_FIELD
:
2764 target
= patch_info
->data
.target
;
2766 case MONO_PATCH_INFO_IID
:
2767 mono_class_init (patch_info
->data
.klass
);
2768 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
2770 case MONO_PATCH_INFO_ADJUSTED_IID
:
2771 mono_class_init (patch_info
->data
.klass
);
2772 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
2774 case MONO_PATCH_INFO_VTABLE
:
2775 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2778 case MONO_PATCH_INFO_CLASS_INIT
: {
2779 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2782 target
= mono_create_class_init_trampoline (vtable
);
2785 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
2786 target
= mono_create_delegate_trampoline (patch_info
->data
.klass
);
2788 case MONO_PATCH_INFO_SFLDA
: {
2789 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
2791 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
2792 gpointer addr
= NULL
;
2794 mono_domain_lock (domain
);
2795 if (domain
->special_static_fields
)
2796 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
2797 mono_domain_unlock (domain
);
2803 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
2804 /* Done by the generated code */
2808 mono_runtime_class_init (vtable
);
2810 target
= (char*)vtable
->data
+ patch_info
->data
.field
->offset
;
2813 case MONO_PATCH_INFO_RVA
: {
2814 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
2817 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
2818 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
2821 case MONO_PATCH_INFO_R4
:
2822 case MONO_PATCH_INFO_R8
:
2823 target
= patch_info
->data
.target
;
2825 case MONO_PATCH_INFO_EXC_NAME
:
2826 target
= patch_info
->data
.name
;
2828 case MONO_PATCH_INFO_LDSTR
:
2830 mono_ldstr (domain
, patch_info
->data
.token
->image
,
2831 mono_metadata_token_index (patch_info
->data
.token
->token
));
2833 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
2835 MonoClass
*handle_class
;
2837 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2838 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
2839 mono_class_init (handle_class
);
2840 mono_class_init (mono_class_from_mono_type (handle
));
2843 mono_type_get_object (domain
, handle
);
2846 case MONO_PATCH_INFO_LDTOKEN
: {
2848 MonoClass
*handle_class
;
2850 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2851 patch_info
->data
.token
->token
, &handle_class
, NULL
);
2852 mono_class_init (handle_class
);
2857 case MONO_PATCH_INFO_DECLSEC
:
2858 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
2860 case MONO_PATCH_INFO_ICALL_ADDR
:
2861 /* run_cctors == 0 -> AOT */
2862 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2864 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, NULL
, NULL
);
2866 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
));
2871 target
= mono_lookup_internal_call (patch_info
->data
.method
);
2873 if (!target
&& run_cctors
)
2874 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2877 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
2878 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2880 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
2881 g_assert_not_reached ();
2886 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
2887 target
= mono_thread_interruption_request_flag ();
2889 case MONO_PATCH_INFO_METHOD_RGCTX
: {
2890 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
2893 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
2896 case MONO_PATCH_INFO_BB_OVF
:
2897 case MONO_PATCH_INFO_EXC_OVF
:
2898 case MONO_PATCH_INFO_GOT_OFFSET
:
2899 case MONO_PATCH_INFO_NONE
:
2901 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2902 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
2905 switch (entry
->data
->type
) {
2906 case MONO_PATCH_INFO_CLASS
:
2907 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
));
2909 case MONO_PATCH_INFO_METHOD
:
2910 case MONO_PATCH_INFO_METHODCONST
:
2911 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
));
2913 case MONO_PATCH_INFO_FIELD
:
2914 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
));
2917 g_assert_not_reached ();
2921 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
2924 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
2925 target
= mono_create_generic_class_init_trampoline ();
2927 case MONO_PATCH_INFO_MONITOR_ENTER
:
2928 target
= mono_create_monitor_enter_trampoline ();
2930 case MONO_PATCH_INFO_MONITOR_EXIT
:
2931 target
= mono_create_monitor_exit_trampoline ();
2933 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2934 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2936 /* AOT, not needed */
2939 target
= mono_arch_get_seq_point_info (domain
, code
);
2942 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
2943 #ifdef MONO_ARCH_LLVM_SUPPORTED
2944 g_assert (mono_use_llvm
);
2945 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
2947 g_assert_not_reached ();
2951 g_assert_not_reached ();
2954 return (gpointer
)target
;
2958 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
2960 ins
->inst_offset
= native_offset
;
2961 g_ptr_array_add (cfg
->seq_points
, ins
);
2962 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
2963 bb
->last_seq_point
= ins
;
2969 mono_compile_create_vars (MonoCompile
*cfg
)
2971 MonoMethodSignature
*sig
;
2972 MonoMethodHeader
*header
;
2975 header
= cfg
->header
;
2977 sig
= mono_method_signature (cfg
->method
);
2979 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
2980 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
2981 /* Inhibit optimizations */
2982 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
2984 if (cfg
->verbose_level
> 2)
2985 g_print ("creating vars\n");
2987 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
2990 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
2992 for (i
= 0; i
< sig
->param_count
; ++i
) {
2993 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
2996 if (cfg
->verbose_level
> 2) {
2998 printf ("\treturn : ");
2999 mono_print_ins (cfg
->ret
);
3003 printf ("\tthis: ");
3004 mono_print_ins (cfg
->args
[0]);
3007 for (i
= 0; i
< sig
->param_count
; ++i
) {
3008 printf ("\targ [%d]: ", i
);
3009 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
3013 cfg
->locals_start
= cfg
->num_varinfo
;
3014 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
3016 if (cfg
->verbose_level
> 2)
3017 g_print ("creating locals\n");
3019 for (i
= 0; i
< header
->num_locals
; ++i
)
3020 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
3022 if (cfg
->verbose_level
> 2)
3023 g_print ("locals done\n");
3025 mono_arch_create_vars (cfg
);
3028 #endif /* #ifndef DISABLE_JIT */
3031 mono_print_code (MonoCompile
*cfg
, const char* msg
)
3035 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3036 mono_print_bb (bb
, msg
);
3042 mono_postprocess_patches (MonoCompile
*cfg
)
3044 MonoJumpInfo
*patch_info
;
3047 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3048 switch (patch_info
->type
) {
3049 case MONO_PATCH_INFO_ABS
: {
3050 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
3053 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
3057 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
3058 // FIXME: CLEAN UP THIS MESS.
3059 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
3060 strstr (cfg
->method
->name
, info
->name
)) {
3062 * This is an icall wrapper, and this is a call to the
3065 if (cfg
->compile_aot
) {
3066 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
3067 patch_info
->data
.name
= info
->name
;
3070 /* for these array methods we currently register the same function pointer
3071 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
3072 * will return the incorrect one depending on the order they are registered.
3073 * See tests/test-arr.cs
3075 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
3076 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3077 patch_info
->data
.name
= info
->name
;
3082 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
3083 if (cfg
->abs_patches
) {
3084 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
3086 patch_info
->type
= abs_ji
->type
;
3087 patch_info
->data
.target
= abs_ji
->data
.target
;
3094 case MONO_PATCH_INFO_SWITCH
: {
3096 if (cfg
->method
->dynamic
) {
3097 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3099 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3102 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3103 /* Might be NULL if the switch is eliminated */
3104 if (patch_info
->data
.table
->table
[i
]) {
3105 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
3106 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
3111 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
3114 case MONO_PATCH_INFO_METHOD_JUMP
: {
3116 MonoDomain
*domain
= cfg
->domain
;
3117 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
3119 mono_domain_lock (domain
);
3120 if (!domain_jit_info (domain
)->jump_target_hash
)
3121 domain_jit_info (domain
)->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3122 list
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3123 list
= g_slist_prepend (list
, ip
);
3124 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, list
);
3125 mono_domain_unlock (domain
);
3136 collect_pred_seq_points (MonoBasicBlock
*bb
, MonoInst
*ins
, GSList
**next
, int depth
)
3139 MonoBasicBlock
*in_bb
;
3141 for (i
= 0; i
< bb
->in_count
; ++i
) {
3142 in_bb
= bb
->in_bb
[i
];
3144 if (in_bb
->last_seq_point
) {
3145 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
));
3147 /* Have to look at its predecessors */
3149 collect_pred_seq_points (in_bb
, ins
, next
, depth
+ 1);
3155 mono_save_seq_point_info (MonoCompile
*cfg
)
3158 GSList
*bb_seq_points
, *l
;
3160 MonoDomain
*domain
= cfg
->domain
;
3162 MonoSeqPointInfo
*info
;
3165 if (!cfg
->seq_points
)
3168 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3169 info
->len
= cfg
->seq_points
->len
;
3170 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3171 SeqPoint
*sp
= &info
->seq_points
[i
];
3172 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3174 sp
->il_offset
= ins
->inst_imm
;
3175 sp
->native_offset
= ins
->inst_offset
;
3178 ins
->backend
.size
= i
;
3182 * For each sequence point, compute the list of sequence points immediately
3183 * following it, this is needed to implement 'step over' in the debugger agent.
3185 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3186 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3187 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3189 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3190 MonoInst
*ins
= l
->data
;
3192 if (!(ins
->flags
& MONO_INST_SINGLE_STEP_LOC
))
3196 /* Link with the previous seq point in the same bb */
3197 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3199 /* Link with the last bb in the previous bblocks */
3200 collect_pred_seq_points (bb
, ins
, next
, 0);
3207 if (cfg
->verbose_level
> 2) {
3208 printf ("\nSEQ POINT MAP: \n");
3211 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3212 SeqPoint
*sp
= &info
->seq_points
[i
];
3216 sp
->next_len
= g_slist_length (next
[i
]);
3217 sp
->next
= g_new (int, sp
->next_len
);
3219 if (cfg
->verbose_level
> 2 && next
[i
]) {
3220 printf ("\t0x%x ->", sp
->il_offset
);
3221 for (l
= next
[i
]; l
; l
= l
->next
) {
3222 next_index
= GPOINTER_TO_UINT (l
->data
);
3223 printf (" 0x%x", info
->seq_points
[next_index
].il_offset
);
3227 for (l
= next
[i
]; l
; l
= l
->next
) {
3228 next_index
= GPOINTER_TO_UINT (l
->data
);
3229 sp
->next
[j
++] = next_index
;
3231 g_slist_free (next
[i
]);
3235 cfg
->seq_point_info
= info
;
3237 // FIXME: dynamic methods
3238 mono_domain_lock (domain
);
3239 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3240 mono_domain_unlock (domain
);
3242 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3243 cfg
->seq_points
= NULL
;
3247 mono_codegen (MonoCompile
*cfg
)
3250 int max_epilog_size
;
3253 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3254 cfg
->spill_count
= 0;
3255 /* we reuse dfn here */
3256 /* bb->dfn = bb_count++; */
3258 mono_arch_lowering_pass (cfg
, bb
);
3260 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3261 mono_arch_peephole_pass_1 (cfg
, bb
);
3264 mono_local_regalloc (cfg
, bb
);
3266 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3267 mono_arch_peephole_pass_2 (cfg
, bb
);
3270 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3271 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3273 code
= mono_arch_emit_prolog (cfg
);
3275 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3276 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3278 cfg
->code_len
= code
- cfg
->native_code
;
3279 cfg
->prolog_end
= cfg
->code_len
;
3281 mono_debug_open_method (cfg
);
3283 /* emit code all basic blocks */
3284 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3285 bb
->native_offset
= cfg
->code_len
;
3286 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3287 mono_arch_output_basic_block (cfg
, bb
);
3288 bb
->native_length
= cfg
->code_len
- bb
->native_offset
;
3290 if (bb
== cfg
->bb_exit
) {
3291 cfg
->epilog_begin
= cfg
->code_len
;
3293 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3294 code
= cfg
->native_code
+ cfg
->code_len
;
3295 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3296 cfg
->code_len
= code
- cfg
->native_code
;
3297 g_assert (cfg
->code_len
< cfg
->code_size
);
3300 mono_arch_emit_epilog (cfg
);
3304 mono_arch_emit_exceptions (cfg
);
3306 max_epilog_size
= 0;
3308 code
= cfg
->native_code
+ cfg
->code_len
;
3310 /* we always allocate code in cfg->domain->code_mp to increase locality */
3311 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3312 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3314 if (cfg
->method
->dynamic
) {
3315 guint unwindlen
= 0;
3316 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3317 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3319 /* Allocate the code into a separate memory pool so it can be freed */
3320 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3321 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3322 mono_domain_lock (cfg
->domain
);
3323 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3324 mono_domain_unlock (cfg
->domain
);
3326 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3328 guint unwindlen
= 0;
3329 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3330 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3332 code
= mono_domain_code_reserve (cfg
->domain
, cfg
->code_size
+ unwindlen
);
3335 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3336 g_free (cfg
->native_code
);
3337 cfg
->native_code
= code
;
3338 code
= cfg
->native_code
+ cfg
->code_len
;
3340 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3341 mono_postprocess_patches (cfg
);
3343 #ifdef VALGRIND_JIT_REGISTER_MAP
3344 if (valgrind_register
){
3345 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3346 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3351 if (cfg
->verbose_level
> 0) {
3352 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3353 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3355 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3360 gboolean is_generic
= FALSE
;
3362 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3363 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3367 if (cfg
->generic_sharing_context
)
3368 g_assert (is_generic
);
3371 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3372 mono_arch_save_unwind_info (cfg
);
3375 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->run_cctors
);
3377 if (cfg
->method
->dynamic
) {
3378 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3380 mono_domain_code_commit (cfg
->domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3382 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3384 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3386 mono_debug_close_method (cfg
);
3388 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3389 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3394 compute_reachable (MonoBasicBlock
*bb
)
3398 if (!(bb
->flags
& BB_VISITED
)) {
3399 bb
->flags
|= BB_VISITED
;
3400 for (i
= 0; i
< bb
->out_count
; ++i
)
3401 compute_reachable (bb
->out_bb
[i
]);
3406 create_jit_info (MonoCompile
*cfg
, MonoMethod
*method_to_compile
)
3409 MonoMethodHeader
*header
;
3412 int generic_info_size
;
3413 int holes_size
= 0, num_holes
= 0;
3415 g_assert (method_to_compile
== cfg
->method
);
3416 header
= cfg
->header
;
3418 if (cfg
->generic_sharing_context
)
3419 generic_info_size
= sizeof (MonoGenericJitInfo
);
3421 generic_info_size
= 0;
3423 if (cfg
->try_block_holes
) {
3424 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3425 TryBlockHole
*hole
= tmp
->data
;
3426 MonoExceptionClause
*ec
= hole
->clause
;
3427 int hole_end
= hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
;
3428 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3429 g_assert (clause_last_bb
);
3431 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3432 if (clause_last_bb
->native_offset
!= hole_end
)
3436 holes_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3437 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3438 printf ("Number of try block holes %d\n", num_holes
);
3441 if (COMPILE_LLVM (cfg
))
3442 num_clauses
= cfg
->llvm_ex_info_len
;
3444 num_clauses
= header
->num_clauses
;
3446 if (cfg
->method
->dynamic
) {
3447 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3448 generic_info_size
+ holes_size
);
3450 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
3451 (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
3452 generic_info_size
+ holes_size
);
3455 jinfo
->method
= cfg
->method_to_register
;
3456 jinfo
->code_start
= cfg
->native_code
;
3457 jinfo
->code_size
= cfg
->code_len
;
3458 jinfo
->used_regs
= cfg
->used_int_regs
;
3459 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
3460 jinfo
->cas_inited
= FALSE
; /* initialization delayed at the first stalk walk using this method */
3461 jinfo
->num_clauses
= num_clauses
;
3462 if (COMPILE_LLVM (cfg
))
3463 jinfo
->from_llvm
= TRUE
;
3465 if (cfg
->generic_sharing_context
) {
3467 MonoGenericJitInfo
*gi
;
3469 jinfo
->has_generic_jit_info
= 1;
3471 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3474 gi
->generic_sharing_context
= cfg
->generic_sharing_context
;
3476 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3477 mini_method_get_context (method_to_compile
)->method_inst
||
3478 method_to_compile
->klass
->valuetype
) {
3479 g_assert (cfg
->rgctx_var
);
3484 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
3485 mini_method_get_context (method_to_compile
)->method_inst
||
3486 method_to_compile
->klass
->valuetype
) {
3487 inst
= cfg
->rgctx_var
;
3488 if (!COMPILE_LLVM (cfg
))
3489 g_assert (inst
->opcode
== OP_REGOFFSET
);
3491 inst
= cfg
->args
[0];
3494 if (COMPILE_LLVM (cfg
)) {
3495 g_assert (cfg
->llvm_this_reg
!= -1);
3496 gi
->this_in_reg
= 0;
3497 gi
->this_reg
= cfg
->llvm_this_reg
;
3498 gi
->this_offset
= cfg
->llvm_this_offset
;
3499 } else if (inst
->opcode
== OP_REGVAR
) {
3500 gi
->this_in_reg
= 1;
3501 gi
->this_reg
= inst
->dreg
;
3503 g_assert (inst
->opcode
== OP_REGOFFSET
);
3505 g_assert (inst
->inst_basereg
== X86_EBP
);
3506 #elif defined(TARGET_AMD64)
3507 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
3509 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
3511 gi
->this_in_reg
= 0;
3512 gi
->this_reg
= inst
->inst_basereg
;
3513 gi
->this_offset
= inst
->inst_offset
;
3518 MonoTryBlockHoleTableJitInfo
*table
;
3521 jinfo
->has_try_block_holes
= 1;
3522 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3523 table
->num_holes
= (guint16
)num_holes
;
3525 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3526 guint32 start_bb_offset
;
3527 MonoTryBlockHoleJitInfo
*hole
;
3528 TryBlockHole
*hole_data
= tmp
->data
;
3529 MonoExceptionClause
*ec
= hole_data
->clause
;
3530 int hole_end
= hole_data
->basic_block
->native_offset
+ hole_data
->basic_block
->native_length
;
3531 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3532 g_assert (clause_last_bb
);
3534 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3535 if (clause_last_bb
->native_offset
== hole_end
)
3538 start_bb_offset
= hole_data
->start_offset
- hole_data
->basic_block
->native_offset
;
3539 hole
= &table
->holes
[i
++];
3540 hole
->clause
= hole_data
->clause
- &header
->clauses
[0];
3541 hole
->offset
= (guint32
)hole_data
->start_offset
;
3542 hole
->length
= (guint16
)(hole_data
->basic_block
->native_length
- start_bb_offset
);
3544 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3545 printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole
->clause
, hole
->offset
, hole
->length
);
3547 g_assert (i
== num_holes
);
3550 if (COMPILE_LLVM (cfg
)) {
3552 memcpy (&jinfo
->clauses
[0], &cfg
->llvm_ex_info
[0], num_clauses
* sizeof (MonoJitExceptionInfo
));
3553 } else if (header
->num_clauses
) {
3556 for (i
= 0; i
< header
->num_clauses
; i
++) {
3557 MonoExceptionClause
*ec
= &header
->clauses
[i
];
3558 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3559 MonoBasicBlock
*tblock
;
3562 ei
->flags
= ec
->flags
;
3564 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
3565 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
3567 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
3568 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
3570 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
3572 ei
->data
.catch_class
= ec
->data
.catch_class
;
3575 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
3577 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
3578 g_assert (tblock
->native_offset
);
3579 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3581 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
3582 g_assert (tblock
->native_offset
);
3583 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
3585 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
3587 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3588 TryBlockHole
*hole
= tmp
->data
;
3589 gpointer hole_end
= cfg
->native_code
+ (hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
3590 if (hole
->clause
== ec
&& hole_end
== ei
->try_end
) {
3591 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3592 printf ("\tShortening try block %d from %x to %x\n", i
, (int)((guint8
*)ei
->try_end
- cfg
->native_code
), hole
->start_offset
);
3594 ei
->try_end
= cfg
->native_code
+ hole
->start_offset
;
3599 if (ec
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
3601 if (ec
->handler_offset
+ ec
->handler_len
< header
->code_size
) {
3602 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
+ ec
->handler_len
];
3604 end_offset
= tblock
->native_offset
;
3606 end_offset
= cfg
->epilog_begin
;
3608 ei
->data
.handler_end
= cfg
->native_code
+ end_offset
;
3613 if (G_UNLIKELY (cfg
->verbose_level
>= 4)) {
3615 for (i
= 0; i
< jinfo
->num_clauses
; i
++) {
3616 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3617 int start
= (guint8
*)ei
->try_start
- cfg
->native_code
;
3618 int end
= (guint8
*)ei
->try_end
- cfg
->native_code
;
3619 int handler
= (guint8
*)ei
->handler_start
- cfg
->native_code
;
3621 printf ("JitInfo EH clause %d flags %x try %x-%x handler %x\n", i
, ei
->flags
, start
, end
, handler
);
3626 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
3627 * using it during runtime, hence the define.
3629 #ifdef MONO_ARCH_HAVE_XP_UNWIND
3630 if (cfg
->encoded_unwind_ops
) {
3631 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
3632 g_free (cfg
->encoded_unwind_ops
);
3633 } else if (cfg
->unwind_ops
) {
3635 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
3637 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
3638 g_free (unwind_info
);
3647 * mini_get_shared_method:
3649 * Return the method which is actually compiled/registered when doing generic sharing.
3652 mini_get_shared_method (MonoMethod
*method
)
3654 MonoGenericContext shared_context
;
3655 MonoMethod
*declaring_method
, *res
;
3657 gboolean partial
= FALSE
;
3659 if (method
->is_generic
|| method
->klass
->generic_container
)
3660 declaring_method
= method
;
3662 declaring_method
= mono_method_get_declaring_generic_method (method
);
3664 if (declaring_method
->is_generic
)
3665 shared_context
= mono_method_get_generic_container (declaring_method
)->context
;
3667 shared_context
= declaring_method
->klass
->generic_container
->context
;
3669 /* Handle partial sharing */
3670 if (method
!= declaring_method
&& method
->is_inflated
&& !mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
)) {
3671 MonoGenericContext
*context
= mono_method_get_context (method
);
3672 MonoGenericInst
*inst
;
3673 MonoType
**type_argv
;
3676 * Create the shared context by replacing the ref type arguments with
3677 * type parameters, and keeping the rest.
3680 inst
= context
->class_inst
;
3682 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3683 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3684 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
)
3685 type_argv
[i
] = shared_context
.class_inst
->type_argv
[i
];
3687 type_argv
[i
] = inst
->type_argv
[i
];
3690 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3694 inst
= context
->method_inst
;
3696 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
3697 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3698 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
)
3699 type_argv
[i
] = shared_context
.method_inst
->type_argv
[i
];
3701 type_argv
[i
] = inst
->type_argv
[i
];
3704 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
3709 res
= mono_class_inflate_generic_method (declaring_method
, &shared_context
);
3711 /* The result should be an inflated method whose parent is not inflated */
3712 g_assert (!res
->klass
->is_inflated
);
3719 * mini_method_compile:
3720 * @method: the method to compile
3721 * @opts: the optimization flags to use
3722 * @domain: the domain where the method will be compiled in
3723 * @run_cctors: whether we should run type ctors if possible
3724 * @compile_aot: whether this is an AOT compilation
3725 * @parts: debug flag
3727 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3728 * field in the returned struct to see if compilation succeded.
3731 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
3733 MonoMethodHeader
*header
;
3734 MonoMethodSignature
*sig
;
3738 int dfn
, i
, code_size_ratio
;
3739 gboolean deadce_has_run
= FALSE
;
3740 gboolean try_generic_shared
, try_llvm
;
3741 MonoMethod
*method_to_compile
, *method_to_register
;
3743 mono_jit_stats
.methods_compiled
++;
3744 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
3745 mono_profiler_method_jit (method
);
3746 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
3747 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
3751 * We might get passed the original generic method definition or
3752 * instances with type parameters.
3753 * FIXME: Remove the method->klass->generic_class limitation.
3755 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3756 (opts
& MONO_OPT_GSHARED
) && ((method
->is_generic
|| method
->klass
->generic_container
) || (!method
->klass
->generic_class
&& mono_method_is_generic_sharable_impl (method
, TRUE
)));
3758 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3759 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl (method
, FALSE
);
3761 if (opts
& MONO_OPT_GSHARED
) {
3762 if (try_generic_shared
)
3763 mono_stats
.generics_sharable_methods
++;
3764 else if (mono_method_is_generic_impl (method
))
3765 mono_stats
.generics_unsharable_methods
++;
3775 if (try_generic_shared
) {
3776 method_to_compile
= mini_get_shared_method (method
);
3777 g_assert (method_to_compile
);
3779 method_to_compile
= method
;
3782 cfg
= g_new0 (MonoCompile
, 1);
3783 cfg
->method
= method_to_compile
;
3784 cfg
->header
= mono_method_get_header (cfg
->method
);
3785 cfg
->mempool
= mono_mempool_new ();
3787 cfg
->prof_options
= mono_profiler_get_events ();
3788 cfg
->run_cctors
= run_cctors
;
3789 cfg
->domain
= domain
;
3790 cfg
->verbose_level
= mini_verbose
;
3791 cfg
->compile_aot
= compile_aot
;
3792 cfg
->skip_visibility
= method
->skip_visibility
;
3793 cfg
->orig_method
= method
;
3794 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
3795 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
3796 if (try_generic_shared
)
3797 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->generic_sharing_context
;
3798 cfg
->compile_llvm
= try_llvm
;
3799 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
3801 if (cfg
->gen_seq_points
)
3802 cfg
->seq_points
= g_ptr_array_new ();
3804 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
)) {
3805 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
3809 if (cfg
->generic_sharing_context
) {
3810 method_to_register
= method_to_compile
;
3812 g_assert (method
== method_to_compile
);
3813 method_to_register
= method
;
3815 cfg
->method_to_register
= method_to_register
;
3817 mono_error_init (&err
);
3818 sig
= mono_method_signature_checked (cfg
->method
, &err
);
3820 cfg
->exception_type
= MONO_EXCEPTION_TYPE_LOAD
;
3821 cfg
->exception_message
= g_strdup (mono_error_get_message (&err
));
3822 mono_error_cleanup (&err
);
3823 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3824 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3828 header
= cfg
->header
;
3830 MonoLoaderError
*error
;
3832 if ((error
= mono_loader_get_last_error ())) {
3833 cfg
->exception_type
= error
->exception_type
;
3835 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
3836 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
3838 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3839 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3845 static gboolean inited
;
3848 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_with_llvm
);
3849 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_without_llvm
);
3854 * Check for methods which cannot be compiled by LLVM early, to avoid
3855 * the extra compilation pass.
3857 if (COMPILE_LLVM (cfg
)) {
3858 mono_llvm_check_method_supported (cfg
);
3859 if (cfg
->disable_llvm
) {
3860 if (cfg
->verbose_level
>= 1) {
3861 //nm = mono_method_full_name (cfg->method, TRUE);
3862 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3865 InterlockedIncrement (&methods_without_llvm
);
3866 mono_destroy_compile (cfg
);
3868 goto restart_compile
;
3874 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3875 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
3876 cfg
->disable_reuse_registers
= TRUE
;
3877 cfg
->disable_reuse_stack_slots
= TRUE
;
3879 * This decreases the change the debugger will read registers/stack slots which are
3880 * not yet initialized.
3882 cfg
->disable_initlocals_opt
= TRUE
;
3884 cfg
->extend_live_ranges
= TRUE
;
3886 /* Temporarily disable this when running in the debugger until we have support
3887 * for this in the debugger. */
3888 cfg
->disable_omit_fp
= TRUE
;
3890 /* The debugger needs all locals to be on the stack or in a global register */
3891 cfg
->disable_vreg_to_lvreg
= TRUE
;
3893 /* Don't remove unused variables when running inside the debugger since the user
3894 * may still want to view them. */
3895 cfg
->disable_deadce_vars
= TRUE
;
3897 // cfg->opt |= MONO_OPT_SHARED;
3898 cfg
->opt
&= ~MONO_OPT_DEADCE
;
3899 cfg
->opt
&= ~MONO_OPT_INLINE
;
3900 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
3901 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
3902 cfg
->opt
&= ~MONO_OPT_GSHARED
;
3904 /* This is needed for the soft debugger, which doesn't like code after the epilog */
3905 cfg
->disable_out_of_line_bblocks
= TRUE
;
3908 if (mono_using_xdebug
) {
3910 * Make each variable use its own register/stack slot and extend
3911 * their liveness to cover the whole method, making them displayable
3912 * in gdb even after they are dead.
3914 cfg
->disable_reuse_registers
= TRUE
;
3915 cfg
->disable_reuse_stack_slots
= TRUE
;
3916 cfg
->extend_live_ranges
= TRUE
;
3917 cfg
->compute_precise_live_ranges
= TRUE
;
3920 mini_gc_init_gc_map (cfg
);
3922 if (COMPILE_LLVM (cfg
)) {
3923 cfg
->opt
|= MONO_OPT_ABCREM
;
3926 if (getenv ("MONO_VERBOSE_METHOD")) {
3927 char *name
= getenv ("MONO_VERBOSE_METHOD");
3929 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
3930 MonoMethodDesc
*desc
;
3932 desc
= mono_method_desc_new (name
, TRUE
);
3933 if (mono_method_desc_full_match (desc
, cfg
->method
)) {
3934 cfg
->verbose_level
= 4;
3936 mono_method_desc_free (desc
);
3938 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
3939 cfg
->verbose_level
= 4;
3943 ip
= (guint8
*)header
->code
;
3945 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
3947 if (cfg
->verbose_level
> 0) {
3948 if (COMPILE_LLVM (cfg
))
3949 g_print ("converting llvm method %s\n", mono_method_full_name (method
, TRUE
));
3950 else if (cfg
->generic_sharing_context
)
3951 g_print ("converting shared method %s\n", mono_method_full_name (method_to_compile
, TRUE
));
3953 g_print ("converting method %s\n", mono_method_full_name (method
, TRUE
));
3956 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
3957 cfg
->opt
|= MONO_OPT_SSA
;
3960 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")))
3961 cfg->globalra = TRUE;
3964 //cfg->globalra = TRUE;
3966 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3967 // cfg->globalra = TRUE;
3970 static int count
= 0;
3974 if (getenv ("COUNT2")) {
3975 cfg->globalra = TRUE;
3976 if (count == atoi (getenv ("COUNT2")))
3977 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3978 if (count > atoi (getenv ("COUNT2")))
3979 cfg->globalra = FALSE;
3984 if (header
->clauses
)
3985 cfg
->globalra
= FALSE
;
3987 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
3988 /* The code in the prolog clobbers caller saved registers */
3989 cfg
->globalra
= FALSE
;
3991 // FIXME: Disable globalra in case of tracing/profiling
3993 if (cfg
->method
->save_lmf
)
3994 /* The LMF saving code might clobber caller saved registers */
3995 cfg
->globalra
= FALSE
;
3997 if (header
->code_size
> 5000)
3999 /* Too large bblocks could overflow the ins positions */
4000 cfg
->globalra
= FALSE
;
4002 cfg
->rs
= mono_regstate_new ();
4004 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
4005 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
4007 /* FIXME: Fix SSA to handle branches inside bblocks */
4008 if (cfg
->opt
& MONO_OPT_SSA
)
4009 cfg
->enable_extended_bblocks
= FALSE
;
4012 * FIXME: This confuses liveness analysis because variables which are assigned after
4013 * a branch inside a bblock become part of the kill set, even though the assignment
4014 * might not get executed. This causes the optimize_initlocals pass to delete some
4015 * assignments which are needed.
4016 * Also, the mono_if_conversion pass needs to be modified to recognize the code
4019 //cfg->enable_extended_bblocks = TRUE;
4021 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
4022 if (mono_compile_is_broken (cfg
))
4026 * create MonoInst* which represents arguments and local variables
4028 mono_compile_create_vars (cfg
);
4030 /* SSAPRE is not supported on linear IR */
4031 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
4033 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
4036 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
4038 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4039 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4042 mono_destroy_compile (cfg
);
4043 try_generic_shared
= FALSE
;
4044 goto restart_compile
;
4046 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
4048 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4049 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4050 /* cfg contains the details of the failure, so let the caller cleanup */
4054 mono_jit_stats
.basic_blocks
+= cfg
->num_bblocks
;
4055 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->num_bblocks
, mono_jit_stats
.max_basic_blocks
);
4057 if (COMPILE_LLVM (cfg
)) {
4060 /* The IR has to be in SSA form for LLVM */
4061 cfg
->opt
|= MONO_OPT_SSA
;
4065 // Allow SSA on the result value
4066 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
4068 // Add an explicit return instruction referencing the return value
4069 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
4070 ins
->sreg1
= cfg
->ret
->dreg
;
4072 MONO_ADD_INS (cfg
->bb_exit
, ins
);
4075 cfg
->opt
&= ~MONO_OPT_LINEARS
;
4078 cfg
->opt
&= ~MONO_OPT_BRANCH
;
4081 /* todo: remove code when we have verified that the liveness for try/catch blocks
4085 * Currently, this can't be commented out since exception blocks are not
4086 * processed during liveness analysis.
4087 * It is also needed, because otherwise the local optimization passes would
4088 * delete assignments in cases like this:
4090 * <something which throws>
4092 * This also allows SSA to be run on methods containing exception clauses, since
4093 * SSA will ignore variables marked VOLATILE.
4095 mono_liveness_handle_exception_clauses (cfg
);
4097 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
4099 if (!COMPILE_LLVM (cfg
))
4100 mono_decompose_long_opts (cfg
);
4102 /* Should be done before branch opts */
4103 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
4104 mono_local_cprop (cfg
);
4106 if (cfg
->opt
& MONO_OPT_BRANCH
)
4107 mono_optimize_branches (cfg
);
4109 /* This must be done _before_ global reg alloc and _after_ decompose */
4110 mono_handle_global_vregs (cfg
);
4111 if (cfg
->opt
& MONO_OPT_DEADCE
)
4112 mono_local_deadce (cfg
);
4113 /* Disable this for LLVM to make the IR easier to handle */
4114 if (!COMPILE_LLVM (cfg
))
4115 mono_if_conversion (cfg
);
4117 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
4118 mono_remove_critical_edges (cfg
);
4120 /* Depth-first ordering on basic blocks */
4121 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4123 cfg
->max_block_num
= cfg
->num_bblocks
;
4126 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4127 if (cfg
->num_bblocks
!= dfn
+ 1) {
4130 cfg
->num_bblocks
= dfn
+ 1;
4132 /* remove unreachable code, because the code in them may be
4133 * inconsistent (access to dead variables for example) */
4134 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4135 bb
->flags
&= ~BB_VISITED
;
4136 compute_reachable (cfg
->bb_entry
);
4137 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4138 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
4139 compute_reachable (bb
);
4140 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4141 if (!(bb
->flags
& BB_VISITED
)) {
4142 if (cfg
->verbose_level
> 1)
4143 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
4144 bb
->code
= bb
->last_ins
= NULL
;
4145 while (bb
->out_count
)
4146 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
4149 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4150 bb
->flags
&= ~BB_VISITED
;
4153 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
4155 * we disable some optimizations if there are too many variables
4156 * because JIT time may become too expensive. The actual number needs
4157 * to be tweaked and eventually the non-linear algorithms should be fixed.
4159 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
4160 cfg
->disable_ssa
= TRUE
;
4163 if (cfg
->opt
& MONO_OPT_LOOP
) {
4164 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
4165 mono_compute_natural_loops (cfg
);
4168 /* after method_to_ir */
4170 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4171 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4176 if (header->num_clauses)
4177 cfg->disable_ssa = TRUE;
4180 //#define DEBUGSSA "logic_run"
4181 #define DEBUGSSA_CLASS "Tests"
4184 if (!cfg
->disable_ssa
) {
4185 mono_local_cprop (cfg
);
4188 mono_ssa_compute (cfg
);
4192 if (cfg
->opt
& MONO_OPT_SSA
) {
4193 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
4195 mono_ssa_compute (cfg
);
4198 if (cfg
->verbose_level
>= 2) {
4205 /* after SSA translation */
4207 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4208 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4212 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
4213 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4215 mono_ssa_cprop (cfg
);
4221 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4222 //mono_ssa_strength_reduction (cfg);
4224 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
4225 mono_perform_ssapre (cfg
);
4226 //mono_local_cprop (cfg);
4229 if (cfg
->opt
& MONO_OPT_DEADCE
) {
4230 mono_ssa_deadce (cfg
);
4231 deadce_has_run
= TRUE
;
4234 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4235 mono_perform_abc_removal (cfg
);
4237 mono_ssa_remove (cfg
);
4238 mono_local_cprop (cfg
);
4239 mono_handle_global_vregs (cfg
);
4240 if (cfg
->opt
& MONO_OPT_DEADCE
)
4241 mono_local_deadce (cfg
);
4243 if (cfg
->opt
& MONO_OPT_BRANCH
) {
4246 mono_optimize_branches (cfg
);
4248 /* Have to recompute cfg->bblocks and bb->dfn */
4249 if (cfg
->globalra
) {
4250 mono_remove_critical_edges (cfg
);
4252 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4255 /* Depth-first ordering on basic blocks */
4256 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4259 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4260 cfg
->num_bblocks
= dfn
+ 1;
4266 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
4267 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
4268 mono_perform_abc_removal (cfg
);
4271 /* after SSA removal */
4273 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4274 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4278 #ifdef MONO_ARCH_SOFT_FLOAT
4279 if (!COMPILE_LLVM (cfg
))
4280 mono_decompose_soft_float (cfg
);
4282 if (!COMPILE_LLVM (cfg
))
4283 mono_decompose_vtype_opts (cfg
);
4284 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4285 mono_decompose_array_access_opts (cfg
);
4288 #ifndef MONO_ARCH_GOT_REG
4293 g_assert (cfg
->got_var_allocated
);
4296 * Allways allocate the GOT var to a register, because keeping it
4297 * in memory will increase the number of live temporaries in some
4298 * code created by inssel.brg, leading to the well known spills+
4299 * branches problem. Testcase: mcs crash in
4300 * System.MonoCustomAttrs:GetCustomAttributes.
4302 #ifdef MONO_ARCH_GOT_REG
4303 got_reg
= MONO_ARCH_GOT_REG
;
4305 regs
= mono_arch_get_global_int_regs (cfg
);
4307 got_reg
= GPOINTER_TO_INT (regs
->data
);
4310 cfg
->got_var
->opcode
= OP_REGVAR
;
4311 cfg
->got_var
->dreg
= got_reg
;
4312 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
4316 * Have to call this again to process variables added since the first call.
4318 mono_liveness_handle_exception_clauses (cfg
);
4320 if (cfg
->globalra
) {
4323 /* Have to do this before regalloc since it can create vregs */
4324 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4325 mono_arch_lowering_pass (cfg
, bb
);
4327 mono_global_regalloc (cfg
);
4330 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
4331 GList
*vars
, *regs
, *l
;
4333 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
4334 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
4335 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
4336 mono_analyze_liveness (cfg
);
4338 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
4339 regs
= mono_arch_get_global_int_regs (cfg
);
4340 /* Remove the reg reserved for holding the GOT address */
4342 for (l
= regs
; l
; l
= l
->next
) {
4343 if (GPOINTER_TO_UINT (l
->data
) == cfg
->got_var
->dreg
) {
4344 regs
= g_list_delete_link (regs
, l
);
4349 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
4353 //mono_print_code (cfg, "");
4357 /* variables are allocated after decompose, since decompose could create temps */
4358 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4359 mono_arch_allocate_vars (cfg
);
4360 if (cfg
->exception_type
)
4366 gboolean need_local_opts
;
4368 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
4369 mono_spill_global_vars (cfg
, &need_local_opts
);
4371 if (need_local_opts
|| cfg
->compile_aot
) {
4372 /* To optimize code created by spill_global_vars */
4373 mono_local_cprop (cfg
);
4374 if (cfg
->opt
& MONO_OPT_DEADCE
)
4375 mono_local_deadce (cfg
);
4379 /* Add branches between non-consecutive bblocks */
4380 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4381 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
4382 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
4383 /* we are careful when inverting, since bugs like #59580
4384 * could show up when dealing with NaNs.
4386 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
4387 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
4388 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
4389 bb
->last_ins
->inst_false_bb
= tmp
;
4391 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
4393 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
4394 inst
->opcode
= OP_BR
;
4395 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
4396 mono_bblock_add_inst (bb
, inst
);
4401 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
4402 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4403 MonoInst
*tree
= bb
->code
;
4404 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
4407 for (; tree
; tree
= tree
->next
) {
4408 mono_print_ins_index (-1, tree
);
4414 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4415 bb
->max_vreg
= cfg
->next_vreg
;
4419 if (COMPILE_LLVM (cfg
)) {
4423 /* The IR has to be in SSA form for LLVM */
4424 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
4425 cfg
->exception_message
= g_strdup ("SSA disabled.");
4426 cfg
->disable_llvm
= TRUE
;
4429 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
4430 mono_decompose_array_access_opts (cfg
);
4432 if (!cfg
->disable_llvm
)
4433 mono_llvm_emit_method (cfg
);
4434 if (cfg
->disable_llvm
) {
4435 if (cfg
->verbose_level
>= 1) {
4436 //nm = mono_method_full_name (cfg->method, TRUE);
4437 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
4440 InterlockedIncrement (&methods_without_llvm
);
4441 mono_destroy_compile (cfg
);
4443 goto restart_compile
;
4446 InterlockedIncrement (&methods_with_llvm
);
4448 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
4449 nm
= mono_method_full_name (cfg
->method
, TRUE
);
4450 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
4452 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
4460 if (cfg
->verbose_level
>= 2) {
4461 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
4462 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
4466 cfg
->jit_info
= create_jit_info (cfg
, method_to_compile
);
4468 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
4469 if (cfg
->extend_live_ranges
) {
4470 /* Extend live ranges to cover the whole method */
4471 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
4472 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
4476 mono_save_xdebug_info (cfg
);
4478 mini_gc_create_gc_map (cfg
);
4480 mono_save_seq_point_info (cfg
);
4482 if (!cfg
->compile_aot
) {
4483 mono_domain_lock (cfg
->domain
);
4484 mono_jit_info_table_add (cfg
->domain
, cfg
->jit_info
);
4486 if (cfg
->method
->dynamic
)
4487 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= cfg
->jit_info
;
4488 mono_domain_unlock (cfg
->domain
);
4491 /* collect statistics */
4492 mono_perfcounters
->jit_methods
++;
4493 mono_perfcounters
->jit_bytes
+= header
->code_size
;
4494 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
4495 code_size_ratio
= cfg
->code_len
;
4496 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
4497 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
4498 g_free (mono_jit_stats
.biggest_method
);
4499 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4501 code_size_ratio
= (code_size_ratio
* 100) / header
->code_size
;
4502 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
4503 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
4504 g_free (mono_jit_stats
.max_ratio_method
);
4505 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4507 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
4509 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4510 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4518 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4520 g_assert_not_reached ();
4524 #endif /* DISABLE_JIT */
4527 mono_domain_lookup_shared_generic (MonoDomain
*domain
, MonoMethod
*method
)
4529 static gboolean inited
= FALSE
;
4530 static int lookups
= 0;
4531 static int failed_lookups
= 0;
4534 ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, mini_get_shared_method (method
));
4535 if (ji
&& !ji
->has_generic_jit_info
)
4539 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
4540 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
4552 * LOCKING: Assumes domain->jit_code_hash_lock is held.
4555 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
4557 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
4562 if (!mono_method_is_generic_sharable_impl (method
, FALSE
))
4564 return mono_domain_lookup_shared_generic (domain
, method
);
4568 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
4572 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
4573 mono_domain_jit_code_hash_lock (domain
);
4574 info
= lookup_method_inner (domain
, method
);
4575 mono_domain_jit_code_hash_unlock (domain
);
4576 mono_loader_unlock ();
4582 static FILE* perf_map_file
= NULL
;
4585 mono_enable_jit_map (void)
4587 if (!perf_map_file
) {
4589 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
4591 perf_map_file
= fopen (name
, "w");
4596 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
4599 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
4603 mono_emit_jit_map (MonoJitInfo
*jinfo
)
4605 if (perf_map_file
) {
4606 char *name
= mono_method_full_name (jinfo
->method
, TRUE
);
4607 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
4614 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
4617 gpointer code
= NULL
;
4618 MonoJitInfo
*jinfo
, *info
;
4620 MonoException
*ex
= NULL
;
4621 guint32 prof_options
;
4623 #ifdef MONO_USE_AOT_COMPILER
4624 if (opt
& MONO_OPT_AOT
) {
4625 MonoDomain
*domain
= mono_domain_get ();
4627 mono_class_init (method
->klass
);
4629 if ((code
= mono_aot_get_method (domain
, method
))) {
4630 vtable
= mono_class_vtable (domain
, method
->klass
);
4632 mono_runtime_class_init (vtable
);
4639 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4640 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
4642 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
4644 if (!piinfo
->addr
) {
4645 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
4646 piinfo
->addr
= mono_lookup_internal_call (method
);
4647 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
4649 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
);
4651 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
);
4654 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
4656 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
4657 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4659 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
4660 //mono_debug_add_wrapper (method, nm);
4661 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
4662 const char *name
= method
->name
;
4665 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
4666 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
4667 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
4670 * We need to make sure this wrapper
4671 * is compiled because it might end up
4672 * in an (M)RGCTX if generic sharing
4673 * is enabled, and would be called
4674 * indirectly. If it were a
4675 * trampoline we'd try to patch that
4676 * indirect call, which is not
4679 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
4680 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
4681 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4682 return mono_create_delegate_trampoline (method
->klass
);
4684 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
4685 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4687 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
4688 nm
= mono_marshal_get_delegate_begin_invoke (method
);
4689 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4690 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
4691 nm
= mono_marshal_get_delegate_end_invoke (method
);
4692 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4698 if (mono_aot_only
) {
4699 char *fullname
= mono_method_full_name (method
, TRUE
);
4700 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only.\n", fullname
);
4702 *jit_ex
= mono_get_exception_execution_engine (msg
);
4709 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
4711 switch (cfg
->exception_type
) {
4712 case MONO_EXCEPTION_NONE
:
4714 case MONO_EXCEPTION_TYPE_LOAD
:
4715 case MONO_EXCEPTION_MISSING_FIELD
:
4716 case MONO_EXCEPTION_MISSING_METHOD
:
4717 case MONO_EXCEPTION_FILE_NOT_FOUND
:
4718 case MONO_EXCEPTION_BAD_IMAGE
: {
4719 /* Throw a type load exception if needed */
4720 MonoLoaderError
*error
= mono_loader_get_last_error ();
4723 ex
= mono_loader_error_prepare_exception (error
);
4725 if (cfg
->exception_ptr
) {
4726 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
4728 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
4729 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
4730 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
4731 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
4732 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
4733 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
4734 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
4735 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
4736 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
4737 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
4739 g_assert_not_reached ();
4744 case MONO_EXCEPTION_INVALID_PROGRAM
:
4745 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
4747 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
4748 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
4750 case MONO_EXCEPTION_METHOD_ACCESS
:
4751 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
4753 case MONO_EXCEPTION_FIELD_ACCESS
:
4754 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
4756 /* this can only be set if the security manager is active */
4757 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
4758 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
4759 MonoObject
*exc
= NULL
;
4762 args
[0] = &cfg
->exception_data
;
4764 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
4766 ex
= (MonoException
*)exc
;
4769 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
4770 MonoException
*exp
= cfg
->exception_ptr
;
4771 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
4777 g_assert_not_reached ();
4781 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4782 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
4784 mono_destroy_compile (cfg
);
4790 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
4791 mono_domain_lock (target_domain
);
4793 /* Check if some other thread already did the job. In this case, we can
4794 discard the code this thread generated. */
4796 mono_domain_jit_code_hash_lock (target_domain
);
4798 info
= lookup_method_inner (target_domain
, method
);
4800 /* We can't use a domain specific method in another domain */
4801 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
4802 code
= info
->code_start
;
4803 // printf("Discarding code for method %s\n", method->name);
4808 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
4809 mono_domain_jit_code_hash_unlock (target_domain
);
4810 code
= cfg
->native_code
;
4812 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl (method
, FALSE
))
4813 mono_stats
.generics_shared_methods
++;
4815 mono_domain_jit_code_hash_unlock (target_domain
);
4818 jinfo
= cfg
->jit_info
;
4820 prof_options
= cfg
->prof_options
;
4822 mono_destroy_compile (cfg
);
4825 if (domain_jit_info (target_domain
)->jump_target_hash
) {
4826 MonoJumpInfo patch_info
;
4828 list
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4830 patch_info
.next
= NULL
;
4831 patch_info
.ip
.i
= 0;
4832 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
4833 patch_info
.data
.method
= method
;
4834 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4836 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
4837 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, TRUE
);
4838 g_slist_free (list
);
4841 mono_emit_jit_map (jinfo
);
4843 mono_domain_unlock (target_domain
);
4844 mono_loader_unlock ();
4846 vtable
= mono_class_vtable (target_domain
, method
->klass
);
4848 ex
= mono_class_get_exception_for_failure (method
->klass
);
4854 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
) {
4855 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
4856 if (mono_marshal_method_from_wrapper (method
)) {
4857 /* Native func wrappers have no method */
4858 /* The profiler doesn't know about wrappers, so pass the original icall method */
4859 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method
), jinfo
, MONO_PROFILE_OK
);
4862 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
4866 ex
= mono_runtime_class_init_full (vtable
, FALSE
);
4875 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
4877 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
4880 MonoJitICallInfo
*callinfo
= NULL
;
4883 * ICALL wrappers are handled specially, since there is only one copy of them
4884 * shared by all appdomains.
4886 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
4887 const char *icall_name
;
4889 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
4890 g_assert (icall_name
);
4891 callinfo
= mono_find_jit_icall_by_name (icall_name
);
4892 g_assert (callinfo
);
4894 /* Must be domain neutral since there is only one copy */
4895 opt
|= MONO_OPT_SHARED
;
4898 if (opt
& MONO_OPT_SHARED
)
4899 target_domain
= mono_get_root_domain ();
4901 target_domain
= domain
;
4903 info
= lookup_method (target_domain
, method
);
4905 /* We can't use a domain specific method in another domain */
4906 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4909 mono_jit_stats
.methods_lookups
++;
4910 vtable
= mono_class_vtable (domain
, method
->klass
);
4912 mono_runtime_class_init (vtable
);
4913 return mono_create_ftnptr (target_domain
, info
->code_start
);
4917 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
4921 p
= mono_create_ftnptr (target_domain
, code
);
4925 if (!callinfo
->wrapper
) {
4926 callinfo
->wrapper
= p
;
4927 mono_register_jit_icall_wrapper (callinfo
, p
);
4928 mono_debug_add_icall_wrapper (method
, callinfo
);
4937 mono_jit_compile_method (MonoMethod
*method
)
4939 MonoException
*ex
= NULL
;
4942 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
4945 mono_raise_exception (ex
);
4951 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4953 invalidated_delegate_trampoline (char *desc
)
4955 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
4956 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
4962 * mono_jit_free_method:
4964 * Free all memory allocated by the JIT for METHOD.
4967 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
4969 MonoJitDynamicMethodInfo
*ji
;
4970 gboolean destroy
= TRUE
;
4972 g_assert (method
->dynamic
);
4974 mono_domain_lock (domain
);
4975 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
4976 mono_domain_unlock (domain
);
4980 mono_domain_lock (domain
);
4981 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
4982 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
4983 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
4984 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
4985 mono_domain_unlock (domain
);
4987 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4988 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
4990 * Instead of freeing the code, change it to call an error routine
4991 * so people can fix their code.
4993 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
4994 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
4997 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
5003 * This needs to be done before freeing code_mp, since the code address is the
5004 * key in the table, so if we free the code_mp first, another thread can grab the
5005 * same code address and replace our entry in the table.
5007 mono_jit_info_table_remove (domain
, ji
->ji
);
5010 mono_code_manager_destroy (ji
->code_mp
);
5015 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
5017 MonoDomain
*target_domain
;
5020 if (default_opt
& MONO_OPT_SHARED
)
5021 target_domain
= mono_get_root_domain ();
5023 target_domain
= domain
;
5025 info
= lookup_method (target_domain
, method
);
5027 /* We can't use a domain specific method in another domain */
5028 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
5029 mono_jit_stats
.methods_lookups
++;
5032 return info
->code_start
;
5042 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
5044 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
5049 gpointer compiled_method
;
5050 gpointer runtime_invoke
;
5052 MonoDynCallInfo
*dyn_call_info
;
5053 MonoClass
*ret_box_class
;
5054 } RuntimeInvokeInfo
;
5057 * mono_jit_runtime_invoke:
5058 * @method: the method to invoke
5059 * @obj: this pointer
5060 * @params: array of parameter values.
5061 * @exc: used to catch exceptions objects
5064 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
5067 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
5068 MonoDomain
*domain
= mono_domain_get ();
5069 MonoJitDomainInfo
*domain_info
;
5070 RuntimeInvokeInfo
*info
, *info2
;
5072 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
5073 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
5077 domain_info
= domain_jit_info (domain
);
5079 mono_domain_lock (domain
);
5080 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5081 mono_domain_unlock (domain
);
5084 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
5086 * This might be redundant since mono_class_vtable () already does this,
5087 * but keep it just in case for moonlight.
5089 mono_class_setup_vtable (method
->klass
);
5090 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
5092 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
5094 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
5099 info
= g_new0 (RuntimeInvokeInfo
, 1);
5101 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
5102 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
5103 g_assert (info
->vtable
);
5105 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
5106 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
5108 * Array Get/Set/Address methods. The JIT implements them using inline code
5109 * inside the runtime invoke wrappers, so no need to compile them.
5111 info
->compiled_method
= NULL
;
5113 MonoException
*jit_ex
= NULL
;
5115 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
5116 if (!info
->compiled_method
) {
5120 *exc
= (MonoObject
*)jit_ex
;
5123 mono_raise_exception (jit_ex
);
5127 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
5128 info
->compiled_method
= mono_create_static_rgctx_trampoline (method
, info
->compiled_method
);
5132 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
5133 * in full-aot mode, so we use a slower, but more generic wrapper if
5134 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
5136 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5137 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
5138 MonoMethodSignature
*sig
= mono_method_signature (method
);
5139 gboolean supported
= TRUE
;
5142 if (method
->string_ctor
)
5143 sig
= mono_marshal_get_string_ctor_signature (method
);
5145 for (i
= 0; i
< sig
->param_count
; ++i
) {
5146 MonoType
*t
= sig
->params
[i
];
5148 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
5152 if (method
->klass
->contextbound
|| !info
->compiled_method
)
5156 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
5158 if (info
->dyn_call_info
) {
5159 switch (sig
->ret
->type
) {
5160 case MONO_TYPE_VOID
:
5172 case MONO_TYPE_BOOLEAN
:
5173 case MONO_TYPE_CHAR
:
5176 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5179 info
->ret_box_class
= mono_defaults
.int_class
;
5181 case MONO_TYPE_STRING
:
5182 case MONO_TYPE_CLASS
:
5183 case MONO_TYPE_ARRAY
:
5184 case MONO_TYPE_SZARRAY
:
5185 case MONO_TYPE_OBJECT
:
5187 case MONO_TYPE_GENERICINST
:
5188 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
5189 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5191 case MONO_TYPE_VALUETYPE
:
5192 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
5195 g_assert_not_reached ();
5202 if (!info
->dyn_call_info
)
5203 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
5205 mono_domain_lock (domain
);
5206 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5211 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
5213 mono_domain_unlock (domain
);
5216 runtime_invoke
= info
->runtime_invoke
;
5219 * We need this here because mono_marshal_get_runtime_invoke can place
5220 * the helper method in System.Object and not the target class.
5223 *exc
= (MonoObject
*)mono_runtime_class_init_full (info
->vtable
, FALSE
);
5227 mono_runtime_class_init (info
->vtable
);
5230 /* The wrappers expect this to be initialized to NULL */
5234 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5235 if (info
->dyn_call_info
) {
5236 MonoMethodSignature
*sig
= mono_method_signature (method
);
5238 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
5241 guint8 retval
[128];
5243 if (!dyn_runtime_invoke
) {
5244 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
5245 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
5248 /* Convert the arguments to the format expected by start_dyn_call () */
5249 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
5252 args
[pindex
++] = &obj
;
5253 for (i
= 0; i
< sig
->param_count
; ++i
) {
5254 MonoType
*t
= sig
->params
[i
];
5257 args
[pindex
++] = ¶ms
[i
];
5258 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
5259 args
[pindex
++] = ¶ms
[i
];
5261 args
[pindex
++] = params
[i
];
5265 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
5267 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
5269 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
5271 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
5273 if (info
->ret_box_class
)
5274 return mono_value_box (domain
, info
->ret_box_class
, retval
);
5276 return *(MonoObject
**)retval
;
5280 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
5284 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
5286 MonoException
*exc
= NULL
;
5288 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
5293 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5295 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
5296 if (mono_arch_is_int_overflow (ctx
, info
))
5297 exc
= mono_get_exception_arithmetic ();
5299 exc
= mono_get_exception_divide_by_zero ();
5301 exc
= mono_get_exception_divide_by_zero ();
5305 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5308 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5311 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5315 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
5320 exc
= mono_get_exception_execution_engine ("SIGILL");
5322 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5325 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
5326 #define HAVE_SIG_INFO
5330 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
5333 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
5337 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
5338 if (mono_arch_is_single_step_event (info
, ctx
)) {
5339 mono_debugger_agent_single_step_event (ctx
);
5341 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
5342 mono_debugger_agent_breakpoint_hit (ctx
);
5347 #if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
5348 if (mono_aot_is_pagefault (info
->si_addr
)) {
5349 mono_aot_handle_pagefault (info
->si_addr
);
5354 /* The thread might no be registered with the runtime */
5355 if (!mono_domain_get () || !jit_tls
) {
5356 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5358 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5361 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
5363 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
5364 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
5367 /* The hard-guard page has been hit: there is not much we can do anymore
5368 * Print a hopefully clear message and abort.
5370 if (jit_tls
->stack_size
&&
5371 ABS ((guint8
*)info
->si_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 32768) {
5373 /* we don't do much now, but we can warn the user with a useful message */
5374 fprintf (stderr
, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx
), (gpointer
)info
->si_addr
);
5375 if (ji
&& ji
->method
)
5376 method
= mono_method_full_name (ji
->method
, TRUE
);
5378 method
= "Unmanaged";
5379 fprintf (stderr
, "At %s\n", method
);
5382 /* The original handler might not like that it is executed on an altstack... */
5383 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
5386 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
5391 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
5394 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
5397 mono_arch_handle_exception (ctx
, NULL
, FALSE
);
5402 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
5407 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
5409 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5412 /* mono_jit_create_remoting_trampoline:
5413 * @method: pointer to the method info
5415 * Creates a trampoline which calls the remoting functions. This
5416 * is used in the vtable of transparent proxies.
5418 * Returns: a pointer to the newly created code
5421 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
5424 guint8
*addr
= NULL
;
5426 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
5427 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
5431 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
5432 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
5433 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
5434 addr
= mono_compile_method (nm
);
5436 addr
= mono_compile_method (method
);
5438 return mono_get_addr_from_ftnptr (addr
);
5441 static gpointer
*vtable_trampolines
;
5442 static int vtable_trampolines_size
;
5445 mini_get_vtable_trampoline (int slot_index
)
5447 if (!mono_use_llvm
) {
5448 /* Use only one trampoline */
5449 if (slot_index
< 0) {
5450 static gpointer tramp
= NULL
;
5453 tramp
= mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5456 static gpointer tramp
= NULL
;
5459 tramp
= mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5463 int index
= slot_index
+ MONO_IMT_SIZE
;
5465 /* For LLVM, use one trampoline per vtable slot index */
5466 g_assert (slot_index
>= - MONO_IMT_SIZE
);
5467 if (!vtable_trampolines
|| slot_index
+ MONO_IMT_SIZE
>= vtable_trampolines_size
) {
5469 if (!vtable_trampolines
|| index
>= vtable_trampolines_size
) {
5473 new_size
= vtable_trampolines_size
? vtable_trampolines_size
* 2 : 128;
5474 while (new_size
<= index
)
5476 new_table
= g_new0 (gpointer
, new_size
);
5478 if (vtable_trampolines
)
5479 memcpy (new_table
, vtable_trampolines
, vtable_trampolines_size
* sizeof (gpointer
));
5480 mono_memory_barrier ();
5481 vtable_trampolines
= new_table
;
5482 vtable_trampolines_size
= new_size
;
5487 #ifdef MONO_ARCH_LLVM_SUPPORTED
5488 if (!vtable_trampolines
[index
])
5489 vtable_trampolines
[index
] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index
), MONO_TRAMPOLINE_LLVM_VCALL
, mono_get_root_domain (), NULL
);
5490 return vtable_trampolines
[index
];
5492 g_assert_not_reached ();
5498 mini_get_imt_trampoline (int slot_index
)
5500 return mini_get_vtable_trampoline (slot_index
- MONO_IMT_SIZE
);
5504 mini_parse_debug_options (void)
5506 char *options
= getenv ("MONO_DEBUG");
5507 gchar
**args
, **ptr
;
5512 args
= g_strsplit (options
, ",", -1);
5514 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
5515 const char *arg
= *ptr
;
5517 if (!strcmp (arg
, "handle-sigint"))
5518 debug_options
.handle_sigint
= TRUE
;
5519 else if (!strcmp (arg
, "keep-delegates"))
5520 debug_options
.keep_delegates
= TRUE
;
5521 else if (!strcmp (arg
, "collect-pagefault-stats"))
5522 debug_options
.collect_pagefault_stats
= TRUE
;
5523 else if (!strcmp (arg
, "break-on-unverified"))
5524 debug_options
.break_on_unverified
= TRUE
;
5525 else if (!strcmp (arg
, "no-gdb-backtrace"))
5526 debug_options
.no_gdb_backtrace
= TRUE
;
5527 else if (!strcmp (arg
, "suspend-on-sigsegv"))
5528 debug_options
.suspend_on_sigsegv
= TRUE
;
5529 else if (!strcmp (arg
, "dont-free-domains"))
5530 mono_dont_free_domains
= TRUE
;
5531 else if (!strcmp (arg
, "dyn-runtime-invoke"))
5532 debug_options
.dyn_runtime_invoke
= TRUE
;
5533 else if (!strcmp (arg
, "gdb"))
5534 debug_options
.gdb
= TRUE
;
5535 else if (!strcmp (arg
, "explicit-null-checks"))
5536 debug_options
.explicit_null_checks
= TRUE
;
5537 else if (!strcmp (arg
, "gen-seq-points"))
5538 debug_options
.gen_seq_points
= TRUE
;
5539 else if (!strcmp (arg
, "init-stacks"))
5540 debug_options
.init_stacks
= TRUE
;
5542 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
5543 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");
5552 mini_get_debug_options (void)
5554 return &debug_options
;
5558 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
5563 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
5569 #elif defined(__ppc64__) || defined(__powerpc64__)
5572 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
5585 mini_get_addr_from_ftnptr (gpointer descr
)
5587 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
5588 return *(gpointer
*)descr
;
5594 static void runtime_invoke_info_free (gpointer value
);
5597 mini_create_jit_domain_info (MonoDomain
*domain
)
5599 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
5601 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5602 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5603 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5604 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5605 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5606 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
5607 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
5608 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5610 domain
->runtime_info
= info
;
5614 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
5616 g_slist_free (value
);
5620 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
5622 MonoJitDynamicMethodInfo
*di
= value
;
5623 mono_code_manager_destroy (di
->code_mp
);
5628 runtime_invoke_info_free (gpointer value
)
5630 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
5632 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5633 if (info
->dyn_call_info
)
5634 mono_arch_dyn_call_free (info
->dyn_call_info
);
5640 mini_free_jit_domain_info (MonoDomain
*domain
)
5642 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
5644 if (info
->jump_target_hash
) {
5645 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
5646 g_hash_table_destroy (info
->jump_target_hash
);
5648 if (info
->jump_target_got_slot_hash
) {
5649 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
5650 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
5652 if (info
->dynamic_code_hash
) {
5653 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
5654 g_hash_table_destroy (info
->dynamic_code_hash
);
5656 if (info
->method_code_hash
)
5657 g_hash_table_destroy (info
->method_code_hash
);
5658 g_hash_table_destroy (info
->class_init_trampoline_hash
);
5659 g_hash_table_destroy (info
->jump_trampoline_hash
);
5660 g_hash_table_destroy (info
->jit_trampoline_hash
);
5661 g_hash_table_destroy (info
->delegate_trampoline_hash
);
5662 if (info
->static_rgctx_trampoline_hash
)
5663 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
5664 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
5665 g_hash_table_destroy (info
->runtime_invoke_hash
);
5666 g_hash_table_destroy (info
->seq_points
);
5667 g_hash_table_destroy (info
->arch_seq_points
);
5669 if (info
->agent_info
)
5670 mono_debugger_agent_free_domain_info (domain
);
5672 g_free (domain
->runtime_info
);
5673 domain
->runtime_info
= NULL
;
5677 mini_init (const char *filename
, const char *runtime_version
)
5680 MonoRuntimeCallbacks callbacks
;
5682 MONO_PROBE_VES_INIT_BEGIN ();
5685 if (access ("/proc/self/maps", F_OK
) != 0) {
5686 g_print ("Mono requires /proc to be mounted.\n");
5691 /* Happens when using the embedding interface */
5692 if (!default_opt_set
)
5693 default_opt
= mono_parse_default_optimizations (NULL
);
5695 InitializeCriticalSection (&jit_mutex
);
5697 #ifdef MONO_DEBUGGER_SUPPORTED
5698 if (mini_debug_running_inside_mdb ())
5699 mini_debugger_init ();
5702 #ifdef MONO_ARCH_HAVE_TLS_GET
5703 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET
);
5705 mono_runtime_set_has_tls_get (FALSE
);
5708 if (!global_codeman
)
5709 global_codeman
= mono_code_manager_new ();
5710 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
5712 memset (&callbacks
, 0, sizeof (callbacks
));
5713 callbacks
.create_ftnptr
= mini_create_ftnptr
;
5714 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
5715 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
5717 #ifdef MONO_ARCH_HAVE_IMT
5719 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
5720 callbacks
.get_imt_trampoline
= mini_get_imt_trampoline
;
5724 mono_install_callbacks (&callbacks
);
5726 mono_arch_cpu_init ();
5730 mono_unwind_init ();
5734 if (getenv ("MONO_DEBUG") != NULL
)
5735 mini_parse_debug_options ();
5737 if (getenv ("MONO_XDEBUG")) {
5738 char *xdebug_opts
= getenv ("MONO_XDEBUG");
5739 mono_xdebug_init (xdebug_opts
);
5740 /* So methods for multiple domains don't have the same address */
5741 mono_dont_free_domains
= TRUE
;
5742 mono_using_xdebug
= TRUE
;
5743 } else if (mini_get_debug_options ()->gdb
) {
5744 mono_xdebug_init ((char*)"gdb");
5745 mono_dont_free_domains
= TRUE
;
5746 mono_using_xdebug
= TRUE
;
5753 mono_trampolines_init ();
5755 if (!g_thread_supported ())
5756 g_thread_init (NULL
);
5758 mono_jit_tls_id
= TlsAlloc ();
5759 setup_jit_tls_data ((gpointer
)-1, mono_thread_abort
);
5761 if (default_opt
& MONO_OPT_AOT
)
5764 mono_debugger_agent_init ();
5766 #ifdef MONO_ARCH_GSHARED_SUPPORTED
5767 mono_set_generic_sharing_supported (TRUE
);
5770 #ifndef MONO_CROSS_COMPILE
5771 mono_runtime_install_handlers ();
5773 mono_threads_install_cleanup (mini_thread_cleanup
);
5775 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
5776 // This is experimental code so provide an env var to switch it off
5777 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
5778 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
5780 check_for_pending_exc
= FALSE
;
5781 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
5785 #define JIT_TRAMPOLINES_WORK
5786 #ifdef JIT_TRAMPOLINES_WORK
5787 mono_install_compile_method (mono_jit_compile_method
);
5788 mono_install_free_method (mono_jit_free_method
);
5789 mono_install_trampoline (mono_create_jit_trampoline
);
5790 mono_install_jump_trampoline (mono_create_jump_trampoline
);
5791 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
5792 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
5793 mono_install_create_domain_hook (mini_create_jit_domain_info
);
5794 mono_install_free_domain_hook (mini_free_jit_domain_info
);
5796 #define JIT_INVOKE_WORKS
5797 #ifdef JIT_INVOKE_WORKS
5798 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
5800 mono_install_stack_walk (mono_jit_walk_stack
);
5801 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
5802 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
5803 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
5805 if (debug_options
.collect_pagefault_stats
) {
5806 mono_aot_set_make_unreadable (TRUE
);
5809 if (runtime_version
)
5810 domain
= mono_init_version (filename
, runtime_version
);
5812 domain
= mono_init_from_assembly (filename
, filename
);
5814 if (mono_aot_only
) {
5815 /* This helps catch code allocation requests */
5816 mono_code_manager_set_read_only (domain
->code_mp
);
5819 #ifdef MONO_ARCH_HAVE_IMT
5822 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
5824 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
5828 /* This must come after mono_init () in the aot-only case */
5829 mono_exceptions_init ();
5830 mono_install_handler (mono_get_throw_exception ());
5834 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
5835 ves_icall_get_frame_info
);
5836 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
5837 ves_icall_get_trace
);
5838 mono_add_internal_call ("System.Exception::get_trace",
5839 ves_icall_System_Exception_get_trace
);
5840 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
5841 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
5842 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
5843 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
5844 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
5845 mono_runtime_install_handlers
);
5848 create_helper_signature ();
5850 #define JIT_CALLS_WORK
5851 #ifdef JIT_CALLS_WORK
5852 /* Needs to be called here since register_jit_icall depends on it */
5853 mono_marshal_init ();
5855 mono_arch_register_lowlevel_calls ();
5856 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
5857 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
5858 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
5859 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
5860 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
5861 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "void", TRUE
);
5862 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
5864 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
5865 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
5866 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
5867 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
5870 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
5871 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
5872 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
5873 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
5874 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
5877 * NOTE, NOTE, NOTE, NOTE:
5878 * when adding emulation for some opcodes, remember to also add a dummy
5879 * rule to the burg files, because we need the arity information to be correct.
5881 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
5882 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
5883 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
5884 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
5885 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
5886 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
5887 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
5888 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
5891 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
5892 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
5893 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
5894 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
5897 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
5898 mono_register_opcode_emulation (CEE_DIV
, "__emul_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5899 mono_register_opcode_emulation (CEE_DIV_UN
, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5900 mono_register_opcode_emulation (CEE_REM
, "__emul_irem", "int32 int32 int32", mono_irem
, FALSE
);
5901 mono_register_opcode_emulation (CEE_REM_UN
, "__emul_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5902 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5903 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5904 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
5905 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5908 #ifdef MONO_ARCH_EMULATE_MUL_DIV
5909 mono_register_opcode_emulation (CEE_MUL
, "__emul_imul", "int32 int32 int32", mono_imul
, TRUE
);
5910 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
5913 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
5914 mono_register_opcode_emulation (CEE_MUL_OVF
, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5915 mono_register_opcode_emulation (CEE_MUL_OVF_UN
, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5916 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5917 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5920 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
5921 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
5924 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
5925 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
5926 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
5927 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
5929 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
5930 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
5932 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
5933 mono_register_opcode_emulation (CEE_CONV_R_UN
, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5934 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5936 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
5937 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
5939 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
5940 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
5942 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
5943 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
5945 #ifdef MONO_ARCH_EMULATE_FREM
5946 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
5949 #ifdef MONO_ARCH_SOFT_FLOAT
5950 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
5951 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
5952 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
5953 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
5954 mono_register_opcode_emulation (CEE_CONV_R8
, "__emul_conv_r8", "double int32", mono_conv_to_r8
, FALSE
);
5955 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
5956 mono_register_opcode_emulation (CEE_CONV_R4
, "__emul_conv_r4", "double int32", mono_conv_to_r4
, FALSE
);
5957 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
5958 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
5959 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
5960 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
5961 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
5962 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
5963 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
5964 #if SIZEOF_VOID_P == 4
5965 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
5968 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
5969 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
5970 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
5971 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
5972 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
5973 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
5974 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
5975 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
5976 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
5977 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
5979 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
5980 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
5981 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
5982 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
5983 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
5985 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
5986 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
5987 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
5988 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
5991 #ifdef COMPRESSED_INTERFACE_BITMAP
5992 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
5995 #if SIZEOF_REGISTER == 4
5996 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
5999 /* other jit icalls */
6000 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
6001 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
6002 "ptr ptr ptr", FALSE
);
6003 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
6004 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
6005 "ptr ptr ptr ptr", FALSE
);
6006 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
6007 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
6008 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
6009 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
6010 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
6011 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
6012 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
6013 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
6014 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
6015 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
6016 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
6017 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
6018 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
6019 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
6020 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
6021 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
6022 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
6023 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
6024 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
6025 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
6026 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
6027 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
6028 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
6029 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
6030 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
6031 register_icall (mono_resume_unwind
, "mono_resume_unwind", "void", TRUE
);
6033 #ifdef HAVE_WRITE_BARRIERS
6034 register_icall (mono_gc_wbarrier_value_copy_bitmap
, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE
);
6039 mono_generic_sharing_init ();
6041 #ifdef MONO_ARCH_SIMD_INTRINSICS
6042 mono_simd_intrinsics_init ();
6045 #if MONO_SUPPORT_TASKLETS
6046 mono_tasklets_init ();
6049 if (mono_compile_aot
)
6051 * Avoid running managed code when AOT compiling, since the platform
6052 * might only support aot-only execution.
6054 mono_runtime_set_no_exec (TRUE
);
6056 #define JIT_RUNTIME_WORKS
6057 #ifdef JIT_RUNTIME_WORKS
6058 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
6059 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
6060 mono_thread_attach (domain
);
6063 mono_profiler_runtime_initialized ();
6065 MONO_PROBE_VES_INIT_END ();
6070 MonoJitStats mono_jit_stats
= {0};
6073 print_jit_stats (void)
6075 if (mono_jit_stats
.enabled
) {
6076 g_print ("Mono Jit statistics\n");
6077 g_print ("Compiled methods: %ld\n", mono_jit_stats
.methods_compiled
);
6078 g_print ("Methods from AOT: %ld\n", mono_jit_stats
.methods_aot
);
6079 g_print ("Methods cache lookup: %ld\n", mono_jit_stats
.methods_lookups
);
6080 g_print ("Method trampolines: %ld\n", mono_jit_stats
.method_trampolines
);
6081 g_print ("Basic blocks: %ld\n", mono_jit_stats
.basic_blocks
);
6082 g_print ("Max basic blocks: %ld\n", mono_jit_stats
.max_basic_blocks
);
6083 g_print ("Allocated vars: %ld\n", mono_jit_stats
.allocate_var
);
6084 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats
.cil_code_size
);
6085 g_print ("Native code size: %ld\n", mono_jit_stats
.native_code_size
);
6086 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
6087 mono_jit_stats
.max_ratio_method
);
6088 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
6089 mono_jit_stats
.biggest_method
);
6090 g_print ("Code reallocs: %ld\n", mono_jit_stats
.code_reallocs
);
6091 g_print ("Allocated code size: %ld\n", mono_jit_stats
.allocated_code_size
);
6092 g_print ("Inlineable methods: %ld\n", mono_jit_stats
.inlineable_methods
);
6093 g_print ("Inlined methods: %ld\n", mono_jit_stats
.inlined_methods
);
6094 g_print ("Regvars: %ld\n", mono_jit_stats
.regvars
);
6095 g_print ("Locals stack size: %ld\n", mono_jit_stats
.locals_stack_size
);
6097 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
6098 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
6099 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
6100 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
6101 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
6102 g_print ("Methods: %ld\n", mono_stats
.method_count
);
6103 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
6104 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
6105 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
6107 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
6108 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
6109 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
6111 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
6112 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
6113 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
6115 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
6116 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
6117 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
6119 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
6120 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
6121 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
6122 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
6123 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
6124 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
6125 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
6126 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
6128 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
6129 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
6130 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
6132 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
6134 g_print ("Minor GC collections: %ld\n", mono_stats
.minor_gc_count
);
6136 g_print ("Major GC collections: %ld\n", mono_stats
.major_gc_count
);
6138 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats
.minor_gc_time_usecs
/ 1000.0);
6140 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats
.major_gc_time_usecs
/ 1000.0);
6141 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
6142 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
6143 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
6144 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
6145 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
6146 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
6147 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
6150 g_free (mono_jit_stats
.max_ratio_method
);
6151 mono_jit_stats
.max_ratio_method
= NULL
;
6152 g_free (mono_jit_stats
.biggest_method
);
6153 mono_jit_stats
.biggest_method
= NULL
;
6158 mini_cleanup (MonoDomain
*domain
)
6160 mono_runtime_shutdown_stat_profiler ();
6163 cominterop_release_all_rcws ();
6166 mono_runtime_shutdown ();
6168 #ifndef MONO_CROSS_COMPILE
6170 * mono_runtime_cleanup() and mono_domain_finalize () need to
6171 * be called early since they need the execution engine still
6172 * fully working (mono_domain_finalize may invoke managed finalizers
6173 * and mono_runtime_cleanup will wait for other threads to finish).
6175 mono_domain_finalize (domain
, 2000);
6178 /* This accesses metadata so needs to be called before runtime shutdown */
6181 mono_profiler_shutdown ();
6183 #ifndef MONO_CROSS_COMPILE
6184 mono_runtime_cleanup (domain
);
6187 mono_icall_cleanup ();
6189 mono_runtime_cleanup_handlers ();
6191 mono_domain_free (domain
, TRUE
);
6193 mono_debugger_cleanup ();
6196 mono_llvm_cleanup ();
6199 mono_trampolines_cleanup ();
6201 mono_unwind_cleanup ();
6203 if (!mono_dont_free_global_codeman
)
6204 mono_code_manager_destroy (global_codeman
);
6205 g_hash_table_destroy (jit_icall_name_hash
);
6206 g_free (emul_opcode_map
);
6207 g_free (emul_opcode_opcodes
);
6209 mono_arch_cleanup ();
6213 mono_trace_cleanup ();
6215 mono_counters_dump (-1, stdout
);
6217 if (mono_inject_async_exc_method
)
6218 mono_method_desc_free (mono_inject_async_exc_method
);
6220 TlsFree(mono_jit_tls_id
);
6222 DeleteCriticalSection (&jit_mutex
);
6224 DeleteCriticalSection (&mono_delegate_section
);
6228 mono_set_defaults (int verbose_level
, guint32 opts
)
6230 mini_verbose
= verbose_level
;
6232 default_opt_set
= TRUE
;
6236 mono_disable_optimizations (guint32 opts
)
6238 default_opt
&= ~opts
;
6242 * mono_get_runtime_build_info:
6244 * Return the runtime version + build date in string format.
6245 * The returned string is owned by the caller.
6248 mono_get_runtime_build_info (void)
6250 if (mono_build_date
)
6251 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
6253 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
6257 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
6259 GHashTable
*assemblies
= (GHashTable
*)user_data
;
6260 MonoImage
*image
= mono_assembly_get_image (ass
);
6261 MonoMethod
*method
, *invoke
;
6264 if (g_hash_table_lookup (assemblies
, ass
))
6267 g_hash_table_insert (assemblies
, ass
, ass
);
6269 if (mini_verbose
> 0)
6270 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
6272 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
6273 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
6274 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
6278 if (mini_verbose
> 1) {
6279 char * desc
= mono_method_full_name (method
, TRUE
);
6280 g_print ("Compiling %d %s\n", count
, desc
);
6283 mono_compile_method (method
);
6284 if (strcmp (method
->name
, "Finalize") == 0) {
6285 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
6286 mono_compile_method (invoke
);
6288 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
6289 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
6290 mono_compile_method (invoke
);
6294 /* Load and precompile referenced assemblies as well */
6295 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
6296 mono_assembly_load_reference (image
, i
);
6297 if (image
->references
[i
])
6298 mono_precompile_assembly (image
->references
[i
], assemblies
);
6302 void mono_precompile_assemblies ()
6304 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
6306 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
6308 g_hash_table_destroy (assemblies
);
6314 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
6315 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);
6319 mono_cfg_add_try_hole (MonoCompile
*cfg
, MonoExceptionClause
*clause
, guint8
*start
, MonoBasicBlock
*bb
)
6321 TryBlockHole
*hole
= mono_mempool_alloc (cfg
->mempool
, sizeof (TryBlockHole
));
6322 hole
->clause
= clause
;
6323 hole
->start_offset
= start
- cfg
->native_code
;
6324 hole
->basic_block
= bb
;
6326 cfg
->try_block_holes
= g_slist_append_mempool (cfg
->mempool
, cfg
->try_block_holes
, hole
);