2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
20 #ifdef HAVE_SYS_TIME_H
24 #include <mono/utils/memcheck.h>
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/threads.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/gc-internal.h>
43 #include <mono/metadata/threads-types.h>
44 #include <mono/metadata/verify.h>
45 #include <mono/metadata/verify-internals.h>
46 #include <mono/metadata/mempool-internals.h>
47 #include <mono/metadata/attach.h>
48 #include <mono/utils/mono-math.h>
49 #include <mono/utils/mono-compiler.h>
50 #include <mono/utils/mono-counters.h>
51 #include <mono/utils/mono-logger.h>
52 #include <mono/utils/mono-mmap.h>
53 #include <mono/utils/dtrace.h>
62 #include "jit-icalls.h"
64 #include "debug-mini.h"
66 #include "debugger-agent.h"
68 static gpointer
mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
);
70 /* helper methods signature */
71 /* FIXME: Make these static again */
72 MonoMethodSignature
*helper_sig_class_init_trampoline
= NULL
;
73 MonoMethodSignature
*helper_sig_domain_get
= NULL
;
74 MonoMethodSignature
*helper_sig_generic_class_init_trampoline
= NULL
;
75 MonoMethodSignature
*helper_sig_rgctx_lazy_fetch_trampoline
= NULL
;
76 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline
= NULL
;
77 MonoMethodSignature
*helper_sig_monitor_enter_exit_trampoline_llvm
= NULL
;
79 static guint32 default_opt
= 0;
80 static gboolean default_opt_set
= FALSE
;
82 guint32 mono_jit_tls_id
= -1;
85 static __thread gpointer mono_jit_tls MONO_TLS_FAST
;
88 MonoTraceSpec
*mono_jit_trace_calls
= NULL
;
89 gboolean mono_break_on_exc
= FALSE
;
90 gboolean mono_compile_aot
= FALSE
;
91 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
92 gboolean mono_aot_only
= FALSE
;
93 /* Whenever to use IMT */
94 #ifdef MONO_ARCH_HAVE_IMT
95 gboolean mono_use_imt
= TRUE
;
97 gboolean mono_use_imt
= FALSE
;
99 MonoMethodDesc
*mono_inject_async_exc_method
= NULL
;
100 int mono_inject_async_exc_pos
;
101 MonoMethodDesc
*mono_break_at_bb_method
= NULL
;
102 int mono_break_at_bb_bb_num
;
103 gboolean mono_do_x86_stack_align
= TRUE
;
104 const char *mono_build_date
;
105 gboolean mono_do_signal_chaining
;
106 static gboolean mono_using_xdebug
;
107 static int mini_verbose
= 0;
111 static int methods_with_llvm
, methods_without_llvm
;
115 * This flag controls whenever the runtime uses LLVM compiled code.
116 * Enabling this causes different/slower code paths to be used, which is why it
117 * defaults to FALSE if ENABLE_LLVM is not defined, i.e. the runtime is only capable of
118 * running AOT code compiled by LLVM.
119 * Changes when this flag is set include:
120 * - a per method vtable trampoline is used to handle virtual calls, instead of only
122 * - fast generic virtual calls are not supported.
125 gboolean mono_use_llvm
= TRUE
;
127 gboolean mono_use_llvm
= FALSE
;
130 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
131 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
132 static CRITICAL_SECTION jit_mutex
;
134 static MonoCodeManager
*global_codeman
= NULL
;
136 /* FIXME: Make this static again */
137 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
;
179 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
181 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
184 ud
->method
= (MonoMethod
*)key
;
188 G_GNUC_UNUSED
static char*
189 get_method_from_ip (void *ip
)
194 MonoDomain
*domain
= mono_domain_get ();
195 MonoDebugSourceLocation
*location
;
196 FindTrampUserData user_data
;
198 ji
= mono_jit_info_table_find (domain
, ip
);
201 user_data
.method
= NULL
;
202 mono_domain_lock (domain
);
203 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
204 mono_domain_unlock (domain
);
205 if (user_data
.method
) {
206 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
207 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
214 method
= mono_method_full_name (ji
->method
, TRUE
);
215 /* FIXME: unused ? */
216 location
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
218 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
);
220 mono_debug_free_source_location (location
);
228 * @ip: an instruction pointer address
230 * This method is used from a debugger to get the name of the
231 * method at address @ip. This routine is typically invoked from
232 * a debugger like this:
234 * (gdb) print mono_pmip ($pc)
236 * Returns: the name of the method at address @ip.
241 return get_method_from_ip (ip
);
245 * mono_print_method_from_ip
246 * @ip: an instruction pointer address
248 * This method is used from a debugger to get the name of the
249 * method at address @ip.
251 * This prints the name of the method at address @ip in the standard
252 * output. Unlike mono_pmip which returns a string, this routine
253 * prints the value on the standard output.
256 mono_print_method_from_ip (void *ip
)
260 MonoDebugSourceLocation
*source
;
261 MonoDomain
*domain
= mono_domain_get ();
262 MonoDomain
*target_domain
= mono_domain_get ();
263 FindTrampUserData user_data
;
265 ji
= mini_jit_info_table_find (domain
, ip
, &target_domain
);
268 user_data
.method
= NULL
;
269 mono_domain_lock (domain
);
270 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
271 mono_domain_unlock (domain
);
272 if (user_data
.method
) {
273 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
274 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
278 g_print ("No method at %p\n", ip
);
281 method
= mono_method_full_name (ji
->method
, TRUE
);
282 source
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
284 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
);
287 g_print ("%s:%d\n", source
->source_file
, source
->row
);
289 mono_debug_free_source_location (source
);
294 * mono_method_same_domain:
296 * Determine whenever two compiled methods are in the same domain, thus
297 * the address of the callee can be embedded in the caller.
299 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
301 if (!caller
|| !callee
)
305 * If the call was made from domain-neutral to domain-specific
306 * code, we can't patch the call site.
308 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
311 if ((caller
->method
->klass
== mono_defaults
.appdomain_class
) &&
312 (strstr (caller
->method
->name
, "InvokeInDomain"))) {
313 /* The InvokeInDomain methods change the current appdomain */
321 * mono_global_codeman_reserve:
323 * Allocate code memory from the global code manager.
325 void *mono_global_codeman_reserve (int size
)
330 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
332 if (!global_codeman
) {
333 /* This can happen during startup */
334 global_codeman
= mono_code_manager_new ();
335 return mono_code_manager_reserve (global_codeman
, size
);
339 ptr
= mono_code_manager_reserve (global_codeman
, size
);
346 * mono_create_unwind_op:
348 * Create an unwind op with the given parameters.
351 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
353 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
364 * mono_emit_unwind_op:
366 * Add an unwind op with the given parameters for the list of unwind ops stored in
370 mono_emit_unwind_op (MonoCompile
*cfg
, int when
, int tag
, int reg
, int val
)
372 MonoUnwindOp
*op
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoUnwindOp
));
379 cfg
->unwind_ops
= g_slist_append_mempool (cfg
->mempool
, cfg
->unwind_ops
, op
);
383 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
385 MonoJumpInfoToken
*res
= mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
388 res
->has_context
= context
!= NULL
;
390 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
396 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
398 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
401 #define MONO_INIT_VARINFO(vi,id) do { \
402 (vi)->range.first_use.pos.bid = 0xffff; \
408 * mono_unlink_bblock:
410 * Unlink two basic blocks.
413 mono_unlink_bblock (MonoCompile
*cfg
, MonoBasicBlock
*from
, MonoBasicBlock
* to
)
419 for (i
= 0; i
< from
->out_count
; ++i
) {
420 if (to
== from
->out_bb
[i
]) {
427 for (i
= 0; i
< from
->out_count
; ++i
) {
428 if (from
->out_bb
[i
] != to
)
429 from
->out_bb
[pos
++] = from
->out_bb
[i
];
431 g_assert (pos
== from
->out_count
- 1);
436 for (i
= 0; i
< to
->in_count
; ++i
) {
437 if (from
== to
->in_bb
[i
]) {
444 for (i
= 0; i
< to
->in_count
; ++i
) {
445 if (to
->in_bb
[i
] != from
)
446 to
->in_bb
[pos
++] = to
->in_bb
[i
];
448 g_assert (pos
== to
->in_count
- 1);
454 * mono_bblocks_linked:
456 * Return whenever BB1 and BB2 are linked in the CFG.
459 mono_bblocks_linked (MonoBasicBlock
*bb1
, MonoBasicBlock
*bb2
)
463 for (i
= 0; i
< bb1
->out_count
; ++i
) {
464 if (bb1
->out_bb
[i
] == bb2
)
472 mono_find_block_region_notry (MonoCompile
*cfg
, int offset
)
474 MonoMethod
*method
= cfg
->method
;
475 MonoMethodHeader
*header
= mono_method_get_header (method
);
476 MonoExceptionClause
*clause
;
479 for (i
= 0; i
< header
->num_clauses
; ++i
) {
480 clause
= &header
->clauses
[i
];
481 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) && (offset
>= clause
->data
.filter_offset
) &&
482 (offset
< (clause
->handler_offset
)))
483 return ((i
+ 1) << 8) | MONO_REGION_FILTER
| clause
->flags
;
485 if (MONO_OFFSET_IN_HANDLER (clause
, offset
)) {
486 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
487 return ((i
+ 1) << 8) | MONO_REGION_FINALLY
| clause
->flags
;
488 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
)
489 return ((i
+ 1) << 8) | MONO_REGION_FAULT
| clause
->flags
;
491 return ((i
+ 1) << 8) | MONO_REGION_CATCH
| clause
->flags
;
499 * mono_get_block_region_notry:
501 * Return the region corresponding to REGION, ignoring try clauses nested inside
505 mono_get_block_region_notry (MonoCompile
*cfg
, int region
)
507 if ((region
& (0xf << 4)) == MONO_REGION_TRY
) {
508 MonoMethodHeader
*header
= mono_method_get_header (cfg
->method
);
511 * This can happen if a try clause is nested inside a finally clause.
513 int clause_index
= (region
>> 8) - 1;
514 g_assert (clause_index
>= 0 && clause_index
< header
->num_clauses
);
516 region
= mono_find_block_region_notry (cfg
, header
->clauses
[clause_index
].try_offset
);
523 mono_find_spvar_for_region (MonoCompile
*cfg
, int region
)
525 region
= mono_get_block_region_notry (cfg
, region
);
527 return g_hash_table_lookup (cfg
->spvars
, GINT_TO_POINTER (region
));
531 df_visit (MonoBasicBlock
*start
, int *dfn
, MonoBasicBlock
**array
)
535 array
[*dfn
] = start
;
536 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
537 for (i
= 0; i
< start
->out_count
; ++i
) {
538 if (start
->out_bb
[i
]->dfn
)
541 start
->out_bb
[i
]->dfn
= *dfn
;
542 start
->out_bb
[i
]->df_parent
= start
;
543 array
[*dfn
] = start
->out_bb
[i
];
544 df_visit (start
->out_bb
[i
], dfn
, array
);
549 mono_reverse_branch_op (guint32 opcode
)
551 static const int reverse_map
[] = {
552 CEE_BNE_UN
, CEE_BLT
, CEE_BLE
, CEE_BGT
, CEE_BGE
,
553 CEE_BEQ
, CEE_BLT_UN
, CEE_BLE_UN
, CEE_BGT_UN
, CEE_BGE_UN
555 static const int reverse_fmap
[] = {
556 OP_FBNE_UN
, OP_FBLT
, OP_FBLE
, OP_FBGT
, OP_FBGE
,
557 OP_FBEQ
, OP_FBLT_UN
, OP_FBLE_UN
, OP_FBGT_UN
, OP_FBGE_UN
559 static const int reverse_lmap
[] = {
560 OP_LBNE_UN
, OP_LBLT
, OP_LBLE
, OP_LBGT
, OP_LBGE
,
561 OP_LBEQ
, OP_LBLT_UN
, OP_LBLE_UN
, OP_LBGT_UN
, OP_LBGE_UN
563 static const int reverse_imap
[] = {
564 OP_IBNE_UN
, OP_IBLT
, OP_IBLE
, OP_IBGT
, OP_IBGE
,
565 OP_IBEQ
, OP_IBLT_UN
, OP_IBLE_UN
, OP_IBGT_UN
, OP_IBGE_UN
568 if (opcode
>= CEE_BEQ
&& opcode
<= CEE_BLT_UN
) {
569 opcode
= reverse_map
[opcode
- CEE_BEQ
];
570 } else if (opcode
>= OP_FBEQ
&& opcode
<= OP_FBLT_UN
) {
571 opcode
= reverse_fmap
[opcode
- OP_FBEQ
];
572 } else if (opcode
>= OP_LBEQ
&& opcode
<= OP_LBLT_UN
) {
573 opcode
= reverse_lmap
[opcode
- OP_LBEQ
];
574 } else if (opcode
>= OP_IBEQ
&& opcode
<= OP_IBLT_UN
) {
575 opcode
= reverse_imap
[opcode
- OP_IBEQ
];
577 g_assert_not_reached ();
583 mono_type_to_store_membase (MonoCompile
*cfg
, MonoType
*type
)
586 return OP_STORE_MEMBASE_REG
;
589 switch (type
->type
) {
592 case MONO_TYPE_BOOLEAN
:
593 return OP_STOREI1_MEMBASE_REG
;
597 return OP_STOREI2_MEMBASE_REG
;
600 return OP_STOREI4_MEMBASE_REG
;
604 case MONO_TYPE_FNPTR
:
605 return OP_STORE_MEMBASE_REG
;
606 case MONO_TYPE_CLASS
:
607 case MONO_TYPE_STRING
:
608 case MONO_TYPE_OBJECT
:
609 case MONO_TYPE_SZARRAY
:
610 case MONO_TYPE_ARRAY
:
611 return OP_STORE_MEMBASE_REG
;
614 return OP_STOREI8_MEMBASE_REG
;
616 return OP_STORER4_MEMBASE_REG
;
618 return OP_STORER8_MEMBASE_REG
;
619 case MONO_TYPE_VALUETYPE
:
620 if (type
->data
.klass
->enumtype
) {
621 type
= mono_class_enum_basetype (type
->data
.klass
);
624 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
625 return OP_STOREX_MEMBASE
;
626 return OP_STOREV_MEMBASE
;
627 case MONO_TYPE_TYPEDBYREF
:
628 return OP_STOREV_MEMBASE
;
629 case MONO_TYPE_GENERICINST
:
630 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
634 /* FIXME: all the arguments must be references for now,
635 * later look inside cfg and see if the arg num is
638 g_assert (cfg
->generic_sharing_context
);
639 return OP_STORE_MEMBASE_REG
;
641 g_error ("unknown type 0x%02x in type_to_store_membase", type
->type
);
647 mono_type_to_load_membase (MonoCompile
*cfg
, MonoType
*type
)
650 return OP_LOAD_MEMBASE
;
652 type
= mono_type_get_underlying_type (type
);
654 switch (type
->type
) {
656 return OP_LOADI1_MEMBASE
;
658 case MONO_TYPE_BOOLEAN
:
659 return OP_LOADU1_MEMBASE
;
661 return OP_LOADI2_MEMBASE
;
664 return OP_LOADU2_MEMBASE
;
666 return OP_LOADI4_MEMBASE
;
668 return OP_LOADU4_MEMBASE
;
672 case MONO_TYPE_FNPTR
:
673 return OP_LOAD_MEMBASE
;
674 case MONO_TYPE_CLASS
:
675 case MONO_TYPE_STRING
:
676 case MONO_TYPE_OBJECT
:
677 case MONO_TYPE_SZARRAY
:
678 case MONO_TYPE_ARRAY
:
679 return OP_LOAD_MEMBASE
;
682 return OP_LOADI8_MEMBASE
;
684 return OP_LOADR4_MEMBASE
;
686 return OP_LOADR8_MEMBASE
;
687 case MONO_TYPE_VALUETYPE
:
688 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
689 return OP_LOADX_MEMBASE
;
690 case MONO_TYPE_TYPEDBYREF
:
691 return OP_LOADV_MEMBASE
;
692 case MONO_TYPE_GENERICINST
:
693 if (mono_type_generic_inst_is_valuetype (type
))
694 return OP_LOADV_MEMBASE
;
696 return OP_LOAD_MEMBASE
;
700 /* FIXME: all the arguments must be references for now,
701 * later look inside cfg and see if the arg num is
704 g_assert (cfg
->generic_sharing_context
);
705 return OP_LOAD_MEMBASE
;
707 g_error ("unknown type 0x%02x in type_to_load_membase", type
->type
);
713 mini_type_to_ldind (MonoCompile
* cfg
, MonoType
*type
)
715 if (cfg
->generic_sharing_context
&& !type
->byref
) {
716 /* FIXME: all the arguments must be references for now,
717 * later look inside cfg and see if the arg num is
720 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
721 return CEE_LDIND_REF
;
723 return mono_type_to_ldind (type
);
727 mini_type_to_stind (MonoCompile
* cfg
, MonoType
*type
)
729 if (cfg
->generic_sharing_context
&& !type
->byref
) {
730 /* FIXME: all the arguments must be references for now,
731 * later look inside cfg and see if the arg num is
734 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)
735 return CEE_STIND_REF
;
737 return mono_type_to_stind (type
);
741 mono_op_imm_to_op (int opcode
)
745 #if SIZEOF_REGISTER == 4
761 #if SIZEOF_REGISTER == 4
767 #if SIZEOF_REGISTER == 4
773 #if SIZEOF_REGISTER == 4
811 #if SIZEOF_REGISTER == 4
817 #if SIZEOF_REGISTER == 4
836 case OP_ICOMPARE_IMM
:
838 case OP_LOCALLOC_IMM
:
841 printf ("%s\n", mono_inst_name (opcode
));
842 g_assert_not_reached ();
848 * mono_decompose_op_imm:
850 * Replace the OP_.._IMM INS with its non IMM variant.
853 mono_decompose_op_imm (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
857 MONO_INST_NEW (cfg
, temp
, OP_ICONST
);
858 temp
->inst_c0
= ins
->inst_imm
;
859 temp
->dreg
= mono_alloc_ireg (cfg
);
860 mono_bblock_insert_before_ins (bb
, ins
, temp
);
861 ins
->opcode
= mono_op_imm_to_op (ins
->opcode
);
862 if (ins
->opcode
== OP_LOCALLOC
)
863 ins
->sreg1
= temp
->dreg
;
865 ins
->sreg2
= temp
->dreg
;
867 bb
->max_vreg
= MAX (bb
->max_vreg
, cfg
->next_vreg
);
871 set_vreg_to_inst (MonoCompile
*cfg
, int vreg
, MonoInst
*inst
)
873 if (vreg
>= cfg
->vreg_to_inst_len
) {
874 MonoInst
**tmp
= cfg
->vreg_to_inst
;
875 int size
= cfg
->vreg_to_inst_len
;
877 while (vreg
>= cfg
->vreg_to_inst_len
)
878 cfg
->vreg_to_inst_len
= cfg
->vreg_to_inst_len
? cfg
->vreg_to_inst_len
* 2 : 32;
879 cfg
->vreg_to_inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
*) * cfg
->vreg_to_inst_len
);
881 memcpy (cfg
->vreg_to_inst
, tmp
, size
* sizeof (MonoInst
*));
883 cfg
->vreg_to_inst
[vreg
] = inst
;
886 #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)))
887 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
892 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
900 mono_compile_create_var_for_vreg (MonoCompile
*cfg
, MonoType
*type
, int opcode
, int vreg
)
903 int num
= cfg
->num_varinfo
;
906 if ((num
+ 1) >= cfg
->varinfo_count
) {
907 int orig_count
= cfg
->varinfo_count
;
908 cfg
->varinfo_count
= cfg
->varinfo_count
? (cfg
->varinfo_count
* 2) : 64;
909 cfg
->varinfo
= (MonoInst
**)g_realloc (cfg
->varinfo
, sizeof (MonoInst
*) * cfg
->varinfo_count
);
910 cfg
->vars
= (MonoMethodVar
*)g_realloc (cfg
->vars
, sizeof (MonoMethodVar
) * cfg
->varinfo_count
);
911 memset (&cfg
->vars
[orig_count
], 0, (cfg
->varinfo_count
- orig_count
) * sizeof (MonoMethodVar
));
914 mono_jit_stats
.allocate_var
++;
916 MONO_INST_NEW (cfg
, inst
, opcode
);
918 inst
->inst_vtype
= type
;
919 inst
->klass
= mono_class_from_mono_type (type
);
920 type_to_eval_stack_type (cfg
, type
, inst
);
921 /* if set to 1 the variable is native */
922 inst
->backend
.is_pinvoke
= 0;
925 cfg
->varinfo
[num
] = inst
;
927 MONO_INIT_VARINFO (&cfg
->vars
[num
], num
);
928 MONO_VARINFO (cfg
, num
)->vreg
= vreg
;
931 set_vreg_to_inst (cfg
, vreg
, inst
);
933 #if SIZEOF_REGISTER == 4
934 #ifdef MONO_ARCH_SOFT_FLOAT
935 regpair
= mono_type_is_long (type
) || mono_type_is_float (type
);
937 regpair
= mono_type_is_long (type
);
947 * These two cannot be allocated using create_var_for_vreg since that would
948 * put it into the cfg->varinfo array, confusing many parts of the JIT.
952 * Set flags to VOLATILE so SSA skips it.
955 if (cfg
->verbose_level
>= 4) {
956 printf (" Create LVAR R%d (R%d, R%d)\n", inst
->dreg
, inst
->dreg
+ 1, inst
->dreg
+ 2);
959 #ifdef MONO_ARCH_SOFT_FLOAT
960 if (cfg
->opt
& MONO_OPT_SSA
) {
961 if (mono_type_is_float (type
))
962 inst
->flags
= MONO_INST_VOLATILE
;
966 /* Allocate a dummy MonoInst for the first vreg */
967 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
968 tree
->dreg
= inst
->dreg
+ 1;
969 if (cfg
->opt
& MONO_OPT_SSA
)
970 tree
->flags
= MONO_INST_VOLATILE
;
972 tree
->type
= STACK_I4
;
973 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
974 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
976 set_vreg_to_inst (cfg
, inst
->dreg
+ 1, tree
);
978 /* Allocate a dummy MonoInst for the second vreg */
979 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
980 tree
->dreg
= inst
->dreg
+ 2;
981 if (cfg
->opt
& MONO_OPT_SSA
)
982 tree
->flags
= MONO_INST_VOLATILE
;
984 tree
->type
= STACK_I4
;
985 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
986 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
988 set_vreg_to_inst (cfg
, inst
->dreg
+ 2, tree
);
992 if (cfg
->verbose_level
> 2)
993 g_print ("created temp %d (R%d) of type %s\n", num
, vreg
, mono_type_get_name (type
));
998 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1002 if (mono_type_is_long (type
))
1003 dreg
= mono_alloc_dreg (cfg
, STACK_I8
);
1004 #ifdef MONO_ARCH_SOFT_FLOAT
1005 else if (mono_type_is_float (type
))
1006 dreg
= mono_alloc_dreg (cfg
, STACK_R8
);
1009 /* All the others are unified */
1010 dreg
= mono_alloc_preg (cfg
);
1012 return mono_compile_create_var_for_vreg (cfg
, type
, opcode
, dreg
);
1016 * Transform a MonoInst into a load from the variable of index var_index.
1019 mono_compile_make_var_load (MonoCompile
*cfg
, MonoInst
*dest
, gssize var_index
) {
1020 memset (dest
, 0, sizeof (MonoInst
));
1021 dest
->inst_i0
= cfg
->varinfo
[var_index
];
1022 dest
->opcode
= mini_type_to_ldind (cfg
, dest
->inst_i0
->inst_vtype
);
1023 type_to_eval_stack_type (cfg
, dest
->inst_i0
->inst_vtype
, dest
);
1024 dest
->klass
= dest
->inst_i0
->klass
;
1030 type_from_stack_type (MonoInst
*ins
) {
1031 switch (ins
->type
) {
1032 case STACK_I4
: return &mono_defaults
.int32_class
->byval_arg
;
1033 case STACK_I8
: return &mono_defaults
.int64_class
->byval_arg
;
1034 case STACK_PTR
: return &mono_defaults
.int_class
->byval_arg
;
1035 case STACK_R8
: return &mono_defaults
.double_class
->byval_arg
;
1038 * this if used to be commented without any specific reason, but
1039 * it breaks #80235 when commented
1042 return &ins
->klass
->this_arg
;
1044 return &mono_defaults
.object_class
->this_arg
;
1046 /* ins->klass may not be set for ldnull.
1047 * Also, if we have a boxed valuetype, we want an object lass,
1048 * not the valuetype class
1050 if (ins
->klass
&& !ins
->klass
->valuetype
)
1051 return &ins
->klass
->byval_arg
;
1052 return &mono_defaults
.object_class
->byval_arg
;
1053 case STACK_VTYPE
: return &ins
->klass
->byval_arg
;
1055 g_error ("stack type %d to montype not handled\n", ins
->type
);
1061 mono_type_from_stack_type (MonoInst
*ins
) {
1062 return type_from_stack_type (ins
);
1066 * mono_add_ins_to_end:
1068 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
1071 mono_add_ins_to_end (MonoBasicBlock
*bb
, MonoInst
*inst
)
1076 MONO_ADD_INS (bb
, inst
);
1080 switch (bb
->last_ins
->opcode
) {
1094 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1097 if (MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1098 /* Need to insert the ins before the compare */
1099 if (bb
->code
== bb
->last_ins
) {
1100 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1104 if (bb
->code
->next
== bb
->last_ins
) {
1105 /* Only two instructions */
1106 opcode
= bb
->code
->opcode
;
1108 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
)) {
1110 mono_bblock_insert_before_ins (bb
, bb
->code
, inst
);
1112 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1115 opcode
= bb
->last_ins
->prev
->opcode
;
1117 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
)) {
1119 mono_bblock_insert_before_ins (bb
, bb
->last_ins
->prev
, inst
);
1121 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1126 MONO_ADD_INS (bb
, inst
);
1132 mono_create_jump_table (MonoCompile
*cfg
, MonoInst
*label
, MonoBasicBlock
**bbs
, int num_blocks
)
1134 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
1135 MonoJumpInfoBBTable
*table
;
1137 table
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfoBBTable
));
1139 table
->table_size
= num_blocks
;
1141 ji
->ip
.label
= label
;
1142 ji
->type
= MONO_PATCH_INFO_SWITCH
;
1143 ji
->data
.table
= table
;
1144 ji
->next
= cfg
->patch_info
;
1145 cfg
->patch_info
= ji
;
1148 static MonoMethodSignature
*
1149 mono_get_array_new_va_signature (int arity
)
1151 static GHashTable
*sighash
= NULL
;
1152 MonoMethodSignature
*res
;
1157 sighash
= g_hash_table_new (NULL
, NULL
);
1159 else if ((res
= g_hash_table_lookup (sighash
, GINT_TO_POINTER (arity
)))) {
1164 res
= mono_metadata_signature_alloc (mono_defaults
.corlib
, arity
+ 1);
1167 #ifdef MONO_ARCH_VARARG_ICALLS
1168 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1169 res
->call_convention
= MONO_CALL_VARARG
;
1173 res
->call_convention
= MONO_CALL_C
;
1176 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
1177 for (i
= 0; i
< arity
; i
++)
1178 res
->params
[i
+ 1] = &mono_defaults
.int_class
->byval_arg
;
1180 res
->ret
= &mono_defaults
.object_class
->byval_arg
;
1182 g_hash_table_insert (sighash
, GINT_TO_POINTER (arity
), res
);
1189 mono_get_array_new_va_icall (int rank
)
1191 MonoMethodSignature
*esig
;
1192 char icall_name
[256];
1194 MonoJitICallInfo
*info
;
1196 /* Need to register the icall so it gets an icall wrapper */
1197 sprintf (icall_name
, "ves_array_new_va_%d", rank
);
1200 info
= mono_find_jit_icall_by_name (icall_name
);
1202 esig
= mono_get_array_new_va_signature (rank
);
1203 name
= g_strdup (icall_name
);
1204 info
= mono_register_jit_icall (mono_array_new_va
, name
, esig
, FALSE
);
1206 g_hash_table_insert (jit_icall_name_hash
, name
, name
);
1214 mini_class_is_system_array (MonoClass
*klass
)
1216 if (klass
->parent
== mono_defaults
.array_class
)
1222 static MonoJitICallInfo
**emul_opcode_map
= NULL
;
1225 mono_find_jit_opcode_emulation (int opcode
)
1227 g_assert (opcode
>= 0 && opcode
<= OP_LAST
);
1228 if (emul_opcode_map
)
1229 return emul_opcode_map
[opcode
];
1235 mini_assembly_can_skip_verification (MonoDomain
*domain
, MonoMethod
*method
)
1237 MonoAssembly
*assembly
= method
->klass
->image
->assembly
;
1238 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
1240 if (assembly
->in_gac
|| assembly
->image
== mono_defaults
.corlib
)
1242 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE
)
1244 return mono_assembly_has_skip_verification (assembly
);
1248 * mini_method_verify:
1250 * Verify the method using the new verfier.
1252 * Returns true if the method is invalid.
1255 mini_method_verify (MonoCompile
*cfg
, MonoMethod
*method
)
1258 gboolean is_fulltrust
;
1259 MonoLoaderError
*error
;
1261 if (method
->verification_success
)
1264 is_fulltrust
= mono_verifier_is_method_full_trust (method
);
1266 if (!mono_verifier_is_enabled_for_method (method
))
1269 res
= mono_method_verify_with_current_settings (method
, cfg
->skip_visibility
);
1271 if ((error
= mono_loader_get_last_error ())) {
1272 cfg
->exception_type
= error
->exception_type
;
1274 mono_free_verify_list (res
);
1279 for (tmp
= res
; tmp
; tmp
= tmp
->next
) {
1280 MonoVerifyInfoExtended
*info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1281 if (info
->info
.status
== MONO_VERIFY_ERROR
) {
1282 cfg
->exception_type
= info
->exception_type
;
1283 cfg
->exception_message
= g_strdup (info
->info
.message
);
1284 mono_free_verify_list (res
);
1287 if (info
->info
.status
== MONO_VERIFY_NOT_VERIFIABLE
&& (!is_fulltrust
|| info
->exception_type
== MONO_EXCEPTION_METHOD_ACCESS
|| info
->exception_type
== MONO_EXCEPTION_FIELD_ACCESS
)) {
1288 cfg
->exception_type
= info
->exception_type
;
1289 cfg
->exception_message
= g_strdup (info
->info
.message
);
1290 mono_free_verify_list (res
);
1294 mono_free_verify_list (res
);
1296 method
->verification_success
= 1;
1301 create_helper_signature (void)
1303 helper_sig_domain_get
= mono_create_icall_signature ("ptr");
1304 helper_sig_class_init_trampoline
= mono_create_icall_signature ("void");
1305 helper_sig_generic_class_init_trampoline
= mono_create_icall_signature ("void");
1306 helper_sig_rgctx_lazy_fetch_trampoline
= mono_create_icall_signature ("ptr ptr");
1307 helper_sig_monitor_enter_exit_trampoline
= mono_create_icall_signature ("void");
1308 helper_sig_monitor_enter_exit_trampoline_llvm
= mono_create_icall_signature ("void object");
1311 static gconstpointer
1312 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
1315 MonoMethod
*wrapper
;
1316 gconstpointer trampoline
;
1317 MonoDomain
*domain
= mono_get_root_domain ();
1319 if (callinfo
->wrapper
) {
1320 return callinfo
->wrapper
;
1323 if (callinfo
->trampoline
)
1324 return callinfo
->trampoline
;
1327 * We use the lock on the root domain instead of the JIT lock to protect
1328 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
1330 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
1331 mono_domain_lock (domain
);
1333 if (callinfo
->trampoline
) {
1334 mono_domain_unlock (domain
);
1335 mono_loader_unlock ();
1336 return callinfo
->trampoline
;
1339 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
1340 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_for_pending_exc
);
1344 trampoline
= mono_compile_method (wrapper
);
1346 trampoline
= mono_create_ftnptr (domain
, mono_create_jit_trampoline_in_domain (domain
, wrapper
));
1347 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
1349 callinfo
->trampoline
= trampoline
;
1351 mono_domain_unlock (domain
);
1352 mono_loader_unlock ();
1354 return callinfo
->trampoline
;
1358 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
1360 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
1364 mono_dynamic_code_hash_insert (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*ji
)
1366 if (!domain_jit_info (domain
)->dynamic_code_hash
)
1367 domain_jit_info (domain
)->dynamic_code_hash
= g_hash_table_new (NULL
, NULL
);
1368 g_hash_table_insert (domain_jit_info (domain
)->dynamic_code_hash
, method
, ji
);
1371 static MonoJitDynamicMethodInfo
*
1372 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
1374 MonoJitDynamicMethodInfo
*res
;
1376 if (domain_jit_info (domain
)->dynamic_code_hash
)
1377 res
= g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1385 GList
*active
, *inactive
;
1390 compare_by_interval_start_pos_func (gconstpointer a
, gconstpointer b
)
1392 MonoMethodVar
*v1
= (MonoMethodVar
*)a
;
1393 MonoMethodVar
*v2
= (MonoMethodVar
*)b
;
1397 else if (v1
->interval
->range
&& v2
->interval
->range
)
1398 return v1
->interval
->range
->from
- v2
->interval
->range
->from
;
1399 else if (v1
->interval
->range
)
1408 #define LSCAN_DEBUG(a) do { a; } while (0)
1410 #define LSCAN_DEBUG(a)
1414 mono_allocate_stack_slots_full2 (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1416 int i
, slot
, offset
, size
;
1421 GList
*vars
= NULL
, *l
, *unhandled
;
1422 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1426 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg
->method
, TRUE
)));
1428 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1429 vtype_stack_slots
= NULL
;
1432 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1433 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1436 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1437 inst
= cfg
->varinfo
[i
];
1438 vmv
= MONO_VARINFO (cfg
, i
);
1440 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1443 vars
= g_list_prepend (vars
, vmv
);
1446 vars
= g_list_sort (g_list_copy (vars
), compare_by_interval_start_pos_func
);
1451 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1452 MonoMethodVar *current = unhandled->data;
1454 if (current->interval->range) {
1455 g_assert (current->interval->range->from >= i);
1456 i = current->interval->range->from;
1463 for (unhandled
= vars
; unhandled
; unhandled
= unhandled
->next
) {
1464 MonoMethodVar
*current
= unhandled
->data
;
1467 inst
= cfg
->varinfo
[vmv
->idx
];
1469 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1470 * pinvoke wrappers when they call functions returning structures */
1471 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1472 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1477 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1480 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1484 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1486 case MONO_TYPE_GENERICINST
:
1487 if (!mono_type_generic_inst_is_valuetype (t
)) {
1488 slot_info
= &scalar_stack_slots
[t
->type
];
1492 case MONO_TYPE_VALUETYPE
:
1493 if (!vtype_stack_slots
)
1494 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1495 for (i
= 0; i
< nvtypes
; ++i
)
1496 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1499 slot_info
= &vtype_stack_slots
[i
];
1501 g_assert (nvtypes
< 256);
1502 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1503 slot_info
= &vtype_stack_slots
[nvtypes
];
1511 #if SIZEOF_REGISTER == 4
1517 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1523 case MONO_TYPE_CLASS
:
1524 case MONO_TYPE_OBJECT
:
1525 case MONO_TYPE_ARRAY
:
1526 case MONO_TYPE_SZARRAY
:
1527 case MONO_TYPE_STRING
:
1528 /* Share non-float stack slots of the same size */
1529 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1533 slot_info
= &scalar_stack_slots
[t
->type
];
1537 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1541 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1543 if (!current
->interval
->range
) {
1544 if (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
1548 inst
->flags
|= MONO_INST_IS_DEAD
;
1553 pos
= current
->interval
->range
->from
;
1555 LSCAN_DEBUG (printf ("process R%d ", inst
->dreg
));
1556 if (current
->interval
->range
)
1557 LSCAN_DEBUG (mono_linterval_print (current
->interval
));
1558 LSCAN_DEBUG (printf ("\n"));
1560 /* Check for intervals in active which expired or inactive */
1562 /* FIXME: Optimize this */
1565 for (l
= slot_info
->active
; l
!= NULL
; l
= l
->next
) {
1566 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1568 if (v
->interval
->last_range
->to
< pos
) {
1569 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1570 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[v
->idx
]));
1571 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1575 else if (!mono_linterval_covers (v
->interval
, pos
)) {
1576 slot_info
->inactive
= g_list_append (slot_info
->inactive
, v
);
1577 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1578 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg
->varinfo
[v
->idx
]->dreg
));
1585 /* Check for intervals in inactive which expired or active */
1587 /* FIXME: Optimize this */
1590 for (l
= slot_info
->inactive
; l
!= NULL
; l
= l
->next
) {
1591 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1593 if (v
->interval
->last_range
->to
< pos
) {
1594 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1595 // FIXME: Enabling this seems to cause impossible to debug crashes
1596 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1597 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1601 else if (mono_linterval_covers (v
->interval
, pos
)) {
1602 slot_info
->active
= g_list_append (slot_info
->active
, v
);
1603 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1604 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg
->varinfo
[v
->idx
]->dreg
));
1612 * This also handles the case when the variable is used in an
1613 * exception region, as liveness info is not computed there.
1616 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1619 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1620 if (slot_info
->slots
) {
1621 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1623 slot_info
->slots
= slot_info
->slots
->next
;
1626 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1628 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1634 static int count
= 0;
1637 if (count
== atoi (getenv ("COUNT3")))
1638 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1639 if (count
> atoi (getenv ("COUNT3")))
1642 mono_print_ins (inst
);
1647 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst
->dreg
, mono_type_full_name (t
), slot
));
1649 if (slot
== 0xffffff) {
1651 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1652 * efficient copying (and to work around the fact that OP_MEMCPY
1653 * and OP_MEMSET ignores alignment).
1655 if (MONO_TYPE_ISSTRUCT (t
)) {
1656 align
= MAX (align
, sizeof (gpointer
));
1657 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1662 offset
+= align
- 1;
1663 offset
&= ~(align
- 1);
1667 offset
+= align
- 1;
1668 offset
&= ~(align
- 1);
1673 if (*stack_align
== 0)
1674 *stack_align
= align
;
1677 offsets
[vmv
->idx
] = slot
;
1680 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1681 if (scalar_stack_slots
[i
].active
)
1682 g_list_free (scalar_stack_slots
[i
].active
);
1684 for (i
= 0; i
< nvtypes
; ++i
) {
1685 if (vtype_stack_slots
[i
].active
)
1686 g_list_free (vtype_stack_slots
[i
].active
);
1689 mono_jit_stats
.locals_stack_size
+= offset
;
1691 *stack_size
= offset
;
1696 * mono_allocate_stack_slots_full:
1698 * Allocate stack slots for all non register allocated variables using a
1699 * linear scan algorithm.
1700 * Returns: an array of stack offsets.
1701 * STACK_SIZE is set to the amount of stack space needed.
1702 * STACK_ALIGN is set to the alignment needed by the locals area.
1705 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1707 int i
, slot
, offset
, size
;
1712 GList
*vars
= NULL
, *l
;
1713 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1717 if ((cfg
->num_varinfo
> 0) && MONO_VARINFO (cfg
, 0)->interval
)
1718 return mono_allocate_stack_slots_full2 (cfg
, backward
, stack_size
, stack_align
);
1720 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1721 vtype_stack_slots
= NULL
;
1724 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1725 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1728 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1729 inst
= cfg
->varinfo
[i
];
1730 vmv
= MONO_VARINFO (cfg
, i
);
1732 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1735 vars
= g_list_prepend (vars
, vmv
);
1738 vars
= mono_varlist_sort (cfg
, vars
, 0);
1740 *stack_align
= sizeof (gpointer
);
1741 for (l
= vars
; l
; l
= l
->next
) {
1743 inst
= cfg
->varinfo
[vmv
->idx
];
1745 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1746 * pinvoke wrappers when they call functions returning structures */
1747 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (inst
->inst_vtype
) && inst
->inst_vtype
->type
!= MONO_TYPE_TYPEDBYREF
) {
1748 size
= mono_class_native_size (mono_class_from_mono_type (inst
->inst_vtype
), &align
);
1752 size
= mono_type_size (inst
->inst_vtype
, &ialign
);
1755 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (inst
->inst_vtype
)))
1759 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1761 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1764 case MONO_TYPE_GENERICINST
:
1765 if (!mono_type_generic_inst_is_valuetype (t
)) {
1766 slot_info
= &scalar_stack_slots
[t
->type
];
1770 case MONO_TYPE_VALUETYPE
:
1771 if (!vtype_stack_slots
)
1772 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1773 for (i
= 0; i
< nvtypes
; ++i
)
1774 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1777 slot_info
= &vtype_stack_slots
[i
];
1779 g_assert (nvtypes
< 256);
1780 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1781 slot_info
= &vtype_stack_slots
[nvtypes
];
1789 #if SIZEOF_REGISTER == 4
1795 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1801 case MONO_TYPE_CLASS
:
1802 case MONO_TYPE_OBJECT
:
1803 case MONO_TYPE_ARRAY
:
1804 case MONO_TYPE_SZARRAY
:
1805 case MONO_TYPE_STRING
:
1806 /* Share non-float stack slots of the same size */
1807 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1811 slot_info
= &scalar_stack_slots
[t
->type
];
1816 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1817 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1819 /* expire old intervals in active */
1820 while (slot_info
->active
) {
1821 MonoMethodVar
*amv
= (MonoMethodVar
*)slot_info
->active
->data
;
1823 if (amv
->range
.last_use
.abs_pos
> vmv
->range
.first_use
.abs_pos
)
1826 //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);
1828 slot_info
->active
= g_list_delete_link (slot_info
->active
, slot_info
->active
);
1829 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[amv
->idx
]));
1833 * This also handles the case when the variable is used in an
1834 * exception region, as liveness info is not computed there.
1837 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1840 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1841 if (slot_info
->slots
) {
1842 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1844 slot_info
->slots
= slot_info
->slots
->next
;
1847 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1852 static int count
= 0;
1856 if (count == atoi (getenv ("COUNT")))
1857 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1858 if (count > atoi (getenv ("COUNT")))
1861 mono_print_ins (inst);
1866 if (cfg
->disable_reuse_stack_slots
)
1869 if (slot
== 0xffffff) {
1871 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1872 * efficient copying (and to work around the fact that OP_MEMCPY
1873 * and OP_MEMSET ignores alignment).
1875 if (MONO_TYPE_ISSTRUCT (t
)) {
1876 align
= MAX (align
, sizeof (gpointer
));
1877 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1879 * Align the size too so the code generated for passing vtypes in
1880 * registers doesn't overwrite random locals.
1882 size
= (size
+ (align
- 1)) & ~(align
-1);
1887 offset
+= align
- 1;
1888 offset
&= ~(align
- 1);
1892 offset
+= align
- 1;
1893 offset
&= ~(align
- 1);
1898 *stack_align
= MAX (*stack_align
, align
);
1901 offsets
[vmv
->idx
] = slot
;
1904 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1905 if (scalar_stack_slots
[i
].active
)
1906 g_list_free (scalar_stack_slots
[i
].active
);
1908 for (i
= 0; i
< nvtypes
; ++i
) {
1909 if (vtype_stack_slots
[i
].active
)
1910 g_list_free (vtype_stack_slots
[i
].active
);
1913 mono_jit_stats
.locals_stack_size
+= offset
;
1915 *stack_size
= offset
;
1922 mono_allocate_stack_slots_full (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1924 g_assert_not_reached ();
1928 #endif /* DISABLE_JIT */
1931 mono_allocate_stack_slots (MonoCompile
*m
, guint32
*stack_size
, guint32
*stack_align
)
1933 return mono_allocate_stack_slots_full (m
, TRUE
, stack_size
, stack_align
);
1937 mono_register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, gboolean no_throw
)
1939 MonoJitICallInfo
*info
;
1940 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
1942 if (!emul_opcode_map
)
1943 emul_opcode_map
= g_new0 (MonoJitICallInfo
*, OP_LAST
+ 1);
1945 g_assert (!sig
->hasthis
);
1946 g_assert (sig
->param_count
< 3);
1948 info
= mono_register_jit_icall (func
, name
, sig
, no_throw
);
1950 emul_opcode_map
[opcode
] = info
;
1954 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
1956 MonoMethodSignature
*sig
;
1959 sig
= mono_create_icall_signature (sigstr
);
1963 mono_register_jit_icall (func
, name
, sig
, save
);
1967 print_dfn (MonoCompile
*cfg
) {
1973 g_print ("IR code for method %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1975 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
1976 bb
= cfg
->bblocks
[i
];
1977 /*if (bb->cil_code) {
1978 char* code1, *code2;
1979 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
1980 if (bb->last_ins->cil_code)
1981 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
1983 code2 = g_strdup ("");
1985 code1 [strlen (code1) - 1] = 0;
1986 code = g_strdup_printf ("%s -> %s", code1, code2);
1990 code
= g_strdup ("\n");
1991 g_print ("\nBB%d (%d) (len: %d): %s", bb
->block_num
, i
, bb
->cil_length
, code
);
1992 MONO_BB_FOR_EACH_INS (bb
, c
) {
1993 mono_print_ins_index (-1, c
);
1996 g_print ("\tprev:");
1997 for (j
= 0; j
< bb
->in_count
; ++j
) {
1998 g_print (" BB%d", bb
->in_bb
[j
]->block_num
);
2000 g_print ("\t\tsucc:");
2001 for (j
= 0; j
< bb
->out_count
; ++j
) {
2002 g_print (" BB%d", bb
->out_bb
[j
]->block_num
);
2004 g_print ("\n\tidom: BB%d\n", bb
->idom
? bb
->idom
->block_num
: -1);
2007 g_assert (mono_bitset_test_fast (bb
->dominators
, bb
->idom
->dfn
));
2010 mono_blockset_print (cfg
, bb
->dominators
, "\tdominators", bb
->idom
? bb
->idom
->dfn
: -1);
2012 mono_blockset_print (cfg
, bb
->dfrontier
, "\tdfrontier", -1);
2020 mono_bblock_add_inst (MonoBasicBlock
*bb
, MonoInst
*inst
)
2022 MONO_ADD_INS (bb
, inst
);
2026 mono_bblock_insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2030 bb
->code
= ins_to_insert
;
2032 /* Link with next */
2033 ins_to_insert
->next
= ins
;
2035 ins
->prev
= ins_to_insert
;
2037 if (bb
->last_ins
== NULL
)
2038 bb
->last_ins
= ins_to_insert
;
2040 /* Link with next */
2041 ins_to_insert
->next
= ins
->next
;
2043 ins
->next
->prev
= ins_to_insert
;
2045 /* Link with previous */
2046 ins
->next
= ins_to_insert
;
2047 ins_to_insert
->prev
= ins
;
2049 if (bb
->last_ins
== ins
)
2050 bb
->last_ins
= ins_to_insert
;
2055 mono_bblock_insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2059 bb
->code
= ins_to_insert
;
2060 ins_to_insert
->next
= ins
;
2061 if (bb
->last_ins
== NULL
)
2062 bb
->last_ins
= ins_to_insert
;
2064 /* Link with previous */
2066 ins
->prev
->next
= ins_to_insert
;
2067 ins_to_insert
->prev
= ins
->prev
;
2069 /* Link with next */
2070 ins
->prev
= ins_to_insert
;
2071 ins_to_insert
->next
= ins
;
2073 if (bb
->code
== ins
)
2074 bb
->code
= ins_to_insert
;
2079 * mono_verify_bblock:
2081 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2084 mono_verify_bblock (MonoBasicBlock
*bb
)
2086 MonoInst
*ins
, *prev
;
2089 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2090 g_assert (ins
->prev
== prev
);
2094 g_assert (!bb
->last_ins
->next
);
2100 * Perform consistency checks on the JIT data structures and the IR
2103 mono_verify_cfg (MonoCompile
*cfg
)
2107 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2108 mono_verify_bblock (bb
);
2112 mono_destroy_compile (MonoCompile
*cfg
)
2114 //mono_mempool_stats (cfg->mempool);
2115 mono_free_loop_info (cfg
);
2117 mono_regstate_free (cfg
->rs
);
2119 g_hash_table_destroy (cfg
->spvars
);
2121 g_hash_table_destroy (cfg
->exvars
);
2122 mono_mempool_destroy (cfg
->mempool
);
2123 g_list_free (cfg
->ldstr_list
);
2124 g_hash_table_destroy (cfg
->token_info_hash
);
2125 if (cfg
->abs_patches
)
2126 g_hash_table_destroy (cfg
->abs_patches
);
2128 g_free (cfg
->varinfo
);
2130 g_free (cfg
->exception_message
);
2134 #ifdef HAVE_KW_THREAD
2135 static __thread gpointer mono_lmf_addr MONO_TLS_FAST
;
2136 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
2138 * When this is defined, the current lmf is stored in this tls variable instead of in
2141 static __thread gpointer mono_lmf MONO_TLS_FAST
;
2146 mono_get_jit_tls_key (void)
2148 return mono_jit_tls_id
;
2152 mono_get_jit_tls_offset (void)
2154 #ifdef HAVE_KW_THREAD
2156 MONO_THREAD_VAR_OFFSET (mono_jit_tls
, offset
);
2164 mono_get_lmf_tls_offset (void)
2166 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2168 MONO_THREAD_VAR_OFFSET(mono_lmf
,offset
);
2176 mono_get_lmf_addr_tls_offset (void)
2179 MONO_THREAD_VAR_OFFSET(mono_lmf_addr
,offset
);
2186 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2189 MonoJitTlsData
*jit_tls
;
2191 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2192 return jit_tls
->lmf
;
2194 g_assert_not_reached ();
2200 mono_get_lmf_addr (void)
2202 #ifdef HAVE_KW_THREAD
2203 return mono_lmf_addr
;
2205 MonoJitTlsData
*jit_tls
;
2207 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2208 return &jit_tls
->lmf
;
2211 * When resolving the call to mono_jit_thread_attach full-aot will look
2212 * in the plt, which causes a call into the generic trampoline, which in turn
2213 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
2214 * call mono_jit_thread_attach from the native-to-managed wrapper, without
2215 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
2218 mono_jit_thread_attach (NULL
);
2220 if ((jit_tls
= TlsGetValue (mono_jit_tls_id
)))
2221 return &jit_tls
->lmf
;
2223 g_assert_not_reached ();
2229 mono_set_lmf (MonoLMF
*lmf
)
2231 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2235 (*mono_get_lmf_addr ()) = lmf
;
2238 /* Called by native->managed wrappers */
2240 mono_jit_thread_attach (MonoDomain
*domain
)
2244 * Happens when called from AOTed code which is only used in the root
2247 domain
= mono_get_root_domain ();
2249 #ifdef HAVE_KW_THREAD
2250 if (!mono_lmf_addr
) {
2251 mono_thread_attach (domain
);
2254 if (!TlsGetValue (mono_jit_tls_id
))
2255 mono_thread_attach (domain
);
2257 if (mono_domain_get () != domain
)
2258 mono_domain_set (domain
, TRUE
);
2262 * mono_thread_abort:
2263 * @obj: exception object
2265 * abort the thread, print exception information and stack trace
2268 mono_thread_abort (MonoObject
*obj
)
2270 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
2272 /* handle_remove should be eventually called for this thread, too
2275 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
2276 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
2277 mono_thread_exit ();
2279 exit (mono_environment_exitcode_get ());
2284 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
2286 MonoJitTlsData
*jit_tls
;
2289 jit_tls
= TlsGetValue (mono_jit_tls_id
);
2293 jit_tls
= g_new0 (MonoJitTlsData
, 1);
2295 TlsSetValue (mono_jit_tls_id
, jit_tls
);
2297 #ifdef HAVE_KW_THREAD
2298 mono_jit_tls
= jit_tls
;
2301 jit_tls
->abort_func
= abort_func
;
2302 jit_tls
->end_of_stack
= stack_start
;
2304 lmf
= g_new0 (MonoLMF
, 1);
2305 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
2306 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
2311 jit_tls
->first_lmf
= lmf
;
2313 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2314 /* jit_tls->lmf is unused */
2316 mono_lmf_addr
= &mono_lmf
;
2318 #if defined(HAVE_KW_THREAD)
2319 mono_lmf_addr
= &jit_tls
->lmf
;
2325 mono_arch_setup_jit_tls_data (jit_tls
);
2326 mono_setup_altstack (jit_tls
);
2332 mono_thread_start_cb (gsize tid
, gpointer stack_start
, gpointer func
)
2334 MonoInternalThread
*thread
;
2335 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
2336 thread
= mono_thread_internal_current ();
2337 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, jit_tls
, func
);
2339 thread
->jit_data
= jit_tls
;
2342 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
2345 mono_thread_abort_dummy (MonoObject
*obj
)
2347 if (mono_thread_attach_aborted_cb
)
2348 mono_thread_attach_aborted_cb (obj
);
2350 mono_thread_abort (obj
);
2354 mono_thread_attach_cb (gsize tid
, gpointer stack_start
)
2356 MonoInternalThread
*thread
;
2357 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
2358 thread
= mono_thread_internal_current ();
2359 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, (MonoJitTlsData
*) jit_tls
, NULL
);
2361 thread
->jit_data
= jit_tls
;
2362 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
2363 mono_runtime_setup_stat_profiler ();
2367 mini_thread_cleanup (MonoThread
*thread
)
2369 MonoInternalThread
*internal
= thread
->internal_thread
;
2370 MonoJitTlsData
*jit_tls
= internal
->jit_data
;
2373 mono_debugger_thread_cleanup (jit_tls
);
2374 mono_arch_free_jit_tls_data (jit_tls
);
2376 mono_free_altstack (jit_tls
);
2377 g_free (jit_tls
->first_lmf
);
2379 internal
->jit_data
= NULL
;
2381 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
2382 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
2383 * not a trivial thing.
2385 * The current offender is mono_thread_manage which cleanup threads from the outside.
2387 if (internal
== mono_thread_internal_current ()) {
2388 TlsSetValue (mono_jit_tls_id
, NULL
);
2390 #ifdef HAVE_KW_THREAD
2391 mono_jit_tls
= NULL
;
2392 mono_lmf_addr
= NULL
;
2393 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2402 mono_create_tls_get (MonoCompile
*cfg
, int offset
)
2404 #ifdef MONO_ARCH_HAVE_TLS_GET
2410 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2411 ins
->dreg
= mono_alloc_preg (cfg
);
2412 ins
->inst_offset
= offset
;
2420 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2422 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2426 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2428 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2432 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2434 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2438 ji
->data
.target
= target
;
2439 ji
->next
= cfg
->patch_info
;
2441 cfg
->patch_info
= ji
;
2445 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2447 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2451 ji
->data
.target
= target
;
2458 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2460 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2463 if ((*ji
)->ip
.i
== ip
)
2466 ji
= &((*ji
)->next
);
2471 * mono_patch_info_dup_mp:
2473 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2476 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2478 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2479 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2481 switch (patch_info
->type
) {
2482 case MONO_PATCH_INFO_RVA
:
2483 case MONO_PATCH_INFO_LDSTR
:
2484 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2485 case MONO_PATCH_INFO_LDTOKEN
:
2486 case MONO_PATCH_INFO_DECLSEC
:
2487 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2488 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2490 case MONO_PATCH_INFO_SWITCH
:
2491 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2492 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2493 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2494 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2496 case MONO_PATCH_INFO_RGCTX_FETCH
:
2497 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2498 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2499 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2509 mono_patch_info_hash (gconstpointer data
)
2511 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2514 case MONO_PATCH_INFO_RVA
:
2515 case MONO_PATCH_INFO_LDSTR
:
2516 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2517 case MONO_PATCH_INFO_LDTOKEN
:
2518 case MONO_PATCH_INFO_DECLSEC
:
2519 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2520 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2521 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2522 case MONO_PATCH_INFO_VTABLE
:
2523 case MONO_PATCH_INFO_CLASS
:
2524 case MONO_PATCH_INFO_IID
:
2525 case MONO_PATCH_INFO_ADJUSTED_IID
:
2526 case MONO_PATCH_INFO_CLASS_INIT
:
2527 case MONO_PATCH_INFO_METHODCONST
:
2528 case MONO_PATCH_INFO_METHOD
:
2529 case MONO_PATCH_INFO_METHOD_JUMP
:
2530 case MONO_PATCH_INFO_IMAGE
:
2531 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2532 case MONO_PATCH_INFO_FIELD
:
2533 case MONO_PATCH_INFO_SFLDA
:
2534 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2535 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2537 return (ji
->type
<< 8);
2542 * mono_patch_info_equal:
2544 * This might fail to recognize equivalent patches, i.e. floats, so its only
2545 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2549 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2551 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2552 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2554 if (ji1
->type
!= ji2
->type
)
2557 switch (ji1
->type
) {
2558 case MONO_PATCH_INFO_RVA
:
2559 case MONO_PATCH_INFO_LDSTR
:
2560 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2561 case MONO_PATCH_INFO_LDTOKEN
:
2562 case MONO_PATCH_INFO_DECLSEC
:
2563 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2564 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2565 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2566 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2567 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2570 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2571 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2573 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2574 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
2575 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
2577 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
);
2580 if (ji1
->data
.target
!= ji2
->data
.target
)
2589 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
2591 unsigned char *ip
= patch_info
->ip
.i
+ code
;
2592 gconstpointer target
= NULL
;
2594 switch (patch_info
->type
) {
2595 case MONO_PATCH_INFO_BB
:
2597 * FIXME: This could be hit for methods without a prolog. Should use -1
2598 * but too much code depends on a 0 initial value.
2600 //g_assert (patch_info->data.bb->native_offset);
2601 target
= patch_info
->data
.bb
->native_offset
+ code
;
2603 case MONO_PATCH_INFO_ABS
:
2604 target
= patch_info
->data
.target
;
2606 case MONO_PATCH_INFO_LABEL
:
2607 target
= patch_info
->data
.inst
->inst_c0
+ code
;
2609 case MONO_PATCH_INFO_IP
:
2612 case MONO_PATCH_INFO_METHOD_REL
:
2613 target
= code
+ patch_info
->data
.offset
;
2615 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
2616 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2618 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
2619 g_assert_not_reached ();
2621 target
= mono_icall_get_wrapper (mi
);
2624 case MONO_PATCH_INFO_METHOD_JUMP
:
2625 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
2627 case MONO_PATCH_INFO_METHOD
:
2628 if (patch_info
->data
.method
== method
) {
2631 /* get the trampoline to the method from the domain */
2632 target
= mono_create_jit_trampoline (patch_info
->data
.method
);
2635 case MONO_PATCH_INFO_SWITCH
: {
2636 gpointer
*jump_table
;
2639 if (method
&& method
->dynamic
) {
2640 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2642 if (mono_aot_only
) {
2643 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2645 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2649 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++)
2650 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
2651 target
= jump_table
;
2654 case MONO_PATCH_INFO_METHODCONST
:
2655 case MONO_PATCH_INFO_CLASS
:
2656 case MONO_PATCH_INFO_IMAGE
:
2657 case MONO_PATCH_INFO_FIELD
:
2658 target
= patch_info
->data
.target
;
2660 case MONO_PATCH_INFO_IID
:
2661 mono_class_init (patch_info
->data
.klass
);
2662 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
2664 case MONO_PATCH_INFO_ADJUSTED_IID
:
2665 mono_class_init (patch_info
->data
.klass
);
2666 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
2668 case MONO_PATCH_INFO_VTABLE
:
2669 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2672 case MONO_PATCH_INFO_CLASS_INIT
: {
2673 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
2676 target
= mono_create_class_init_trampoline (vtable
);
2679 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
2680 target
= mono_create_delegate_trampoline (patch_info
->data
.klass
);
2682 case MONO_PATCH_INFO_SFLDA
: {
2683 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
2686 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
2687 /* Done by the generated code */
2691 mono_runtime_class_init (vtable
);
2693 target
= (char*)vtable
->data
+ patch_info
->data
.field
->offset
;
2696 case MONO_PATCH_INFO_RVA
: {
2697 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
2700 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
2701 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
2704 case MONO_PATCH_INFO_R4
:
2705 case MONO_PATCH_INFO_R8
:
2706 target
= patch_info
->data
.target
;
2708 case MONO_PATCH_INFO_EXC_NAME
:
2709 target
= patch_info
->data
.name
;
2711 case MONO_PATCH_INFO_LDSTR
:
2713 mono_ldstr (domain
, patch_info
->data
.token
->image
,
2714 mono_metadata_token_index (patch_info
->data
.token
->token
));
2716 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
2718 MonoClass
*handle_class
;
2720 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2721 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
2722 mono_class_init (handle_class
);
2723 mono_class_init (mono_class_from_mono_type (handle
));
2726 mono_type_get_object (domain
, handle
);
2729 case MONO_PATCH_INFO_LDTOKEN
: {
2731 MonoClass
*handle_class
;
2733 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
2734 patch_info
->data
.token
->token
, &handle_class
, NULL
);
2735 mono_class_init (handle_class
);
2740 case MONO_PATCH_INFO_DECLSEC
:
2741 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
2743 case MONO_PATCH_INFO_ICALL_ADDR
:
2744 /* run_cctors == 0 -> AOT */
2745 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
2747 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, NULL
, NULL
);
2749 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
));
2754 target
= mono_lookup_internal_call (patch_info
->data
.method
);
2756 if (!target
&& run_cctors
)
2757 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
2760 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
2761 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
2763 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
2764 g_assert_not_reached ();
2769 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
2770 target
= mono_thread_interruption_request_flag ();
2772 case MONO_PATCH_INFO_METHOD_RGCTX
: {
2773 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
2776 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
2779 case MONO_PATCH_INFO_BB_OVF
:
2780 case MONO_PATCH_INFO_EXC_OVF
:
2781 case MONO_PATCH_INFO_GOT_OFFSET
:
2782 case MONO_PATCH_INFO_NONE
:
2784 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2785 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
2788 switch (entry
->data
->type
) {
2789 case MONO_PATCH_INFO_CLASS
:
2790 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
));
2792 case MONO_PATCH_INFO_METHOD
:
2793 case MONO_PATCH_INFO_METHODCONST
:
2794 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
));
2796 case MONO_PATCH_INFO_FIELD
:
2797 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
));
2800 g_assert_not_reached ();
2804 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
2807 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
2808 target
= mono_create_generic_class_init_trampoline ();
2810 case MONO_PATCH_INFO_MONITOR_ENTER
:
2811 target
= mono_create_monitor_enter_trampoline ();
2813 case MONO_PATCH_INFO_MONITOR_EXIT
:
2814 target
= mono_create_monitor_exit_trampoline ();
2816 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2817 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2819 /* AOT, not needed */
2822 target
= mono_arch_get_seq_point_info (domain
, code
);
2825 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
2826 #ifdef MONO_ARCH_LLVM_SUPPORTED
2827 g_assert (mono_use_llvm
);
2828 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
2830 g_assert_not_reached ();
2834 g_assert_not_reached ();
2837 return (gpointer
)target
;
2841 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
2843 ins
->inst_offset
= native_offset
;
2844 g_ptr_array_add (cfg
->seq_points
, ins
);
2845 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
2846 bb
->last_seq_point
= ins
;
2850 mono_compile_create_vars (MonoCompile
*cfg
)
2852 MonoMethodSignature
*sig
;
2853 MonoMethodHeader
*header
;
2856 header
= mono_method_get_header (cfg
->method
);
2858 sig
= mono_method_signature (cfg
->method
);
2860 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
2861 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
2862 /* Inhibit optimizations */
2863 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
2865 if (cfg
->verbose_level
> 2)
2866 g_print ("creating vars\n");
2868 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
2871 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
2873 for (i
= 0; i
< sig
->param_count
; ++i
) {
2874 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
2877 if (cfg
->verbose_level
> 2) {
2879 printf ("\treturn : ");
2880 mono_print_ins (cfg
->ret
);
2884 printf ("\tthis: ");
2885 mono_print_ins (cfg
->args
[0]);
2888 for (i
= 0; i
< sig
->param_count
; ++i
) {
2889 printf ("\targ [%d]: ", i
);
2890 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
2894 cfg
->locals_start
= cfg
->num_varinfo
;
2895 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
2897 if (cfg
->verbose_level
> 2)
2898 g_print ("creating locals\n");
2900 for (i
= 0; i
< header
->num_locals
; ++i
)
2901 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
2903 if (cfg
->verbose_level
> 2)
2904 g_print ("locals done\n");
2906 mono_arch_create_vars (cfg
);
2910 mono_print_code (MonoCompile
*cfg
, const char* msg
)
2914 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2915 mono_print_bb (bb
, msg
);
2921 mono_postprocess_patches (MonoCompile
*cfg
)
2923 MonoJumpInfo
*patch_info
;
2926 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
2927 switch (patch_info
->type
) {
2928 case MONO_PATCH_INFO_ABS
: {
2929 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
2932 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
2936 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
2937 // FIXME: CLEAN UP THIS MESS.
2938 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
2939 strstr (cfg
->method
->name
, info
->name
)) {
2941 * This is an icall wrapper, and this is a call to the
2944 if (cfg
->compile_aot
) {
2945 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
2946 patch_info
->data
.name
= info
->name
;
2949 /* for these array methods we currently register the same function pointer
2950 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
2951 * will return the incorrect one depending on the order they are registered.
2952 * See tests/test-arr.cs
2954 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
2955 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
2956 patch_info
->data
.name
= info
->name
;
2961 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
2962 if (cfg
->abs_patches
) {
2963 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
2965 patch_info
->type
= abs_ji
->type
;
2966 patch_info
->data
.target
= abs_ji
->data
.target
;
2973 case MONO_PATCH_INFO_SWITCH
: {
2975 if (cfg
->method
->dynamic
) {
2976 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2978 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
2981 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
2982 /* Might be NULL if the switch is eliminated */
2983 if (patch_info
->data
.table
->table
[i
]) {
2984 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
2985 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
2990 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
2993 case MONO_PATCH_INFO_METHOD_JUMP
: {
2995 MonoDomain
*domain
= cfg
->domain
;
2996 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
2998 mono_domain_lock (domain
);
2999 if (!domain_jit_info (domain
)->jump_target_hash
)
3000 domain_jit_info (domain
)->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3001 list
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3002 list
= g_slist_prepend (list
, ip
);
3003 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, list
);
3004 mono_domain_unlock (domain
);
3015 mono_save_seq_point_info (MonoCompile
*cfg
)
3017 MonoBasicBlock
*bb
, *in_bb
;
3018 GSList
*bb_seq_points
, *l
;
3020 MonoDomain
*domain
= cfg
->domain
;
3022 MonoSeqPointInfo
*info
;
3025 if (!cfg
->seq_points
)
3028 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3029 info
->len
= cfg
->seq_points
->len
;
3030 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3031 SeqPoint
*sp
= &info
->seq_points
[i
];
3032 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3034 sp
->il_offset
= ins
->inst_imm
;
3035 sp
->native_offset
= ins
->inst_offset
;
3038 ins
->backend
.size
= i
;
3042 * For each sequence point, compute the list of sequence points immediately
3043 * following it, this is needed to implement 'step over' in the debugger agent.
3045 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3046 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3047 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3049 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3050 MonoInst
*ins
= l
->data
;
3052 if (!(ins
->flags
& MONO_INST_SINGLE_STEP_LOC
))
3056 /* Link with the previous seq point in the same bb */
3057 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3059 /* Link with the last bb in the previous bblocks */
3061 * FIXME: What if the prev bb doesn't have a seq point, but
3062 * one of its predecessors has ?
3064 for (i
= 0; i
< bb
->in_count
; ++i
) {
3065 in_bb
= bb
->in_bb
[i
];
3067 if (in_bb
->last_seq_point
)
3068 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
));
3076 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3077 SeqPoint
*sp
= &info
->seq_points
[i
];
3081 sp
->next_len
= g_slist_length (next
[i
]);
3082 sp
->next
= g_new (int, sp
->next_len
);
3084 for (l
= next
[i
]; l
; l
= l
->next
)
3085 sp
->next
[j
++] = GPOINTER_TO_UINT (l
->data
);
3086 g_slist_free (next
[i
]);
3090 cfg
->seq_point_info
= info
;
3092 // FIXME: dynamic methods
3093 mono_domain_lock (domain
);
3094 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3095 mono_domain_unlock (domain
);
3097 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3098 cfg
->seq_points
= NULL
;
3102 mono_codegen (MonoCompile
*cfg
)
3105 int max_epilog_size
;
3108 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3109 cfg
->spill_count
= 0;
3110 /* we reuse dfn here */
3111 /* bb->dfn = bb_count++; */
3113 mono_arch_lowering_pass (cfg
, bb
);
3115 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3116 mono_arch_peephole_pass_1 (cfg
, bb
);
3119 mono_local_regalloc (cfg
, bb
);
3121 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3122 mono_arch_peephole_pass_2 (cfg
, bb
);
3125 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3126 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3128 code
= mono_arch_emit_prolog (cfg
);
3130 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3131 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3133 cfg
->code_len
= code
- cfg
->native_code
;
3134 cfg
->prolog_end
= cfg
->code_len
;
3136 mono_debug_open_method (cfg
);
3138 /* emit code all basic blocks */
3139 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3140 bb
->native_offset
= cfg
->code_len
;
3141 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3142 mono_arch_output_basic_block (cfg
, bb
);
3144 if (bb
== cfg
->bb_exit
) {
3145 cfg
->epilog_begin
= cfg
->code_len
;
3147 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3148 code
= cfg
->native_code
+ cfg
->code_len
;
3149 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3150 cfg
->code_len
= code
- cfg
->native_code
;
3151 g_assert (cfg
->code_len
< cfg
->code_size
);
3154 mono_arch_emit_epilog (cfg
);
3158 mono_arch_emit_exceptions (cfg
);
3160 max_epilog_size
= 0;
3162 code
= cfg
->native_code
+ cfg
->code_len
;
3164 /* we always allocate code in cfg->domain->code_mp to increase locality */
3165 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3166 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3168 if (cfg
->method
->dynamic
) {
3169 guint unwindlen
= 0;
3170 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3171 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3173 /* Allocate the code into a separate memory pool so it can be freed */
3174 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3175 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3176 mono_domain_lock (cfg
->domain
);
3177 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3178 mono_domain_unlock (cfg
->domain
);
3180 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3182 guint unwindlen
= 0;
3183 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3184 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3186 code
= mono_domain_code_reserve (cfg
->domain
, cfg
->code_size
+ unwindlen
);
3189 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3190 g_free (cfg
->native_code
);
3191 cfg
->native_code
= code
;
3192 code
= cfg
->native_code
+ cfg
->code_len
;
3194 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3195 mono_postprocess_patches (cfg
);
3197 #ifdef VALGRIND_JIT_REGISTER_MAP
3198 if (valgrind_register
){
3199 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3200 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3205 if (cfg
->verbose_level
> 0) {
3206 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3207 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3209 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3214 gboolean is_generic
= FALSE
;
3216 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3217 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3221 if (cfg
->generic_sharing_context
)
3222 g_assert (is_generic
);
3225 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3226 mono_arch_save_unwind_info (cfg
);
3229 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->run_cctors
);
3231 if (cfg
->method
->dynamic
) {
3232 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3234 mono_domain_code_commit (cfg
->domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3236 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3238 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3240 mono_debug_close_method (cfg
);
3242 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3243 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3248 compute_reachable (MonoBasicBlock
*bb
)
3252 if (!(bb
->flags
& BB_VISITED
)) {
3253 bb
->flags
|= BB_VISITED
;
3254 for (i
= 0; i
< bb
->out_count
; ++i
)
3255 compute_reachable (bb
->out_bb
[i
]);
3260 * mini_method_compile:
3261 * @method: the method to compile
3262 * @opts: the optimization flags to use
3263 * @domain: the domain where the method will be compiled in
3264 * @run_cctors: whether we should run type ctors if possible
3265 * @compile_aot: whether this is an AOT compilation
3266 * @parts: debug flag
3268 * Returns: a MonoCompile* pointer. Caller must check the exception_type
3269 * field in the returned struct to see if compilation succeded.
3272 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
3274 MonoMethodHeader
*header
;
3278 int dfn
, i
, code_size_ratio
;
3279 gboolean deadce_has_run
= FALSE
;
3280 gboolean try_generic_shared
, try_llvm
;
3281 MonoMethod
*method_to_compile
, *method_to_register
;
3282 int generic_info_size
;
3284 mono_jit_stats
.methods_compiled
++;
3285 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
3286 mono_profiler_method_jit (method
);
3287 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
3288 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
3291 /* We are passed the original generic method definition */
3292 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3293 (opts
& MONO_OPT_GSHARED
) && (method
->is_generic
|| method
->klass
->generic_container
);
3295 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
3296 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl (method
, FALSE
);
3298 if (opts
& MONO_OPT_GSHARED
) {
3299 if (try_generic_shared
)
3300 mono_stats
.generics_sharable_methods
++;
3301 else if (mono_method_is_generic_impl (method
))
3302 mono_stats
.generics_unsharable_methods
++;
3312 if (try_generic_shared
) {
3313 MonoMethod
*declaring_method
;
3314 MonoGenericContext
*shared_context
;
3317 declaring_method
= method
;
3319 declaring_method
= mono_method_get_declaring_generic_method (method
);
3320 if (method
->klass
->generic_class
)
3321 g_assert (method
->klass
->generic_class
->container_class
== declaring_method
->klass
);
3323 g_assert (method
->klass
== declaring_method
->klass
);
3326 if (declaring_method
->is_generic
)
3327 shared_context
= &(mono_method_get_generic_container (declaring_method
)->context
);
3329 shared_context
= &declaring_method
->klass
->generic_container
->context
;
3331 method_to_compile
= mono_class_inflate_generic_method (declaring_method
, shared_context
);
3332 g_assert (method_to_compile
);
3334 method_to_compile
= method
;
3337 cfg
= g_new0 (MonoCompile
, 1);
3338 cfg
->method
= method_to_compile
;
3339 cfg
->mempool
= mono_mempool_new ();
3341 cfg
->prof_options
= mono_profiler_get_events ();
3342 cfg
->run_cctors
= run_cctors
;
3343 cfg
->domain
= domain
;
3344 cfg
->verbose_level
= mini_verbose
;
3345 cfg
->compile_aot
= compile_aot
;
3346 cfg
->skip_visibility
= method
->skip_visibility
;
3347 cfg
->orig_method
= method
;
3348 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
3349 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
3350 if (try_generic_shared
)
3351 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->generic_sharing_context
;
3352 cfg
->compile_llvm
= try_llvm
;
3353 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
3355 if (cfg
->gen_seq_points
)
3356 cfg
->seq_points
= g_ptr_array_new ();
3358 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
)) {
3359 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
3363 if (cfg
->generic_sharing_context
) {
3364 MonoGenericContext object_context
= mono_method_construct_object_context (method_to_compile
);
3366 method_to_register
= mono_class_inflate_generic_method (method_to_compile
, &object_context
);
3368 g_assert (method
== method_to_compile
);
3369 method_to_register
= method
;
3371 cfg
->method_to_register
= method_to_register
;
3373 /* No way to obtain the location info for 'this' */
3374 if (try_generic_shared
) {
3375 cfg
->exception_message
= g_strdup ("gshared");
3376 cfg
->disable_llvm
= TRUE
;
3379 if (cfg
->method
->save_lmf
) {
3380 cfg
->exception_message
= g_strdup ("lmf");
3381 cfg
->disable_llvm
= TRUE
;
3385 if (cfg
->method
->dynamic
) {
3386 cfg
->exception_message
= g_strdup ("dynamic.");
3387 cfg
->disable_llvm
= TRUE
;
3390 header
= mono_method_get_header (method_to_compile
);
3392 MonoLoaderError
*error
;
3394 if ((error
= mono_loader_get_last_error ())) {
3395 cfg
->exception_type
= error
->exception_type
;
3397 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
3398 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
3400 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3401 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3405 if (header
->clauses
) {
3407 * Cannot be enabled until LLVM supports implicit exceptions, or we use
3408 * explicit checks, or we disable this for methods which might throw implicit
3409 * exceptions inside clauses.
3411 cfg
->exception_message
= g_strdup ("clauses");
3412 cfg
->disable_llvm
= TRUE
;
3417 static gboolean inited
;
3420 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_with_llvm
);
3421 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &methods_without_llvm
);
3426 * Check for methods which cannot be compiled by LLVM early, to avoid
3427 * the extra compilation pass.
3429 if (COMPILE_LLVM (cfg
) && cfg
->disable_llvm
) {
3430 if (cfg
->verbose_level
>= 1) {
3431 //nm = mono_method_full_name (cfg->method, TRUE);
3432 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3435 InterlockedIncrement (&methods_without_llvm
);
3436 mono_destroy_compile (cfg
);
3438 goto restart_compile
;
3443 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
3444 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
3445 cfg
->disable_reuse_registers
= TRUE
;
3446 cfg
->disable_reuse_stack_slots
= TRUE
;
3448 * This decreases the change the debugger will read registers/stack slots which are
3449 * not yet initialized.
3451 cfg
->disable_initlocals_opt
= TRUE
;
3453 cfg
->extend_live_ranges
= TRUE
;
3455 /* Temporarily disable this when running in the debugger until we have support
3456 * for this in the debugger. */
3457 cfg
->disable_omit_fp
= TRUE
;
3459 /* The debugger needs all locals to be on the stack or in a global register */
3460 cfg
->disable_vreg_to_lvreg
= TRUE
;
3462 /* Don't remove unused variables when running inside the debugger since the user
3463 * may still want to view them. */
3464 cfg
->disable_deadce_vars
= TRUE
;
3466 // cfg->opt |= MONO_OPT_SHARED;
3467 cfg
->opt
&= ~MONO_OPT_DEADCE
;
3468 cfg
->opt
&= ~MONO_OPT_INLINE
;
3469 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
3470 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
3471 cfg
->opt
&= ~MONO_OPT_GSHARED
;
3474 if (mono_using_xdebug
) {
3476 * Make each variable use its own register/stack slot and extend
3477 * their liveness to cover the whole method, making them displayable
3478 * in gdb even after they are dead.
3480 cfg
->disable_reuse_registers
= TRUE
;
3481 cfg
->disable_reuse_stack_slots
= TRUE
;
3482 cfg
->extend_live_ranges
= TRUE
;
3485 if (COMPILE_LLVM (cfg
)) {
3486 cfg
->opt
|= MONO_OPT_ABCREM
;
3489 if (getenv ("MONO_VERBOSE_METHOD")) {
3490 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
3491 cfg
->verbose_level
= 4;
3494 ip
= (guint8
*)header
->code
;
3496 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
3498 if (cfg
->verbose_level
> 0) {
3499 if (COMPILE_LLVM (cfg
))
3500 g_print ("converting llvm method %s\n", mono_method_full_name (method
, TRUE
));
3501 else if (cfg
->generic_sharing_context
)
3502 g_print ("converting shared method %s\n", mono_method_full_name (method
, TRUE
));
3504 g_print ("converting method %s\n", mono_method_full_name (method
, TRUE
));
3507 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
3508 cfg
->opt
|= MONO_OPT_SSA
;
3511 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")))
3512 cfg->globalra = TRUE;
3515 //cfg->globalra = TRUE;
3517 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
3518 // cfg->globalra = TRUE;
3521 static int count
= 0;
3525 if (getenv ("COUNT2")) {
3526 cfg->globalra = TRUE;
3527 if (count == atoi (getenv ("COUNT2")))
3528 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
3529 if (count > atoi (getenv ("COUNT2")))
3530 cfg->globalra = FALSE;
3535 if (header
->clauses
)
3536 cfg
->globalra
= FALSE
;
3538 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
3539 /* The code in the prolog clobbers caller saved registers */
3540 cfg
->globalra
= FALSE
;
3542 // FIXME: Disable globalra in case of tracing/profiling
3544 if (cfg
->method
->save_lmf
)
3545 /* The LMF saving code might clobber caller saved registers */
3546 cfg
->globalra
= FALSE
;
3548 if (header
->code_size
> 5000)
3550 /* Too large bblocks could overflow the ins positions */
3551 cfg
->globalra
= FALSE
;
3553 cfg
->rs
= mono_regstate_new ();
3555 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
3556 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
3558 /* FIXME: Fix SSA to handle branches inside bblocks */
3559 if (cfg
->opt
& MONO_OPT_SSA
)
3560 cfg
->enable_extended_bblocks
= FALSE
;
3563 * FIXME: This confuses liveness analysis because variables which are assigned after
3564 * a branch inside a bblock become part of the kill set, even though the assignment
3565 * might not get executed. This causes the optimize_initlocals pass to delete some
3566 * assignments which are needed.
3567 * Also, the mono_if_conversion pass needs to be modified to recognize the code
3570 //cfg->enable_extended_bblocks = TRUE;
3573 * create MonoInst* which represents arguments and local variables
3575 mono_compile_create_vars (cfg
);
3577 /* SSAPRE is not supported on linear IR */
3578 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
3580 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
3583 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
3585 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3586 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3589 mono_destroy_compile (cfg
);
3590 try_generic_shared
= FALSE
;
3591 goto restart_compile
;
3593 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
3595 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3596 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
3597 /* cfg contains the details of the failure, so let the caller cleanup */
3601 mono_jit_stats
.basic_blocks
+= cfg
->num_bblocks
;
3602 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->num_bblocks
, mono_jit_stats
.max_basic_blocks
);
3604 if (COMPILE_LLVM (cfg
)) {
3607 /* The IR has to be in SSA form for LLVM */
3608 cfg
->opt
|= MONO_OPT_SSA
;
3612 // Allow SSA on the result value
3613 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
3615 // Add an explicit return instruction referencing the return value
3616 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
3617 ins
->sreg1
= cfg
->ret
->dreg
;
3619 MONO_ADD_INS (cfg
->bb_exit
, ins
);
3622 cfg
->opt
&= ~MONO_OPT_LINEARS
;
3625 cfg
->opt
&= ~MONO_OPT_BRANCH
;
3628 /* todo: remove code when we have verified that the liveness for try/catch blocks
3632 * Currently, this can't be commented out since exception blocks are not
3633 * processed during liveness analysis.
3634 * It is also needed, because otherwise the local optimization passes would
3635 * delete assignments in cases like this:
3637 * <something which throws>
3639 * This also allows SSA to be run on methods containing exception clauses, since
3640 * SSA will ignore variables marked VOLATILE.
3642 mono_liveness_handle_exception_clauses (cfg
);
3644 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
3646 if (!COMPILE_LLVM (cfg
))
3647 mono_decompose_long_opts (cfg
);
3649 /* Should be done before branch opts */
3650 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
3651 mono_local_cprop (cfg
);
3653 if (cfg
->opt
& MONO_OPT_BRANCH
)
3654 mono_optimize_branches (cfg
);
3656 /* This must be done _before_ global reg alloc and _after_ decompose */
3657 mono_handle_global_vregs (cfg
);
3658 if (cfg
->opt
& MONO_OPT_DEADCE
)
3659 mono_local_deadce (cfg
);
3660 /* Disable this for LLVM to make the IR easier to handle */
3661 if (!COMPILE_LLVM (cfg
))
3662 mono_if_conversion (cfg
);
3664 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
3665 mono_remove_critical_edges (cfg
);
3667 /* Depth-first ordering on basic blocks */
3668 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
3670 cfg
->max_block_num
= cfg
->num_bblocks
;
3673 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
3674 if (cfg
->num_bblocks
!= dfn
+ 1) {
3677 cfg
->num_bblocks
= dfn
+ 1;
3679 /* remove unreachable code, because the code in them may be
3680 * inconsistent (access to dead variables for example) */
3681 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3682 bb
->flags
&= ~BB_VISITED
;
3683 compute_reachable (cfg
->bb_entry
);
3684 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3685 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
3686 compute_reachable (bb
);
3687 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3688 if (!(bb
->flags
& BB_VISITED
)) {
3689 if (cfg
->verbose_level
> 1)
3690 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
3691 bb
->code
= bb
->last_ins
= NULL
;
3692 while (bb
->out_count
)
3693 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
3696 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3697 bb
->flags
&= ~BB_VISITED
;
3700 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
3702 * we disable some optimizations if there are too many variables
3703 * because JIT time may become too expensive. The actual number needs
3704 * to be tweaked and eventually the non-linear algorithms should be fixed.
3706 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
3707 cfg
->disable_ssa
= TRUE
;
3710 if (cfg
->opt
& MONO_OPT_LOOP
) {
3711 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
3712 mono_compute_natural_loops (cfg
);
3715 /* after method_to_ir */
3717 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3718 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
3723 if (header->num_clauses)
3724 cfg->disable_ssa = TRUE;
3727 //#define DEBUGSSA "logic_run"
3728 #define DEBUGSSA_CLASS "Tests"
3731 if (!cfg
->disable_ssa
) {
3732 mono_local_cprop (cfg
);
3735 mono_ssa_compute (cfg
);
3739 if (cfg
->opt
& MONO_OPT_SSA
) {
3740 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
3742 mono_ssa_compute (cfg
);
3745 if (cfg
->verbose_level
>= 2) {
3752 /* after SSA translation */
3754 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3755 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
3759 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
3760 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
3762 mono_ssa_cprop (cfg
);
3768 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
3769 //mono_ssa_strength_reduction (cfg);
3771 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
3772 mono_perform_ssapre (cfg
);
3773 //mono_local_cprop (cfg);
3776 if (cfg
->opt
& MONO_OPT_DEADCE
) {
3777 mono_ssa_deadce (cfg
);
3778 deadce_has_run
= TRUE
;
3781 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
3782 mono_perform_abc_removal (cfg
);
3784 mono_ssa_remove (cfg
);
3785 mono_local_cprop (cfg
);
3786 mono_handle_global_vregs (cfg
);
3787 if (cfg
->opt
& MONO_OPT_DEADCE
)
3788 mono_local_deadce (cfg
);
3790 if (cfg
->opt
& MONO_OPT_BRANCH
) {
3793 mono_optimize_branches (cfg
);
3795 /* Have to recompute cfg->bblocks and bb->dfn */
3796 if (cfg
->globalra
) {
3797 mono_remove_critical_edges (cfg
);
3799 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3802 /* Depth-first ordering on basic blocks */
3803 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
3806 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
3807 cfg
->num_bblocks
= dfn
+ 1;
3813 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
3814 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
3815 mono_perform_abc_removal (cfg
);
3818 /* after SSA removal */
3820 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
3821 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
3825 #ifdef MONO_ARCH_SOFT_FLOAT
3826 if (!COMPILE_LLVM (cfg
))
3827 mono_decompose_soft_float (cfg
);
3829 if (!COMPILE_LLVM (cfg
))
3830 mono_decompose_vtype_opts (cfg
);
3831 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
3832 mono_decompose_array_access_opts (cfg
);
3837 g_assert (cfg
->got_var_allocated
);
3840 * Allways allocate the GOT var to a register, because keeping it
3841 * in memory will increase the number of live temporaries in some
3842 * code created by inssel.brg, leading to the well known spills+
3843 * branches problem. Testcase: mcs crash in
3844 * System.MonoCustomAttrs:GetCustomAttributes.
3846 regs
= mono_arch_get_global_int_regs (cfg
);
3848 cfg
->got_var
->opcode
= OP_REGVAR
;
3849 cfg
->got_var
->dreg
= GPOINTER_TO_INT (regs
->data
);
3850 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
3856 * Have to call this again to process variables added since the first call.
3858 mono_liveness_handle_exception_clauses (cfg
);
3860 if (cfg
->globalra
) {
3863 /* Have to do this before regalloc since it can create vregs */
3864 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3865 mono_arch_lowering_pass (cfg
, bb
);
3867 mono_global_regalloc (cfg
);
3870 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
3873 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
3874 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
3875 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
3876 mono_analyze_liveness (cfg
);
3878 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
3879 regs
= mono_arch_get_global_int_regs (cfg
);
3881 regs
= g_list_delete_link (regs
, regs
);
3882 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
3886 //mono_print_code (cfg, "");
3890 /* variables are allocated after decompose, since decompose could create temps */
3891 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
3892 mono_arch_allocate_vars (cfg
);
3893 if (cfg
->exception_type
)
3899 gboolean need_local_opts
;
3901 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
3902 mono_spill_global_vars (cfg
, &need_local_opts
);
3904 if (need_local_opts
|| cfg
->compile_aot
) {
3905 /* To optimize code created by spill_global_vars */
3906 mono_local_cprop (cfg
);
3907 if (cfg
->opt
& MONO_OPT_DEADCE
)
3908 mono_local_deadce (cfg
);
3912 /* Add branches between non-consecutive bblocks */
3913 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3914 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
3915 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
3916 /* we are careful when inverting, since bugs like #59580
3917 * could show up when dealing with NaNs.
3919 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
3920 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
3921 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
3922 bb
->last_ins
->inst_false_bb
= tmp
;
3924 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
3926 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
3927 inst
->opcode
= OP_BR
;
3928 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
3929 mono_bblock_add_inst (bb
, inst
);
3934 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
3935 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3936 MonoInst
*tree
= bb
->code
;
3937 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
3940 for (; tree
; tree
= tree
->next
) {
3941 mono_print_ins_index (-1, tree
);
3947 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3948 bb
->max_vreg
= cfg
->next_vreg
;
3952 if (COMPILE_LLVM (cfg
)) {
3956 /* The IR has to be in SSA form for LLVM */
3957 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
3958 cfg
->exception_message
= g_strdup ("SSA disabled.");
3959 cfg
->disable_llvm
= TRUE
;
3962 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
3963 mono_decompose_array_access_opts (cfg
);
3965 if (!cfg
->disable_llvm
)
3966 mono_llvm_emit_method (cfg
);
3967 if (!cfg
->disable_llvm
&& header
->num_clauses
&& !cfg
->compile_aot
&& cfg
->llvm_ex_info_len
!= header
->num_clauses
) {
3968 cfg
->exception_message
= g_strdup ("clause num mismatch.");
3969 cfg
->disable_llvm
= TRUE
;
3971 if (cfg
->disable_llvm
) {
3972 if (cfg
->verbose_level
>= 1) {
3973 //nm = mono_method_full_name (cfg->method, TRUE);
3974 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
3977 InterlockedIncrement (&methods_without_llvm
);
3978 mono_destroy_compile (cfg
);
3980 goto restart_compile
;
3983 InterlockedIncrement (&methods_with_llvm
);
3985 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
3986 nm
= mono_method_full_name (cfg
->method
, TRUE
);
3987 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
3989 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3997 if (cfg
->verbose_level
>= 2) {
3998 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
3999 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
4003 if (cfg
->generic_sharing_context
)
4004 generic_info_size
= sizeof (MonoGenericJitInfo
);
4006 generic_info_size
= 0;
4008 if (cfg
->method
->dynamic
) {
4009 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (header
->num_clauses
* sizeof (MonoJitExceptionInfo
)) +
4012 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
4013 (header
->num_clauses
* sizeof (MonoJitExceptionInfo
)) +
4017 jinfo
->method
= method_to_register
;
4018 jinfo
->code_start
= cfg
->native_code
;
4019 jinfo
->code_size
= cfg
->code_len
;
4020 jinfo
->used_regs
= cfg
->used_int_regs
;
4021 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
4022 jinfo
->cas_inited
= FALSE
; /* initialization delayed at the first stalk walk using this method */
4023 jinfo
->num_clauses
= header
->num_clauses
;
4024 if (COMPILE_LLVM (cfg
))
4025 jinfo
->from_llvm
= TRUE
;
4027 if (cfg
->generic_sharing_context
) {
4029 MonoGenericJitInfo
*gi
;
4031 jinfo
->has_generic_jit_info
= 1;
4033 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
4036 gi
->generic_sharing_context
= cfg
->generic_sharing_context
;
4038 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
4039 mini_method_get_context (method_to_compile
)->method_inst
||
4040 method_to_compile
->klass
->valuetype
) {
4041 g_assert (cfg
->rgctx_var
);
4046 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
4047 mini_method_get_context (method_to_compile
)->method_inst
||
4048 method_to_compile
->klass
->valuetype
) {
4049 inst
= cfg
->rgctx_var
;
4050 g_assert (inst
->opcode
== OP_REGOFFSET
);
4052 inst
= cfg
->args
[0];
4055 if (inst
->opcode
== OP_REGVAR
) {
4056 gi
->this_in_reg
= 1;
4057 gi
->this_reg
= inst
->dreg
;
4059 g_assert (inst
->opcode
== OP_REGOFFSET
);
4061 g_assert (inst
->inst_basereg
== X86_EBP
);
4062 #elif defined(TARGET_AMD64)
4063 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
4065 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
4067 gi
->this_in_reg
= 0;
4068 gi
->this_reg
= inst
->inst_basereg
;
4069 gi
->this_offset
= inst
->inst_offset
;
4073 if (header
->num_clauses
) {
4076 for (i
= 0; i
< header
->num_clauses
; i
++) {
4077 MonoExceptionClause
*ec
= &header
->clauses
[i
];
4078 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
4079 MonoBasicBlock
*tblock
;
4082 ei
->flags
= ec
->flags
;
4084 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
4085 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
4087 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4088 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
4090 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
4092 ei
->data
.catch_class
= ec
->data
.catch_class
;
4095 if (COMPILE_LLVM (cfg
)) {
4096 if (!cfg
->compile_aot
) {
4097 g_assert (cfg
->llvm_ex_info
&& i
< cfg
->llvm_ex_info_len
);
4098 ei
->try_start
= cfg
->llvm_ex_info
[i
].try_start
;
4099 ei
->try_end
= cfg
->llvm_ex_info
[i
].try_end
;
4100 ei
->handler_start
= cfg
->llvm_ex_info
[i
].handler_start
;
4103 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
4105 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
4106 g_assert (tblock
->native_offset
);
4107 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
4109 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
4110 g_assert (tblock
->native_offset
);
4111 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
4113 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
4119 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
4120 * using it during runtime, hence the define.
4122 #ifdef MONO_ARCH_HAVE_XP_UNWIND
4123 if (cfg
->encoded_unwind_ops
) {
4124 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
4125 g_free (cfg
->encoded_unwind_ops
);
4126 } else if (cfg
->unwind_ops
) {
4128 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
4130 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
4131 g_free (unwind_info
);
4135 cfg
->jit_info
= jinfo
;
4137 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
4138 if (cfg
->extend_live_ranges
) {
4139 /* Extend live ranges to cover the whole method */
4140 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
4141 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
4145 mono_save_xdebug_info (cfg
);
4147 mini_gc_create_gc_map (cfg
);
4149 mono_save_seq_point_info (cfg
);
4151 if (!cfg
->compile_aot
) {
4152 mono_domain_lock (cfg
->domain
);
4153 mono_jit_info_table_add (cfg
->domain
, jinfo
);
4155 if (cfg
->method
->dynamic
)
4156 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= jinfo
;
4157 mono_domain_unlock (cfg
->domain
);
4160 /* collect statistics */
4161 mono_perfcounters
->jit_methods
++;
4162 mono_perfcounters
->jit_bytes
+= header
->code_size
;
4163 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
4164 code_size_ratio
= cfg
->code_len
;
4165 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
4166 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
4167 g_free (mono_jit_stats
.biggest_method
);
4168 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4170 code_size_ratio
= (code_size_ratio
* 100) / mono_method_get_header (method
)->code_size
;
4171 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
4172 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
4173 g_free (mono_jit_stats
.max_ratio_method
);
4174 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
4176 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
4178 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
4179 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4187 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4189 g_assert_not_reached ();
4193 #endif /* DISABLE_JIT */
4196 lookup_generic_method (MonoDomain
*domain
, MonoMethod
*method
)
4198 MonoMethod
*open_method
;
4200 if (!mono_method_is_generic_sharable_impl (method
, FALSE
))
4203 open_method
= mono_method_get_declaring_generic_method (method
);
4205 return mono_domain_lookup_shared_generic (domain
, open_method
);
4209 * LOCKING: Assumes domain->jit_code_hash_lock is held.
4212 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
4214 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
4219 return lookup_generic_method (domain
, method
);
4223 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
4227 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
4228 mono_domain_jit_code_hash_lock (domain
);
4229 info
= lookup_method_inner (domain
, method
);
4230 mono_domain_jit_code_hash_unlock (domain
);
4231 mono_loader_unlock ();
4237 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
4240 gpointer code
= NULL
;
4241 MonoJitInfo
*jinfo
, *info
;
4243 MonoException
*ex
= NULL
;
4244 guint32 prof_options
;
4246 #ifdef MONO_USE_AOT_COMPILER
4247 if (opt
& MONO_OPT_AOT
) {
4248 MonoDomain
*domain
= mono_domain_get ();
4250 mono_class_init (method
->klass
);
4252 if ((code
= mono_aot_get_method (domain
, method
))) {
4253 vtable
= mono_class_vtable (domain
, method
->klass
);
4255 mono_runtime_class_init (vtable
);
4262 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4263 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
4265 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
4267 if (!piinfo
->addr
) {
4268 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
4269 piinfo
->addr
= mono_lookup_internal_call (method
);
4270 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
4272 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
);
4274 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
);
4277 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
4279 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
4280 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4282 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
4283 //mono_debug_add_wrapper (method, nm);
4284 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
4285 const char *name
= method
->name
;
4288 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
4289 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
4290 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
4293 * We need to make sure this wrapper
4294 * is compiled because it might end up
4295 * in an (M)RGCTX if generic sharing
4296 * is enabled, and would be called
4297 * indirectly. If it were a
4298 * trampoline we'd try to patch that
4299 * indirect call, which is not
4302 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
4303 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
4304 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
4305 return mono_create_delegate_trampoline (method
->klass
);
4307 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
4308 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4310 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
4311 nm
= mono_marshal_get_delegate_begin_invoke (method
);
4312 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4313 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
4314 nm
= mono_marshal_get_delegate_end_invoke (method
);
4315 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
4321 if (mono_aot_only
) {
4322 char *fullname
= mono_method_full_name (method
, TRUE
);
4323 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only.\n", fullname
);
4325 *jit_ex
= mono_get_exception_execution_engine (msg
);
4332 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
4334 switch (cfg
->exception_type
) {
4335 case MONO_EXCEPTION_NONE
:
4337 case MONO_EXCEPTION_TYPE_LOAD
:
4338 case MONO_EXCEPTION_MISSING_FIELD
:
4339 case MONO_EXCEPTION_MISSING_METHOD
:
4340 case MONO_EXCEPTION_FILE_NOT_FOUND
:
4341 case MONO_EXCEPTION_BAD_IMAGE
: {
4342 /* Throw a type load exception if needed */
4343 MonoLoaderError
*error
= mono_loader_get_last_error ();
4346 ex
= mono_loader_error_prepare_exception (error
);
4348 if (cfg
->exception_ptr
) {
4349 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
4351 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
4352 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
4353 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
4354 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
4355 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
4356 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
4357 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
4358 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
4359 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
4360 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
4362 g_assert_not_reached ();
4367 case MONO_EXCEPTION_INVALID_PROGRAM
:
4368 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
4370 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
4371 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
4373 case MONO_EXCEPTION_METHOD_ACCESS
:
4374 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
4376 case MONO_EXCEPTION_FIELD_ACCESS
:
4377 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
4379 /* this can only be set if the security manager is active */
4380 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
4381 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
4382 MonoObject
*exc
= NULL
;
4385 args
[0] = &cfg
->exception_data
;
4387 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
4389 ex
= (MonoException
*)exc
;
4392 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
4393 MonoException
*exp
= cfg
->exception_ptr
;
4394 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
4400 g_assert_not_reached ();
4404 mono_destroy_compile (cfg
);
4407 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4408 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
4413 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
4414 mono_domain_lock (target_domain
);
4416 /* Check if some other thread already did the job. In this case, we can
4417 discard the code this thread generated. */
4419 mono_domain_jit_code_hash_lock (target_domain
);
4421 info
= lookup_method_inner (target_domain
, method
);
4423 /* We can't use a domain specific method in another domain */
4424 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
4425 code
= info
->code_start
;
4426 // printf("Discarding code for method %s\n", method->name);
4431 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
4432 mono_domain_jit_code_hash_unlock (target_domain
);
4433 code
= cfg
->native_code
;
4435 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl (method
, FALSE
))
4436 mono_stats
.generics_shared_methods
++;
4438 mono_domain_jit_code_hash_unlock (target_domain
);
4441 jinfo
= cfg
->jit_info
;
4443 prof_options
= cfg
->prof_options
;
4445 mono_destroy_compile (cfg
);
4447 if (domain_jit_info (target_domain
)->jump_target_hash
) {
4448 MonoJumpInfo patch_info
;
4450 list
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4452 patch_info
.next
= NULL
;
4453 patch_info
.ip
.i
= 0;
4454 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
4455 patch_info
.data
.method
= method
;
4456 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
4458 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
4459 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, TRUE
);
4460 g_slist_free (list
);
4463 mono_domain_unlock (target_domain
);
4464 mono_loader_unlock ();
4466 vtable
= mono_class_vtable (target_domain
, method
->klass
);
4469 exc
= mono_class_get_exception_for_failure (method
->klass
);
4471 mono_raise_exception (exc
);
4474 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
)
4475 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
4477 mono_runtime_class_init (vtable
);
4482 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
4484 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
4487 MonoJitICallInfo
*callinfo
= NULL
;
4490 * ICALL wrappers are handled specially, since there is only one copy of them
4491 * shared by all appdomains.
4493 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
4494 const char *icall_name
;
4496 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
4497 g_assert (icall_name
);
4498 callinfo
= mono_find_jit_icall_by_name (icall_name
);
4499 g_assert (callinfo
);
4501 /* Must be domain neutral since there is only one copy */
4502 opt
|= MONO_OPT_SHARED
;
4505 if (opt
& MONO_OPT_SHARED
)
4506 target_domain
= mono_get_root_domain ();
4508 target_domain
= domain
;
4510 info
= lookup_method (target_domain
, method
);
4512 /* We can't use a domain specific method in another domain */
4513 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4516 mono_jit_stats
.methods_lookups
++;
4517 vtable
= mono_class_vtable (domain
, method
->klass
);
4519 mono_runtime_class_init (vtable
);
4520 return mono_create_ftnptr (target_domain
, info
->code_start
);
4524 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
4528 p
= mono_create_ftnptr (target_domain
, code
);
4532 if (!callinfo
->wrapper
) {
4533 callinfo
->wrapper
= p
;
4534 mono_register_jit_icall_wrapper (callinfo
, p
);
4535 mono_debug_add_icall_wrapper (method
, callinfo
);
4544 mono_jit_compile_method (MonoMethod
*method
)
4546 MonoException
*ex
= NULL
;
4549 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
4552 mono_raise_exception (ex
);
4558 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4560 invalidated_delegate_trampoline (char *desc
)
4562 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
4563 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
4569 * mono_jit_free_method:
4571 * Free all memory allocated by the JIT for METHOD.
4574 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
4576 MonoJitDynamicMethodInfo
*ji
;
4577 gboolean destroy
= TRUE
;
4579 g_assert (method
->dynamic
);
4581 mono_domain_lock (domain
);
4582 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
4583 mono_domain_unlock (domain
);
4587 mono_domain_lock (domain
);
4588 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
4589 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
4590 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
4591 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
4592 mono_domain_unlock (domain
);
4594 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
4595 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
4597 * Instead of freeing the code, change it to call an error routine
4598 * so people can fix their code.
4600 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
4601 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
4604 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
4610 * This needs to be done before freeing code_mp, since the code address is the
4611 * key in the table, so if we free the code_mp first, another thread can grab the
4612 * same code address and replace our entry in the table.
4614 mono_jit_info_table_remove (domain
, ji
->ji
);
4617 mono_code_manager_destroy (ji
->code_mp
);
4622 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
4624 MonoDomain
*target_domain
;
4627 if (default_opt
& MONO_OPT_SHARED
)
4628 target_domain
= mono_get_root_domain ();
4630 target_domain
= domain
;
4632 info
= lookup_method (target_domain
, method
);
4634 /* We can't use a domain specific method in another domain */
4635 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
4636 mono_jit_stats
.methods_lookups
++;
4639 return info
->code_start
;
4649 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
4651 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
4656 gpointer compiled_method
;
4657 gpointer runtime_invoke
;
4659 MonoDynCallInfo
*dyn_call_info
;
4660 MonoClass
*ret_box_class
;
4661 } RuntimeInvokeInfo
;
4664 * mono_jit_runtime_invoke:
4665 * @method: the method to invoke
4666 * @obj: this pointer
4667 * @params: array of parameter values.
4668 * @exc: used to catch exceptions objects
4671 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
4674 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
4675 MonoDomain
*domain
= mono_domain_get ();
4676 MonoJitDomainInfo
*domain_info
;
4677 RuntimeInvokeInfo
*info
, *info2
;
4679 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
4680 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
4684 domain_info
= domain_jit_info (domain
);
4686 mono_domain_lock (domain
);
4687 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
4688 mono_domain_unlock (domain
);
4691 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
4693 * This might be redundant since mono_class_vtable () already does this,
4694 * but keep it just in case for moonlight.
4696 mono_class_setup_vtable (method
->klass
);
4697 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
4699 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
4701 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
4706 info
= g_new0 (RuntimeInvokeInfo
, 1);
4708 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
4709 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
4710 g_assert (info
->vtable
);
4712 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
4713 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
4715 * Array Get/Set/Address methods. The JIT implements them using inline code
4716 * inside the runtime invoke wrappers, so no need to compile them.
4718 info
->compiled_method
= NULL
;
4720 MonoException
*jit_ex
= NULL
;
4722 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
4723 if (!info
->compiled_method
) {
4727 *exc
= (MonoObject
*)jit_ex
;
4730 mono_raise_exception (jit_ex
);
4734 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
4735 info
->compiled_method
= mono_create_static_rgctx_trampoline (method
, info
->compiled_method
);
4739 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
4740 * in full-aot mode, so we use a slower, but more generic wrapper if
4741 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
4743 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4744 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
4745 MonoMethodSignature
*sig
= mono_method_signature (method
);
4746 gboolean supported
= TRUE
;
4749 if (method
->string_ctor
)
4750 sig
= mono_marshal_get_string_ctor_signature (method
);
4752 for (i
= 0; i
< sig
->param_count
; ++i
) {
4753 MonoType
*t
= sig
->params
[i
];
4755 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
4759 if (method
->klass
->contextbound
|| !info
->compiled_method
)
4763 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
4765 if (info
->dyn_call_info
) {
4766 switch (sig
->ret
->type
) {
4767 case MONO_TYPE_VOID
:
4779 case MONO_TYPE_BOOLEAN
:
4780 case MONO_TYPE_CHAR
:
4783 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
4786 info
->ret_box_class
= mono_defaults
.int_class
;
4788 case MONO_TYPE_STRING
:
4789 case MONO_TYPE_CLASS
:
4790 case MONO_TYPE_ARRAY
:
4791 case MONO_TYPE_SZARRAY
:
4792 case MONO_TYPE_OBJECT
:
4794 case MONO_TYPE_GENERICINST
:
4795 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
4796 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
4798 case MONO_TYPE_VALUETYPE
:
4799 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
4802 g_assert_not_reached ();
4809 if (!info
->dyn_call_info
)
4810 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
4812 mono_domain_lock (domain
);
4813 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
4818 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
4820 mono_domain_unlock (domain
);
4823 runtime_invoke
= info
->runtime_invoke
;
4826 * We need this here because mono_marshal_get_runtime_invoke can place
4827 * the helper method in System.Object and not the target class.
4829 mono_runtime_class_init (info
->vtable
);
4831 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4832 if (info
->dyn_call_info
) {
4833 MonoMethodSignature
*sig
= mono_method_signature (method
);
4835 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
4838 guint8 retval
[128];
4840 if (!dyn_runtime_invoke
) {
4841 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
4842 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
4845 /* Convert the arguments to the format expected by start_dyn_call () */
4846 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
4849 args
[pindex
++] = &obj
;
4850 for (i
= 0; i
< sig
->param_count
; ++i
) {
4851 MonoType
*t
= sig
->params
[i
];
4854 args
[pindex
++] = ¶ms
[i
];
4855 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
4856 args
[pindex
++] = ¶ms
[i
];
4858 args
[pindex
++] = params
[i
];
4862 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
4864 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
4866 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
4868 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
4870 if (info
->ret_box_class
)
4871 return mono_value_box (domain
, info
->ret_box_class
, retval
);
4873 return *(MonoObject
**)retval
;
4877 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
4881 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
4883 MonoException
*exc
= NULL
;
4885 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
4890 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
4892 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
4893 if (mono_arch_is_int_overflow (ctx
, info
))
4894 exc
= mono_get_exception_arithmetic ();
4896 exc
= mono_get_exception_divide_by_zero ();
4898 exc
= mono_get_exception_divide_by_zero ();
4902 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
4905 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
4908 mono_arch_handle_exception (ctx
, exc
, FALSE
);
4912 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
4917 exc
= mono_get_exception_execution_engine ("SIGILL");
4919 mono_arch_handle_exception (ctx
, exc
, FALSE
);
4923 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
4925 #ifndef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4926 MonoException
*exc
= NULL
;
4929 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
4933 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
4934 if (mono_arch_is_single_step_event (info
, ctx
)) {
4935 mono_debugger_agent_single_step_event (ctx
);
4937 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
4938 mono_debugger_agent_breakpoint_hit (ctx
);
4943 /* The thread might no be registered with the runtime */
4944 if (!mono_domain_get () || !jit_tls
) {
4945 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
4947 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
4950 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
4952 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4953 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
4956 /* The hard-guard page has been hit: there is not much we can do anymore
4957 * Print a hopefully clear message and abort.
4959 if (jit_tls
->stack_size
&&
4960 ABS ((guint8
*)info
->si_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 32768) {
4962 /* we don't do much now, but we can warn the user with a useful message */
4963 fprintf (stderr
, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx
), (gpointer
)info
->si_addr
);
4964 if (ji
&& ji
->method
)
4965 method
= mono_method_full_name (ji
->method
, TRUE
);
4967 method
= "Unmanaged";
4968 fprintf (stderr
, "At %s\n", method
);
4971 /* The original handler might not like that it is executed on an altstack... */
4972 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
4975 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
4980 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
4983 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
4986 mono_arch_handle_exception (ctx
, exc
, FALSE
);
4991 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
4996 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
4998 mono_arch_handle_exception (ctx
, exc
, FALSE
);
5001 /* mono_jit_create_remoting_trampoline:
5002 * @method: pointer to the method info
5004 * Creates a trampoline which calls the remoting functions. This
5005 * is used in the vtable of transparent proxies.
5007 * Returns: a pointer to the newly created code
5010 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
5013 guint8
*addr
= NULL
;
5015 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
5016 return mono_arch_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
5020 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
5021 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
5022 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
5023 addr
= mono_compile_method (nm
);
5025 addr
= mono_compile_method (method
);
5027 return mono_get_addr_from_ftnptr (addr
);
5030 #ifdef MONO_ARCH_HAVE_IMT
5031 static G_GNUC_UNUSED gpointer
5032 mini_get_imt_trampoline (void)
5034 static gpointer tramp
= NULL
;
5036 tramp
= mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5042 mini_get_vtable_trampoline (void)
5044 static gpointer tramp
= NULL
;
5046 tramp
= mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD
, MONO_TRAMPOLINE_JIT
, mono_get_root_domain (), NULL
);
5051 mini_parse_debug_options (void)
5053 char *options
= getenv ("MONO_DEBUG");
5054 gchar
**args
, **ptr
;
5059 args
= g_strsplit (options
, ",", -1);
5061 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
5062 const char *arg
= *ptr
;
5064 if (!strcmp (arg
, "handle-sigint"))
5065 debug_options
.handle_sigint
= TRUE
;
5066 else if (!strcmp (arg
, "keep-delegates"))
5067 debug_options
.keep_delegates
= TRUE
;
5068 else if (!strcmp (arg
, "collect-pagefault-stats"))
5069 debug_options
.collect_pagefault_stats
= TRUE
;
5070 else if (!strcmp (arg
, "break-on-unverified"))
5071 debug_options
.break_on_unverified
= TRUE
;
5072 else if (!strcmp (arg
, "no-gdb-backtrace"))
5073 debug_options
.no_gdb_backtrace
= TRUE
;
5074 else if (!strcmp (arg
, "suspend-on-sigsegv"))
5075 debug_options
.suspend_on_sigsegv
= TRUE
;
5076 else if (!strcmp (arg
, "dont-free-domains"))
5077 mono_dont_free_domains
= TRUE
;
5078 else if (!strcmp (arg
, "dyn-runtime-invoke"))
5079 debug_options
.dyn_runtime_invoke
= TRUE
;
5080 else if (!strcmp (arg
, "gdb"))
5081 debug_options
.gdb
= TRUE
;
5082 else if (!strcmp (arg
, "explicit-null-checks"))
5083 debug_options
.explicit_null_checks
= TRUE
;
5084 else if (!strcmp (arg
, "gen-seq-points"))
5085 debug_options
.gen_seq_points
= TRUE
;
5087 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
5088 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'\n");
5097 mini_get_debug_options (void)
5099 return &debug_options
;
5103 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
5108 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
5114 #elif defined(__ppc64__) || defined(__powerpc64__)
5117 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
5130 mini_get_addr_from_ftnptr (gpointer descr
)
5132 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
5133 return *(gpointer
*)descr
;
5139 static void runtime_invoke_info_free (gpointer value
);
5142 mini_create_jit_domain_info (MonoDomain
*domain
)
5144 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
5146 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5147 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5148 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5149 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5150 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5151 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
5152 info
->seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5153 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5155 domain
->runtime_info
= info
;
5159 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
5161 g_slist_free (value
);
5165 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
5167 MonoJitDynamicMethodInfo
*di
= value
;
5168 mono_code_manager_destroy (di
->code_mp
);
5173 runtime_invoke_info_free (gpointer value
)
5175 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
5177 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
5178 if (info
->dyn_call_info
)
5179 mono_arch_dyn_call_free (info
->dyn_call_info
);
5185 mini_free_jit_domain_info (MonoDomain
*domain
)
5187 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
5189 if (info
->jump_target_hash
) {
5190 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
5191 g_hash_table_destroy (info
->jump_target_hash
);
5193 if (info
->jump_target_got_slot_hash
) {
5194 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
5195 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
5197 if (info
->dynamic_code_hash
) {
5198 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
5199 g_hash_table_destroy (info
->dynamic_code_hash
);
5201 if (info
->method_code_hash
)
5202 g_hash_table_destroy (info
->method_code_hash
);
5203 g_hash_table_destroy (info
->class_init_trampoline_hash
);
5204 g_hash_table_destroy (info
->jump_trampoline_hash
);
5205 g_hash_table_destroy (info
->jit_trampoline_hash
);
5206 g_hash_table_destroy (info
->delegate_trampoline_hash
);
5207 if (info
->static_rgctx_trampoline_hash
)
5208 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
5209 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
5210 g_hash_table_destroy (info
->runtime_invoke_hash
);
5212 if (info
->agent_info
)
5213 mono_debugger_agent_free_domain_info (domain
);
5215 g_free (domain
->runtime_info
);
5216 domain
->runtime_info
= NULL
;
5220 mini_init (const char *filename
, const char *runtime_version
)
5223 MonoRuntimeCallbacks callbacks
;
5225 MONO_PROBE_VES_INIT_BEGIN ();
5228 if (access ("/proc/self/maps", F_OK
) != 0) {
5229 g_print ("Mono requires /proc to be mounted.\n");
5234 /* Happens when using the embedding interface */
5235 if (!default_opt_set
)
5236 default_opt
= mono_parse_default_optimizations (NULL
);
5238 InitializeCriticalSection (&jit_mutex
);
5240 #ifdef MONO_DEBUGGER_SUPPORTED
5241 if (mini_debug_running_inside_mdb ())
5242 mini_debugger_init ();
5245 #ifdef MONO_ARCH_HAVE_TLS_GET
5246 mono_runtime_set_has_tls_get (TRUE
);
5248 mono_runtime_set_has_tls_get (FALSE
);
5251 if (!global_codeman
)
5252 global_codeman
= mono_code_manager_new ();
5253 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
5255 memset (&callbacks
, 0, sizeof (callbacks
));
5256 callbacks
.create_ftnptr
= mini_create_ftnptr
;
5257 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
5258 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
5260 mono_install_callbacks (&callbacks
);
5262 mono_arch_cpu_init ();
5266 mono_unwind_init ();
5270 if (getenv ("MONO_DEBUG") != NULL
)
5271 mini_parse_debug_options ();
5273 if (getenv ("MONO_XDEBUG")) {
5274 char *xdebug_opts
= getenv ("MONO_XDEBUG");
5275 mono_xdebug_init (xdebug_opts
);
5276 /* So methods for multiple domains don't have the same address */
5277 mono_dont_free_domains
= TRUE
;
5278 mono_using_xdebug
= TRUE
;
5279 } else if (mini_get_debug_options ()->gdb
) {
5280 mono_xdebug_init ((char*)"gdb");
5281 mono_dont_free_domains
= TRUE
;
5282 mono_using_xdebug
= TRUE
;
5289 mono_trampolines_init ();
5291 if (!g_thread_supported ())
5292 g_thread_init (NULL
);
5294 mono_gc_base_init ();
5296 mono_jit_tls_id
= TlsAlloc ();
5297 setup_jit_tls_data ((gpointer
)-1, mono_thread_abort
);
5299 if (default_opt
& MONO_OPT_AOT
)
5302 mono_debugger_agent_init ();
5304 #ifdef MONO_ARCH_GSHARED_SUPPORTED
5305 mono_set_generic_sharing_supported (TRUE
);
5308 #ifndef MONO_CROSS_COMPILE
5309 mono_runtime_install_handlers ();
5311 mono_threads_install_cleanup (mini_thread_cleanup
);
5313 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
5314 // This is experimental code so provide an env var to switch it off
5315 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
5316 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
5318 check_for_pending_exc
= FALSE
;
5319 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
5323 #define JIT_TRAMPOLINES_WORK
5324 #ifdef JIT_TRAMPOLINES_WORK
5325 mono_install_compile_method (mono_jit_compile_method
);
5326 mono_install_free_method (mono_jit_free_method
);
5327 #ifdef MONO_ARCH_LLVM_SUPPORTED
5329 /* The runtime currently only uses this for filling out vtables */
5330 mono_install_trampoline (mono_create_llvm_vcall_trampoline
);
5332 mono_install_trampoline (mono_create_jit_trampoline
);
5334 mono_install_trampoline (mono_create_jit_trampoline
);
5336 mono_install_jump_trampoline (mono_create_jump_trampoline
);
5337 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
5338 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
5339 mono_install_create_domain_hook (mini_create_jit_domain_info
);
5340 mono_install_free_domain_hook (mini_free_jit_domain_info
);
5342 #define JIT_INVOKE_WORKS
5343 #ifdef JIT_INVOKE_WORKS
5344 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
5346 mono_install_stack_walk (mono_jit_walk_stack
);
5347 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
5348 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
5349 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
5351 if (runtime_version
)
5352 domain
= mono_init_version (filename
, runtime_version
);
5354 domain
= mono_init_from_assembly (filename
, filename
);
5356 if (mono_aot_only
) {
5357 /* This helps catch code allocation requests */
5358 mono_code_manager_set_read_only (domain
->code_mp
);
5361 #ifdef MONO_ARCH_HAVE_IMT
5364 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
5366 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
5367 if (!mono_use_llvm
) {
5368 /* LLVM needs a per-method vtable trampoline */
5369 mono_install_vtable_trampoline (mini_get_vtable_trampoline ());
5371 * The imt code in mono_magic_trampoline () can't handle LLVM code. By disabling
5372 * this, we force iface calls to go through the llvm vcall trampoline.
5374 mono_install_imt_trampoline (mini_get_imt_trampoline ());
5379 /* This must come after mono_init () in the aot-only case */
5380 mono_exceptions_init ();
5381 mono_install_handler (mono_get_throw_exception ());
5385 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
5386 ves_icall_get_frame_info
);
5387 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
5388 ves_icall_get_trace
);
5389 mono_add_internal_call ("System.Exception::get_trace",
5390 ves_icall_System_Exception_get_trace
);
5391 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
5392 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
5393 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
5394 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
5395 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
5396 mono_runtime_install_handlers
);
5399 create_helper_signature ();
5401 #define JIT_CALLS_WORK
5402 #ifdef JIT_CALLS_WORK
5403 /* Needs to be called here since register_jit_icall depends on it */
5404 mono_marshal_init ();
5406 mono_arch_register_lowlevel_calls ();
5407 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
5408 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
5409 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
5410 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
5411 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
5412 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "void", TRUE
);
5413 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
5415 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
5416 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
5417 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE
);
5418 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
5419 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
5422 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
5423 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
5424 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
5425 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
5426 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
5429 * NOTE, NOTE, NOTE, NOTE:
5430 * when adding emulation for some opcodes, remember to also add a dummy
5431 * rule to the burg files, because we need the arity information to be correct.
5433 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
5434 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
5435 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
5436 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
5437 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
5438 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
5439 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
5440 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
5443 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
5444 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
5445 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
5446 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
5449 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
5450 mono_register_opcode_emulation (CEE_DIV
, "__emul_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5451 mono_register_opcode_emulation (CEE_DIV_UN
, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5452 mono_register_opcode_emulation (CEE_REM
, "__emul_irem", "int32 int32 int32", mono_irem
, FALSE
);
5453 mono_register_opcode_emulation (CEE_REM_UN
, "__emul_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5454 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
5455 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
5456 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
5457 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
5460 #ifdef MONO_ARCH_EMULATE_MUL_DIV
5461 mono_register_opcode_emulation (CEE_MUL
, "__emul_imul", "int32 int32 int32", mono_imul
, TRUE
);
5462 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
5465 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
5466 mono_register_opcode_emulation (CEE_MUL_OVF
, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5467 mono_register_opcode_emulation (CEE_MUL_OVF_UN
, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5468 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
5469 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
5472 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
5473 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
5476 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
5477 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
5478 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
5479 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
5481 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
5482 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
5484 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
5485 mono_register_opcode_emulation (CEE_CONV_R_UN
, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5486 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
5488 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
5489 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
5491 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
5492 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
5494 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
5495 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
5497 #ifdef MONO_ARCH_EMULATE_FREM
5498 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
5501 #ifdef MONO_ARCH_SOFT_FLOAT
5502 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
5503 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
5504 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
5505 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
5506 mono_register_opcode_emulation (CEE_CONV_R8
, "__emul_conv_r8", "double int32", mono_conv_to_r8
, FALSE
);
5507 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
5508 mono_register_opcode_emulation (CEE_CONV_R4
, "__emul_conv_r4", "double int32", mono_conv_to_r4
, FALSE
);
5509 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
5510 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
5511 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
5512 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
5513 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
5514 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
5515 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
5516 #if SIZEOF_VOID_P == 4
5517 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
5520 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
5521 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
5522 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
5523 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
5524 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
5525 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
5526 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
5527 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
5528 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
5529 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
5531 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
5532 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
5533 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
5534 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
5535 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
5537 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
5538 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
5539 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
5540 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
5543 #if SIZEOF_REGISTER == 4
5544 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
5547 /* other jit icalls */
5548 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
5549 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
5550 "ptr ptr ptr", FALSE
);
5551 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
5552 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
5553 "ptr ptr ptr ptr", FALSE
);
5554 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
5555 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
5556 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
5557 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
5558 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
5559 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
5560 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
5561 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
5562 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
5563 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
5564 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
5565 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
5566 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
5567 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
5568 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
5569 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
5570 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
5571 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
5572 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
5573 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
5574 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
5575 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
5576 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
5577 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
5580 mono_generic_sharing_init ();
5582 #ifdef MONO_ARCH_SIMD_INTRINSICS
5583 mono_simd_intrinsics_init ();
5586 #if MONO_SUPPORT_TASKLETS
5587 mono_tasklets_init ();
5590 if (mono_compile_aot
)
5592 * Avoid running managed code when AOT compiling, since the platform
5593 * might only support aot-only execution.
5595 mono_runtime_set_no_exec (TRUE
);
5597 #define JIT_RUNTIME_WORKS
5598 #ifdef JIT_RUNTIME_WORKS
5599 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
5600 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
5601 mono_thread_attach (domain
);
5604 mono_profiler_runtime_initialized ();
5606 MONO_PROBE_VES_INIT_END ();
5611 MonoJitStats mono_jit_stats
= {0};
5614 print_jit_stats (void)
5616 if (mono_jit_stats
.enabled
) {
5617 g_print ("Mono Jit statistics\n");
5618 g_print ("Compiled methods: %ld\n", mono_jit_stats
.methods_compiled
);
5619 g_print ("Methods from AOT: %ld\n", mono_jit_stats
.methods_aot
);
5620 g_print ("Methods cache lookup: %ld\n", mono_jit_stats
.methods_lookups
);
5621 g_print ("Method trampolines: %ld\n", mono_jit_stats
.method_trampolines
);
5622 g_print ("Basic blocks: %ld\n", mono_jit_stats
.basic_blocks
);
5623 g_print ("Max basic blocks: %ld\n", mono_jit_stats
.max_basic_blocks
);
5624 g_print ("Allocated vars: %ld\n", mono_jit_stats
.allocate_var
);
5625 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats
.cil_code_size
);
5626 g_print ("Native code size: %ld\n", mono_jit_stats
.native_code_size
);
5627 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
5628 mono_jit_stats
.max_ratio_method
);
5629 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
5630 mono_jit_stats
.biggest_method
);
5631 g_print ("Code reallocs: %ld\n", mono_jit_stats
.code_reallocs
);
5632 g_print ("Allocated code size: %ld\n", mono_jit_stats
.allocated_code_size
);
5633 g_print ("Inlineable methods: %ld\n", mono_jit_stats
.inlineable_methods
);
5634 g_print ("Inlined methods: %ld\n", mono_jit_stats
.inlined_methods
);
5635 g_print ("Regvars: %ld\n", mono_jit_stats
.regvars
);
5636 g_print ("Locals stack size: %ld\n", mono_jit_stats
.locals_stack_size
);
5638 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
5639 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
5640 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
5641 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
5642 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
5643 g_print ("Methods: %ld\n", mono_stats
.method_count
);
5644 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
5645 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
5646 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
5648 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
5649 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
5650 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
5652 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
5653 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
5654 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
5656 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
5657 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
5658 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
5660 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
5661 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
5662 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
5663 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
5664 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
5665 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
5666 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
5667 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
5669 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
5670 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
5671 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
5673 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
5675 g_print ("Minor GC collections: %ld\n", mono_stats
.minor_gc_count
);
5677 g_print ("Major GC collections: %ld\n", mono_stats
.major_gc_count
);
5679 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats
.minor_gc_time_usecs
/ 1000.0);
5681 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats
.major_gc_time_usecs
/ 1000.0);
5682 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
5683 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
5684 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
5685 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
5686 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
5687 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
5688 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
5691 g_free (mono_jit_stats
.max_ratio_method
);
5692 mono_jit_stats
.max_ratio_method
= NULL
;
5693 g_free (mono_jit_stats
.biggest_method
);
5694 mono_jit_stats
.biggest_method
= NULL
;
5699 mini_cleanup (MonoDomain
*domain
)
5701 mono_runtime_shutdown_stat_profiler ();
5704 cominterop_release_all_rcws ();
5707 #ifndef MONO_CROSS_COMPILE
5709 * mono_runtime_cleanup() and mono_domain_finalize () need to
5710 * be called early since they need the execution engine still
5711 * fully working (mono_domain_finalize may invoke managed finalizers
5712 * and mono_runtime_cleanup will wait for other threads to finish).
5714 mono_domain_finalize (domain
, 2000);
5717 /* This accesses metadata so needs to be called before runtime shutdown */
5720 mono_profiler_shutdown ();
5722 #ifndef MONO_CROSS_COMPILE
5723 mono_runtime_cleanup (domain
);
5726 mono_icall_cleanup ();
5728 mono_runtime_cleanup_handlers ();
5730 mono_domain_free (domain
, TRUE
);
5732 mono_debugger_cleanup ();
5735 mono_llvm_cleanup ();
5738 mono_trampolines_cleanup ();
5740 mono_unwind_cleanup ();
5742 if (!mono_dont_free_global_codeman
)
5743 mono_code_manager_destroy (global_codeman
);
5744 g_hash_table_destroy (jit_icall_name_hash
);
5745 g_free (emul_opcode_map
);
5747 mono_arch_cleanup ();
5751 mono_trace_cleanup ();
5753 mono_counters_dump (-1, stdout
);
5755 if (mono_inject_async_exc_method
)
5756 mono_method_desc_free (mono_inject_async_exc_method
);
5758 TlsFree(mono_jit_tls_id
);
5760 DeleteCriticalSection (&jit_mutex
);
5762 DeleteCriticalSection (&mono_delegate_section
);
5766 mono_set_defaults (int verbose_level
, guint32 opts
)
5768 mini_verbose
= verbose_level
;
5770 default_opt_set
= TRUE
;
5774 mono_disable_optimizations (guint32 opts
)
5776 default_opt
&= ~opts
;
5780 * mono_get_runtime_build_info:
5782 * Return the runtime version + build date in string format.
5783 * The returned string is owned by the caller.
5786 mono_get_runtime_build_info (void)
5788 if (mono_build_date
)
5789 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
5791 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
5795 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
5797 GHashTable
*assemblies
= (GHashTable
*)user_data
;
5798 MonoImage
*image
= mono_assembly_get_image (ass
);
5799 MonoMethod
*method
, *invoke
;
5802 if (g_hash_table_lookup (assemblies
, ass
))
5805 g_hash_table_insert (assemblies
, ass
, ass
);
5807 if (mini_verbose
> 0)
5808 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
5810 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
5811 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
5812 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
5816 if (mini_verbose
> 1) {
5817 char * desc
= mono_method_full_name (method
, TRUE
);
5818 g_print ("Compiling %d %s\n", count
, desc
);
5821 mono_compile_method (method
);
5822 if (strcmp (method
->name
, "Finalize") == 0) {
5823 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
5824 mono_compile_method (invoke
);
5826 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
5827 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
5828 mono_compile_method (invoke
);
5832 /* Load and precompile referenced assemblies as well */
5833 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
5834 mono_assembly_load_reference (image
, i
);
5835 if (image
->references
[i
])
5836 mono_precompile_assembly (image
->references
[i
], assemblies
);
5840 void mono_precompile_assemblies ()
5842 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
5844 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
5846 g_hash_table_destroy (assemblies
);
5850 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
5851 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);