2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * Copyright 2002-2003 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc.
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 #define MONO_LLVM_IN_MINI 1
23 #ifdef HAVE_SYS_TIME_H
27 #include <mono/utils/memcheck.h>
29 #include <mono/metadata/assembly.h>
30 #include <mono/metadata/loader.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/object.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/debug-helpers.h>
39 #include <mono/io-layer/io-layer.h>
40 #include "mono/metadata/profiler.h"
41 #include <mono/metadata/profiler-private.h>
42 #include <mono/metadata/mono-config.h>
43 #include <mono/metadata/environment.h>
44 #include <mono/metadata/mono-debug.h>
45 #include <mono/metadata/gc-internal.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/verify.h>
48 #include <mono/metadata/verify-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/attach.h>
51 #include <mono/metadata/runtime.h>
52 #include <mono/utils/mono-math.h>
53 #include <mono/utils/mono-compiler.h>
54 #include <mono/utils/mono-counters.h>
55 #include <mono/utils/mono-logger-internal.h>
56 #include <mono/utils/mono-mmap.h>
57 #include <mono/utils/mono-tls.h>
58 #include <mono/utils/dtrace.h>
61 #include "mini-llvm.h"
68 #include "jit-icalls.h"
70 #include "debug-mini.h"
72 #include "debugger-agent.h"
74 static gpointer
mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
);
77 static guint32 default_opt
= 0;
78 static gboolean default_opt_set
= FALSE
;
80 MonoNativeTlsKey mono_jit_tls_id
;
82 #ifdef MONO_HAVE_FAST_TLS
83 MONO_FAST_TLS_DECLARE(mono_jit_tls
);
86 #ifndef MONO_ARCH_MONITOR_ENTER_ADJUSTMENT
87 #define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 1
90 MonoTraceSpec
*mono_jit_trace_calls
= NULL
;
91 gboolean mono_compile_aot
= FALSE
;
92 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
93 gboolean mono_aot_only
= FALSE
;
94 /* Whenever to use IMT */
95 #ifdef MONO_ARCH_HAVE_IMT
96 gboolean mono_use_imt
= TRUE
;
98 gboolean mono_use_imt
= FALSE
;
100 MonoMethodDesc
*mono_inject_async_exc_method
= NULL
;
101 int mono_inject_async_exc_pos
;
102 MonoMethodDesc
*mono_break_at_bb_method
= NULL
;
103 int mono_break_at_bb_bb_num
;
104 gboolean mono_do_x86_stack_align
= TRUE
;
105 const char *mono_build_date
;
106 gboolean mono_do_signal_chaining
;
107 static gboolean mono_using_xdebug
;
108 static int mini_verbose
= 0;
111 * This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
112 * it can load AOT code compiled by LLVM.
114 gboolean mono_use_llvm
= FALSE
;
116 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
117 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
118 static CRITICAL_SECTION jit_mutex
;
120 static MonoCodeManager
*global_codeman
= NULL
;
122 static GHashTable
*jit_icall_name_hash
= NULL
;
124 static MonoDebugOptions debug_options
;
126 #ifdef VALGRIND_JIT_REGISTER_MAP
127 static int valgrind_register
= 0;
131 * Table written to by the debugger with a 1-based index into the
132 * mono_breakpoint_info table, which contains changes made to
133 * the JIT instructions by the debugger.
136 mono_breakpoint_info_index
[MONO_BREAKPOINT_ARRAY_SIZE
];
138 /* Whenever to check for pending exceptions in managed-to-native wrappers */
139 gboolean check_for_pending_exc
= TRUE
;
141 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
142 gboolean disable_vtypes_in_regs
= FALSE
;
144 gboolean mono_dont_free_global_codeman
;
147 mono_realloc_native_code (MonoCompile
*cfg
)
149 #if defined(__default_codegen__)
150 return g_realloc (cfg
->native_code
, cfg
->code_size
);
151 #elif defined(__native_client_codegen__)
153 gpointer native_code
;
154 guint alignment_check
;
156 /* Save the old alignment offset so we can re-align after the realloc. */
157 old_padding
= (guint
)(cfg
->native_code
- cfg
->native_code_alloc
);
159 cfg
->native_code_alloc
= g_realloc ( cfg
->native_code_alloc
,
160 cfg
->code_size
+ kNaClAlignment
);
162 /* Align native_code to next nearest kNaClAlignment byte. */
163 native_code
= (guint
)cfg
->native_code_alloc
+ kNaClAlignment
;
164 native_code
= (guint
)native_code
& ~kNaClAlignmentMask
;
166 /* Shift the data to be 32-byte aligned again. */
167 memmove (native_code
, cfg
->native_code_alloc
+ old_padding
, cfg
->code_size
);
169 alignment_check
= (guint
)native_code
& kNaClAlignmentMask
;
170 g_assert (alignment_check
== 0);
173 g_assert_not_reached ();
174 return cfg
->native_code
;
178 #ifdef __native_client_codegen__
180 /* Prevent instructions from straddling a 32-byte alignment boundary. */
181 /* Instructions longer than 32 bytes must be aligned internally. */
182 /* IN: pcode, instlen */
184 void mono_nacl_align_inst(guint8
**pcode
, int instlen
) {
187 space_in_block
= kNaClAlignment
- ((uintptr_t)(*pcode
) & kNaClAlignmentMask
);
189 if (G_UNLIKELY (instlen
>= kNaClAlignment
)) {
190 g_assert_not_reached();
191 } else if (instlen
> space_in_block
) {
192 *pcode
= mono_arch_nacl_pad(*pcode
, space_in_block
);
196 /* Move emitted call sequence to the end of a kNaClAlignment-byte block. */
197 /* IN: start pointer to start of call sequence */
198 /* IN: pcode pointer to end of call sequence (current "IP") */
199 /* OUT: start pointer to the start of the call sequence after padding */
200 /* OUT: pcode pointer to the end of the call sequence after padding */
201 void mono_nacl_align_call(guint8
**start
, guint8
**pcode
) {
202 const size_t MAX_NACL_CALL_LENGTH
= kNaClAlignment
;
203 guint8 copy_of_call
[MAX_NACL_CALL_LENGTH
];
206 const size_t length
= (size_t)((*pcode
)-(*start
));
207 g_assert(length
< MAX_NACL_CALL_LENGTH
);
209 memcpy(copy_of_call
, *start
, length
);
210 temp
= mono_nacl_pad_call(*start
, (guint8
)length
);
211 memcpy(temp
, copy_of_call
, length
);
213 (*pcode
) = temp
+ length
;
216 /* mono_nacl_pad_call(): Insert padding for Native Client call instructions */
217 /* code pointer to buffer for emitting code */
218 /* ilength length of call instruction */
219 guint8
*mono_nacl_pad_call(guint8
*code
, guint8 ilength
) {
220 int freeSpaceInBlock
= kNaClAlignment
- ((uintptr_t)code
& kNaClAlignmentMask
);
221 int padding
= freeSpaceInBlock
- ilength
;
224 /* There isn't enough space in this block for the instruction. */
225 /* Fill this block and start a new one. */
226 code
= mono_arch_nacl_pad(code
, freeSpaceInBlock
);
227 freeSpaceInBlock
= kNaClAlignment
;
228 padding
= freeSpaceInBlock
- ilength
;
230 g_assert(ilength
> 0);
231 g_assert(padding
>= 0);
232 g_assert(padding
< kNaClAlignment
);
233 if (0 == padding
) return code
;
234 return mono_arch_nacl_pad(code
, padding
);
237 guint8
*mono_nacl_align(guint8
*code
) {
238 int padding
= kNaClAlignment
- ((uintptr_t)code
& kNaClAlignmentMask
);
239 if (padding
!= kNaClAlignment
) code
= mono_arch_nacl_pad(code
, padding
);
243 void mono_nacl_fix_patches(const guint8
*code
, MonoJumpInfo
*ji
)
245 MonoJumpInfo
*patch_info
;
246 for (patch_info
= ji
; patch_info
; patch_info
= patch_info
->next
) {
247 unsigned char *ip
= patch_info
->ip
.i
+ code
;
248 ip
= mono_arch_nacl_skip_nops(ip
);
249 patch_info
->ip
.i
= ip
- code
;
252 #endif /* __native_client_codegen__ */
255 mono_running_on_valgrind (void)
257 if (RUNNING_ON_VALGRIND
){
258 #ifdef VALGRIND_JIT_REGISTER_MAP
259 valgrind_register
= TRUE
;
267 MonoExceptionClause
*clause
;
268 MonoBasicBlock
*basic_block
;
278 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
280 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
283 ud
->method
= (MonoMethod
*)key
;
287 G_GNUC_UNUSED
static char*
288 get_method_from_ip (void *ip
)
293 MonoDomain
*domain
= mono_domain_get ();
294 MonoDebugSourceLocation
*location
;
295 FindTrampUserData user_data
;
298 domain
= mono_get_root_domain ();
300 ji
= mono_jit_info_table_find (domain
, ip
);
303 user_data
.method
= NULL
;
304 mono_domain_lock (domain
);
305 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
306 mono_domain_unlock (domain
);
307 if (user_data
.method
) {
308 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
309 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
316 method
= mono_method_full_name (ji
->method
, TRUE
);
317 /* FIXME: unused ? */
318 location
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
320 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
);
322 mono_debug_free_source_location (location
);
330 * @ip: an instruction pointer address
332 * This method is used from a debugger to get the name of the
333 * method at address @ip. This routine is typically invoked from
334 * a debugger like this:
336 * (gdb) print mono_pmip ($pc)
338 * Returns: the name of the method at address @ip.
343 return get_method_from_ip (ip
);
347 * mono_print_method_from_ip
348 * @ip: an instruction pointer address
350 * This method is used from a debugger to get the name of the
351 * method at address @ip.
353 * This prints the name of the method at address @ip in the standard
354 * output. Unlike mono_pmip which returns a string, this routine
355 * prints the value on the standard output.
358 /* Prevent the linker from optimizing this away in embedding setups to help debugging */
359 __attribute__((used
))
362 mono_print_method_from_ip (void *ip
)
366 MonoDebugSourceLocation
*source
;
367 MonoDomain
*domain
= mono_domain_get ();
368 MonoDomain
*target_domain
= mono_domain_get ();
369 FindTrampUserData user_data
;
370 MonoGenericSharingContext
*gsctx
;
371 const char *shared_type
;
373 ji
= mini_jit_info_table_find (domain
, ip
, &target_domain
);
376 user_data
.method
= NULL
;
377 mono_domain_lock (domain
);
378 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
379 mono_domain_unlock (domain
);
380 if (user_data
.method
) {
381 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
382 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
386 g_print ("No method at %p\n", ip
);
390 method
= mono_method_full_name (ji
->method
, TRUE
);
391 source
= mono_debug_lookup_source_location (ji
->method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
393 gsctx
= mono_jit_info_get_generic_sharing_context (ji
);
396 if (gsctx
->var_is_vt
|| gsctx
->mvar_is_vt
)
397 shared_type
= "gsharedvt ";
399 shared_type
= "gshared ";
402 g_print ("IP %p at offset 0x%x of %smethod %s (%p %p)[domain %p - %s]\n", ip
, (int)((char*)ip
- (char*)ji
->code_start
), shared_type
, method
, ji
->code_start
, (char*)ji
->code_start
+ ji
->code_size
, target_domain
, target_domain
->friendly_name
);
405 g_print ("%s:%d\n", source
->source_file
, source
->row
);
408 mono_debug_free_source_location (source
);
413 * mono_method_same_domain:
415 * Determine whenever two compiled methods are in the same domain, thus
416 * the address of the callee can be embedded in the caller.
418 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
420 if (!caller
|| !callee
)
424 * If the call was made from domain-neutral to domain-specific
425 * code, we can't patch the call site.
427 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
430 if ((caller
->method
->klass
== mono_defaults
.appdomain_class
) &&
431 (strstr (caller
->method
->name
, "InvokeInDomain"))) {
432 /* The InvokeInDomain methods change the current appdomain */
440 * mono_global_codeman_reserve:
442 * Allocate code memory from the global code manager.
444 void *mono_global_codeman_reserve (int size
)
449 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
451 if (!global_codeman
) {
452 /* This can happen during startup */
453 global_codeman
= mono_code_manager_new ();
454 return mono_code_manager_reserve (global_codeman
, size
);
458 ptr
= mono_code_manager_reserve (global_codeman
, size
);
464 #if defined(__native_client_codegen__) && defined(__native_client__)
465 /* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
466 * which we are generating code, return a pointer to the destination in the
467 * dynamic code segment into which the code will be copied when
468 * mono_global_codeman_commit is called.
469 * LOCKING: Acquires the jit lock.
472 nacl_global_codeman_get_dest (void *data
)
476 dest
= nacl_code_manager_get_code_dest (global_codeman
, data
);
482 mono_global_codeman_commit (void *data
, int size
, int newsize
)
485 mono_code_manager_commit (global_codeman
, data
, size
, newsize
);
490 * Convenience function which calls mono_global_codeman_commit to validate and
491 * copy the code. The caller sets *buf_base and *buf_size to the start and size
492 * of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
493 * the byte after the last instruction byte. On return, *buf_base will point to
494 * the start of the copied in the code segment, and *code_end will point after
495 * the end of the copied code.
498 nacl_global_codeman_validate (guint8
**buf_base
, int buf_size
, guint8
**code_end
)
500 guint8
*tmp
= nacl_global_codeman_get_dest (*buf_base
);
501 mono_global_codeman_commit (*buf_base
, buf_size
, *code_end
- *buf_base
);
502 *code_end
= tmp
+ (*code_end
- *buf_base
);
506 /* no-op versions of Native Client functions */
508 nacl_global_codeman_get_dest (void *data
)
514 mono_global_codeman_commit (void *data
, int size
, int newsize
)
519 nacl_global_codeman_validate (guint8
**buf_base
, int buf_size
, guint8
**code_end
)
523 #endif /* __native_client__ */
526 * mono_create_unwind_op:
528 * Create an unwind op with the given parameters.
531 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
533 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
544 * mono_emit_unwind_op:
546 * Add an unwind op with the given parameters for the list of unwind ops stored in
550 mono_emit_unwind_op (MonoCompile
*cfg
, int when
, int tag
, int reg
, int val
)
552 MonoUnwindOp
*op
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoUnwindOp
));
559 cfg
->unwind_ops
= g_slist_append_mempool (cfg
->mempool
, cfg
->unwind_ops
, op
);
563 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
565 MonoJumpInfoToken
*res
= mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
568 res
->has_context
= context
!= NULL
;
570 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
576 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
578 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
582 * mono_tramp_info_create:
584 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
585 * of NAME, JI, and UNWIND_OPS.
588 mono_tramp_info_create (const char *name
, guint8
*code
, guint32 code_size
, MonoJumpInfo
*ji
, GSList
*unwind_ops
)
590 MonoTrampInfo
*info
= g_new0 (MonoTrampInfo
, 1);
592 info
->name
= (char*)name
;
594 info
->code_size
= code_size
;
596 info
->unwind_ops
= unwind_ops
;
602 mono_tramp_info_free (MonoTrampInfo
*info
)
609 for (l
= info
->unwind_ops
; l
; l
= l
->next
)
611 g_slist_free (info
->unwind_ops
);
615 G_GNUC_UNUSED
static void
621 * Runtime debugging tool, use if (debug_count ()) <x> else <y> to do <x> the first COUNT times, then do <y> afterwards.
622 * Set a breakpoint in break_count () to break the last time <x> is done.
624 G_GNUC_UNUSED gboolean
625 mono_debug_count (void)
627 static int count
= 0;
630 if (!getenv ("COUNT"))
633 if (count
== atoi (getenv ("COUNT"))) {
637 if (count
> atoi (getenv ("COUNT"))) {
644 #define MONO_INIT_VARINFO(vi,id) do { \
645 (vi)->range.first_use.pos.bid = 0xffff; \
651 * mono_unlink_bblock:
653 * Unlink two basic blocks.
656 mono_unlink_bblock (MonoCompile
*cfg
, MonoBasicBlock
*from
, MonoBasicBlock
* to
)
662 for (i
= 0; i
< from
->out_count
; ++i
) {
663 if (to
== from
->out_bb
[i
]) {
670 for (i
= 0; i
< from
->out_count
; ++i
) {
671 if (from
->out_bb
[i
] != to
)
672 from
->out_bb
[pos
++] = from
->out_bb
[i
];
674 g_assert (pos
== from
->out_count
- 1);
679 for (i
= 0; i
< to
->in_count
; ++i
) {
680 if (from
== to
->in_bb
[i
]) {
687 for (i
= 0; i
< to
->in_count
; ++i
) {
688 if (to
->in_bb
[i
] != from
)
689 to
->in_bb
[pos
++] = to
->in_bb
[i
];
691 g_assert (pos
== to
->in_count
- 1);
697 * mono_bblocks_linked:
699 * Return whenever BB1 and BB2 are linked in the CFG.
702 mono_bblocks_linked (MonoBasicBlock
*bb1
, MonoBasicBlock
*bb2
)
706 for (i
= 0; i
< bb1
->out_count
; ++i
) {
707 if (bb1
->out_bb
[i
] == bb2
)
715 mono_find_block_region_notry (MonoCompile
*cfg
, int offset
)
717 MonoMethodHeader
*header
= cfg
->header
;
718 MonoExceptionClause
*clause
;
721 for (i
= 0; i
< header
->num_clauses
; ++i
) {
722 clause
= &header
->clauses
[i
];
723 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) && (offset
>= clause
->data
.filter_offset
) &&
724 (offset
< (clause
->handler_offset
)))
725 return ((i
+ 1) << 8) | MONO_REGION_FILTER
| clause
->flags
;
727 if (MONO_OFFSET_IN_HANDLER (clause
, offset
)) {
728 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
729 return ((i
+ 1) << 8) | MONO_REGION_FINALLY
| clause
->flags
;
730 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
)
731 return ((i
+ 1) << 8) | MONO_REGION_FAULT
| clause
->flags
;
733 return ((i
+ 1) << 8) | MONO_REGION_CATCH
| clause
->flags
;
741 * mono_get_block_region_notry:
743 * Return the region corresponding to REGION, ignoring try clauses nested inside
747 mono_get_block_region_notry (MonoCompile
*cfg
, int region
)
749 if ((region
& (0xf << 4)) == MONO_REGION_TRY
) {
750 MonoMethodHeader
*header
= cfg
->header
;
753 * This can happen if a try clause is nested inside a finally clause.
755 int clause_index
= (region
>> 8) - 1;
756 g_assert (clause_index
>= 0 && clause_index
< header
->num_clauses
);
758 region
= mono_find_block_region_notry (cfg
, header
->clauses
[clause_index
].try_offset
);
765 mono_find_spvar_for_region (MonoCompile
*cfg
, int region
)
767 region
= mono_get_block_region_notry (cfg
, region
);
769 return g_hash_table_lookup (cfg
->spvars
, GINT_TO_POINTER (region
));
773 df_visit (MonoBasicBlock
*start
, int *dfn
, MonoBasicBlock
**array
)
777 array
[*dfn
] = start
;
778 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
779 for (i
= 0; i
< start
->out_count
; ++i
) {
780 if (start
->out_bb
[i
]->dfn
)
783 start
->out_bb
[i
]->dfn
= *dfn
;
784 start
->out_bb
[i
]->df_parent
= start
;
785 array
[*dfn
] = start
->out_bb
[i
];
786 df_visit (start
->out_bb
[i
], dfn
, array
);
791 mono_reverse_branch_op (guint32 opcode
)
793 static const int reverse_map
[] = {
794 CEE_BNE_UN
, CEE_BLT
, CEE_BLE
, CEE_BGT
, CEE_BGE
,
795 CEE_BEQ
, CEE_BLT_UN
, CEE_BLE_UN
, CEE_BGT_UN
, CEE_BGE_UN
797 static const int reverse_fmap
[] = {
798 OP_FBNE_UN
, OP_FBLT
, OP_FBLE
, OP_FBGT
, OP_FBGE
,
799 OP_FBEQ
, OP_FBLT_UN
, OP_FBLE_UN
, OP_FBGT_UN
, OP_FBGE_UN
801 static const int reverse_lmap
[] = {
802 OP_LBNE_UN
, OP_LBLT
, OP_LBLE
, OP_LBGT
, OP_LBGE
,
803 OP_LBEQ
, OP_LBLT_UN
, OP_LBLE_UN
, OP_LBGT_UN
, OP_LBGE_UN
805 static const int reverse_imap
[] = {
806 OP_IBNE_UN
, OP_IBLT
, OP_IBLE
, OP_IBGT
, OP_IBGE
,
807 OP_IBEQ
, OP_IBLT_UN
, OP_IBLE_UN
, OP_IBGT_UN
, OP_IBGE_UN
810 if (opcode
>= CEE_BEQ
&& opcode
<= CEE_BLT_UN
) {
811 opcode
= reverse_map
[opcode
- CEE_BEQ
];
812 } else if (opcode
>= OP_FBEQ
&& opcode
<= OP_FBLT_UN
) {
813 opcode
= reverse_fmap
[opcode
- OP_FBEQ
];
814 } else if (opcode
>= OP_LBEQ
&& opcode
<= OP_LBLT_UN
) {
815 opcode
= reverse_lmap
[opcode
- OP_LBEQ
];
816 } else if (opcode
>= OP_IBEQ
&& opcode
<= OP_IBLT_UN
) {
817 opcode
= reverse_imap
[opcode
- OP_IBEQ
];
819 g_assert_not_reached ();
825 mono_type_to_store_membase (MonoCompile
*cfg
, MonoType
*type
)
828 return OP_STORE_MEMBASE_REG
;
831 switch (type
->type
) {
834 case MONO_TYPE_BOOLEAN
:
835 return OP_STOREI1_MEMBASE_REG
;
839 return OP_STOREI2_MEMBASE_REG
;
842 return OP_STOREI4_MEMBASE_REG
;
846 case MONO_TYPE_FNPTR
:
847 return OP_STORE_MEMBASE_REG
;
848 case MONO_TYPE_CLASS
:
849 case MONO_TYPE_STRING
:
850 case MONO_TYPE_OBJECT
:
851 case MONO_TYPE_SZARRAY
:
852 case MONO_TYPE_ARRAY
:
853 return OP_STORE_MEMBASE_REG
;
856 return OP_STOREI8_MEMBASE_REG
;
858 return OP_STORER4_MEMBASE_REG
;
860 return OP_STORER8_MEMBASE_REG
;
861 case MONO_TYPE_VALUETYPE
:
862 if (type
->data
.klass
->enumtype
) {
863 type
= mono_class_enum_basetype (type
->data
.klass
);
866 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
867 return OP_STOREX_MEMBASE
;
868 return OP_STOREV_MEMBASE
;
869 case MONO_TYPE_TYPEDBYREF
:
870 return OP_STOREV_MEMBASE
;
871 case MONO_TYPE_GENERICINST
:
872 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
876 if (mini_type_var_is_vt (cfg
, type
))
877 return OP_STOREV_MEMBASE
;
879 return OP_STORE_MEMBASE_REG
;
881 g_error ("unknown type 0x%02x in type_to_store_membase", type
->type
);
887 mono_type_to_load_membase (MonoCompile
*cfg
, MonoType
*type
)
890 return OP_LOAD_MEMBASE
;
892 type
= mono_type_get_underlying_type (type
);
894 switch (type
->type
) {
896 return OP_LOADI1_MEMBASE
;
898 case MONO_TYPE_BOOLEAN
:
899 return OP_LOADU1_MEMBASE
;
901 return OP_LOADI2_MEMBASE
;
904 return OP_LOADU2_MEMBASE
;
906 return OP_LOADI4_MEMBASE
;
908 return OP_LOADU4_MEMBASE
;
912 case MONO_TYPE_FNPTR
:
913 return OP_LOAD_MEMBASE
;
914 case MONO_TYPE_CLASS
:
915 case MONO_TYPE_STRING
:
916 case MONO_TYPE_OBJECT
:
917 case MONO_TYPE_SZARRAY
:
918 case MONO_TYPE_ARRAY
:
919 return OP_LOAD_MEMBASE
;
922 return OP_LOADI8_MEMBASE
;
924 return OP_LOADR4_MEMBASE
;
926 return OP_LOADR8_MEMBASE
;
927 case MONO_TYPE_VALUETYPE
:
928 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (type
)))
929 return OP_LOADX_MEMBASE
;
930 case MONO_TYPE_TYPEDBYREF
:
931 return OP_LOADV_MEMBASE
;
932 case MONO_TYPE_GENERICINST
:
933 if (mono_type_generic_inst_is_valuetype (type
))
934 return OP_LOADV_MEMBASE
;
936 return OP_LOAD_MEMBASE
;
940 g_assert (cfg
->generic_sharing_context
);
941 if (mini_type_var_is_vt (cfg
, type
))
942 return OP_LOADV_MEMBASE
;
944 return OP_LOAD_MEMBASE
;
946 g_error ("unknown type 0x%02x in type_to_load_membase", type
->type
);
952 mini_type_to_ldind (MonoCompile
* cfg
, MonoType
*type
)
954 if (cfg
->generic_sharing_context
&& !type
->byref
) {
955 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
) {
956 if (mini_type_var_is_vt (cfg
, type
))
959 return CEE_LDIND_REF
;
962 return mono_type_to_ldind (type
);
966 mini_type_to_stind (MonoCompile
* cfg
, MonoType
*type
)
968 if (cfg
->generic_sharing_context
&& !type
->byref
) {
969 if (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
) {
970 if (mini_type_var_is_vt (cfg
, type
))
973 return CEE_STIND_REF
;
976 return mono_type_to_stind (type
);
980 mono_op_imm_to_op (int opcode
)
984 #if SIZEOF_REGISTER == 4
1000 #if SIZEOF_REGISTER == 4
1006 #if SIZEOF_REGISTER == 4
1012 #if SIZEOF_REGISTER == 4
1037 case OP_ISHR_UN_IMM
:
1039 case OP_LSHR_UN_IMM
:
1043 case OP_IDIV_UN_IMM
:
1045 case OP_IREM_UN_IMM
:
1050 #if SIZEOF_REGISTER == 4
1056 #if SIZEOF_REGISTER == 4
1073 case OP_COMPARE_IMM
:
1075 case OP_ICOMPARE_IMM
:
1077 case OP_LOCALLOC_IMM
:
1080 printf ("%s\n", mono_inst_name (opcode
));
1081 g_assert_not_reached ();
1087 * mono_decompose_op_imm:
1089 * Replace the OP_.._IMM INS with its non IMM variant.
1092 mono_decompose_op_imm (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
)
1096 MONO_INST_NEW (cfg
, temp
, OP_ICONST
);
1097 temp
->inst_c0
= ins
->inst_imm
;
1098 temp
->dreg
= mono_alloc_ireg (cfg
);
1099 mono_bblock_insert_before_ins (bb
, ins
, temp
);
1100 ins
->opcode
= mono_op_imm_to_op (ins
->opcode
);
1101 if (ins
->opcode
== OP_LOCALLOC
)
1102 ins
->sreg1
= temp
->dreg
;
1104 ins
->sreg2
= temp
->dreg
;
1106 bb
->max_vreg
= MAX (bb
->max_vreg
, cfg
->next_vreg
);
1110 set_vreg_to_inst (MonoCompile
*cfg
, int vreg
, MonoInst
*inst
)
1112 if (vreg
>= cfg
->vreg_to_inst_len
) {
1113 MonoInst
**tmp
= cfg
->vreg_to_inst
;
1114 int size
= cfg
->vreg_to_inst_len
;
1116 while (vreg
>= cfg
->vreg_to_inst_len
)
1117 cfg
->vreg_to_inst_len
= cfg
->vreg_to_inst_len
? cfg
->vreg_to_inst_len
* 2 : 32;
1118 cfg
->vreg_to_inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
*) * cfg
->vreg_to_inst_len
);
1120 memcpy (cfg
->vreg_to_inst
, tmp
, size
* sizeof (MonoInst
*));
1122 cfg
->vreg_to_inst
[vreg
] = inst
;
1125 #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)))
1126 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
1131 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1139 mono_compile_create_var_for_vreg (MonoCompile
*cfg
, MonoType
*type
, int opcode
, int vreg
)
1142 int num
= cfg
->num_varinfo
;
1145 if ((num
+ 1) >= cfg
->varinfo_count
) {
1146 int orig_count
= cfg
->varinfo_count
;
1147 cfg
->varinfo_count
= cfg
->varinfo_count
? (cfg
->varinfo_count
* 2) : 64;
1148 cfg
->varinfo
= (MonoInst
**)g_realloc (cfg
->varinfo
, sizeof (MonoInst
*) * cfg
->varinfo_count
);
1149 cfg
->vars
= (MonoMethodVar
*)g_realloc (cfg
->vars
, sizeof (MonoMethodVar
) * cfg
->varinfo_count
);
1150 memset (&cfg
->vars
[orig_count
], 0, (cfg
->varinfo_count
- orig_count
) * sizeof (MonoMethodVar
));
1153 cfg
->stat_allocate_var
++;
1155 MONO_INST_NEW (cfg
, inst
, opcode
);
1156 inst
->inst_c0
= num
;
1157 inst
->inst_vtype
= type
;
1158 inst
->klass
= mono_class_from_mono_type (type
);
1159 type_to_eval_stack_type (cfg
, type
, inst
);
1160 /* if set to 1 the variable is native */
1161 inst
->backend
.is_pinvoke
= 0;
1164 if (inst
->klass
->exception_type
)
1165 mono_cfg_set_exception (cfg
, MONO_EXCEPTION_TYPE_LOAD
);
1167 if (cfg
->compute_gc_maps
) {
1169 mono_mark_vreg_as_mp (cfg
, vreg
);
1171 MonoType
*t
= mini_type_get_underlying_type (NULL
, type
);
1172 if ((MONO_TYPE_ISSTRUCT (t
) && inst
->klass
->has_references
) || mini_type_is_reference (cfg
, t
)) {
1173 inst
->flags
|= MONO_INST_GC_TRACK
;
1174 mono_mark_vreg_as_ref (cfg
, vreg
);
1179 cfg
->varinfo
[num
] = inst
;
1181 MONO_INIT_VARINFO (&cfg
->vars
[num
], num
);
1182 MONO_VARINFO (cfg
, num
)->vreg
= vreg
;
1185 set_vreg_to_inst (cfg
, vreg
, inst
);
1187 #if SIZEOF_REGISTER == 4
1188 #ifdef MONO_ARCH_SOFT_FLOAT
1189 regpair
= mono_type_is_long (type
) || mono_type_is_float (type
);
1191 regpair
= mono_type_is_long (type
);
1201 * These two cannot be allocated using create_var_for_vreg since that would
1202 * put it into the cfg->varinfo array, confusing many parts of the JIT.
1206 * Set flags to VOLATILE so SSA skips it.
1209 if (cfg
->verbose_level
>= 4) {
1210 printf (" Create LVAR R%d (R%d, R%d)\n", inst
->dreg
, inst
->dreg
+ 1, inst
->dreg
+ 2);
1213 #ifdef MONO_ARCH_SOFT_FLOAT
1214 if (cfg
->opt
& MONO_OPT_SSA
) {
1215 if (mono_type_is_float (type
))
1216 inst
->flags
= MONO_INST_VOLATILE
;
1220 /* Allocate a dummy MonoInst for the first vreg */
1221 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
1222 tree
->dreg
= inst
->dreg
+ 1;
1223 if (cfg
->opt
& MONO_OPT_SSA
)
1224 tree
->flags
= MONO_INST_VOLATILE
;
1225 tree
->inst_c0
= num
;
1226 tree
->type
= STACK_I4
;
1227 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1228 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1230 set_vreg_to_inst (cfg
, inst
->dreg
+ 1, tree
);
1232 /* Allocate a dummy MonoInst for the second vreg */
1233 MONO_INST_NEW (cfg
, tree
, OP_LOCAL
);
1234 tree
->dreg
= inst
->dreg
+ 2;
1235 if (cfg
->opt
& MONO_OPT_SSA
)
1236 tree
->flags
= MONO_INST_VOLATILE
;
1237 tree
->inst_c0
= num
;
1238 tree
->type
= STACK_I4
;
1239 tree
->inst_vtype
= &mono_defaults
.int32_class
->byval_arg
;
1240 tree
->klass
= mono_class_from_mono_type (tree
->inst_vtype
);
1242 set_vreg_to_inst (cfg
, inst
->dreg
+ 2, tree
);
1246 if (cfg
->verbose_level
> 2)
1247 g_print ("created temp %d (R%d) of type %s\n", num
, vreg
, mono_type_get_name (type
));
1252 mono_compile_create_var (MonoCompile
*cfg
, MonoType
*type
, int opcode
)
1256 if (mono_type_is_long (type
))
1257 dreg
= mono_alloc_dreg (cfg
, STACK_I8
);
1258 #ifdef MONO_ARCH_SOFT_FLOAT
1259 else if (mono_type_is_float (type
))
1260 dreg
= mono_alloc_dreg (cfg
, STACK_R8
);
1263 /* All the others are unified */
1264 dreg
= mono_alloc_preg (cfg
);
1266 return mono_compile_create_var_for_vreg (cfg
, type
, opcode
, dreg
);
1270 * Transform a MonoInst into a load from the variable of index var_index.
1273 mono_compile_make_var_load (MonoCompile
*cfg
, MonoInst
*dest
, gssize var_index
) {
1274 memset (dest
, 0, sizeof (MonoInst
));
1275 dest
->inst_i0
= cfg
->varinfo
[var_index
];
1276 dest
->opcode
= mini_type_to_ldind (cfg
, dest
->inst_i0
->inst_vtype
);
1277 type_to_eval_stack_type (cfg
, dest
->inst_i0
->inst_vtype
, dest
);
1278 dest
->klass
= dest
->inst_i0
->klass
;
1284 mono_mark_vreg_as_ref (MonoCompile
*cfg
, int vreg
)
1286 if (vreg
>= cfg
->vreg_is_ref_len
) {
1287 gboolean
*tmp
= cfg
->vreg_is_ref
;
1288 int size
= cfg
->vreg_is_ref_len
;
1290 while (vreg
>= cfg
->vreg_is_ref_len
)
1291 cfg
->vreg_is_ref_len
= cfg
->vreg_is_ref_len
? cfg
->vreg_is_ref_len
* 2 : 32;
1292 cfg
->vreg_is_ref
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (gboolean
) * cfg
->vreg_is_ref_len
);
1294 memcpy (cfg
->vreg_is_ref
, tmp
, size
* sizeof (gboolean
));
1296 cfg
->vreg_is_ref
[vreg
] = TRUE
;
1300 mono_mark_vreg_as_mp (MonoCompile
*cfg
, int vreg
)
1302 if (vreg
>= cfg
->vreg_is_mp_len
) {
1303 gboolean
*tmp
= cfg
->vreg_is_mp
;
1304 int size
= cfg
->vreg_is_mp_len
;
1306 while (vreg
>= cfg
->vreg_is_mp_len
)
1307 cfg
->vreg_is_mp_len
= cfg
->vreg_is_mp_len
? cfg
->vreg_is_mp_len
* 2 : 32;
1308 cfg
->vreg_is_mp
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (gboolean
) * cfg
->vreg_is_mp_len
);
1310 memcpy (cfg
->vreg_is_mp
, tmp
, size
* sizeof (gboolean
));
1312 cfg
->vreg_is_mp
[vreg
] = TRUE
;
1316 type_from_stack_type (MonoInst
*ins
) {
1317 switch (ins
->type
) {
1318 case STACK_I4
: return &mono_defaults
.int32_class
->byval_arg
;
1319 case STACK_I8
: return &mono_defaults
.int64_class
->byval_arg
;
1320 case STACK_PTR
: return &mono_defaults
.int_class
->byval_arg
;
1321 case STACK_R8
: return &mono_defaults
.double_class
->byval_arg
;
1324 * this if used to be commented without any specific reason, but
1325 * it breaks #80235 when commented
1328 return &ins
->klass
->this_arg
;
1330 return &mono_defaults
.object_class
->this_arg
;
1332 /* ins->klass may not be set for ldnull.
1333 * Also, if we have a boxed valuetype, we want an object lass,
1334 * not the valuetype class
1336 if (ins
->klass
&& !ins
->klass
->valuetype
)
1337 return &ins
->klass
->byval_arg
;
1338 return &mono_defaults
.object_class
->byval_arg
;
1339 case STACK_VTYPE
: return &ins
->klass
->byval_arg
;
1341 g_error ("stack type %d to montype not handled\n", ins
->type
);
1347 mono_type_from_stack_type (MonoInst
*ins
) {
1348 return type_from_stack_type (ins
);
1352 * mono_add_ins_to_end:
1354 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
1357 mono_add_ins_to_end (MonoBasicBlock
*bb
, MonoInst
*inst
)
1362 MONO_ADD_INS (bb
, inst
);
1366 switch (bb
->last_ins
->opcode
) {
1380 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1383 if (MONO_IS_COND_BRANCH_OP (bb
->last_ins
)) {
1384 /* Need to insert the ins before the compare */
1385 if (bb
->code
== bb
->last_ins
) {
1386 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1390 if (bb
->code
->next
== bb
->last_ins
) {
1391 /* Only two instructions */
1392 opcode
= bb
->code
->opcode
;
1394 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
)) {
1396 mono_bblock_insert_before_ins (bb
, bb
->code
, inst
);
1398 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1401 opcode
= bb
->last_ins
->prev
->opcode
;
1403 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
)) {
1405 mono_bblock_insert_before_ins (bb
, bb
->last_ins
->prev
, inst
);
1407 mono_bblock_insert_before_ins (bb
, bb
->last_ins
, inst
);
1412 MONO_ADD_INS (bb
, inst
);
1418 mono_create_jump_table (MonoCompile
*cfg
, MonoInst
*label
, MonoBasicBlock
**bbs
, int num_blocks
)
1420 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
1421 MonoJumpInfoBBTable
*table
;
1423 table
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfoBBTable
));
1425 table
->table_size
= num_blocks
;
1427 ji
->ip
.label
= label
;
1428 ji
->type
= MONO_PATCH_INFO_SWITCH
;
1429 ji
->data
.table
= table
;
1430 ji
->next
= cfg
->patch_info
;
1431 cfg
->patch_info
= ji
;
1434 static MonoMethodSignature
*
1435 mono_get_array_new_va_signature (int arity
)
1437 static GHashTable
*sighash
= NULL
;
1438 MonoMethodSignature
*res
;
1443 sighash
= g_hash_table_new (NULL
, NULL
);
1445 else if ((res
= g_hash_table_lookup (sighash
, GINT_TO_POINTER (arity
)))) {
1450 res
= mono_metadata_signature_alloc (mono_defaults
.corlib
, arity
+ 1);
1453 #ifdef MONO_ARCH_VARARG_ICALLS
1454 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
1455 res
->call_convention
= MONO_CALL_VARARG
;
1459 res
->call_convention
= MONO_CALL_C
;
1462 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
1463 for (i
= 0; i
< arity
; i
++)
1464 res
->params
[i
+ 1] = &mono_defaults
.int_class
->byval_arg
;
1466 res
->ret
= &mono_defaults
.object_class
->byval_arg
;
1468 g_hash_table_insert (sighash
, GINT_TO_POINTER (arity
), res
);
1475 mono_get_array_new_va_icall (int rank
)
1477 MonoMethodSignature
*esig
;
1478 char icall_name
[256];
1480 MonoJitICallInfo
*info
;
1482 /* Need to register the icall so it gets an icall wrapper */
1483 sprintf (icall_name
, "ves_array_new_va_%d", rank
);
1486 info
= mono_find_jit_icall_by_name (icall_name
);
1488 esig
= mono_get_array_new_va_signature (rank
);
1489 name
= g_strdup (icall_name
);
1490 info
= mono_register_jit_icall (mono_array_new_va
, name
, esig
, FALSE
);
1492 g_hash_table_insert (jit_icall_name_hash
, name
, name
);
1500 mini_class_is_system_array (MonoClass
*klass
)
1502 if (klass
->parent
== mono_defaults
.array_class
)
1509 mini_assembly_can_skip_verification (MonoDomain
*domain
, MonoMethod
*method
)
1511 MonoAssembly
*assembly
= method
->klass
->image
->assembly
;
1512 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
&& method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
)
1514 if (assembly
->in_gac
|| assembly
->image
== mono_defaults
.corlib
)
1516 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE
)
1518 return mono_assembly_has_skip_verification (assembly
);
1522 * mini_method_verify:
1524 * Verify the method using the new verfier.
1526 * Returns true if the method is invalid.
1529 mini_method_verify (MonoCompile
*cfg
, MonoMethod
*method
, gboolean fail_compile
)
1532 gboolean is_fulltrust
;
1533 MonoLoaderError
*error
;
1535 if (method
->verification_success
)
1538 if (!mono_verifier_is_enabled_for_method (method
))
1541 /*skip verification implies the assembly must be */
1542 is_fulltrust
= mono_verifier_is_method_full_trust (method
) || mini_assembly_can_skip_verification (cfg
->domain
, method
);
1544 res
= mono_method_verify_with_current_settings (method
, cfg
->skip_visibility
, is_fulltrust
);
1546 if ((error
= mono_loader_get_last_error ())) {
1548 cfg
->exception_type
= error
->exception_type
;
1550 mono_loader_clear_error ();
1552 mono_free_verify_list (res
);
1557 for (tmp
= res
; tmp
; tmp
= tmp
->next
) {
1558 MonoVerifyInfoExtended
*info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1559 if (info
->info
.status
== MONO_VERIFY_ERROR
) {
1561 char *method_name
= mono_method_full_name (method
, TRUE
);
1562 cfg
->exception_type
= info
->exception_type
;
1563 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1564 g_free (method_name
);
1566 mono_free_verify_list (res
);
1569 if (info
->info
.status
== MONO_VERIFY_NOT_VERIFIABLE
&& (!is_fulltrust
|| info
->exception_type
== MONO_EXCEPTION_METHOD_ACCESS
|| info
->exception_type
== MONO_EXCEPTION_FIELD_ACCESS
)) {
1571 char *method_name
= mono_method_full_name (method
, TRUE
);
1572 cfg
->exception_type
= info
->exception_type
;
1573 cfg
->exception_message
= g_strdup_printf ("Error verifying %s: %s", method_name
, info
->info
.message
);
1574 g_free (method_name
);
1576 mono_free_verify_list (res
);
1580 mono_free_verify_list (res
);
1582 method
->verification_success
= 1;
1586 /*Returns true if something went wrong*/
1588 mono_compile_is_broken (MonoCompile
*cfg
, MonoMethod
*method
, gboolean fail_compile
)
1590 MonoMethod
*method_definition
= method
;
1591 gboolean dont_verify
= method
->klass
->image
->assembly
->corlib_internal
;
1593 while (method_definition
->is_inflated
) {
1594 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method_definition
;
1595 method_definition
= imethod
->declaring
;
1598 return !dont_verify
&& mini_method_verify (cfg
, method_definition
, fail_compile
);
1601 static gconstpointer
1602 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
1605 MonoMethod
*wrapper
;
1606 gconstpointer trampoline
;
1607 MonoDomain
*domain
= mono_get_root_domain ();
1609 if (callinfo
->wrapper
) {
1610 return callinfo
->wrapper
;
1613 if (callinfo
->trampoline
)
1614 return callinfo
->trampoline
;
1617 * We use the lock on the root domain instead of the JIT lock to protect
1618 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
1620 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
1621 mono_domain_lock (domain
);
1623 if (callinfo
->trampoline
) {
1624 mono_domain_unlock (domain
);
1625 mono_loader_unlock ();
1626 return callinfo
->trampoline
;
1629 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
1630 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_for_pending_exc
);
1634 trampoline
= mono_compile_method (wrapper
);
1636 trampoline
= mono_create_ftnptr (domain
, mono_create_jit_trampoline_in_domain (domain
, wrapper
));
1637 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
1639 callinfo
->trampoline
= trampoline
;
1641 mono_domain_unlock (domain
);
1642 mono_loader_unlock ();
1644 return callinfo
->trampoline
;
1648 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
1650 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
1654 mono_dynamic_code_hash_insert (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*ji
)
1656 if (!domain_jit_info (domain
)->dynamic_code_hash
)
1657 domain_jit_info (domain
)->dynamic_code_hash
= g_hash_table_new (NULL
, NULL
);
1658 g_hash_table_insert (domain_jit_info (domain
)->dynamic_code_hash
, method
, ji
);
1661 static MonoJitDynamicMethodInfo
*
1662 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
1664 MonoJitDynamicMethodInfo
*res
;
1666 if (domain_jit_info (domain
)->dynamic_code_hash
)
1667 res
= g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1675 GList
*active
, *inactive
;
1680 compare_by_interval_start_pos_func (gconstpointer a
, gconstpointer b
)
1682 MonoMethodVar
*v1
= (MonoMethodVar
*)a
;
1683 MonoMethodVar
*v2
= (MonoMethodVar
*)b
;
1687 else if (v1
->interval
->range
&& v2
->interval
->range
)
1688 return v1
->interval
->range
->from
- v2
->interval
->range
->from
;
1689 else if (v1
->interval
->range
)
1698 #define LSCAN_DEBUG(a) do { a; } while (0)
1700 #define LSCAN_DEBUG(a)
1704 mono_allocate_stack_slots2 (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
1706 int i
, slot
, offset
, size
;
1711 GList
*vars
= NULL
, *l
, *unhandled
;
1712 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
1715 gboolean reuse_slot
;
1717 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg
->method
, TRUE
)));
1719 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
1720 vtype_stack_slots
= NULL
;
1723 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
1724 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
1727 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
1728 inst
= cfg
->varinfo
[i
];
1729 vmv
= MONO_VARINFO (cfg
, i
);
1731 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
1734 vars
= g_list_prepend (vars
, vmv
);
1737 vars
= g_list_sort (g_list_copy (vars
), compare_by_interval_start_pos_func
);
1742 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
1743 MonoMethodVar *current = unhandled->data;
1745 if (current->interval->range) {
1746 g_assert (current->interval->range->from >= i);
1747 i = current->interval->range->from;
1754 for (unhandled
= vars
; unhandled
; unhandled
= unhandled
->next
) {
1755 MonoMethodVar
*current
= unhandled
->data
;
1758 inst
= cfg
->varinfo
[vmv
->idx
];
1760 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
1761 if (cfg
->gsharedvt
&& mini_is_gsharedvt_variable_type (cfg
, t
))
1762 t
= mini_get_gsharedvt_alloc_type_for_type (cfg
, t
);
1764 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1765 * pinvoke wrappers when they call functions returning structures */
1766 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (t
) && t
->type
!= MONO_TYPE_TYPEDBYREF
) {
1767 size
= mono_class_native_size (mono_class_from_mono_type (t
), &align
);
1772 size
= mono_type_size (t
, &ialign
);
1775 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (t
)))
1780 if (cfg
->disable_reuse_stack_slots
)
1784 case MONO_TYPE_GENERICINST
:
1785 if (!mono_type_generic_inst_is_valuetype (t
)) {
1786 slot_info
= &scalar_stack_slots
[t
->type
];
1790 case MONO_TYPE_VALUETYPE
:
1791 if (!vtype_stack_slots
)
1792 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
1793 for (i
= 0; i
< nvtypes
; ++i
)
1794 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
1797 slot_info
= &vtype_stack_slots
[i
];
1799 g_assert (nvtypes
< 256);
1800 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
1801 slot_info
= &vtype_stack_slots
[nvtypes
];
1804 if (cfg
->disable_reuse_ref_stack_slots
)
1811 #if SIZEOF_VOID_P == 4
1816 if (cfg
->disable_ref_noref_stack_slot_share
) {
1817 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
1822 case MONO_TYPE_CLASS
:
1823 case MONO_TYPE_OBJECT
:
1824 case MONO_TYPE_ARRAY
:
1825 case MONO_TYPE_SZARRAY
:
1826 case MONO_TYPE_STRING
:
1827 /* Share non-float stack slots of the same size */
1828 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
1829 if (cfg
->disable_reuse_ref_stack_slots
)
1834 slot_info
= &scalar_stack_slots
[t
->type
];
1838 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
1842 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
1844 if (!current
->interval
->range
) {
1845 if (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
1849 inst
->flags
|= MONO_INST_IS_DEAD
;
1854 pos
= current
->interval
->range
->from
;
1856 LSCAN_DEBUG (printf ("process R%d ", inst
->dreg
));
1857 if (current
->interval
->range
)
1858 LSCAN_DEBUG (mono_linterval_print (current
->interval
));
1859 LSCAN_DEBUG (printf ("\n"));
1861 /* Check for intervals in active which expired or inactive */
1863 /* FIXME: Optimize this */
1866 for (l
= slot_info
->active
; l
!= NULL
; l
= l
->next
) {
1867 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1869 if (v
->interval
->last_range
->to
< pos
) {
1870 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1871 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[v
->idx
]));
1872 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1876 else if (!mono_linterval_covers (v
->interval
, pos
)) {
1877 slot_info
->inactive
= g_list_append (slot_info
->inactive
, v
);
1878 slot_info
->active
= g_list_delete_link (slot_info
->active
, l
);
1879 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg
->varinfo
[v
->idx
]->dreg
));
1886 /* Check for intervals in inactive which expired or active */
1888 /* FIXME: Optimize this */
1891 for (l
= slot_info
->inactive
; l
!= NULL
; l
= l
->next
) {
1892 MonoMethodVar
*v
= (MonoMethodVar
*)l
->data
;
1894 if (v
->interval
->last_range
->to
< pos
) {
1895 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1896 // FIXME: Enabling this seems to cause impossible to debug crashes
1897 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
1898 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg
->varinfo
[v
->idx
]->dreg
, offsets
[v
->idx
]));
1902 else if (mono_linterval_covers (v
->interval
, pos
)) {
1903 slot_info
->active
= g_list_append (slot_info
->active
, v
);
1904 slot_info
->inactive
= g_list_delete_link (slot_info
->inactive
, l
);
1905 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg
->varinfo
[v
->idx
]->dreg
));
1913 * This also handles the case when the variable is used in an
1914 * exception region, as liveness info is not computed there.
1917 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
1920 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
1921 if (slot_info
->slots
) {
1922 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
1924 slot_info
->slots
= slot_info
->slots
->next
;
1927 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
1929 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
1935 static int count
= 0;
1938 if (count
== atoi (getenv ("COUNT3")))
1939 printf ("LAST: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
1940 if (count
> atoi (getenv ("COUNT3")))
1943 mono_print_ins (inst
);
1948 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst
->dreg
, mono_type_full_name (t
), slot
));
1950 if (inst
->flags
& MONO_INST_LMF
) {
1951 size
= sizeof (MonoLMF
);
1952 align
= sizeof (mgreg_t
);
1959 if (slot
== 0xffffff) {
1961 * Allways allocate valuetypes to sizeof (gpointer) to allow more
1962 * efficient copying (and to work around the fact that OP_MEMCPY
1963 * and OP_MEMSET ignores alignment).
1965 if (MONO_TYPE_ISSTRUCT (t
)) {
1966 align
= MAX (align
, sizeof (gpointer
));
1967 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
1972 offset
+= align
- 1;
1973 offset
&= ~(align
- 1);
1977 offset
+= align
- 1;
1978 offset
&= ~(align
- 1);
1983 if (*stack_align
== 0)
1984 *stack_align
= align
;
1987 offsets
[vmv
->idx
] = slot
;
1990 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
1991 if (scalar_stack_slots
[i
].active
)
1992 g_list_free (scalar_stack_slots
[i
].active
);
1994 for (i
= 0; i
< nvtypes
; ++i
) {
1995 if (vtype_stack_slots
[i
].active
)
1996 g_list_free (vtype_stack_slots
[i
].active
);
1999 cfg
->stat_locals_stack_size
+= offset
;
2001 *stack_size
= offset
;
2006 * mono_allocate_stack_slots:
2008 * Allocate stack slots for all non register allocated variables using a
2009 * linear scan algorithm.
2010 * Returns: an array of stack offsets.
2011 * STACK_SIZE is set to the amount of stack space needed.
2012 * STACK_ALIGN is set to the alignment needed by the locals area.
2015 mono_allocate_stack_slots (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
2017 int i
, slot
, offset
, size
;
2022 GList
*vars
= NULL
, *l
;
2023 StackSlotInfo
*scalar_stack_slots
, *vtype_stack_slots
, *slot_info
;
2026 gboolean reuse_slot
;
2028 if ((cfg
->num_varinfo
> 0) && MONO_VARINFO (cfg
, 0)->interval
)
2029 return mono_allocate_stack_slots2 (cfg
, backward
, stack_size
, stack_align
);
2031 scalar_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * MONO_TYPE_PINNED
);
2032 vtype_stack_slots
= NULL
;
2035 offsets
= mono_mempool_alloc (cfg
->mempool
, sizeof (gint32
) * cfg
->num_varinfo
);
2036 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
2039 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
2040 inst
= cfg
->varinfo
[i
];
2041 vmv
= MONO_VARINFO (cfg
, i
);
2043 if ((inst
->flags
& MONO_INST_IS_DEAD
) || inst
->opcode
== OP_REGVAR
|| inst
->opcode
== OP_REGOFFSET
)
2046 vars
= g_list_prepend (vars
, vmv
);
2049 vars
= mono_varlist_sort (cfg
, vars
, 0);
2051 *stack_align
= sizeof(mgreg_t
);
2052 for (l
= vars
; l
; l
= l
->next
) {
2054 inst
= cfg
->varinfo
[vmv
->idx
];
2056 t
= mono_type_get_underlying_type (inst
->inst_vtype
);
2057 if (cfg
->gsharedvt
&& mini_is_gsharedvt_variable_type (cfg
, t
))
2058 t
= mini_get_gsharedvt_alloc_type_for_type (cfg
, t
);
2060 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
2061 * pinvoke wrappers when they call functions returning structures */
2062 if (inst
->backend
.is_pinvoke
&& MONO_TYPE_ISSTRUCT (t
) && t
->type
!= MONO_TYPE_TYPEDBYREF
) {
2063 size
= mono_class_native_size (mono_class_from_mono_type (t
), &align
);
2067 size
= mono_type_size (t
, &ialign
);
2070 if (mono_class_from_mono_type (t
)->exception_type
)
2071 mono_cfg_set_exception (cfg
, MONO_EXCEPTION_TYPE_LOAD
);
2073 if (MONO_CLASS_IS_SIMD (cfg
, mono_class_from_mono_type (t
)))
2078 if (cfg
->disable_reuse_stack_slots
)
2082 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
2085 case MONO_TYPE_GENERICINST
:
2086 if (!mono_type_generic_inst_is_valuetype (t
)) {
2087 slot_info
= &scalar_stack_slots
[t
->type
];
2091 case MONO_TYPE_VALUETYPE
:
2092 if (!vtype_stack_slots
)
2093 vtype_stack_slots
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (StackSlotInfo
) * 256);
2094 for (i
= 0; i
< nvtypes
; ++i
)
2095 if (t
->data
.klass
== vtype_stack_slots
[i
].vtype
)
2098 slot_info
= &vtype_stack_slots
[i
];
2100 g_assert (nvtypes
< 256);
2101 vtype_stack_slots
[nvtypes
].vtype
= t
->data
.klass
;
2102 slot_info
= &vtype_stack_slots
[nvtypes
];
2105 if (cfg
->disable_reuse_ref_stack_slots
)
2112 #if SIZEOF_VOID_P == 4
2117 if (cfg
->disable_ref_noref_stack_slot_share
) {
2118 slot_info
= &scalar_stack_slots
[MONO_TYPE_I
];
2123 case MONO_TYPE_CLASS
:
2124 case MONO_TYPE_OBJECT
:
2125 case MONO_TYPE_ARRAY
:
2126 case MONO_TYPE_SZARRAY
:
2127 case MONO_TYPE_STRING
:
2128 /* Share non-float stack slots of the same size */
2129 slot_info
= &scalar_stack_slots
[MONO_TYPE_CLASS
];
2130 if (cfg
->disable_reuse_ref_stack_slots
)
2134 case MONO_TYPE_MVAR
:
2135 slot_info
= &scalar_stack_slots
[t
->type
];
2138 slot_info
= &scalar_stack_slots
[t
->type
];
2144 if (cfg
->comp_done
& MONO_COMP_LIVENESS
) {
2145 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
2147 /* expire old intervals in active */
2148 while (slot_info
->active
) {
2149 MonoMethodVar
*amv
= (MonoMethodVar
*)slot_info
->active
->data
;
2151 if (amv
->range
.last_use
.abs_pos
> vmv
->range
.first_use
.abs_pos
)
2154 //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);
2156 slot_info
->active
= g_list_delete_link (slot_info
->active
, slot_info
->active
);
2157 slot_info
->slots
= g_slist_prepend_mempool (cfg
->mempool
, slot_info
->slots
, GINT_TO_POINTER (offsets
[amv
->idx
]));
2161 * This also handles the case when the variable is used in an
2162 * exception region, as liveness info is not computed there.
2165 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
2168 if (! (inst
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
2169 if (slot_info
->slots
) {
2170 slot
= GPOINTER_TO_INT (slot_info
->slots
->data
);
2172 slot_info
->slots
= slot_info
->slots
->next
;
2175 slot_info
->active
= mono_varlist_insert_sorted (cfg
, slot_info
->active
, vmv
, TRUE
);
2180 static int count
= 0;
2184 if (count == atoi (getenv ("COUNT")))
2185 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
2186 if (count > atoi (getenv ("COUNT")))
2189 mono_print_ins (inst);
2194 if (inst
->flags
& MONO_INST_LMF
) {
2196 * This variable represents a MonoLMF structure, which has no corresponding
2197 * CLR type, so hard-code its size/alignment.
2199 size
= sizeof (MonoLMF
);
2200 align
= sizeof (mgreg_t
);
2207 if (slot
== 0xffffff) {
2209 * Allways allocate valuetypes to sizeof (gpointer) to allow more
2210 * efficient copying (and to work around the fact that OP_MEMCPY
2211 * and OP_MEMSET ignores alignment).
2213 if (MONO_TYPE_ISSTRUCT (t
)) {
2214 align
= MAX (align
, sizeof (gpointer
));
2215 align
= MAX (align
, mono_class_min_align (mono_class_from_mono_type (t
)));
2217 * Align the size too so the code generated for passing vtypes in
2218 * registers doesn't overwrite random locals.
2220 size
= (size
+ (align
- 1)) & ~(align
-1);
2225 offset
+= align
- 1;
2226 offset
&= ~(align
- 1);
2230 offset
+= align
- 1;
2231 offset
&= ~(align
- 1);
2236 *stack_align
= MAX (*stack_align
, align
);
2239 offsets
[vmv
->idx
] = slot
;
2242 for (i
= 0; i
< MONO_TYPE_PINNED
; ++i
) {
2243 if (scalar_stack_slots
[i
].active
)
2244 g_list_free (scalar_stack_slots
[i
].active
);
2246 for (i
= 0; i
< nvtypes
; ++i
) {
2247 if (vtype_stack_slots
[i
].active
)
2248 g_list_free (vtype_stack_slots
[i
].active
);
2251 cfg
->stat_locals_stack_size
+= offset
;
2253 *stack_size
= offset
;
2260 mono_allocate_stack_slots (MonoCompile
*cfg
, gboolean backward
, guint32
*stack_size
, guint32
*stack_align
)
2262 g_assert_not_reached ();
2266 #endif /* DISABLE_JIT */
2268 #define EMUL_HIT_SHIFT 3
2269 #define EMUL_HIT_MASK ((1 << EMUL_HIT_SHIFT) - 1)
2270 /* small hit bitmap cache */
2271 static mono_byte emul_opcode_hit_cache
[(OP_LAST
>>EMUL_HIT_SHIFT
) + 1] = {0};
2272 static short emul_opcode_num
= 0;
2273 static short emul_opcode_alloced
= 0;
2274 static short *emul_opcode_opcodes
= NULL
;
2275 static MonoJitICallInfo
**emul_opcode_map
= NULL
;
2278 mono_find_jit_opcode_emulation (int opcode
)
2280 g_assert (opcode
>= 0 && opcode
<= OP_LAST
);
2281 if (emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] & (1 << (opcode
& EMUL_HIT_MASK
))) {
2283 for (i
= 0; i
< emul_opcode_num
; ++i
) {
2284 if (emul_opcode_opcodes
[i
] == opcode
)
2285 return emul_opcode_map
[i
];
2292 mono_register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, gboolean no_throw
)
2294 MonoJitICallInfo
*info
;
2295 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
2297 g_assert (!sig
->hasthis
);
2298 g_assert (sig
->param_count
< 3);
2300 info
= mono_register_jit_icall (func
, name
, sig
, no_throw
);
2302 if (emul_opcode_num
>= emul_opcode_alloced
) {
2303 int incr
= emul_opcode_alloced
? emul_opcode_alloced
/2: 16;
2304 emul_opcode_alloced
+= incr
;
2305 emul_opcode_map
= g_realloc (emul_opcode_map
, sizeof (emul_opcode_map
[0]) * emul_opcode_alloced
);
2306 emul_opcode_opcodes
= g_realloc (emul_opcode_opcodes
, sizeof (emul_opcode_opcodes
[0]) * emul_opcode_alloced
);
2308 emul_opcode_map
[emul_opcode_num
] = info
;
2309 emul_opcode_opcodes
[emul_opcode_num
] = opcode
;
2311 emul_opcode_hit_cache
[opcode
>> (EMUL_HIT_SHIFT
+ 3)] |= (1 << (opcode
& EMUL_HIT_MASK
));
2315 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
2317 MonoMethodSignature
*sig
;
2320 sig
= mono_create_icall_signature (sigstr
);
2324 mono_register_jit_icall (func
, name
, sig
, save
);
2328 print_dfn (MonoCompile
*cfg
) {
2335 char *method_name
= mono_method_full_name (cfg
->method
, TRUE
);
2336 g_print ("IR code for method %s\n", method_name
);
2337 g_free (method_name
);
2340 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
2341 bb
= cfg
->bblocks
[i
];
2342 /*if (bb->cil_code) {
2343 char* code1, *code2;
2344 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
2345 if (bb->last_ins->cil_code)
2346 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
2348 code2 = g_strdup ("");
2350 code1 [strlen (code1) - 1] = 0;
2351 code = g_strdup_printf ("%s -> %s", code1, code2);
2355 code
= g_strdup ("\n");
2356 g_print ("\nBB%d (%d) (len: %d): %s", bb
->block_num
, i
, bb
->cil_length
, code
);
2357 MONO_BB_FOR_EACH_INS (bb
, c
) {
2358 mono_print_ins_index (-1, c
);
2361 g_print ("\tprev:");
2362 for (j
= 0; j
< bb
->in_count
; ++j
) {
2363 g_print (" BB%d", bb
->in_bb
[j
]->block_num
);
2365 g_print ("\t\tsucc:");
2366 for (j
= 0; j
< bb
->out_count
; ++j
) {
2367 g_print (" BB%d", bb
->out_bb
[j
]->block_num
);
2369 g_print ("\n\tidom: BB%d\n", bb
->idom
? bb
->idom
->block_num
: -1);
2372 g_assert (mono_bitset_test_fast (bb
->dominators
, bb
->idom
->dfn
));
2375 mono_blockset_print (cfg
, bb
->dominators
, "\tdominators", bb
->idom
? bb
->idom
->dfn
: -1);
2377 mono_blockset_print (cfg
, bb
->dfrontier
, "\tdfrontier", -1);
2385 mono_bblock_add_inst (MonoBasicBlock
*bb
, MonoInst
*inst
)
2387 MONO_ADD_INS (bb
, inst
);
2391 mono_bblock_insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2395 bb
->code
= ins_to_insert
;
2397 /* Link with next */
2398 ins_to_insert
->next
= ins
;
2400 ins
->prev
= ins_to_insert
;
2402 if (bb
->last_ins
== NULL
)
2403 bb
->last_ins
= ins_to_insert
;
2405 /* Link with next */
2406 ins_to_insert
->next
= ins
->next
;
2408 ins
->next
->prev
= ins_to_insert
;
2410 /* Link with previous */
2411 ins
->next
= ins_to_insert
;
2412 ins_to_insert
->prev
= ins
;
2414 if (bb
->last_ins
== ins
)
2415 bb
->last_ins
= ins_to_insert
;
2420 mono_bblock_insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
*ins_to_insert
)
2425 ins
->prev
= ins_to_insert
;
2426 bb
->code
= ins_to_insert
;
2427 ins_to_insert
->next
= ins
;
2428 if (bb
->last_ins
== NULL
)
2429 bb
->last_ins
= ins_to_insert
;
2431 /* Link with previous */
2433 ins
->prev
->next
= ins_to_insert
;
2434 ins_to_insert
->prev
= ins
->prev
;
2436 /* Link with next */
2437 ins
->prev
= ins_to_insert
;
2438 ins_to_insert
->next
= ins
;
2440 if (bb
->code
== ins
)
2441 bb
->code
= ins_to_insert
;
2446 * mono_verify_bblock:
2448 * Verify that the next and prev pointers are consistent inside the instructions in BB.
2451 mono_verify_bblock (MonoBasicBlock
*bb
)
2453 MonoInst
*ins
, *prev
;
2456 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2457 g_assert (ins
->prev
== prev
);
2461 g_assert (!bb
->last_ins
->next
);
2467 * Perform consistency checks on the JIT data structures and the IR
2470 mono_verify_cfg (MonoCompile
*cfg
)
2474 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
2475 mono_verify_bblock (bb
);
2479 mono_destroy_compile (MonoCompile
*cfg
)
2484 mono_metadata_free_mh (cfg
->header
);
2485 //mono_mempool_stats (cfg->mempool);
2486 mono_free_loop_info (cfg
);
2488 mono_regstate_free (cfg
->rs
);
2490 g_hash_table_destroy (cfg
->spvars
);
2492 g_hash_table_destroy (cfg
->exvars
);
2493 for (l
= cfg
->headers_to_free
; l
; l
= l
->next
)
2494 mono_metadata_free_mh (l
->data
);
2495 g_list_free (cfg
->ldstr_list
);
2496 g_hash_table_destroy (cfg
->token_info_hash
);
2497 if (cfg
->abs_patches
)
2498 g_hash_table_destroy (cfg
->abs_patches
);
2499 mono_mempool_destroy (cfg
->mempool
);
2501 mono_debug_free_method (cfg
);
2503 g_free (cfg
->varinfo
);
2505 g_free (cfg
->exception_message
);
2509 #ifdef MONO_HAVE_FAST_TLS
2510 MONO_FAST_TLS_DECLARE(mono_lmf_addr
);
2511 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
2513 * When this is defined, the current lmf is stored in this tls variable instead of in
2516 MONO_FAST_TLS_DECLARE(mono_lmf
);
2521 mono_get_jit_tls_key (void)
2523 return mono_jit_tls_id
;
2527 mono_get_jit_tls_offset (void)
2530 MONO_THREAD_VAR_OFFSET (mono_jit_tls
, offset
);
2535 mono_get_lmf_tls_offset (void)
2537 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2539 MONO_THREAD_VAR_OFFSET(mono_lmf
,offset
);
2547 mono_get_lmf_addr_tls_offset (void)
2550 MONO_THREAD_VAR_OFFSET(mono_lmf_addr
,offset
);
2557 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2558 return MONO_FAST_TLS_GET (mono_lmf
);
2560 MonoJitTlsData
*jit_tls
;
2562 if ((jit_tls
= mono_native_tls_get_value (mono_jit_tls_id
)))
2563 return jit_tls
->lmf
;
2565 * We do not assert here because this function can be called from
2566 * mini-gc.c on a thread that has not executed any managed code, yet
2567 * (the thread object allocation can trigger a collection).
2574 mono_get_lmf_addr (void)
2576 #ifdef MONO_HAVE_FAST_TLS
2577 return MONO_FAST_TLS_GET (mono_lmf_addr
);
2579 MonoJitTlsData
*jit_tls
;
2581 if ((jit_tls
= mono_native_tls_get_value (mono_jit_tls_id
)))
2582 return &jit_tls
->lmf
;
2585 * When resolving the call to mono_jit_thread_attach full-aot will look
2586 * in the plt, which causes a call into the generic trampoline, which in turn
2587 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
2588 * call mono_jit_thread_attach from the native-to-managed wrapper, without
2589 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
2592 mono_jit_thread_attach (NULL
);
2594 if ((jit_tls
= mono_native_tls_get_value (mono_jit_tls_id
)))
2595 return &jit_tls
->lmf
;
2597 g_assert_not_reached ();
2603 mono_set_lmf (MonoLMF
*lmf
)
2605 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2606 MONO_FAST_TLS_SET (mono_lmf
, lmf
);
2609 (*mono_get_lmf_addr ()) = lmf
;
2613 mono_set_jit_tls (MonoJitTlsData
*jit_tls
)
2615 mono_native_tls_set_value (mono_jit_tls_id
, jit_tls
);
2617 #ifdef MONO_HAVE_FAST_TLS
2618 MONO_FAST_TLS_SET (mono_jit_tls
, jit_tls
);
2623 mono_set_lmf_addr (gpointer lmf_addr
)
2625 #ifdef MONO_HAVE_FAST_TLS
2626 MONO_FAST_TLS_SET (mono_lmf_addr
, lmf_addr
);
2631 * mono_jit_thread_attach:
2633 * Called by native->managed wrappers. Returns the original domain which needs to be
2634 * restored, or NULL.
2637 mono_jit_thread_attach (MonoDomain
*domain
)
2643 * Happens when called from AOTed code which is only used in the root
2646 domain
= mono_get_root_domain ();
2648 #ifdef MONO_HAVE_FAST_TLS
2649 if (!MONO_FAST_TLS_GET (mono_lmf_addr
)) {
2650 mono_thread_attach (domain
);
2652 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background
);
2655 if (!mono_native_tls_get_value (mono_jit_tls_id
)) {
2656 mono_thread_attach (domain
);
2657 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background
);
2660 orig
= mono_domain_get ();
2662 mono_domain_set (domain
, TRUE
);
2664 return orig
!= domain
? orig
: NULL
;
2667 /* Called by native->managed wrappers */
2669 mono_jit_set_domain (MonoDomain
*domain
)
2672 mono_domain_set (domain
, TRUE
);
2676 * mono_thread_abort:
2677 * @obj: exception object
2679 * abort the thread, print exception information and stack trace
2682 mono_thread_abort (MonoObject
*obj
)
2684 /* MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id); */
2686 /* handle_remove should be eventually called for this thread, too
2689 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
2690 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
2691 mono_thread_exit ();
2693 mono_invoke_unhandled_exception_hook (obj
);
2698 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
2700 MonoJitTlsData
*jit_tls
;
2703 jit_tls
= mono_native_tls_get_value (mono_jit_tls_id
);
2707 jit_tls
= g_new0 (MonoJitTlsData
, 1);
2709 jit_tls
->abort_func
= abort_func
;
2710 jit_tls
->end_of_stack
= stack_start
;
2712 mono_set_jit_tls (jit_tls
);
2714 lmf
= g_new0 (MonoLMF
, 1);
2715 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
2717 jit_tls
->first_lmf
= lmf
;
2719 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
2720 /* jit_tls->lmf is unused */
2721 MONO_FAST_TLS_SET (mono_lmf
, lmf
);
2722 mono_set_lmf_addr (MONO_FAST_TLS_ADDR (mono_lmf
));
2724 mono_set_lmf_addr (&jit_tls
->lmf
);
2729 mono_setup_altstack (jit_tls
);
2735 free_jit_tls_data (MonoJitTlsData
*jit_tls
)
2737 mono_arch_free_jit_tls_data (jit_tls
);
2738 mono_free_altstack (jit_tls
);
2740 g_free (jit_tls
->first_lmf
);
2745 mono_thread_start_cb (intptr_t tid
, gpointer stack_start
, gpointer func
)
2747 MonoInternalThread
*thread
;
2748 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
2749 thread
= mono_thread_internal_current ();
2750 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, jit_tls
, func
);
2752 thread
->jit_data
= jit_tls
;
2754 mono_arch_cpu_init ();
2757 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
2760 mono_thread_abort_dummy (MonoObject
*obj
)
2762 if (mono_thread_attach_aborted_cb
)
2763 mono_thread_attach_aborted_cb (obj
);
2765 mono_thread_abort (obj
);
2769 mono_thread_attach_cb (intptr_t tid
, gpointer stack_start
)
2771 MonoInternalThread
*thread
;
2772 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
2773 thread
= mono_thread_internal_current ();
2774 mono_debugger_thread_created (tid
, thread
->root_domain_thread
, (MonoJitTlsData
*) jit_tls
, NULL
);
2776 thread
->jit_data
= jit_tls
;
2777 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
2778 mono_runtime_setup_stat_profiler ();
2780 mono_arch_cpu_init ();
2784 mini_thread_cleanup (MonoInternalThread
*thread
)
2786 MonoJitTlsData
*jit_tls
= thread
->jit_data
;
2789 mono_debugger_thread_cleanup (jit_tls
);
2791 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
2792 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
2793 * not a trivial thing.
2795 * The current offender is mono_thread_manage which cleanup threads from the outside.
2797 if (thread
== mono_thread_internal_current ())
2798 mono_set_jit_tls (NULL
);
2800 /* If we attach a thread but never call into managed land, we might never get an lmf.*/
2801 if (mono_get_lmf ()) {
2802 mono_set_lmf (NULL
);
2803 mono_set_lmf_addr (NULL
);
2806 free_jit_tls_data (jit_tls
);
2808 thread
->jit_data
= NULL
;
2813 mono_create_tls_get (MonoCompile
*cfg
, int offset
)
2815 #ifdef MONO_ARCH_HAVE_TLS_GET
2816 if (MONO_ARCH_HAVE_TLS_GET
) {
2822 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
2823 ins
->dreg
= mono_alloc_preg (cfg
);
2824 ins
->inst_offset
= offset
;
2832 mono_get_jit_tls_intrinsic (MonoCompile
*cfg
)
2834 return mono_create_tls_get (cfg
, mono_get_jit_tls_offset ());
2838 mono_get_domain_intrinsic (MonoCompile
* cfg
)
2840 return mono_create_tls_get (cfg
, mono_domain_get_tls_offset ());
2844 mono_get_thread_intrinsic (MonoCompile
* cfg
)
2846 return mono_create_tls_get (cfg
, mono_thread_get_tls_offset ());
2850 mono_get_lmf_intrinsic (MonoCompile
* cfg
)
2852 return mono_create_tls_get (cfg
, mono_get_lmf_tls_offset ());
2856 mono_add_patch_info (MonoCompile
*cfg
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2858 MonoJumpInfo
*ji
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoJumpInfo
));
2862 ji
->data
.target
= target
;
2863 ji
->next
= cfg
->patch_info
;
2865 cfg
->patch_info
= ji
;
2869 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
2871 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
2875 ji
->data
.target
= target
;
2882 mono_remove_patch_info (MonoCompile
*cfg
, int ip
)
2884 MonoJumpInfo
**ji
= &cfg
->patch_info
;
2887 if ((*ji
)->ip
.i
== ip
)
2890 ji
= &((*ji
)->next
);
2895 * mono_patch_info_dup_mp:
2897 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
2900 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
2902 MonoJumpInfo
*res
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
2903 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
2905 switch (patch_info
->type
) {
2906 case MONO_PATCH_INFO_RVA
:
2907 case MONO_PATCH_INFO_LDSTR
:
2908 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2909 case MONO_PATCH_INFO_LDTOKEN
:
2910 case MONO_PATCH_INFO_DECLSEC
:
2911 res
->data
.token
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
2912 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
2914 case MONO_PATCH_INFO_SWITCH
:
2915 res
->data
.table
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
2916 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
2917 res
->data
.table
->table
= mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2918 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
2920 case MONO_PATCH_INFO_RGCTX_FETCH
:
2921 res
->data
.rgctx_entry
= mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
2922 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
2923 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
2933 mono_patch_info_hash (gconstpointer data
)
2935 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
2938 case MONO_PATCH_INFO_RVA
:
2939 case MONO_PATCH_INFO_LDSTR
:
2940 case MONO_PATCH_INFO_LDTOKEN
:
2941 case MONO_PATCH_INFO_DECLSEC
:
2942 return (ji
->type
<< 8) | ji
->data
.token
->token
;
2943 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2944 return (ji
->type
<< 8) | ji
->data
.token
->token
| (ji
->data
.token
->has_context
? (gsize
)ji
->data
.token
->context
.class_inst
: 0);
2945 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2946 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
2947 case MONO_PATCH_INFO_VTABLE
:
2948 case MONO_PATCH_INFO_CLASS
:
2949 case MONO_PATCH_INFO_IID
:
2950 case MONO_PATCH_INFO_ADJUSTED_IID
:
2951 case MONO_PATCH_INFO_CLASS_INIT
:
2952 case MONO_PATCH_INFO_METHODCONST
:
2953 case MONO_PATCH_INFO_METHOD
:
2954 case MONO_PATCH_INFO_METHOD_JUMP
:
2955 case MONO_PATCH_INFO_IMAGE
:
2956 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
2957 case MONO_PATCH_INFO_FIELD
:
2958 case MONO_PATCH_INFO_SFLDA
:
2959 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
2960 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
2962 return (ji
->type
<< 8);
2967 * mono_patch_info_equal:
2969 * This might fail to recognize equivalent patches, i.e. floats, so its only
2970 * usable in those cases where this is not a problem, i.e. sharing GOT slots
2974 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
2976 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
2977 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
2979 if (ji1
->type
!= ji2
->type
)
2982 switch (ji1
->type
) {
2983 case MONO_PATCH_INFO_RVA
:
2984 case MONO_PATCH_INFO_LDSTR
:
2985 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
2986 case MONO_PATCH_INFO_LDTOKEN
:
2987 case MONO_PATCH_INFO_DECLSEC
:
2988 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
2989 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
2990 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
2991 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
2992 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
2995 case MONO_PATCH_INFO_INTERNAL_METHOD
:
2996 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
2998 case MONO_PATCH_INFO_RGCTX_FETCH
: {
2999 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
3000 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
3002 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
);
3005 if (ji1
->data
.target
!= ji2
->data
.target
)
3014 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
)
3016 unsigned char *ip
= patch_info
->ip
.i
+ code
;
3017 gconstpointer target
= NULL
;
3019 switch (patch_info
->type
) {
3020 case MONO_PATCH_INFO_BB
:
3022 * FIXME: This could be hit for methods without a prolog. Should use -1
3023 * but too much code depends on a 0 initial value.
3025 //g_assert (patch_info->data.bb->native_offset);
3026 target
= patch_info
->data
.bb
->native_offset
+ code
;
3028 case MONO_PATCH_INFO_ABS
:
3029 target
= patch_info
->data
.target
;
3031 case MONO_PATCH_INFO_LABEL
:
3032 target
= patch_info
->data
.inst
->inst_c0
+ code
;
3034 case MONO_PATCH_INFO_IP
:
3035 #if defined(__native_client__) && defined(__native_client_codegen__)
3036 /* Need to transform to the destination address, it's */
3037 /* emitted as an immediate in the code. */
3038 target
= nacl_inverse_modify_patch_target(ip
);
3043 case MONO_PATCH_INFO_METHOD_REL
:
3044 target
= code
+ patch_info
->data
.offset
;
3046 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
3047 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
3049 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
3050 g_assert_not_reached ();
3052 target
= mono_icall_get_wrapper (mi
);
3055 case MONO_PATCH_INFO_METHOD_JUMP
:
3056 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
);
3057 #if defined(__native_client__) && defined(__native_client_codegen__)
3058 #if defined(TARGET_AMD64)
3059 /* This target is an absolute address, not relative to the */
3060 /* current code being emitted on AMD64. */
3061 target
= nacl_inverse_modify_patch_target(target
);
3065 case MONO_PATCH_INFO_METHOD
:
3066 if (patch_info
->data
.method
== method
) {
3069 /* get the trampoline to the method from the domain */
3070 target
= mono_create_jit_trampoline_in_domain (domain
, patch_info
->data
.method
);
3073 case MONO_PATCH_INFO_SWITCH
: {
3074 gpointer
*jump_table
;
3077 #if defined(__native_client__) && defined(__native_client_codegen__)
3078 /* This memory will leak, but we don't care if we're */
3079 /* not deleting JIT'd methods anyway */
3080 jump_table
= g_malloc0 (sizeof(gpointer
) * patch_info
->data
.table
->table_size
);
3082 if (method
&& method
->dynamic
) {
3083 jump_table
= mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3085 if (mono_aot_only
) {
3086 jump_table
= mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3088 jump_table
= mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3093 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3094 #if defined(__native_client__) && defined(__native_client_codegen__)
3095 /* 'code' is relative to the current code blob, we */
3096 /* need to do this transform on it to make the */
3097 /* pointers in this table absolute */
3098 jump_table
[i
] = nacl_inverse_modify_patch_target (code
) + GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
3100 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
3104 #if defined(__native_client__) && defined(__native_client_codegen__)
3105 /* jump_table is in the data section, we need to transform */
3106 /* it here so when it gets modified in amd64_patch it will */
3107 /* then point back to the absolute data address */
3108 target
= nacl_inverse_modify_patch_target (jump_table
);
3110 target
= jump_table
;
3114 case MONO_PATCH_INFO_METHODCONST
:
3115 case MONO_PATCH_INFO_CLASS
:
3116 case MONO_PATCH_INFO_IMAGE
:
3117 case MONO_PATCH_INFO_FIELD
:
3118 case MONO_PATCH_INFO_SIGNATURE
:
3119 target
= patch_info
->data
.target
;
3121 case MONO_PATCH_INFO_IID
:
3122 mono_class_init (patch_info
->data
.klass
);
3123 target
= GINT_TO_POINTER ((int)patch_info
->data
.klass
->interface_id
);
3125 case MONO_PATCH_INFO_ADJUSTED_IID
:
3126 mono_class_init (patch_info
->data
.klass
);
3127 target
= GINT_TO_POINTER ((int)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
3129 case MONO_PATCH_INFO_VTABLE
:
3130 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
3133 case MONO_PATCH_INFO_CLASS_INIT
: {
3134 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.klass
);
3137 target
= mono_create_class_init_trampoline (vtable
);
3140 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
3141 target
= mono_create_delegate_trampoline (domain
, patch_info
->data
.klass
);
3143 case MONO_PATCH_INFO_SFLDA
: {
3144 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
3146 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
3147 gpointer addr
= NULL
;
3149 mono_domain_lock (domain
);
3150 if (domain
->special_static_fields
)
3151 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
3152 mono_domain_unlock (domain
);
3158 if (!vtable
->initialized
&& !(vtable
->klass
->flags
& TYPE_ATTRIBUTE_BEFORE_FIELD_INIT
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
3159 /* Done by the generated code */
3163 mono_runtime_class_init (vtable
);
3165 target
= (char*)mono_vtable_get_static_field_data (vtable
) + patch_info
->data
.field
->offset
;
3168 case MONO_PATCH_INFO_RVA
: {
3169 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
3172 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
3173 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
3176 case MONO_PATCH_INFO_R4
:
3177 case MONO_PATCH_INFO_R8
:
3178 target
= patch_info
->data
.target
;
3180 case MONO_PATCH_INFO_EXC_NAME
:
3181 target
= patch_info
->data
.name
;
3183 case MONO_PATCH_INFO_LDSTR
:
3185 mono_ldstr (domain
, patch_info
->data
.token
->image
,
3186 mono_metadata_token_index (patch_info
->data
.token
->token
));
3188 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
3190 MonoClass
*handle_class
;
3192 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
3193 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
);
3194 mono_class_init (handle_class
);
3195 mono_class_init (mono_class_from_mono_type (handle
));
3198 mono_type_get_object (domain
, handle
);
3201 case MONO_PATCH_INFO_LDTOKEN
: {
3203 MonoClass
*handle_class
;
3205 handle
= mono_ldtoken (patch_info
->data
.token
->image
,
3206 patch_info
->data
.token
->token
, &handle_class
, NULL
);
3207 mono_class_init (handle_class
);
3212 case MONO_PATCH_INFO_DECLSEC
:
3213 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
3215 case MONO_PATCH_INFO_ICALL_ADDR
:
3216 /* run_cctors == 0 -> AOT */
3217 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
3218 const char *exc_class
;
3219 const char *exc_arg
;
3222 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, &exc_class
, &exc_arg
);
3225 mono_raise_exception (mono_exception_from_name_msg (mono_defaults
.corlib
, "System", exc_class
, exc_arg
));
3226 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
));
3232 target
= mono_lookup_internal_call (patch_info
->data
.method
);
3234 if (!target
&& run_cctors
)
3235 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
3238 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
3239 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
3241 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
3242 g_assert_not_reached ();
3247 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
3248 target
= mono_thread_interruption_request_flag ();
3250 case MONO_PATCH_INFO_METHOD_RGCTX
: {
3251 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
3254 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
3257 case MONO_PATCH_INFO_BB_OVF
:
3258 case MONO_PATCH_INFO_EXC_OVF
:
3259 case MONO_PATCH_INFO_GOT_OFFSET
:
3260 case MONO_PATCH_INFO_NONE
:
3262 case MONO_PATCH_INFO_RGCTX_FETCH
: {
3263 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
3266 switch (entry
->data
->type
) {
3267 case MONO_PATCH_INFO_CLASS
:
3268 slot
= mono_method_lookup_or_register_info (entry
->method
, entry
->in_mrgctx
, &entry
->data
->data
.klass
->byval_arg
, entry
->info_type
, mono_method_get_context (entry
->method
));
3270 case MONO_PATCH_INFO_METHOD
:
3271 case MONO_PATCH_INFO_METHODCONST
:
3272 slot
= mono_method_lookup_or_register_info (entry
->method
, entry
->in_mrgctx
, entry
->data
->data
.method
, entry
->info_type
, mono_method_get_context (entry
->method
));
3274 case MONO_PATCH_INFO_FIELD
:
3275 slot
= mono_method_lookup_or_register_info (entry
->method
, entry
->in_mrgctx
, entry
->data
->data
.field
, entry
->info_type
, mono_method_get_context (entry
->method
));
3278 g_assert_not_reached ();
3282 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
3285 case MONO_PATCH_INFO_GENERIC_CLASS_INIT
:
3286 target
= mono_create_generic_class_init_trampoline ();
3288 case MONO_PATCH_INFO_MONITOR_ENTER
:
3289 target
= mono_create_monitor_enter_trampoline ();
3291 case MONO_PATCH_INFO_MONITOR_EXIT
:
3292 target
= mono_create_monitor_exit_trampoline ();
3294 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3295 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
3297 /* AOT, not needed */
3300 target
= mono_arch_get_seq_point_info (domain
, code
);
3303 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE
:
3304 #ifdef MONO_ARCH_LLVM_SUPPORTED
3305 g_assert (mono_use_llvm
);
3306 target
= mono_create_llvm_imt_trampoline (domain
, patch_info
->data
.imt_tramp
->method
, patch_info
->data
.imt_tramp
->vt_offset
);
3308 g_assert_not_reached ();
3311 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
: {
3312 int card_table_shift_bits
;
3313 gpointer card_table_mask
;
3315 target
= mono_gc_get_card_table (&card_table_shift_bits
, &card_table_mask
);
3318 case MONO_PATCH_INFO_CASTCLASS_CACHE
: {
3319 target
= mono_domain_alloc0 (domain
, sizeof (gpointer
));
3323 g_assert_not_reached ();
3326 return (gpointer
)target
;
3330 mono_add_seq_point (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, int native_offset
)
3332 ins
->inst_offset
= native_offset
;
3333 g_ptr_array_add (cfg
->seq_points
, ins
);
3334 bb
->seq_points
= g_slist_prepend_mempool (cfg
->mempool
, bb
->seq_points
, ins
);
3335 bb
->last_seq_point
= ins
;
3339 mono_add_var_location (MonoCompile
*cfg
, MonoInst
*var
, gboolean is_reg
, int reg
, int offset
, int from
, int to
)
3341 MonoDwarfLocListEntry
*entry
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoDwarfLocListEntry
));
3344 g_assert (offset
== 0);
3346 entry
->is_reg
= is_reg
;
3348 entry
->offset
= offset
;
3352 if (var
== cfg
->args
[0])
3353 cfg
->this_loclist
= g_slist_append_mempool (cfg
->mempool
, cfg
->this_loclist
, entry
);
3354 else if (var
== cfg
->rgctx_var
)
3355 cfg
->rgctx_loclist
= g_slist_append_mempool (cfg
->mempool
, cfg
->rgctx_loclist
, entry
);
3361 mono_compile_create_vars (MonoCompile
*cfg
)
3363 MonoMethodSignature
*sig
;
3364 MonoMethodHeader
*header
;
3367 header
= cfg
->header
;
3369 sig
= mono_method_signature (cfg
->method
);
3371 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
3372 cfg
->ret
= mono_compile_create_var (cfg
, sig
->ret
, OP_ARG
);
3373 /* Inhibit optimizations */
3374 cfg
->ret
->flags
|= MONO_INST_VOLATILE
;
3376 if (cfg
->verbose_level
> 2)
3377 g_print ("creating vars\n");
3379 cfg
->args
= mono_mempool_alloc0 (cfg
->mempool
, (sig
->param_count
+ sig
->hasthis
) * sizeof (MonoInst
*));
3382 cfg
->args
[0] = mono_compile_create_var (cfg
, &cfg
->method
->klass
->this_arg
, OP_ARG
);
3384 for (i
= 0; i
< sig
->param_count
; ++i
) {
3385 cfg
->args
[i
+ sig
->hasthis
] = mono_compile_create_var (cfg
, sig
->params
[i
], OP_ARG
);
3388 if (cfg
->verbose_level
> 2) {
3390 printf ("\treturn : ");
3391 mono_print_ins (cfg
->ret
);
3395 printf ("\tthis: ");
3396 mono_print_ins (cfg
->args
[0]);
3399 for (i
= 0; i
< sig
->param_count
; ++i
) {
3400 printf ("\targ [%d]: ", i
);
3401 mono_print_ins (cfg
->args
[i
+ sig
->hasthis
]);
3405 cfg
->locals_start
= cfg
->num_varinfo
;
3406 cfg
->locals
= mono_mempool_alloc0 (cfg
->mempool
, header
->num_locals
* sizeof (MonoInst
*));
3408 if (cfg
->verbose_level
> 2)
3409 g_print ("creating locals\n");
3411 for (i
= 0; i
< header
->num_locals
; ++i
)
3412 cfg
->locals
[i
] = mono_compile_create_var (cfg
, header
->locals
[i
], OP_LOCAL
);
3414 if (cfg
->verbose_level
> 2)
3415 g_print ("locals done\n");
3417 mono_arch_create_vars (cfg
);
3420 #endif /* #ifndef DISABLE_JIT */
3423 mono_print_code (MonoCompile
*cfg
, const char* msg
)
3427 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
3428 mono_print_bb (bb
, msg
);
3434 mono_postprocess_patches (MonoCompile
*cfg
)
3436 MonoJumpInfo
*patch_info
;
3439 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3440 switch (patch_info
->type
) {
3441 case MONO_PATCH_INFO_ABS
: {
3442 MonoJitICallInfo
*info
= mono_find_jit_icall_by_addr (patch_info
->data
.target
);
3445 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
3449 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
3450 // FIXME: CLEAN UP THIS MESS.
3451 if ((cfg
->method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) &&
3452 strstr (cfg
->method
->name
, info
->name
)) {
3454 * This is an icall wrapper, and this is a call to the
3457 if (cfg
->compile_aot
) {
3458 patch_info
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR
;
3459 patch_info
->data
.name
= info
->name
;
3462 /* for these array methods we currently register the same function pointer
3463 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
3464 * will return the incorrect one depending on the order they are registered.
3465 * See tests/test-arr.cs
3467 if (strstr (info
->name
, "ves_array_new_va_") == NULL
&& strstr (info
->name
, "ves_array_element_address_") == NULL
) {
3468 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3469 patch_info
->data
.name
= info
->name
;
3474 if (patch_info
->type
== MONO_PATCH_INFO_ABS
) {
3475 if (cfg
->abs_patches
) {
3476 MonoJumpInfo
*abs_ji
= g_hash_table_lookup (cfg
->abs_patches
, patch_info
->data
.target
);
3478 patch_info
->type
= abs_ji
->type
;
3479 patch_info
->data
.target
= abs_ji
->data
.target
;
3486 case MONO_PATCH_INFO_SWITCH
: {
3488 #if defined(__native_client__) && defined(__native_client_codegen__)
3489 /* This memory will leak. */
3490 /* TODO: can we free this when */
3491 /* making the final jump table? */
3492 table
= g_malloc0 (sizeof(gpointer
) * patch_info
->data
.table
->table_size
);
3494 if (cfg
->method
->dynamic
) {
3495 table
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3497 table
= mono_domain_code_reserve (cfg
->domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
3501 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
3502 /* Might be NULL if the switch is eliminated */
3503 if (patch_info
->data
.table
->table
[i
]) {
3504 g_assert (patch_info
->data
.table
->table
[i
]->native_offset
);
3505 table
[i
] = GINT_TO_POINTER (patch_info
->data
.table
->table
[i
]->native_offset
);
3510 patch_info
->data
.table
->table
= (MonoBasicBlock
**)table
;
3513 case MONO_PATCH_INFO_METHOD_JUMP
: {
3514 MonoJumpList
*jlist
;
3515 MonoDomain
*domain
= cfg
->domain
;
3516 unsigned char *ip
= cfg
->native_code
+ patch_info
->ip
.i
;
3517 #if defined(__native_client__) && defined(__native_client_codegen__)
3518 /* When this jump target gets evaluated, the method */
3519 /* will be installed in the dynamic code section, */
3520 /* not at the location of cfg->native_code. */
3521 ip
= nacl_inverse_modify_patch_target (cfg
->native_code
) + patch_info
->ip
.i
;
3524 mono_domain_lock (domain
);
3525 jlist
= g_hash_table_lookup (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
);
3527 jlist
= mono_domain_alloc0 (domain
, sizeof (MonoJumpList
));
3528 g_hash_table_insert (domain_jit_info (domain
)->jump_target_hash
, patch_info
->data
.method
, jlist
);
3530 jlist
->list
= g_slist_prepend (jlist
->list
, ip
);
3531 mono_domain_unlock (domain
);
3542 collect_pred_seq_points (MonoBasicBlock
*bb
, MonoInst
*ins
, GSList
**next
, int depth
)
3545 MonoBasicBlock
*in_bb
;
3548 for (i
= 0; i
< bb
->in_count
; ++i
) {
3549 in_bb
= bb
->in_bb
[i
];
3551 if (in_bb
->last_seq_point
) {
3552 int src_index
= in_bb
->last_seq_point
->backend
.size
;
3553 int dst_index
= ins
->backend
.size
;
3555 /* bb->in_bb might contain duplicates */
3556 for (l
= next
[src_index
]; l
; l
= l
->next
)
3557 if (GPOINTER_TO_UINT (l
->data
) == dst_index
)
3560 next
[src_index
] = g_slist_append (next
[src_index
], GUINT_TO_POINTER (dst_index
));
3562 /* Have to look at its predecessors */
3564 collect_pred_seq_points (in_bb
, ins
, next
, depth
+ 1);
3570 mono_save_seq_point_info (MonoCompile
*cfg
)
3573 GSList
*bb_seq_points
, *l
;
3575 MonoDomain
*domain
= cfg
->domain
;
3577 MonoSeqPointInfo
*info
;
3580 if (!cfg
->seq_points
)
3583 info
= g_malloc0 (sizeof (MonoSeqPointInfo
) + (cfg
->seq_points
->len
- MONO_ZERO_LEN_ARRAY
) * sizeof (SeqPoint
));
3584 info
->len
= cfg
->seq_points
->len
;
3585 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3586 SeqPoint
*sp
= &info
->seq_points
[i
];
3587 MonoInst
*ins
= g_ptr_array_index (cfg
->seq_points
, i
);
3589 sp
->il_offset
= ins
->inst_imm
;
3590 sp
->native_offset
= ins
->inst_offset
;
3593 ins
->backend
.size
= i
;
3597 * For each sequence point, compute the list of sequence points immediately
3598 * following it, this is needed to implement 'step over' in the debugger agent.
3600 next
= g_new0 (GSList
*, cfg
->seq_points
->len
);
3601 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3602 bb_seq_points
= g_slist_reverse (bb
->seq_points
);
3604 for (l
= bb_seq_points
; l
; l
= l
->next
) {
3605 MonoInst
*ins
= l
->data
;
3607 if (ins
->inst_imm
== METHOD_ENTRY_IL_OFFSET
|| ins
->inst_imm
== METHOD_EXIT_IL_OFFSET
)
3608 /* Used to implement method entry/exit events */
3612 /* Link with the previous seq point in the same bb */
3613 next
[last
->backend
.size
] = g_slist_append (next
[last
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3615 /* Link with the last bb in the previous bblocks */
3616 collect_pred_seq_points (bb
, ins
, next
, 0);
3622 if (bb
->last_ins
&& bb
->last_ins
->opcode
== OP_ENDFINALLY
) {
3623 MonoBasicBlock
*bb2
;
3624 MonoInst
*endfinally_seq_point
= NULL
;
3627 * The ENDFINALLY branches are not represented in the cfg, so link it with all seq points starting bbs.
3629 l
= g_slist_last (bb
->seq_points
);
3631 endfinally_seq_point
= l
->data
;
3633 for (bb2
= cfg
->bb_entry
; bb2
; bb2
= bb2
->next_bb
) {
3634 GSList
*l
= g_slist_last (bb2
->seq_points
);
3637 MonoInst
*ins
= l
->data
;
3639 if (!(ins
->inst_imm
== METHOD_ENTRY_IL_OFFSET
|| ins
->inst_imm
== METHOD_EXIT_IL_OFFSET
) && ins
!= endfinally_seq_point
)
3640 next
[endfinally_seq_point
->backend
.size
] = g_slist_append (next
[endfinally_seq_point
->backend
.size
], GUINT_TO_POINTER (ins
->backend
.size
));
3646 if (cfg
->verbose_level
> 2) {
3647 printf ("\nSEQ POINT MAP: \n");
3650 for (i
= 0; i
< cfg
->seq_points
->len
; ++i
) {
3651 SeqPoint
*sp
= &info
->seq_points
[i
];
3655 sp
->next_len
= g_slist_length (next
[i
]);
3656 sp
->next
= g_new (int, sp
->next_len
);
3658 if (cfg
->verbose_level
> 2 && next
[i
]) {
3659 printf ("\tIL0x%x ->", sp
->il_offset
);
3660 for (l
= next
[i
]; l
; l
= l
->next
) {
3661 next_index
= GPOINTER_TO_UINT (l
->data
);
3662 printf (" IL0x%x", info
->seq_points
[next_index
].il_offset
);
3666 for (l
= next
[i
]; l
; l
= l
->next
) {
3667 next_index
= GPOINTER_TO_UINT (l
->data
);
3668 sp
->next
[j
++] = next_index
;
3670 g_slist_free (next
[i
]);
3674 cfg
->seq_point_info
= info
;
3676 // FIXME: dynamic methods
3677 if (!cfg
->compile_aot
) {
3678 mono_domain_lock (domain
);
3679 // FIXME: How can the lookup succeed ?
3680 if (!g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
))
3681 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, cfg
->method_to_register
, info
);
3682 mono_domain_unlock (domain
);
3685 g_ptr_array_free (cfg
->seq_points
, TRUE
);
3686 cfg
->seq_points
= NULL
;
3690 mono_codegen (MonoCompile
*cfg
)
3693 int max_epilog_size
;
3695 MonoDomain
*code_domain
;
3697 if (mono_using_xdebug
)
3699 * Recent gdb versions have trouble processing symbol files containing
3700 * overlapping address ranges, so allocate all code from the code manager
3701 * of the root domain. (#666152).
3703 code_domain
= mono_get_root_domain ();
3705 code_domain
= cfg
->domain
;
3707 #if defined(__native_client_codegen__) && defined(__native_client__)
3710 /* This keeps patch targets from being transformed during
3711 * ordinary method compilation, for local branches and jumps.
3713 nacl_allow_target_modification (FALSE
);
3716 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3717 cfg
->spill_count
= 0;
3718 /* we reuse dfn here */
3719 /* bb->dfn = bb_count++; */
3721 mono_arch_lowering_pass (cfg
, bb
);
3723 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3724 mono_arch_peephole_pass_1 (cfg
, bb
);
3727 mono_local_regalloc (cfg
, bb
);
3729 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
3730 mono_arch_peephole_pass_2 (cfg
, bb
);
3733 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3734 cfg
->coverage_info
= mono_profiler_coverage_alloc (cfg
->method
, cfg
->num_bblocks
);
3736 code
= mono_arch_emit_prolog (cfg
);
3738 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
3739 code
= mono_arch_instrument_prolog (cfg
, mono_profiler_method_enter
, code
, FALSE
);
3741 cfg
->code_len
= code
- cfg
->native_code
;
3742 cfg
->prolog_end
= cfg
->code_len
;
3744 mono_debug_open_method (cfg
);
3746 /* emit code all basic blocks */
3747 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3748 bb
->native_offset
= cfg
->code_len
;
3749 bb
->real_native_offset
= cfg
->code_len
;
3750 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
3751 mono_arch_output_basic_block (cfg
, bb
);
3752 bb
->native_length
= cfg
->code_len
- bb
->native_offset
;
3754 if (bb
== cfg
->bb_exit
) {
3755 cfg
->epilog_begin
= cfg
->code_len
;
3757 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
) {
3758 code
= cfg
->native_code
+ cfg
->code_len
;
3759 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
3760 cfg
->code_len
= code
- cfg
->native_code
;
3761 g_assert (cfg
->code_len
< cfg
->code_size
);
3764 mono_arch_emit_epilog (cfg
);
3768 #ifdef __native_client_codegen__
3769 mono_nacl_fix_patches (cfg
->native_code
, cfg
->patch_info
);
3771 mono_arch_emit_exceptions (cfg
);
3773 max_epilog_size
= 0;
3775 /* we always allocate code in cfg->domain->code_mp to increase locality */
3776 cfg
->code_size
= cfg
->code_len
+ max_epilog_size
;
3777 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
3779 if (cfg
->method
->dynamic
) {
3780 guint unwindlen
= 0;
3781 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3782 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3784 /* Allocate the code into a separate memory pool so it can be freed */
3785 cfg
->dynamic_info
= g_new0 (MonoJitDynamicMethodInfo
, 1);
3786 cfg
->dynamic_info
->code_mp
= mono_code_manager_new_dynamic ();
3787 mono_domain_lock (cfg
->domain
);
3788 mono_dynamic_code_hash_insert (cfg
->domain
, cfg
->method
, cfg
->dynamic_info
);
3789 mono_domain_unlock (cfg
->domain
);
3791 if (mono_using_xdebug
)
3792 /* See the comment for cfg->code_domain */
3793 code
= mono_domain_code_reserve (code_domain
, cfg
->code_size
+ unwindlen
);
3795 code
= mono_code_manager_reserve (cfg
->dynamic_info
->code_mp
, cfg
->code_size
+ unwindlen
);
3797 guint unwindlen
= 0;
3798 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3799 unwindlen
= mono_arch_unwindinfo_get_size (cfg
->arch
.unwindinfo
);
3801 code
= mono_domain_code_reserve (code_domain
, cfg
->code_size
+ unwindlen
);
3803 #if defined(__native_client_codegen__) && defined(__native_client__)
3804 nacl_allow_target_modification (TRUE
);
3808 memcpy (code
, cfg
->native_code
, cfg
->code_len
);
3809 #if defined(__default_codegen__)
3810 g_free (cfg
->native_code
);
3811 #elif defined(__native_client_codegen__)
3812 if (cfg
->native_code_alloc
) {
3813 g_free (cfg
->native_code_alloc
);
3814 cfg
->native_code_alloc
= 0;
3816 else if (cfg
->native_code
) {
3817 g_free (cfg
->native_code
);
3819 #endif /* __native_client_codegen__ */
3820 cfg
->native_code
= code
;
3821 code
= cfg
->native_code
+ cfg
->code_len
;
3823 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
3824 mono_postprocess_patches (cfg
);
3826 #ifdef VALGRIND_JIT_REGISTER_MAP
3827 if (valgrind_register
){
3828 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3829 VALGRIND_JIT_REGISTER_MAP (nm
, cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
);
3834 if (cfg
->verbose_level
> 0) {
3835 char* nm
= mono_method_full_name (cfg
->method
, TRUE
);
3836 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
3838 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
3843 gboolean is_generic
= FALSE
;
3845 if (cfg
->method
->is_inflated
|| mono_method_get_generic_container (cfg
->method
) ||
3846 cfg
->method
->klass
->generic_container
|| cfg
->method
->klass
->generic_class
) {
3850 if (cfg
->generic_sharing_context
)
3851 g_assert (is_generic
);
3854 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
3855 mono_arch_save_unwind_info (cfg
);
3858 #if defined(__native_client_codegen__) && defined(__native_client__)
3859 if (!cfg
->compile_aot
) {
3860 if (cfg
->method
->dynamic
) {
3861 code_dest
= nacl_code_manager_get_code_dest(cfg
->dynamic_info
->code_mp
, cfg
->native_code
);
3863 code_dest
= nacl_domain_get_code_dest(cfg
->domain
, cfg
->native_code
);
3868 #if defined(__native_client_codegen__)
3869 mono_nacl_fix_patches (cfg
->native_code
, cfg
->patch_info
);
3872 mono_arch_patch_code (cfg
->method
, cfg
->domain
, cfg
->native_code
, cfg
->patch_info
, cfg
->dynamic_info
? cfg
->dynamic_info
->code_mp
: NULL
, cfg
->run_cctors
);
3874 if (cfg
->method
->dynamic
) {
3875 if (mono_using_xdebug
)
3876 mono_domain_code_commit (code_domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3878 mono_code_manager_commit (cfg
->dynamic_info
->code_mp
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3880 mono_domain_code_commit (code_domain
, cfg
->native_code
, cfg
->code_size
, cfg
->code_len
);
3882 #if defined(__native_client_codegen__) && defined(__native_client__)
3883 cfg
->native_code
= code_dest
;
3885 mono_profiler_code_buffer_new (cfg
->native_code
, cfg
->code_len
, MONO_PROFILER_CODE_BUFFER_METHOD
, cfg
->method
);
3887 mono_arch_flush_icache (cfg
->native_code
, cfg
->code_len
);
3889 mono_debug_close_method (cfg
);
3891 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
3892 mono_arch_unwindinfo_install_unwind_info (&cfg
->arch
.unwindinfo
, cfg
->native_code
, cfg
->code_len
);
3897 compute_reachable (MonoBasicBlock
*bb
)
3901 if (!(bb
->flags
& BB_VISITED
)) {
3902 bb
->flags
|= BB_VISITED
;
3903 for (i
= 0; i
< bb
->out_count
; ++i
)
3904 compute_reachable (bb
->out_bb
[i
]);
3909 mono_handle_out_of_line_bblock (MonoCompile
*cfg
)
3912 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3913 if (bb
->next_bb
&& bb
->next_bb
->out_of_line
&& bb
->last_ins
&& !MONO_IS_BRANCH_OP (bb
->last_ins
)) {
3915 MONO_INST_NEW (cfg
, ins
, OP_BR
);
3916 MONO_ADD_INS (bb
, ins
);
3917 ins
->inst_target_bb
= bb
->next_bb
;
3922 #endif /* #ifndef DISABLE_JIT */
3925 create_jit_info_for_trampoline (MonoMethod
*wrapper
, MonoTrampInfo
*info
)
3927 MonoDomain
*domain
= mono_get_root_domain ();
3932 uw_info
= mono_unwind_ops_encode (info
->unwind_ops
, &info_len
);
3934 jinfo
= mono_domain_alloc0 (domain
, MONO_SIZEOF_JIT_INFO
);
3935 jinfo
->method
= wrapper
;
3936 jinfo
->code_start
= info
->code
;
3937 jinfo
->code_size
= info
->code_size
;
3938 jinfo
->used_regs
= mono_cache_unwind_info (uw_info
, info_len
);
3946 create_jit_info (MonoCompile
*cfg
, MonoMethod
*method_to_compile
)
3949 MonoMethodHeader
*header
;
3952 int generic_info_size
, arch_eh_info_size
= 0;
3953 int holes_size
= 0, num_holes
= 0, cas_size
= 0;
3954 guint32 stack_size
= 0;
3956 g_assert (method_to_compile
== cfg
->method
);
3957 header
= cfg
->header
;
3959 if (cfg
->generic_sharing_context
)
3960 generic_info_size
= sizeof (MonoGenericJitInfo
);
3962 generic_info_size
= 0;
3964 if (cfg
->arch_eh_jit_info
) {
3965 MonoJitArgumentInfo
*arg_info
;
3966 MonoMethodSignature
*sig
= mono_method_signature (cfg
->method_to_register
);
3969 * This cannot be computed during stack walking, as
3970 * mono_arch_get_argument_info () is not signal safe.
3972 arg_info
= g_newa (MonoJitArgumentInfo
, sig
->param_count
+ 1);
3973 stack_size
= mono_arch_get_argument_info (cfg
->generic_sharing_context
, sig
, sig
->param_count
, arg_info
);
3976 arch_eh_info_size
= sizeof (MonoArchEHJitInfo
);
3979 if (cfg
->try_block_holes
) {
3980 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
3981 TryBlockHole
*hole
= tmp
->data
;
3982 MonoExceptionClause
*ec
= hole
->clause
;
3983 int hole_end
= hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
;
3984 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
3985 g_assert (clause_last_bb
);
3987 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
3988 if (clause_last_bb
->native_offset
!= hole_end
)
3992 holes_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3993 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
3994 printf ("Number of try block holes %d\n", num_holes
);
3997 if (mono_method_has_declsec (cfg
->method_to_register
)) {
3998 cas_size
= sizeof (MonoMethodCasInfo
);
4001 if (COMPILE_LLVM (cfg
))
4002 num_clauses
= cfg
->llvm_ex_info_len
;
4004 num_clauses
= header
->num_clauses
;
4006 if (cfg
->method
->dynamic
) {
4007 jinfo
= g_malloc0 (MONO_SIZEOF_JIT_INFO
+ (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
4008 generic_info_size
+ holes_size
+ arch_eh_info_size
+ cas_size
);
4010 jinfo
= mono_domain_alloc0 (cfg
->domain
, MONO_SIZEOF_JIT_INFO
+
4011 (num_clauses
* sizeof (MonoJitExceptionInfo
)) +
4012 generic_info_size
+ holes_size
+ arch_eh_info_size
+ cas_size
);
4015 jinfo
->method
= cfg
->method_to_register
;
4016 jinfo
->code_start
= cfg
->native_code
;
4017 jinfo
->code_size
= cfg
->code_len
;
4018 jinfo
->used_regs
= cfg
->used_int_regs
;
4019 jinfo
->domain_neutral
= (cfg
->opt
& MONO_OPT_SHARED
) != 0;
4020 jinfo
->num_clauses
= num_clauses
;
4022 if (COMPILE_LLVM (cfg
))
4023 jinfo
->from_llvm
= TRUE
;
4025 if (cfg
->generic_sharing_context
) {
4027 MonoGenericJitInfo
*gi
;
4028 GSList
*loclist
= NULL
;
4030 jinfo
->has_generic_jit_info
= 1;
4032 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
4035 if (cfg
->method
->dynamic
)
4036 gi
->generic_sharing_context
= g_new0 (MonoGenericSharingContext
, 1);
4038 gi
->generic_sharing_context
= mono_domain_alloc0 (cfg
->domain
, sizeof (MonoGenericSharingContext
));
4039 memcpy (gi
->generic_sharing_context
, cfg
->generic_sharing_context
, sizeof (MonoGenericSharingContext
));
4041 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
4042 mini_method_get_context (method_to_compile
)->method_inst
||
4043 method_to_compile
->klass
->valuetype
) {
4044 g_assert (cfg
->rgctx_var
);
4049 if ((method_to_compile
->flags
& METHOD_ATTRIBUTE_STATIC
) ||
4050 mini_method_get_context (method_to_compile
)->method_inst
||
4051 method_to_compile
->klass
->valuetype
) {
4052 inst
= cfg
->rgctx_var
;
4053 if (!COMPILE_LLVM (cfg
))
4054 g_assert (inst
->opcode
== OP_REGOFFSET
);
4055 loclist
= cfg
->rgctx_loclist
;
4057 inst
= cfg
->args
[0];
4058 loclist
= cfg
->this_loclist
;
4062 /* Needed to handle async exceptions */
4066 gi
->nlocs
= g_slist_length (loclist
);
4067 if (cfg
->method
->dynamic
)
4068 gi
->locations
= g_malloc0 (gi
->nlocs
* sizeof (MonoDwarfLocListEntry
));
4070 gi
->locations
= mono_domain_alloc0 (cfg
->domain
, gi
->nlocs
* sizeof (MonoDwarfLocListEntry
));
4072 for (l
= loclist
; l
; l
= l
->next
) {
4073 memcpy (&(gi
->locations
[i
]), l
->data
, sizeof (MonoDwarfLocListEntry
));
4078 if (COMPILE_LLVM (cfg
)) {
4079 g_assert (cfg
->llvm_this_reg
!= -1);
4080 gi
->this_in_reg
= 0;
4081 gi
->this_reg
= cfg
->llvm_this_reg
;
4082 gi
->this_offset
= cfg
->llvm_this_offset
;
4083 } else if (inst
->opcode
== OP_REGVAR
) {
4084 gi
->this_in_reg
= 1;
4085 gi
->this_reg
= inst
->dreg
;
4087 g_assert (inst
->opcode
== OP_REGOFFSET
);
4089 g_assert (inst
->inst_basereg
== X86_EBP
);
4090 #elif defined(TARGET_AMD64)
4091 g_assert (inst
->inst_basereg
== X86_EBP
|| inst
->inst_basereg
== X86_ESP
);
4093 g_assert (inst
->inst_offset
>= G_MININT32
&& inst
->inst_offset
<= G_MAXINT32
);
4095 gi
->this_in_reg
= 0;
4096 gi
->this_reg
= inst
->inst_basereg
;
4097 gi
->this_offset
= inst
->inst_offset
;
4102 MonoTryBlockHoleTableJitInfo
*table
;
4105 jinfo
->has_try_block_holes
= 1;
4106 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
4107 table
->num_holes
= (guint16
)num_holes
;
4109 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
4110 guint32 start_bb_offset
;
4111 MonoTryBlockHoleJitInfo
*hole
;
4112 TryBlockHole
*hole_data
= tmp
->data
;
4113 MonoExceptionClause
*ec
= hole_data
->clause
;
4114 int hole_end
= hole_data
->basic_block
->native_offset
+ hole_data
->basic_block
->native_length
;
4115 MonoBasicBlock
*clause_last_bb
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
4116 g_assert (clause_last_bb
);
4118 /* Holes at the end of a try region can be represented by simply reducing the size of the block itself.*/
4119 if (clause_last_bb
->native_offset
== hole_end
)
4122 start_bb_offset
= hole_data
->start_offset
- hole_data
->basic_block
->native_offset
;
4123 hole
= &table
->holes
[i
++];
4124 hole
->clause
= hole_data
->clause
- &header
->clauses
[0];
4125 hole
->offset
= (guint32
)hole_data
->start_offset
;
4126 hole
->length
= (guint16
)(hole_data
->basic_block
->native_length
- start_bb_offset
);
4128 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
4129 printf ("\tTry block hole at eh clause %d offset %x length %x\n", hole
->clause
, hole
->offset
, hole
->length
);
4131 g_assert (i
== num_holes
);
4134 if (arch_eh_info_size
) {
4135 MonoArchEHJitInfo
*info
;
4137 jinfo
->has_arch_eh_info
= 1;
4138 info
= mono_jit_info_get_arch_eh_info (jinfo
);
4140 info
->stack_size
= stack_size
;
4144 jinfo
->has_cas_info
= 1;
4147 if (COMPILE_LLVM (cfg
)) {
4149 memcpy (&jinfo
->clauses
[0], &cfg
->llvm_ex_info
[0], num_clauses
* sizeof (MonoJitExceptionInfo
));
4150 } else if (header
->num_clauses
) {
4153 for (i
= 0; i
< header
->num_clauses
; i
++) {
4154 MonoExceptionClause
*ec
= &header
->clauses
[i
];
4155 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
4156 MonoBasicBlock
*tblock
;
4159 ei
->flags
= ec
->flags
;
4161 exvar
= mono_find_exvar_for_offset (cfg
, ec
->handler_offset
);
4162 ei
->exvar_offset
= exvar
? exvar
->inst_offset
: 0;
4164 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4165 tblock
= cfg
->cil_offset_to_bb
[ec
->data
.filter_offset
];
4167 ei
->data
.filter
= cfg
->native_code
+ tblock
->native_offset
;
4169 ei
->data
.catch_class
= ec
->data
.catch_class
;
4172 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
];
4174 g_assert (tblock
->native_offset
);
4175 ei
->try_start
= cfg
->native_code
+ tblock
->native_offset
;
4176 if (tblock
->extend_try_block
) {
4178 * Extend the try block backwards to include parts of the previous call
4181 ei
->try_start
= (guint8
*)ei
->try_start
- MONO_ARCH_MONITOR_ENTER_ADJUSTMENT
;
4183 tblock
= cfg
->cil_offset_to_bb
[ec
->try_offset
+ ec
->try_len
];
4185 if (!tblock
->native_offset
) {
4187 for (j
= ec
->try_offset
+ ec
->try_len
, end
= ec
->try_offset
; j
>= end
; --j
) {
4188 MonoBasicBlock
*bb
= cfg
->cil_offset_to_bb
[j
];
4189 if (bb
&& bb
->native_offset
) {
4195 ei
->try_end
= cfg
->native_code
+ tblock
->native_offset
;
4196 g_assert (tblock
->native_offset
);
4197 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
];
4199 ei
->handler_start
= cfg
->native_code
+ tblock
->native_offset
;
4201 for (tmp
= cfg
->try_block_holes
; tmp
; tmp
= tmp
->next
) {
4202 TryBlockHole
*hole
= tmp
->data
;
4203 gpointer hole_end
= cfg
->native_code
+ (hole
->basic_block
->native_offset
+ hole
->basic_block
->native_length
);
4204 if (hole
->clause
== ec
&& hole_end
== ei
->try_end
) {
4205 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
4206 printf ("\tShortening try block %d from %x to %x\n", i
, (int)((guint8
*)ei
->try_end
- cfg
->native_code
), hole
->start_offset
);
4208 ei
->try_end
= cfg
->native_code
+ hole
->start_offset
;
4213 if (ec
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
) {
4215 if (ec
->handler_offset
+ ec
->handler_len
< header
->code_size
) {
4216 tblock
= cfg
->cil_offset_to_bb
[ec
->handler_offset
+ ec
->handler_len
];
4218 end_offset
= tblock
->native_offset
;
4220 end_offset
= cfg
->epilog_begin
;
4222 ei
->data
.handler_end
= cfg
->native_code
+ end_offset
;
4227 if (G_UNLIKELY (cfg
->verbose_level
>= 4)) {
4229 for (i
= 0; i
< jinfo
->num_clauses
; i
++) {
4230 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
4231 int start
= (guint8
*)ei
->try_start
- cfg
->native_code
;
4232 int end
= (guint8
*)ei
->try_end
- cfg
->native_code
;
4233 int handler
= (guint8
*)ei
->handler_start
- cfg
->native_code
;
4235 printf ("JitInfo EH clause %d flags %x try %x-%x handler %x\n", i
, ei
->flags
, start
, end
, handler
);
4240 * Its possible to generate dwarf unwind info for xdebug etc, but not actually
4241 * using it during runtime, hence the define.
4243 #ifdef MONO_ARCH_HAVE_XP_UNWIND
4244 if (cfg
->encoded_unwind_ops
) {
4245 jinfo
->used_regs
= mono_cache_unwind_info (cfg
->encoded_unwind_ops
, cfg
->encoded_unwind_ops_len
);
4246 g_free (cfg
->encoded_unwind_ops
);
4247 } else if (cfg
->unwind_ops
) {
4249 guint8
*unwind_info
= mono_unwind_ops_encode (cfg
->unwind_ops
, &info_len
);
4251 jinfo
->used_regs
= mono_cache_unwind_info (unwind_info
, info_len
);
4252 g_free (unwind_info
);
4261 get_gsharedvt_type (MonoType
*t
)
4263 MonoGenericParam
*par
= t
->data
.generic_param
;
4267 * Create an anonymous gparam with a different serial so normal gshared and gsharedvt methods have
4268 * a different instantiation.
4270 g_assert (mono_generic_param_info (par
));
4271 par
= g_memdup (par
, sizeof (MonoGenericParamFull
));
4274 par
->image
= mono_defaults
.corlib
;
4276 res
= mono_metadata_type_dup (NULL
, t
);
4277 res
->data
.generic_param
= par
;
4283 is_gsharedvt_type (MonoType
*t
)
4285 return (t
->type
== MONO_TYPE_VAR
|| t
->type
== MONO_TYPE_MVAR
) && t
->data
.generic_param
->serial
== 1;
4288 /* Return whenever METHOD is a gsharedvt method */
4290 is_gsharedvt_method (MonoMethod
*method
)
4292 MonoGenericContext
*context
;
4293 MonoGenericInst
*inst
;
4296 if (!method
->is_inflated
)
4298 context
= mono_method_get_context (method
);
4299 inst
= context
->class_inst
;
4301 for (i
= 0; i
< inst
->type_argc
; ++i
)
4302 if (is_gsharedvt_type (inst
->type_argv
[i
]))
4305 inst
= context
->method_inst
;
4307 for (i
= 0; i
< inst
->type_argc
; ++i
)
4308 if (is_gsharedvt_type (inst
->type_argv
[i
]))
4315 * mini_get_shared_method_full:
4317 * Return the method which is actually compiled/registered when doing generic sharing.
4318 * If ALL_VT is true, return the shared method belonging to an all-vtype instantiation.
4319 * If IS_GSHAREDVT is true, treat METHOD as a gsharedvt method even if it fails some constraints.
4322 mini_get_shared_method_full (MonoMethod
*method
, gboolean all_vt
, gboolean is_gsharedvt
)
4324 MonoGenericContext shared_context
;
4325 MonoMethod
*declaring_method
, *res
;
4327 gboolean partial
= FALSE
;
4328 gboolean gsharedvt
= FALSE
;
4330 if (method
->is_generic
|| method
->klass
->generic_container
)
4331 declaring_method
= method
;
4333 declaring_method
= mono_method_get_declaring_generic_method (method
);
4335 if (declaring_method
->is_generic
)
4336 shared_context
= mono_method_get_generic_container (declaring_method
)->context
;
4338 shared_context
= declaring_method
->klass
->generic_container
->context
;
4340 /* Handle gsharedvt/partial sharing */
4341 if ((method
!= declaring_method
&& method
->is_inflated
&& !mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
, TRUE
)) ||
4342 is_gsharedvt
|| mini_is_gsharedvt_sharable_method (method
)) {
4343 MonoGenericContext
*context
= mono_method_get_context (method
);
4344 MonoGenericInst
*inst
;
4345 MonoType
**type_argv
;
4347 gsharedvt
= is_gsharedvt
|| mini_is_gsharedvt_sharable_method (method
);
4350 * Create the shared context by replacing the ref type arguments with
4351 * type parameters, and keeping the rest.
4354 inst
= context
->class_inst
;
4356 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
4357 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4358 if (!all_vt
&& (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
))
4359 type_argv
[i
] = shared_context
.class_inst
->type_argv
[i
];
4361 type_argv
[i
] = get_gsharedvt_type (shared_context
.class_inst
->type_argv
[i
]);
4363 type_argv
[i
] = inst
->type_argv
[i
];
4366 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
4370 inst
= context
->method_inst
;
4372 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
4373 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4374 if (!all_vt
&& (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
))
4375 type_argv
[i
] = shared_context
.method_inst
->type_argv
[i
];
4377 type_argv
[i
] = get_gsharedvt_type (shared_context
.method_inst
->type_argv
[i
]);
4379 type_argv
[i
] = inst
->type_argv
[i
];
4382 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
4387 res
= mono_class_inflate_generic_method (declaring_method
, &shared_context
);
4389 /* The result should be an inflated method whose parent is not inflated */
4390 g_assert (!res
->klass
->is_inflated
);
4396 mini_get_shared_method (MonoMethod
*method
)
4398 return mini_get_shared_method_full (method
, FALSE
, FALSE
);
4402 mini_init_gsctx_full (MonoGenericContext
*context
, MonoGenericSharingContext
*gsctx
, gboolean all_vt
)
4404 MonoGenericInst
*inst
;
4407 memset (gsctx
, 0, sizeof (MonoGenericSharingContext
));
4409 if (context
->class_inst
) {
4410 inst
= context
->class_inst
;
4411 gsctx
->var_is_vt
= g_new0 (gboolean
, inst
->type_argc
);
4413 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4414 MonoType
*type
= inst
->type_argv
[i
];
4416 if (!all_vt
&& (MONO_TYPE_IS_REFERENCE (type
) || (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
))) {
4418 gsctx
->var_is_vt
[i
] = TRUE
;
4422 if (context
->method_inst
) {
4423 inst
= context
->method_inst
;
4424 gsctx
->mvar_is_vt
= g_new0 (gboolean
, inst
->type_argc
);
4426 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4427 MonoType
*type
= inst
->type_argv
[i
];
4429 if (!all_vt
&& (MONO_TYPE_IS_REFERENCE (type
) || (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
))) {
4431 gsctx
->mvar_is_vt
[i
] = TRUE
;
4438 mini_init_gsctx (MonoGenericContext
*context
, MonoGenericSharingContext
*gsctx
)
4440 mini_init_gsctx_full (context
, gsctx
, FALSE
);
4445 * mini_method_compile:
4446 * @method: the method to compile
4447 * @opts: the optimization flags to use
4448 * @domain: the domain where the method will be compiled in
4449 * @run_cctors: whether we should run type ctors if possible
4450 * @compile_aot: whether this is an AOT compilation
4451 * @parts: debug flag
4453 * Returns: a MonoCompile* pointer. Caller must check the exception_type
4454 * field in the returned struct to see if compilation succeded.
4457 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
4459 MonoMethodHeader
*header
;
4460 MonoMethodSignature
*sig
;
4464 int dfn
, i
, code_size_ratio
;
4465 gboolean deadce_has_run
= FALSE
;
4466 gboolean try_generic_shared
, try_llvm
= FALSE
;
4467 MonoMethod
*method_to_compile
, *method_to_register
;
4468 gboolean method_is_gshared
= FALSE
;
4470 InterlockedIncrement (&mono_jit_stats
.methods_compiled
);
4471 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION
)
4472 mono_profiler_method_jit (method
);
4473 if (MONO_METHOD_COMPILE_BEGIN_ENABLED ())
4474 MONO_PROBE_METHOD_COMPILE_BEGIN (method
);
4478 * We might get passed the original generic method definition or
4479 * instances with type parameters.
4480 * FIXME: Remove the method->klass->generic_class limitation.
4482 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
4483 (opts
& MONO_OPT_GSHARED
) && ((method
->is_generic
|| method
->klass
->generic_container
) || (!method
->klass
->generic_class
&& mono_method_is_generic_sharable_impl_full (method
, TRUE
, FALSE
, FALSE
)));
4485 try_generic_shared
= mono_class_generic_sharing_enabled (method
->klass
) &&
4486 (opts
& MONO_OPT_GSHARED
) && mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
, TRUE
);
4488 if (opts
& MONO_OPT_GSHARED
) {
4489 if (try_generic_shared
)
4490 mono_stats
.generics_sharable_methods
++;
4491 else if (mono_method_is_generic_impl (method
))
4492 mono_stats
.generics_unsharable_methods
++;
4495 if (mini_is_gsharedvt_sharable_method (method
)) {
4496 if (!mono_debug_count ())
4497 try_generic_shared
= FALSE
;
4499 try_generic_shared
= FALSE
;
4502 if (is_gsharedvt_method (method
)) {
4503 /* We are AOTing a gshared method directly */
4504 method_is_gshared
= TRUE
;
4505 g_assert (compile_aot
);
4506 try_generic_shared
= TRUE
;
4510 try_llvm
= mono_use_llvm
;
4514 if (method_is_gshared
) {
4515 method_to_compile
= method
;
4517 if (try_generic_shared
) {
4518 method_to_compile
= mini_get_shared_method (method
);
4519 g_assert (method_to_compile
);
4521 method_to_compile
= method
;
4525 cfg
= g_new0 (MonoCompile
, 1);
4526 cfg
->method
= method_to_compile
;
4527 cfg
->header
= mono_method_get_header (cfg
->method
);
4528 cfg
->mempool
= mono_mempool_new ();
4530 cfg
->prof_options
= mono_profiler_get_events ();
4531 cfg
->run_cctors
= run_cctors
;
4532 cfg
->domain
= domain
;
4533 cfg
->verbose_level
= mini_verbose
;
4534 cfg
->compile_aot
= compile_aot
;
4535 cfg
->skip_visibility
= method
->skip_visibility
;
4536 cfg
->orig_method
= method
;
4537 cfg
->gen_seq_points
= debug_options
.gen_seq_points
;
4538 cfg
->explicit_null_checks
= debug_options
.explicit_null_checks
;
4539 cfg
->soft_breakpoints
= debug_options
.soft_breakpoints
;
4540 if (try_generic_shared
)
4541 cfg
->generic_sharing_context
= (MonoGenericSharingContext
*)&cfg
->gsctx
;
4542 cfg
->compile_llvm
= try_llvm
;
4543 cfg
->token_info_hash
= g_hash_table_new (NULL
, NULL
);
4545 if (cfg
->gen_seq_points
)
4546 cfg
->seq_points
= g_ptr_array_new ();
4548 if (cfg
->compile_aot
&& !try_generic_shared
&& (method
->is_generic
|| method
->klass
->generic_container
|| method_is_gshared
)) {
4549 cfg
->exception_type
= MONO_EXCEPTION_GENERIC_SHARING_FAILED
;
4553 if (cfg
->generic_sharing_context
&& (mini_is_gsharedvt_sharable_method (method
) || method_is_gshared
)) {
4554 MonoMethodInflated
*inflated
;
4555 MonoGenericContext
*context
;
4557 g_assert (method
->is_inflated
);
4558 inflated
= (MonoMethodInflated
*)method
;
4559 context
= &inflated
->context
;
4561 // FIXME: Free the contents of gsctx if compilation fails
4562 if (method_is_gshared
) {
4563 /* We are compiling a gsharedvt method directly */
4564 g_assert (compile_aot
);
4565 mini_init_gsctx_full (context
, &cfg
->gsctx
, TRUE
);
4567 mini_init_gsctx (context
, &cfg
->gsctx
);
4570 cfg
->gsharedvt
= TRUE
;
4572 cfg
->disable_llvm
= TRUE
;
4575 if (cfg
->generic_sharing_context
) {
4576 method_to_register
= method_to_compile
;
4578 g_assert (method
== method_to_compile
);
4579 method_to_register
= method
;
4581 cfg
->method_to_register
= method_to_register
;
4583 mono_error_init (&err
);
4584 sig
= mono_method_signature_checked (cfg
->method
, &err
);
4586 cfg
->exception_type
= MONO_EXCEPTION_TYPE_LOAD
;
4587 cfg
->exception_message
= g_strdup (mono_error_get_message (&err
));
4588 mono_error_cleanup (&err
);
4589 if (MONO_METHOD_COMPILE_END_ENABLED ())
4590 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4594 header
= cfg
->header
;
4596 MonoLoaderError
*error
;
4598 if ((error
= mono_loader_get_last_error ())) {
4599 cfg
->exception_type
= error
->exception_type
;
4601 cfg
->exception_type
= MONO_EXCEPTION_INVALID_PROGRAM
;
4602 cfg
->exception_message
= g_strdup_printf ("Missing or incorrect header for method %s", cfg
->method
->name
);
4604 if (MONO_METHOD_COMPILE_END_ENABLED ())
4605 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4611 static gboolean inited
;
4618 * Check for methods which cannot be compiled by LLVM early, to avoid
4619 * the extra compilation pass.
4621 if (COMPILE_LLVM (cfg
)) {
4622 mono_llvm_check_method_supported (cfg
);
4623 if (cfg
->disable_llvm
) {
4624 if (cfg
->verbose_level
>= 1) {
4625 //nm = mono_method_full_name (cfg->method, TRUE);
4626 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
4629 mono_destroy_compile (cfg
);
4631 goto restart_compile
;
4637 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
4638 if (mono_debug_using_mono_debugger () || debug_options
.mdb_optimizations
) {
4639 cfg
->disable_reuse_registers
= TRUE
;
4640 cfg
->disable_reuse_stack_slots
= TRUE
;
4642 * This decreases the change the debugger will read registers/stack slots which are
4643 * not yet initialized.
4645 cfg
->disable_initlocals_opt
= TRUE
;
4647 cfg
->extend_live_ranges
= TRUE
;
4649 /* Temporarily disable this when running in the debugger until we have support
4650 * for this in the debugger. */
4651 /* This is no longer needed with sdb */
4652 //cfg->disable_omit_fp = TRUE;
4654 /* The debugger needs all locals to be on the stack or in a global register */
4655 cfg
->disable_vreg_to_lvreg
= TRUE
;
4657 /* Don't remove unused variables when running inside the debugger since the user
4658 * may still want to view them. */
4659 cfg
->disable_deadce_vars
= TRUE
;
4661 // cfg->opt |= MONO_OPT_SHARED;
4662 cfg
->opt
&= ~MONO_OPT_DEADCE
;
4663 cfg
->opt
&= ~MONO_OPT_INLINE
;
4664 cfg
->opt
&= ~MONO_OPT_COPYPROP
;
4665 cfg
->opt
&= ~MONO_OPT_CONSPROP
;
4666 /* This is no longer needed with sdb */
4667 //cfg->opt &= ~MONO_OPT_GSHARED;
4669 /* This is needed for the soft debugger, which doesn't like code after the epilog */
4670 cfg
->disable_out_of_line_bblocks
= TRUE
;
4673 if (mono_using_xdebug
) {
4675 * Make each variable use its own register/stack slot and extend
4676 * their liveness to cover the whole method, making them displayable
4677 * in gdb even after they are dead.
4679 cfg
->disable_reuse_registers
= TRUE
;
4680 cfg
->disable_reuse_stack_slots
= TRUE
;
4681 cfg
->extend_live_ranges
= TRUE
;
4682 cfg
->compute_precise_live_ranges
= TRUE
;
4685 mini_gc_init_cfg (cfg
);
4687 if (COMPILE_LLVM (cfg
)) {
4688 cfg
->opt
|= MONO_OPT_ABCREM
;
4691 if (getenv ("MONO_VERBOSE_METHOD")) {
4692 char *name
= getenv ("MONO_VERBOSE_METHOD");
4694 if ((strchr (name
, '.') > name
) || strchr (name
, ':')) {
4695 MonoMethodDesc
*desc
;
4697 desc
= mono_method_desc_new (name
, TRUE
);
4698 if (mono_method_desc_full_match (desc
, cfg
->method
)) {
4699 cfg
->verbose_level
= 4;
4701 mono_method_desc_free (desc
);
4703 if (strcmp (cfg
->method
->name
, getenv ("MONO_VERBOSE_METHOD")) == 0)
4704 cfg
->verbose_level
= 4;
4708 ip
= (guint8
*)header
->code
;
4710 cfg
->intvars
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (guint16
) * STACK_MAX
* header
->max_stack
);
4712 if (cfg
->verbose_level
> 0) {
4715 method_name
= mono_method_full_name (method
, TRUE
);
4716 g_print ("converting %s%s%smethod %s\n", COMPILE_LLVM (cfg
) ? "llvm " : "", cfg
->gsharedvt
? "gsharedvt " : "", (cfg
->generic_sharing_context
&& !cfg
->gsharedvt
) ? "gshared " : "", method_name
);
4718 if (COMPILE_LLVM (cfg))
4719 g_print ("converting llvm method %s\n", method_name = mono_method_full_name (method, TRUE));
4720 else if (cfg->gsharedvt)
4721 g_print ("converting gsharedvt method %s\n", method_name = mono_method_full_name (method_to_compile, TRUE));
4722 else if (cfg->generic_sharing_context)
4723 g_print ("converting shared method %s\n", method_name = mono_method_full_name (method_to_compile, TRUE));
4725 g_print ("converting method %s\n", method_name = mono_method_full_name (method, TRUE));
4727 g_free (method_name
);
4730 if (cfg
->opt
& (MONO_OPT_ABCREM
| MONO_OPT_SSAPRE
))
4731 cfg
->opt
|= MONO_OPT_SSA
;
4734 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")))
4735 cfg->globalra = TRUE;
4738 //cfg->globalra = TRUE;
4740 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
4741 // cfg->globalra = TRUE;
4744 static int count
= 0;
4748 if (getenv ("COUNT2")) {
4749 cfg->globalra = TRUE;
4750 if (count == atoi (getenv ("COUNT2")))
4751 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
4752 if (count > atoi (getenv ("COUNT2")))
4753 cfg->globalra = FALSE;
4758 if (header
->clauses
)
4759 cfg
->globalra
= FALSE
;
4761 if (cfg
->method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)
4762 /* The code in the prolog clobbers caller saved registers */
4763 cfg
->globalra
= FALSE
;
4765 // FIXME: Disable globalra in case of tracing/profiling
4767 if (cfg
->method
->save_lmf
)
4768 /* The LMF saving code might clobber caller saved registers */
4769 cfg
->globalra
= FALSE
;
4771 if (header
->code_size
> 5000)
4773 /* Too large bblocks could overflow the ins positions */
4774 cfg
->globalra
= FALSE
;
4776 cfg
->rs
= mono_regstate_new ();
4778 cfg
->rs
->next_vreg
= MONO_MAX_IREGS
+ MONO_MAX_FREGS
;
4779 cfg
->next_vreg
= cfg
->rs
->next_vreg
;
4781 /* FIXME: Fix SSA to handle branches inside bblocks */
4782 if (cfg
->opt
& MONO_OPT_SSA
)
4783 cfg
->enable_extended_bblocks
= FALSE
;
4786 * FIXME: This confuses liveness analysis because variables which are assigned after
4787 * a branch inside a bblock become part of the kill set, even though the assignment
4788 * might not get executed. This causes the optimize_initlocals pass to delete some
4789 * assignments which are needed.
4790 * Also, the mono_if_conversion pass needs to be modified to recognize the code
4793 //cfg->enable_extended_bblocks = TRUE;
4795 /*We must verify the method before doing any IR generation as mono_compile_create_vars can assert.*/
4796 if (mono_compile_is_broken (cfg
, cfg
->method
, TRUE
)) {
4797 if (mini_get_debug_options ()->break_on_unverified
)
4803 * create MonoInst* which represents arguments and local variables
4805 mono_compile_create_vars (cfg
);
4807 /* SSAPRE is not supported on linear IR */
4808 cfg
->opt
&= ~MONO_OPT_SSAPRE
;
4810 i
= mono_method_to_ir (cfg
, method_to_compile
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
);
4813 if (try_generic_shared
&& cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
4815 if (MONO_METHOD_COMPILE_END_ENABLED ())
4816 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4819 mono_destroy_compile (cfg
);
4820 try_generic_shared
= FALSE
;
4821 goto restart_compile
;
4823 g_assert (cfg
->exception_type
!= MONO_EXCEPTION_GENERIC_SHARING_FAILED
);
4825 if (MONO_METHOD_COMPILE_END_ENABLED ())
4826 MONO_PROBE_METHOD_COMPILE_END (method
, FALSE
);
4827 /* cfg contains the details of the failure, so let the caller cleanup */
4831 cfg
->stat_basic_blocks
+= cfg
->num_bblocks
;
4833 if (COMPILE_LLVM (cfg
)) {
4836 /* The IR has to be in SSA form for LLVM */
4837 cfg
->opt
|= MONO_OPT_SSA
;
4841 // Allow SSA on the result value
4842 cfg
->ret
->flags
&= ~MONO_INST_VOLATILE
;
4844 // Add an explicit return instruction referencing the return value
4845 MONO_INST_NEW (cfg
, ins
, OP_SETRET
);
4846 ins
->sreg1
= cfg
->ret
->dreg
;
4848 MONO_ADD_INS (cfg
->bb_exit
, ins
);
4851 cfg
->opt
&= ~MONO_OPT_LINEARS
;
4854 cfg
->opt
&= ~MONO_OPT_BRANCH
;
4857 /* todo: remove code when we have verified that the liveness for try/catch blocks
4861 * Currently, this can't be commented out since exception blocks are not
4862 * processed during liveness analysis.
4863 * It is also needed, because otherwise the local optimization passes would
4864 * delete assignments in cases like this:
4866 * <something which throws>
4868 * This also allows SSA to be run on methods containing exception clauses, since
4869 * SSA will ignore variables marked VOLATILE.
4871 mono_liveness_handle_exception_clauses (cfg
);
4873 mono_handle_out_of_line_bblock (cfg
);
4875 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
4877 if (!COMPILE_LLVM (cfg
))
4878 mono_decompose_long_opts (cfg
);
4880 /* Should be done before branch opts */
4881 if (cfg
->opt
& (MONO_OPT_CONSPROP
| MONO_OPT_COPYPROP
))
4882 mono_local_cprop (cfg
);
4884 if (cfg
->opt
& MONO_OPT_BRANCH
)
4885 mono_optimize_branches (cfg
);
4887 /* This must be done _before_ global reg alloc and _after_ decompose */
4888 mono_handle_global_vregs (cfg
);
4889 if (cfg
->opt
& MONO_OPT_DEADCE
)
4890 mono_local_deadce (cfg
);
4891 /* Disable this for LLVM to make the IR easier to handle */
4892 if (!COMPILE_LLVM (cfg
))
4893 mono_if_conversion (cfg
);
4895 if ((cfg
->opt
& MONO_OPT_SSAPRE
) || cfg
->globalra
)
4896 mono_remove_critical_edges (cfg
);
4898 /* Depth-first ordering on basic blocks */
4899 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
4901 cfg
->max_block_num
= cfg
->num_bblocks
;
4904 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
4905 if (cfg
->num_bblocks
!= dfn
+ 1) {
4908 cfg
->num_bblocks
= dfn
+ 1;
4910 /* remove unreachable code, because the code in them may be
4911 * inconsistent (access to dead variables for example) */
4912 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4913 bb
->flags
&= ~BB_VISITED
;
4914 compute_reachable (cfg
->bb_entry
);
4915 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4916 if (bb
->flags
& BB_EXCEPTION_HANDLER
)
4917 compute_reachable (bb
);
4918 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
4919 if (!(bb
->flags
& BB_VISITED
)) {
4920 if (cfg
->verbose_level
> 1)
4921 g_print ("found unreachable code in BB%d\n", bb
->block_num
);
4922 bb
->code
= bb
->last_ins
= NULL
;
4923 while (bb
->out_count
)
4924 mono_unlink_bblock (cfg
, bb
, bb
->out_bb
[0]);
4927 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
4928 bb
->flags
&= ~BB_VISITED
;
4931 if (((cfg
->num_varinfo
> 2000) || (cfg
->num_bblocks
> 1000)) && !cfg
->compile_aot
) {
4933 * we disable some optimizations if there are too many variables
4934 * because JIT time may become too expensive. The actual number needs
4935 * to be tweaked and eventually the non-linear algorithms should be fixed.
4937 cfg
->opt
&= ~ (MONO_OPT_LINEARS
| MONO_OPT_COPYPROP
| MONO_OPT_CONSPROP
);
4938 cfg
->disable_ssa
= TRUE
;
4941 if (cfg
->opt
& MONO_OPT_LOOP
) {
4942 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
);
4943 mono_compute_natural_loops (cfg
);
4946 /* after method_to_ir */
4948 if (MONO_METHOD_COMPILE_END_ENABLED ())
4949 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4954 if (header->num_clauses)
4955 cfg->disable_ssa = TRUE;
4958 //#define DEBUGSSA "logic_run"
4959 #define DEBUGSSA_CLASS "Tests"
4962 if (!cfg
->disable_ssa
) {
4963 mono_local_cprop (cfg
);
4966 mono_ssa_compute (cfg
);
4970 if (cfg
->opt
& MONO_OPT_SSA
) {
4971 if (!(cfg
->comp_done
& MONO_COMP_SSA
) && !cfg
->disable_ssa
) {
4973 mono_ssa_compute (cfg
);
4976 if (cfg
->verbose_level
>= 2) {
4983 /* after SSA translation */
4985 if (MONO_METHOD_COMPILE_END_ENABLED ())
4986 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
4990 if ((cfg
->opt
& MONO_OPT_CONSPROP
) || (cfg
->opt
& MONO_OPT_COPYPROP
)) {
4991 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
4993 mono_ssa_cprop (cfg
);
4999 if (cfg
->comp_done
& MONO_COMP_SSA
&& !COMPILE_LLVM (cfg
)) {
5000 //mono_ssa_strength_reduction (cfg);
5002 if (cfg
->opt
& MONO_OPT_SSAPRE
) {
5003 mono_perform_ssapre (cfg
);
5004 //mono_local_cprop (cfg);
5007 if (cfg
->opt
& MONO_OPT_DEADCE
) {
5008 mono_ssa_deadce (cfg
);
5009 deadce_has_run
= TRUE
;
5012 if ((cfg
->flags
& (MONO_CFG_HAS_LDELEMA
|MONO_CFG_HAS_CHECK_THIS
)) && (cfg
->opt
& MONO_OPT_ABCREM
))
5013 mono_perform_abc_removal (cfg
);
5015 mono_ssa_remove (cfg
);
5016 mono_local_cprop (cfg
);
5017 mono_handle_global_vregs (cfg
);
5018 if (cfg
->opt
& MONO_OPT_DEADCE
)
5019 mono_local_deadce (cfg
);
5021 if (cfg
->opt
& MONO_OPT_BRANCH
) {
5024 mono_optimize_branches (cfg
);
5026 /* Have to recompute cfg->bblocks and bb->dfn */
5027 if (cfg
->globalra
) {
5028 mono_remove_critical_edges (cfg
);
5030 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
5033 /* Depth-first ordering on basic blocks */
5034 cfg
->bblocks
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoBasicBlock
*) * (cfg
->num_bblocks
+ 1));
5037 df_visit (cfg
->bb_entry
, &dfn
, cfg
->bblocks
);
5038 cfg
->num_bblocks
= dfn
+ 1;
5044 if (cfg
->comp_done
& MONO_COMP_SSA
&& COMPILE_LLVM (cfg
)) {
5045 /* This removes MONO_INST_FAULT flags too so perform it unconditionally */
5046 if (cfg
->opt
& MONO_OPT_ABCREM
)
5047 mono_perform_abc_removal (cfg
);
5050 /* after SSA removal */
5052 if (MONO_METHOD_COMPILE_END_ENABLED ())
5053 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
5057 #ifdef MONO_ARCH_SOFT_FLOAT
5058 if (!COMPILE_LLVM (cfg
))
5059 mono_decompose_soft_float (cfg
);
5061 if (!COMPILE_LLVM (cfg
))
5062 mono_decompose_vtype_opts (cfg
);
5063 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
5064 mono_decompose_array_access_opts (cfg
);
5067 #ifndef MONO_ARCH_GOT_REG
5072 g_assert (cfg
->got_var_allocated
);
5075 * Allways allocate the GOT var to a register, because keeping it
5076 * in memory will increase the number of live temporaries in some
5077 * code created by inssel.brg, leading to the well known spills+
5078 * branches problem. Testcase: mcs crash in
5079 * System.MonoCustomAttrs:GetCustomAttributes.
5081 #ifdef MONO_ARCH_GOT_REG
5082 got_reg
= MONO_ARCH_GOT_REG
;
5084 regs
= mono_arch_get_global_int_regs (cfg
);
5086 got_reg
= GPOINTER_TO_INT (regs
->data
);
5089 cfg
->got_var
->opcode
= OP_REGVAR
;
5090 cfg
->got_var
->dreg
= got_reg
;
5091 cfg
->used_int_regs
|= 1LL << cfg
->got_var
->dreg
;
5095 * Have to call this again to process variables added since the first call.
5097 mono_liveness_handle_exception_clauses (cfg
);
5099 if (cfg
->globalra
) {
5102 /* Have to do this before regalloc since it can create vregs */
5103 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
5104 mono_arch_lowering_pass (cfg
, bb
);
5106 mono_global_regalloc (cfg
);
5109 if ((cfg
->opt
& MONO_OPT_LINEARS
) && !cfg
->globalra
) {
5110 GList
*vars
, *regs
, *l
;
5112 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
5113 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
5114 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
5115 mono_analyze_liveness (cfg
);
5117 if ((vars
= mono_arch_get_allocatable_int_vars (cfg
))) {
5118 regs
= mono_arch_get_global_int_regs (cfg
);
5119 /* Remove the reg reserved for holding the GOT address */
5121 for (l
= regs
; l
; l
= l
->next
) {
5122 if (GPOINTER_TO_UINT (l
->data
) == cfg
->got_var
->dreg
) {
5123 regs
= g_list_delete_link (regs
, l
);
5128 mono_linear_scan (cfg
, vars
, regs
, &cfg
->used_int_regs
);
5132 //mono_print_code (cfg, "");
5136 /* variables are allocated after decompose, since decompose could create temps */
5137 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
5138 mono_arch_allocate_vars (cfg
);
5139 if (cfg
->exception_type
)
5145 gboolean need_local_opts
;
5147 if (!cfg
->globalra
&& !COMPILE_LLVM (cfg
)) {
5148 mono_spill_global_vars (cfg
, &need_local_opts
);
5150 if (need_local_opts
|| cfg
->compile_aot
) {
5151 /* To optimize code created by spill_global_vars */
5152 mono_local_cprop (cfg
);
5153 if (cfg
->opt
& MONO_OPT_DEADCE
)
5154 mono_local_deadce (cfg
);
5158 /* Add branches between non-consecutive bblocks */
5159 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
5160 if (bb
->last_ins
&& MONO_IS_COND_BRANCH_OP (bb
->last_ins
) &&
5161 bb
->last_ins
->inst_false_bb
&& bb
->next_bb
!= bb
->last_ins
->inst_false_bb
) {
5162 /* we are careful when inverting, since bugs like #59580
5163 * could show up when dealing with NaNs.
5165 if (MONO_IS_COND_BRANCH_NOFP(bb
->last_ins
) && bb
->next_bb
== bb
->last_ins
->inst_true_bb
) {
5166 MonoBasicBlock
*tmp
= bb
->last_ins
->inst_true_bb
;
5167 bb
->last_ins
->inst_true_bb
= bb
->last_ins
->inst_false_bb
;
5168 bb
->last_ins
->inst_false_bb
= tmp
;
5170 bb
->last_ins
->opcode
= mono_reverse_branch_op (bb
->last_ins
->opcode
);
5172 MonoInst
*inst
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoInst
));
5173 inst
->opcode
= OP_BR
;
5174 inst
->inst_target_bb
= bb
->last_ins
->inst_false_bb
;
5175 mono_bblock_add_inst (bb
, inst
);
5180 if (cfg
->verbose_level
>= 4 && !cfg
->globalra
) {
5181 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
5182 MonoInst
*tree
= bb
->code
;
5183 g_print ("DUMP BLOCK %d:\n", bb
->block_num
);
5186 for (; tree
; tree
= tree
->next
) {
5187 mono_print_ins_index (-1, tree
);
5193 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
5194 bb
->max_vreg
= cfg
->next_vreg
;
5198 if (COMPILE_LLVM (cfg
)) {
5202 /* The IR has to be in SSA form for LLVM */
5203 if (!(cfg
->comp_done
& MONO_COMP_SSA
)) {
5204 cfg
->exception_message
= g_strdup ("SSA disabled.");
5205 cfg
->disable_llvm
= TRUE
;
5208 if (cfg
->flags
& MONO_CFG_HAS_ARRAY_ACCESS
)
5209 mono_decompose_array_access_opts (cfg
);
5211 if (!cfg
->disable_llvm
)
5212 mono_llvm_emit_method (cfg
);
5213 if (cfg
->disable_llvm
) {
5214 if (cfg
->verbose_level
>= 1) {
5215 //nm = mono_method_full_name (cfg->method, TRUE);
5216 printf ("LLVM failed for '%s': %s\n", method
->name
, cfg
->exception_message
);
5219 mono_destroy_compile (cfg
);
5221 goto restart_compile
;
5224 if (cfg
->verbose_level
> 0 && !cfg
->compile_aot
) {
5225 nm
= mono_method_full_name (cfg
->method
, TRUE
);
5226 g_print ("LLVM Method %s emitted at %p to %p (code length %d) [%s]\n",
5228 cfg
->native_code
, cfg
->native_code
+ cfg
->code_len
, cfg
->code_len
, cfg
->domain
->friendly_name
);
5236 if (COMPILE_LLVM (cfg
))
5237 InterlockedIncrement (&mono_jit_stats
.methods_with_llvm
);
5239 InterlockedIncrement (&mono_jit_stats
.methods_without_llvm
);
5241 cfg
->jit_info
= create_jit_info (cfg
, method_to_compile
);
5243 #ifdef MONO_ARCH_HAVE_LIVERANGE_OPS
5244 if (cfg
->extend_live_ranges
) {
5245 /* Extend live ranges to cover the whole method */
5246 for (i
= 0; i
< cfg
->num_varinfo
; ++i
)
5247 MONO_VARINFO (cfg
, i
)->live_range_end
= cfg
->code_len
;
5251 if (!cfg
->compile_aot
)
5252 mono_save_xdebug_info (cfg
);
5254 mini_gc_create_gc_map (cfg
);
5256 mono_save_seq_point_info (cfg
);
5258 if (cfg
->verbose_level
>= 2) {
5259 char *id
= mono_method_full_name (cfg
->method
, FALSE
);
5260 mono_disassemble_code (cfg
, cfg
->native_code
, cfg
->code_len
, id
+ 3);
5264 if (!cfg
->compile_aot
) {
5265 mono_domain_lock (cfg
->domain
);
5266 mono_jit_info_table_add (cfg
->domain
, cfg
->jit_info
);
5268 if (cfg
->method
->dynamic
)
5269 mono_dynamic_code_hash_lookup (cfg
->domain
, cfg
->method
)->ji
= cfg
->jit_info
;
5270 mono_domain_unlock (cfg
->domain
);
5275 printf ("GSHAREDVT: %s\n", mono_method_full_name (cfg
->method
, TRUE
));
5278 /* collect statistics */
5279 #ifndef DISABLE_PERFCOUNTERS
5280 mono_perfcounters
->jit_methods
++;
5281 mono_perfcounters
->jit_bytes
+= header
->code_size
;
5283 mono_jit_stats
.allocated_code_size
+= cfg
->code_len
;
5284 code_size_ratio
= cfg
->code_len
;
5285 if (code_size_ratio
> mono_jit_stats
.biggest_method_size
&& mono_jit_stats
.enabled
) {
5286 mono_jit_stats
.biggest_method_size
= code_size_ratio
;
5287 g_free (mono_jit_stats
.biggest_method
);
5288 mono_jit_stats
.biggest_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
5290 code_size_ratio
= (code_size_ratio
* 100) / header
->code_size
;
5291 if (code_size_ratio
> mono_jit_stats
.max_code_size_ratio
&& mono_jit_stats
.enabled
) {
5292 mono_jit_stats
.max_code_size_ratio
= code_size_ratio
;
5293 g_free (mono_jit_stats
.max_ratio_method
);
5294 mono_jit_stats
.max_ratio_method
= g_strdup_printf ("%s::%s)", method
->klass
->name
, method
->name
);
5296 mono_jit_stats
.native_code_size
+= cfg
->code_len
;
5298 if (MONO_METHOD_COMPILE_END_ENABLED ())
5299 MONO_PROBE_METHOD_COMPILE_END (method
, TRUE
);
5307 mini_method_compile (MonoMethod
*method
, guint32 opts
, MonoDomain
*domain
, gboolean run_cctors
, gboolean compile_aot
, int parts
)
5309 g_assert_not_reached ();
5313 #endif /* DISABLE_JIT */
5316 mono_domain_lookup_shared_generic (MonoDomain
*domain
, MonoMethod
*method
)
5318 static gboolean inited
= FALSE
;
5319 static int lookups
= 0;
5320 static int failed_lookups
= 0;
5323 ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, mini_get_shared_method (method
));
5324 if (ji
&& !ji
->has_generic_jit_info
)
5328 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
5329 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
5341 * LOCKING: Assumes domain->jit_code_hash_lock is held.
5344 lookup_method_inner (MonoDomain
*domain
, MonoMethod
*method
)
5346 MonoJitInfo
*ji
= mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
5351 if (!mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
, TRUE
))
5353 return mono_domain_lookup_shared_generic (domain
, method
);
5357 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
5361 mono_loader_lock (); /*FIXME lookup_method_inner acquired it*/
5362 mono_domain_jit_code_hash_lock (domain
);
5363 info
= lookup_method_inner (domain
, method
);
5364 mono_domain_jit_code_hash_unlock (domain
);
5365 mono_loader_unlock ();
5371 static FILE* perf_map_file
= NULL
;
5374 mono_enable_jit_map (void)
5376 if (!perf_map_file
) {
5378 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
5380 perf_map_file
= fopen (name
, "w");
5385 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
5388 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
5392 mono_emit_jit_map (MonoJitInfo
*jinfo
)
5394 if (perf_map_file
) {
5395 char *name
= mono_method_full_name (jinfo
->method
, TRUE
);
5396 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
5402 mono_jit_map_is_enabled (void)
5404 return perf_map_file
!= NULL
;
5410 mono_jit_compile_method_inner (MonoMethod
*method
, MonoDomain
*target_domain
, int opt
, MonoException
**jit_ex
)
5413 gpointer code
= NULL
;
5414 MonoJitInfo
*jinfo
, *info
;
5416 MonoException
*ex
= NULL
;
5417 guint32 prof_options
;
5419 MonoMethod
*prof_method
;
5421 #ifdef MONO_USE_AOT_COMPILER
5422 if (opt
& MONO_OPT_AOT
) {
5423 MonoDomain
*domain
= mono_domain_get ();
5425 mono_class_init (method
->klass
);
5427 if ((code
= mono_aot_get_method (domain
, method
))) {
5428 vtable
= mono_class_vtable (domain
, method
->klass
);
5430 mono_runtime_class_init (vtable
);
5437 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
5438 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
5440 MonoMethodPInvoke
* piinfo
= (MonoMethodPInvoke
*) method
;
5442 if (!piinfo
->addr
) {
5443 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
5444 piinfo
->addr
= mono_lookup_internal_call (method
);
5445 else if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
5447 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
);
5449 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
);
5452 mono_lookup_pinvoke_call (method
, NULL
, NULL
);
5454 nm
= mono_marshal_get_native_wrapper (method
, check_for_pending_exc
, FALSE
);
5455 code
= mono_get_addr_from_ftnptr (mono_compile_method (nm
));
5456 jinfo
= mono_jit_info_table_find (target_domain
, code
);
5458 jinfo
= mono_jit_info_table_find (mono_domain_get (), code
);
5460 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
5463 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
5464 //mono_debug_add_wrapper (method, nm);
5465 } else if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
5466 const char *name
= method
->name
;
5467 char *full_name
, *msg
;
5470 if (method
->klass
->parent
== mono_defaults
.multicastdelegate_class
) {
5471 if (*name
== '.' && (strcmp (name
, ".ctor") == 0)) {
5472 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_delegate_ctor");
5475 * We need to make sure this wrapper
5476 * is compiled because it might end up
5477 * in an (M)RGCTX if generic sharing
5478 * is enabled, and would be called
5479 * indirectly. If it were a
5480 * trampoline we'd try to patch that
5481 * indirect call, which is not
5484 return mono_get_addr_from_ftnptr ((gpointer
)mono_icall_get_wrapper_full (mi
, TRUE
));
5485 } else if (*name
== 'I' && (strcmp (name
, "Invoke") == 0)) {
5486 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
5487 return mono_create_delegate_trampoline (target_domain
, method
->klass
);
5489 nm
= mono_marshal_get_delegate_invoke (method
, NULL
);
5490 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
5492 } else if (*name
== 'B' && (strcmp (name
, "BeginInvoke") == 0)) {
5493 nm
= mono_marshal_get_delegate_begin_invoke (method
);
5494 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
5495 } else if (*name
== 'E' && (strcmp (name
, "EndInvoke") == 0)) {
5496 nm
= mono_marshal_get_delegate_end_invoke (method
);
5497 return mono_get_addr_from_ftnptr (mono_compile_method (nm
));
5501 full_name
= mono_method_full_name (method
, TRUE
);
5502 msg
= g_strdup_printf ("Unrecognizable runtime implemented method '%s'", full_name
);
5503 *jit_ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", msg
);
5509 if (method
->wrapper_type
== MONO_WRAPPER_UNKNOWN
) {
5510 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
5512 if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN
|| info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT
) {
5513 static MonoTrampInfo
*tinfo
;
5520 * This is a special wrapper whose body is implemented in assembly, like a trampoline. We use a wrapper so EH
5522 * FIXME: The caller signature doesn't match the callee, which might cause problems on some platforms
5524 if (mono_aot_only
) {
5526 return mono_aot_get_trampoline ("gsharedvt_trampoline");
5528 mono_arch_get_gsharedvt_trampoline (&tinfo
, FALSE
);
5529 jinfo
= create_jit_info_for_trampoline (method
, tinfo
);
5530 mono_jit_info_table_add (mono_get_root_domain (), jinfo
);
5536 if (mono_aot_only
) {
5537 char *fullname
= mono_method_full_name (method
, TRUE
);
5538 char *msg
= g_strdup_printf ("Attempting to JIT compile method '%s' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.\n", fullname
);
5540 *jit_ex
= mono_get_exception_execution_engine (msg
);
5547 jit_timer
= g_timer_new ();
5549 cfg
= mini_method_compile (method
, opt
, target_domain
, TRUE
, FALSE
, 0);
5550 prof_method
= cfg
->method
;
5552 g_timer_stop (jit_timer
);
5553 mono_jit_stats
.jit_time
+= g_timer_elapsed (jit_timer
, NULL
);
5554 g_timer_destroy (jit_timer
);
5556 switch (cfg
->exception_type
) {
5557 case MONO_EXCEPTION_NONE
:
5559 case MONO_EXCEPTION_TYPE_LOAD
:
5560 case MONO_EXCEPTION_MISSING_FIELD
:
5561 case MONO_EXCEPTION_MISSING_METHOD
:
5562 case MONO_EXCEPTION_FILE_NOT_FOUND
:
5563 case MONO_EXCEPTION_BAD_IMAGE
: {
5564 /* Throw a type load exception if needed */
5565 MonoLoaderError
*error
= mono_loader_get_last_error ();
5568 ex
= mono_loader_error_prepare_exception (error
);
5570 if (cfg
->exception_ptr
) {
5571 ex
= mono_class_get_exception_for_failure (cfg
->exception_ptr
);
5573 if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_FIELD
)
5574 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingFieldException", cfg
->exception_message
);
5575 else if (cfg
->exception_type
== MONO_EXCEPTION_MISSING_METHOD
)
5576 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MissingMethodException", cfg
->exception_message
);
5577 else if (cfg
->exception_type
== MONO_EXCEPTION_TYPE_LOAD
)
5578 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "TypeLoadException", cfg
->exception_message
);
5579 else if (cfg
->exception_type
== MONO_EXCEPTION_FILE_NOT_FOUND
)
5580 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FileNotFoundException", cfg
->exception_message
);
5581 else if (cfg
->exception_type
== MONO_EXCEPTION_BAD_IMAGE
)
5582 ex
= mono_get_exception_bad_image_format (cfg
->exception_message
);
5584 g_assert_not_reached ();
5589 case MONO_EXCEPTION_INVALID_PROGRAM
:
5590 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "InvalidProgramException", cfg
->exception_message
);
5592 case MONO_EXCEPTION_UNVERIFIABLE_IL
:
5593 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Security", "VerificationException", cfg
->exception_message
);
5595 case MONO_EXCEPTION_METHOD_ACCESS
:
5596 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "MethodAccessException", cfg
->exception_message
);
5598 case MONO_EXCEPTION_FIELD_ACCESS
:
5599 ex
= mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "FieldAccessException", cfg
->exception_message
);
5601 /* this can only be set if the security manager is active */
5602 case MONO_EXCEPTION_SECURITY_LINKDEMAND
: {
5603 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
5604 MonoObject
*exc
= NULL
;
5607 args
[0] = &cfg
->exception_data
;
5609 mono_runtime_invoke (secman
->linkdemandsecurityexception
, NULL
, args
, &exc
);
5611 ex
= (MonoException
*)exc
;
5614 case MONO_EXCEPTION_OBJECT_SUPPLIED
: {
5615 MonoException
*exp
= cfg
->exception_ptr
;
5616 MONO_GC_UNREGISTER_ROOT (cfg
->exception_ptr
);
5621 case MONO_EXCEPTION_OUT_OF_MEMORY
:
5622 ex
= mono_domain_get ()->out_of_memory_ex
;
5625 g_assert_not_reached ();
5629 if (cfg
->prof_options
& MONO_PROFILE_JIT_COMPILATION
)
5630 mono_profiler_method_end_jit (method
, NULL
, MONO_PROFILE_FAILED
);
5632 mono_destroy_compile (cfg
);
5638 mono_loader_lock (); /*FIXME lookup_method_inner requires the loader lock*/
5639 mono_domain_lock (target_domain
);
5641 /* Check if some other thread already did the job. In this case, we can
5642 discard the code this thread generated. */
5644 mono_domain_jit_code_hash_lock (target_domain
);
5646 info
= lookup_method_inner (target_domain
, method
);
5648 /* We can't use a domain specific method in another domain */
5649 if ((target_domain
== mono_domain_get ()) || info
->domain_neutral
) {
5650 code
= info
->code_start
;
5651 // printf("Discarding code for method %s\n", method->name);
5656 mono_internal_hash_table_insert (&target_domain
->jit_code_hash
, cfg
->jit_info
->method
, cfg
->jit_info
);
5657 mono_domain_jit_code_hash_unlock (target_domain
);
5658 code
= cfg
->native_code
;
5660 if (cfg
->generic_sharing_context
&& mono_method_is_generic_sharable_impl_full (method
, FALSE
, FALSE
, TRUE
))
5661 mono_stats
.generics_shared_methods
++;
5663 mono_stats
.gsharedvt_methods
++;
5665 mono_domain_jit_code_hash_unlock (target_domain
);
5668 jinfo
= cfg
->jit_info
;
5670 prof_options
= cfg
->prof_options
;
5673 * Update global stats while holding a lock, instead of doing many
5674 * InterlockedIncrement operations during JITting.
5676 mono_jit_stats
.allocate_var
+= cfg
->stat_allocate_var
;
5677 mono_jit_stats
.locals_stack_size
+= cfg
->stat_locals_stack_size
;
5678 mono_jit_stats
.basic_blocks
+= cfg
->stat_basic_blocks
;
5679 mono_jit_stats
.max_basic_blocks
= MAX (cfg
->stat_basic_blocks
, mono_jit_stats
.max_basic_blocks
);
5680 mono_jit_stats
.cil_code_size
+= cfg
->stat_cil_code_size
;
5681 mono_jit_stats
.regvars
+= cfg
->stat_n_regvars
;
5682 mono_jit_stats
.inlineable_methods
+= cfg
->stat_inlineable_methods
;
5683 mono_jit_stats
.inlined_methods
+= cfg
->stat_inlined_methods
;
5684 mono_jit_stats
.cas_demand_generation
+= cfg
->stat_cas_demand_generation
;
5685 mono_jit_stats
.code_reallocs
+= cfg
->stat_code_reallocs
;
5687 mono_destroy_compile (cfg
);
5690 if (domain_jit_info (target_domain
)->jump_target_hash
) {
5691 MonoJumpInfo patch_info
;
5692 MonoJumpList
*jlist
;
5694 jlist
= g_hash_table_lookup (domain_jit_info (target_domain
)->jump_target_hash
, method
);
5696 patch_info
.next
= NULL
;
5697 patch_info
.ip
.i
= 0;
5698 patch_info
.type
= MONO_PATCH_INFO_METHOD_JUMP
;
5699 patch_info
.data
.method
= method
;
5700 g_hash_table_remove (domain_jit_info (target_domain
)->jump_target_hash
, method
);
5702 for (tmp
= jlist
->list
; tmp
; tmp
= tmp
->next
)
5703 mono_arch_patch_code (NULL
, target_domain
, tmp
->data
, &patch_info
, NULL
, TRUE
);
5707 mono_emit_jit_map (jinfo
);
5709 mono_domain_unlock (target_domain
);
5710 mono_loader_unlock ();
5712 vtable
= mono_class_vtable (target_domain
, method
->klass
);
5714 ex
= mono_class_get_exception_for_failure (method
->klass
);
5720 if (prof_options
& MONO_PROFILE_JIT_COMPILATION
) {
5721 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
5722 if (mono_marshal_method_from_wrapper (method
)) {
5723 /* Native func wrappers have no method */
5724 /* The profiler doesn't know about wrappers, so pass the original icall method */
5725 mono_profiler_method_end_jit (mono_marshal_method_from_wrapper (method
), jinfo
, MONO_PROFILE_OK
);
5728 mono_profiler_method_end_jit (method
, jinfo
, MONO_PROFILE_OK
);
5729 if (prof_method
!= method
) {
5730 mono_profiler_method_end_jit (prof_method
, jinfo
, MONO_PROFILE_OK
);
5734 ex
= mono_runtime_class_init_full (vtable
, FALSE
);
5743 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, MonoException
**ex
)
5745 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
5748 MonoJitICallInfo
*callinfo
= NULL
;
5751 * ICALL wrappers are handled specially, since there is only one copy of them
5752 * shared by all appdomains.
5754 if ((method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) && (strstr (method
->name
, "__icall_wrapper_") == method
->name
)) {
5755 const char *icall_name
;
5757 icall_name
= method
->name
+ strlen ("__icall_wrapper_");
5758 g_assert (icall_name
);
5759 callinfo
= mono_find_jit_icall_by_name (icall_name
);
5760 g_assert (callinfo
);
5762 /* Must be domain neutral since there is only one copy */
5763 opt
|= MONO_OPT_SHARED
;
5766 if (method
->dynamic
)
5767 opt
&= ~MONO_OPT_SHARED
;
5769 /* These methods can become invalid when a domain is unloaded */
5770 if (method
->klass
->image
!= mono_get_corlib () || method
->is_inflated
)
5771 opt
&= ~MONO_OPT_SHARED
;
5773 if (opt
& MONO_OPT_SHARED
)
5774 target_domain
= mono_get_root_domain ();
5776 target_domain
= domain
;
5778 if (method
->wrapper_type
== MONO_WRAPPER_UNKNOWN
) {
5779 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
5782 if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
5783 method
= info
->d
.synchronized_inner
.method
;
5786 info
= lookup_method (target_domain
, method
);
5788 /* We can't use a domain specific method in another domain */
5789 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
5791 MonoException
*tmpEx
;
5793 mono_jit_stats
.methods_lookups
++;
5794 vtable
= mono_class_vtable (domain
, method
->klass
);
5796 tmpEx
= mono_runtime_class_init_full (vtable
, ex
== NULL
);
5801 return mono_create_ftnptr (target_domain
, info
->code_start
);
5805 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, ex
);
5809 p
= mono_create_ftnptr (target_domain
, code
);
5812 /*mono_register_jit_icall_wrapper takes the loader lock, so we take it on the outside. */
5813 mono_loader_lock ();
5815 if (!callinfo
->wrapper
) {
5816 callinfo
->wrapper
= p
;
5817 mono_register_jit_icall_wrapper (callinfo
, p
);
5818 mono_debug_add_icall_wrapper (method
, callinfo
);
5821 mono_loader_unlock ();
5828 mono_jit_compile_method (MonoMethod
*method
)
5830 MonoException
*ex
= NULL
;
5833 code
= mono_jit_compile_method_with_opt (method
, default_opt
, &ex
);
5836 mono_raise_exception (ex
);
5842 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
5844 invalidated_delegate_trampoline (char *desc
)
5846 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
5847 "See http://www.mono-project.com/Diagnostic:Delegate for an explanation and ways to fix this.",
5853 * mono_jit_free_method:
5855 * Free all memory allocated by the JIT for METHOD.
5858 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
5860 MonoJitDynamicMethodInfo
*ji
;
5861 gboolean destroy
= TRUE
;
5862 GHashTableIter iter
;
5863 MonoJumpList
*jlist
;
5865 g_assert (method
->dynamic
);
5867 mono_domain_lock (domain
);
5868 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
5869 mono_domain_unlock (domain
);
5874 mono_debug_remove_method (method
, domain
);
5876 mono_domain_lock (domain
);
5877 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
5878 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
5879 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
5880 g_hash_table_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
5882 /* Remove jump targets in this method */
5883 g_hash_table_iter_init (&iter
, domain_jit_info (domain
)->jump_target_hash
);
5884 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&jlist
)) {
5885 GSList
*tmp
, *remove
;
5888 for (tmp
= jlist
->list
; tmp
; tmp
= tmp
->next
) {
5889 guint8
*ip
= tmp
->data
;
5891 if (ip
>= (guint8
*)ji
->ji
->code_start
&& ip
< (guint8
*)ji
->ji
->code_start
+ ji
->ji
->code_size
)
5892 remove
= g_slist_prepend (remove
, tmp
);
5894 for (tmp
= remove
; tmp
; tmp
= tmp
->next
) {
5895 jlist
->list
= g_slist_delete_link (jlist
->list
, tmp
->data
);
5897 g_slist_free (remove
);
5900 mono_domain_unlock (domain
);
5902 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
5903 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
5905 * Instead of freeing the code, change it to call an error routine
5906 * so people can fix their code.
5908 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
5909 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
5912 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
5918 * This needs to be done before freeing code_mp, since the code address is the
5919 * key in the table, so if we free the code_mp first, another thread can grab the
5920 * same code address and replace our entry in the table.
5922 mono_jit_info_table_remove (domain
, ji
->ji
);
5925 mono_code_manager_destroy (ji
->code_mp
);
5930 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
5932 MonoDomain
*target_domain
;
5935 if (default_opt
& MONO_OPT_SHARED
)
5936 target_domain
= mono_get_root_domain ();
5938 target_domain
= domain
;
5940 info
= lookup_method (target_domain
, method
);
5942 /* We can't use a domain specific method in another domain */
5943 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
5944 mono_jit_stats
.methods_lookups
++;
5947 return info
->code_start
;
5957 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
5959 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
5964 gpointer compiled_method
;
5965 gpointer runtime_invoke
;
5967 MonoDynCallInfo
*dyn_call_info
;
5968 MonoClass
*ret_box_class
;
5969 } RuntimeInvokeInfo
;
5972 * mono_jit_runtime_invoke:
5973 * @method: the method to invoke
5974 * @obj: this pointer
5975 * @params: array of parameter values.
5976 * @exc: used to catch exceptions objects
5979 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
5982 MonoObject
*(*runtime_invoke
) (MonoObject
*this, void **params
, MonoObject
**exc
, void* compiled_method
);
5983 MonoDomain
*domain
= mono_domain_get ();
5984 MonoJitDomainInfo
*domain_info
;
5985 RuntimeInvokeInfo
*info
, *info2
;
5987 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
5988 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
5992 domain_info
= domain_jit_info (domain
);
5994 mono_domain_lock (domain
);
5995 info
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
5996 mono_domain_unlock (domain
);
5999 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
6001 * This might be redundant since mono_class_vtable () already does this,
6002 * but keep it just in case for moonlight.
6004 mono_class_setup_vtable (method
->klass
);
6005 if (method
->klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
6007 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
6009 mono_raise_exception (mono_class_get_exception_for_failure (method
->klass
));
6014 info
= g_new0 (RuntimeInvokeInfo
, 1);
6016 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
6017 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, TRUE
);
6018 g_assert (info
->vtable
);
6020 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
6021 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
6023 * Array Get/Set/Address methods. The JIT implements them using inline code
6024 * inside the runtime invoke wrappers, so no need to compile them.
6026 info
->compiled_method
= NULL
;
6028 MonoException
*jit_ex
= NULL
;
6030 info
->compiled_method
= mono_jit_compile_method_with_opt (method
, default_opt
, &jit_ex
);
6031 if (!info
->compiled_method
) {
6035 *exc
= (MonoObject
*)jit_ex
;
6038 mono_raise_exception (jit_ex
);
6042 info
->compiled_method
= mini_add_method_trampoline (NULL
, method
, info
->compiled_method
, NULL
, mono_method_needs_static_rgctx_invoke (method
, FALSE
));
6046 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
6047 * in full-aot mode, so we use a slower, but more generic wrapper if
6048 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
6050 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
6051 if (mono_aot_only
|| debug_options
.dyn_runtime_invoke
) {
6052 MonoMethodSignature
*sig
= mono_method_signature (method
);
6053 gboolean supported
= TRUE
;
6056 if (method
->string_ctor
)
6057 sig
= mono_marshal_get_string_ctor_signature (method
);
6059 for (i
= 0; i
< sig
->param_count
; ++i
) {
6060 MonoType
*t
= sig
->params
[i
];
6062 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
6066 if (method
->klass
->contextbound
|| !info
->compiled_method
)
6070 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
6072 if (info
->dyn_call_info
) {
6073 switch (sig
->ret
->type
) {
6074 case MONO_TYPE_VOID
:
6086 case MONO_TYPE_BOOLEAN
:
6087 case MONO_TYPE_CHAR
:
6090 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
6093 info
->ret_box_class
= mono_defaults
.int_class
;
6095 case MONO_TYPE_STRING
:
6096 case MONO_TYPE_CLASS
:
6097 case MONO_TYPE_ARRAY
:
6098 case MONO_TYPE_SZARRAY
:
6099 case MONO_TYPE_OBJECT
:
6101 case MONO_TYPE_GENERICINST
:
6102 if (!MONO_TYPE_IS_REFERENCE (sig
->ret
))
6103 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
6105 case MONO_TYPE_VALUETYPE
:
6106 info
->ret_box_class
= mono_class_from_mono_type (sig
->ret
);
6109 g_assert_not_reached ();
6116 if (!info
->dyn_call_info
)
6117 info
->runtime_invoke
= mono_jit_compile_method (invoke
);
6119 mono_domain_lock (domain
);
6120 info2
= g_hash_table_lookup (domain_info
->runtime_invoke_hash
, method
);
6125 g_hash_table_insert (domain_info
->runtime_invoke_hash
, method
, info
);
6127 mono_domain_unlock (domain
);
6130 runtime_invoke
= info
->runtime_invoke
;
6133 * We need this here because mono_marshal_get_runtime_invoke can place
6134 * the helper method in System.Object and not the target class.
6137 *exc
= (MonoObject
*)mono_runtime_class_init_full (info
->vtable
, FALSE
);
6141 mono_runtime_class_init (info
->vtable
);
6144 /* The wrappers expect this to be initialized to NULL */
6148 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
6149 if (info
->dyn_call_info
) {
6150 MonoMethodSignature
*sig
= mono_method_signature (method
);
6152 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
6155 guint8 retval
[128];
6157 if (!dyn_runtime_invoke
) {
6158 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
6159 dyn_runtime_invoke
= mono_jit_compile_method (invoke
);
6162 /* Convert the arguments to the format expected by start_dyn_call () */
6163 args
= g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
6166 args
[pindex
++] = &obj
;
6167 for (i
= 0; i
< sig
->param_count
; ++i
) {
6168 MonoType
*t
= sig
->params
[i
];
6171 args
[pindex
++] = ¶ms
[i
];
6172 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
6173 args
[pindex
++] = ¶ms
[i
];
6175 args
[pindex
++] = params
[i
];
6179 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
6181 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
6183 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
6185 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
6187 if (info
->ret_box_class
)
6188 return mono_value_box (domain
, info
->ret_box_class
, retval
);
6190 return *(MonoObject
**)retval
;
6194 return runtime_invoke (obj
, params
, exc
, info
->compiled_method
);
6198 SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler
)
6200 MonoException
*exc
= NULL
;
6202 #if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
6207 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
6209 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
6210 if (mono_arch_is_int_overflow (ctx
, info
))
6212 * The spec says this throws ArithmeticException, but MS throws the derived
6213 * OverflowException.
6215 exc
= mono_get_exception_overflow ();
6217 exc
= mono_get_exception_divide_by_zero ();
6219 exc
= mono_get_exception_divide_by_zero ();
6223 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
6226 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
6229 mono_arch_handle_exception (ctx
, exc
);
6233 SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler
)
6238 exc
= mono_get_exception_execution_engine ("SIGILL");
6240 mono_arch_handle_exception (ctx
, exc
);
6243 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
6244 #define HAVE_SIG_INFO
6248 SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler
)
6251 MonoJitTlsData
*jit_tls
= mono_native_tls_get_value (mono_jit_tls_id
);
6252 gpointer fault_addr
= NULL
;
6256 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
6257 if (mono_arch_is_single_step_event (info
, ctx
)) {
6258 mono_debugger_agent_single_step_event (ctx
);
6260 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
6261 mono_debugger_agent_breakpoint_hit (ctx
);
6266 #if !defined(HOST_WIN32) && defined(HAVE_SIG_INFO)
6267 fault_addr
= info
->si_addr
;
6268 if (mono_aot_is_pagefault (info
->si_addr
)) {
6269 mono_aot_handle_pagefault (info
->si_addr
);
6274 /* The thread might no be registered with the runtime */
6275 if (!mono_domain_get () || !jit_tls
) {
6276 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
6278 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
6281 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx
));
6283 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
6284 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
))
6287 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
6288 /* info->si_addr seems to be NULL on some kernels when handling stack overflows */
6289 fault_addr
= info
->si_addr
;
6290 if (fault_addr
== NULL
) {
6293 mono_arch_sigctx_to_monoctx (ctx
, &mctx
);
6295 fault_addr
= MONO_CONTEXT_GET_SP (&mctx
);
6299 if (jit_tls
->stack_size
&&
6300 ABS ((guint8
*)fault_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 8192 * sizeof (gpointer
)) {
6302 * The hard-guard page has been hit: there is not much we can do anymore
6303 * Print a hopefully clear message and abort.
6305 mono_handle_hard_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
);
6306 g_assert_not_reached ();
6308 /* The original handler might not like that it is executed on an altstack... */
6309 if (!ji
&& mono_chain_signal (SIG_HANDLER_PARAMS
))
6312 mono_arch_handle_altstack_exception (ctx
, info
->si_addr
, FALSE
);
6317 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
6320 mono_handle_native_sigsegv (SIGSEGV
, ctx
);
6323 mono_arch_handle_exception (ctx
, NULL
);
6328 SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler
)
6333 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
6335 mono_arch_handle_exception (ctx
, exc
);
6338 /* mono_jit_create_remoting_trampoline:
6339 * @method: pointer to the method info
6341 * Creates a trampoline which calls the remoting functions. This
6342 * is used in the vtable of transparent proxies.
6344 * Returns: a pointer to the newly created code
6347 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
)
6350 guint8
*addr
= NULL
;
6352 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
6353 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
6357 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
6358 (mono_method_signature (method
)->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
))) {
6359 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
6360 addr
= mono_compile_method (nm
);
6362 addr
= mono_compile_method (method
);
6364 return mono_get_addr_from_ftnptr (addr
);
6367 static gpointer
*vtable_trampolines
;
6368 static int vtable_trampolines_size
;
6371 mini_get_vtable_trampoline (int slot_index
)
6373 int index
= slot_index
+ MONO_IMT_SIZE
;
6375 g_assert (slot_index
>= - MONO_IMT_SIZE
);
6376 if (!vtable_trampolines
|| slot_index
+ MONO_IMT_SIZE
>= vtable_trampolines_size
) {
6378 if (!vtable_trampolines
|| index
>= vtable_trampolines_size
) {
6382 new_size
= vtable_trampolines_size
? vtable_trampolines_size
* 2 : 128;
6383 while (new_size
<= index
)
6385 new_table
= g_new0 (gpointer
, new_size
);
6387 if (vtable_trampolines
)
6388 memcpy (new_table
, vtable_trampolines
, vtable_trampolines_size
* sizeof (gpointer
));
6389 g_free (vtable_trampolines
);
6390 mono_memory_barrier ();
6391 vtable_trampolines
= new_table
;
6392 vtable_trampolines_size
= new_size
;
6397 if (!vtable_trampolines
[index
])
6398 vtable_trampolines
[index
] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index
), MONO_TRAMPOLINE_VCALL
, mono_get_root_domain (), NULL
);
6399 return vtable_trampolines
[index
];
6403 mini_get_imt_trampoline (int slot_index
)
6405 return mini_get_vtable_trampoline (slot_index
- MONO_IMT_SIZE
);
6409 mini_parse_debug_options (void)
6411 char *options
= getenv ("MONO_DEBUG");
6412 gchar
**args
, **ptr
;
6417 args
= g_strsplit (options
, ",", -1);
6419 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
6420 const char *arg
= *ptr
;
6422 if (!strcmp (arg
, "handle-sigint"))
6423 debug_options
.handle_sigint
= TRUE
;
6424 else if (!strcmp (arg
, "keep-delegates"))
6425 debug_options
.keep_delegates
= TRUE
;
6426 else if (!strcmp (arg
, "reverse-pinvoke-exceptions"))
6427 debug_options
.reverse_pinvoke_exceptions
= TRUE
;
6428 else if (!strcmp (arg
, "collect-pagefault-stats"))
6429 debug_options
.collect_pagefault_stats
= TRUE
;
6430 else if (!strcmp (arg
, "break-on-unverified"))
6431 debug_options
.break_on_unverified
= TRUE
;
6432 else if (!strcmp (arg
, "no-gdb-backtrace"))
6433 debug_options
.no_gdb_backtrace
= TRUE
;
6434 else if (!strcmp (arg
, "suspend-on-sigsegv"))
6435 debug_options
.suspend_on_sigsegv
= TRUE
;
6436 else if (!strcmp (arg
, "suspend-on-unhandled"))
6437 debug_options
.suspend_on_unhandled
= TRUE
;
6438 else if (!strcmp (arg
, "dont-free-domains"))
6439 mono_dont_free_domains
= TRUE
;
6440 else if (!strcmp (arg
, "dyn-runtime-invoke"))
6441 debug_options
.dyn_runtime_invoke
= TRUE
;
6442 else if (!strcmp (arg
, "gdb"))
6443 debug_options
.gdb
= TRUE
;
6444 else if (!strcmp (arg
, "explicit-null-checks"))
6445 debug_options
.explicit_null_checks
= TRUE
;
6446 else if (!strcmp (arg
, "gen-seq-points"))
6447 debug_options
.gen_seq_points
= TRUE
;
6448 else if (!strcmp (arg
, "init-stacks"))
6449 debug_options
.init_stacks
= TRUE
;
6450 else if (!strcmp (arg
, "casts"))
6451 debug_options
.better_cast_details
= TRUE
;
6452 else if (!strcmp (arg
, "soft-breakpoints"))
6453 debug_options
.soft_breakpoints
= TRUE
;
6455 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
6456 fprintf (stderr
, "Available options: 'handle-sigint', 'keep-delegates', 'reverse-pinvoke-exceptions', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains', 'suspend-on-sigsegv', 'suspend-on-unhandled', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'init-stacks'\n");
6465 mini_get_debug_options (void)
6467 return &debug_options
;
6471 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
6473 #if !defined(__ia64__) && !defined(__ppc64__) && !defined(__powerpc64__)
6477 gpointer
* desc
= NULL
;
6479 if ((desc
= g_hash_table_lookup (domain
->ftnptrs_hash
, addr
)))
6482 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
6486 # elif defined(__ppc64__) || defined(__powerpc64__)
6489 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
6495 g_hash_table_insert (domain
->ftnptrs_hash
, addr
, desc
);
6501 mini_get_addr_from_ftnptr (gpointer descr
)
6503 #if defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__)
6504 return *(gpointer
*)descr
;
6511 register_jit_stats (void)
6513 mono_counters_register ("Compiled methods", MONO_COUNTER_JIT
| MONO_COUNTER_WORD
, &mono_jit_stats
.methods_compiled
);
6514 mono_counters_register ("Methods from AOT", MONO_COUNTER_JIT
| MONO_COUNTER_WORD
, &mono_jit_stats
.methods_aot
);
6515 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_without_llvm
);
6516 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_with_llvm
);
6517 mono_counters_register ("Total time spent JITting (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_time
);
6518 mono_counters_register ("Basic blocks", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.basic_blocks
);
6519 mono_counters_register ("Max basic blocks", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.max_basic_blocks
);
6520 mono_counters_register ("Allocated vars", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.allocate_var
);
6521 mono_counters_register ("Code reallocs", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.code_reallocs
);
6522 mono_counters_register ("Allocated code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.allocated_code_size
);
6523 mono_counters_register ("Inlineable methods", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.inlineable_methods
);
6524 mono_counters_register ("Inlined methods", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.inlined_methods
);
6525 mono_counters_register ("Regvars", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.regvars
);
6526 mono_counters_register ("Locals stack size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.locals_stack_size
);
6527 mono_counters_register ("Method cache lookups", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_lookups
);
6528 mono_counters_register ("Compiled CIL code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.cil_code_size
);
6529 mono_counters_register ("Native code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.native_code_size
);
6532 static void runtime_invoke_info_free (gpointer value
);
6535 mini_create_jit_domain_info (MonoDomain
*domain
)
6537 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
6539 info
->class_init_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6540 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6541 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6542 info
->delegate_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6543 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6544 info
->runtime_invoke_hash
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
6545 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
6546 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
6547 info
->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
6549 domain
->runtime_info
= info
;
6553 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
6555 MonoJumpList
*jlist
= value
;
6556 g_slist_free (jlist
->list
);
6560 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
6562 MonoJitDynamicMethodInfo
*di
= value
;
6563 mono_code_manager_destroy (di
->code_mp
);
6568 runtime_invoke_info_free (gpointer value
)
6570 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
6572 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
6573 if (info
->dyn_call_info
)
6574 mono_arch_dyn_call_free (info
->dyn_call_info
);
6580 mini_free_jit_domain_info (MonoDomain
*domain
)
6582 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
6584 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
6585 g_hash_table_destroy (info
->jump_target_hash
);
6586 if (info
->jump_target_got_slot_hash
) {
6587 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_jump_list
, NULL
);
6588 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
6590 if (info
->dynamic_code_hash
) {
6591 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
6592 g_hash_table_destroy (info
->dynamic_code_hash
);
6594 if (info
->method_code_hash
)
6595 g_hash_table_destroy (info
->method_code_hash
);
6596 g_hash_table_destroy (info
->class_init_trampoline_hash
);
6597 g_hash_table_destroy (info
->jump_trampoline_hash
);
6598 g_hash_table_destroy (info
->jit_trampoline_hash
);
6599 g_hash_table_destroy (info
->delegate_trampoline_hash
);
6600 if (info
->static_rgctx_trampoline_hash
)
6601 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
6602 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
6603 g_hash_table_destroy (info
->runtime_invoke_hash
);
6604 g_hash_table_destroy (info
->seq_points
);
6605 g_hash_table_destroy (info
->arch_seq_points
);
6607 if (info
->agent_info
)
6608 mono_debugger_agent_free_domain_info (domain
);
6610 g_free (domain
->runtime_info
);
6611 domain
->runtime_info
= NULL
;
6615 mini_init (const char *filename
, const char *runtime_version
)
6618 MonoRuntimeCallbacks callbacks
;
6619 MonoThreadInfoRuntimeCallbacks ticallbacks
;
6621 MONO_VES_INIT_BEGIN ();
6623 #if defined(__linux__) && !defined(__native_client__)
6624 if (access ("/proc/self/maps", F_OK
) != 0) {
6625 g_print ("Mono requires /proc to be mounted.\n");
6630 /* Happens when using the embedding interface */
6631 if (!default_opt_set
)
6632 default_opt
= mono_parse_default_optimizations (NULL
);
6634 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
6636 mono_set_generic_sharing_vt_supported (TRUE
);
6639 InitializeCriticalSection (&jit_mutex
);
6641 #ifdef MONO_DEBUGGER_SUPPORTED
6642 if (mini_debug_running_inside_mdb ())
6643 mini_debugger_init ();
6646 #ifdef MONO_HAVE_FAST_TLS
6647 MONO_FAST_TLS_INIT (mono_jit_tls
);
6648 MONO_FAST_TLS_INIT (mono_lmf_addr
);
6649 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
6650 MONO_FAST_TLS_INIT (mono_lmf
);
6654 #ifdef MONO_ARCH_HAVE_TLS_GET
6655 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET
);
6657 mono_runtime_set_has_tls_get (FALSE
);
6660 if (!global_codeman
)
6661 global_codeman
= mono_code_manager_new ();
6662 jit_icall_name_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
6664 memset (&callbacks
, 0, sizeof (callbacks
));
6665 callbacks
.create_ftnptr
= mini_create_ftnptr
;
6666 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
6667 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
6668 callbacks
.set_cast_details
= mono_set_cast_details
;
6669 callbacks
.debug_log
= mono_debugger_agent_debug_log
;
6670 callbacks
.debug_log_is_enabled
= mono_debugger_agent_debug_log_is_enabled
;
6672 #ifdef MONO_ARCH_HAVE_IMT
6674 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
6675 callbacks
.get_imt_trampoline
= mini_get_imt_trampoline
;
6679 mono_install_callbacks (&callbacks
);
6681 memset (&ticallbacks
, 0, sizeof (ticallbacks
));
6682 ticallbacks
.setup_async_callback
= mono_setup_async_callback
;
6683 ticallbacks
.thread_state_init_from_sigctx
= mono_thread_state_init_from_sigctx
;
6684 ticallbacks
.thread_state_init_from_handle
= mono_thread_state_init_from_handle
;
6686 mono_threads_runtime_init (&ticallbacks
);
6689 if (getenv ("MONO_DEBUG") != NULL
)
6690 mini_parse_debug_options ();
6692 mono_arch_cpu_init ();
6696 mono_unwind_init ();
6698 if (getenv ("MONO_XDEBUG")) {
6699 char *xdebug_opts
= getenv ("MONO_XDEBUG");
6700 mono_xdebug_init (xdebug_opts
);
6701 /* So methods for multiple domains don't have the same address */
6702 mono_dont_free_domains
= TRUE
;
6703 mono_using_xdebug
= TRUE
;
6704 } else if (mini_get_debug_options ()->gdb
) {
6705 mono_xdebug_init ((char*)"gdb");
6706 mono_dont_free_domains
= TRUE
;
6707 mono_using_xdebug
= TRUE
;
6711 if (mono_use_llvm
) {
6712 if (!mono_llvm_load (NULL
)) {
6713 mono_use_llvm
= FALSE
;
6714 fprintf (stderr
, "Mono Warning: llvm support could not be loaded.\n");
6721 mono_trampolines_init ();
6723 if (!g_thread_supported ())
6724 g_thread_init (NULL
);
6726 mono_native_tls_alloc (&mono_jit_tls_id
, NULL
);
6728 if (default_opt
& MONO_OPT_AOT
)
6731 mono_debugger_agent_init ();
6733 #ifdef MONO_ARCH_GSHARED_SUPPORTED
6734 mono_set_generic_sharing_supported (TRUE
);
6737 #ifndef MONO_CROSS_COMPILE
6738 mono_runtime_install_handlers ();
6740 mono_threads_install_cleanup (mini_thread_cleanup
);
6742 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
6743 // This is experimental code so provide an env var to switch it off
6744 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
6745 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
6747 check_for_pending_exc
= FALSE
;
6748 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc
);
6752 #define JIT_TRAMPOLINES_WORK
6753 #ifdef JIT_TRAMPOLINES_WORK
6754 mono_install_compile_method (mono_jit_compile_method
);
6755 mono_install_free_method (mono_jit_free_method
);
6756 mono_install_trampoline (mono_create_jit_trampoline
);
6757 mono_install_jump_trampoline (mono_create_jump_trampoline
);
6758 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline
);
6759 mono_install_delegate_trampoline (mono_create_delegate_trampoline
);
6760 mono_install_create_domain_hook (mini_create_jit_domain_info
);
6761 mono_install_free_domain_hook (mini_free_jit_domain_info
);
6763 #define JIT_INVOKE_WORKS
6764 #ifdef JIT_INVOKE_WORKS
6765 mono_install_runtime_invoke (mono_jit_runtime_invoke
);
6767 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
6768 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
6769 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
6771 if (debug_options
.collect_pagefault_stats
) {
6772 mono_aot_set_make_unreadable (TRUE
);
6775 if (runtime_version
)
6776 domain
= mono_init_version (filename
, runtime_version
);
6778 domain
= mono_init_from_assembly (filename
, filename
);
6780 if (mono_aot_only
) {
6781 /* This helps catch code allocation requests */
6782 mono_code_manager_set_read_only (domain
->code_mp
);
6783 mono_marshal_use_aot_wrappers (TRUE
);
6786 #ifdef MONO_ARCH_HAVE_IMT
6789 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk
);
6791 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk
);
6794 /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
6795 mono_arch_finish_init ();
6798 /* This must come after mono_init () in the aot-only case */
6799 mono_exceptions_init ();
6803 /* This should come after mono_init () too */
6806 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6807 ves_icall_get_frame_info
);
6808 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6809 ves_icall_get_trace
);
6810 mono_add_internal_call ("System.Exception::get_trace",
6811 ves_icall_System_Exception_get_trace
);
6812 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
6813 ves_icall_System_Security_SecurityFrame_GetSecurityFrame
);
6814 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
6815 ves_icall_System_Security_SecurityFrame_GetSecurityStack
);
6816 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6817 mono_runtime_install_handlers
);
6819 #ifdef PLATFORM_ANDROID
6820 mono_add_internal_call ("System.Diagnostics.Debugger::Mono_UnhandledException_internal",
6821 mono_debugger_agent_unhandled_exception
);
6824 mono_create_helper_signatures ();
6826 register_jit_stats ();
6828 #define JIT_CALLS_WORK
6829 #ifdef JIT_CALLS_WORK
6830 /* Needs to be called here since register_jit_icall depends on it */
6831 mono_marshal_init ();
6833 mono_arch_register_lowlevel_calls ();
6834 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", NULL
, TRUE
);
6835 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", NULL
, TRUE
);
6836 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
6837 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
6838 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
6839 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "ptr ptr", TRUE
);
6840 register_icall (mono_jit_set_domain
, "mono_jit_set_domain", "void ptr", TRUE
);
6841 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
6843 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
6844 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
6845 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
6847 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
6848 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "void", FALSE
);
6849 register_icall (mono_thread_force_interruption_checkpoint
, "mono_thread_force_interruption_checkpoint", "void", FALSE
);
6850 register_icall (mono_load_remote_field_new
, "mono_load_remote_field_new", "object object ptr ptr", FALSE
);
6851 register_icall (mono_store_remote_field_new
, "mono_store_remote_field_new", "void object ptr ptr object", FALSE
);
6853 #if defined(__native_client__) || defined(__native_client_codegen__)
6854 register_icall (mono_nacl_gc
, "mono_nacl_gc", "void", TRUE
);
6857 * NOTE, NOTE, NOTE, NOTE:
6858 * when adding emulation for some opcodes, remember to also add a dummy
6859 * rule to the burg files, because we need the arity information to be correct.
6861 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
6862 mono_register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, TRUE
);
6863 mono_register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, FALSE
);
6864 mono_register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, FALSE
);
6865 mono_register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, FALSE
);
6866 mono_register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, FALSE
);
6867 mono_register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, FALSE
);
6868 mono_register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, FALSE
);
6871 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
6872 mono_register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, TRUE
);
6873 mono_register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, TRUE
);
6874 mono_register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, TRUE
);
6877 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
6878 mono_register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, FALSE
);
6879 mono_register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, FALSE
);
6880 mono_register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, FALSE
);
6881 mono_register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, FALSE
);
6884 #ifdef MONO_ARCH_EMULATE_MUL_DIV
6885 mono_register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, TRUE
);
6888 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
6889 mono_register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, FALSE
);
6890 mono_register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, FALSE
);
6893 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
6894 mono_register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, FALSE
);
6897 mono_register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, FALSE
);
6898 mono_register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, FALSE
);
6899 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, FALSE
);
6900 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, FALSE
);
6902 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
6903 mono_register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, FALSE
);
6905 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
6906 mono_register_opcode_emulation (OP_ICONV_TO_R_UN
, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un
, FALSE
);
6908 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
6909 mono_register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, FALSE
);
6911 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
6912 mono_register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, FALSE
);
6914 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
6915 mono_register_opcode_emulation (OP_LCONV_TO_R_UN
, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un
, FALSE
);
6917 #ifdef MONO_ARCH_EMULATE_FREM
6918 #if defined(__default_codegen__)
6919 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, FALSE
);
6920 #elif defined(__native_client_codegen__)
6921 mono_register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", mono_fmod
, FALSE
);
6925 #ifdef MONO_ARCH_SOFT_FLOAT
6926 mono_register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, FALSE
);
6927 mono_register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, FALSE
);
6928 mono_register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, FALSE
);
6929 mono_register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, FALSE
);
6930 mono_register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, FALSE
);
6931 mono_register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, FALSE
);
6932 mono_register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, FALSE
);
6933 mono_register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, FALSE
);
6934 mono_register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, FALSE
);
6935 mono_register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, FALSE
);
6936 mono_register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, FALSE
);
6937 mono_register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, FALSE
);
6938 #if SIZEOF_VOID_P == 4
6939 mono_register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, FALSE
);
6942 mono_register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, FALSE
);
6943 mono_register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, FALSE
);
6944 mono_register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, FALSE
);
6945 mono_register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, FALSE
);
6946 mono_register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, FALSE
);
6947 mono_register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, FALSE
);
6948 mono_register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, FALSE
);
6949 mono_register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, FALSE
);
6950 mono_register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, FALSE
);
6951 mono_register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, FALSE
);
6953 mono_register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, FALSE
);
6954 mono_register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, FALSE
);
6955 mono_register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, FALSE
);
6956 mono_register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, FALSE
);
6957 mono_register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, FALSE
);
6959 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
6960 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
6961 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
6962 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
6965 #ifdef COMPRESSED_INTERFACE_BITMAP
6966 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
6969 #if SIZEOF_REGISTER == 4
6970 mono_register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, TRUE
);
6973 /* other jit icalls */
6974 register_icall (mono_delegate_ctor
, "mono_delegate_ctor", "void object object ptr", FALSE
);
6975 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
6976 "ptr ptr ptr", FALSE
);
6977 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
6978 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
6979 "ptr ptr ptr ptr", FALSE
);
6980 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
6981 register_icall (mono_ldstr
, "mono_ldstr", "object ptr ptr int32", FALSE
);
6982 register_icall (mono_helper_stelem_ref_check
, "helper_stelem_ref_check", "void object object", FALSE
);
6983 register_icall (mono_object_new
, "mono_object_new", "object ptr ptr", FALSE
);
6984 register_icall (mono_object_new_specific
, "mono_object_new_specific", "object ptr", FALSE
);
6985 register_icall (mono_array_new
, "mono_array_new", "object ptr ptr int32", FALSE
);
6986 register_icall (mono_array_new_specific
, "mono_array_new_specific", "object ptr int32", FALSE
);
6987 register_icall (mono_runtime_class_init
, "mono_runtime_class_init", "void ptr", FALSE
);
6988 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
6989 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
6990 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
6991 register_icall (mono_helper_compile_generic_method
, "compile_generic_method", "ptr object ptr ptr", FALSE
);
6992 register_icall (mono_helper_ldstr
, "helper_ldstr", "object ptr int", FALSE
);
6993 register_icall (mono_helper_ldstr_mscorlib
, "helper_ldstr_mscorlib", "object int", FALSE
);
6994 register_icall (mono_helper_newobj_mscorlib
, "helper_newobj_mscorlib", "object int", FALSE
);
6995 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
6996 register_icall (mono_object_castclass
, "mono_object_castclass", "object object ptr", FALSE
);
6997 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
6998 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
6999 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
7000 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
7001 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
7002 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
7003 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
7004 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
7005 register_icall (mono_resume_unwind
, "mono_resume_unwind", "void", TRUE
);
7007 register_icall (mono_gc_wbarrier_value_copy_bitmap
, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE
);
7009 register_icall (mono_object_castclass_with_cache
, "mono_object_castclass_with_cache", "object object ptr ptr", FALSE
);
7010 register_icall (mono_object_isinst_with_cache
, "mono_object_isinst_with_cache", "object object ptr ptr", FALSE
);
7012 register_icall (mono_debugger_agent_user_break
, "mono_debugger_agent_user_break", "void", FALSE
);
7016 mono_generic_sharing_init ();
7018 #ifdef MONO_ARCH_SIMD_INTRINSICS
7019 mono_simd_intrinsics_init ();
7022 #if MONO_SUPPORT_TASKLETS
7023 mono_tasklets_init ();
7026 if (mono_compile_aot
)
7028 * Avoid running managed code when AOT compiling, since the platform
7029 * might only support aot-only execution.
7031 mono_runtime_set_no_exec (TRUE
);
7033 #define JIT_RUNTIME_WORKS
7034 #ifdef JIT_RUNTIME_WORKS
7035 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
7036 mono_runtime_init (domain
, mono_thread_start_cb
, mono_thread_attach_cb
);
7037 mono_thread_attach (domain
);
7040 mono_profiler_runtime_initialized ();
7042 MONO_VES_INIT_END ();
7047 MonoJitStats mono_jit_stats
= {0};
7050 print_jit_stats (void)
7052 if (mono_jit_stats
.enabled
) {
7053 g_print ("Mono Jit statistics\n");
7054 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
7055 mono_jit_stats
.max_ratio_method
);
7056 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
7057 mono_jit_stats
.biggest_method
);
7059 g_print ("\nCreated object count: %ld\n", mono_stats
.new_object_count
);
7060 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
7061 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
7062 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
7063 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
7064 g_print ("Methods: %ld\n", mono_stats
.method_count
);
7065 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
7066 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
7067 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
7069 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
7070 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
7071 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
7073 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
7074 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
7075 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
7076 g_print ("Shared vtype generic methods: %ld\n", mono_stats
.gsharedvt_methods
);
7078 g_print ("Dynamic code allocs: %ld\n", mono_stats
.dynamic_code_alloc_count
);
7079 g_print ("Dynamic code bytes: %ld\n", mono_stats
.dynamic_code_bytes_count
);
7080 g_print ("Dynamic code frees: %ld\n", mono_stats
.dynamic_code_frees_count
);
7082 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
7083 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
7084 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
7085 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
7086 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
7087 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
7088 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
7089 g_print ("IMT thunks size: %ld\n", mono_stats
.imt_thunks_size
);
7091 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
7092 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
7093 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
7095 g_print ("Hazardous pointers: %ld\n", mono_stats
.hazardous_pointer_count
);
7096 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS
) {
7097 g_print ("\nDecl security check : %ld\n", mono_jit_stats
.cas_declsec_check
);
7098 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats
.cas_linkdemand
);
7099 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats
.cas_linkdemand_icall
);
7100 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats
.cas_linkdemand_pinvoke
);
7101 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats
.cas_linkdemand_aptc
);
7102 g_print ("Demand (code gen) : %ld\n", mono_jit_stats
.cas_demand_generation
);
7105 g_free (mono_jit_stats
.max_ratio_method
);
7106 mono_jit_stats
.max_ratio_method
= NULL
;
7107 g_free (mono_jit_stats
.biggest_method
);
7108 mono_jit_stats
.biggest_method
= NULL
;
7113 mini_cleanup (MonoDomain
*domain
)
7115 mono_runtime_shutdown_stat_profiler ();
7118 cominterop_release_all_rcws ();
7121 #ifndef MONO_CROSS_COMPILE
7122 mono_runtime_shutdown ();
7124 * mono_runtime_cleanup() and mono_domain_finalize () need to
7125 * be called early since they need the execution engine still
7126 * fully working (mono_domain_finalize may invoke managed finalizers
7127 * and mono_runtime_cleanup will wait for other threads to finish).
7129 mono_domain_finalize (domain
, 2000);
7132 /* This accesses metadata so needs to be called before runtime shutdown */
7135 mono_profiler_shutdown ();
7137 #ifndef MONO_CROSS_COMPILE
7138 mono_runtime_cleanup (domain
);
7141 free_jit_tls_data (mono_native_tls_get_value (mono_jit_tls_id
));
7143 mono_icall_cleanup ();
7145 mono_runtime_cleanup_handlers ();
7147 mono_domain_free (domain
, TRUE
);
7149 mono_debugger_cleanup ();
7153 mono_llvm_cleanup ();
7156 mono_aot_cleanup ();
7158 mono_trampolines_cleanup ();
7160 mono_unwind_cleanup ();
7162 if (!mono_dont_free_global_codeman
)
7163 mono_code_manager_destroy (global_codeman
);
7164 g_hash_table_destroy (jit_icall_name_hash
);
7165 g_free (emul_opcode_map
);
7166 g_free (emul_opcode_opcodes
);
7167 g_free (vtable_trampolines
);
7169 mono_arch_cleanup ();
7171 mono_generic_sharing_cleanup ();
7175 mono_trace_cleanup ();
7177 mono_counters_dump (-1, stdout
);
7179 if (mono_inject_async_exc_method
)
7180 mono_method_desc_free (mono_inject_async_exc_method
);
7182 mono_native_tls_free (mono_jit_tls_id
);
7184 DeleteCriticalSection (&jit_mutex
);
7186 DeleteCriticalSection (&mono_delegate_section
);
7190 mono_set_defaults (int verbose_level
, guint32 opts
)
7192 mini_verbose
= verbose_level
;
7193 mono_set_optimizations (opts
);
7197 mono_disable_optimizations (guint32 opts
)
7199 default_opt
&= ~opts
;
7203 mono_set_optimizations (guint32 opts
)
7206 default_opt_set
= TRUE
;
7207 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
7208 mono_set_generic_sharing_vt_supported (mono_aot_only
|| ((default_opt
& MONO_OPT_GSHAREDVT
) != 0));
7213 mono_set_verbose_level (guint32 level
)
7215 mini_verbose
= level
;
7219 * mono_get_runtime_build_info:
7221 * Return the runtime version + build date in string format.
7222 * The returned string is owned by the caller. The returned string
7223 * format is "VERSION (FULL_VERSION BUILD_DATE)" and build date is optional.
7226 mono_get_runtime_build_info (void)
7228 if (mono_build_date
)
7229 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
7231 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
7235 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
7237 GHashTable
*assemblies
= (GHashTable
*)user_data
;
7238 MonoImage
*image
= mono_assembly_get_image (ass
);
7239 MonoMethod
*method
, *invoke
;
7242 if (g_hash_table_lookup (assemblies
, ass
))
7245 g_hash_table_insert (assemblies
, ass
, ass
);
7247 if (mini_verbose
> 0)
7248 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
7250 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
7251 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
7252 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
7256 if (mini_verbose
> 1) {
7257 char * desc
= mono_method_full_name (method
, TRUE
);
7258 g_print ("Compiling %d %s\n", count
, desc
);
7261 mono_compile_method (method
);
7262 if (strcmp (method
->name
, "Finalize") == 0) {
7263 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
7264 mono_compile_method (invoke
);
7266 if (method
->klass
->marshalbyref
&& mono_method_signature (method
)->hasthis
) {
7267 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
7268 mono_compile_method (invoke
);
7272 /* Load and precompile referenced assemblies as well */
7273 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
7274 mono_assembly_load_reference (image
, i
);
7275 if (image
->references
[i
])
7276 mono_precompile_assembly (image
->references
[i
], assemblies
);
7280 void mono_precompile_assemblies ()
7282 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
7284 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
7286 g_hash_table_destroy (assemblies
);
7292 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
) {
7293 return mono_arch_instrument_epilog_full (cfg
, func
, p
, enable_arguments
, FALSE
);
7297 mono_cfg_add_try_hole (MonoCompile
*cfg
, MonoExceptionClause
*clause
, guint8
*start
, MonoBasicBlock
*bb
)
7299 TryBlockHole
*hole
= mono_mempool_alloc (cfg
->mempool
, sizeof (TryBlockHole
));
7300 hole
->clause
= clause
;
7301 hole
->start_offset
= start
- cfg
->native_code
;
7302 hole
->basic_block
= bb
;
7304 cfg
->try_block_holes
= g_slist_append_mempool (cfg
->mempool
, cfg
->try_block_holes
, hole
);
7308 mono_cfg_set_exception (MonoCompile
*cfg
, int type
)
7310 cfg
->exception_type
= type
;
7315 /* Dummy versions of some arch specific functions to avoid ifdefs at call sites */
7317 #ifndef MONO_ARCH_GSHAREDVT_SUPPORTED
7320 mono_arch_gsharedvt_sig_supported (MonoMethodSignature
*sig
)
7326 mono_arch_get_gsharedvt_call_info (gpointer addr
, MonoMethod
*normal_method
, MonoMethod
*gsharedvt_method
, MonoGenericSharingContext
*gsctx
, gboolean gsharedvt_in
, gboolean
virtual)
7328 g_assert_not_reached ();
7333 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain
*domain
, gpointer arg
, gpointer addr
)
7335 g_assert_not_reached ();
7340 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo
**info
, gboolean aot
)
7342 g_assert_not_reached ();
7351 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain
*domain
, gpointer arg
, gpointer addr
)
7353 g_assert_not_reached ();
7358 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo
**info
, gboolean aot
)
7360 g_assert_not_reached ();