2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
17 #ifdef HAVE_SYS_TIME_H
21 #ifdef PLATFORM_MACOSX
22 #include <mach/mach.h>
23 #include <mach/mach_error.h>
24 #include <mach/exception.h>
25 #include <mach/task.h>
29 #ifdef HAVE_VALGRIND_MEMCHECK_H
30 #include <valgrind/memcheck.h>
33 #include <mono/metadata/assembly.h>
34 #include <mono/metadata/loader.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/class.h>
37 #include <mono/metadata/object.h>
38 #include <mono/metadata/exception.h>
39 #include <mono/metadata/opcodes.h>
40 #include <mono/metadata/mono-endian.h>
41 #include <mono/metadata/tokentype.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/threads.h>
44 #include <mono/metadata/marshal.h>
45 #include <mono/metadata/socket-io.h>
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/debug-helpers.h>
48 #include <mono/io-layer/io-layer.h>
49 #include "mono/metadata/profiler.h"
50 #include <mono/metadata/profiler-private.h>
51 #include <mono/metadata/mono-config.h>
52 #include <mono/metadata/environment.h>
53 #include <mono/metadata/mono-debug.h>
54 #include <mono/metadata/monitor.h>
55 #include <mono/metadata/gc-internal.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/verify.h>
60 #include <mono/metadata/verify-internals.h>
61 #include <mono/metadata/mempool-internals.h>
62 #include <mono/metadata/attach.h>
63 #include <mono/utils/mono-math.h>
64 #include <mono/utils/mono-compiler.h>
65 #include <mono/utils/mono-counters.h>
66 #include <mono/utils/mono-logger.h>
67 #include <mono/utils/mono-mmap.h>
68 #include <mono/utils/dtrace.h>
76 #include "jit-icalls.h"
80 #include "debug-mini.h"
82 #define BRANCH_COST 100
83 #define INLINE_LENGTH_LIMIT 20
84 #define INLINE_FAILURE do {\
85 if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
88 #define CHECK_CFG_EXCEPTION do {\
89 if (cfg->exception_type != MONO_EXCEPTION_NONE)\
92 #define METHOD_ACCESS_FAILURE do { \
93 char *method_fname = mono_method_full_name (method, TRUE); \
94 char *cil_method_fname = mono_method_full_name (cil_method, TRUE); \
95 cfg->exception_type = MONO_EXCEPTION_METHOD_ACCESS; \
96 cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname); \
97 g_free (method_fname); \
98 g_free (cil_method_fname); \
99 goto exception_exit; \
101 #define FIELD_ACCESS_FAILURE do { \
102 char *method_fname = mono_method_full_name (method, TRUE); \
103 char *field_fname = mono_field_full_name (field); \
104 cfg->exception_type = MONO_EXCEPTION_FIELD_ACCESS; \
105 cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname); \
106 g_free (method_fname); \
107 g_free (field_fname); \
108 goto exception_exit; \
110 #define GENERIC_SHARING_FAILURE(opcode) do { \
111 if (cfg->generic_sharing_context) { \
112 if (cfg->verbose_level > 1) \
113 printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", method->klass->name_space, method->klass->name, method->name, method->signature->param_count, mono_opcode_name ((opcode)), __LINE__); \
114 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED; \
115 goto exception_exit; \
118 #define GET_RGCTX(rgctx, context_used) do { \
119 MonoInst *this = NULL; \
120 g_assert (context_used); \
121 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) && \
122 !((context_used) & MONO_GENERIC_CONTEXT_USED_METHOD) && \
123 !method->klass->valuetype) \
124 NEW_ARGLOAD (cfg, this, 0); \
125 (rgctx) = get_runtime_generic_context (cfg, method, (context_used), this, ip); \
129 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
131 static void setup_stat_profiler (void);
132 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
133 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
134 static gpointer mono_jit_compile_method (MonoMethod *method);
135 inline static int mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip);
137 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
138 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
140 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
142 int mono_method_to_ir2 (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
143 MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
144 guint inline_offset, gboolean is_virtual_call);
146 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
147 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
148 guint inline_offset, gboolean is_virtual_call);
150 #ifdef MONO_ARCH_SOFT_FLOAT
152 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip);
155 /* helper methods signature */
156 /* FIXME: Make these static again */
157 MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
158 MonoMethodSignature *helper_sig_domain_get = NULL;
159 MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
160 MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
162 static guint32 default_opt = 0;
163 static gboolean default_opt_set = FALSE;
165 guint32 mono_jit_tls_id = -1;
167 #ifdef HAVE_KW_THREAD
168 static __thread gpointer mono_jit_tls MONO_TLS_FAST;
171 MonoTraceSpec *mono_jit_trace_calls = NULL;
172 gboolean mono_break_on_exc = FALSE;
173 gboolean mono_compile_aot = FALSE;
174 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
175 gboolean mono_aot_only = FALSE;
176 /* Whenever to use IMT */
177 #ifdef MONO_ARCH_HAVE_IMT
178 gboolean mono_use_imt = TRUE;
180 gboolean mono_use_imt = FALSE;
182 MonoMethodDesc *mono_inject_async_exc_method = NULL;
183 int mono_inject_async_exc_pos;
184 MonoMethodDesc *mono_break_at_bb_method = NULL;
185 int mono_break_at_bb_bb_num;
186 gboolean mono_do_x86_stack_align = TRUE;
188 static int mini_verbose = 0;
190 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
191 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
192 static CRITICAL_SECTION jit_mutex;
194 static MonoCodeManager *global_codeman = NULL;
196 /* FIXME: Make this static again */
197 GHashTable *jit_icall_name_hash = NULL;
199 static MonoDebugOptions debug_options;
201 #ifdef VALGRIND_JIT_REGISTER_MAP
202 static int valgrind_register = 0;
206 * Table written to by the debugger with a 1-based index into the
207 * mono_breakpoint_info table, which contains changes made to
208 * the JIT instructions by the debugger.
211 mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
213 /* Whenever to check for pending exceptions in managed-to-native wrappers */
214 gboolean check_for_pending_exc = TRUE;
216 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
217 gboolean disable_vtypes_in_regs = FALSE;
219 gboolean mono_dont_free_global_codeman;
222 /* Define this here, since many files reference it */
223 const guint8 mono_burg_arity [MBMAX_OPCODES] = {
228 mono_running_on_valgrind (void)
230 #ifdef HAVE_VALGRIND_MEMCHECK_H
231 if (RUNNING_ON_VALGRIND){
232 #ifdef VALGRIND_JIT_REGISTER_MAP
233 valgrind_register = TRUE;
249 find_tramp (gpointer key, gpointer value, gpointer user_data)
251 FindTrampUserData *ud = (FindTrampUserData*)user_data;
254 ud->method = (MonoMethod*)key;
258 G_GNUC_UNUSED static char*
259 get_method_from_ip (void *ip)
264 MonoDomain *domain = mono_domain_get ();
265 MonoDebugSourceLocation *location;
266 FindTrampUserData user_data;
268 ji = mono_jit_info_table_find (domain, ip);
271 user_data.method = NULL;
272 mono_domain_lock (domain);
273 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
274 mono_domain_unlock (domain);
275 if (user_data.method) {
276 char *mname = mono_method_full_name (user_data.method, TRUE);
277 res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
284 method = mono_method_full_name (ji->method, TRUE);
285 /* FIXME: unused ? */
286 location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
288 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);
290 mono_debug_free_source_location (location);
298 * @ip: an instruction pointer address
300 * This method is used from a debugger to get the name of the
301 * method at address @ip. This routine is typically invoked from
302 * a debugger like this:
304 * (gdb) print mono_pmip ($pc)
306 * Returns: the name of the method at address @ip.
311 return get_method_from_ip (ip);
315 * mono_print_method_from_ip
316 * @ip: an instruction pointer address
318 * This method is used from a debugger to get the name of the
319 * method at address @ip.
321 * This prints the name of the method at address @ip in the standard
322 * output. Unlike mono_pmip which returns a string, this routine
323 * prints the value on the standard output.
326 mono_print_method_from_ip (void *ip)
330 MonoDebugSourceLocation *source;
331 MonoDomain *domain = mono_domain_get ();
332 FindTrampUserData user_data;
334 ji = mono_jit_info_table_find (domain, ip);
337 user_data.method = NULL;
338 mono_domain_lock (domain);
339 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
340 mono_domain_unlock (domain);
341 if (user_data.method) {
342 char *mname = mono_method_full_name (user_data.method, TRUE);
343 printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
347 g_print ("No method at %p\n", ip);
350 method = mono_method_full_name (ji->method, TRUE);
351 source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
353 g_print ("IP %p at offset 0x%x of method %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), method, ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
356 g_print ("%s:%d\n", source->source_file, source->row);
358 mono_debug_free_source_location (source);
363 * mono_method_same_domain:
365 * Determine whenever two compiled methods are in the same domain, thus
366 * the address of the callee can be embedded in the caller.
368 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
370 if (!caller || !callee)
374 * If the call was made from domain-neutral to domain-specific
375 * code, we can't patch the call site.
377 if (caller->domain_neutral && !callee->domain_neutral)
380 if ((caller->method->klass == mono_defaults.appdomain_class) &&
381 (strstr (caller->method->name, "InvokeInDomain"))) {
382 /* The InvokeInDomain methods change the current appdomain */
390 * mono_global_codeman_reserve:
392 * Allocate code memory from the global code manager.
394 void *mono_global_codeman_reserve (int size)
399 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
401 if (!global_codeman) {
402 /* This can happen during startup */
403 global_codeman = mono_code_manager_new ();
404 return mono_code_manager_reserve (global_codeman, size);
408 ptr = mono_code_manager_reserve (global_codeman, size);
415 mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context)
417 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
420 res->has_context = context != NULL;
422 memcpy (&res->context, context, sizeof (MonoGenericContext));
428 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
430 return mono_jump_info_token_new2 (mp, image, token, NULL);
433 #define MONO_INIT_VARINFO(vi,id) do { \
434 (vi)->range.first_use.pos.bid = 0xffff; \
439 //#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
440 #define UNVERIFIED do { if (debug_options.break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
443 * Basic blocks have two numeric identifiers:
444 * dfn: Depth First Number
445 * block_num: unique ID assigned at bblock creation
447 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
448 #define ADD_BBLOCK(cfg,b) do { \
449 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
450 (b)->block_num = cfg->num_bblocks++; \
451 (b)->real_offset = real_offset; \
454 #define GET_BBLOCK(cfg,tblock,ip) do { \
455 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
457 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
458 (tblock) = NEW_BBLOCK (cfg); \
459 (tblock)->cil_code = (ip); \
460 ADD_BBLOCK (cfg, (tblock)); \
464 #define CHECK_BBLOCK(target,ip,tblock) do { \
465 if ((target) < (ip) && !(tblock)->code) { \
466 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
467 if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (int)((target) - header->code), (int)((ip) - header->code)); \
471 #define NEW_ICONST(cfg,dest,val) do { \
472 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
473 (dest)->opcode = OP_ICONST; \
474 (dest)->inst_c0 = (val); \
475 (dest)->type = STACK_I4; \
478 #define NEW_PCONST(cfg,dest,val) do { \
479 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
480 (dest)->inst_p0 = (val); \
481 (dest)->type = STACK_PTR; \
485 #ifdef MONO_ARCH_NEED_GOT_VAR
487 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
488 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
489 (dest)->inst_left = (gpointer)(el1); \
490 (dest)->inst_right = (gpointer)(el2); \
493 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
494 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
495 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
496 if (cfg->compile_aot) { \
497 MonoInst *group, *got_var, *got_loc; \
498 got_loc = mono_get_got_var (cfg); \
499 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
500 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
501 (dest)->inst_p0 = got_var; \
502 (dest)->inst_p1 = group; \
504 (dest)->inst_p0 = (cons); \
505 (dest)->inst_i1 = (gpointer)(patch_type); \
507 (dest)->type = STACK_PTR; \
510 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
511 MonoInst *group, *got_var, *got_loc; \
512 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
513 got_loc = mono_get_got_var (cfg); \
514 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
515 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
516 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
517 (dest)->inst_p0 = got_var; \
518 (dest)->inst_p1 = group; \
519 (dest)->type = (stack_type); \
520 (dest)->klass = (stack_class); \
525 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
526 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
527 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
528 (dest)->inst_p0 = (cons); \
529 (dest)->inst_i1 = (gpointer)(patch_type); \
530 (dest)->type = STACK_PTR; \
533 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
534 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
535 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
536 (dest)->inst_p1 = (gpointer)(patch_type); \
537 (dest)->type = (stack_type); \
538 (dest)->klass = (stack_class); \
543 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
545 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
547 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
549 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
551 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
553 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
555 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
557 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
559 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
561 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
562 if (cfg->compile_aot) { \
563 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
565 NEW_PCONST (cfg, args [0], (entry).blob); \
569 #define NEW_DOMAINCONST(cfg,dest) do { \
570 if (cfg->opt & MONO_OPT_SHARED) { \
571 /* avoid depending on undefined C behavior in sequence points */ \
572 MonoInst* __domain_var = mono_get_domainvar (cfg); \
573 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
575 NEW_PCONST (cfg, dest, (cfg)->domain); \
579 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
581 #define NEW_ARGLOAD(cfg,dest,num) do { \
582 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
583 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
584 (dest)->ssa_op = MONO_SSA_LOAD; \
585 (dest)->inst_i0 = arg_array [(num)]; \
586 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
587 type_to_eval_stack_type ((cfg), param_types [(num)], (dest)); \
588 (dest)->klass = (dest)->inst_i0->klass; \
591 #define NEW_LOCLOAD(cfg,dest,num) do { \
592 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
593 (dest)->ssa_op = MONO_SSA_LOAD; \
594 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
595 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
596 type_to_eval_stack_type ((cfg), header->locals [(num)], (dest)); \
597 (dest)->klass = (dest)->inst_i0->klass; \
600 #define NEW_LOCLOADA(cfg,dest,num) do { \
601 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
602 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
603 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
604 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
605 (dest)->type = STACK_MP; \
606 (dest)->klass = (dest)->inst_i0->klass; \
607 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
608 (cfg)->disable_ssa = TRUE; \
611 #define NEW_RETLOADA(cfg,dest) do { \
612 if (cfg->vret_addr) { \
613 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
614 (dest)->ssa_op = MONO_SSA_LOAD; \
615 (dest)->inst_i0 = cfg->vret_addr; \
616 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
617 (dest)->type = STACK_MP; \
618 (dest)->klass = (dest)->inst_i0->klass; \
620 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
621 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
622 (dest)->inst_i0 = (cfg)->ret; \
623 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
624 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
625 (dest)->type = STACK_MP; \
626 (dest)->klass = (dest)->inst_i0->klass; \
627 (cfg)->disable_ssa = TRUE; \
631 #define NEW_ARGLOADA(cfg,dest,num) do { \
632 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
633 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
634 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
635 (dest)->inst_i0 = arg_array [(num)]; \
636 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
637 (dest)->type = STACK_MP; \
638 (dest)->klass = (dest)->inst_i0->klass; \
639 (cfg)->disable_ssa = TRUE; \
642 #define NEW_TEMPLOAD(cfg,dest,num) do { \
643 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
644 (dest)->ssa_op = MONO_SSA_LOAD; \
645 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
646 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
647 type_to_eval_stack_type ((cfg), (dest)->inst_i0->inst_vtype, (dest)); \
648 (dest)->klass = (dest)->inst_i0->klass; \
651 #define NEW_TEMPLOADA(cfg,dest,num) do { \
652 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
653 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
654 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
655 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
656 (dest)->type = STACK_MP; \
657 (dest)->klass = (dest)->inst_i0->klass; \
658 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
659 (cfg)->disable_ssa = TRUE; \
662 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
663 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
664 (dest)->inst_left = addr; \
665 (dest)->opcode = mini_type_to_ldind ((cfg), vtype); \
666 type_to_eval_stack_type ((cfg), vtype, (dest)); \
667 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
670 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
671 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
672 (dest)->inst_i0 = addr; \
673 (dest)->opcode = mini_type_to_stind ((cfg), vtype); \
674 (dest)->inst_i1 = (value); \
675 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
678 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
679 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
680 (dest)->ssa_op = MONO_SSA_STORE; \
681 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
682 (dest)->opcode = mini_type_to_stind ((cfg), (dest)->inst_i0->inst_vtype); \
683 (dest)->inst_i1 = (inst); \
684 (dest)->klass = (dest)->inst_i0->klass; \
687 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
688 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
689 (dest)->opcode = mini_type_to_stind ((cfg), header->locals [(num)]); \
690 (dest)->ssa_op = MONO_SSA_STORE; \
691 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
692 (dest)->inst_i1 = (inst); \
693 (dest)->klass = (dest)->inst_i0->klass; \
696 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
697 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
698 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
699 (dest)->opcode = mini_type_to_stind ((cfg), param_types [(num)]); \
700 (dest)->ssa_op = MONO_SSA_STORE; \
701 (dest)->inst_i0 = arg_array [(num)]; \
702 (dest)->inst_i1 = (inst); \
703 (dest)->klass = (dest)->inst_i0->klass; \
706 #define NEW_MEMCPY(cfg,dest,dst,src,memcpy_size,memcpy_align) do { \
707 MONO_INST_NEW (cfg, dest, OP_MEMCPY); \
708 (dest)->inst_left = (dst); \
709 (dest)->inst_right = (src); \
710 (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
711 (dest)->backend.memcpy_args->size = (memcpy_size); \
712 (dest)->backend.memcpy_args->align = (memcpy_align); \
715 #define NEW_MEMSET(cfg,dest,dst,imm,memcpy_size,memcpy_align) do { \
716 MONO_INST_NEW (cfg, dest, OP_MEMSET); \
717 (dest)->inst_left = (dst); \
718 (dest)->inst_imm = (imm); \
719 (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
720 (dest)->backend.memcpy_args->size = (memcpy_size); \
721 (dest)->backend.memcpy_args->align = (memcpy_align); \
724 #define NEW_DUMMY_USE(cfg,dest,load) do { \
725 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
726 (dest)->inst_left = (load); \
729 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
730 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_STORE); \
731 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
732 (dest)->klass = (dest)->inst_i0->klass; \
735 #define ADD_BINOP(op) do { \
736 MONO_INST_NEW (cfg, ins, (op)); \
738 ins->inst_i0 = sp [0]; \
739 ins->inst_i1 = sp [1]; \
741 type_from_op (ins); \
743 /* Have to insert a widening op */ \
744 /* FIXME: Need to add many more cases */ \
745 if (ins->inst_i0->type == STACK_PTR && ins->inst_i1->type == STACK_I4) { \
747 MONO_INST_NEW (cfg, widen, CEE_CONV_I); \
748 widen->inst_i0 = ins->inst_i1; \
749 ins->inst_i1 = widen; \
753 #define ADD_UNOP(op) do { \
754 MONO_INST_NEW (cfg, ins, (op)); \
756 ins->inst_i0 = sp [0]; \
758 type_from_op (ins); \
762 #define ADD_BINCOND(next_block) do { \
765 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
766 cmp->inst_i0 = sp [0]; \
767 cmp->inst_i1 = sp [1]; \
768 cmp->cil_code = ins->cil_code; \
769 type_from_op (cmp); \
771 ins->inst_i0 = cmp; \
772 MONO_ADD_INS (bblock, ins); \
773 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
774 GET_BBLOCK (cfg, tblock, target); \
775 link_bblock (cfg, bblock, tblock); \
776 ins->inst_true_bb = tblock; \
777 CHECK_BBLOCK (target, ip, tblock); \
778 if ((next_block)) { \
779 link_bblock (cfg, bblock, (next_block)); \
780 ins->inst_false_bb = (next_block); \
781 start_new_bblock = 1; \
783 GET_BBLOCK (cfg, tblock, ip); \
784 link_bblock (cfg, bblock, tblock); \
785 ins->inst_false_bb = tblock; \
786 start_new_bblock = 2; \
790 /* FIXME: handle float, long ... */
791 #define ADD_UNCOND(istrue) do { \
794 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
795 cmp->inst_i0 = sp [0]; \
796 switch (cmp->inst_i0->type) { \
798 cmp->inst_i1 = zero_int64; break; \
800 cmp->inst_i1 = zero_r8; break; \
803 cmp->inst_i1 = zero_ptr; break; \
805 cmp->inst_i1 = zero_obj; break; \
807 cmp->inst_i1 = zero_int32; \
809 cmp->cil_code = ins->cil_code; \
810 type_from_op (cmp); \
812 ins->inst_i0 = cmp; \
813 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
814 MONO_ADD_INS (bblock, ins); \
815 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
816 GET_BBLOCK (cfg, tblock, target); \
817 link_bblock (cfg, bblock, tblock); \
818 ins->inst_true_bb = tblock; \
819 CHECK_BBLOCK (target, ip, tblock); \
820 GET_BBLOCK (cfg, tblock, ip); \
821 link_bblock (cfg, bblock, tblock); \
822 ins->inst_false_bb = tblock; \
823 start_new_bblock = 2; \
826 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
827 MONO_INST_NEW ((cfg), (dest), CEE_LDELEMA); \
828 (dest)->inst_left = (sp) [0]; \
829 (dest)->inst_right = (sp) [1]; \
830 (dest)->type = STACK_MP; \
831 (dest)->klass = (k); \
832 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
835 #define NEW_GROUP(cfg,dest,el1,el2) do { \
836 MONO_INST_NEW ((cfg), (dest), OP_GROUP); \
837 (dest)->inst_left = (el1); \
838 (dest)->inst_right = (el2); \
843 compare_bblock (gconstpointer a, gconstpointer b)
845 const MonoBasicBlock *b1 = a;
846 const MonoBasicBlock *b2 = b;
848 return b2->cil_code - b1->cil_code;
853 * link_bblock: Links two basic blocks
855 * links two basic blocks in the control flow graph, the 'from'
856 * argument is the starting block and the 'to' argument is the block
857 * the control flow ends to after 'from'.
860 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
862 MonoBasicBlock **newa;
866 if (from->cil_code) {
868 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
870 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
873 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
875 g_print ("edge from entry to exit\n");
879 for (i = 0; i < from->out_count; ++i) {
880 if (to == from->out_bb [i]) {
886 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
887 for (i = 0; i < from->out_count; ++i) {
888 newa [i] = from->out_bb [i];
896 for (i = 0; i < to->in_count; ++i) {
897 if (from == to->in_bb [i]) {
903 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
904 for (i = 0; i < to->in_count; ++i) {
905 newa [i] = to->in_bb [i];
914 * mono_unlink_bblock:
916 * Unlink two basic blocks.
919 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
925 for (i = 0; i < from->out_count; ++i) {
926 if (to == from->out_bb [i]) {
933 for (i = 0; i < from->out_count; ++i) {
934 if (from->out_bb [i] != to)
935 from->out_bb [pos ++] = from->out_bb [i];
937 g_assert (pos == from->out_count - 1);
942 for (i = 0; i < to->in_count; ++i) {
943 if (from == to->in_bb [i]) {
950 for (i = 0; i < to->in_count; ++i) {
951 if (to->in_bb [i] != from)
952 to->in_bb [pos ++] = to->in_bb [i];
954 g_assert (pos == to->in_count - 1);
960 * mono_bblocks_linked:
962 * Return whenever BB1 and BB2 are linked in the CFG.
965 mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
969 for (i = 0; i < bb1->out_count; ++i) {
970 if (bb1->out_bb [i] == bb2)
978 * mono_find_block_region:
980 * We mark each basic block with a region ID. We use that to avoid BB
981 * optimizations when blocks are in different regions.
984 * A region token that encodes where this region is, and information
985 * about the clause owner for this block.
987 * The region encodes the try/catch/filter clause that owns this block
988 * as well as the type. -1 is a special value that represents a block
989 * that is in none of try/catch/filter.
992 mono_find_block_region (MonoCompile *cfg, int offset)
994 MonoMethod *method = cfg->method;
995 MonoMethodHeader *header = mono_method_get_header (method);
996 MonoExceptionClause *clause;
999 /* first search for handlers and filters */
1000 for (i = 0; i < header->num_clauses; ++i) {
1001 clause = &header->clauses [i];
1002 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
1003 (offset < (clause->handler_offset)))
1004 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
1006 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
1007 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1008 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
1009 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
1010 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
1011 else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1012 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
1016 /* search the try blocks */
1017 for (i = 0; i < header->num_clauses; ++i) {
1018 clause = &header->clauses [i];
1019 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1020 return ((i + 1) << 8) | clause->flags;
1027 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
1029 MonoMethod *method = cfg->method;
1030 MonoMethodHeader *header = mono_method_get_header (method);
1031 MonoExceptionClause *clause;
1032 MonoBasicBlock *handler;
1036 for (i = 0; i < header->num_clauses; ++i) {
1037 clause = &header->clauses [i];
1038 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
1039 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
1040 if (clause->flags == type) {
1041 handler = cfg->cil_offset_to_bb [clause->handler_offset];
1043 res = g_list_append (res, handler);
1051 mono_find_spvar_for_region (MonoCompile *cfg, int region)
1053 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1057 mono_create_spvar_for_region (MonoCompile *cfg, int region)
1061 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1065 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1066 /* prevent it from being register allocated */
1067 var->flags |= MONO_INST_INDIRECT;
1069 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
1073 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
1075 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1079 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
1083 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1087 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
1088 /* prevent it from being register allocated */
1089 var->flags |= MONO_INST_INDIRECT;
1091 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
1097 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
1101 array [*dfn] = start;
1102 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
1103 for (i = 0; i < start->out_count; ++i) {
1104 if (start->out_bb [i]->dfn)
1107 start->out_bb [i]->dfn = *dfn;
1108 start->out_bb [i]->df_parent = start;
1109 array [*dfn] = start->out_bb [i];
1110 df_visit (start->out_bb [i], dfn, array);
1114 static MonoBasicBlock*
1115 find_previous (MonoBasicBlock **bblocks, guint32 n_bblocks, MonoBasicBlock *start, const guchar *code)
1117 MonoBasicBlock *best = start;
1120 for (i = 0; i < n_bblocks; ++i) {
1122 MonoBasicBlock *bb = bblocks [i];
1124 if (bb->cil_code && bb->cil_code < code && bb->cil_code > best->cil_code)
1133 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
1142 * FIXME: take into account all the details:
1143 * second may have been the target of more than one bblock
1145 second->out_count = first->out_count;
1146 second->out_bb = first->out_bb;
1148 for (i = 0; i < first->out_count; ++i) {
1149 bb = first->out_bb [i];
1150 for (j = 0; j < bb->in_count; ++j) {
1151 if (bb->in_bb [j] == first)
1152 bb->in_bb [j] = second;
1156 first->out_count = 0;
1157 first->out_bb = NULL;
1158 link_bblock (cfg, first, second);
1160 second->last_ins = first->last_ins;
1162 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
1163 MONO_BB_FOR_EACH_INS (first, inst) {
1164 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
1165 g_print ("found %p: %s", inst->next->cil_code, code);
1167 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
1168 second->code = inst->next;
1170 first->last_ins = inst;
1171 second->next_bb = first->next_bb;
1172 first->next_bb = second;
1176 if (!second->code) {
1177 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
1183 mono_reverse_branch_op (guint32 opcode)
1185 static const int reverse_map [] = {
1186 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
1187 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
1189 static const int reverse_fmap [] = {
1190 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
1191 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
1193 static const int reverse_lmap [] = {
1194 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
1195 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
1197 static const int reverse_imap [] = {
1198 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
1199 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
1202 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
1203 opcode = reverse_map [opcode - CEE_BEQ];
1204 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
1205 opcode = reverse_fmap [opcode - OP_FBEQ];
1206 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
1207 opcode = reverse_lmap [opcode - OP_LBEQ];
1208 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
1209 opcode = reverse_imap [opcode - OP_IBEQ];
1211 g_assert_not_reached ();
1217 mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
1220 return OP_STORE_MEMBASE_REG;
1223 switch (type->type) {
1226 case MONO_TYPE_BOOLEAN:
1227 return OP_STOREI1_MEMBASE_REG;
1230 case MONO_TYPE_CHAR:
1231 return OP_STOREI2_MEMBASE_REG;
1234 return OP_STOREI4_MEMBASE_REG;
1238 case MONO_TYPE_FNPTR:
1239 return OP_STORE_MEMBASE_REG;
1240 case MONO_TYPE_CLASS:
1241 case MONO_TYPE_STRING:
1242 case MONO_TYPE_OBJECT:
1243 case MONO_TYPE_SZARRAY:
1244 case MONO_TYPE_ARRAY:
1245 return OP_STORE_MEMBASE_REG;
1248 return OP_STOREI8_MEMBASE_REG;
1250 return OP_STORER4_MEMBASE_REG;
1252 return OP_STORER8_MEMBASE_REG;
1253 case MONO_TYPE_VALUETYPE:
1254 if (type->data.klass->enumtype) {
1255 type = type->data.klass->enum_basetype;
1258 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
1259 return OP_STOREX_MEMBASE;
1260 return OP_STOREV_MEMBASE;
1261 case MONO_TYPE_TYPEDBYREF:
1262 return OP_STOREV_MEMBASE;
1263 case MONO_TYPE_GENERICINST:
1264 type = &type->data.generic_class->container_class->byval_arg;
1267 case MONO_TYPE_MVAR:
1268 /* FIXME: all the arguments must be references for now,
1269 * later look inside cfg and see if the arg num is
1270 * really a reference
1272 g_assert (cfg->generic_sharing_context);
1273 return OP_STORE_MEMBASE_REG;
1275 g_error ("unknown type 0x%02x in type_to_store_membase", type->type);
1281 mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
1284 return OP_LOAD_MEMBASE;
1286 switch (mono_type_get_underlying_type (type)->type) {
1288 return OP_LOADI1_MEMBASE;
1290 case MONO_TYPE_BOOLEAN:
1291 return OP_LOADU1_MEMBASE;
1293 return OP_LOADI2_MEMBASE;
1295 case MONO_TYPE_CHAR:
1296 return OP_LOADU2_MEMBASE;
1298 return OP_LOADI4_MEMBASE;
1300 return OP_LOADU4_MEMBASE;
1304 case MONO_TYPE_FNPTR:
1305 return OP_LOAD_MEMBASE;
1306 case MONO_TYPE_CLASS:
1307 case MONO_TYPE_STRING:
1308 case MONO_TYPE_OBJECT:
1309 case MONO_TYPE_SZARRAY:
1310 case MONO_TYPE_ARRAY:
1311 return OP_LOAD_MEMBASE;
1314 return OP_LOADI8_MEMBASE;
1316 return OP_LOADR4_MEMBASE;
1318 return OP_LOADR8_MEMBASE;
1319 case MONO_TYPE_VALUETYPE:
1320 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
1321 return OP_LOADX_MEMBASE;
1322 case MONO_TYPE_TYPEDBYREF:
1323 return OP_LOADV_MEMBASE;
1324 case MONO_TYPE_GENERICINST:
1325 if (mono_type_generic_inst_is_valuetype (type))
1326 return OP_LOADV_MEMBASE;
1328 return OP_LOAD_MEMBASE;
1331 case MONO_TYPE_MVAR:
1332 /* FIXME: all the arguments must be references for now,
1333 * later look inside cfg and see if the arg num is
1334 * really a reference
1336 g_assert (cfg->generic_sharing_context);
1337 return OP_LOAD_MEMBASE;
1339 g_error ("unknown type 0x%02x in type_to_load_membase", type->type);
1344 #ifdef MONO_ARCH_SOFT_FLOAT
1346 condbr_to_fp_br (int opcode)
1349 case CEE_BEQ: return OP_FBEQ;
1350 case CEE_BGE: return OP_FBGE;
1351 case CEE_BGT: return OP_FBGT;
1352 case CEE_BLE: return OP_FBLE;
1353 case CEE_BLT: return OP_FBLT;
1354 case CEE_BNE_UN: return OP_FBNE_UN;
1355 case CEE_BGE_UN: return OP_FBGE_UN;
1356 case CEE_BGT_UN: return OP_FBGT_UN;
1357 case CEE_BLE_UN: return OP_FBLE_UN;
1358 case CEE_BLT_UN: return OP_FBLT_UN;
1360 g_assert_not_reached ();
1366 * The following tables are used to quickly validate the IL code in type_from_op ().
1369 bin_num_table [STACK_MAX] [STACK_MAX] = {
1370 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1371 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1372 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1373 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1374 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1375 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1376 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1377 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1382 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1385 /* reduce the size of this table */
1387 bin_int_table [STACK_MAX] [STACK_MAX] = {
1388 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1389 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1390 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1391 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1392 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1393 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1394 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1395 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1399 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1400 /* Inv i L p F & O vt */
1402 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
1403 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
1404 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
1405 {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
1406 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
1407 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
1408 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
1411 /* reduce the size of this table */
1413 shift_table [STACK_MAX] [STACK_MAX] = {
1414 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1415 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1416 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1417 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1418 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1419 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1420 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1421 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1425 * Tables to map from the non-specific opcode to the matching
1426 * type-specific opcode.
1428 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1429 static const guint16
1430 binops_op_map [STACK_MAX] = {
1431 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1434 /* handles from CEE_NEG to CEE_CONV_U8 */
1435 static const guint16
1436 unops_op_map [STACK_MAX] = {
1437 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1440 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1441 static const guint16
1442 ovfops_op_map [STACK_MAX] = {
1443 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2
1446 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1447 static const guint16
1448 ovf2ops_op_map [STACK_MAX] = {
1449 0, 0, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
1452 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1453 static const guint16
1454 ovf3ops_op_map [STACK_MAX] = {
1455 0, 0, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1
1458 /* handles from CEE_CEQ to CEE_CLT_UN */
1459 static const guint16
1460 ceqops_op_map [STACK_MAX] = {
1461 0, 0, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_LCEQ-OP_CEQ
1465 * Sets ins->type (the type on the eval stack) according to the
1466 * type of the opcode and the arguments to it.
1467 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1469 * FIXME: this function sets ins->type unconditionally in some cases, but
1470 * it should set it to invalid for some types (a conv.x on an object)
1473 type_from_op (MonoInst *ins) {
1474 switch (ins->opcode) {
1481 /* FIXME: check unverifiable args for STACK_MP */
1482 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1483 ins->opcode += binops_op_map [ins->type];
1490 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1491 ins->opcode += binops_op_map [ins->type];
1496 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1497 ins->opcode += binops_op_map [ins->type];
1501 /* FIXME: handle some specifics with ins->next->type */
1502 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1503 if ((ins->inst_i0->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((ins->inst_i0->type == STACK_PTR) || (ins->inst_i0->type == STACK_OBJ) || (ins->inst_i0->type == STACK_MP))))
1504 ins->opcode = OP_LCOMPARE;
1507 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1508 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1515 ins->type = (bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] & 1) ? STACK_I4: STACK_INV;
1516 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1520 ins->type = neg_table [ins->inst_i0->type];
1521 ins->opcode += unops_op_map [ins->type];
1524 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1525 ins->type = ins->inst_i0->type;
1527 ins->type = STACK_INV;
1528 ins->opcode += unops_op_map [ins->type];
1534 ins->type = STACK_I4;
1535 ins->opcode += unops_op_map [ins->inst_i0->type];
1538 ins->type = STACK_R8;
1539 switch (ins->inst_i0->type) {
1544 ins->opcode = OP_LCONV_TO_R_UN;
1548 case CEE_CONV_OVF_I1:
1549 case CEE_CONV_OVF_U1:
1550 case CEE_CONV_OVF_I2:
1551 case CEE_CONV_OVF_U2:
1552 case CEE_CONV_OVF_I4:
1553 case CEE_CONV_OVF_U4:
1554 ins->type = STACK_I4;
1555 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1557 case CEE_CONV_OVF_I_UN:
1558 case CEE_CONV_OVF_U_UN:
1559 ins->type = STACK_PTR;
1560 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1562 case CEE_CONV_OVF_I1_UN:
1563 case CEE_CONV_OVF_I2_UN:
1564 case CEE_CONV_OVF_I4_UN:
1565 case CEE_CONV_OVF_U1_UN:
1566 case CEE_CONV_OVF_U2_UN:
1567 case CEE_CONV_OVF_U4_UN:
1568 ins->type = STACK_I4;
1569 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1572 ins->type = STACK_PTR;
1573 switch (ins->inst_i0->type) {
1578 #if SIZEOF_VOID_P == 8
1579 ins->opcode = OP_LCONV_TO_U;
1583 ins->opcode = OP_LCONV_TO_U;
1586 ins->opcode = OP_FCONV_TO_U;
1592 ins->type = STACK_I8;
1593 ins->opcode += unops_op_map [ins->inst_i0->type];
1595 case CEE_CONV_OVF_I8:
1596 case CEE_CONV_OVF_U8:
1597 ins->type = STACK_I8;
1598 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1600 case CEE_CONV_OVF_U8_UN:
1601 case CEE_CONV_OVF_I8_UN:
1602 ins->type = STACK_I8;
1603 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1607 ins->type = STACK_R8;
1608 ins->opcode += unops_op_map [ins->inst_i0->type];
1611 ins->type = STACK_R8;
1615 ins->type = STACK_I4;
1616 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1619 case CEE_CONV_OVF_I:
1620 case CEE_CONV_OVF_U:
1621 ins->type = STACK_PTR;
1622 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1625 case CEE_ADD_OVF_UN:
1627 case CEE_MUL_OVF_UN:
1629 case CEE_SUB_OVF_UN:
1630 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1631 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1632 if (ins->type == STACK_R8)
1633 ins->type = STACK_INV;
1636 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1643 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1646 /* map ldelem.x to the matching ldind.x opcode */
1648 ldelem_to_ldind [] = {
1662 /* map stelem.x to the matching stind.x opcode */
1664 stelem_to_stind [] = {
1676 #ifdef MONO_ARCH_SOFT_FLOAT
1678 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip)
1680 MonoInst *iargs [2];
1684 mono_emit_jit_icall (cfg, bblock, mono_fstore_r4, iargs, ip);
1688 handle_load_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, const unsigned char *ip)
1690 MonoInst *iargs [1];
1693 return mono_emit_jit_icall (cfg, bblock, mono_fload_r4, iargs, ip);
1696 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1697 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) { \
1699 NEW_LOCLOADA (cfg, (ins), (idx)); \
1700 temp = handle_load_float (cfg, bblock, (ins), (ip)); \
1701 NEW_TEMPLOAD (cfg, (ins), temp); \
1704 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1705 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) { \
1706 NEW_LOCLOADA (cfg, (ins), (idx)); \
1707 handle_store_float (cfg, bblock, (ins), *sp, (ip)); \
1708 MONO_INST_NEW (cfg, (ins), OP_NOP); \
1711 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1712 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1714 NEW_ARGLOADA (cfg, (ins), (idx)); \
1715 temp = handle_load_float (cfg, bblock, (ins), (ip)); \
1716 NEW_TEMPLOAD (cfg, (ins), temp); \
1719 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1720 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1721 NEW_ARGLOADA (cfg, (ins), (idx)); \
1722 handle_store_float (cfg, bblock, (ins), *sp, (ip)); \
1723 MONO_INST_NEW (cfg, (ins), OP_NOP); \
1727 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip) do { \
1728 if ((ins)->opcode == CEE_LDIND_R4) { \
1731 NEW_TEMPLOADA (cfg, (ins), (idx)); \
1732 temp = handle_load_float (cfg, (bblock), (ins), ip); \
1733 NEW_TEMPLOAD (cfg, (ins), (temp)); \
1737 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip) do { \
1738 if ((ins)->opcode == CEE_STIND_R4) { \
1740 NEW_TEMPLOADA (cfg, (ins), (idx)); \
1741 handle_store_float ((cfg), (bblock), (ins), (val), (ip)); \
1747 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1748 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1749 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip)
1750 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip)
1751 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip)
1752 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip)
1758 param_table [STACK_MAX] [STACK_MAX] = {
1763 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig)
1768 switch (args->type) {
1778 for (i = 0; i < sig->param_count; ++i) {
1779 switch (args [i].type) {
1783 if (!sig->params [i]->byref)
1787 if (sig->params [i]->byref)
1789 switch (sig->params [i]->type) {
1790 case MONO_TYPE_CLASS:
1791 case MONO_TYPE_STRING:
1792 case MONO_TYPE_OBJECT:
1793 case MONO_TYPE_SZARRAY:
1794 case MONO_TYPE_ARRAY:
1801 if (sig->params [i]->byref)
1803 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1812 /*if (!param_table [args [i].type] [sig->params [i]->type])
1820 mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
1822 if (cfg->generic_sharing_context && !type->byref) {
1823 /* FIXME: all the arguments must be references for now,
1824 * later look inside cfg and see if the arg num is
1825 * really a reference
1827 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1828 return CEE_LDIND_REF;
1830 return mono_type_to_ldind (type);
1834 mini_type_to_stind (MonoCompile* cfg, MonoType *type)
1836 if (cfg->generic_sharing_context && !type->byref) {
1837 /* FIXME: all the arguments must be references for now,
1838 * later look inside cfg and see if the arg num is
1839 * really a reference
1841 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1842 return CEE_STIND_REF;
1844 return mono_type_to_stind (type);
1848 mono_op_imm_to_op (int opcode)
1852 #if SIZEOF_VOID_P == 4
1868 #if SIZEOF_VOID_P == 4
1893 case OP_ISHR_UN_IMM:
1895 case OP_LSHR_UN_IMM:
1899 case OP_IDIV_UN_IMM:
1901 case OP_IREM_UN_IMM:
1906 #if SIZEOF_VOID_P == 4
1912 #if SIZEOF_VOID_P == 4
1929 case OP_COMPARE_IMM:
1931 case OP_ICOMPARE_IMM:
1933 case OP_LOCALLOC_IMM:
1936 printf ("%s\n", mono_inst_name (opcode));
1937 g_assert_not_reached ();
1943 * mono_decompose_op_imm:
1945 * Replace the OP_.._IMM INS with its non IMM variant.
1948 mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
1952 MONO_INST_NEW (cfg, temp, OP_ICONST);
1953 temp->inst_c0 = ins->inst_imm;
1954 temp->dreg = mono_regstate_next_int (cfg->rs);
1955 mono_bblock_insert_before_ins (bb, ins, temp);
1956 ins->opcode = mono_op_imm_to_op (ins->opcode);
1957 if (ins->opcode == OP_LOCALLOC)
1958 ins->sreg1 = temp->dreg;
1960 ins->sreg2 = temp->dreg;
1962 bb->max_vreg = MAX (bb->max_vreg, cfg->rs->next_vreg);
1966 * When we need a pointer to the current domain many times in a method, we
1967 * call mono_domain_get() once and we store the result in a local variable.
1968 * This function returns the variable that represents the MonoDomain*.
1970 inline static MonoInst *
1971 mono_get_domainvar (MonoCompile *cfg)
1973 if (!cfg->domainvar)
1974 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1975 return cfg->domainvar;
1979 * The got_var contains the address of the Global Offset Table when AOT
1982 inline static MonoInst *
1983 mono_get_got_var (MonoCompile *cfg)
1985 #ifdef MONO_ARCH_NEED_GOT_VAR
1986 if (!cfg->compile_aot)
1988 if (!cfg->got_var) {
1989 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1991 return cfg->got_var;
1998 mono_get_vtable_var (MonoCompile *cfg)
2000 g_assert (cfg->generic_sharing_context);
2002 if (!cfg->rgctx_var) {
2003 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2004 /* force the var to be stack allocated */
2005 cfg->rgctx_var->flags |= MONO_INST_INDIRECT;
2008 return cfg->rgctx_var;
2012 set_vreg_to_inst (MonoCompile *cfg, int vreg, MonoInst *inst)
2014 if (vreg >= cfg->vreg_to_inst_len) {
2015 MonoInst **tmp = cfg->vreg_to_inst;
2016 int size = cfg->vreg_to_inst_len;
2018 while (vreg >= cfg->vreg_to_inst_len)
2019 cfg->vreg_to_inst_len = cfg->vreg_to_inst_len ? cfg->vreg_to_inst_len * 2 : 32;
2020 cfg->vreg_to_inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * cfg->vreg_to_inst_len);
2022 memcpy (cfg->vreg_to_inst, tmp, size * sizeof (MonoInst*));
2024 cfg->vreg_to_inst [vreg] = inst;
2027 #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)))
2028 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
2031 mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode, int vreg)
2034 int num = cfg->num_varinfo;
2037 if ((num + 1) >= cfg->varinfo_count) {
2038 int orig_count = cfg->varinfo_count;
2039 cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 64;
2040 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
2041 cfg->vars = (MonoMethodVar *)g_realloc (cfg->vars, sizeof (MonoMethodVar) * cfg->varinfo_count);
2042 memset (&cfg->vars [orig_count], 0, (cfg->varinfo_count - orig_count) * sizeof (MonoMethodVar));
2045 mono_jit_stats.allocate_var++;
2047 MONO_INST_NEW (cfg, inst, opcode);
2048 inst->inst_c0 = num;
2049 inst->inst_vtype = type;
2050 inst->klass = mono_class_from_mono_type (type);
2051 type_to_eval_stack_type (cfg, type, inst);
2052 /* if set to 1 the variable is native */
2053 inst->backend.is_pinvoke = 0;
2056 cfg->varinfo [num] = inst;
2058 MONO_INIT_VARINFO (&cfg->vars [num], num);
2061 set_vreg_to_inst (cfg, vreg, inst);
2063 #if SIZEOF_VOID_P == 4
2064 #ifdef MONO_ARCH_SOFT_FLOAT
2065 regpair = mono_type_is_long (type) || mono_type_is_float (type);
2067 regpair = mono_type_is_long (type);
2077 * These two cannot be allocated using create_var_for_vreg since that would
2078 * put it into the cfg->varinfo array, confusing many parts of the JIT.
2082 * Set flags to VOLATILE so SSA skips it.
2085 if (cfg->verbose_level >= 4) {
2086 printf (" Create LVAR R%d (R%d, R%d)\n", inst->dreg, inst->dreg + 1, inst->dreg + 2);
2089 #ifdef MONO_ARCH_SOFT_FLOAT
2090 if (cfg->opt & MONO_OPT_SSA) {
2091 if (mono_type_is_float (type))
2092 inst->flags = MONO_INST_VOLATILE;
2096 /* Allocate a dummy MonoInst for the first vreg */
2097 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2098 tree->dreg = inst->dreg + 1;
2099 if (cfg->opt & MONO_OPT_SSA)
2100 tree->flags = MONO_INST_VOLATILE;
2101 tree->inst_c0 = num;
2102 tree->type = STACK_I4;
2103 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2104 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2106 set_vreg_to_inst (cfg, inst->dreg + 1, tree);
2108 /* Allocate a dummy MonoInst for the second vreg */
2109 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2110 tree->dreg = inst->dreg + 2;
2111 if (cfg->opt & MONO_OPT_SSA)
2112 tree->flags = MONO_INST_VOLATILE;
2113 tree->inst_c0 = num;
2114 tree->type = STACK_I4;
2115 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2116 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2118 set_vreg_to_inst (cfg, inst->dreg + 2, tree);
2122 if (cfg->verbose_level > 2)
2123 g_print ("created temp %d (R%d) of type %s\n", num, vreg, mono_type_get_name (type));
2128 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
2132 if (mono_type_is_long (type))
2133 dreg = mono_alloc_dreg (cfg, STACK_I8);
2134 #ifdef MONO_ARCH_SOFT_FLOAT
2135 else if (mono_type_is_float (type))
2136 dreg = mono_alloc_dreg (cfg, STACK_R8);
2139 /* All the others are unified */
2140 dreg = mono_alloc_preg (cfg);
2142 return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
2146 * Transform a MonoInst into a load from the variable of index var_index.
2149 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
2150 memset (dest, 0, sizeof (MonoInst));
2151 dest->ssa_op = MONO_SSA_LOAD;
2152 dest->inst_i0 = cfg->varinfo [var_index];
2153 dest->opcode = mini_type_to_ldind (cfg, dest->inst_i0->inst_vtype);
2154 type_to_eval_stack_type (cfg, dest->inst_i0->inst_vtype, dest);
2155 dest->klass = dest->inst_i0->klass;
2159 * Create a MonoInst that is a load from the variable of index var_index.
2162 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
2164 NEW_TEMPLOAD (cfg,dest,var_index);
2169 * Create a MonoInst that is a store of the given value into the variable of index var_index.
2172 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
2174 NEW_TEMPSTORE (cfg, dest, var_index, value);
2179 type_from_stack_type (MonoInst *ins) {
2180 switch (ins->type) {
2181 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
2182 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
2183 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
2184 case STACK_R8: return &mono_defaults.double_class->byval_arg;
2187 * this if used to be commented without any specific reason, but
2188 * it breaks #80235 when commented
2191 return &ins->klass->this_arg;
2193 return &mono_defaults.object_class->this_arg;
2195 /* ins->klass may not be set for ldnull.
2196 * Also, if we have a boxed valuetype, we want an object lass,
2197 * not the valuetype class
2199 if (ins->klass && !ins->klass->valuetype)
2200 return &ins->klass->byval_arg;
2201 return &mono_defaults.object_class->byval_arg;
2202 case STACK_VTYPE: return &ins->klass->byval_arg;
2204 g_error ("stack type %d to montype not handled\n", ins->type);
2210 mono_type_from_stack_type (MonoInst *ins) {
2211 return type_from_stack_type (ins);
2215 array_access_to_klass (int opcode, MonoInst *array_obj)
2219 return mono_defaults.byte_class;
2221 return mono_defaults.uint16_class;
2224 return mono_defaults.int_class;
2227 return mono_defaults.sbyte_class;
2230 return mono_defaults.int16_class;
2233 return mono_defaults.int32_class;
2235 return mono_defaults.uint32_class;
2238 return mono_defaults.int64_class;
2241 return mono_defaults.single_class;
2244 return mono_defaults.double_class;
2245 case CEE_LDELEM_REF:
2246 case CEE_STELEM_REF: {
2247 MonoClass *klass = array_obj->klass;
2248 /* FIXME: add assert */
2249 if (klass && klass->rank)
2250 return klass->element_class;
2251 return mono_defaults.object_class;
2254 g_assert_not_reached ();
2260 * mono_add_ins_to_end:
2262 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
2265 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
2270 MONO_ADD_INS (bb, inst);
2274 switch (bb->last_ins->opcode) {
2288 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2291 if (MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
2292 /* Need to insert the ins before the compare */
2293 if (bb->code == bb->last_ins) {
2294 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2298 if (bb->code->next == bb->last_ins) {
2299 /* Only two instructions */
2300 opcode = bb->code->opcode;
2302 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)) {
2304 mono_bblock_insert_before_ins (bb, bb->code, inst);
2306 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2309 opcode = bb->last_ins->prev->opcode;
2311 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)) {
2313 mono_bblock_insert_before_ins (bb, bb->last_ins->prev, inst);
2315 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2320 MONO_ADD_INS (bb, inst);
2328 * Replace INS with its decomposition which is stored in a series of bblocks starting
2329 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
2330 * On return, it will be set to the last ins of the decomposition.
2333 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
2335 MonoInst *next = ins->next;
2337 if (next && next->opcode == OP_NOP) {
2338 /* Avoid NOPs following branches */
2339 ins->next = next->next;
2343 if (first_bb == last_bb) {
2345 * Only one replacement bb, merge the code into
2349 /* Delete links between the first_bb and its successors */
2350 while (first_bb->out_count)
2351 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
2355 (*prev)->next = first_bb->code;
2356 first_bb->code->prev = (*prev);
2358 bb->code = first_bb->code;
2362 last_bb->last_ins->next = next;
2364 next->prev = last_bb->last_ins;
2366 bb->last_ins = last_bb->last_ins;
2367 *prev = last_bb->last_ins;
2370 MonoBasicBlock **tmp_bblocks, *tmp;
2376 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
2377 tmp->region = bb->region;
2379 /* Split the original bb */
2381 ins->next->prev = NULL;
2385 /* Merge the second part of the original bb into the last bb */
2386 if (last_bb->last_ins) {
2387 last_bb->last_ins->next = next;
2389 next->prev = last_bb->last_ins;
2391 last_bb->code = next;
2395 for (last = next; last->next != NULL; last = last->next)
2397 last_bb->last_ins = last;
2400 for (i = 0; i < bb->out_count; ++i)
2401 link_bblock (cfg, last_bb, bb->out_bb [i]);
2403 /* Merge the first (dummy) bb to the original bb */
2405 (*prev)->next = first_bb->code;
2406 first_bb->code->prev = (*prev);
2408 bb->code = first_bb->code;
2410 bb->last_ins = first_bb->last_ins;
2412 /* Delete the links between the original bb and its successors */
2413 tmp_bblocks = bb->out_bb;
2414 count = bb->out_count;
2415 for (i = 0; i < count; ++i)
2416 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
2418 /* Add links between the original bb and the first_bb's successors */
2419 for (i = 0; i < first_bb->out_count; ++i) {
2420 MonoBasicBlock *out_bb = first_bb->out_bb [i];
2422 link_bblock (cfg, bb, out_bb);
2424 /* Delete links between the first_bb and its successors */
2425 for (i = 0; i < bb->out_count; ++i) {
2426 MonoBasicBlock *out_bb = bb->out_bb [i];
2428 mono_unlink_bblock (cfg, first_bb, out_bb);
2430 last_bb->next_bb = bb->next_bb;
2431 bb->next_bb = first_bb->next_bb;
2438 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
2440 MonoInst *inst, *load;
2442 NEW_TEMPLOAD (cfg, load, src);
2444 NEW_TEMPSTORE (cfg, inst, dest, load);
2445 /* FIXME: handle CEE_STIND_R4 */
2446 if (inst->opcode == CEE_STOBJ) {
2447 NEW_TEMPLOADA (cfg, inst, dest);
2448 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
2450 inst->cil_code = NULL;
2451 mono_add_ins_to_end (bb, inst);
2456 * This function is called to handle items that are left on the evaluation stack
2457 * at basic block boundaries. What happens is that we save the values to local variables
2458 * and we reload them later when first entering the target basic block (with the
2459 * handle_loaded_temps () function).
2460 * It is also used to handle items on the stack in store opcodes, since it is
2461 * possible that the variable to be stored into is already on the stack, in
2462 * which case its old value should be used.
2463 * A single joint point will use the same variables (stored in the array bb->out_stack or
2464 * bb->in_stack, if the basic block is before or after the joint point).
2465 * If the stack merge fails at a join point, cfg->unverifiable is set.
2468 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count)
2471 MonoBasicBlock *outb;
2472 MonoInst *inst, **locals;
2477 if (cfg->verbose_level > 3)
2478 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
2480 if (!bb->out_scount) {
2481 bb->out_scount = count;
2482 //g_print ("bblock %d has out:", bb->block_num);
2484 for (i = 0; i < bb->out_count; ++i) {
2485 outb = bb->out_bb [i];
2486 /* exception handlers are linked, but they should not be considered for stack args */
2487 if (outb->flags & BB_EXCEPTION_HANDLER)
2489 //g_print (" %d", outb->block_num);
2490 if (outb->in_stack) {
2492 bb->out_stack = outb->in_stack;
2498 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
2499 for (i = 0; i < count; ++i) {
2501 * try to reuse temps already allocated for this purpouse, if they occupy the same
2502 * stack slot and if they are of the same type.
2503 * This won't cause conflicts since if 'local' is used to
2504 * store one of the values in the in_stack of a bblock, then
2505 * the same variable will be used for the same outgoing stack
2507 * This doesn't work when inlining methods, since the bblocks
2508 * in the inlined methods do not inherit their in_stack from
2509 * the bblock they are inlined to. See bug #58863 for an
2511 * This hack is disabled since it also prevents proper tracking of types.
2514 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2516 if (cfg->inlined_method)
2517 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2519 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
2525 for (i = 0; i < bb->out_count; ++i) {
2526 outb = bb->out_bb [i];
2527 /* exception handlers are linked, but they should not be considered for stack args */
2528 if (outb->flags & BB_EXCEPTION_HANDLER)
2530 if (outb->in_scount) {
2531 if (outb->in_scount != bb->out_scount) {
2532 cfg->unverifiable = TRUE;
2535 continue; /* check they are the same locals */
2537 outb->in_scount = count;
2538 outb->in_stack = bb->out_stack;
2541 locals = bb->out_stack;
2542 for (i = 0; i < count; ++i) {
2543 /* add store ops at the end of the bb, before the branch */
2544 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
2545 if (inst->opcode == CEE_STOBJ) {
2546 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
2547 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
2549 inst->cil_code = sp [i]->cil_code;
2550 mono_add_ins_to_end (bb, inst);
2552 if (cfg->verbose_level > 3)
2553 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
2557 * It is possible that the out bblocks already have in_stack assigned, and
2558 * the in_stacks differ. In this case, we will store to all the different
2565 /* Find a bblock which has a different in_stack */
2567 while (bindex < bb->out_count) {
2568 outb = bb->out_bb [bindex];
2569 /* exception handlers are linked, but they should not be considered for stack args */
2570 if (outb->flags & BB_EXCEPTION_HANDLER) {
2574 if (outb->in_stack != locals) {
2576 * Instead of storing sp [i] to locals [i], we need to store
2577 * locals [i] to <new locals>[i], since the sp [i] tree can't
2578 * be shared between trees.
2580 for (i = 0; i < count; ++i)
2581 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
2582 locals = outb->in_stack;
2592 ret_type_to_call_opcode (MonoType *type, int calli, int virt, MonoGenericSharingContext *gsctx)
2595 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2598 type = mini_get_basic_type_from_generic (gsctx, type);
2599 switch (type->type) {
2600 case MONO_TYPE_VOID:
2601 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
2604 case MONO_TYPE_BOOLEAN:
2607 case MONO_TYPE_CHAR:
2610 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2614 case MONO_TYPE_FNPTR:
2615 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2616 case MONO_TYPE_CLASS:
2617 case MONO_TYPE_STRING:
2618 case MONO_TYPE_OBJECT:
2619 case MONO_TYPE_SZARRAY:
2620 case MONO_TYPE_ARRAY:
2621 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2624 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
2627 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
2628 case MONO_TYPE_VALUETYPE:
2629 if (type->data.klass->enumtype) {
2630 type = type->data.klass->enum_basetype;
2633 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2634 case MONO_TYPE_TYPEDBYREF:
2635 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2636 case MONO_TYPE_GENERICINST:
2637 type = &type->data.generic_class->container_class->byval_arg;
2640 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2646 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
2648 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2649 MonoJumpInfoBBTable *table;
2651 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
2653 table->table_size = num_blocks;
2655 ji->ip.label = label;
2656 ji->type = MONO_PATCH_INFO_SWITCH;
2657 ji->data.table = table;
2658 ji->next = cfg->patch_info;
2659 cfg->patch_info = ji;
2663 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
2665 if (cfg->compile_aot) {
2666 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
2667 jump_info_token->image = image;
2668 jump_info_token->token = token;
2669 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
2674 * When we add a tree of instructions, we need to ensure the instructions currently
2675 * on the stack are executed before (like, if we load a value from a local).
2676 * We ensure this by saving the currently loaded values to temps and rewriting the
2677 * instructions to load the values.
2678 * This is not done for opcodes that terminate a basic block (because it's handled already
2679 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
2682 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
2684 MonoInst *load, *store, *temp, *ins;
2686 while (stack < sp) {
2688 /* handle also other constants */
2689 if ((ins->opcode != OP_ICONST) &&
2690 /* temps never get written to again, so we can safely avoid duplicating them */
2691 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2692 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2693 temp->flags |= MONO_INST_IS_TEMP;
2694 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2695 store->cil_code = ins->cil_code;
2696 if (store->opcode == CEE_STOBJ) {
2697 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2698 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
2700 MONO_ADD_INS (bblock, store);
2701 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2702 load->cil_code = ins->cil_code;
2710 * target_type_is_incompatible:
2711 * @cfg: MonoCompile context
2713 * Check that the item @arg on the evaluation stack can be stored
2714 * in the target type (can be a local, or field, etc).
2715 * The cfg arg can be used to check if we need verification or just
2718 * Returns: non-0 value if arg can't be stored on a target.
2721 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2723 MonoType *simple_type;
2726 if (target->byref) {
2727 /* FIXME: check that the pointed to types match */
2728 if (arg->type == STACK_MP)
2729 return arg->klass != mono_class_from_mono_type (target);
2730 if (arg->type == STACK_PTR)
2734 simple_type = mono_type_get_underlying_type (target);
2735 switch (simple_type->type) {
2736 case MONO_TYPE_VOID:
2740 case MONO_TYPE_BOOLEAN:
2743 case MONO_TYPE_CHAR:
2746 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2750 /* STACK_MP is needed when setting pinned locals */
2751 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2756 case MONO_TYPE_FNPTR:
2757 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2760 case MONO_TYPE_OBJECT:
2761 if (arg->type != STACK_OBJ)
2764 case MONO_TYPE_STRING:
2765 if (arg->type != STACK_OBJ)
2767 /* ldnull has arg->klass unset */
2768 /*if (arg->klass && arg->klass != mono_defaults.string_class) {
2773 case MONO_TYPE_CLASS:
2774 case MONO_TYPE_SZARRAY:
2775 case MONO_TYPE_ARRAY:
2776 if (arg->type != STACK_OBJ)
2778 /* FIXME: check type compatibility */
2782 if (arg->type != STACK_I8)
2787 if (arg->type != STACK_R8)
2790 case MONO_TYPE_VALUETYPE:
2791 if (arg->type != STACK_VTYPE)
2793 klass = mono_class_from_mono_type (simple_type);
2794 if (klass != arg->klass)
2797 case MONO_TYPE_TYPEDBYREF:
2798 if (arg->type != STACK_VTYPE)
2800 klass = mono_class_from_mono_type (simple_type);
2801 if (klass != arg->klass)
2804 case MONO_TYPE_GENERICINST:
2805 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2806 klass = mono_class_from_mono_type (simple_type);
2807 if (klass->enumtype)
2808 return target_type_is_incompatible (cfg, klass->enum_basetype, arg);
2809 if (arg->type != STACK_VTYPE)
2811 if (klass != arg->klass)
2815 if (arg->type != STACK_OBJ)
2817 /* FIXME: check type compatibility */
2821 case MONO_TYPE_MVAR:
2822 /* FIXME: all the arguments must be references for now,
2823 * later look inside cfg and see if the arg num is
2824 * really a reference
2826 g_assert (cfg->generic_sharing_context);
2827 if (arg->type != STACK_OBJ)
2831 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2837 * Prepare arguments for passing to a function call.
2838 * Return a non-zero value if the arguments can't be passed to the given
2840 * The type checks are not yet complete and some conversions may need
2841 * casts on 32 or 64 bit architectures.
2843 * FIXME: implement this using target_type_is_incompatible ()
2846 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2848 MonoType *simple_type;
2852 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2856 for (i = 0; i < sig->param_count; ++i) {
2857 if (sig->params [i]->byref) {
2858 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2862 simple_type = sig->params [i];
2863 simple_type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, simple_type);
2865 switch (simple_type->type) {
2866 case MONO_TYPE_VOID:
2871 case MONO_TYPE_BOOLEAN:
2874 case MONO_TYPE_CHAR:
2877 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2883 case MONO_TYPE_FNPTR:
2884 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2887 case MONO_TYPE_CLASS:
2888 case MONO_TYPE_STRING:
2889 case MONO_TYPE_OBJECT:
2890 case MONO_TYPE_SZARRAY:
2891 case MONO_TYPE_ARRAY:
2892 if (args [i]->type != STACK_OBJ)
2897 if (args [i]->type != STACK_I8)
2902 if (args [i]->type != STACK_R8)
2905 case MONO_TYPE_VALUETYPE:
2906 if (simple_type->data.klass->enumtype) {
2907 simple_type = simple_type->data.klass->enum_basetype;
2910 if (args [i]->type != STACK_VTYPE)
2913 case MONO_TYPE_TYPEDBYREF:
2914 if (args [i]->type != STACK_VTYPE)
2917 case MONO_TYPE_GENERICINST:
2918 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2922 g_error ("unknown type 0x%02x in check_call_signature",
2930 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
2931 const guint8 *ip, gboolean to_end)
2933 MonoInst *temp, *store, *ins = (MonoInst*)call;
2934 MonoType *ret = sig->ret;
2936 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2938 call->inst.type = STACK_OBJ;
2939 call->inst.opcode = OP_CALL;
2940 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2942 type_to_eval_stack_type (cfg, ret, ins);
2943 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2946 temp->flags |= MONO_INST_IS_TEMP;
2948 if (MONO_TYPE_ISSTRUCT (ret)) {
2949 MonoInst *loada, *dummy_store;
2952 * Emit a dummy store to the local holding the result so the
2953 * liveness info remains correct.
2955 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2957 mono_add_ins_to_end (bblock, dummy_store);
2959 MONO_ADD_INS (bblock, dummy_store);
2961 /* we use this to allocate native sized structs */
2962 temp->backend.is_pinvoke = sig->pinvoke;
2964 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2965 if (call->inst.opcode == OP_VCALL || call->inst.opcode == OP_VCALL_RGCTX)
2966 ins->inst_left = loada;
2968 ins->inst_right = loada; /* a virtual or indirect call */
2971 mono_add_ins_to_end (bblock, ins);
2973 MONO_ADD_INS (bblock, ins);
2975 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2976 store->cil_code = ip;
2978 #ifdef MONO_ARCH_SOFT_FLOAT
2979 if (store->opcode == CEE_STIND_R4) {
2980 /*FIXME implement proper support for to_end*/
2982 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2983 handle_store_float (cfg, bblock, store, ins, ip);
2987 mono_add_ins_to_end (bblock, store);
2989 MONO_ADD_INS (bblock, store);
2991 return temp->inst_c0;
2994 mono_add_ins_to_end (bblock, ins);
2996 MONO_ADD_INS (bblock, ins);
3001 inline static MonoCallInst *
3002 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3003 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
3008 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual, cfg->generic_sharing_context));
3010 #ifdef MONO_ARCH_SOFT_FLOAT
3011 /* we need to convert the r4 value to an int value */
3014 for (i = 0; i < sig->param_count; ++i) {
3015 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4) {
3016 MonoInst *iargs [1];
3018 iargs [0] = args [i + sig->hasthis];
3020 temp = mono_emit_jit_icall (cfg, bblock, mono_fload_r4_arg, iargs, ip);
3021 NEW_TEMPLOAD (cfg, arg, temp);
3022 args [i + sig->hasthis] = arg;
3028 call->inst.cil_code = ip;
3030 call->signature = sig;
3031 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
3032 type_to_eval_stack_type (cfg, sig->ret, &call->inst);
3034 for (arg = call->out_args; arg;) {
3035 MonoInst *narg = arg->next;
3040 mono_add_ins_to_end (bblock, arg);
3042 MONO_ADD_INS (bblock, arg);
3048 inline static MonoCallInst*
3049 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3050 MonoInst **args, MonoInst *addr, const guint8 *ip)
3052 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
3054 call->inst.inst_i0 = addr;
3059 inline static MonoCallInst*
3060 mono_emit_rgctx_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3061 MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3063 MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3066 switch (call->inst.opcode) {
3067 case OP_CALL_REG: call->inst.opcode = OP_CALL_REG_RGCTX; break;
3068 case OP_VOIDCALL_REG: call->inst.opcode = OP_VOIDCALL_REG_RGCTX; break;
3069 case OP_FCALL_REG: call->inst.opcode = OP_FCALL_REG_RGCTX; break;
3070 case OP_LCALL_REG: call->inst.opcode = OP_LCALL_REG_RGCTX; break;
3071 case OP_VCALL_REG: {
3074 NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3075 call->inst.inst_left = group;
3076 call->inst.opcode = OP_VCALL_REG_RGCTX;
3079 default: g_assert_not_reached ();
3082 if (call->inst.opcode != OP_VCALL_REG_RGCTX) {
3083 g_assert (!call->inst.inst_right);
3084 call->inst.inst_right = rgctx_arg;
3086 g_assert (!call->inst.inst_left->inst_right);
3087 call->inst.inst_left->inst_right = rgctx_arg;
3095 mono_emit_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3096 MonoInst **args, MonoInst *addr, const guint8 *ip)
3098 MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3100 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3104 mono_emit_rgctx_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3105 MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3107 MonoCallInst *call = mono_emit_rgctx_calli (cfg, bblock, sig, args, addr, rgctx_arg, ip);
3109 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3112 static MonoCallInst*
3113 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3114 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
3116 gboolean virtual = this != NULL;
3119 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
3121 if (this && sig->hasthis &&
3122 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
3123 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
3124 call->method = mono_marshal_get_remoting_invoke_with_check (method);
3126 call->method = method;
3128 call->inst.flags |= MONO_INST_HAS_METHOD;
3129 call->inst.inst_left = this;
3131 if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3132 /* Needed by the code generated in inssel.brg */
3133 mono_get_got_var (cfg);
3138 static MonoCallInst*
3139 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3140 MonoInst **args, const guint8 *ip, MonoInst *this)
3142 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3146 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3147 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3149 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
3151 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3155 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3156 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
3157 gboolean ret_object, gboolean to_end)
3159 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
3161 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
3165 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
3166 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
3172 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
3175 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
3179 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
3181 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
3184 g_warning ("unregistered JIT ICall");
3185 g_assert_not_reached ();
3188 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
3191 static MonoCallInst*
3192 mono_emit_rgctx_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3193 MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg, const guint8 *ip, MonoInst *this)
3195 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3197 g_assert (!(rgctx_arg && imt_arg));
3200 switch (call->inst.opcode) {
3201 case OP_CALL: call->inst.opcode = OP_CALL_RGCTX; break;
3202 case OP_VOIDCALL: call->inst.opcode = OP_VOIDCALL_RGCTX; break;
3203 case OP_FCALL: call->inst.opcode = OP_FCALL_RGCTX; break;
3204 case OP_LCALL: call->inst.opcode = OP_LCALL_RGCTX; break;
3205 case OP_VCALL: call->inst.opcode = OP_VCALL_RGCTX; break;
3206 default: g_assert_not_reached ();
3209 if (call->inst.opcode != OP_VCALL_RGCTX) {
3210 g_assert (!call->inst.inst_left);
3211 call->inst.inst_left = rgctx_arg;
3213 g_assert (!call->inst.inst_right);
3214 call->inst.inst_right = rgctx_arg;
3216 } else if (imt_arg) {
3217 switch (call->inst.opcode) {
3218 case OP_CALLVIRT: call->inst.opcode = OP_CALLVIRT_IMT; break;
3219 case OP_VOIDCALLVIRT: call->inst.opcode = OP_VOIDCALLVIRT_IMT; break;
3220 case OP_FCALLVIRT: call->inst.opcode = OP_FCALLVIRT_IMT; break;
3221 case OP_LCALLVIRT: call->inst.opcode = OP_LCALLVIRT_IMT; break;
3222 case OP_VCALLVIRT: {
3225 NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3226 call->inst.inst_left = group;
3227 call->inst.opcode = OP_VCALLVIRT_IMT;
3230 default: g_assert_not_reached ();
3233 if (call->inst.opcode != OP_VCALLVIRT_IMT) {
3234 g_assert (!call->inst.inst_right);
3235 call->inst.inst_right = imt_arg;
3237 g_assert (!call->inst.inst_left->inst_right);
3238 call->inst.inst_left->inst_right = imt_arg;
3246 mono_emit_rgctx_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3247 MonoMethodSignature *signature, MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg,
3248 const guint8 *ip, MonoInst *this)
3250 MonoCallInst *call = mono_emit_rgctx_method_call (cfg, bblock, method, signature, args, rgctx_arg, imt_arg, ip, this);
3252 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3256 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
3258 MonoInst *ins, *temp = NULL, *store, *load, *begin;
3259 MonoInst *last_arg = NULL;
3263 //g_print ("emulating: ");
3264 //mono_print_tree_nl (tree);
3265 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE, cfg->generic_sharing_context));
3266 ins = (MonoInst*)call;
3268 call->inst.cil_code = tree->cil_code;
3270 call->signature = info->sig;
3272 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
3274 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3275 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
3276 temp->flags |= MONO_INST_IS_TEMP;
3277 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3278 /* FIXME: handle CEE_STIND_R4 */
3279 store->cil_code = tree->cil_code;
3284 nargs = info->sig->param_count + info->sig->hasthis;
3286 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
3289 last_arg->next = store;
3292 begin = call->out_args;
3296 if (cfg->prev_ins) {
3298 * This assumes that that in a tree, emulate_opcode is called for a
3299 * node before it is called for its children. dec_foreach needs to
3300 * take this into account.
3302 store->next = cfg->prev_ins->next;
3303 cfg->prev_ins->next = begin;
3305 store->next = cfg->cbb->code;
3306 cfg->cbb->code = begin;
3309 call->fptr = mono_icall_get_wrapper (info);
3311 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3312 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3318 * This entry point could be used later for arbitrary method
3322 mini_redirect_call (int *temp, MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3323 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3326 if (method->klass == mono_defaults.string_class) {
3327 /* managed string allocation support */
3328 if (strcmp (method->name, "InternalAllocateStr") == 0) {
3329 MonoInst *iargs [2];
3330 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3331 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, FALSE);
3334 NEW_VTABLECONST (cfg, iargs [0], vtable);
3335 iargs [1] = args [0];
3336 *temp = mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, this);
3343 static MonoMethodSignature *
3344 mono_get_array_new_va_signature (int arity)
3346 static GHashTable *sighash = NULL;
3347 MonoMethodSignature *res;
3352 sighash = g_hash_table_new (NULL, NULL);
3354 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
3359 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
3362 #ifdef MONO_ARCH_VARARG_ICALLS
3363 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
3364 res->call_convention = MONO_CALL_VARARG;
3367 #ifdef PLATFORM_WIN32
3368 res->call_convention = MONO_CALL_C;
3371 res->params [0] = &mono_defaults.int_class->byval_arg;
3372 for (i = 0; i < arity; i++)
3373 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
3375 res->ret = &mono_defaults.object_class->byval_arg;
3377 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
3384 mono_get_array_new_va_icall (int rank)
3386 MonoMethodSignature *esig;
3387 char icall_name [256];
3389 MonoJitICallInfo *info;
3391 /* Need to register the icall so it gets an icall wrapper */
3392 sprintf (icall_name, "ves_array_new_va_%d", rank);
3395 info = mono_find_jit_icall_by_name (icall_name);
3397 esig = mono_get_array_new_va_signature (rank);
3398 name = g_strdup (icall_name);
3399 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
3401 g_hash_table_insert (jit_icall_name_hash, name, name);
3409 get_memcpy_method (void)
3411 static MonoMethod *memcpy_method = NULL;
3412 if (!memcpy_method) {
3413 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3415 g_error ("Old corlib found. Install a new one");
3417 return memcpy_method;
3421 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
3422 MonoInst *iargs [3];
3425 MonoMethod *memcpy_method;
3429 * This check breaks with spilled vars... need to handle it during verification anyway.
3430 * g_assert (klass && klass == src->klass && klass == dest->klass);
3434 n = mono_class_native_size (klass, &align);
3436 n = mono_class_value_size (klass, &align);
3438 #if HAVE_WRITE_BARRIERS
3439 /* if native is true there should be no references in the struct */
3440 if (write_barrier && klass->has_references && !native) {
3443 NEW_PCONST (cfg, iargs [2], klass);
3445 mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
3450 /* FIXME: add write barrier handling */
3451 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
3453 if (dest->opcode == OP_LDADDR) {
3454 /* Keep liveness info correct */
3455 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
3456 MONO_ADD_INS (bblock, inst);
3458 NEW_MEMCPY (cfg, inst, dest, src, n, align);
3459 MONO_ADD_INS (bblock, inst);
3464 NEW_ICONST (cfg, iargs [2], n);
3466 memcpy_method = get_memcpy_method ();
3467 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
3471 get_memset_method (void)
3473 static MonoMethod *memset_method = NULL;
3474 if (!memset_method) {
3475 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3477 g_error ("Old corlib found. Install a new one");
3479 return memset_method;
3483 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
3485 MonoInst *iargs [3];
3486 MonoInst *ins, *zero_int32;
3489 MonoMethod *memset_method;
3491 NEW_ICONST (cfg, zero_int32, 0);
3493 mono_class_init (klass);
3494 n = mono_class_value_size (klass, &align);
3495 MONO_INST_NEW (cfg, ins, 0);
3497 ins->inst_left = dest;
3498 ins->inst_right = zero_int32;
3500 ins->opcode = CEE_STIND_I1;
3501 MONO_ADD_INS (bblock, ins);
3502 } else if ((n == 2) && (align >= 2)) {
3503 ins->opcode = CEE_STIND_I2;
3504 MONO_ADD_INS (bblock, ins);
3505 } else if ((n == 2) && (align >= 4)) {
3506 ins->opcode = CEE_STIND_I4;
3507 MONO_ADD_INS (bblock, ins);
3508 } else if (n <= sizeof (gpointer) * 5) {
3509 NEW_MEMSET (cfg, ins, dest, 0, n, align);
3510 MONO_ADD_INS (bblock, ins);
3512 memset_method = get_memset_method ();
3513 handle_loaded_temps (cfg, bblock, stack_start, sp);
3515 NEW_ICONST (cfg, iargs [1], 0);
3516 NEW_ICONST (cfg, iargs [2], n);
3517 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
3522 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
3524 MonoInst *iargs [2];
3527 if (cfg->opt & MONO_OPT_SHARED) {
3528 NEW_DOMAINCONST (cfg, iargs [0]);
3529 NEW_CLASSCONST (cfg, iargs [1], klass);
3531 alloc_ftn = mono_object_new;
3532 } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
3533 /* This happens often in argument checking code, eg. throw new FooException... */
3534 /* Avoid relocations by calling a helper function specialized to mscorlib */
3535 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3536 return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
3538 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3539 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3542 if (managed_alloc) {
3543 NEW_VTABLECONST (cfg, iargs [0], vtable);
3544 return mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, NULL);
3546 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
3548 guint32 lw = vtable->klass->instance_size;
3549 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
3550 NEW_ICONST (cfg, iargs [0], lw);
3551 NEW_VTABLECONST (cfg, iargs [1], vtable);
3554 NEW_VTABLECONST (cfg, iargs [0], vtable);
3557 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3561 handle_alloc_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *data_inst,
3562 gboolean for_box, const guchar *ip)
3564 MonoInst *iargs [2];
3565 MonoMethod *managed_alloc = NULL;
3568 FIXME: we cannot get managed_alloc here because we can't get
3569 the class's vtable (because it's not a closed class)
3571 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3572 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3575 if (cfg->opt & MONO_OPT_SHARED) {
3576 NEW_DOMAINCONST (cfg, iargs [0]);
3577 iargs [1] = data_inst;
3578 alloc_ftn = mono_object_new;
3580 g_assert (!cfg->compile_aot);
3582 if (managed_alloc) {
3583 iargs [0] = data_inst;
3584 return mono_emit_method_call_spilled (cfg, bblock, managed_alloc,
3585 mono_method_signature (managed_alloc), iargs, ip, NULL);
3588 iargs [0] = data_inst;
3589 alloc_ftn = mono_object_new_specific;
3592 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3596 handle_box_copy (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass, int temp)
3598 MonoInst *dest, *vtoffset, *add, *vstore;
3600 NEW_TEMPLOAD (cfg, dest, temp);
3601 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3602 MONO_INST_NEW (cfg, add, OP_PADD);
3603 add->inst_left = dest;
3604 add->inst_right = vtoffset;
3607 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3608 vstore->opcode = mini_type_to_stind (cfg, &klass->byval_arg);
3609 vstore->cil_code = ip;
3610 vstore->inst_left = add;
3611 vstore->inst_right = val;
3613 #ifdef MONO_ARCH_SOFT_FLOAT
3614 if (vstore->opcode == CEE_STIND_R4) {
3615 handle_store_float (cfg, bblock, add, val, ip);
3618 if (vstore->opcode == CEE_STOBJ) {
3619 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
3621 MONO_ADD_INS (bblock, vstore);
3623 NEW_TEMPLOAD (cfg, dest, temp);
3628 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
3633 if (mono_class_is_nullable (klass)) {
3634 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
3635 temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
3636 NEW_TEMPLOAD (cfg, dest, temp);
3640 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
3642 return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3646 handle_box_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip,
3647 MonoClass *klass, MonoInst *data_inst)
3651 g_assert (!mono_class_is_nullable (klass));
3653 temp = handle_alloc_from_inst (cfg, bblock, klass, data_inst, TRUE, ip);
3655 return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3659 handle_delegate_ctor (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *target, MonoMethod *method, unsigned char *ip)
3661 gpointer *trampoline;
3662 MonoInst *obj, *ins, *store, *offset_ins, *method_ins, *tramp_ins;
3665 temp = handle_alloc (cfg, bblock, klass, FALSE, ip);
3667 /* Inline the contents of mono_delegate_ctor */
3669 /* Set target field */
3670 /* Optimize away setting of NULL target */
3671 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
3672 NEW_TEMPLOAD (cfg, obj, temp);
3673 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, target));
3674 MONO_INST_NEW (cfg, ins, OP_PADD);
3675 ins->inst_left = obj;
3676 ins->inst_right = offset_ins;
3678 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3679 store->inst_left = ins;
3680 store->inst_right = target;
3681 mono_bblock_add_inst (bblock, store);
3684 /* Set method field */
3685 NEW_TEMPLOAD (cfg, obj, temp);
3686 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, method));
3687 MONO_INST_NEW (cfg, ins, OP_PADD);
3688 ins->inst_left = obj;
3689 ins->inst_right = offset_ins;
3691 NEW_METHODCONST (cfg, method_ins, method);
3693 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3694 store->inst_left = ins;
3695 store->inst_right = method_ins;
3696 mono_bblock_add_inst (bblock, store);
3698 /* Set invoke_impl field */
3699 NEW_TEMPLOAD (cfg, obj, temp);
3700 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, invoke_impl));
3701 MONO_INST_NEW (cfg, ins, OP_PADD);
3702 ins->inst_left = obj;
3703 ins->inst_right = offset_ins;
3705 trampoline = mono_create_delegate_trampoline (klass);
3706 NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_ABS, trampoline);
3708 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3709 store->inst_left = ins;
3710 store->inst_right = tramp_ins;
3711 mono_bblock_add_inst (bblock, store);
3713 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
3715 NEW_TEMPLOAD (cfg, obj, temp);
3721 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
3723 MonoJitICallInfo *info;
3725 info = mono_get_array_new_va_icall (rank);
3727 cfg->flags |= MONO_CFG_HAS_VARARGS;
3729 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
3730 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
3734 mono_emit_load_got_addr (MonoCompile *cfg)
3736 MonoInst *load, *store, *dummy_use;
3739 if (!cfg->got_var || cfg->got_var_allocated)
3742 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
3743 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
3745 /* Add it to the start of the first bblock */
3746 if (cfg->bb_entry->code) {
3747 store->next = cfg->bb_entry->code;
3748 cfg->bb_entry->code = store;
3751 MONO_ADD_INS (cfg->bb_entry, store);
3753 cfg->got_var_allocated = TRUE;
3756 * Add a dummy use to keep the got_var alive, since real uses might
3757 * only be generated in the decompose or instruction selection phases.
3758 * Add it to end_bblock, so the variable's lifetime covers the whole
3761 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
3762 NEW_DUMMY_USE (cfg, dummy_use, load);
3763 MONO_ADD_INS (cfg->bb_exit, dummy_use);
3766 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
3769 mini_class_is_system_array (MonoClass *klass)
3771 if (klass->parent == mono_defaults.array_class)
3778 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
3780 MonoMethodHeader *header = mono_method_get_header (method);
3781 MonoMethodSignature *signature = mono_method_signature (method);
3785 if (cfg->generic_sharing_context)
3788 if (method->inline_failure)
3791 #ifdef MONO_ARCH_HAVE_LMF_OPS
3792 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3793 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
3794 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
3798 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3799 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3800 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
3801 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
3802 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3803 (method->klass->marshalbyref) ||
3804 !header || header->num_clauses ||
3805 /* fixme: why cant we inline valuetype returns? */
3806 MONO_TYPE_ISSTRUCT (signature->ret))
3809 #ifdef MONO_ARCH_SOFT_FLOAT
3810 /* this complicates things, fix later */
3811 if (signature->ret->type == MONO_TYPE_R4)
3814 /* its not worth to inline methods with valuetype arguments?? */
3815 for (i = 0; i < signature->param_count; i++) {
3816 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
3819 #ifdef MONO_ARCH_SOFT_FLOAT
3820 /* this complicates things, fix later */
3821 if (!signature->params [i]->byref && signature->params [i]->type == MONO_TYPE_R4)
3826 /* also consider num_locals? */
3827 /* Do the size check early to avoid creating vtables */
3828 if (getenv ("MONO_INLINELIMIT")) {
3829 if (header->code_size >= atoi (getenv ("MONO_INLINELIMIT"))) {
3832 } else if (header->code_size >= INLINE_LENGTH_LIMIT)
3836 * if we can initialize the class of the method right away, we do,
3837 * otherwise we don't allow inlining if the class needs initialization,
3838 * since it would mean inserting a call to mono_runtime_class_init()
3839 * inside the inlined code
3841 if (!(cfg->opt & MONO_OPT_SHARED)) {
3842 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
3843 if (cfg->run_cctors && method->klass->has_cctor) {
3844 if (!method->klass->runtime_info)
3845 /* No vtable created yet */
3847 vtable = mono_class_vtable (cfg->domain, method->klass);
3850 /* This makes so that inline cannot trigger */
3851 /* .cctors: too many apps depend on them */
3852 /* running with a specific order... */
3853 if (! vtable->initialized)
3855 mono_runtime_class_init (vtable);
3857 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
3858 if (!method->klass->runtime_info)
3859 /* No vtable created yet */
3861 vtable = mono_class_vtable (cfg->domain, method->klass);
3864 if (!vtable->initialized)
3869 * If we're compiling for shared code
3870 * the cctor will need to be run at aot method load time, for example,
3871 * or at the end of the compilation of the inlining method.
3873 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3876 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
3879 * CAS - do not inline methods with declarative security
3880 * Note: this has to be before any possible return TRUE;
3882 if (mono_method_has_declsec (method))
3889 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
3891 if (vtable->initialized && !cfg->compile_aot)
3894 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3897 if (!mono_class_needs_cctor_run (vtable->klass, method))
3900 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3901 /* The initialization is already done before the method is called */
3908 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3912 MonoMethod *addr_method;
3915 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3918 MONO_INST_NEW (cfg, addr, CEE_LDELEMA);
3919 addr->inst_left = sp [0];
3920 addr->inst_right = sp [1];
3921 addr->type = STACK_MP;
3922 addr->klass = cmethod->klass->element_class;
3926 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3927 #if defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(MONO_ARCH_NO_EMULATE_MUL)
3928 /* OP_LDELEMA2D depends on OP_LMUL */
3931 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
3932 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
3933 addr->inst_left = sp [0];
3934 addr->inst_right = indexes;
3935 addr->type = STACK_MP;
3936 addr->klass = cmethod->klass->element_class;
3941 element_size = mono_class_array_element_size (cmethod->klass->element_class);
3942 addr_method = mono_marshal_get_array_address (rank, element_size);
3943 temp = mono_emit_method_call_spilled (cfg, bblock, addr_method, addr_method->signature, sp, ip, NULL);
3944 NEW_TEMPLOAD (cfg, addr, temp);
3949 static MonoJitICallInfo **emul_opcode_map = NULL;
3952 mono_find_jit_opcode_emulation (int opcode)
3954 g_assert (opcode >= 0 && opcode <= OP_LAST);
3955 if (emul_opcode_map)
3956 return emul_opcode_map [opcode];
3962 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3964 MonoInst *ins = NULL;
3966 static MonoClass *runtime_helpers_class = NULL;
3967 if (! runtime_helpers_class)
3968 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
3969 "System.Runtime.CompilerServices", "RuntimeHelpers");
3971 if (cmethod->klass == mono_defaults.string_class) {
3972 if (strcmp (cmethod->name, "get_Chars") == 0) {
3973 MONO_INST_NEW (cfg, ins, OP_GETCHR);
3974 ins->inst_i0 = args [0];
3975 ins->inst_i1 = args [1];
3977 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3978 MONO_INST_NEW (cfg, ins, OP_STRLEN);
3979 ins->inst_i0 = args [0];
3981 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
3983 MONO_INST_NEW (cfg, get_addr, OP_STR_CHAR_ADDR);
3984 get_addr->inst_i0 = args [0];
3985 get_addr->inst_i1 = args [1];
3986 MONO_INST_NEW (cfg, ins, CEE_STIND_I2);
3987 ins->inst_i0 = get_addr;
3988 ins->inst_i1 = args [2];
3992 } else if (cmethod->klass == mono_defaults.object_class) {
3993 if (strcmp (cmethod->name, "GetType") == 0) {
3994 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
3995 ins->inst_i0 = args [0];
3997 /* The OP_GETHASHCODE rule depends on OP_MUL */
3998 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
3999 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
4000 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
4001 ins->inst_i0 = args [0];
4004 } else if (strcmp (cmethod->name, ".ctor") == 0) {
4005 MONO_INST_NEW (cfg, ins, OP_NOP);
4009 } else if (cmethod->klass == mono_defaults.array_class) {
4010 if (cmethod->name [0] != 'g')
4013 if (strcmp (cmethod->name, "get_Rank") == 0) {
4014 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
4015 ins->inst_i0 = args [0];
4017 } else if (strcmp (cmethod->name, "get_Length") == 0) {
4018 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
4019 ins->inst_i0 = args [0];
4023 } else if (cmethod->klass == runtime_helpers_class) {
4024 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
4025 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
4029 } else if (cmethod->klass == mono_defaults.thread_class) {
4030 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
4032 if (strcmp (cmethod->name, "MemoryBarrier") == 0) {
4033 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4036 if (strcmp (cmethod->name, "SpinWait_nop") == 0) {
4037 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
4040 } else if (mini_class_is_system_array (cmethod->klass) &&
4041 strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
4043 MonoInst *ldelem, *store, *load;
4044 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
4046 n = mini_type_to_stind (cfg, &eklass->byval_arg);
4051 NEW_LDELEMA (cfg, ldelem, sp, eklass);
4052 ldelem->flags |= MONO_INST_NORANGECHECK;
4053 MONO_INST_NEW (cfg, store, n);
4054 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &eklass->byval_arg));
4055 type_to_eval_stack_type (cfg, &eklass->byval_arg, load);
4056 load->inst_left = ldelem;
4057 store->inst_left = args [2];
4058 store->inst_right = load;
4060 } else if (cmethod->klass == mono_defaults.math_class) {
4062 * There is general branches code for Min/Max, but it does not work for
4064 * http://everything2.com/?node_id=1051618
4066 } else if (cmethod->klass->image == mono_defaults.corlib &&
4067 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4068 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4071 #if SIZEOF_VOID_P == 8
4072 if (strcmp (cmethod->name, "Read") == 0 && (fsig->params [0]->type == MONO_TYPE_I8)) {
4073 /* 64 bit reads are already atomic */
4074 MONO_INST_NEW (cfg, ins, CEE_LDIND_I8);
4075 ins->inst_i0 = args [0];
4079 #ifdef MONO_ARCH_HAVE_ATOMIC_ADD
4080 if (strcmp (cmethod->name, "Increment") == 0) {
4081 MonoInst *ins_iconst;
4084 if (fsig->params [0]->type == MONO_TYPE_I4)
4085 opcode = OP_ATOMIC_ADD_NEW_I4;
4086 else if (fsig->params [0]->type == MONO_TYPE_I8)
4087 opcode = OP_ATOMIC_ADD_NEW_I8;
4089 g_assert_not_reached ();
4091 #if SIZEOF_VOID_P == 4
4092 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4096 MONO_INST_NEW (cfg, ins, opcode);
4097 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4098 ins_iconst->inst_c0 = 1;
4100 ins->inst_i0 = args [0];
4101 ins->inst_i1 = ins_iconst;
4102 } else if (strcmp (cmethod->name, "Decrement") == 0) {
4103 MonoInst *ins_iconst;
4106 if (fsig->params [0]->type == MONO_TYPE_I4)
4107 opcode = OP_ATOMIC_ADD_NEW_I4;
4108 else if (fsig->params [0]->type == MONO_TYPE_I8)
4109 opcode = OP_ATOMIC_ADD_NEW_I8;
4111 g_assert_not_reached ();
4113 #if SIZEOF_VOID_P == 4
4114 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4118 MONO_INST_NEW (cfg, ins, opcode);
4119 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4120 ins_iconst->inst_c0 = -1;
4122 ins->inst_i0 = args [0];
4123 ins->inst_i1 = ins_iconst;
4124 } else if (strcmp (cmethod->name, "Add") == 0) {
4127 if (fsig->params [0]->type == MONO_TYPE_I4)
4128 opcode = OP_ATOMIC_ADD_NEW_I4;
4129 else if (fsig->params [0]->type == MONO_TYPE_I8)
4130 opcode = OP_ATOMIC_ADD_NEW_I8;
4132 g_assert_not_reached ();
4134 #if SIZEOF_VOID_P == 4
4135 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4139 MONO_INST_NEW (cfg, ins, opcode);
4141 ins->inst_i0 = args [0];
4142 ins->inst_i1 = args [1];
4144 #endif /* MONO_ARCH_HAVE_ATOMIC_ADD */
4146 #ifdef MONO_ARCH_HAVE_ATOMIC_EXCHANGE
4147 if (strcmp (cmethod->name, "Exchange") == 0) {
4150 if (fsig->params [0]->type == MONO_TYPE_I4)
4151 opcode = OP_ATOMIC_EXCHANGE_I4;
4152 #if SIZEOF_VOID_P == 8
4153 else if ((fsig->params [0]->type == MONO_TYPE_I8) ||
4154 (fsig->params [0]->type == MONO_TYPE_I) ||
4155 (fsig->params [0]->type == MONO_TYPE_OBJECT))
4156 opcode = OP_ATOMIC_EXCHANGE_I8;
4158 else if ((fsig->params [0]->type == MONO_TYPE_I) ||
4159 (fsig->params [0]->type == MONO_TYPE_OBJECT))
4160 opcode = OP_ATOMIC_EXCHANGE_I4;
4165 #if SIZEOF_VOID_P == 4
4166 if (opcode == OP_ATOMIC_EXCHANGE_I8)
4170 MONO_INST_NEW (cfg, ins, opcode);
4172 ins->inst_i0 = args [0];
4173 ins->inst_i1 = args [1];
4175 #endif /* MONO_ARCH_HAVE_ATOMIC_EXCHANGE */
4177 #ifdef MONO_ARCH_HAVE_ATOMIC_CAS_IMM
4179 * Can't implement CompareExchange methods this way since they have
4180 * three arguments. We can implement one of the common cases, where the new
4181 * value is a constant.
4183 if ((strcmp (cmethod->name, "CompareExchange") == 0)) {
4184 if (fsig->params [1]->type == MONO_TYPE_I4 && args [2]->opcode == OP_ICONST) {
4185 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_IMM_I4);
4186 ins->inst_i0 = args [0];
4187 ins->inst_i1 = args [1];
4188 ins->backend.data = GINT_TO_POINTER (args [2]->inst_c0);
4190 /* The I8 case is hard to detect, since the arg might be a conv.i8 (iconst) tree */
4192 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS_IMM */
4196 } else if (cmethod->klass->image == mono_defaults.corlib) {
4197 if (cmethod->name [0] == 'B' && strcmp (cmethod->name, "Break") == 0
4198 && strcmp (cmethod->klass->name, "Debugger") == 0) {
4199 MONO_INST_NEW (cfg, ins, OP_BREAK);
4202 if (cmethod->name [0] == 'g' && strcmp (cmethod->name, "get_IsRunningOnWindows") == 0
4203 && strcmp (cmethod->klass->name, "Environment") == 0) {
4204 #ifdef PLATFORM_WIN32
4205 NEW_ICONST (cfg, ins, 1);
4207 NEW_ICONST (cfg, ins, 0);
4213 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
4217 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
4219 MonoInst *store, *temp;
4222 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
4224 if (!sig->hasthis && sig->param_count == 0)
4228 if (sp [0]->opcode == OP_ICONST) {
4231 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
4233 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4234 /* FIXME: handle CEE_STIND_R4 */
4235 store->cil_code = sp [0]->cil_code;
4236 MONO_ADD_INS (bblock, store);
4241 for (i = 0; i < sig->param_count; ++i) {
4242 if (sp [0]->opcode == OP_ICONST) {
4245 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
4247 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4248 store->cil_code = sp [0]->cil_code;
4249 /* FIXME: handle CEE_STIND_R4 */
4250 if (store->opcode == CEE_STOBJ) {
4251 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4252 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
4253 #ifdef MONO_ARCH_SOFT_FLOAT
4254 } else if (store->opcode == CEE_STIND_R4) {
4255 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4256 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
4259 MONO_ADD_INS (bblock, store);
4265 #define MONO_INLINE_CALLED_LIMITED_METHODS 0
4266 #define MONO_INLINE_CALLER_LIMITED_METHODS 0
4268 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4270 mono_inline_called_method_name_limit = NULL;
4271 static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
4272 char *called_method_name = mono_method_full_name (called_method, TRUE);
4275 if (mono_inline_called_method_name_limit == NULL) {
4276 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
4277 if (limit_string != NULL) {
4278 mono_inline_called_method_name_limit = limit_string;
4280 mono_inline_called_method_name_limit = (char *) "";
4284 strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
4285 g_free (called_method_name);
4287 //return (strncmp_result <= 0);
4288 return (strncmp_result == 0);
4292 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4294 mono_inline_caller_method_name_limit = NULL;
4295 static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
4296 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
4299 if (mono_inline_caller_method_name_limit == NULL) {
4300 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
4301 if (limit_string != NULL) {
4302 mono_inline_caller_method_name_limit = limit_string;
4304 mono_inline_caller_method_name_limit = (char *) "";
4308 strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
4309 g_free (caller_method_name);
4311 //return (strncmp_result <= 0);
4312 return (strncmp_result == 0);
4317 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
4318 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
4320 MonoInst *ins, *rvar = NULL;
4321 MonoMethodHeader *cheader;
4322 MonoBasicBlock *ebblock, *sbblock;
4323 int i, costs, new_locals_offset;
4324 MonoMethod *prev_inlined_method;
4325 MonoBasicBlock **prev_cil_offset_to_bb;
4326 unsigned char* prev_cil_start;
4327 guint32 prev_cil_offset_to_bb_len;
4329 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
4331 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4332 if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
4335 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4336 if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
4340 if (bblock->out_of_line && !inline_allways)
4343 if (cfg->verbose_level > 2)
4344 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4346 if (!cmethod->inline_info) {
4347 mono_jit_stats.inlineable_methods++;
4348 cmethod->inline_info = 1;
4350 /* allocate space to store the return value */
4351 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4352 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4355 /* allocate local variables */
4356 cheader = mono_method_get_header (cmethod);
4357 new_locals_offset = cfg->num_varinfo;
4358 for (i = 0; i < cheader->num_locals; ++i)
4359 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
4361 /* allocate starte and end blocks */
4362 sbblock = NEW_BBLOCK (cfg);
4363 sbblock->block_num = cfg->num_bblocks++;
4364 sbblock->real_offset = real_offset;
4366 ebblock = NEW_BBLOCK (cfg);
4367 ebblock->block_num = cfg->num_bblocks++;
4368 ebblock->real_offset = real_offset;
4370 prev_inlined_method = cfg->inlined_method;
4371 cfg->inlined_method = cmethod;
4372 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
4373 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
4374 prev_cil_start = cfg->cil_start;
4376 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
4378 cfg->inlined_method = prev_inlined_method;
4379 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
4380 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
4381 cfg->cil_start = prev_cil_start;
4383 if ((costs >= 0 && costs < 60) || inline_allways) {
4384 if (cfg->verbose_level > 2)
4385 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4387 mono_jit_stats.inlined_methods++;
4389 /* always add some code to avoid block split failures */
4390 MONO_INST_NEW (cfg, ins, OP_NOP);
4391 MONO_ADD_INS (bblock, ins);
4394 bblock->next_bb = sbblock;
4395 link_bblock (cfg, bblock, sbblock);
4398 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
4399 NEW_TEMPLOAD_SOFT_FLOAT (cfg, ebblock, ins, rvar->inst_c0, ip);
4405 if (cfg->verbose_level > 2)
4406 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
4407 cfg->exception_type = MONO_EXCEPTION_NONE;
4408 mono_loader_clear_error ();
4409 cmethod->inline_failure = TRUE;
4415 * Some of these comments may well be out-of-date.
4416 * Design decisions: we do a single pass over the IL code (and we do bblock
4417 * splitting/merging in the few cases when it's required: a back jump to an IL
4418 * address that was not already seen as bblock starting point).
4419 * Code is validated as we go (full verification is still better left to metadata/verify.c).
4420 * Complex operations are decomposed in simpler ones right away. We need to let the
4421 * arch-specific code peek and poke inside this process somehow (except when the
4422 * optimizations can take advantage of the full semantic info of coarse opcodes).
4423 * All the opcodes of the form opcode.s are 'normalized' to opcode.
4424 * MonoInst->opcode initially is the IL opcode or some simplification of that
4425 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
4426 * opcode with value bigger than OP_LAST.
4427 * At this point the IR can be handed over to an interpreter, a dumb code generator
4428 * or to the optimizing code generator that will translate it to SSA form.
4430 * Profiling directed optimizations.
4431 * We may compile by default with few or no optimizations and instrument the code
4432 * or the user may indicate what methods to optimize the most either in a config file
4433 * or through repeated runs where the compiler applies offline the optimizations to
4434 * each method and then decides if it was worth it.
4438 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
4439 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
4440 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
4441 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
4442 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
4443 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
4444 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
4445 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) {cfg->exception_ptr = klass; goto load_error;}
4447 /* offset from br.s -> br like opcodes */
4448 #define BIG_BRANCH_OFFSET 13
4450 static inline gboolean
4451 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
4453 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
4455 return b == NULL || b == bb;
4459 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
4461 unsigned char *ip = start;
4462 unsigned char *target;
4465 MonoBasicBlock *bblock;
4466 const MonoOpcode *opcode;
4469 cli_addr = ip - start;
4470 i = mono_opcode_value ((const guint8 **)&ip, end);
4473 opcode = &mono_opcodes [i];
4474 switch (opcode->argument) {
4475 case MonoInlineNone:
4478 case MonoInlineString:
4479 case MonoInlineType:
4480 case MonoInlineField:
4481 case MonoInlineMethod:
4484 case MonoShortInlineR:
4491 case MonoShortInlineVar:
4492 case MonoShortInlineI:
4495 case MonoShortInlineBrTarget:
4496 target = start + cli_addr + 2 + (signed char)ip [1];
4497 GET_BBLOCK (cfg, bblock, target);
4500 GET_BBLOCK (cfg, bblock, ip);
4502 case MonoInlineBrTarget:
4503 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
4504 GET_BBLOCK (cfg, bblock, target);
4507 GET_BBLOCK (cfg, bblock, ip);
4509 case MonoInlineSwitch: {
4510 guint32 n = read32 (ip + 1);
4513 cli_addr += 5 + 4 * n;
4514 target = start + cli_addr;
4515 GET_BBLOCK (cfg, bblock, target);
4517 for (j = 0; j < n; ++j) {
4518 target = start + cli_addr + (gint32)read32 (ip);
4519 GET_BBLOCK (cfg, bblock, target);
4529 g_assert_not_reached ();
4532 if (i == CEE_THROW) {
4533 unsigned char *bb_start = ip - 1;
4535 /* Find the start of the bblock containing the throw */
4537 while ((bb_start >= start) && !bblock) {
4538 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
4542 bblock->out_of_line = 1;
4552 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
4554 MonoInst *store, *temp, *load;
4556 if (ip_in_bb (cfg, bblock, ip_next) &&
4557 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
4560 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4561 temp->flags |= MONO_INST_IS_TEMP;
4562 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4563 /* FIXME: handle CEE_STIND_R4 */
4564 store->cil_code = ins->cil_code;
4565 MONO_ADD_INS (bblock, store);
4566 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4567 load->cil_code = ins->cil_code;
4571 static inline MonoMethod *
4572 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4576 if (m->wrapper_type != MONO_WRAPPER_NONE)
4577 return mono_method_get_wrapper_data (m, token);
4579 method = mono_get_method_full (m->klass->image, token, klass, context);
4584 static inline MonoMethod *
4585 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4587 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
4589 if (method && cfg && !cfg->generic_sharing_context && mono_class_is_open_constructed_type (&method->klass->byval_arg))
4595 static inline MonoClass*
4596 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
4600 if (method->wrapper_type != MONO_WRAPPER_NONE)
4601 klass = mono_method_get_wrapper_data (method, token);
4603 klass = mono_class_get_full (method->klass->image, token, context);
4605 mono_class_init (klass);
4610 * Returns TRUE if the JIT should abort inlining because "callee"
4611 * is influenced by security attributes.
4614 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
4618 if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
4622 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
4623 if (result == MONO_JIT_SECURITY_OK)
4626 if (result == MONO_JIT_LINKDEMAND_ECMA) {
4627 /* Generate code to throw a SecurityException before the actual call/link */
4628 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4631 NEW_ICONST (cfg, args [0], 4);
4632 NEW_METHODCONST (cfg, args [1], caller);
4633 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
4634 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
4635 /* don't hide previous results */
4636 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
4637 cfg->exception_data = result;
4645 method_access_exception (void)
4647 static MonoMethod *method = NULL;
4650 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4651 method = mono_class_get_method_from_name (secman->securitymanager,
4652 "MethodAccessException", 2);
4659 emit_throw_method_access_exception (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4660 MonoBasicBlock *bblock, unsigned char *ip)
4662 MonoMethod *thrower = method_access_exception ();
4665 NEW_METHODCONST (cfg, args [0], caller);
4666 NEW_METHODCONST (cfg, args [1], callee);
4667 mono_emit_method_call_spilled (cfg, bblock, thrower,
4668 mono_method_signature (thrower), args, ip, NULL);
4672 verification_exception (void)
4674 static MonoMethod *method = NULL;
4677 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4678 method = mono_class_get_method_from_name (secman->securitymanager,
4679 "VerificationException", 0);
4686 emit_throw_verification_exception (MonoCompile *cfg, MonoBasicBlock *bblock, unsigned char *ip)
4688 MonoMethod *thrower = verification_exception ();
4690 mono_emit_method_call_spilled (cfg, bblock, thrower,
4691 mono_method_signature (thrower),
4696 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4697 MonoBasicBlock *bblock, unsigned char *ip)
4699 MonoSecurityCoreCLRLevel caller_level = mono_security_core_clr_method_level (caller, TRUE);
4700 MonoSecurityCoreCLRLevel callee_level = mono_security_core_clr_method_level (callee, TRUE);
4701 gboolean is_safe = TRUE;
4703 if (!(caller_level >= callee_level ||
4704 caller_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL ||
4705 callee_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL)) {
4710 emit_throw_method_access_exception (cfg, caller, callee, bblock, ip);
4714 method_is_safe (MonoMethod *method)
4717 if (strcmp (method->name, "unsafeMethod") == 0)
4724 * Check that the IL instructions at ip are the array initialization
4725 * sequence and return the pointer to the data and the size.
4728 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoInst *newarr, int *out_size)
4731 * newarr[System.Int32]
4733 * ldtoken field valuetype ...
4734 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
4736 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
4737 MonoClass *klass = newarr->inst_newa_class;
4738 guint32 field_token = read32 (ip + 2);
4739 guint32 field_index = field_token & 0xffffff;
4740 guint32 token = read32 (ip + 7);
4742 const char *data_ptr;
4744 MonoMethod *cmethod;
4745 MonoClass *dummy_class;
4746 MonoClassField *field = mono_field_from_token (method->klass->image, field_token, &dummy_class, NULL);
4752 if (newarr->inst_newa_len->opcode != OP_ICONST)
4754 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
4757 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
4759 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
4760 case MONO_TYPE_BOOLEAN:
4764 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
4765 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
4766 case MONO_TYPE_CHAR:
4776 return NULL; /* stupid ARM FP swapped format */
4785 size *= newarr->inst_newa_len->inst_c0;
4786 if (size > mono_type_size (field->type, &dummy_align))
4789 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
4790 field_index = read32 (ip + 2) & 0xffffff;
4791 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
4792 data_ptr = mono_image_rva_map (method->klass->image, rva);
4793 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
4794 /* for aot code we do the lookup on load */
4795 if (aot && data_ptr)
4796 return GUINT_TO_POINTER (rva);
4803 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
4805 char *method_fname = mono_method_full_name (method, TRUE);
4808 if (mono_method_get_header (method)->code_size == 0)
4809 method_code = g_strdup ("method body is empty.");
4811 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
4812 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
4813 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
4814 g_free (method_fname);
4815 g_free (method_code);
4819 set_exception_object (MonoCompile *cfg, MonoException *exception)
4821 cfg->exception_type = MONO_EXCEPTION_OBJECT_SUPPLIED;
4822 MONO_GC_REGISTER_ROOT (cfg->exception_ptr);
4823 cfg->exception_ptr = exception;
4827 get_runtime_generic_context (MonoCompile *cfg, MonoMethod *method, int context_used, MonoInst *this, unsigned char *ip)
4829 g_assert (cfg->generic_sharing_context);
4831 if (method->klass->valuetype)
4834 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
4835 MonoInst *mrgctx_loc, *mrgctx_var;
4838 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
4840 mrgctx_loc = mono_get_vtable_var (cfg);
4841 NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
4844 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
4845 MonoInst *vtable_loc, *vtable_var;
4849 vtable_loc = mono_get_vtable_var (cfg);
4850 NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
4852 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
4853 MonoInst *mrgctx_var = vtable_var;
4855 g_assert (G_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable) == 0);
4857 MONO_INST_NEW (cfg, vtable_var, CEE_LDIND_I);
4858 vtable_var->cil_code = ip;
4859 vtable_var->inst_left = mrgctx_var;
4860 vtable_var->type = STACK_PTR;
4869 MONO_INST_NEW (cfg, vtable, CEE_LDIND_I);
4870 vtable->inst_left = this;
4871 vtable->type = STACK_PTR;
4878 get_runtime_generic_context_other_table_ptr (MonoCompile *cfg, MonoBasicBlock *bblock,
4879 MonoInst *rgc_ptr, guint32 slot, const unsigned char *ip)
4881 MonoMethodSignature *sig = helper_sig_rgctx_lazy_fetch_trampoline;
4882 guint8 *tramp = mono_create_rgctx_lazy_fetch_trampoline (slot);
4886 temp = mono_emit_native_call (cfg, bblock, tramp, sig, &rgc_ptr, ip, FALSE, FALSE);
4888 NEW_TEMPLOAD (cfg, field, temp);
4894 get_runtime_generic_context_ptr (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4895 MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, unsigned char *ip)
4897 guint32 slot = mono_method_lookup_or_register_other_info (method,
4898 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, &klass->byval_arg, rgctx_type, generic_context);
4900 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4904 get_runtime_generic_context_method (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4905 MonoMethod *cmethod, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, const unsigned char *ip)
4907 guint32 slot = mono_method_lookup_or_register_other_info (method,
4908 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, cmethod, rgctx_type, generic_context);
4910 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4914 get_runtime_generic_context_field (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4915 MonoClassField *field, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type,
4916 const unsigned char *ip)
4918 guint32 slot = mono_method_lookup_or_register_other_info (method,
4919 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, field, rgctx_type, generic_context);
4921 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4925 get_runtime_generic_context_method_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4926 MonoMethod *rgctx_method, MonoGenericContext *generic_context, MonoInst *rgctx, const unsigned char *ip)
4928 guint32 slot = mono_method_lookup_or_register_other_info (method,
4929 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, rgctx_method,
4930 MONO_RGCTX_INFO_METHOD_RGCTX, generic_context);
4932 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4936 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4940 if (cfg->generic_sharing_context)
4941 type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, &klass->byval_arg);
4943 type = &klass->byval_arg;
4944 return MONO_TYPE_IS_REFERENCE (type);
4948 * Handles unbox of a Nullable<T>, returning a temp variable where the
4949 * result is stored. If a rgctx is passed, then shared generic code
4953 handle_unbox_nullable (MonoCompile* cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock* bblock,
4954 MonoInst* val, const guchar *ip, MonoClass* klass, MonoGenericContext *generic_context, MonoInst *rgctx)
4956 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4957 MonoMethodSignature *signature = mono_method_signature (method);
4960 /* FIXME: What if the class is shared? We might not
4961 have to get the address of the method from the
4963 MonoInst *addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
4964 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
4966 return mono_emit_rgctx_calli_spilled (cfg, bblock, signature, &val, addr, NULL, ip);
4968 return mono_emit_method_call_spilled (cfg, bblock, method, signature, &val, ip, NULL);
4973 handle_box_nullable_from_inst (MonoCompile *cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock *bblock,
4974 MonoInst *val, const guchar *ip, MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx)
4976 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4977 MonoInst *dest, *method_addr;
4980 g_assert (mono_class_is_nullable (klass));
4982 /* FIXME: What if the class is shared? We might not have to
4983 get the method address from the RGCTX. */
4984 method_addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
4985 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
4986 temp = mono_emit_rgctx_calli_spilled (cfg, bblock, mono_method_signature (method), &val,
4987 method_addr, NULL, ip);
4988 NEW_TEMPLOAD (cfg, dest, temp);
4993 emit_castclass (MonoClass *klass, guint32 token, int context_used, gboolean inst_is_castclass, MonoCompile *cfg,
4994 MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
4995 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
4996 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
4998 MonoBasicBlock *bblock = *_bblock;
4999 unsigned char *ip = *_ip;
5000 MonoInst **sp = *_sp;
5001 int inline_costs = *_inline_costs;
5002 guint real_offset = *_real_offset;
5003 int return_value = 0;
5006 MonoInst *rgctx, *args [2];
5009 g_assert (!method->klass->valuetype);
5015 GET_RGCTX (rgctx, context_used);
5016 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
5017 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5019 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass, args, ip);
5020 NEW_TEMPLOAD (cfg, *sp, temp);
5025 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5027 MonoMethod *mono_castclass;
5028 MonoInst *iargs [1];
5029 MonoBasicBlock *ebblock;
5033 mono_castclass = mono_marshal_get_castclass (klass);
5036 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5037 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5039 g_assert (costs > 0);
5044 GET_BBLOCK (cfg, bblock, ip);
5045 ebblock->next_bb = bblock;
5046 link_bblock (cfg, ebblock, bblock);
5048 temp = iargs [0]->inst_i0->inst_c0;
5049 NEW_TEMPLOAD (cfg, *sp, temp);
5053 inline_costs += costs;
5057 /* Needed by the code generated in inssel.brg */
5058 mono_get_got_var (cfg);
5060 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5061 ins->type = STACK_OBJ;
5062 ins->inst_left = *sp;
5064 ins->inst_newa_class = klass;
5065 if (inst_is_castclass)
5066 ins->backend.record_cast_details = debug_options.better_cast_details;
5067 if (inst_is_castclass)
5068 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5078 *_inline_costs = inline_costs;
5079 *_real_offset = real_offset;
5080 return return_value;
5087 emit_unbox (MonoClass *klass, guint32 token, int context_used,
5088 MonoCompile *cfg, MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
5089 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
5090 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
5092 MonoBasicBlock *bblock = *_bblock;
5093 unsigned char *ip = *_ip;
5094 MonoInst **sp = *_sp;
5095 int inline_costs = *_inline_costs;
5096 guint real_offset = *_real_offset;
5097 int return_value = 0;
5099 MonoInst *add, *vtoffset, *ins;
5101 /* Needed by the code generated in inssel.brg */
5102 mono_get_got_var (cfg);
5105 MonoInst *rgctx, *element_class;
5107 /* This assertion is from the unboxcast insn */
5108 g_assert (klass->rank == 0);
5110 GET_RGCTX (rgctx, context_used);
5111 element_class = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
5112 klass->element_class, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5114 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST_REG);
5115 ins->type = STACK_OBJ;
5116 ins->inst_left = *sp;
5117 ins->inst_right = element_class;
5121 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5122 ins->type = STACK_OBJ;
5123 ins->inst_left = *sp;
5125 ins->inst_newa_class = klass;
5129 MONO_INST_NEW (cfg, add, OP_PADD);
5130 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5131 add->inst_left = ins;
5132 add->inst_right = vtoffset;
5133 add->type = STACK_MP;
5140 *_inline_costs = inline_costs;
5141 *_real_offset = real_offset;
5142 return return_value;
5146 mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method)
5148 MonoAssembly *assembly = method->klass->image->assembly;
5149 if (method->wrapper_type != MONO_WRAPPER_NONE)
5151 if (assembly->in_gac || assembly->image == mono_defaults.corlib)
5153 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE)
5155 return mono_assembly_has_skip_verification (assembly);
5159 * mini_method_verify:
5161 * Verify the method using the new verfier.
5163 * Returns true if the method is invalid.
5166 mini_method_verify (MonoCompile *cfg, MonoMethod *method)
5169 gboolean is_fulltrust;
5170 MonoLoaderError *error;
5172 if (method->verification_success)
5175 is_fulltrust = mono_verifier_is_method_full_trust (method);
5177 if (!mono_verifier_is_enabled_for_method (method))
5180 res = mono_method_verify_with_current_settings (method, cfg->skip_visibility);
5182 if ((error = mono_loader_get_last_error ())) {
5183 cfg->exception_type = error->exception_type;
5185 mono_free_verify_list (res);
5190 for (tmp = res; tmp; tmp = tmp->next) {
5191 MonoVerifyInfoExtended *info = (MonoVerifyInfoExtended *)tmp->data;
5192 if (info->info.status == MONO_VERIFY_ERROR) {
5193 cfg->exception_type = info->exception_type;
5194 cfg->exception_message = g_strdup (info->info.message);
5195 mono_free_verify_list (res);
5198 if (info->info.status == MONO_VERIFY_NOT_VERIFIABLE && !is_fulltrust) {
5199 cfg->exception_type = info->exception_type;
5200 cfg->exception_message = g_strdup (info->info.message);
5201 mono_free_verify_list (res);
5205 mono_free_verify_list (res);
5207 method->verification_success = 1;
5212 * mono_method_to_ir: translates IL into basic blocks containing trees
5215 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
5216 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
5217 guint inline_offset, gboolean is_virtual_call)
5219 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
5220 MonoInst *ins, **sp, **stack_start;
5221 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
5222 MonoMethod *cmethod, *method_definition;
5223 MonoInst **arg_array;
5224 MonoMethodHeader *header;
5226 guint32 token, ins_flag;
5228 MonoClass *constrained_call = NULL;
5229 unsigned char *ip, *end, *target, *err_pos;
5230 static double r8_0 = 0.0;
5231 MonoMethodSignature *sig;
5232 MonoGenericContext *generic_context = NULL;
5233 MonoGenericContainer *generic_container = NULL;
5234 MonoType **param_types;
5235 GList *bb_recheck = NULL, *tmp;
5236 int i, n, start_new_bblock, ialign;
5237 int num_calls = 0, inline_costs = 0;
5238 int breakpoint_id = 0;
5240 guint real_offset, num_args;
5241 MonoBoolean security, pinvoke;
5242 MonoSecurityManager* secman = NULL;
5243 MonoDeclSecurityActions actions;
5244 GSList *class_inits = NULL;
5245 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
5248 /* serialization and xdomain stuff may need access to private fields and methods */
5249 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
5250 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
5251 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
5252 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
5253 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
5254 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
5256 /* turn off visibility checks for smcs */
5257 dont_verify |= mono_security_get_mode () == MONO_SECURITY_MODE_SMCS_HACK;
5259 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
5260 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
5261 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
5262 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
5264 image = method->klass->image;
5265 header = mono_method_get_header (method);
5266 generic_container = mono_method_get_generic_container (method);
5267 sig = mono_method_signature (method);
5268 num_args = sig->hasthis + sig->param_count;
5269 ip = (unsigned char*)header->code;
5270 cfg->cil_start = ip;
5271 end = ip + header->code_size;
5272 mono_jit_stats.cil_code_size += header->code_size;
5274 method_definition = method;
5275 while (method_definition->is_inflated) {
5276 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
5277 method_definition = imethod->declaring;
5280 /* SkipVerification is not allowed if core-clr is enabled */
5281 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
5283 dont_verify_stloc = TRUE;
5286 if (!dont_verify && mini_method_verify (cfg, method_definition))
5287 goto exception_exit;
5289 if (mono_debug_using_mono_debugger ())
5290 cfg->keep_cil_nops = TRUE;
5292 if (sig->is_inflated)
5293 generic_context = mono_method_get_context (method);
5294 else if (generic_container)
5295 generic_context = &generic_container->context;
5297 if (!cfg->generic_sharing_context)
5298 g_assert (!sig->has_type_parameters);
5300 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
5301 g_assert (method->is_inflated);
5302 g_assert (mono_method_get_context (method)->method_inst);
5304 if (method->is_inflated && mono_method_get_context (method)->method_inst)
5305 g_assert (sig->generic_param_count);
5307 if (cfg->method == method)
5310 real_offset = inline_offset;
5312 cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
5313 cfg->cil_offset_to_bb_len = header->code_size;
5315 if (cfg->verbose_level > 2)
5316 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
5318 dont_inline = g_list_prepend (dont_inline, method);
5319 if (cfg->method == method) {
5321 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
5322 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
5325 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
5326 start_bblock->cil_code = NULL;
5327 start_bblock->cil_length = 0;
5328 start_bblock->block_num = cfg->num_bblocks++;
5331 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
5332 end_bblock->cil_code = NULL;
5333 end_bblock->cil_length = 0;
5334 end_bblock->block_num = cfg->num_bblocks++;
5335 g_assert (cfg->num_bblocks == 2);
5337 arg_array = alloca (sizeof (MonoInst *) * num_args);
5338 for (i = num_args - 1; i >= 0; i--)
5339 arg_array [i] = cfg->varinfo [i];
5341 if (header->num_clauses) {
5342 cfg->spvars = g_hash_table_new (NULL, NULL);
5343 cfg->exvars = g_hash_table_new (NULL, NULL);
5345 /* handle exception clauses */
5346 for (i = 0; i < header->num_clauses; ++i) {
5347 MonoBasicBlock *try_bb;
5348 MonoExceptionClause *clause = &header->clauses [i];
5350 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
5351 try_bb->real_offset = clause->try_offset;
5352 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
5353 tblock->real_offset = clause->handler_offset;
5354 tblock->flags |= BB_EXCEPTION_HANDLER;
5356 link_bblock (cfg, try_bb, tblock);
5358 if (*(ip + clause->handler_offset) == CEE_POP)
5359 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
5361 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
5362 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
5363 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
5364 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5365 MONO_ADD_INS (tblock, ins);
5367 /* todo: is a fault block unsafe to optimize? */
5368 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
5369 tblock->flags |= BB_EXCEPTION_UNSAFE;
5373 /*g_print ("clause try IL_%04x to IL_%04x handler %d at IL_%04x to IL_%04x\n", clause->try_offset, clause->try_offset + clause->try_len, clause->flags, clause->handler_offset, clause->handler_offset + clause->handler_len);
5375 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
5377 /* catch and filter blocks get the exception object on the stack */
5378 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
5379 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5380 MonoInst *load, *dummy_use;
5382 /* mostly like handle_stack_args (), but just sets the input args */
5383 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
5384 tblock->in_scount = 1;
5385 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5386 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5389 * Add a dummy use for the exvar so its liveness info will be
5392 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
5393 NEW_DUMMY_USE (cfg, dummy_use, load);
5394 MONO_ADD_INS (tblock, dummy_use);
5396 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5397 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
5398 tblock->real_offset = clause->data.filter_offset;
5399 tblock->in_scount = 1;
5400 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5401 /* The filter block shares the exvar with the handler block */
5402 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5403 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5404 MONO_ADD_INS (tblock, ins);
5408 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
5409 clause->data.catch_class &&
5410 cfg->generic_sharing_context &&
5411 mono_class_check_context_used (clause->data.catch_class)) {
5412 if (mono_method_get_context (method)->method_inst)
5413 GENERIC_SHARING_FAILURE (CEE_NOP);
5416 * In shared generic code with catch
5417 * clauses containing type variables
5418 * the exception handling code has to
5419 * be able to get to the rgctx.
5420 * Therefore we have to make sure that
5421 * the vtable/mrgctx argument (for
5422 * static or generic methods) or the
5423 * "this" argument (for non-static
5424 * methods) are live.
5426 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
5427 mini_method_get_context (method)->method_inst ||
5428 method->klass->valuetype) {
5429 mono_get_vtable_var (cfg);
5431 MonoInst *this, *dummy_use;
5432 MonoType *this_type;
5434 if (method->klass->valuetype)
5435 this_type = &method->klass->this_arg;
5437 this_type = &method->klass->byval_arg;
5439 if (arg_array [0]->opcode == OP_ICONST) {
5440 this = arg_array [0];
5442 this = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
5443 this->ssa_op = MONO_SSA_LOAD;
5444 this->inst_i0 = arg_array [0];
5445 this->opcode = mini_type_to_ldind ((cfg), this->inst_i0->inst_vtype);
5446 type_to_eval_stack_type ((cfg), this_type, this);
5447 this->klass = this->inst_i0->klass;
5450 NEW_DUMMY_USE (cfg, dummy_use, this);
5451 MONO_ADD_INS (tblock, dummy_use);
5456 arg_array = alloca (sizeof (MonoInst *) * num_args);
5457 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
5460 /* FIRST CODE BLOCK */
5461 bblock = NEW_BBLOCK (cfg);
5462 bblock->cil_code = ip;
5464 ADD_BBLOCK (cfg, bblock);
5466 if (cfg->method == method) {
5467 breakpoint_id = mono_debugger_method_has_breakpoint (method);
5468 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
5469 MONO_INST_NEW (cfg, ins, OP_BREAK);
5470 MONO_ADD_INS (bblock, ins);
5474 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
5475 secman = mono_security_manager_get_methods ();
5477 security = (secman && mono_method_has_declsec (method));
5478 /* at this point having security doesn't mean we have any code to generate */
5479 if (security && (cfg->method == method)) {
5480 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
5481 * And we do not want to enter the next section (with allocation) if we
5482 * have nothing to generate */
5483 security = mono_declsec_get_demands (method, &actions);
5486 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
5487 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
5489 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5490 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5491 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
5493 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
5494 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5498 mono_custom_attrs_free (custom);
5501 custom = mono_custom_attrs_from_class (wrapped->klass);
5502 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5506 mono_custom_attrs_free (custom);
5509 /* not a P/Invoke after all */
5514 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || cfg->compile_aot || security || pinvoke) {
5515 /* we use a separate basic block for the initialization code */
5516 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
5517 init_localsbb->real_offset = real_offset;
5518 start_bblock->next_bb = init_localsbb;
5519 init_localsbb->next_bb = bblock;
5520 link_bblock (cfg, start_bblock, init_localsbb);
5521 link_bblock (cfg, init_localsbb, bblock);
5522 init_localsbb->block_num = cfg->num_bblocks++;
5524 start_bblock->next_bb = bblock;
5525 link_bblock (cfg, start_bblock, bblock);
5528 /* at this point we know, if security is TRUE, that some code needs to be generated */
5529 if (security && (cfg->method == method)) {
5532 mono_jit_stats.cas_demand_generation++;
5534 if (actions.demand.blob) {
5535 /* Add code for SecurityAction.Demand */
5536 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
5537 NEW_ICONST (cfg, args [1], actions.demand.size);
5538 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5539 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5541 if (actions.noncasdemand.blob) {
5542 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
5543 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
5544 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
5545 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
5546 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5547 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5549 if (actions.demandchoice.blob) {
5550 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
5551 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
5552 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
5553 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
5554 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
5558 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
5560 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
5563 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
5564 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
5565 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5566 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5567 if (!(method->klass && method->klass->image &&
5568 mono_security_core_clr_is_platform_image (method->klass->image))) {
5569 emit_throw_method_access_exception (cfg, method, wrapped, bblock, ip);
5573 if (!method_is_safe (method))
5574 emit_throw_verification_exception (cfg, bblock, ip);
5577 if (header->code_size == 0)
5580 if (get_basic_blocks (cfg, header, real_offset, ip, end, &err_pos)) {
5585 if (cfg->method == method)
5586 mono_debug_init_method (cfg, bblock, breakpoint_id);
5588 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
5590 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
5591 for (n = 0; n < sig->param_count; ++n)
5592 param_types [n + sig->hasthis] = sig->params [n];
5593 for (n = 0; n < header->num_locals; ++n) {
5594 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
5599 /* do this somewhere outside - not here */
5600 NEW_ICONST (cfg, zero_int32, 0);
5601 NEW_ICONST (cfg, zero_int64, 0);
5602 zero_int64->type = STACK_I8;
5603 NEW_PCONST (cfg, zero_ptr, 0);
5604 NEW_PCONST (cfg, zero_obj, 0);
5605 zero_obj->type = STACK_OBJ;
5607 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
5608 zero_r8->type = STACK_R8;
5609 zero_r8->inst_p0 = &r8_0;
5611 /* add a check for this != NULL to inlined methods */
5612 if (is_virtual_call) {
5613 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
5614 NEW_ARGLOAD (cfg, ins->inst_left, 0);
5616 MONO_ADD_INS (bblock, ins);
5619 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
5620 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
5623 start_new_bblock = 0;
5626 if (cfg->method == method)
5627 real_offset = ip - header->code;
5629 real_offset = inline_offset;
5634 if (start_new_bblock) {
5635 bblock->cil_length = ip - bblock->cil_code;
5636 if (start_new_bblock == 2) {
5637 g_assert (ip == tblock->cil_code);
5639 GET_BBLOCK (cfg, tblock, ip);
5641 bblock->next_bb = tblock;
5643 start_new_bblock = 0;
5644 for (i = 0; i < bblock->in_scount; ++i) {
5645 if (cfg->verbose_level > 3)
5646 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5647 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5650 g_slist_free (class_inits);
5653 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
5654 link_bblock (cfg, bblock, tblock);
5655 if (sp != stack_start) {
5656 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5658 CHECK_UNVERIFIABLE (cfg);
5660 bblock->next_bb = tblock;
5662 for (i = 0; i < bblock->in_scount; ++i) {
5663 if (cfg->verbose_level > 3)
5664 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5665 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5668 g_slist_free (class_inits);
5673 bblock->real_offset = real_offset;
5675 if ((cfg->method == method) && cfg->coverage_info) {
5676 MonoInst *store, *one;
5677 guint32 cil_offset = ip - header->code;
5678 cfg->coverage_info->data [cil_offset].cil_code = ip;
5680 /* TODO: Use an increment here */
5681 NEW_ICONST (cfg, one, 1);
5684 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
5687 MONO_INST_NEW (cfg, store, CEE_STIND_I);
5688 store->inst_left = ins;
5689 store->inst_right = one;
5691 MONO_ADD_INS (bblock, store);
5694 if (cfg->verbose_level > 3)
5695 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
5699 if (cfg->keep_cil_nops)
5700 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
5702 MONO_INST_NEW (cfg, ins, OP_NOP);
5704 MONO_ADD_INS (bblock, ins);
5707 MONO_INST_NEW (cfg, ins, OP_BREAK);
5709 MONO_ADD_INS (bblock, ins);
5715 CHECK_STACK_OVF (1);
5716 n = (*ip)-CEE_LDARG_0;
5718 NEW_ARGLOAD (cfg, ins, n);
5719 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
5727 CHECK_STACK_OVF (1);
5728 n = (*ip)-CEE_LDLOC_0;
5730 NEW_LOCLOAD (cfg, ins, n);
5731 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
5740 n = (*ip)-CEE_STLOC_0;
5743 handle_loaded_temps (cfg, bblock, stack_start, sp);
5744 NEW_LOCSTORE (cfg, ins, n, *sp);
5745 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
5747 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
5748 if (ins->opcode == CEE_STOBJ) {
5749 NEW_LOCLOADA (cfg, ins, n);
5750 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5752 MONO_ADD_INS (bblock, ins);
5758 CHECK_STACK_OVF (1);
5760 NEW_ARGLOAD (cfg, ins, ip [1]);
5761 LDARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5767 CHECK_STACK_OVF (1);
5769 NEW_ARGLOADA (cfg, ins, ip [1]);
5778 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
5779 handle_loaded_temps (cfg, bblock, stack_start, sp);
5780 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
5782 STARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5783 if (ins->opcode == CEE_STOBJ) {
5784 NEW_ARGLOADA (cfg, ins, ip [1]);
5785 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5787 MONO_ADD_INS (bblock, ins);
5792 CHECK_STACK_OVF (1);
5793 CHECK_LOCAL (ip [1]);
5794 NEW_LOCLOAD (cfg, ins, ip [1]);
5795 LDLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5801 CHECK_STACK_OVF (1);
5802 CHECK_LOCAL (ip [1]);
5803 NEW_LOCLOADA (cfg, ins, ip [1]);
5811 handle_loaded_temps (cfg, bblock, stack_start, sp);
5812 CHECK_LOCAL (ip [1]);
5813 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
5814 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
5816 STLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5817 if (ins->opcode == CEE_STOBJ) {
5818 NEW_LOCLOADA (cfg, ins, ip [1]);
5819 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5821 MONO_ADD_INS (bblock, ins);
5826 CHECK_STACK_OVF (1);
5827 NEW_PCONST (cfg, ins, NULL);
5828 ins->type = STACK_OBJ;
5833 CHECK_STACK_OVF (1);
5834 NEW_ICONST (cfg, ins, -1);
5847 CHECK_STACK_OVF (1);
5848 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
5854 CHECK_STACK_OVF (1);
5856 NEW_ICONST (cfg, ins, *((signed char*)ip));
5862 CHECK_STACK_OVF (1);
5863 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
5869 CHECK_STACK_OVF (1);
5870 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5871 ins->type = STACK_I8;
5873 ins->inst_l = (gint64)read64 (ip);
5879 /* we should really allocate this only late in the compilation process */
5880 mono_domain_lock (cfg->domain);
5881 f = mono_domain_alloc (cfg->domain, sizeof (float));
5882 mono_domain_unlock (cfg->domain);
5884 CHECK_STACK_OVF (1);
5885 MONO_INST_NEW (cfg, ins, OP_R4CONST);
5886 ins->type = STACK_R8;
5897 mono_domain_lock (cfg->domain);
5898 d = mono_domain_alloc (cfg->domain, sizeof (double));
5899 mono_domain_unlock (cfg->domain);
5901 CHECK_STACK_OVF (1);
5902 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5903 ins->type = STACK_R8;
5913 MonoInst *temp, *store;
5915 CHECK_STACK_OVF (1);
5920 * small optimization: if the loaded value was from a local already,
5921 * just load it twice.
5923 if (ins->ssa_op == MONO_SSA_LOAD &&
5924 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
5926 MONO_INST_NEW (cfg, temp, 0);
5930 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5931 temp->flags |= MONO_INST_IS_TEMP;
5932 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5933 /* FIXME: handle CEE_STIND_R4 */
5934 if (store->opcode == CEE_STOBJ) {
5935 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
5936 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
5938 MONO_ADD_INS (bblock, store);
5940 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5942 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5951 MONO_INST_NEW (cfg, ins, CEE_POP);
5952 MONO_ADD_INS (bblock, ins);
5959 if (stack_start != sp)
5961 MONO_INST_NEW (cfg, ins, OP_JMP);
5962 token = read32 (ip + 1);
5963 /* FIXME: check the signature matches */
5964 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
5969 if (cfg->generic_sharing_context && mono_method_check_context_used (cmethod))
5970 GENERIC_SHARING_FAILURE (CEE_JMP);
5972 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
5973 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
5975 CHECK_CFG_EXCEPTION;
5978 ins->inst_p0 = cmethod;
5979 MONO_ADD_INS (bblock, ins);
5981 start_new_bblock = 1;
5985 case CEE_CALLVIRT: {
5986 MonoInst *addr = NULL;
5987 MonoMethodSignature *fsig = NULL;
5988 int temp, array_rank = 0;
5989 int virtual = *ip == CEE_CALLVIRT;
5991 gboolean pass_imt_from_rgctx = FALSE;
5992 MonoInst *imt_arg = NULL;
5993 gboolean pass_vtable = FALSE;
5994 gboolean pass_mrgctx = FALSE;
5995 MonoInst *vtable_arg = NULL;
5996 gboolean check_this = FALSE;
5999 token = read32 (ip + 1);
6001 if (*ip == CEE_CALLI) {
6006 if (method->wrapper_type != MONO_WRAPPER_NONE)
6007 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6009 fsig = mono_metadata_parse_signature (image, token);
6011 n = fsig->param_count + fsig->hasthis;
6013 MonoMethod *cil_method;
6015 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6016 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
6017 cil_method = cmethod;
6018 } else if (constrained_call) {
6019 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
6020 cil_method = cmethod;
6022 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
6023 cil_method = cmethod;
6028 if (!dont_verify && !cfg->skip_visibility) {
6029 MonoMethod *target_method = cil_method;
6030 if (method->is_inflated) {
6031 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
6033 if (!mono_method_can_access_method (method_definition, target_method) &&
6034 !mono_method_can_access_method (method, cil_method))
6035 METHOD_ACCESS_FAILURE;
6038 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
6039 ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
6041 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
6042 /* MS.NET seems to silently convert this to a callvirt */
6045 if (!cmethod->klass->inited){
6046 if (!mono_class_init (cmethod->klass))
6050 if (mono_method_signature (cmethod)->pinvoke) {
6051 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6052 fsig = mono_method_signature (wrapper);
6053 } else if (constrained_call) {
6054 fsig = mono_method_signature (cmethod);
6056 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
6059 mono_save_token_info (cfg, image, token, cmethod);
6061 n = fsig->param_count + fsig->hasthis;
6063 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
6064 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6066 CHECK_CFG_EXCEPTION;
6069 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
6070 mini_class_is_system_array (cmethod->klass)) {
6071 array_rank = cmethod->klass->rank;
6074 if (cmethod->string_ctor)
6075 g_assert_not_reached ();
6079 if (!cfg->generic_sharing_context && cmethod && cmethod->klass->generic_container)
6082 if (!cfg->generic_sharing_context && cmethod)
6083 g_assert (!mono_method_check_context_used (cmethod));
6087 //g_assert (!virtual || fsig->hasthis);
6091 if (constrained_call) {
6093 * We have the `constrained.' prefix opcode.
6095 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
6098 * The type parameter is instantiated as a valuetype,
6099 * but that type doesn't override the method we're
6100 * calling, so we need to box `this'.
6101 * sp [0] is a pointer to the data: we need the value
6102 * in handle_box (), so load it here.
6104 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &constrained_call->byval_arg));
6105 type_to_eval_stack_type (cfg, &constrained_call->byval_arg, load);
6106 load->inst_left = sp [0];
6107 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
6108 } else if (!constrained_call->valuetype) {
6112 * The type parameter is instantiated as a reference
6113 * type. We have a managed pointer on the stack, so
6114 * we need to dereference it here.
6117 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
6118 ins->inst_i0 = sp [0];
6119 ins->type = STACK_OBJ;
6120 ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
6122 } else if (cmethod->klass->valuetype)
6124 constrained_call = NULL;
6127 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
6130 if (cmethod && ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
6131 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
6132 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6133 MonoGenericContext *context = mini_class_get_context (cmethod->klass);
6134 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6137 * Pass vtable iff target method might
6138 * be shared, which means that sharing
6139 * is enabled for its class and its
6140 * context is sharable (and it's not a
6143 if (sharing_enabled && context_sharable &&
6144 !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
6148 if (cmethod && mini_method_get_context (cmethod) &&
6149 mini_method_get_context (cmethod)->method_inst) {
6150 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6151 MonoGenericContext *context = mini_method_get_context (cmethod);
6152 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6154 g_assert (!pass_vtable);
6156 if (sharing_enabled && context_sharable)
6160 if (cfg->generic_sharing_context && cmethod) {
6161 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
6163 context_used = mono_method_check_context_used (cmethod);
6165 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
6166 /* Generic method interface
6167 calls are resolved via a
6168 helper function and don't
6170 if (!cmethod_context || !cmethod_context->method_inst)
6171 pass_imt_from_rgctx = TRUE;
6175 * If a shared method calls another
6176 * shared method then the caller must
6177 * have a generic sharing context
6178 * because the magic trampoline
6179 * requires it. FIXME: We shouldn't
6180 * have to force the vtable/mrgctx
6181 * variable here. Instead there
6182 * should be a flag in the cfg to
6183 * request a generic sharing context.
6185 if (context_used && ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
6186 mono_get_vtable_var (cfg);
6193 GET_RGCTX (rgctx, context_used);
6194 vtable_arg = get_runtime_generic_context_ptr (cfg, method, context_used,
6195 bblock, cmethod->klass, generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
6197 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
6199 CHECK_TYPELOAD (cmethod->klass);
6200 NEW_VTABLECONST (cfg, vtable_arg, vtable);
6205 g_assert (!vtable_arg);
6210 GET_RGCTX (rgctx, context_used);
6211 vtable_arg = get_runtime_generic_context_method_rgctx (cfg, method,
6212 context_used, bblock, cmethod, generic_context, rgctx, ip);
6214 MonoMethodRuntimeGenericContext *mrgctx;
6216 mrgctx = mono_method_lookup_rgctx (mono_class_vtable (cfg->domain, cmethod->klass),
6217 mini_method_get_context (cmethod)->method_inst);
6219 cfg->disable_aot = TRUE;
6220 NEW_PCONST (cfg, vtable_arg, mrgctx);
6223 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
6224 (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) {
6231 if (pass_imt_from_rgctx) {
6234 g_assert (!pass_vtable);
6237 GET_RGCTX (rgctx, context_used);
6238 imt_arg = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6239 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6245 MONO_INST_NEW (cfg, check, OP_CHECK_THIS_PASSTHROUGH);
6246 check->cil_code = ip;
6247 check->inst_left = sp [0];
6248 check->type = sp [0]->type;
6252 if (cmethod && virtual &&
6253 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
6254 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
6255 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
6256 mono_method_signature (cmethod)->generic_param_count) {
6257 MonoInst *this_temp, *this_arg_temp, *store;
6258 MonoInst *iargs [4];
6260 g_assert (mono_method_signature (cmethod)->is_inflated);
6261 /* Prevent inlining of methods that contain indirect calls */
6264 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6265 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
6266 MONO_ADD_INS (bblock, store);
6268 /* FIXME: This should be a managed pointer */
6269 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
6271 /* Because of the PCONST below */
6272 cfg->disable_aot = TRUE;
6273 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
6277 GET_RGCTX (rgctx, context_used);
6278 iargs [1] = get_runtime_generic_context_method (cfg, method, context_used,
6280 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6281 NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6282 temp = mono_emit_jit_icall (cfg, bblock,
6283 mono_helper_compile_generic_method, iargs, ip);
6285 NEW_METHODCONST (cfg, iargs [1], cmethod);
6286 NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6287 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method,
6291 NEW_TEMPLOAD (cfg, addr, temp);
6292 NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
6294 if ((temp = mono_emit_calli_spilled (cfg, bblock, fsig, sp, addr, ip)) != -1) {
6295 NEW_TEMPLOAD (cfg, *sp, temp);
6304 /* FIXME: runtime generic context pointer for jumps? */
6305 /* FIXME: handle this for generic sharing eventually */
6306 if ((ins_flag & MONO_INST_TAILCALL) && !cfg->generic_sharing_context && !vtable_arg && cmethod && (*ip == CEE_CALL) &&
6307 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
6310 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6312 /* FIXME: This assumes the two methods has the same number and type of arguments */
6314 * We implement tail calls by storing the actual arguments into the
6315 * argument variables, then emitting a OP_JMP. Since the actual arguments
6316 * can refer to the arg variables, we have to spill them.
6318 handle_loaded_temps (cfg, bblock, sp, sp + n);
6319 for (i = 0; i < n; ++i) {
6320 /* Prevent argument from being register allocated */
6321 arg_array [i]->flags |= MONO_INST_VOLATILE;
6323 /* Check if argument is the same */
6325 * FIXME: This loses liveness info, so it can only be done if the
6326 * argument is not register allocated.
6328 NEW_ARGLOAD (cfg, ins, i);
6329 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
6332 NEW_ARGSTORE (cfg, ins, i, sp [i]);
6333 /* FIXME: handle CEE_STIND_R4 */
6334 if (ins->opcode == CEE_STOBJ) {
6335 NEW_ARGLOADA (cfg, ins, i);
6336 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
6339 MONO_ADD_INS (bblock, ins);
6341 MONO_INST_NEW (cfg, ins, OP_JMP);
6342 ins->inst_p0 = cmethod;
6343 ins->inst_p1 = arg_array [0];
6344 MONO_ADD_INS (bblock, ins);
6345 link_bblock (cfg, bblock, end_bblock);
6346 start_new_bblock = 1;
6347 /* skip CEE_RET as well */
6352 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
6353 if (MONO_TYPE_IS_VOID (fsig->ret)) {
6354 MONO_ADD_INS (bblock, ins);
6356 type_to_eval_stack_type (cfg, fsig->ret, ins);
6366 handle_loaded_temps (cfg, bblock, stack_start, sp);
6368 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && //!check_this &&
6369 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
6370 mono_method_check_inlining (cfg, cmethod) &&
6371 !g_list_find (dont_inline, cmethod)) {
6373 MonoBasicBlock *ebblock;
6374 gboolean allways = FALSE;
6376 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6377 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6378 /* Prevent inlining of methods that call wrappers */
6380 cmethod = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6384 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
6388 GET_BBLOCK (cfg, bblock, ip);
6389 ebblock->next_bb = bblock;
6390 link_bblock (cfg, ebblock, bblock);
6392 if (!MONO_TYPE_IS_VOID (fsig->ret))
6395 /* indicates start of a new block, and triggers a load of all
6396 stack arguments at bb boundarie */
6399 inline_costs += costs;
6405 inline_costs += 10 * num_calls++;
6407 /* tail recursion elimination */
6408 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET &&
6410 gboolean has_vtargs = FALSE;
6413 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6415 /* keep it simple */
6416 for (i = fsig->param_count - 1; i >= 0; i--) {
6417 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
6422 for (i = 0; i < n; ++i) {
6423 /* FIXME: handle CEE_STIND_R4 */
6424 NEW_ARGSTORE (cfg, ins, i, sp [i]);
6425 MONO_ADD_INS (bblock, ins);
6427 MONO_INST_NEW (cfg, ins, OP_BR);
6428 MONO_ADD_INS (bblock, ins);
6429 tblock = start_bblock->out_bb [0];
6430 link_bblock (cfg, bblock, tblock);
6431 ins->inst_target_bb = tblock;
6432 start_new_bblock = 1;
6434 /* skip the CEE_RET, too */
6435 if (ip_in_bb (cfg, bblock, ip + 5))
6444 if (ip_in_bb (cfg, bblock, ip + 5)
6445 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
6446 && (!MONO_TYPE_IS_VOID (fsig->ret) || (cmethod && cmethod->string_ctor))
6447 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET))
6448 /* No need to spill */
6453 if (context_used && !imt_arg &&
6454 (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
6455 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
6456 (!virtual || cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6457 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
6462 g_assert (cfg->generic_sharing_context && cmethod);
6466 * We are compiling a call to
6467 * non-shared generic code from shared
6468 * code, which means that we have to
6469 * look up the method in the rgctx and
6470 * do an indirect call.
6472 GET_RGCTX (rgctx, context_used);
6473 addr = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6474 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
6479 g_assert (!imt_arg);
6481 if (*ip == CEE_CALL) {
6482 g_assert (context_used);
6483 } else if (*ip == CEE_CALLI) {
6484 g_assert (!vtable_arg);
6486 g_assert (cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6487 !(cmethod->flags & METHOD_ATTRIBUTE_FINAL));
6490 /* Prevent inlining of methods with indirect calls */
6493 ins = (MonoInst*)mono_emit_rgctx_calli (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6496 temp = mono_emit_rgctx_calli_spilled (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6498 NEW_TEMPLOAD (cfg, *sp, temp);
6499 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
6503 } else if (array_rank) {
6506 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
6507 if (sp [fsig->param_count]->type == STACK_OBJ) {
6508 MonoInst *iargs [2];
6509 MonoInst *array, *to_store, *store;
6511 handle_loaded_temps (cfg, bblock, stack_start, sp);
6513 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6514 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
6515 MONO_ADD_INS (bblock, store);
6516 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
6518 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
6519 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
6520 /* FIXME: handle CEE_STIND_R4 */
6521 MONO_ADD_INS (bblock, store);
6522 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
6525 * We first save the args for the call so that the args are copied to the stack
6526 * and a new instruction tree for them is created. If we don't do this,
6527 * the same MonoInst is added to two different trees and this is not
6530 mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
6532 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
6533 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
6536 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
6537 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
6538 /* FIXME: handle CEE_STIND_R4 */
6539 if (ins->opcode == CEE_STOBJ) {
6540 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
6542 MONO_ADD_INS (bblock, ins);
6545 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
6546 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6547 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
6550 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
6551 if (!cmethod->klass->element_class->valuetype && !readonly) {
6553 //* Needed by the code generated in inssel.brg * /
6554 mono_get_got_var (cfg);
6556 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
6557 check->klass = cmethod->klass;
6558 check->inst_left = sp [0];
6559 check->type = STACK_OBJ;
6564 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6567 g_assert_not_reached ();
6571 /* Prevent inlining of methods which call other methods */
6573 if (mini_redirect_call (&temp, cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) {
6575 NEW_TEMPLOAD (cfg, *sp, temp);
6578 } else if (no_spill) {
6579 ins = (MonoInst*)mono_emit_rgctx_method_call (cfg, bblock, cmethod, fsig, sp,
6580 vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL);
6583 if ((temp = mono_emit_rgctx_method_call_spilled (cfg, bblock, cmethod, fsig, sp,
6584 vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL)) != -1) {
6586 NEW_TEMPLOAD (cfg, load, temp);
6588 #ifdef MONO_ARCH_SOFT_FLOAT
6589 if (load->opcode == CEE_LDIND_R4) {
6590 NEW_TEMPLOADA (cfg, load, temp);
6591 temp = handle_load_float (cfg, bblock, load, ip);
6592 NEW_TEMPLOAD (cfg, load, temp);
6605 if (cfg->method != method) {
6606 /* return from inlined method */
6611 //g_assert (returnvar != -1);
6612 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
6613 store->cil_code = sp [0]->cil_code;
6614 if (store->opcode == CEE_STOBJ) {
6615 g_assert_not_reached ();
6616 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6617 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6618 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
6619 #ifdef MONO_ARCH_SOFT_FLOAT
6620 } else if (store->opcode == CEE_STIND_R4) {
6621 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6622 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
6625 MONO_ADD_INS (bblock, store);
6629 g_assert (!return_var);
6632 MONO_INST_NEW (cfg, ins, OP_NOP);
6633 ins->opcode = mini_type_to_stind (cfg, mono_method_signature (method)->ret);
6634 if (ins->opcode == CEE_STOBJ) {
6635 NEW_RETLOADA (cfg, ins);
6636 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6637 handle_stobj (cfg, bblock, ins, *sp, ip, cfg->ret->klass, FALSE, FALSE, FALSE);
6639 ins->opcode = OP_SETRET;
6640 ins->inst_i0 = *sp;;
6641 ins->inst_i1 = NULL;
6642 MONO_ADD_INS (bblock, ins);
6646 if (sp != stack_start)
6648 MONO_INST_NEW (cfg, ins, OP_BR);
6650 ins->inst_target_bb = end_bblock;
6651 MONO_ADD_INS (bblock, ins);
6652 link_bblock (cfg, bblock, end_bblock);
6653 start_new_bblock = 1;
6657 MONO_INST_NEW (cfg, ins, OP_BR);
6659 MONO_ADD_INS (bblock, ins);
6660 target = ip + 1 + (signed char)(*ip);
6662 GET_BBLOCK (cfg, tblock, target);
6663 link_bblock (cfg, bblock, tblock);
6664 CHECK_BBLOCK (target, ip, tblock);
6665 ins->inst_target_bb = tblock;
6666 if (sp != stack_start) {
6667 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6669 CHECK_UNVERIFIABLE (cfg);
6671 start_new_bblock = 1;
6672 inline_costs += BRANCH_COST;
6678 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6680 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6682 target = ip + 1 + *(signed char*)ip;
6684 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
6685 if (sp != stack_start) {
6686 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6688 CHECK_UNVERIFIABLE (cfg);
6690 inline_costs += BRANCH_COST;
6704 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6706 target = ip + 1 + *(signed char*)ip;
6708 #ifdef MONO_ARCH_SOFT_FLOAT
6709 if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6710 ins->opcode = condbr_to_fp_br (ins->opcode);
6712 ins->inst_left = sp [0];
6713 ins->inst_right = sp [1];
6714 ins->type = STACK_I4;
6715 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6716 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6724 if (sp != stack_start) {
6725 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6727 CHECK_UNVERIFIABLE (cfg);
6729 inline_costs += BRANCH_COST;
6733 MONO_INST_NEW (cfg, ins, OP_BR);
6735 MONO_ADD_INS (bblock, ins);
6736 target = ip + 4 + (gint32)read32(ip);
6738 GET_BBLOCK (cfg, tblock, target);
6739 link_bblock (cfg, bblock, tblock);
6740 CHECK_BBLOCK (target, ip, tblock);
6741 ins->inst_target_bb = tblock;
6742 if (sp != stack_start) {
6743 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6745 CHECK_UNVERIFIABLE (cfg);
6747 start_new_bblock = 1;
6748 inline_costs += BRANCH_COST;
6754 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6756 MONO_INST_NEW (cfg, ins, *ip);
6758 target = ip + 4 + (gint32)read32(ip);
6760 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
6761 if (sp != stack_start) {
6762 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6764 CHECK_UNVERIFIABLE (cfg);
6766 inline_costs += BRANCH_COST;
6780 MONO_INST_NEW (cfg, ins, *ip);
6782 target = ip + 4 + (gint32)read32(ip);
6784 #ifdef MONO_ARCH_SOFT_FLOAT
6785 if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6786 ins->opcode = condbr_to_fp_br (ins->opcode);
6788 ins->inst_left = sp [0];
6789 ins->inst_right = sp [1];
6790 ins->type = STACK_I4;
6791 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6792 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6800 if (sp != stack_start) {
6801 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6803 CHECK_UNVERIFIABLE (cfg);
6805 inline_costs += BRANCH_COST;
6810 n = read32 (ip + 1);
6811 MONO_INST_NEW (cfg, ins, OP_SWITCH);
6813 ins->inst_left = *sp;
6814 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
6817 CHECK_OPSIZE (n * sizeof (guint32));
6818 target = ip + n * sizeof (guint32);
6819 MONO_ADD_INS (bblock, ins);
6820 GET_BBLOCK (cfg, tblock, target);
6821 link_bblock (cfg, bblock, tblock);
6822 ins->klass = GUINT_TO_POINTER (n);
6823 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
6824 ins->inst_many_bb [n] = tblock;
6826 for (i = 0; i < n; ++i) {
6827 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
6828 link_bblock (cfg, bblock, tblock);
6829 ins->inst_many_bb [i] = tblock;
6832 if (sp != stack_start) {
6833 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6835 CHECK_UNVERIFIABLE (cfg);
6837 /* Needed by the code generated in inssel.brg */
6838 mono_get_got_var (cfg);
6839 inline_costs += (BRANCH_COST * 2);
6853 MONO_INST_NEW (cfg, ins, *ip);
6857 ins->type = ldind_type [*ip - CEE_LDIND_I1];
6858 ins->flags |= ins_flag;
6860 if (ins->type == STACK_OBJ)
6861 ins->klass = mono_defaults.object_class;
6862 #ifdef MONO_ARCH_SOFT_FLOAT
6863 if (*ip == CEE_LDIND_R4) {
6866 temp = handle_load_float (cfg, bblock, ins->inst_i0, ip);
6867 NEW_TEMPLOAD (cfg, *sp, temp);
6881 #ifdef MONO_ARCH_SOFT_FLOAT
6882 if (*ip == CEE_STIND_R4) {
6884 handle_store_float (cfg, bblock, sp [0], sp [1], ip);
6889 #if HAVE_WRITE_BARRIERS
6890 if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [-1]->opcode == OP_PCONST) && (sp [-1]->inst_p0 == 0))) {
6891 /* insert call to write barrier */
6892 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
6894 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
6899 MONO_INST_NEW (cfg, ins, *ip);
6902 handle_loaded_temps (cfg, bblock, stack_start, sp);
6903 MONO_ADD_INS (bblock, ins);
6904 ins->inst_i0 = sp [0];
6905 ins->inst_i1 = sp [1];
6906 ins->flags |= ins_flag;
6914 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
6915 /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
6916 if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
6917 switch (ins->opcode) {
6919 ins->opcode = OP_IMUL_IMM;
6920 ins->inst_imm = ins->inst_right->inst_c0;
6923 ins->opcode = OP_LMUL_IMM;
6924 ins->inst_imm = ins->inst_right->inst_c0;
6927 g_assert_not_reached ();
6932 if (mono_find_jit_opcode_emulation (ins->opcode)) {
6934 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6952 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
6953 * later apply the speedup to the left shift as well
6956 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
6957 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
6958 ins->opcode = OP_LSHR_UN_32;
6959 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
6963 if (mono_find_jit_opcode_emulation (ins->opcode)) {
6965 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6979 case CEE_CONV_OVF_I8:
6980 case CEE_CONV_OVF_U8:
6985 #ifdef MONO_ARCH_SOFT_FLOAT
6987 * Its rather hard to emit the soft float code during the decompose
6988 * pass, so avoid it in some specific cases.
6990 if (ins->opcode == OP_LCONV_TO_R4) {
6993 ins->opcode = OP_LCONV_TO_R8;
6994 ins->type = STACK_R8;
6997 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6999 MONO_INST_NEW (cfg, conv, CEE_CONV_R4);
7000 conv->inst_left = sp [-1];
7001 conv->type = STACK_R8;
7009 if (mono_find_jit_opcode_emulation (ins->opcode)) {
7011 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7015 case CEE_CONV_OVF_I4:
7016 case CEE_CONV_OVF_I1:
7017 case CEE_CONV_OVF_I2:
7018 case CEE_CONV_OVF_I:
7019 case CEE_CONV_OVF_U:
7022 if (sp [-1]->type == STACK_R8) {
7023 ADD_UNOP (CEE_CONV_OVF_I8);
7031 case CEE_CONV_OVF_U1:
7032 case CEE_CONV_OVF_U2:
7033 case CEE_CONV_OVF_U4:
7036 if (sp [-1]->type == STACK_R8) {
7037 ADD_UNOP (CEE_CONV_OVF_U8);
7045 case CEE_CONV_OVF_I1_UN:
7046 case CEE_CONV_OVF_I2_UN:
7047 case CEE_CONV_OVF_I4_UN:
7048 case CEE_CONV_OVF_I8_UN:
7049 case CEE_CONV_OVF_U1_UN:
7050 case CEE_CONV_OVF_U2_UN:
7051 case CEE_CONV_OVF_U4_UN:
7052 case CEE_CONV_OVF_U8_UN:
7053 case CEE_CONV_OVF_I_UN:
7054 case CEE_CONV_OVF_U_UN:
7062 token = read32 (ip + 1);
7063 klass = mini_get_class (method, token, generic_context);
7064 CHECK_TYPELOAD (klass);
7066 if (generic_class_is_reference_type (cfg, klass)) {
7067 MonoInst *store, *load;
7068 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
7069 load->inst_i0 = sp [1];
7070 load->type = STACK_OBJ;
7071 load->klass = klass;
7072 load->flags |= ins_flag;
7073 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
7074 handle_loaded_temps (cfg, bblock, stack_start, sp);
7075 MONO_ADD_INS (bblock, store);
7076 store->inst_i0 = sp [0];
7077 store->inst_i1 = load;
7078 store->flags |= ins_flag;
7082 n = mono_class_value_size (klass, &align);
7083 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7085 NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, align);
7086 MONO_ADD_INS (bblock, copy);
7088 MonoMethod *memcpy_method = get_memcpy_method ();
7089 MonoInst *iargs [3];
7092 NEW_ICONST (cfg, iargs [2], n);
7094 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7101 MonoInst *iargs [3];
7109 token = read32 (ip + 1);
7110 klass = mini_get_class (method, token, generic_context);
7111 CHECK_TYPELOAD (klass);
7112 if (generic_class_is_reference_type (cfg, klass)) {
7113 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
7114 ins->inst_i0 = sp [0];
7115 ins->type = STACK_OBJ;
7117 ins->flags |= ins_flag;
7124 /* Optimize the common ldobj+stloc combination */
7134 loc_index = ip [5] - CEE_STLOC_0;
7141 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
7142 CHECK_LOCAL (loc_index);
7143 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
7145 /* FIXME: handle CEE_STIND_R4 */
7146 if (ins->opcode == CEE_STOBJ) {
7147 handle_loaded_temps (cfg, bblock, stack_start, sp);
7148 g_assert (ins->opcode == CEE_STOBJ);
7149 NEW_LOCLOADA (cfg, ins, loc_index);
7150 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7157 n = mono_class_value_size (klass, &align);
7158 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7159 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7160 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7162 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7163 MONO_ADD_INS (bblock, copy);
7165 MonoMethod *memcpy_method = get_memcpy_method ();
7167 NEW_ICONST (cfg, iargs [2], n);
7169 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7171 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7179 CHECK_STACK_OVF (1);
7181 n = read32 (ip + 1);
7183 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
7184 /* FIXME: moving GC */
7185 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
7186 ins->type = STACK_OBJ;
7187 ins->klass = mono_defaults.string_class;
7190 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
7192 MonoInst *iargs [1];
7194 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
7195 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
7196 NEW_TEMPLOAD (cfg, *sp, temp);
7200 if (cfg->opt & MONO_OPT_SHARED) {
7202 MonoInst *iargs [3];
7203 MonoInst* domain_var;
7205 if (cfg->compile_aot) {
7206 /* FIXME: bug when inlining methods from different assemblies (n is a token valid just in one). */
7207 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
7209 /* avoid depending on undefined C behavior in sequence points */
7210 domain_var = mono_get_domainvar (cfg);
7211 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7212 NEW_IMAGECONST (cfg, iargs [1], image);
7213 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
7214 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
7215 NEW_TEMPLOAD (cfg, *sp, temp);
7216 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7218 if (bblock->out_of_line) {
7219 MonoInst *iargs [2];
7222 if (cfg->method->klass->image == mono_defaults.corlib) {
7224 * Avoid relocations and save some code size by using a
7225 * version of helper_ldstr specialized to mscorlib.
7227 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
7228 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
7230 /* Avoid creating the string object */
7231 NEW_IMAGECONST (cfg, iargs [0], image);
7232 NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
7233 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
7235 NEW_TEMPLOAD (cfg, *sp, temp);
7238 if (cfg->compile_aot) {
7239 NEW_LDSTRCONST (cfg, ins, image, n);
7243 NEW_PCONST (cfg, ins, NULL);
7244 ins->type = STACK_OBJ;
7245 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7246 ins->klass = mono_defaults.string_class;
7256 MonoInst *iargs [2];
7257 MonoMethodSignature *fsig;
7260 MonoInst *vtable_arg = NULL;
7263 token = read32 (ip + 1);
7264 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
7267 fsig = mono_method_get_signature (cmethod, image, token);
7269 mono_save_token_info (cfg, image, token, cmethod);
7271 if (!mono_class_init (cmethod->klass))
7274 if (cfg->generic_sharing_context)
7275 context_used = mono_method_check_context_used (cmethod);
7277 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
7278 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
7280 CHECK_CFG_EXCEPTION;
7281 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
7282 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
7285 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7286 mono_method_is_generic_sharable_impl (cmethod, TRUE)) {
7287 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7291 GET_RGCTX (rgctx, context_used);
7292 vtable_arg = get_runtime_generic_context_method_rgctx (cfg, method,
7293 context_used, bblock, cmethod, generic_context, rgctx, ip);
7295 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7296 MonoMethodRuntimeGenericContext *mrgctx;
7298 mrgctx = mono_method_lookup_rgctx (vtable,
7299 mini_method_get_context (cmethod)->method_inst);
7301 NEW_PCONST (cfg, vtable_arg, mrgctx);
7307 GET_RGCTX (rgctx, context_used);
7308 vtable_arg = get_runtime_generic_context_ptr (cfg, method, context_used,
7309 bblock, cmethod->klass, generic_context,
7310 rgctx, MONO_RGCTX_INFO_VTABLE, ip);
7312 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7314 CHECK_TYPELOAD (cmethod->klass);
7315 NEW_VTABLECONST (cfg, vtable_arg, vtable);
7320 n = fsig->param_count;
7324 * Generate smaller code for the common newobj <exception> instruction in
7325 * argument checking code.
7327 if (bblock->out_of_line && cmethod->klass->image == mono_defaults.corlib && n <= 2 &&
7328 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
7329 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
7330 MonoInst *iargs [3];
7333 g_assert (!vtable_arg);
7337 NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
7340 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_0, iargs, ip);
7344 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_1, iargs, ip);
7349 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_2, iargs, ip);
7352 g_assert_not_reached ();
7354 NEW_TEMPLOAD (cfg, ins, temp);
7362 /* move the args to allow room for 'this' in the first position */
7368 /* check_call_signature () requires sp[0] to be set */
7369 this_ins.type = STACK_OBJ;
7371 if (check_call_signature (cfg, fsig, sp))
7374 handle_loaded_temps (cfg, bblock, stack_start, sp);
7376 if (mini_class_is_system_array (cmethod->klass)) {
7377 g_assert (!context_used);
7378 g_assert (!vtable_arg);
7380 NEW_METHODCONST (cfg, *sp, cmethod);
7382 if (fsig->param_count == 2)
7383 /* Avoid varargs in the common case */
7384 temp = mono_emit_jit_icall (cfg, bblock, mono_array_new_2, sp, ip);
7386 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
7387 } else if (cmethod->string_ctor) {
7388 g_assert (!context_used);
7389 g_assert (!vtable_arg);
7391 /* we simply pass a null pointer */
7392 NEW_PCONST (cfg, *sp, NULL);
7393 /* now call the string ctor */
7394 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
7396 MonoInst* callvirt_this_arg = NULL;
7398 if (cmethod->klass->valuetype) {
7399 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
7400 temp = iargs [0]->inst_c0;
7402 NEW_TEMPLOADA (cfg, *sp, temp);
7404 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
7406 NEW_TEMPLOADA (cfg, *sp, temp);
7409 * The code generated by mini_emit_virtual_call () expects
7410 * iargs [0] to be a boxed instance, but luckily the vcall
7411 * will be transformed into a normal call there.
7413 } else if (context_used) {
7414 MonoInst *rgctx, *data;
7417 GET_RGCTX (rgctx, context_used);
7418 if (cfg->opt & MONO_OPT_SHARED)
7419 rgctx_info = MONO_RGCTX_INFO_KLASS;
7421 rgctx_info = MONO_RGCTX_INFO_VTABLE;
7422 data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
7423 cmethod->klass, generic_context, rgctx, rgctx_info, ip);
7425 temp = handle_alloc_from_inst (cfg, bblock, cmethod->klass, data, FALSE, ip);
7426 NEW_TEMPLOAD (cfg, *sp, temp);
7428 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7430 CHECK_TYPELOAD (cmethod->klass);
7431 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
7432 guint8 *tramp = mono_create_class_init_trampoline (vtable);
7433 mono_emit_native_call (cfg, bblock, tramp,
7434 helper_sig_class_init_trampoline,
7435 NULL, ip, FALSE, FALSE);
7436 if (cfg->verbose_level > 2)
7437 g_print ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
7438 class_inits = g_slist_prepend (class_inits, vtable);
7440 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
7441 NEW_TEMPLOAD (cfg, *sp, temp);
7444 /* Avoid virtual calls to ctors if possible */
7445 if (cmethod->klass->marshalbyref)
7446 callvirt_this_arg = sp [0];
7448 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7449 mono_method_check_inlining (cfg, cmethod) &&
7450 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
7451 !g_list_find (dont_inline, cmethod)) {
7453 MonoBasicBlock *ebblock;
7454 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
7459 GET_BBLOCK (cfg, bblock, ip);
7460 ebblock->next_bb = bblock;
7461 link_bblock (cfg, ebblock, bblock);
7463 NEW_TEMPLOAD (cfg, *sp, temp);
7466 /* indicates start of a new block, and triggers a load
7467 of all stack arguments at bb boundarie */
7470 inline_costs += costs;
7474 /* Prevent inlining of methods which call other methods */
7476 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
7478 } else if (context_used &&
7479 (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
7480 !mono_class_generic_sharing_enabled (cmethod->klass))) {
7481 MonoInst *rgctx, *cmethod_addr;
7483 g_assert (!callvirt_this_arg);
7485 GET_RGCTX (rgctx, context_used);
7486 cmethod_addr = get_runtime_generic_context_method (cfg, method, context_used,
7488 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
7490 mono_emit_rgctx_calli_spilled (cfg, bblock, fsig, sp, cmethod_addr, vtable_arg, ip);
7492 /* Prevent inlining of methods which call other methods */
7494 /* now call the actual ctor */
7495 mono_emit_rgctx_method_call_spilled (cfg, bblock, cmethod, fsig, sp,
7496 vtable_arg, NULL, ip, callvirt_this_arg);
7500 NEW_TEMPLOAD (cfg, *sp, temp);
7511 token = read32 (ip + 1);
7512 klass = mini_get_class (method, token, generic_context);
7513 CHECK_TYPELOAD (klass);
7514 if (sp [0]->type != STACK_OBJ)
7517 if (cfg->generic_sharing_context)
7518 context_used = mono_class_check_context_used (klass);
7520 /* Needed by the code generated in inssel.brg */
7522 mono_get_got_var (cfg);
7525 MonoInst *rgctx, *args [2];
7532 GET_RGCTX (rgctx, context_used);
7533 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
7534 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
7536 temp = mono_emit_jit_icall (cfg, bblock, mono_object_isinst, args, ip);
7537 NEW_TEMPLOAD (cfg, *sp, temp);
7542 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7544 MonoMethod *mono_isinst;
7545 MonoInst *iargs [1];
7546 MonoBasicBlock *ebblock;
7550 mono_isinst = mono_marshal_get_isinst (klass);
7553 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
7554 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7556 g_assert (costs > 0);
7561 GET_BBLOCK (cfg, bblock, ip);
7562 ebblock->next_bb = bblock;
7563 link_bblock (cfg, ebblock, bblock);
7565 temp = iargs [0]->inst_i0->inst_c0;
7566 NEW_TEMPLOAD (cfg, *sp, temp);
7570 inline_costs += costs;
7572 MONO_INST_NEW (cfg, ins, *ip);
7573 ins->type = STACK_OBJ;
7574 ins->inst_left = *sp;
7575 ins->inst_newa_class = klass;
7577 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
7581 case CEE_UNBOX_ANY: {
7582 MonoInst *iargs [3];
7588 token = read32 (ip + 1);
7589 klass = mini_get_class (method, token, generic_context);
7590 CHECK_TYPELOAD (klass);
7592 if (cfg->generic_sharing_context)
7593 context_used = mono_class_check_context_used (klass);
7595 if (generic_class_is_reference_type (cfg, klass)) {
7596 switch (emit_castclass (klass, token, context_used, FALSE,
7597 cfg, method, arg_array, param_types, dont_inline, end, header,
7598 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7600 case -1: goto unverified;
7601 case -2: goto exception_exit;
7602 default: g_assert_not_reached ();
7607 if (mono_class_is_nullable (klass)) {
7609 MonoInst *rgctx = NULL;
7612 GET_RGCTX (rgctx, context_used);
7614 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7615 generic_context, rgctx);
7616 NEW_TEMPLOAD (cfg, *sp, v);
7622 switch (emit_unbox (klass, token, context_used,
7623 cfg, method, arg_array, param_types, dont_inline, end, header,
7624 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7626 case -1: goto unverified;
7627 case -2: goto exception_exit;
7628 default: g_assert_not_reached ();
7632 n = mono_class_value_size (klass, &align);
7633 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7634 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7635 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7637 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7638 MONO_ADD_INS (bblock, copy);
7640 MonoMethod *memcpy_method = get_memcpy_method ();
7642 NEW_ICONST (cfg, iargs [2], n);
7644 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7646 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7655 token = read32 (ip + 1);
7656 klass = mini_get_class (method, token, generic_context);
7657 CHECK_TYPELOAD (klass);
7659 if (cfg->generic_sharing_context)
7660 context_used = mono_class_check_context_used (klass);
7662 if (mono_class_is_nullable (klass)) {
7664 MonoInst *rgctx = NULL;
7667 GET_RGCTX (rgctx, context_used);
7668 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7669 generic_context, rgctx);
7670 NEW_TEMPLOAD (cfg, *sp, v);
7676 switch (emit_unbox (klass, token, context_used,
7677 cfg, method, arg_array, param_types, dont_inline, end, header,
7678 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7680 case -1: goto unverified;
7681 case -2: goto exception_exit;
7682 default: g_assert_not_reached ();
7693 token = read32 (ip + 1);
7694 klass = mini_get_class (method, token, generic_context);
7695 CHECK_TYPELOAD (klass);
7696 if (sp [0]->type != STACK_OBJ)
7699 if (cfg->generic_sharing_context)
7700 context_used = mono_class_check_context_used (klass);
7702 switch (emit_castclass (klass, token, context_used, TRUE,
7703 cfg, method, arg_array, param_types, dont_inline, end, header,
7704 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7706 case -1: goto unverified;
7707 case -2: goto exception_exit;
7708 default: g_assert_not_reached ();
7713 MONO_INST_NEW (cfg, ins, OP_THROW);
7715 ins->inst_left = *sp;
7717 bblock->out_of_line = TRUE;
7718 MONO_ADD_INS (bblock, ins);
7719 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
7720 ins->cil_code = ip - 1;
7721 MONO_ADD_INS (bblock, ins);
7724 link_bblock (cfg, bblock, end_bblock);
7725 start_new_bblock = 1;
7730 MonoInst *offset_ins;
7731 MonoClassField *field;
7732 MonoBasicBlock *ebblock;
7736 if (*ip == CEE_STFLD) {
7743 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
7745 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
7748 token = read32 (ip + 1);
7749 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7750 field = mono_method_get_wrapper_data (method, token);
7751 klass = field->parent;
7753 field = mono_field_from_token (image, token, &klass, generic_context);
7757 mono_class_init (klass);
7758 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7759 FIELD_ACCESS_FAILURE;
7761 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
7762 /* FIXME: mark instructions for use in SSA */
7763 if (*ip == CEE_STFLD) {
7764 if (target_type_is_incompatible (cfg, field->type, sp [1]))
7766 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7767 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
7768 MonoInst *iargs [5];
7771 NEW_CLASSCONST (cfg, iargs [1], klass);
7772 NEW_FIELDCONST (cfg, iargs [2], field);
7773 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
7777 if (cfg->opt & MONO_OPT_INLINE) {
7778 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
7779 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7780 g_assert (costs > 0);
7785 GET_BBLOCK (cfg, bblock, ip);
7786 ebblock->next_bb = bblock;
7787 link_bblock (cfg, ebblock, bblock);
7789 /* indicates start of a new block, and triggers a load
7790 of all stack arguments at bb boundarie */
7793 inline_costs += costs;
7796 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
7798 #if HAVE_WRITE_BARRIERS
7799 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
7800 /* insert call to write barrier */
7801 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
7802 MonoInst *iargs [2];
7803 NEW_ICONST (cfg, offset_ins, foffset);
7804 MONO_INST_NEW (cfg, ins, OP_PADD);
7805 ins->inst_left = *sp;
7806 ins->inst_right = offset_ins;
7807 ins->type = STACK_MP;
7808 ins->klass = mono_defaults.object_class;
7811 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
7813 #ifdef MONO_ARCH_SOFT_FLOAT
7814 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_R4) {
7815 NEW_ICONST (cfg, offset_ins, foffset);
7816 MONO_INST_NEW (cfg, ins, OP_PADD);
7817 ins->inst_left = *sp;
7818 ins->inst_right = offset_ins;
7819 ins->type = STACK_MP;
7820 ins->klass = mono_defaults.object_class;
7821 handle_store_float (cfg, bblock, ins, sp [1], ip);
7825 NEW_ICONST (cfg, offset_ins, foffset);
7826 MONO_INST_NEW (cfg, ins, OP_PADD);
7827 ins->inst_left = *sp;
7828 ins->inst_right = offset_ins;
7829 ins->type = STACK_MP;
7831 MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
7832 store->inst_left = ins;
7833 store->inst_right = sp [1];
7834 handle_loaded_temps (cfg, bblock, stack_start, sp);
7835 store->flags |= ins_flag;
7837 if (store->opcode == CEE_STOBJ) {
7838 handle_stobj (cfg, bblock, ins, sp [1], ip,
7839 mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
7841 MONO_ADD_INS (bblock, store);
7844 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7845 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
7846 MonoInst *iargs [4];
7850 NEW_CLASSCONST (cfg, iargs [1], klass);
7851 NEW_FIELDCONST (cfg, iargs [2], field);
7852 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
7853 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
7854 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock,
7855 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7856 g_assert (costs > 0);
7861 GET_BBLOCK (cfg, bblock, ip);
7862 ebblock->next_bb = bblock;
7863 link_bblock (cfg, ebblock, bblock);
7865 temp = iargs [0]->inst_i0->inst_c0;
7867 NEW_TEMPLOAD (cfg, *sp, temp);
7870 /* indicates start of a new block, and triggers a load of
7871 all stack arguments at bb boundarie */
7874 inline_costs += costs;
7877 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
7878 NEW_TEMPLOAD (cfg, *sp, temp);
7879 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
7883 NEW_ICONST (cfg, offset_ins, foffset);
7884 MONO_INST_NEW (cfg, ins, OP_PADD);
7885 ins->inst_left = *sp;
7886 ins->inst_right = offset_ins;
7887 ins->type = STACK_MP;
7889 if (*ip == CEE_LDFLDA) {
7890 ins->klass = mono_class_from_mono_type (field->type);
7894 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
7895 type_to_eval_stack_type (cfg, field->type, load);
7896 load->inst_left = ins;
7897 load->flags |= ins_flag;
7899 #ifdef MONO_ARCH_SOFT_FLOAT
7900 if (mini_type_to_ldind (cfg, field->type) == CEE_LDIND_R4) {
7902 temp = handle_load_float (cfg, bblock, ins, ip);
7903 NEW_TEMPLOAD (cfg, *sp, temp);
7917 MonoClassField *field;
7918 gboolean is_special_static;
7919 gpointer addr = NULL;
7922 token = read32 (ip + 1);
7923 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7924 field = mono_method_get_wrapper_data (method, token);
7925 klass = field->parent;
7928 field = mono_field_from_token (image, token, &klass, generic_context);
7931 mono_class_init (klass);
7932 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7933 FIELD_ACCESS_FAILURE;
7936 * We can only support shared generic static
7937 * field access on architectures where the
7938 * trampoline code has been extended to handle
7939 * the generic class init.
7941 #ifndef MONO_ARCH_VTABLE_REG
7942 GENERIC_SHARING_FAILURE (*ip);
7945 if (cfg->generic_sharing_context)
7946 context_used = mono_class_check_context_used (klass);
7948 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
7950 if ((*ip) == CEE_STSFLD)
7951 handle_loaded_temps (cfg, bblock, stack_start, sp);
7953 is_special_static = mono_class_field_is_special_static (field);
7955 if ((cfg->opt & MONO_OPT_SHARED) ||
7956 (cfg->compile_aot && is_special_static) ||
7957 (context_used && is_special_static)) {
7959 MonoInst *iargs [2];
7961 g_assert (field->parent);
7962 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {
7963 MonoInst *domain_var;
7964 /* avoid depending on undefined C behavior in sequence points */
7965 domain_var = mono_get_domainvar (cfg);
7966 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7968 NEW_DOMAINCONST (cfg, iargs [0]);
7973 GET_RGCTX (rgctx, context_used);
7974 iargs [1] = get_runtime_generic_context_field (cfg, method, context_used,
7976 generic_context, rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
7978 NEW_FIELDCONST (cfg, iargs [1], field);
7980 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
7981 NEW_TEMPLOAD (cfg, ins, temp);
7982 } else if (context_used) {
7983 MonoInst *rgctx, *static_data;
7986 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
7987 method->klass->name_space, method->klass->name, method->name,
7988 depth, field->offset);
7991 if (mono_class_needs_cctor_run (klass, method)) {
7992 MonoMethodSignature *sig = helper_sig_generic_class_init_trampoline;
7994 MonoInst *vtable, *rgctx;
7996 GET_RGCTX (rgctx, context_used);
7997 vtable = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
7998 generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
8000 call = mono_emit_call_args (cfg, bblock, sig, NULL, FALSE, FALSE, ip, FALSE);
8001 call->inst.opcode = OP_TRAMPCALL_VTABLE;
8002 call->fptr = mono_create_generic_class_init_trampoline ();
8004 call->inst.inst_left = vtable;
8006 mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
8010 * The pointer we're computing here is
8012 * super_info.static_data + field->offset
8014 GET_RGCTX (rgctx, context_used);
8015 static_data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8016 generic_context, rgctx, MONO_RGCTX_INFO_STATIC_DATA, ip);
8018 if (field->offset == 0) {
8021 MonoInst *field_offset;
8023 NEW_ICONST (cfg, field_offset, field->offset);
8025 MONO_INST_NEW (cfg, ins, OP_PADD);
8026 ins->inst_left = static_data;
8027 ins->inst_right = field_offset;
8028 ins->type = STACK_PTR;
8034 vtable = mono_class_vtable (cfg->domain, klass);
8035 CHECK_TYPELOAD (klass);
8036 if (!is_special_static) {
8037 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
8038 guint8 *tramp = mono_create_class_init_trampoline (vtable);
8039 mono_emit_native_call (cfg, bblock, tramp,
8040 helper_sig_class_init_trampoline,
8041 NULL, ip, FALSE, FALSE);
8042 if (cfg->verbose_level > 2)
8043 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
8044 class_inits = g_slist_prepend (class_inits, vtable);
8046 if (cfg->run_cctors) {
8048 /* This makes so that inline cannot trigger */
8049 /* .cctors: too many apps depend on them */
8050 /* running with a specific order... */
8051 if (! vtable->initialized)
8053 ex = mono_runtime_class_init_full (vtable, FALSE);
8055 set_exception_object (cfg, ex);
8056 goto exception_exit;
8060 addr = (char*)vtable->data + field->offset;
8062 if (cfg->compile_aot)
8063 NEW_SFLDACONST (cfg, ins, field);
8065 NEW_PCONST (cfg, ins, addr);
8068 MonoInst *iargs [1];
8070 /* The special_static_fields
8071 * field is init'd in
8072 * mono_class_vtable, so it
8073 * needs to be called here.
8075 if (!(cfg->opt & MONO_OPT_SHARED)) {
8076 mono_class_vtable (cfg->domain, klass);
8077 CHECK_TYPELOAD (klass);
8079 mono_domain_lock (cfg->domain);
8080 if (cfg->domain->special_static_fields)
8081 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
8082 mono_domain_unlock (cfg->domain);
8085 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
8086 * This could be later optimized to do just a couple of
8087 * memory dereferences with constant offsets.
8089 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
8090 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
8091 NEW_TEMPLOAD (cfg, ins, temp);
8095 /* FIXME: mark instructions for use in SSA */
8096 if (*ip == CEE_LDSFLDA) {
8097 ins->klass = mono_class_from_mono_type (field->type);
8099 } else if (*ip == CEE_STSFLD) {
8103 MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
8104 store->inst_left = ins;
8105 store->inst_right = sp [0];
8106 store->flags |= ins_flag;
8109 #ifdef MONO_ARCH_SOFT_FLOAT
8110 if (store->opcode == CEE_STIND_R4)
8111 handle_store_float (cfg, bblock, ins, sp [0], ip);
8114 if (store->opcode == CEE_STOBJ) {
8115 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
8117 MONO_ADD_INS (bblock, store);
8119 gboolean is_const = FALSE;
8120 MonoVTable *vtable = NULL;
8123 vtable = mono_class_vtable (cfg->domain, klass);
8125 CHECK_TYPELOAD (klass);
8126 if (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
8127 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
8128 gpointer addr = (char*)vtable->data + field->offset;
8129 int ro_type = field->type->type;
8130 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
8131 ro_type = field->type->data.klass->enum_basetype->type;
8133 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
8136 case MONO_TYPE_BOOLEAN:
8138 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
8142 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
8145 case MONO_TYPE_CHAR:
8147 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
8151 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
8156 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
8160 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
8163 #ifndef HAVE_MOVING_COLLECTOR
8166 case MONO_TYPE_STRING:
8167 case MONO_TYPE_OBJECT:
8168 case MONO_TYPE_CLASS:
8169 case MONO_TYPE_SZARRAY:
8171 case MONO_TYPE_FNPTR:
8172 case MONO_TYPE_ARRAY:
8173 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
8174 type_to_eval_stack_type (cfg, field->type, *sp);
8180 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
8181 sp [0]->type = STACK_I8;
8182 sp [0]->inst_l = *((gint64 *)addr);
8187 case MONO_TYPE_VALUETYPE:
8196 CHECK_STACK_OVF (1);
8197 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
8198 type_to_eval_stack_type (cfg, field->type, load);
8199 load->inst_left = ins;
8200 load->flags |= ins_flag;
8201 #ifdef MONO_ARCH_SOFT_FLOAT
8202 if (load->opcode == CEE_LDIND_R4) {
8204 temp = handle_load_float (cfg, bblock, ins, ip);
8205 NEW_TEMPLOAD (cfg, load, temp);
8219 token = read32 (ip + 1);
8220 klass = mini_get_class (method, token, generic_context);
8221 CHECK_TYPELOAD (klass);
8222 n = mini_type_to_stind (cfg, &klass->byval_arg);
8223 /* FIXME: handle CEE_STIND_R4 */
8224 if (n == CEE_STOBJ) {
8225 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
8227 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
8229 MONO_INST_NEW (cfg, store, n);
8230 store->inst_left = sp [0];
8231 store->inst_right = sp [1];
8232 store->flags |= ins_flag;
8233 MONO_ADD_INS (bblock, store);
8246 token = read32 (ip + 1);
8247 klass = mini_get_class (method, token, generic_context);
8248 CHECK_TYPELOAD (klass);
8250 if (cfg->generic_sharing_context)
8251 context_used = mono_class_check_context_used (klass);
8253 if (generic_class_is_reference_type (cfg, klass)) {
8258 if (klass == mono_defaults.void_class)
8260 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
8262 /* frequent check in generic code: box (struct), brtrue */
8263 if (!mono_class_is_nullable (klass) &&
8264 ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
8265 /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
8266 MONO_INST_NEW (cfg, ins, CEE_POP);
8267 MONO_ADD_INS (bblock, ins);
8271 MONO_INST_NEW (cfg, ins, OP_BR);
8272 MONO_ADD_INS (bblock, ins);
8273 if (*ip == CEE_BRTRUE_S) {
8276 target = ip + 1 + (signed char)(*ip);
8281 target = ip + 4 + (gint)(read32 (ip));
8284 GET_BBLOCK (cfg, tblock, target);
8285 link_bblock (cfg, bblock, tblock);
8286 CHECK_BBLOCK (target, ip, tblock);
8287 ins->inst_target_bb = tblock;
8288 GET_BBLOCK (cfg, tblock, ip);
8289 link_bblock (cfg, bblock, tblock);
8290 if (sp != stack_start) {
8291 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
8293 CHECK_UNVERIFIABLE (cfg);
8295 start_new_bblock = 1;
8301 if (mono_class_is_nullable (klass)) {
8302 GET_RGCTX (rgctx, context_used);
8303 *sp++ = handle_box_nullable_from_inst (cfg, method, context_used, bblock, val,
8304 ip, klass, generic_context, rgctx);
8309 GET_RGCTX (rgctx, context_used);
8310 if (cfg->opt & MONO_OPT_SHARED)
8311 rgctx_info = MONO_RGCTX_INFO_KLASS;
8313 rgctx_info = MONO_RGCTX_INFO_VTABLE;
8314 data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8315 generic_context, rgctx, rgctx_info, ip);
8317 *sp++ = handle_box_from_inst (cfg, bblock, val, ip, klass, data);
8320 *sp++ = handle_box (cfg, bblock, val, ip, klass);
8331 token = read32 (ip + 1);
8333 /* allocate the domainvar - becaus this is used in decompose_foreach */
8334 if (cfg->opt & MONO_OPT_SHARED) {
8335 mono_get_domainvar (cfg);
8336 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
8337 cfg->domainvar->flags |= MONO_INST_VOLATILE;
8341 mono_get_got_var (cfg);
8343 klass = mini_get_class (method, token, generic_context);
8344 CHECK_TYPELOAD (klass);
8346 if (cfg->generic_sharing_context)
8347 context_used = mono_class_check_context_used (klass);
8350 MonoInst *rgctx, *args [3];
8354 /* FIXME: what about domain-neutral code? */
8355 NEW_DOMAINCONST (cfg, args [0]);
8358 GET_RGCTX (rgctx, context_used);
8359 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8360 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8365 temp = mono_emit_jit_icall (cfg, bblock, mono_array_new, args, ip);
8366 NEW_TEMPLOAD (cfg, ins, temp);
8368 MONO_INST_NEW (cfg, ins, *ip);
8369 ins->inst_newa_class = klass;
8370 ins->inst_newa_len = *sp;
8371 ins->type = STACK_OBJ;
8372 ins->klass = mono_array_class_get (klass, 1);
8378 * we store the object so calls to create the array are not interleaved
8379 * with the arguments of other calls.
8382 MonoInst *store, *temp, *load;
8383 const char *data_ptr;
8386 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8387 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8388 store->cil_code = ins->cil_code;
8389 MONO_ADD_INS (bblock, store);
8391 * we inline/optimize the initialization sequence if possible.
8392 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
8393 * for small sizes open code the memcpy
8394 * ensure the rva field is big enough
8396 if ((cfg->opt & MONO_OPT_INTRINS) && ip + 6 < end && ip_in_bb (cfg, bblock, ip + 6) && (data_ptr = initialize_array_data (method, cfg->compile_aot, ip, ins, &data_size))) {
8397 MonoMethod *memcpy_method = get_memcpy_method ();
8398 MonoInst *data_offset, *add;
8399 MonoInst *iargs [3];
8400 NEW_ICONST (cfg, iargs [2], data_size);
8401 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8402 load->cil_code = ins->cil_code;
8403 NEW_ICONST (cfg, data_offset, G_STRUCT_OFFSET (MonoArray, vector));
8404 MONO_INST_NEW (cfg, add, OP_PADD);
8405 add->inst_left = load;
8406 add->inst_right = data_offset;
8408 if (cfg->compile_aot) {
8409 NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(data_ptr), STACK_PTR, NULL);
8411 NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
8413 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
8416 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8417 load->cil_code = ins->cil_code;
8425 if (sp [0]->type != STACK_OBJ)
8427 MONO_INST_NEW (cfg, ins, *ip);
8429 ins->inst_left = *sp;
8430 ins->type = STACK_PTR;
8437 if (sp [0]->type != STACK_OBJ)
8440 klass = mini_get_class (method, read32 (ip + 1), generic_context);
8441 CHECK_TYPELOAD (klass);
8442 /* we need to make sure that this array is exactly the type it needs
8443 * to be for correctness. the wrappers are lax with their usage
8444 * so we need to ignore them here
8446 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
8449 /* Needed by the code generated in inssel.brg */
8450 mono_get_got_var (cfg);
8452 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
8453 check->klass = mono_array_class_get (klass, 1);
8454 check->inst_left = sp [0];
8455 check->type = STACK_OBJ;
8460 mono_class_init (klass);
8461 NEW_LDELEMA (cfg, ins, sp, klass);
8465 case CEE_LDELEM_ANY: {
8469 if (sp [0]->type != STACK_OBJ)
8472 token = read32 (ip + 1);
8473 klass = mini_get_class (method, token, generic_context);
8474 CHECK_TYPELOAD (klass);
8475 mono_class_init (klass);
8476 NEW_LDELEMA (cfg, load, sp, klass);
8477 MONO_INST_NEW (cfg, ins, mini_type_to_ldind (cfg, &klass->byval_arg));
8478 ins->inst_left = load;
8480 type_to_eval_stack_type (cfg, &klass->byval_arg, ins);
8494 case CEE_LDELEM_REF: {
8498 * ldind.x (ldelema (array, index))
8499 * ldelema does the bounds check
8503 if (sp [0]->type != STACK_OBJ)
8505 klass = array_access_to_klass (*ip, sp [0]);
8506 NEW_LDELEMA (cfg, load, sp, klass);
8507 #ifdef MONO_ARCH_SOFT_FLOAT
8508 if (*ip == CEE_LDELEM_R4) {
8510 temp = handle_load_float (cfg, bblock, load, ip);
8511 NEW_TEMPLOAD (cfg, *sp, temp);
8517 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
8518 ins->inst_left = load;
8520 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
8531 case CEE_STELEM_R8: {
8535 * stind.x (ldelema (array, index), val)
8536 * ldelema does the bounds check
8540 if (sp [0]->type != STACK_OBJ)
8542 klass = array_access_to_klass (*ip, sp [0]);
8543 NEW_LDELEMA (cfg, load, sp, klass);
8544 #ifdef MONO_ARCH_SOFT_FLOAT
8545 if (*ip == CEE_STELEM_R4) {
8546 handle_store_float (cfg, bblock, load, sp [2], ip);
8551 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8552 ins->inst_left = load;
8553 ins->inst_right = sp [2];
8555 handle_loaded_temps (cfg, bblock, stack_start, sp);
8556 MONO_ADD_INS (bblock, ins);
8560 case CEE_STELEM_ANY: {
8564 * stind.x (ldelema (array, index), val)
8565 * ldelema does the bounds check
8569 if (sp [0]->type != STACK_OBJ)
8572 token = read32 (ip + 1);
8573 klass = mini_get_class (method, token, generic_context);
8574 CHECK_TYPELOAD (klass);
8575 mono_class_init (klass);
8576 if (generic_class_is_reference_type (cfg, klass)) {
8577 /* storing a NULL doesn't need any of the complex checks in stelemref */
8578 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8580 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8581 MONO_INST_NEW (cfg, ins, CEE_STIND_REF);
8582 ins->inst_left = load;
8583 ins->inst_right = sp [2];
8584 MONO_ADD_INS (bblock, ins);
8586 MonoMethod* helper = mono_marshal_get_stelemref ();
8587 MonoInst *iargs [3];
8588 handle_loaded_temps (cfg, bblock, stack_start, sp);
8594 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8597 NEW_LDELEMA (cfg, load, sp, klass);
8599 n = mini_type_to_stind (cfg, &klass->byval_arg);
8600 /* FIXME: CEE_STIND_R4 */
8602 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
8604 MONO_INST_NEW (cfg, ins, n);
8605 ins->inst_left = load;
8606 ins->inst_right = sp [2];
8607 handle_loaded_temps (cfg, bblock, stack_start, sp);
8608 MONO_ADD_INS (bblock, ins);
8615 case CEE_STELEM_REF: {
8616 MonoInst *iargs [3];
8617 MonoMethod* helper = mono_marshal_get_stelemref ();
8621 if (sp [0]->type != STACK_OBJ)
8623 if (sp [2]->type != STACK_OBJ)
8626 handle_loaded_temps (cfg, bblock, stack_start, sp);
8628 /* storing a NULL doesn't need any of the complex checks in stelemref */
8629 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8631 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8632 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8633 ins->inst_left = load;
8634 ins->inst_right = sp [2];
8635 MONO_ADD_INS (bblock, ins);
8641 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8648 case CEE_CKFINITE: {
8649 MonoInst *store, *temp;
8652 /* this instr. can throw exceptions as side effect,
8653 * so we cant eliminate dead code which contains CKFINITE opdodes.
8654 * Spilling to memory makes sure that we always perform
8658 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
8659 ins->inst_left = sp [-1];
8660 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
8662 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8663 MONO_ADD_INS (bblock, store);
8665 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
8674 token = read32 (ip + 1);
8675 klass = mono_class_get_full (image, token, generic_context);
8676 CHECK_TYPELOAD (klass);
8677 mono_class_init (klass);
8679 /* Needed by the code generated in inssel.brg */
8680 mono_get_got_var (cfg);
8682 if (cfg->generic_sharing_context) {
8683 context_used = mono_class_check_context_used (klass);
8684 if (context_used && cfg->compile_aot)
8685 GENERIC_SHARING_FAILURE (*ip);
8691 MONO_INST_NEW (cfg, ins, OP_REFANYVAL_REG);
8692 ins->type = STACK_MP;
8693 ins->inst_left = *sp;
8696 GET_RGCTX (rgctx, context_used);
8697 ins->inst_right = get_runtime_generic_context_ptr (cfg, method, context_used,
8698 bblock, klass, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8700 MONO_INST_NEW (cfg, ins, *ip);
8701 ins->type = STACK_MP;
8702 ins->inst_left = *sp;
8704 ins->inst_newa_class = klass;
8709 case CEE_MKREFANY: {
8715 token = read32 (ip + 1);
8716 klass = mono_class_get_full (image, token, generic_context);
8717 CHECK_TYPELOAD (klass);
8718 mono_class_init (klass);
8720 if (cfg->generic_sharing_context) {
8721 context_used = mono_class_check_context_used (klass);
8722 if (context_used && cfg->compile_aot)
8723 GENERIC_SHARING_FAILURE (CEE_MKREFANY);
8726 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
8728 MonoInst *rgctx, *klass_type, *klass_klass, *loc_load;
8730 GET_RGCTX (rgctx, context_used);
8731 klass_klass = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8732 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8733 GET_RGCTX (rgctx, context_used);
8734 klass_type = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8735 generic_context, rgctx, MONO_RGCTX_INFO_TYPE, ip);
8737 NEW_TEMPLOADA (cfg, loc_load, loc->inst_c0);
8739 MONO_INST_NEW (cfg, ins, OP_MKREFANY_REGS);
8740 NEW_GROUP (cfg, ins->inst_left, klass_type, klass_klass);
8741 NEW_GROUP (cfg, ins->inst_right, *sp, loc_load);
8743 MonoInst *klassconst;
8745 NEW_PCONST (cfg, klassconst, klass);
8747 MONO_INST_NEW (cfg, ins, *ip);
8748 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
8749 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
8752 MONO_ADD_INS (bblock, ins);
8754 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
8761 MonoClass *handle_class;
8763 CHECK_STACK_OVF (1);
8766 n = read32 (ip + 1);
8768 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
8769 handle = mono_method_get_wrapper_data (method, n);
8770 handle_class = mono_method_get_wrapper_data (method, n + 1);
8771 if (handle_class == mono_defaults.typehandle_class)
8772 handle = &((MonoClass*)handle)->byval_arg;
8775 handle = mono_ldtoken (image, n, &handle_class, generic_context);
8779 mono_class_init (handle_class);
8781 if (cfg->generic_sharing_context) {
8782 if (handle_class == mono_defaults.typehandle_class) {
8783 /* If we get a MONO_TYPE_CLASS
8784 then we need to provide the
8786 instantiation of it. */
8787 if (mono_type_get_type (handle) == MONO_TYPE_CLASS)
8790 context_used = mono_class_check_context_used (mono_class_from_mono_type (handle));
8791 } else if (handle_class == mono_defaults.fieldhandle_class)
8792 context_used = mono_class_check_context_used (((MonoClassField*)handle)->parent);
8793 else if (handle_class == mono_defaults.methodhandle_class)
8794 context_used = mono_method_check_context_used (handle);
8796 g_assert_not_reached ();
8799 if (cfg->opt & MONO_OPT_SHARED) {
8801 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
8802 int method_context_used;
8804 if (cfg->generic_sharing_context)
8805 method_context_used = mono_method_check_context_used (method);
8807 method_context_used = 0;
8809 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8811 NEW_IMAGECONST (cfg, iargs [0], image);
8812 NEW_ICONST (cfg, iargs [1], n);
8813 if (method_context_used) {
8816 GET_RGCTX (rgctx, method_context_used);
8817 iargs [2] = get_runtime_generic_context_method (cfg, method, method_context_used,
8819 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
8820 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper_generic_shared,
8823 NEW_PCONST (cfg, iargs [2], generic_context);
8824 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
8826 NEW_TEMPLOAD (cfg, res, temp);
8827 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8828 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
8829 MONO_ADD_INS (bblock, store);
8830 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8832 if ((ip + 10 < end) && ip_in_bb (cfg, bblock, ip + 5) &&
8833 handle_class == mono_defaults.typehandle_class &&
8834 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
8835 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
8836 (cmethod->klass == mono_defaults.monotype_class->parent) &&
8837 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
8838 MonoClass *tclass = mono_class_from_mono_type (handle);
8839 mono_class_init (tclass);
8843 g_assert (!cfg->compile_aot);
8845 GET_RGCTX (rgctx, context_used);
8846 ins = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, tclass,
8847 generic_context, rgctx, MONO_RGCTX_INFO_REFLECTION_TYPE, ip);
8848 } else if (cfg->compile_aot) {
8850 * FIXME: We would have to include the context into the
8851 * aot constant too (tests/generic-array-type.2.exe).
8853 if (generic_context)
8854 cfg->disable_aot = TRUE;
8855 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
8857 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
8859 ins->type = STACK_OBJ;
8860 ins->klass = cmethod->klass;
8863 MonoInst *store, *addr, *vtvar;
8868 g_assert (!cfg->compile_aot);
8870 GET_RGCTX (rgctx, context_used);
8871 if (handle_class == mono_defaults.typehandle_class) {
8872 ins = get_runtime_generic_context_ptr (cfg, method,
8873 context_used, bblock,
8874 mono_class_from_mono_type (handle), generic_context,
8875 rgctx, MONO_RGCTX_INFO_TYPE, ip);
8876 } else if (handle_class == mono_defaults.methodhandle_class) {
8877 ins = get_runtime_generic_context_method (cfg, method,
8878 context_used, bblock, handle, generic_context,
8879 rgctx, MONO_RGCTX_INFO_METHOD, ip);
8880 } else if (handle_class == mono_defaults.fieldhandle_class) {
8881 ins = get_runtime_generic_context_field (cfg, method,
8882 context_used, bblock, handle, generic_context,
8883 rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
8885 g_assert_not_reached ();
8888 else if (cfg->compile_aot) {
8889 NEW_LDTOKENCONST (cfg, ins, image, n);
8891 NEW_PCONST (cfg, ins, handle);
8893 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8894 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8895 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
8896 MONO_ADD_INS (bblock, store);
8897 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8913 case CEE_ADD_OVF_UN:
8915 case CEE_MUL_OVF_UN:
8917 case CEE_SUB_OVF_UN:
8920 if (mono_find_jit_opcode_emulation (ins->opcode)) {
8922 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
8926 case CEE_ENDFINALLY:
8927 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
8928 MONO_ADD_INS (bblock, ins);
8930 start_new_bblock = 1;
8933 * Control will leave the method so empty the stack, otherwise
8934 * the next basic block will start with a nonempty stack.
8936 while (sp != stack_start) {
8937 MONO_INST_NEW (cfg, ins, CEE_POP);
8940 MONO_ADD_INS (bblock, ins);
8947 if (*ip == CEE_LEAVE) {
8949 target = ip + 5 + (gint32)read32(ip + 1);
8952 target = ip + 2 + (signed char)(ip [1]);
8955 /* empty the stack */
8956 while (sp != stack_start) {
8957 MONO_INST_NEW (cfg, ins, CEE_POP);
8960 MONO_ADD_INS (bblock, ins);
8964 * If this leave statement is in a catch block, check for a
8965 * pending exception, and rethrow it if necessary.
8967 for (i = 0; i < header->num_clauses; ++i) {
8968 MonoExceptionClause *clause = &header->clauses [i];
8971 * Use <= in the final comparison to handle clauses with multiple
8972 * leave statements, like in bug #78024.
8973 * The ordering of the exception clauses guarantees that we find the
8976 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) && (ip - header->code + ((*ip == CEE_LEAVE) ? 5 : 2)) <= (clause->handler_offset + clause->handler_len)) {
8980 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
8982 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_undeniable_exception, NULL, ip);
8983 NEW_TEMPLOAD (cfg, *sp, temp);
8985 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
8986 ins->inst_left = *sp;
8987 ins->inst_right = load;
8988 MONO_ADD_INS (bblock, ins);
8992 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
8994 for (tmp = handlers; tmp; tmp = tmp->next) {
8996 link_bblock (cfg, bblock, tblock);
8997 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
8998 ins->inst_target_bb = tblock;
8999 MONO_ADD_INS (bblock, ins);
9001 g_list_free (handlers);
9004 MONO_INST_NEW (cfg, ins, OP_BR);
9005 MONO_ADD_INS (bblock, ins);
9006 GET_BBLOCK (cfg, tblock, target);
9007 link_bblock (cfg, bblock, tblock);
9008 CHECK_BBLOCK (target, ip, tblock);
9009 ins->inst_target_bb = tblock;
9010 start_new_bblock = 1;
9012 if (*ip == CEE_LEAVE)
9021 MONO_INST_NEW (cfg, ins, *ip);
9023 handle_loaded_temps (cfg, bblock, stack_start, sp);
9024 MONO_ADD_INS (bblock, ins);
9026 ins->inst_i0 = sp [0];
9027 ins->inst_i1 = sp [1];
9035 /* trampoline mono specific opcodes */
9036 case MONO_CUSTOM_PREFIX: {
9038 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
9043 case CEE_MONO_ICALL: {
9046 MonoJitICallInfo *info;
9048 token = read32 (ip + 2);
9049 func = mono_method_get_wrapper_data (method, token);
9050 info = mono_find_jit_icall_by_addr (func);
9052 g_error ("An attempt has been made to perform an icall to address %p, "
9053 "but the address has not been registered as an icall\n", info);
9054 g_assert_not_reached ();
9057 CHECK_STACK (info->sig->param_count);
9058 sp -= info->sig->param_count;
9060 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
9061 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
9062 NEW_TEMPLOAD (cfg, *sp, temp);
9067 inline_costs += 10 * num_calls++;
9071 case CEE_MONO_LDPTR: {
9074 CHECK_STACK_OVF (1);
9076 token = read32 (ip + 2);
9078 ptr = mono_method_get_wrapper_data (method, token);
9079 if (cfg->compile_aot && (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)) {
9080 MonoMethod *wrapped = mono_marshal_method_from_wrapper (cfg->method);
9082 if (wrapped && ptr != NULL && mono_lookup_internal_call (wrapped) == ptr) {
9083 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, wrapped);
9089 if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
9090 MonoJitICallInfo *callinfo;
9091 const char *icall_name;
9093 icall_name = method->name + strlen ("__icall_wrapper_");
9094 g_assert (icall_name);
9095 callinfo = mono_find_jit_icall_by_name (icall_name);
9096 g_assert (callinfo);
9098 if (ptr == callinfo->func) {
9099 /* Will be transformed into an AOTCONST later */
9100 NEW_PCONST (cfg, ins, ptr);
9107 /* FIXME: Generalize this */
9108 if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
9109 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
9114 NEW_PCONST (cfg, ins, ptr);
9117 inline_costs += 10 * num_calls++;
9118 /* Can't embed random pointers into AOT code */
9119 cfg->disable_aot = 1;
9122 case CEE_MONO_ICALL_ADDR: {
9123 MonoMethod *cmethod;
9125 CHECK_STACK_OVF (1);
9127 token = read32 (ip + 2);
9129 cmethod = mono_method_get_wrapper_data (method, token);
9131 g_assert (cfg->compile_aot);
9133 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
9138 case CEE_MONO_VTADDR:
9141 MONO_INST_NEW (cfg, ins, OP_VTADDR);
9142 ins->type = STACK_MP;
9143 ins->inst_left = *sp;
9147 case CEE_MONO_NEWOBJ: {
9148 MonoInst *iargs [2];
9150 CHECK_STACK_OVF (1);
9152 token = read32 (ip + 2);
9153 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9154 mono_class_init (klass);
9155 NEW_DOMAINCONST (cfg, iargs [0]);
9156 NEW_CLASSCONST (cfg, iargs [1], klass);
9157 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
9158 NEW_TEMPLOAD (cfg, *sp, temp);
9161 inline_costs += 10 * num_calls++;
9164 case CEE_MONO_OBJADDR:
9167 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
9168 ins->type = STACK_MP;
9169 ins->inst_left = *sp;
9173 case CEE_MONO_LDNATIVEOBJ:
9176 token = read32 (ip + 2);
9177 klass = mono_method_get_wrapper_data (method, token);
9178 g_assert (klass->valuetype);
9179 mono_class_init (klass);
9180 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
9184 case CEE_MONO_RETOBJ:
9185 g_assert (cfg->ret);
9186 g_assert (mono_method_signature (method)->pinvoke);
9191 token = read32 (ip + 2);
9192 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9194 NEW_RETLOADA (cfg, ins);
9195 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
9197 if (sp != stack_start)
9200 MONO_INST_NEW (cfg, ins, OP_BR);
9201 ins->inst_target_bb = end_bblock;
9202 MONO_ADD_INS (bblock, ins);
9203 link_bblock (cfg, bblock, end_bblock);
9204 start_new_bblock = 1;
9207 case CEE_MONO_CISINST:
9208 case CEE_MONO_CCASTCLASS: {
9213 token = read32 (ip + 2);
9214 /* Needed by the code generated in inssel.brg */
9215 mono_get_got_var (cfg);
9219 * The code generated for CCASTCLASS has too much register pressure
9220 * (obj+vtable+ibitmap_byte_reg+iid_reg), leading to the usual
9221 * branches-inside-bblocks problem.
9223 cfg->disable_aot = TRUE;
9226 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9227 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
9228 ins->type = STACK_I4;
9229 ins->inst_left = *sp;
9230 ins->inst_newa_class = klass;
9231 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
9235 case CEE_MONO_SAVE_LMF:
9236 case CEE_MONO_RESTORE_LMF:
9237 #ifdef MONO_ARCH_HAVE_LMF_OPS
9238 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
9239 MONO_ADD_INS (bblock, ins);
9240 cfg->need_lmf_area = TRUE;
9244 case CEE_MONO_CLASSCONST:
9245 CHECK_STACK_OVF (1);
9247 token = read32 (ip + 2);
9248 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
9251 inline_costs += 10 * num_calls++;
9253 case CEE_MONO_NOT_TAKEN:
9254 bblock->out_of_line = TRUE;
9258 CHECK_STACK_OVF (1);
9260 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
9261 ins->inst_offset = (gint32)read32 (ip + 2);
9262 ins->type = STACK_PTR;
9267 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
9276 /* somewhat similar to LDTOKEN */
9277 MonoInst *addr, *vtvar;
9278 CHECK_STACK_OVF (1);
9279 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
9281 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
9282 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
9283 ins->inst_left = addr;
9284 MONO_ADD_INS (bblock, ins);
9285 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
9298 * The following transforms:
9299 * CEE_CEQ into OP_CEQ
9300 * CEE_CGT into OP_CGT
9301 * CEE_CGT_UN into OP_CGT_UN
9302 * CEE_CLT into OP_CLT
9303 * CEE_CLT_UN into OP_CLT_UN
9305 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
9307 MONO_INST_NEW (cfg, ins, cmp->opcode);
9309 cmp->inst_i0 = sp [0];
9310 cmp->inst_i1 = sp [1];
9313 ins->type = STACK_I4;
9315 #if MONO_ARCH_SOFT_FLOAT
9316 if (sp [0]->type == STACK_R8) {
9317 cmp->type = STACK_I4;
9318 *sp++ = emit_tree (cfg, bblock, cmp, ip + 2);
9323 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
9324 cmp->opcode = OP_LCOMPARE;
9326 cmp->opcode = OP_COMPARE;
9328 /* spill it to reduce the expression complexity
9329 * and workaround bug 54209
9331 if (cmp->inst_left->type == STACK_I8) {
9333 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
9340 MonoMethod *cil_method, *ctor_method;
9342 gboolean needs_static_rgctx_invoke;
9344 CHECK_STACK_OVF (1);
9346 n = read32 (ip + 2);
9347 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9350 mono_class_init (cmethod->klass);
9352 if (cfg->generic_sharing_context)
9353 context_used = mono_method_check_context_used (cmethod);
9355 needs_static_rgctx_invoke = mono_method_needs_static_rgctx_invoke (cmethod, TRUE);
9357 cil_method = cmethod;
9358 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
9359 METHOD_ACCESS_FAILURE;
9360 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9361 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9363 CHECK_CFG_EXCEPTION;
9364 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9365 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9369 * Optimize the common case of ldftn+delegate creation
9371 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE) && !defined(HAVE_WRITE_BARRIERS)
9372 /* FIXME: SGEN support */
9373 /* FIXME: handle shared static generic methods */
9374 /* FIXME: handle this in shared code */
9375 if (!needs_static_rgctx_invoke && !context_used && (sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, bblock, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context)) && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
9376 MonoInst *target_ins;
9379 if (cfg->verbose_level > 3)
9380 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
9381 target_ins = sp [-1];
9383 *sp = handle_delegate_ctor (cfg, bblock, ctor_method->klass, target_ins, cmethod, ip);
9390 handle_loaded_temps (cfg, bblock, stack_start, sp);
9395 if (needs_static_rgctx_invoke)
9396 cmethod = mono_marshal_get_static_rgctx_invoke (cmethod);
9398 GET_RGCTX (rgctx, context_used);
9399 argconst = get_runtime_generic_context_method (cfg, method, context_used,
9401 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9402 } else if (needs_static_rgctx_invoke) {
9403 NEW_METHODCONST (cfg, argconst, mono_marshal_get_static_rgctx_invoke (cmethod));
9405 NEW_METHODCONST (cfg, argconst, cmethod);
9407 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
9408 NEW_TEMPLOAD (cfg, *sp, temp);
9412 inline_costs += 10 * num_calls++;
9415 case CEE_LDVIRTFTN: {
9421 n = read32 (ip + 2);
9422 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9425 mono_class_init (cmethod->klass);
9427 if (cfg->generic_sharing_context)
9428 context_used = mono_method_check_context_used (cmethod);
9430 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9431 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9433 CHECK_CFG_EXCEPTION;
9434 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9435 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9438 handle_loaded_temps (cfg, bblock, stack_start, sp);
9445 GET_RGCTX (rgctx, context_used);
9446 args [1] = get_runtime_generic_context_method (cfg, method, context_used,
9448 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9449 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn_gshared, args, ip);
9451 NEW_METHODCONST (cfg, args [1], cmethod);
9452 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
9454 NEW_TEMPLOAD (cfg, *sp, temp);
9458 inline_costs += 10 * num_calls++;
9462 CHECK_STACK_OVF (1);
9464 n = read16 (ip + 2);
9466 NEW_ARGLOAD (cfg, ins, n);
9467 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
9472 CHECK_STACK_OVF (1);
9474 n = read16 (ip + 2);
9476 NEW_ARGLOADA (cfg, ins, n);
9483 handle_loaded_temps (cfg, bblock, stack_start, sp);
9485 n = read16 (ip + 2);
9487 NEW_ARGSTORE (cfg, ins, n, *sp);
9488 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
9490 STARG_SOFT_FLOAT (cfg, ins, n, ip);
9491 if (ins->opcode == CEE_STOBJ) {
9492 NEW_ARGLOADA (cfg, ins, n);
9493 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9495 MONO_ADD_INS (bblock, ins);
9499 CHECK_STACK_OVF (1);
9501 n = read16 (ip + 2);
9503 NEW_LOCLOAD (cfg, ins, n);
9504 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
9509 CHECK_STACK_OVF (1);
9511 n = read16 (ip + 2);
9513 NEW_LOCLOADA (cfg, ins, n);
9521 n = read16 (ip + 2);
9523 handle_loaded_temps (cfg, bblock, stack_start, sp);
9524 NEW_LOCSTORE (cfg, ins, n, *sp);
9525 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
9527 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
9528 if (ins->opcode == CEE_STOBJ) {
9529 NEW_LOCLOADA (cfg, ins, n);
9530 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9532 MONO_ADD_INS (bblock, ins);
9539 if (sp != stack_start)
9541 if (cfg->method != method)
9543 * Inlining this into a loop in a parent could lead to
9544 * stack overflows which is different behavior than the
9545 * non-inlined case, thus disable inlining in this case.
9547 goto inline_failure;
9548 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
9549 ins->inst_left = *sp;
9550 ins->type = STACK_PTR;
9552 cfg->flags |= MONO_CFG_HAS_ALLOCA;
9553 if (header->init_locals)
9554 ins->flags |= MONO_INST_INIT;
9558 /* FIXME: set init flag if locals init is set in this method */
9560 case CEE_ENDFILTER: {
9561 MonoExceptionClause *clause, *nearest;
9562 int cc, nearest_num;
9566 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
9568 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
9569 ins->inst_left = *sp;
9570 MONO_ADD_INS (bblock, ins);
9571 start_new_bblock = 1;
9576 for (cc = 0; cc < header->num_clauses; ++cc) {
9577 clause = &header->clauses [cc];
9578 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
9579 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
9580 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
9586 if ((ip - header->code) != nearest->handler_offset)
9591 case CEE_UNALIGNED_:
9592 ins_flag |= MONO_INST_UNALIGNED;
9593 /* FIXME: record alignment? we can assume 1 for now */
9598 ins_flag |= MONO_INST_VOLATILE;
9602 ins_flag |= MONO_INST_TAILCALL;
9603 cfg->flags |= MONO_CFG_HAS_TAIL;
9604 /* Can't inline tail calls at this time */
9605 inline_costs += 100000;
9612 token = read32 (ip + 2);
9613 klass = mini_get_class (method, token, generic_context);
9614 CHECK_TYPELOAD (klass);
9616 if (generic_class_is_reference_type (cfg, klass)) {
9617 MonoInst *store, *load;
9618 NEW_PCONST (cfg, load, NULL);
9619 load->type = STACK_OBJ;
9620 load->klass = klass;
9621 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
9622 handle_loaded_temps (cfg, bblock, stack_start, sp);
9623 MONO_ADD_INS (bblock, store);
9624 store->inst_i0 = sp [0];
9625 store->inst_i1 = load;
9627 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
9632 case CEE_CONSTRAINED_:
9633 /* FIXME: implement */
9635 token = read32 (ip + 2);
9636 constrained_call = mono_class_get_full (image, token, generic_context);
9637 CHECK_TYPELOAD (constrained_call);
9642 MonoInst *iargs [3];
9645 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
9647 NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, 0);
9648 MONO_ADD_INS (bblock, copy);
9655 handle_loaded_temps (cfg, bblock, stack_start, sp);
9656 if (ip [1] == CEE_CPBLK) {
9657 MonoMethod *memcpy_method = get_memcpy_method ();
9658 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
9660 MonoMethod *memset_method = get_memset_method ();
9661 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
9670 ins_flag |= MONO_INST_NOTYPECHECK;
9672 ins_flag |= MONO_INST_NORANGECHECK;
9673 /* we ignore the no-nullcheck for now since we
9674 * really do it explicitly only when doing callvirt->call
9680 int handler_offset = -1;
9682 for (i = 0; i < header->num_clauses; ++i) {
9683 MonoExceptionClause *clause = &header->clauses [i];
9684 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
9685 handler_offset = clause->handler_offset;
9690 bblock->flags |= BB_EXCEPTION_UNSAFE;
9692 g_assert (handler_offset != -1);
9694 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
9695 MONO_INST_NEW (cfg, ins, OP_RETHROW);
9696 ins->inst_left = load;
9697 MONO_ADD_INS (bblock, ins);
9699 link_bblock (cfg, bblock, end_bblock);
9700 start_new_bblock = 1;
9705 CHECK_STACK_OVF (1);
9707 token = read32 (ip + 2);
9708 /* FIXXME: handle generics. */
9709 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
9710 MonoType *type = mono_type_create_from_typespec (image, token);
9711 token = mono_type_size (type, &ialign);
9713 MonoClass *klass = mono_class_get_full (image, token, generic_context);
9714 CHECK_TYPELOAD (klass);
9715 mono_class_init (klass);
9716 token = mono_class_value_size (klass, &align);
9718 NEW_ICONST (cfg, ins, token);
9722 case CEE_REFANYTYPE:
9724 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
9726 ins->type = STACK_MP;
9727 ins->inst_left = *sp;
9728 ins->type = STACK_VTYPE;
9729 ins->klass = mono_defaults.typehandle_class;
9738 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
9743 g_error ("opcode 0x%02x not handled", *ip);
9746 if (start_new_bblock != 1)
9749 bblock->cil_length = ip - bblock->cil_code;
9750 bblock->next_bb = end_bblock;
9752 if (cfg->method == method && cfg->domainvar) {
9754 MonoInst *get_domain;
9756 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
9759 MONO_INST_NEW_CALL (cfg, call, OP_CALL);
9760 call->signature = helper_sig_domain_get;
9761 call->inst.type = STACK_PTR;
9762 call->fptr = mono_domain_get;
9763 get_domain = (MonoInst*)call;
9766 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
9767 MONO_ADD_INS (init_localsbb, store);
9770 if (cfg->method == method && cfg->got_var)
9771 mono_emit_load_got_addr (cfg);
9773 if (header->init_locals) {
9775 cfg->ip = header->code;
9776 for (i = 0; i < header->num_locals; ++i) {
9777 MonoType *ptype = header->locals [i];
9778 int t = ptype->type;
9779 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
9780 t = ptype->data.klass->enum_basetype->type;
9782 NEW_PCONST (cfg, ins, NULL);
9783 NEW_LOCSTORE (cfg, store, i, ins);
9784 MONO_ADD_INS (init_localsbb, store);
9785 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
9786 NEW_ICONST (cfg, ins, 0);
9787 NEW_LOCSTORE (cfg, store, i, ins);
9788 MONO_ADD_INS (init_localsbb, store);
9789 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
9790 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9791 ins->type = STACK_I8;
9793 NEW_LOCSTORE (cfg, store, i, ins);
9794 MONO_ADD_INS (init_localsbb, store);
9795 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
9796 #ifdef MONO_ARCH_SOFT_FLOAT
9797 /* FIXME: handle init of R4 */
9799 MONO_INST_NEW (cfg, ins, OP_R8CONST);
9800 ins->type = STACK_R8;
9801 ins->inst_p0 = (void*)&r8_0;
9802 NEW_LOCSTORE (cfg, store, i, ins);
9803 MONO_ADD_INS (init_localsbb, store);
9805 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
9806 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
9807 NEW_LOCLOADA (cfg, ins, i);
9808 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
9810 NEW_PCONST (cfg, ins, NULL);
9811 NEW_LOCSTORE (cfg, store, i, ins);
9812 MONO_ADD_INS (init_localsbb, store);
9819 /* resolve backward branches in the middle of an existing basic block */
9820 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
9822 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
9823 tblock = find_previous (cfg->cil_offset_to_bb, header->code_size, start_bblock, bblock->cil_code);
9824 if (tblock != start_bblock) {
9826 split_bblock (cfg, tblock, bblock);
9827 l = bblock->cil_code - header->code;
9828 bblock->cil_length = tblock->cil_length - l;
9829 tblock->cil_length = l;
9831 g_print ("recheck failed.\n");
9835 if (cfg->method == method) {
9837 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9838 bb->region = mono_find_block_region (cfg, bb->real_offset);
9840 mono_create_spvar_for_region (cfg, bb->region);
9841 if (cfg->verbose_level > 2)
9842 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
9846 g_slist_free (class_inits);
9847 dont_inline = g_list_remove (dont_inline, method);
9849 if (inline_costs < 0) {
9852 /* Method is too large */
9853 mname = mono_method_full_name (method, TRUE);
9854 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
9855 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
9860 return inline_costs;
9863 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
9864 g_slist_free (class_inits);
9865 dont_inline = g_list_remove (dont_inline, method);
9869 g_slist_free (class_inits);
9870 dont_inline = g_list_remove (dont_inline, method);
9874 g_slist_free (class_inits);
9875 dont_inline = g_list_remove (dont_inline, method);
9876 cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
9880 g_slist_free (class_inits);
9881 dont_inline = g_list_remove (dont_inline, method);
9882 set_exception_type_from_invalid_il (cfg, method, ip);
9887 mono_print_tree (MonoInst *tree) {
9893 arity = mono_burg_arity [tree->opcode];
9895 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
9897 switch (tree->opcode) {
9899 printf ("[%d]", (int)tree->inst_c0);
9902 printf ("[%lld]", (long long)tree->inst_l);
9905 printf ("[%f]", *(double*)tree->inst_p0);
9908 printf ("[%f]", *(float*)tree->inst_p0);
9912 printf ("[%d]", (int)tree->inst_c0);
9915 if (tree->inst_offset < 0)
9916 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9918 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9921 printf ("[%s]", mono_arch_regname (tree->dreg));
9924 printf ("[%s]", tree->inst_newa_class->name);
9925 mono_print_tree (tree->inst_newa_len);
9936 case OP_VOIDCALLVIRT:
9937 case OP_TRAMPCALL_VTABLE: {
9938 MonoCallInst *call = (MonoCallInst*)tree;
9940 printf ("[%s]", call->method->name);
9941 else if (call->fptr) {
9942 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
9944 printf ("[%s]", info->name);
9950 printf ("[%d (", (int)tree->inst_c0);
9951 for (i = 0; i < tree->inst_phi_args [0]; i++) {
9954 printf ("%d", tree->inst_phi_args [i + 1]);
9965 case OP_LOAD_MEMBASE:
9966 case OP_LOADI4_MEMBASE:
9967 case OP_LOADU4_MEMBASE:
9968 case OP_LOADU1_MEMBASE:
9969 case OP_LOADI1_MEMBASE:
9970 case OP_LOADU2_MEMBASE:
9971 case OP_LOADI2_MEMBASE:
9972 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
9975 case OP_CALL_HANDLER:
9976 printf ("[B%d]", tree->inst_target_bb->block_num);
9986 case OP_VOIDCALL_REG:
9987 mono_print_tree (tree->inst_left);
9999 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
10000 mono_print_tree (tree->inst_left);
10003 if (!mono_arch_print_tree(tree, arity)) {
10005 mono_print_tree (tree->inst_left);
10007 mono_print_tree (tree->inst_right);
10018 mono_print_tree_nl (MonoInst *tree)
10020 mono_print_tree (tree);
10025 create_helper_signature (void)
10027 helper_sig_domain_get = mono_create_icall_signature ("ptr");
10028 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
10029 helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
10030 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
10033 static gconstpointer
10034 mono_icall_get_wrapper_full (MonoJitICallInfo* callinfo, gboolean do_compile)
10037 MonoMethod *wrapper;
10038 gconstpointer trampoline;
10039 MonoDomain *domain = mono_get_root_domain ();
10041 if (callinfo->wrapper) {
10042 return callinfo->wrapper;
10045 if (callinfo->trampoline)
10046 return callinfo->trampoline;
10049 * We use the lock on the root domain instead of the JIT lock to protect
10050 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
10052 mono_domain_lock (domain);
10054 if (callinfo->trampoline) {
10055 mono_domain_unlock (domain);
10056 return callinfo->trampoline;
10059 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
10060 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
10064 trampoline = mono_compile_method (wrapper);
10066 trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
10067 mono_register_jit_icall_wrapper (callinfo, trampoline);
10069 callinfo->trampoline = trampoline;
10071 mono_domain_unlock (domain);
10073 return callinfo->trampoline;
10077 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
10079 return mono_icall_get_wrapper_full (callinfo, FALSE);
10083 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
10085 if (!domain_jit_info (domain)->dynamic_code_hash)
10086 domain_jit_info (domain)->dynamic_code_hash = g_hash_table_new (NULL, NULL);
10087 g_hash_table_insert (domain_jit_info (domain)->dynamic_code_hash, method, ji);
10090 static MonoJitDynamicMethodInfo*
10091 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
10093 MonoJitDynamicMethodInfo *res;
10095 if (domain_jit_info (domain)->dynamic_code_hash)
10096 res = g_hash_table_lookup (domain_jit_info (domain)->dynamic_code_hash, method);
10104 GList *active, *inactive;
10109 compare_by_interval_start_pos_func (gconstpointer a, gconstpointer b)
10111 MonoMethodVar *v1 = (MonoMethodVar*)a;
10112 MonoMethodVar *v2 = (MonoMethodVar*)b;
10116 else if (v1->interval->range && v2->interval->range)
10117 return v1->interval->range->from - v2->interval->range->from;
10118 else if (v1->interval->range)
10124 #ifndef DISABLE_JIT
10127 #define LSCAN_DEBUG(a) do { a; } while (0)
10129 #define LSCAN_DEBUG(a)
10133 mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10135 int i, slot, offset, size;
10137 MonoMethodVar *vmv;
10140 GList *vars = NULL, *l, *unhandled;
10141 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10145 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg->method, TRUE)));
10147 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10148 vtype_stack_slots = NULL;
10151 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10152 for (i = 0; i < cfg->num_varinfo; ++i)
10155 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10156 inst = cfg->varinfo [i];
10157 vmv = MONO_VARINFO (cfg, i);
10159 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10162 vars = g_list_prepend (vars, vmv);
10165 vars = g_list_sort (g_list_copy (vars), compare_by_interval_start_pos_func);
10170 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10171 MonoMethodVar *current = unhandled->data;
10173 if (current->interval->range) {
10174 g_assert (current->interval->range->from >= i);
10175 i = current->interval->range->from;
10182 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10183 MonoMethodVar *current = unhandled->data;
10186 inst = cfg->varinfo [vmv->idx];
10188 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10189 * pinvoke wrappers when they call functions returning structures */
10190 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10191 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10195 size = mono_type_size (inst->inst_vtype, &ialign);
10199 t = mono_type_get_underlying_type (inst->inst_vtype);
10201 case MONO_TYPE_GENERICINST:
10202 if (!mono_type_generic_inst_is_valuetype (t)) {
10203 slot_info = &scalar_stack_slots [t->type];
10207 case MONO_TYPE_VALUETYPE:
10208 if (!vtype_stack_slots)
10209 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10210 for (i = 0; i < nvtypes; ++i)
10211 if (t->data.klass == vtype_stack_slots [i].vtype)
10214 slot_info = &vtype_stack_slots [i];
10216 g_assert (nvtypes < 256);
10217 vtype_stack_slots [nvtypes].vtype = t->data.klass;
10218 slot_info = &vtype_stack_slots [nvtypes];
10222 case MONO_TYPE_CLASS:
10223 case MONO_TYPE_OBJECT:
10224 case MONO_TYPE_ARRAY:
10225 case MONO_TYPE_SZARRAY:
10226 case MONO_TYPE_STRING:
10227 case MONO_TYPE_PTR:
10230 #if SIZEOF_VOID_P == 4
10234 /* Share non-float stack slots of the same size */
10235 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10239 slot_info = &scalar_stack_slots [t->type];
10243 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10247 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10249 if (!current->interval->range) {
10250 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
10254 inst->flags |= MONO_INST_IS_DEAD;
10259 pos = current->interval->range->from;
10261 LSCAN_DEBUG (printf ("process R%d ", inst->dreg));
10262 if (current->interval->range)
10263 LSCAN_DEBUG (mono_linterval_print (current->interval));
10264 LSCAN_DEBUG (printf ("\n"));
10266 /* Check for intervals in active which expired or inactive */
10268 /* FIXME: Optimize this */
10271 for (l = slot_info->active; l != NULL; l = l->next) {
10272 MonoMethodVar *v = (MonoMethodVar*)l->data;
10274 if (v->interval->last_range->to < pos) {
10275 slot_info->active = g_list_delete_link (slot_info->active, l);
10276 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10277 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10281 else if (!mono_linterval_covers (v->interval, pos)) {
10282 slot_info->inactive = g_list_append (slot_info->inactive, v);
10283 slot_info->active = g_list_delete_link (slot_info->active, l);
10284 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg->varinfo [v->idx]->dreg));
10291 /* Check for intervals in inactive which expired or active */
10293 /* FIXME: Optimize this */
10296 for (l = slot_info->inactive; l != NULL; l = l->next) {
10297 MonoMethodVar *v = (MonoMethodVar*)l->data;
10299 if (v->interval->last_range->to < pos) {
10300 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10301 // FIXME: Enabling this seems to cause impossible to debug crashes
10302 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10303 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10307 else if (mono_linterval_covers (v->interval, pos)) {
10308 slot_info->active = g_list_append (slot_info->active, v);
10309 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10310 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg->varinfo [v->idx]->dreg));
10318 * This also handles the case when the variable is used in an
10319 * exception region, as liveness info is not computed there.
10322 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10325 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10326 if (slot_info->slots) {
10327 slot = GPOINTER_TO_INT (slot_info->slots->data);
10329 slot_info->slots = slot_info->slots->next;
10332 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
10334 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10340 static int count = 0;
10343 if (count == atoi (getenv ("COUNT3")))
10344 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10345 if (count > atoi (getenv ("COUNT3")))
10348 mono_print_tree_nl (inst);
10353 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst->dreg, mono_type_full_name (t), slot));
10355 if (slot == 0xffffff) {
10357 * Allways allocate valuetypes to sizeof (gpointer) to allow more
10358 * efficient copying (and to work around the fact that OP_MEMCPY
10359 * and OP_MEMSET ignores alignment).
10361 if (MONO_TYPE_ISSTRUCT (t))
10362 align = sizeof (gpointer);
10366 offset += align - 1;
10367 offset &= ~(align - 1);
10371 offset += align - 1;
10372 offset &= ~(align - 1);
10377 if (*stack_align == 0)
10378 *stack_align = align;
10381 offsets [vmv->idx] = slot;
10383 g_list_free (vars);
10384 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10385 if (scalar_stack_slots [i].active)
10386 g_list_free (scalar_stack_slots [i].active);
10388 for (i = 0; i < nvtypes; ++i) {
10389 if (vtype_stack_slots [i].active)
10390 g_list_free (vtype_stack_slots [i].active);
10393 mono_jit_stats.locals_stack_size += offset;
10395 *stack_size = offset;
10400 * mono_allocate_stack_slots_full:
10402 * Allocate stack slots for all non register allocated variables using a
10403 * linear scan algorithm.
10404 * Returns: an array of stack offsets.
10405 * STACK_SIZE is set to the amount of stack space needed.
10406 * STACK_ALIGN is set to the alignment needed by the locals area.
10409 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10411 int i, slot, offset, size;
10413 MonoMethodVar *vmv;
10416 GList *vars = NULL, *l;
10417 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10421 if ((cfg->num_varinfo > 0) && MONO_VARINFO (cfg, 0)->interval)
10422 return mono_allocate_stack_slots_full2 (cfg, backward, stack_size, stack_align);
10424 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10425 vtype_stack_slots = NULL;
10428 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10429 for (i = 0; i < cfg->num_varinfo; ++i)
10432 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10433 inst = cfg->varinfo [i];
10434 vmv = MONO_VARINFO (cfg, i);
10436 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10439 vars = g_list_prepend (vars, vmv);
10442 vars = mono_varlist_sort (cfg, vars, 0);
10444 *stack_align = sizeof (gpointer);
10445 for (l = vars; l; l = l->next) {
10447 inst = cfg->varinfo [vmv->idx];
10449 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10450 * pinvoke wrappers when they call functions returning structures */
10451 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10452 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10456 size = mono_type_size (inst->inst_vtype, &ialign);
10460 t = mono_type_get_underlying_type (inst->inst_vtype);
10462 slot_info = &scalar_stack_slots [MONO_TYPE_I];
10465 case MONO_TYPE_GENERICINST:
10466 if (!mono_type_generic_inst_is_valuetype (t)) {
10467 slot_info = &scalar_stack_slots [t->type];
10471 case MONO_TYPE_VALUETYPE:
10472 if (!vtype_stack_slots)
10473 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10474 for (i = 0; i < nvtypes; ++i)
10475 if (t->data.klass == vtype_stack_slots [i].vtype)
10478 slot_info = &vtype_stack_slots [i];
10480 g_assert (nvtypes < 256);
10481 vtype_stack_slots [nvtypes].vtype = t->data.klass;
10482 slot_info = &vtype_stack_slots [nvtypes];
10486 case MONO_TYPE_CLASS:
10487 case MONO_TYPE_OBJECT:
10488 case MONO_TYPE_ARRAY:
10489 case MONO_TYPE_SZARRAY:
10490 case MONO_TYPE_STRING:
10491 case MONO_TYPE_PTR:
10494 #if SIZEOF_VOID_P == 4
10499 /* Share non-float stack slots of the same size */
10500 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10503 slot_info = &scalar_stack_slots [t->type];
10508 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10509 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10511 /* expire old intervals in active */
10512 while (slot_info->active) {
10513 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
10515 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
10518 //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);
10520 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
10521 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
10525 * This also handles the case when the variable is used in an
10526 * exception region, as liveness info is not computed there.
10529 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10532 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10533 if (slot_info->slots) {
10534 slot = GPOINTER_TO_INT (slot_info->slots->data);
10536 slot_info->slots = slot_info->slots->next;
10539 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10544 static int count = 0;
10548 if (count == atoi (getenv ("COUNT")))
10549 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10550 if (count > atoi (getenv ("COUNT")))
10553 mono_print_tree_nl (inst);
10558 if (cfg->disable_reuse_stack_slots)
10561 if (slot == 0xffffff) {
10563 * Allways allocate valuetypes to sizeof (gpointer) to allow more
10564 * efficient copying (and to work around the fact that OP_MEMCPY
10565 * and OP_MEMSET ignores alignment).
10567 if (MONO_TYPE_ISSTRUCT (t))
10568 align = sizeof (gpointer);
10572 offset += align - 1;
10573 offset &= ~(align - 1);
10577 offset += align - 1;
10578 offset &= ~(align - 1);
10583 *stack_align = MAX (*stack_align, align);
10586 offsets [vmv->idx] = slot;
10588 g_list_free (vars);
10589 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10590 if (scalar_stack_slots [i].active)
10591 g_list_free (scalar_stack_slots [i].active);
10593 for (i = 0; i < nvtypes; ++i) {
10594 if (vtype_stack_slots [i].active)
10595 g_list_free (vtype_stack_slots [i].active);
10598 mono_jit_stats.locals_stack_size += offset;
10600 *stack_size = offset;
10605 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
10607 return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
10613 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10615 g_assert_not_reached ();
10619 #endif /* DISABLE_JIT */
10622 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
10624 MonoJitICallInfo *info;
10625 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
10627 if (!emul_opcode_map)
10628 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
10630 g_assert (!sig->hasthis);
10631 g_assert (sig->param_count < 3);
10633 info = mono_register_jit_icall (func, name, sig, no_throw);
10635 emul_opcode_map [opcode] = info;
10639 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
10641 MonoMethodSignature *sig;
10644 sig = mono_create_icall_signature (sigstr);
10648 mono_register_jit_icall (func, name, sig, save);
10652 decompose_foreach (MonoInst *tree, gpointer data)
10654 static MonoJitICallInfo *newarr_info = NULL;
10655 static MonoJitICallInfo *newarr_specific_info = NULL;
10656 MonoJitICallInfo *info;
10659 switch (tree->opcode) {
10661 MonoCompile *cfg = data;
10662 MonoInst *iargs [3];
10664 if (!newarr_info) {
10665 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
10666 g_assert (newarr_info);
10667 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
10668 g_assert (newarr_specific_info);
10671 if (cfg->opt & MONO_OPT_SHARED) {
10672 NEW_DOMAINCONST (cfg, iargs [0]);
10673 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
10674 iargs [2] = tree->inst_newa_len;
10676 info = newarr_info;
10679 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
10682 NEW_VTABLECONST (cfg, iargs [0], vtable);
10683 iargs [1] = tree->inst_newa_len;
10685 info = newarr_specific_info;
10688 mono_emulate_opcode (cfg, tree, iargs, info);
10690 /* Need to decompose arguments after the the opcode is decomposed */
10691 for (i = 0; i < info->sig->param_count; ++i)
10692 dec_foreach (iargs [i], cfg);
10695 #ifdef MONO_ARCH_SOFT_FLOAT
10706 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10707 MonoCompile *cfg = data;
10708 MonoInst *iargs [2];
10710 iargs [0] = tree->inst_i0;
10711 iargs [1] = tree->inst_i1;
10713 mono_emulate_opcode (cfg, tree, iargs, info);
10715 dec_foreach (iargs [0], cfg);
10716 dec_foreach (iargs [1], cfg);
10719 g_assert_not_reached ();
10728 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10729 MonoCompile *cfg = data;
10730 MonoInst *iargs [2];
10732 /* the args are in the compare opcode ... */
10733 iargs [0] = tree->inst_i0;
10734 iargs [1] = tree->inst_i1;
10736 mono_emulate_opcode (cfg, tree, iargs, info);
10738 dec_foreach (iargs [0], cfg);
10739 dec_foreach (iargs [1], cfg);
10742 g_assert_not_reached ();
10754 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
10756 switch (mono_burg_arity [tree->opcode]) {
10759 mono_inst_foreach (tree->inst_left, func, data);
10762 mono_inst_foreach (tree->inst_left, func, data);
10763 mono_inst_foreach (tree->inst_right, func, data);
10766 g_assert_not_reached ();
10773 mono_print_bb_code (MonoBasicBlock *bb)
10777 MONO_BB_FOR_EACH_INS (bb, c) {
10778 mono_print_tree (c);
10784 print_dfn (MonoCompile *cfg) {
10787 MonoBasicBlock *bb;
10790 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
10792 for (i = 0; i < cfg->num_bblocks; ++i) {
10793 bb = cfg->bblocks [i];
10794 /*if (bb->cil_code) {
10795 char* code1, *code2;
10796 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
10797 if (bb->last_ins->cil_code)
10798 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
10800 code2 = g_strdup ("");
10802 code1 [strlen (code1) - 1] = 0;
10803 code = g_strdup_printf ("%s -> %s", code1, code2);
10807 code = g_strdup ("\n");
10808 g_print ("\nBB%d (%d) (len: %d): %s", bb->block_num, i, bb->cil_length, code);
10809 MONO_BB_FOR_EACH_INS (bb, c) {
10811 mono_print_ins_index (-1, c);
10813 mono_print_tree (c);
10818 g_print ("\tprev:");
10819 for (j = 0; j < bb->in_count; ++j) {
10820 g_print (" BB%d", bb->in_bb [j]->block_num);
10822 g_print ("\t\tsucc:");
10823 for (j = 0; j < bb->out_count; ++j) {
10824 g_print (" BB%d", bb->out_bb [j]->block_num);
10826 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
10829 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
10831 if (bb->dominators)
10832 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
10834 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
10842 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
10844 MONO_ADD_INS (bb, inst);
10848 mono_bblock_insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10852 bb->code = ins_to_insert;
10854 /* Link with next */
10855 ins_to_insert->next = ins;
10857 ins->prev = ins_to_insert;
10859 if (bb->last_ins == NULL)
10860 bb->last_ins = ins_to_insert;
10862 /* Link with next */
10863 ins_to_insert->next = ins->next;
10865 ins->next->prev = ins_to_insert;
10867 /* Link with previous */
10868 ins->next = ins_to_insert;
10869 ins_to_insert->prev = ins;
10871 if (bb->last_ins == ins)
10872 bb->last_ins = ins_to_insert;
10877 mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10882 bb->code = ins_to_insert;
10883 ins_to_insert->next = ins;
10884 if (bb->last_ins == NULL)
10885 bb->last_ins = ins_to_insert;
10887 /* Link with previous */
10889 ins->prev->next = ins_to_insert;
10890 ins_to_insert->prev = ins->prev;
10892 /* Link with next */
10893 ins->prev = ins_to_insert;
10894 ins_to_insert->next = ins;
10896 if (bb->code == ins)
10897 bb->code = ins_to_insert;
10902 * mono_verify_bblock:
10904 * Verify that the next and prev pointers are consistent inside the instructions in BB.
10907 mono_verify_bblock (MonoBasicBlock *bb)
10909 MonoInst *ins, *prev;
10912 for (ins = bb->code; ins; ins = ins->next) {
10913 g_assert (ins->prev == prev);
10917 g_assert (!bb->last_ins->next);
10923 * Perform consistency checks on the JIT data structures and the IR
10926 mono_verify_cfg (MonoCompile *cfg)
10928 MonoBasicBlock *bb;
10930 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
10931 mono_verify_bblock (bb);
10935 mono_destroy_compile (MonoCompile *cfg)
10937 //mono_mempool_stats (cfg->mempool);
10938 mono_free_loop_info (cfg);
10940 mono_regstate_free (cfg->rs);
10942 g_hash_table_destroy (cfg->spvars);
10944 g_hash_table_destroy (cfg->exvars);
10945 mono_mempool_destroy (cfg->mempool);
10946 g_list_free (cfg->ldstr_list);
10947 g_hash_table_destroy (cfg->token_info_hash);
10948 if (cfg->abs_patches)
10949 g_hash_table_destroy (cfg->abs_patches);
10951 g_free (cfg->varinfo);
10952 g_free (cfg->vars);
10953 g_free (cfg->exception_message);
10957 #ifdef HAVE_KW_THREAD
10958 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
10959 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
10961 * When this is defined, the current lmf is stored in this tls variable instead of in
10964 static __thread gpointer mono_lmf MONO_TLS_FAST;
10969 mono_get_jit_tls_key (void)
10971 return mono_jit_tls_id;
10975 mono_get_jit_tls_offset (void)
10977 #ifdef HAVE_KW_THREAD
10979 MONO_THREAD_VAR_OFFSET (mono_jit_tls, offset);
10987 mono_get_lmf_tls_offset (void)
10989 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
10991 MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
10999 mono_get_lmf_addr_tls_offset (void)
11002 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
11007 mono_get_lmf (void)
11009 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11012 MonoJitTlsData *jit_tls;
11014 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11015 return jit_tls->lmf;
11017 g_assert_not_reached ();
11023 mono_get_lmf_addr (void)
11025 #ifdef HAVE_KW_THREAD
11026 return mono_lmf_addr;
11028 MonoJitTlsData *jit_tls;
11030 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11031 return &jit_tls->lmf;
11033 g_assert_not_reached ();
11038 /* Called by native->managed wrappers */
11040 mono_jit_thread_attach (MonoDomain *domain)
11042 #ifdef HAVE_KW_THREAD
11043 if (!mono_lmf_addr) {
11044 mono_thread_attach (domain);
11047 if (!TlsGetValue (mono_jit_tls_id))
11048 mono_thread_attach (domain);
11050 if (mono_domain_get () != domain)
11051 mono_domain_set (domain, TRUE);
11055 * mono_thread_abort:
11056 * @obj: exception object
11058 * abort the thread, print exception information and stack trace
11061 mono_thread_abort (MonoObject *obj)
11063 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
11065 /* handle_remove should be eventually called for this thread, too
11066 g_free (jit_tls);*/
11068 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANLED_POLICY_LEGACY) ||
11069 (obj->vtable->klass == mono_defaults.threadabortexception_class)) {
11070 mono_thread_exit ();
11072 exit (mono_environment_exitcode_get ());
11077 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
11079 MonoJitTlsData *jit_tls;
11082 jit_tls = TlsGetValue (mono_jit_tls_id);
11086 jit_tls = g_new0 (MonoJitTlsData, 1);
11088 TlsSetValue (mono_jit_tls_id, jit_tls);
11090 #ifdef HAVE_KW_THREAD
11091 mono_jit_tls = jit_tls;
11094 jit_tls->abort_func = abort_func;
11095 jit_tls->end_of_stack = stack_start;
11097 lmf = g_new0 (MonoLMF, 1);
11098 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
11099 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf);
11104 jit_tls->first_lmf = lmf;
11106 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11107 /* jit_tls->lmf is unused */
11109 mono_lmf_addr = &mono_lmf;
11111 #if defined(HAVE_KW_THREAD)
11112 mono_lmf_addr = &jit_tls->lmf;
11115 jit_tls->lmf = lmf;
11118 mono_arch_setup_jit_tls_data (jit_tls);
11119 mono_setup_altstack (jit_tls);
11125 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
11127 MonoThread *thread;
11128 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
11129 thread = mono_thread_current ();
11130 mono_debugger_thread_created (tid, thread, jit_tls);
11132 thread->jit_data = jit_tls;
11135 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
11138 mono_thread_abort_dummy (MonoObject *obj)
11140 if (mono_thread_attach_aborted_cb)
11141 mono_thread_attach_aborted_cb (obj);
11143 mono_thread_abort (obj);
11147 mono_thread_attach_cb (gsize tid, gpointer stack_start)
11149 MonoThread *thread;
11150 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
11151 thread = mono_thread_current ();
11152 mono_debugger_thread_created (tid, thread, (MonoJitTlsData *) jit_tls);
11154 thread->jit_data = jit_tls;
11155 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
11156 setup_stat_profiler ();
11160 mini_thread_cleanup (MonoThread *thread)
11162 MonoJitTlsData *jit_tls = thread->jit_data;
11165 mono_debugger_thread_cleanup (jit_tls);
11166 mono_arch_free_jit_tls_data (jit_tls);
11168 mono_free_altstack (jit_tls);
11169 g_free (jit_tls->first_lmf);
11171 thread->jit_data = NULL;
11172 TlsSetValue (mono_jit_tls_id, NULL);
11177 mono_create_tls_get (MonoCompile *cfg, int offset)
11179 #ifdef MONO_ARCH_HAVE_TLS_GET
11185 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
11186 ins->dreg = cfg->new_ir ? mono_alloc_preg (cfg) : mono_regstate_next_int (cfg->rs);
11187 ins->inst_offset = offset;
11195 mono_get_jit_tls_intrinsic (MonoCompile *cfg)
11197 return mono_create_tls_get (cfg, mono_get_jit_tls_offset ());
11201 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
11203 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
11207 ji->data.target = target;
11208 ji->next = cfg->patch_info;
11210 cfg->patch_info = ji;
11214 mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target)
11216 MonoJumpInfo *ji = g_new0 (MonoJumpInfo, 1);
11220 ji->data.target = target;
11227 mono_remove_patch_info (MonoCompile *cfg, int ip)
11229 MonoJumpInfo **ji = &cfg->patch_info;
11232 if ((*ji)->ip.i == ip)
11235 ji = &((*ji)->next);
11240 * mono_patch_info_dup_mp:
11242 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
11245 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
11247 MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
11248 memcpy (res, patch_info, sizeof (MonoJumpInfo));
11250 switch (patch_info->type) {
11251 case MONO_PATCH_INFO_RVA:
11252 case MONO_PATCH_INFO_LDSTR:
11253 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11254 case MONO_PATCH_INFO_LDTOKEN:
11255 case MONO_PATCH_INFO_DECLSEC:
11256 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
11257 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
11259 case MONO_PATCH_INFO_SWITCH:
11260 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
11261 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
11263 case MONO_PATCH_INFO_RGCTX_FETCH:
11264 res->data.rgctx_entry = mono_mempool_alloc (mp, sizeof (MonoJumpInfoRgctxEntry));
11265 memcpy (res->data.rgctx_entry, patch_info->data.rgctx_entry, sizeof (MonoJumpInfoRgctxEntry));
11266 res->data.rgctx_entry->data = mono_patch_info_dup_mp (mp, res->data.rgctx_entry->data);
11276 mono_patch_info_hash (gconstpointer data)
11278 const MonoJumpInfo *ji = (MonoJumpInfo*)data;
11280 switch (ji->type) {
11281 case MONO_PATCH_INFO_RVA:
11282 case MONO_PATCH_INFO_LDSTR:
11283 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11284 case MONO_PATCH_INFO_LDTOKEN:
11285 case MONO_PATCH_INFO_DECLSEC:
11286 return (ji->type << 8) | ji->data.token->token;
11287 case MONO_PATCH_INFO_VTABLE:
11288 case MONO_PATCH_INFO_CLASS:
11289 case MONO_PATCH_INFO_IID:
11290 case MONO_PATCH_INFO_ADJUSTED_IID:
11291 case MONO_PATCH_INFO_CLASS_INIT:
11292 case MONO_PATCH_INFO_METHODCONST:
11293 case MONO_PATCH_INFO_METHOD:
11294 case MONO_PATCH_INFO_METHOD_JUMP:
11295 case MONO_PATCH_INFO_IMAGE:
11296 case MONO_PATCH_INFO_INTERNAL_METHOD:
11297 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
11298 case MONO_PATCH_INFO_FIELD:
11299 case MONO_PATCH_INFO_SFLDA:
11300 return (ji->type << 8) | (gssize)ji->data.target;
11302 return (ji->type << 8);
11307 * mono_patch_info_equal:
11309 * This might fail to recognize equivalent patches, i.e. floats, so its only
11310 * usable in those cases where this is not a problem, i.e. sharing GOT slots
11314 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
11316 const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
11317 const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
11319 if (ji1->type != ji2->type)
11322 switch (ji1->type) {
11323 case MONO_PATCH_INFO_RVA:
11324 case MONO_PATCH_INFO_LDSTR:
11325 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11326 case MONO_PATCH_INFO_LDTOKEN:
11327 case MONO_PATCH_INFO_DECLSEC:
11328 if ((ji1->data.token->image != ji2->data.token->image) ||
11329 (ji1->data.token->token != ji2->data.token->token) ||
11330 (ji1->data.token->has_context != ji2->data.token->has_context) ||
11331 (ji1->data.token->context.class_inst != ji2->data.token->context.class_inst) ||
11332 (ji1->data.token->context.method_inst != ji2->data.token->context.method_inst))
11336 if (ji1->data.target != ji2->data.target)
11345 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
11347 unsigned char *ip = patch_info->ip.i + code;
11348 gconstpointer target = NULL;
11350 switch (patch_info->type) {
11351 case MONO_PATCH_INFO_BB:
11352 g_assert (patch_info->data.bb->native_offset);
11353 target = patch_info->data.bb->native_offset + code;
11355 case MONO_PATCH_INFO_ABS:
11356 target = patch_info->data.target;
11358 case MONO_PATCH_INFO_LABEL:
11359 target = patch_info->data.inst->inst_c0 + code;
11361 case MONO_PATCH_INFO_IP:
11364 case MONO_PATCH_INFO_METHOD_REL:
11365 target = code + patch_info->data.offset;
11367 case MONO_PATCH_INFO_INTERNAL_METHOD: {
11368 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11370 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
11371 g_assert_not_reached ();
11373 target = mono_icall_get_wrapper (mi);
11376 case MONO_PATCH_INFO_METHOD_JUMP:
11377 target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
11379 case MONO_PATCH_INFO_METHOD:
11380 if (patch_info->data.method == method) {
11383 /* get the trampoline to the method from the domain */
11384 if (method && method->wrapper_type == MONO_WRAPPER_STATIC_RGCTX_INVOKE) {
11385 target = mono_create_jit_trampoline_in_domain (mono_domain_get (),
11386 patch_info->data.method);
11388 target = mono_create_jit_trampoline (patch_info->data.method);
11392 case MONO_PATCH_INFO_SWITCH: {
11393 gpointer *jump_table;
11396 if (method && method->dynamic) {
11397 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11399 mono_domain_lock (domain);
11401 jump_table = mono_domain_alloc (domain, sizeof (gpointer) * patch_info->data.table->table_size);
11403 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11404 mono_domain_unlock (domain);
11407 for (i = 0; i < patch_info->data.table->table_size; i++)
11408 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
11409 target = jump_table;
11412 case MONO_PATCH_INFO_METHODCONST:
11413 case MONO_PATCH_INFO_CLASS:
11414 case MONO_PATCH_INFO_IMAGE:
11415 case MONO_PATCH_INFO_FIELD:
11416 target = patch_info->data.target;
11418 case MONO_PATCH_INFO_IID:
11419 mono_class_init (patch_info->data.klass);
11420 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
11422 case MONO_PATCH_INFO_ADJUSTED_IID:
11423 mono_class_init (patch_info->data.klass);
11424 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
11426 case MONO_PATCH_INFO_VTABLE:
11427 target = mono_class_vtable (domain, patch_info->data.klass);
11430 case MONO_PATCH_INFO_CLASS_INIT: {
11431 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.klass);
11434 target = mono_create_class_init_trampoline (vtable);
11437 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
11438 target = mono_create_delegate_trampoline (patch_info->data.klass);
11440 case MONO_PATCH_INFO_SFLDA: {
11441 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
11444 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
11445 /* Done by the generated code */
11449 mono_runtime_class_init (vtable);
11451 target = (char*)vtable->data + patch_info->data.field->offset;
11454 case MONO_PATCH_INFO_RVA:
11455 target = mono_image_rva_map (patch_info->data.token->image, patch_info->data.token->token);
11457 case MONO_PATCH_INFO_R4:
11458 case MONO_PATCH_INFO_R8:
11459 target = patch_info->data.target;
11461 case MONO_PATCH_INFO_EXC_NAME:
11462 target = patch_info->data.name;
11464 case MONO_PATCH_INFO_LDSTR:
11466 mono_ldstr (domain, patch_info->data.token->image,
11467 mono_metadata_token_index (patch_info->data.token->token));
11469 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
11471 MonoClass *handle_class;
11473 handle = mono_ldtoken (patch_info->data.token->image,
11474 patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL);
11475 mono_class_init (handle_class);
11476 mono_class_init (mono_class_from_mono_type (handle));
11479 mono_type_get_object (domain, handle);
11482 case MONO_PATCH_INFO_LDTOKEN: {
11484 MonoClass *handle_class;
11486 handle = mono_ldtoken (patch_info->data.token->image,
11487 patch_info->data.token->token, &handle_class, NULL);
11488 mono_class_init (handle_class);
11493 case MONO_PATCH_INFO_DECLSEC:
11494 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
11496 case MONO_PATCH_INFO_ICALL_ADDR:
11497 target = mono_lookup_internal_call (patch_info->data.method);
11498 /* run_cctors == 0 -> AOT */
11499 if (!target && run_cctors)
11500 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info->data.method, TRUE));
11502 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
11503 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11505 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info->data.name);
11506 g_assert_not_reached ();
11511 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
11512 target = mono_thread_interruption_request_flag ();
11514 case MONO_PATCH_INFO_METHOD_RGCTX:
11515 target = mono_method_lookup_rgctx (mono_class_vtable (domain, patch_info->data.method->klass), mini_method_get_context (patch_info->data.method)->method_inst);
11517 case MONO_PATCH_INFO_BB_OVF:
11518 case MONO_PATCH_INFO_EXC_OVF:
11519 case MONO_PATCH_INFO_GOT_OFFSET:
11520 case MONO_PATCH_INFO_NONE:
11522 case MONO_PATCH_INFO_RGCTX_FETCH: {
11523 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
11526 switch (entry->data->type) {
11527 case MONO_PATCH_INFO_CLASS:
11528 slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, &entry->data->data.klass->byval_arg, entry->info_type, mono_method_get_context (entry->method));
11530 case MONO_PATCH_INFO_METHOD:
11531 case MONO_PATCH_INFO_METHODCONST:
11532 slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, entry->data->data.method, entry->info_type, mono_method_get_context (entry->method));
11534 case MONO_PATCH_INFO_FIELD:
11535 slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, entry->data->data.field, entry->info_type, mono_method_get_context (entry->method));
11538 g_assert_not_reached ();
11542 target = mono_create_rgctx_lazy_fetch_trampoline (slot);
11545 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
11546 target = mono_create_generic_class_init_trampoline ();
11549 g_assert_not_reached ();
11552 return (gpointer)target;
11556 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
11557 MonoJitICallInfo *info;
11559 decompose_foreach (tree, cfg);
11561 switch (mono_burg_arity [tree->opcode]) {
11564 dec_foreach (tree->inst_left, cfg);
11566 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11567 MonoInst *iargs [2];
11569 iargs [0] = tree->inst_left;
11571 mono_emulate_opcode (cfg, tree, iargs, info);
11577 #ifdef MONO_ARCH_BIGMUL_INTRINS
11578 if (tree->opcode == OP_LMUL
11579 && (cfg->opt & MONO_OPT_INTRINS)
11580 && (tree->inst_left->opcode == CEE_CONV_I8
11581 || tree->inst_left->opcode == CEE_CONV_U8)
11582 && tree->inst_left->inst_left->type == STACK_I4
11583 && (tree->inst_right->opcode == CEE_CONV_I8
11584 || tree->inst_right->opcode == CEE_CONV_U8)
11585 && tree->inst_right->inst_left->type == STACK_I4
11586 && tree->inst_left->opcode == tree->inst_right->opcode) {
11587 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
11588 tree->inst_left = tree->inst_left->inst_left;
11589 tree->inst_right = tree->inst_right->inst_left;
11590 dec_foreach (tree, cfg);
11593 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11594 MonoInst *iargs [2];
11596 iargs [0] = tree->inst_i0;
11597 iargs [1] = tree->inst_i1;
11599 mono_emulate_opcode (cfg, tree, iargs, info);
11601 dec_foreach (iargs [0], cfg);
11602 dec_foreach (iargs [1], cfg);
11605 dec_foreach (tree->inst_left, cfg);
11606 dec_foreach (tree->inst_right, cfg);
11610 g_assert_not_reached ();
11615 decompose_pass (MonoCompile *cfg) {
11616 MonoBasicBlock *bb;
11618 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11621 cfg->prev_ins = NULL;
11622 MONO_BB_FOR_EACH_INS (cfg->cbb, tree) {
11623 dec_foreach (tree, cfg);
11624 cfg->prev_ins = tree;
11630 mono_compile_create_vars (MonoCompile *cfg)
11632 MonoMethodSignature *sig;
11633 MonoMethodHeader *header;
11636 header = mono_method_get_header (cfg->method);
11638 sig = mono_method_signature (cfg->method);
11640 if (!MONO_TYPE_IS_VOID (sig->ret)) {
11642 cfg->ret = mono_compile_create_var (cfg, sig->ret, OP_ARG);
11643 /* Inhibit optimizations */
11644 cfg->ret->flags |= MONO_INST_VOLATILE;
11646 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
11647 cfg->ret->opcode = OP_RETARG;
11648 cfg->ret->inst_vtype = sig->ret;
11649 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
11652 if (cfg->verbose_level > 2)
11653 g_print ("creating vars\n");
11655 cfg->args = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + sig->hasthis) * sizeof (MonoInst*));
11658 cfg->args [0] = mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
11660 for (i = 0; i < sig->param_count; ++i) {
11661 cfg->args [i + sig->hasthis] = mono_compile_create_var (cfg, sig->params [i], OP_ARG);
11662 if (sig->params [i]->byref) {
11663 if (!cfg->new_ir) cfg->disable_ssa = TRUE;
11667 if (cfg->new_ir && cfg->verbose_level > 2) {
11669 printf ("\treturn : ");
11670 mono_print_ins (cfg->ret);
11673 if (sig->hasthis) {
11674 printf ("\tthis: ");
11675 mono_print_ins (cfg->args [0]);
11678 for (i = 0; i < sig->param_count; ++i) {
11679 printf ("\targ [%d]: ", i);
11680 mono_print_ins (cfg->args [i + sig->hasthis]);
11684 cfg->locals_start = cfg->num_varinfo;
11685 cfg->locals = mono_mempool_alloc0 (cfg->mempool, header->num_locals * sizeof (MonoInst*));
11687 if (cfg->verbose_level > 2)
11688 g_print ("creating locals\n");
11690 for (i = 0; i < header->num_locals; ++i)
11691 cfg->locals [i] = mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
11693 if (cfg->verbose_level > 2)
11694 g_print ("locals done\n");
11696 mono_arch_create_vars (cfg);
11700 mono_print_code (MonoCompile *cfg, const char* msg)
11702 MonoBasicBlock *bb;
11704 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11705 MonoInst *tree = bb->code;
11708 mono_print_bb (bb, msg);
11713 g_print ("%s CODE BLOCK %d (nesting %d):\n", msg, bb->block_num, bb->nesting);
11715 MONO_BB_FOR_EACH_INS (bb, tree) {
11716 mono_print_tree (tree);
11723 #ifndef DISABLE_JIT
11725 extern const char * const mono_burg_rule_string [];
11728 emit_state (MonoCompile *cfg, MBState *state, int goal)
11730 MBState *kids [10];
11731 int ern = mono_burg_rule (state, goal);
11732 const guint16 *nts = mono_burg_nts_data + mono_burg_nts [ern];
11734 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
11738 // state->reg1 = state->reg2; /* chain rule */
11740 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11743 state->reg1 = mono_regstate_next_int (cfg->rs);
11744 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
11746 case MB_NTERM_lreg:
11747 state->reg1 = mono_regstate_next_int (cfg->rs);
11748 state->reg2 = mono_regstate_next_int (cfg->rs);
11750 case MB_NTERM_freg:
11751 #ifdef MONO_ARCH_SOFT_FLOAT
11752 state->reg1 = mono_regstate_next_int (cfg->rs);
11753 state->reg2 = mono_regstate_next_int (cfg->rs);
11755 state->reg1 = mono_regstate_next_float (cfg->rs);
11759 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11761 * Enabling this might cause bugs to surface in the local register
11762 * allocators on some architectures like x86.
11764 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
11765 /* Do not optimize away reg-reg moves */
11766 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
11767 state->right->reg1 = state->left->tree->dreg;
11776 mono_burg_kids (state, ern, kids);
11778 emit_state (cfg, kids [0], nts [0]);
11780 emit_state (cfg, kids [1], nts [1]);
11782 emit_state (cfg, kids [2], nts [2]);
11784 g_assert (!nts [4]);
11785 emit_state (cfg, kids [3], nts [3]);
11791 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
11792 mono_burg_emit (ern, state, state->tree, cfg);
11795 #define DEBUG_SELECTION
11798 mini_select_instructions (MonoCompile *cfg)
11800 MonoBasicBlock *bb;
11802 cfg->state_pool = mono_mempool_new ();
11803 cfg->rs = mono_regstate_new ();
11805 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11806 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
11807 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
11809 /* we are careful when inverting, since bugs like #59580
11810 * could show up when dealing with NaNs.
11812 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
11813 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
11814 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
11815 bb->last_ins->inst_false_bb = tmp;
11817 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
11821 MONO_INST_NEW (cfg, ins, OP_BR);
11822 ins->inst_target_bb = bb->last_ins->inst_false_bb;
11823 MONO_ADD_INS (bb, ins);
11828 #ifdef DEBUG_SELECTION
11829 if (cfg->verbose_level >= 4) {
11830 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11832 g_print ("DUMP BLOCK %d:\n", bb->block_num);
11833 MONO_BB_FOR_EACH_INS (bb, tree) {
11834 mono_print_tree (tree);
11841 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11842 MonoInst *tree = bb->code, *next;
11848 bb->last_ins = NULL;
11851 mono_regstate_reset (cfg->rs);
11853 #ifdef DEBUG_SELECTION
11854 if (cfg->verbose_level >= 3)
11855 g_print ("LABEL BLOCK %d:\n", bb->block_num);
11857 for (; tree; tree = next) {
11859 #ifdef DEBUG_SELECTION
11860 if (cfg->verbose_level >= 3) {
11861 mono_print_tree (tree);
11866 cfg->ip = tree->cil_code;
11867 if (!(mbstate = mono_burg_label (tree, cfg))) {
11868 g_warning ("unable to label tree %p", tree);
11869 mono_print_tree (tree);
11871 g_assert_not_reached ();
11873 emit_state (cfg, mbstate, MB_NTERM_stmt);
11875 bb->max_vreg = cfg->rs->next_vreg;
11878 bb->last_ins->next = NULL;
11880 mono_mempool_empty (cfg->state_pool);
11882 mono_mempool_destroy (cfg->state_pool);
11888 * mono_normalize_opcodes:
11890 * Replace CEE_ and OP_ opcodes with the corresponding OP_I or OP_L opcodes.
11893 static gint16 *remap_table;
11895 #if SIZEOF_VOID_P == 8
11896 #define REMAP_OPCODE(opcode) OP_L ## opcode
11898 #define REMAP_OPCODE(opcode) OP_I ## opcode
11901 static G_GNUC_UNUSED void
11902 mono_normalize_opcodes (MonoCompile *cfg, MonoBasicBlock *bb)
11906 if (!remap_table) {
11907 remap_table = g_new0 (gint16, OP_LAST);
11909 #if SIZEOF_VOID_P == 8
11910 remap_table [CEE_CONV_U8] = OP_ZEXT_I4;
11911 remap_table [CEE_CONV_U] = OP_ZEXT_I4;
11912 remap_table [CEE_CONV_I8] = OP_SEXT_I4;
11913 remap_table [CEE_CONV_I] = OP_SEXT_I4;
11914 remap_table [CEE_CONV_OVF_U4] = OP_LCONV_TO_OVF_U4;
11915 remap_table [CEE_CONV_OVF_I4_UN] = OP_LCONV_TO_OVF_I4_UN;
11918 remap_table [CEE_CONV_R4] = OP_ICONV_TO_R4;
11919 remap_table [CEE_CONV_R8] = OP_ICONV_TO_R8;
11920 remap_table [CEE_CONV_I4] = OP_MOVE;
11921 remap_table [CEE_CONV_U4] = OP_MOVE;
11922 remap_table [CEE_CONV_I1] = REMAP_OPCODE (CONV_TO_I1);
11923 remap_table [CEE_CONV_I2] = REMAP_OPCODE (CONV_TO_I2);
11924 remap_table [CEE_CONV_U1] = REMAP_OPCODE (CONV_TO_U1);
11925 remap_table [CEE_CONV_U2] = REMAP_OPCODE (CONV_TO_U2);
11926 remap_table [CEE_CONV_R_UN] = REMAP_OPCODE (CONV_TO_R_UN);
11927 remap_table [CEE_ADD] = REMAP_OPCODE (ADD);
11928 remap_table [CEE_SUB] = REMAP_OPCODE (SUB);
11929 remap_table [CEE_MUL] = REMAP_OPCODE (MUL);
11930 remap_table [CEE_DIV] = REMAP_OPCODE (DIV);
11931 remap_table [CEE_REM] = REMAP_OPCODE (REM);
11932 remap_table [CEE_DIV_UN] = REMAP_OPCODE (DIV_UN);
11933 remap_table [CEE_REM_UN] = REMAP_OPCODE (REM_UN);
11934 remap_table [CEE_AND] = REMAP_OPCODE (AND);
11935 remap_table [CEE_OR] = REMAP_OPCODE (OR);
11936 remap_table [CEE_XOR] = REMAP_OPCODE (XOR);
11937 remap_table [CEE_SHL] = REMAP_OPCODE (SHL);
11938 remap_table [CEE_SHR] = REMAP_OPCODE (SHR);
11939 remap_table [CEE_SHR_UN] = REMAP_OPCODE (SHR_UN);
11940 remap_table [CEE_NOT] = REMAP_OPCODE (NOT);
11941 remap_table [CEE_NEG] = REMAP_OPCODE (NEG);
11942 remap_table [CEE_CALL] = OP_CALL;
11943 remap_table [CEE_BEQ] = REMAP_OPCODE (BEQ);
11944 remap_table [CEE_BNE_UN] = REMAP_OPCODE (BNE_UN);
11945 remap_table [CEE_BLT] = REMAP_OPCODE (BLT);
11946 remap_table [CEE_BLT_UN] = REMAP_OPCODE (BLT_UN);
11947 remap_table [CEE_BGT] = REMAP_OPCODE (BGT);
11948 remap_table [CEE_BGT_UN] = REMAP_OPCODE (BGT_UN);
11949 remap_table [CEE_BGE] = REMAP_OPCODE (BGE);
11950 remap_table [CEE_BGE_UN] = REMAP_OPCODE (BGE_UN);
11951 remap_table [CEE_BLE] = REMAP_OPCODE (BLE);
11952 remap_table [CEE_BLE_UN] = REMAP_OPCODE (BLE_UN);
11953 remap_table [CEE_ADD_OVF] = REMAP_OPCODE (ADD_OVF);
11954 remap_table [CEE_ADD_OVF_UN] = REMAP_OPCODE (ADD_OVF_UN);
11955 remap_table [CEE_SUB_OVF] = REMAP_OPCODE (SUB_OVF);
11956 remap_table [CEE_SUB_OVF_UN] = REMAP_OPCODE (SUB_OVF_UN);
11957 remap_table [CEE_MUL_OVF] = REMAP_OPCODE (MUL_OVF);
11958 remap_table [CEE_MUL_OVF_UN] = REMAP_OPCODE (MUL_OVF_UN);
11961 MONO_BB_FOR_EACH_INS (bb, ins) {
11962 int remapped = remap_table [ins->opcode];
11964 ins->opcode = remapped;
11969 mono_codegen (MonoCompile *cfg)
11971 MonoJumpInfo *patch_info;
11972 MonoBasicBlock *bb;
11973 int i, max_epilog_size;
11976 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11977 cfg->spill_count = 0;
11978 /* we reuse dfn here */
11979 /* bb->dfn = bb_count++; */
11980 #ifdef MONO_ARCH_ENABLE_NORMALIZE_OPCODES
11982 mono_normalize_opcodes (cfg, bb);
11985 mono_arch_lowering_pass (cfg, bb);
11987 if (cfg->opt & MONO_OPT_PEEPHOLE)
11988 mono_arch_peephole_pass_1 (cfg, bb);
11990 if (!cfg->globalra)
11991 mono_local_regalloc (cfg, bb);
11993 if (cfg->opt & MONO_OPT_PEEPHOLE)
11994 mono_arch_peephole_pass_2 (cfg, bb);
11997 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
11998 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
12000 code = mono_arch_emit_prolog (cfg);
12002 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
12003 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
12005 cfg->code_len = code - cfg->native_code;
12006 cfg->prolog_end = cfg->code_len;
12008 mono_debug_open_method (cfg);
12010 /* emit code all basic blocks */
12011 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12012 bb->native_offset = cfg->code_len;
12013 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
12014 mono_arch_output_basic_block (cfg, bb);
12016 if (bb == cfg->bb_exit) {
12017 cfg->epilog_begin = cfg->code_len;
12019 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
12020 code = cfg->native_code + cfg->code_len;
12021 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
12022 cfg->code_len = code - cfg->native_code;
12023 g_assert (cfg->code_len < cfg->code_size);
12026 mono_arch_emit_epilog (cfg);
12030 mono_arch_emit_exceptions (cfg);
12032 max_epilog_size = 0;
12034 code = cfg->native_code + cfg->code_len;
12036 /* we always allocate code in cfg->domain->code_mp to increase locality */
12037 cfg->code_size = cfg->code_len + max_epilog_size;
12038 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
12040 if (cfg->method->dynamic) {
12041 guint unwindlen = 0;
12042 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12043 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12045 /* Allocate the code into a separate memory pool so it can be freed */
12046 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
12047 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
12048 mono_domain_lock (cfg->domain);
12049 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
12050 mono_domain_unlock (cfg->domain);
12052 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size + unwindlen);
12054 guint unwindlen = 0;
12055 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12056 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12058 mono_domain_lock (cfg->domain);
12059 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size + unwindlen);
12060 mono_domain_unlock (cfg->domain);
12063 memcpy (code, cfg->native_code, cfg->code_len);
12064 g_free (cfg->native_code);
12065 cfg->native_code = code;
12066 code = cfg->native_code + cfg->code_len;
12068 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
12069 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
12070 switch (patch_info->type) {
12071 case MONO_PATCH_INFO_ABS: {
12072 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
12075 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
12076 * absolute address.
12079 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
12080 // FIXME: CLEAN UP THIS MESS.
12081 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
12082 strstr (cfg->method->name, info->name)) {
12084 * This is an icall wrapper, and this is a call to the
12085 * wrapped function.
12087 if (cfg->compile_aot) {
12088 patch_info->type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
12089 patch_info->data.name = info->name;
12092 /* for these array methods we currently register the same function pointer
12093 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
12094 * will return the incorrect one depending on the order they are registered.
12095 * See tests/test-arr.cs
12097 if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
12098 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
12099 patch_info->data.name = info->name;
12104 if (patch_info->type == MONO_PATCH_INFO_ABS && !cfg->new_ir) {
12105 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
12107 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
12108 patch_info->data.klass = vtable->klass;
12112 if (patch_info->type == MONO_PATCH_INFO_ABS) {
12113 MonoClass *klass = mono_find_delegate_trampoline_by_addr (patch_info->data.target);
12115 patch_info->type = MONO_PATCH_INFO_DELEGATE_TRAMPOLINE;
12116 patch_info->data.klass = klass;
12120 if (patch_info->type == MONO_PATCH_INFO_ABS) {
12121 if (cfg->abs_patches) {
12122 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, patch_info->data.target);
12124 patch_info->type = abs_ji->type;
12125 patch_info->data.target = abs_ji->data.target;
12132 case MONO_PATCH_INFO_SWITCH: {
12134 if (cfg->method->dynamic) {
12135 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12137 mono_domain_lock (cfg->domain);
12138 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12139 mono_domain_unlock (cfg->domain);
12142 if (!cfg->compile_aot && !cfg->new_ir)
12143 /* In the aot case, the patch already points to the correct location */
12144 patch_info->ip.i = patch_info->ip.label->inst_c0;
12145 for (i = 0; i < patch_info->data.table->table_size; i++) {
12146 /* Might be NULL if the switch is eliminated */
12147 if (patch_info->data.table->table [i]) {
12148 g_assert (patch_info->data.table->table [i]->native_offset);
12149 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
12154 patch_info->data.table->table = (MonoBasicBlock**)table;
12157 case MONO_PATCH_INFO_METHOD_JUMP: {
12159 MonoDomain *domain = cfg->domain;
12160 unsigned char *ip = cfg->native_code + patch_info->ip.i;
12162 mono_domain_lock (domain);
12163 if (!domain_jit_info (domain)->jump_target_hash)
12164 domain_jit_info (domain)->jump_target_hash = g_hash_table_new (NULL, NULL);
12165 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_hash, patch_info->data.method);
12166 list = g_slist_prepend (list, ip);
12167 g_hash_table_insert (domain_jit_info (domain)->jump_target_hash, patch_info->data.method, list);
12168 mono_domain_unlock (domain);
12177 #ifdef VALGRIND_JIT_REGISTER_MAP
12178 if (valgrind_register){
12179 char* nm = mono_method_full_name (cfg->method, TRUE);
12180 VALGRIND_JIT_REGISTER_MAP (nm, cfg->native_code, cfg->native_code + cfg->code_len);
12185 if (cfg->verbose_level > 0) {
12186 char* nm = mono_method_full_name (cfg->method, TRUE);
12187 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
12189 cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
12194 gboolean is_generic = FALSE;
12196 if (cfg->method->is_inflated || mono_method_get_generic_container (cfg->method) ||
12197 cfg->method->klass->generic_container || cfg->method->klass->generic_class) {
12201 if (cfg->generic_sharing_context)
12202 g_assert (is_generic);
12205 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
12206 mono_arch_save_unwind_info (cfg);
12209 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
12211 if (cfg->method->dynamic) {
12212 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12214 mono_domain_lock (cfg->domain);
12215 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12216 mono_domain_unlock (cfg->domain);
12219 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
12221 mono_debug_close_method (cfg);
12222 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12223 mono_arch_unwindinfo_install_unwind_info (&cfg->arch.unwindinfo, cfg->native_code, cfg->code_len);
12227 static MonoGenericInst*
12228 get_object_generic_inst (int type_argc)
12230 MonoType **type_argv;
12233 type_argv = alloca (sizeof (MonoType*) * type_argc);
12235 for (i = 0; i < type_argc; ++i)
12236 type_argv [i] = &mono_defaults.object_class->byval_arg;
12238 return mono_metadata_get_generic_inst (type_argc, type_argv);
12242 * mini_method_compile:
12243 * @method: the method to compile
12244 * @opts: the optimization flags to use
12245 * @domain: the domain where the method will be compiled in
12246 * @run_cctors: whether we should run type ctors if possible
12247 * @compile_aot: whether this is an AOT compilation
12248 * @parts: debug flag
12250 * Returns: a MonoCompile* pointer. Caller must check the exception_type
12251 * field in the returned struct to see if compilation succeded.
12254 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
12256 MonoMethodHeader *header;
12259 MonoJitInfo *jinfo;
12260 int dfn, i, code_size_ratio;
12261 gboolean deadce_has_run = FALSE;
12262 gboolean try_generic_shared;
12263 MonoMethod *method_to_compile, *method_to_register;
12264 int generic_info_size;
12266 mono_jit_stats.methods_compiled++;
12267 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
12268 mono_profiler_method_jit (method);
12269 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
12270 MONO_PROBE_METHOD_COMPILE_BEGIN (method);
12273 /* We are passed the original generic method definition */
12274 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12275 (opts & MONO_OPT_GSHARED) && (method->is_generic || method->klass->generic_container);
12277 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12278 (opts & MONO_OPT_GSHARED) && mono_method_is_generic_sharable_impl (method, FALSE);
12280 if (opts & MONO_OPT_GSHARED) {
12281 if (try_generic_shared)
12282 mono_stats.generics_sharable_methods++;
12283 else if (mono_method_is_generic_impl (method))
12284 mono_stats.generics_unsharable_methods++;
12288 if (try_generic_shared) {
12289 MonoMethod *declaring_method;
12290 MonoGenericContext *shared_context;
12293 declaring_method = method;
12295 declaring_method = mono_method_get_declaring_generic_method (method);
12296 if (method->klass->generic_class)
12297 g_assert (method->klass->generic_class->container_class == declaring_method->klass);
12299 g_assert (method->klass == declaring_method->klass);
12302 if (declaring_method->is_generic)
12303 shared_context = &(mono_method_get_generic_container (declaring_method)->context);
12305 shared_context = &declaring_method->klass->generic_container->context;
12307 method_to_compile = mono_class_inflate_generic_method (declaring_method, shared_context);
12308 g_assert (method_to_compile);
12310 method_to_compile = method;
12313 cfg = g_new0 (MonoCompile, 1);
12314 cfg->method = method_to_compile;
12315 cfg->mempool = mono_mempool_new ();
12317 cfg->prof_options = mono_profiler_get_events ();
12318 cfg->run_cctors = run_cctors;
12319 cfg->domain = domain;
12320 cfg->verbose_level = mini_verbose;
12321 cfg->compile_aot = compile_aot;
12322 cfg->skip_visibility = method->skip_visibility;
12323 cfg->orig_method = method;
12324 if (try_generic_shared)
12325 cfg->generic_sharing_context = (MonoGenericSharingContext*)&cfg->generic_sharing_context;
12326 cfg->token_info_hash = g_hash_table_new (NULL, NULL);
12328 if (cfg->compile_aot && !try_generic_shared && (method->is_generic || method->klass->generic_container)) {
12329 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED;
12333 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
12334 if (mono_debug_using_mono_debugger () || debug_options.mdb_optimizations) {
12335 cfg->disable_reuse_registers = TRUE;
12336 cfg->disable_reuse_stack_slots = TRUE;
12338 * This decreases the change the debugger will read registers/stack slots which are
12339 * not yet initialized.
12341 cfg->disable_initlocals_opt = TRUE;
12343 /* Temporarily disable this when running in the debugger until we have support
12344 * for this in the debugger. */
12345 cfg->disable_omit_fp = TRUE;
12347 /* The debugger needs all locals to be on the stack or in a global register */
12348 cfg->disable_vreg_to_lvreg = TRUE;
12350 // cfg->opt |= MONO_OPT_SHARED;
12351 cfg->opt &= ~MONO_OPT_INLINE;
12352 cfg->opt &= ~MONO_OPT_COPYPROP;
12353 cfg->opt &= ~MONO_OPT_CONSPROP;
12354 cfg->opt &= ~MONO_OPT_GSHARED;
12357 header = mono_method_get_header (method_to_compile);
12359 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
12360 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
12361 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12362 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12363 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12364 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12368 if (getenv ("MONO_VERBOSE_METHOD")) {
12369 if (strcmp (cfg->method->name, getenv ("MONO_VERBOSE_METHOD")) == 0)
12370 cfg->verbose_level = 4;
12373 ip = (guint8 *)header->code;
12375 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
12377 if (cfg->verbose_level > 2) {
12378 if (cfg->generic_sharing_context)
12379 g_print ("converting shared method %s\n", mono_method_full_name (method, TRUE));
12381 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
12384 if (cfg->opt & (MONO_OPT_ABCREM | MONO_OPT_SSAPRE))
12385 cfg->opt |= MONO_OPT_SSA;
12388 static int count = 0;
12392 if (getenv ("MONO_COUNT")) {
12393 if (count == atoi (getenv ("MONO_COUNT"))) {
12394 printf ("LAST: %s\n", mono_method_full_name (method, TRUE));
12395 //cfg->verbose_level = 5;
12397 if (count <= atoi (getenv ("MONO_COUNT")))
12398 cfg->new_ir = TRUE;
12401 * Passing/returning vtypes in registers in managed methods is an ABI change
12402 * from the old JIT.
12404 disable_vtypes_in_regs = TRUE;
12407 cfg->new_ir = TRUE;
12411 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")))
12412 cfg->globalra = TRUE;
12415 //cfg->globalra = TRUE;
12417 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
12418 // cfg->globalra = TRUE;
12421 static int count = 0;
12424 if (getenv ("COUNT2")) {
12425 if (count == atoi (getenv ("COUNT2")))
12426 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
12427 if (count > atoi (getenv ("COUNT2")))
12428 cfg->globalra = FALSE;
12432 if (header->clauses)
12433 cfg->globalra = FALSE;
12435 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
12436 /* The code in the prolog clobbers caller saved registers */
12437 cfg->globalra = FALSE;
12439 // FIXME: Disable globalra in case of tracing/profiling
12441 if (cfg->method->save_lmf)
12442 /* The LMF saving code might clobber caller saved registers */
12443 cfg->globalra = FALSE;
12446 if (!strcmp (cfg->method->name, "CompareInternal"))
12447 cfg->globalra = FALSE;
12450 if (strstr (cfg->method->name, "LoadData"))
12451 cfg->new_ir = FALSE;
12455 cfg->rs = mono_regstate_new ();
12456 cfg->next_vreg = cfg->rs->next_vreg;
12459 /* FIXME: Fix SSA to handle branches inside bblocks */
12460 if (cfg->opt & MONO_OPT_SSA)
12461 cfg->enable_extended_bblocks = FALSE;
12464 * FIXME: This confuses liveness analysis because variables which are assigned after
12465 * a branch inside a bblock become part of the kill set, even though the assignment
12466 * might not get executed. This causes the optimize_initlocals pass to delete some
12467 * assignments which are needed.
12468 * Also, the mono_if_conversion pass needs to be modified to recognize the code
12471 //cfg->enable_extended_bblocks = TRUE;
12474 * create MonoInst* which represents arguments and local variables
12476 mono_compile_create_vars (cfg);
12479 /* SSAPRE is not supported on linear IR */
12480 cfg->opt &= ~MONO_OPT_SSAPRE;
12482 i = mono_method_to_ir2 (cfg, method_to_compile, NULL, NULL, NULL, NULL, NULL, 0, FALSE);
12485 i = mono_method_to_ir (cfg, method_to_compile, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE);
12489 if (try_generic_shared && cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
12491 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12492 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12495 mono_destroy_compile (cfg);
12496 try_generic_shared = FALSE;
12497 goto restart_compile;
12499 g_assert (cfg->exception_type != MONO_EXCEPTION_GENERIC_SHARING_FAILED);
12501 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12502 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12503 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12504 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12505 /* cfg contains the details of the failure, so let the caller cleanup */
12509 mono_jit_stats.basic_blocks += cfg->num_bblocks;
12510 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
12512 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
12515 mono_decompose_long_opts (cfg);
12517 /* Should be done before branch opts */
12518 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
12519 mono_local_cprop2 (cfg);
12522 if (cfg->opt & MONO_OPT_BRANCH)
12523 mono_optimize_branches (cfg);
12526 /* This must be done _before_ global reg alloc and _after_ decompose */
12527 mono_handle_global_vregs (cfg);
12528 mono_local_deadce (cfg);
12529 mono_if_conversion (cfg);
12532 if ((cfg->opt & MONO_OPT_SSAPRE) || cfg->globalra)
12533 mono_remove_critical_edges (cfg);
12535 /* Depth-first ordering on basic blocks */
12536 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12539 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12540 if (cfg->num_bblocks != dfn + 1) {
12541 MonoBasicBlock *bb;
12543 cfg->num_bblocks = dfn + 1;
12545 /* remove unreachable code, because the code in them may be
12546 * inconsistent (access to dead variables for example) */
12547 for (bb = cfg->bb_entry; bb;) {
12548 MonoBasicBlock *bbn = bb->next_bb;
12551 * FIXME: Can't use the second case in methods with clauses, since the
12552 * bblocks inside the clauses are not processed during dfn computation.
12554 if (((header->clauses && (bbn && bbn->region == -1 && bbn->in_count == 0)) ||
12555 (!header->clauses && (bbn && bbn->region == -1 && !bbn->dfn))) &&
12556 bbn != cfg->bb_exit) {
12557 if (cfg->verbose_level > 1)
12558 g_print ("found unreachable code in BB%d\n", bbn->block_num);
12559 /* There may exist unreachable branches to this bb */
12560 bb->next_bb = bbn->next_bb;
12561 mono_nullify_basic_block (bbn);
12568 if (((cfg->num_varinfo > 2000) || (cfg->num_bblocks > 1000)) && !cfg->compile_aot) {
12570 * we disable some optimizations if there are too many variables
12571 * because JIT time may become too expensive. The actual number needs
12572 * to be tweaked and eventually the non-linear algorithms should be fixed.
12574 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
12575 cfg->disable_ssa = TRUE;
12578 if (cfg->opt & MONO_OPT_LOOP) {
12579 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
12580 mono_compute_natural_loops (cfg);
12583 /* after method_to_ir */
12585 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12586 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12590 //#define DEBUGSSA "logic_run"
12591 #define DEBUGSSA_CLASS "Tests"
12594 if (!header->num_clauses && !cfg->disable_ssa) {
12595 mono_local_cprop (cfg);
12597 #ifndef DISABLE_SSA
12599 mono_ssa_compute2 (cfg);
12601 mono_ssa_compute (cfg);
12605 if (cfg->opt & MONO_OPT_SSA) {
12606 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
12607 #ifndef DISABLE_SSA
12609 mono_local_cprop (cfg);
12611 mono_ssa_compute2 (cfg);
12613 mono_ssa_compute (cfg);
12616 if (cfg->verbose_level >= 2) {
12623 /* after SSA translation */
12625 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12626 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12630 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
12631 if (cfg->comp_done & MONO_COMP_SSA) {
12632 #ifndef DISABLE_SSA
12634 mono_ssa_cprop2 (cfg);
12636 mono_ssa_cprop (cfg);
12640 mono_local_cprop (cfg);
12644 #ifndef DISABLE_SSA
12645 if (cfg->comp_done & MONO_COMP_SSA) {
12646 //mono_ssa_strength_reduction (cfg);
12648 if (cfg->opt & MONO_OPT_SSAPRE) {
12649 mono_perform_ssapre (cfg);
12650 //mono_local_cprop (cfg);
12653 if (cfg->opt & MONO_OPT_DEADCE) {
12655 mono_ssa_deadce2 (cfg);
12657 mono_ssa_deadce (cfg);
12658 deadce_has_run = TRUE;
12662 if ((cfg->flags & (MONO_CFG_HAS_LDELEMA|MONO_CFG_HAS_CHECK_THIS)) && (cfg->opt & MONO_OPT_ABCREM))
12663 mono_perform_abc_removal2 (cfg);
12665 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
12666 mono_perform_abc_removal (cfg);
12670 mono_ssa_remove2 (cfg);
12671 mono_local_cprop2 (cfg);
12672 mono_handle_global_vregs (cfg);
12673 mono_local_deadce (cfg);
12676 mono_ssa_remove (cfg);
12678 if (cfg->opt & MONO_OPT_BRANCH) {
12679 MonoBasicBlock *bb;
12681 mono_optimize_branches (cfg);
12683 /* Have to recompute cfg->bblocks and bb->dfn */
12684 if (cfg->globalra) {
12685 mono_remove_critical_edges (cfg);
12687 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12690 /* Depth-first ordering on basic blocks */
12691 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12694 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12695 cfg->num_bblocks = dfn + 1;
12701 /* after SSA removal */
12703 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12704 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12709 #ifdef MONO_ARCH_SOFT_FLOAT
12710 mono_decompose_soft_float (cfg);
12712 mono_decompose_vtype_opts (cfg);
12713 if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
12714 mono_decompose_array_access_opts (cfg);
12717 if (!cfg->new_ir) {
12718 if (cfg->verbose_level > 4)
12719 mono_print_code (cfg, "BEFORE DECOMPOSE");
12721 decompose_pass (cfg);
12724 if (cfg->got_var) {
12727 g_assert (cfg->got_var_allocated);
12730 * Allways allocate the GOT var to a register, because keeping it
12731 * in memory will increase the number of live temporaries in some
12732 * code created by inssel.brg, leading to the well known spills+
12733 * branches problem. Testcase: mcs crash in
12734 * System.MonoCustomAttrs:GetCustomAttributes.
12736 regs = mono_arch_get_global_int_regs (cfg);
12738 cfg->got_var->opcode = OP_REGVAR;
12739 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
12740 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
12742 g_list_free (regs);
12745 /* todo: remove code when we have verified that the liveness for try/catch blocks
12749 * Currently, this can't be commented out since exception blocks are not
12750 * processed during liveness analysis.
12752 mono_liveness_handle_exception_clauses (cfg);
12754 if (cfg->globalra) {
12755 MonoBasicBlock *bb;
12757 /* Have to do this before regalloc since it can create vregs */
12758 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12759 mono_arch_lowering_pass (cfg, bb);
12761 mono_global_regalloc (cfg);
12764 if ((cfg->opt & MONO_OPT_LINEARS) && !cfg->globalra) {
12765 GList *vars, *regs;
12767 /* For now, compute aliasing info only if needed for deadce... */
12768 if (!cfg->new_ir && (cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
12769 cfg->aliasing_info = mono_build_aliasing_information (cfg);
12772 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
12773 cfg->comp_done &= ~MONO_COMP_LIVENESS;
12774 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
12775 mono_analyze_liveness (cfg);
12777 if (cfg->aliasing_info != NULL) {
12778 mono_aliasing_deadce (cfg->aliasing_info);
12779 deadce_has_run = TRUE;
12782 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
12783 regs = mono_arch_get_global_int_regs (cfg);
12785 regs = g_list_delete_link (regs, regs);
12786 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
12789 if (cfg->aliasing_info != NULL) {
12790 mono_destroy_aliasing_information (cfg->aliasing_info);
12791 cfg->aliasing_info = NULL;
12795 //mono_print_code (cfg);
12799 /* variables are allocated after decompose, since decompose could create temps */
12800 if (!cfg->globalra)
12801 mono_arch_allocate_vars (cfg);
12803 if (!cfg->new_ir && cfg->opt & MONO_OPT_CFOLD)
12804 mono_constant_fold (cfg);
12807 MonoBasicBlock *bb;
12808 gboolean need_local_opts;
12810 if (!cfg->globalra) {
12811 mono_spill_global_vars (cfg, &need_local_opts);
12813 if (need_local_opts || cfg->compile_aot) {
12814 /* To optimize code created by spill_global_vars */
12815 mono_local_cprop2 (cfg);
12816 mono_local_deadce (cfg);
12820 /* Add branches between non-consecutive bblocks */
12821 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12822 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
12823 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
12824 /* we are careful when inverting, since bugs like #59580
12825 * could show up when dealing with NaNs.
12827 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
12828 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
12829 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
12830 bb->last_ins->inst_false_bb = tmp;
12832 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
12834 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
12835 inst->opcode = OP_BR;
12836 inst->inst_target_bb = bb->last_ins->inst_false_bb;
12837 mono_bblock_add_inst (bb, inst);
12842 if (cfg->verbose_level >= 4 && !cfg->globalra) {
12843 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12844 MonoInst *tree = bb->code;
12845 g_print ("DUMP BLOCK %d:\n", bb->block_num);
12848 for (; tree; tree = tree->next) {
12849 mono_print_ins_index (-1, tree);
12855 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12856 bb->max_vreg = cfg->next_vreg;
12860 mini_select_instructions (cfg);
12862 mono_codegen (cfg);
12863 if (cfg->verbose_level >= 2) {
12864 char *id = mono_method_full_name (cfg->method, FALSE);
12865 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
12869 if (cfg->generic_sharing_context)
12870 generic_info_size = sizeof (MonoGenericJitInfo);
12872 generic_info_size = 0;
12874 if (cfg->method->dynamic) {
12875 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12876 generic_info_size);
12878 /* we access cfg->domain->mp */
12879 mono_domain_lock (cfg->domain);
12880 jinfo = mono_domain_alloc0 (cfg->domain, sizeof (MonoJitInfo) +
12881 (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12882 generic_info_size);
12883 mono_domain_unlock (cfg->domain);
12886 if (cfg->generic_sharing_context) {
12887 MonoGenericContext object_context;
12889 g_assert (!method_to_compile->klass->generic_class);
12890 if (method_to_compile->klass->generic_container) {
12891 int type_argc = method_to_compile->klass->generic_container->type_argc;
12893 object_context.class_inst = get_object_generic_inst (type_argc);
12895 object_context.class_inst = NULL;
12898 if (mini_method_get_context (method_to_compile)->method_inst) {
12899 int type_argc = mini_method_get_context (method_to_compile)->method_inst->type_argc;
12901 object_context.method_inst = get_object_generic_inst (type_argc);
12903 object_context.method_inst = NULL;
12906 g_assert (object_context.class_inst || object_context.method_inst);
12908 method_to_register = mono_class_inflate_generic_method (method_to_compile, &object_context);
12910 g_assert (method == method_to_compile);
12911 method_to_register = method;
12914 jinfo->method = method_to_register;
12915 jinfo->code_start = cfg->native_code;
12916 jinfo->code_size = cfg->code_len;
12917 jinfo->used_regs = cfg->used_int_regs;
12918 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
12919 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
12920 jinfo->num_clauses = header->num_clauses;
12922 if (cfg->generic_sharing_context) {
12924 MonoGenericJitInfo *gi;
12926 jinfo->has_generic_jit_info = 1;
12928 gi = mono_jit_info_get_generic_jit_info (jinfo);
12931 gi->generic_sharing_context = cfg->generic_sharing_context;
12934 * Non-generic static methods only get a "this" info
12935 * if they use the rgctx variable (which they are
12936 * forced to if they have any open catch clauses).
12938 if (cfg->rgctx_var ||
12939 (!(method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) &&
12940 !mini_method_get_context (method_to_compile)->method_inst &&
12941 !method_to_compile->klass->valuetype)) {
12944 if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
12945 mini_method_get_context (method_to_compile)->method_inst ||
12946 method_to_compile->klass->valuetype) {
12947 inst = cfg->rgctx_var;
12948 g_assert (inst->opcode == OP_REGOFFSET);
12950 inst = cfg->args [0];
12953 if (inst->opcode == OP_REGVAR) {
12954 gi->this_in_reg = 1;
12955 gi->this_reg = inst->dreg;
12957 //g_print ("this in reg %d\n", inst->dreg);
12959 g_assert (inst->opcode == OP_REGOFFSET);
12961 g_assert (inst->inst_basereg == X86_EBP);
12962 #elif defined(__x86_64__)
12963 g_assert (inst->inst_basereg == X86_EBP || inst->inst_basereg == X86_ESP);
12965 g_assert (inst->inst_offset >= G_MININT32 && inst->inst_offset <= G_MAXINT32);
12967 gi->this_in_reg = 0;
12968 gi->this_reg = inst->inst_basereg;
12969 gi->this_offset = inst->inst_offset;
12971 //g_print ("this at offset %d from reg %d\n", gi->this_offset, gi->this_reg);
12978 if (header->num_clauses) {
12981 for (i = 0; i < header->num_clauses; i++) {
12982 MonoExceptionClause *ec = &header->clauses [i];
12983 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
12984 MonoBasicBlock *tblock;
12987 ei->flags = ec->flags;
12989 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
12990 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
12992 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
12993 tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
12995 ei->data.filter = cfg->native_code + tblock->native_offset;
12997 ei->data.catch_class = ec->data.catch_class;
13000 tblock = cfg->cil_offset_to_bb [ec->try_offset];
13002 ei->try_start = cfg->native_code + tblock->native_offset;
13003 g_assert (tblock->native_offset);
13004 tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
13006 ei->try_end = cfg->native_code + tblock->native_offset;
13007 g_assert (tblock->native_offset);
13008 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
13010 ei->handler_start = cfg->native_code + tblock->native_offset;
13014 cfg->jit_info = jinfo;
13015 #if defined(__arm__)
13016 mono_arch_fixup_jinfo (cfg);
13019 if (!cfg->compile_aot) {
13020 mono_domain_lock (cfg->domain);
13021 mono_jit_info_table_add (cfg->domain, jinfo);
13023 if (cfg->method->dynamic)
13024 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
13025 mono_domain_unlock (cfg->domain);
13028 /* collect statistics */
13029 mono_perfcounters->jit_methods++;
13030 mono_perfcounters->jit_bytes += header->code_size;
13031 mono_jit_stats.allocated_code_size += cfg->code_len;
13032 code_size_ratio = cfg->code_len;
13033 if (code_size_ratio > mono_jit_stats.biggest_method_size && mono_jit_stats.enabled) {
13034 mono_jit_stats.biggest_method_size = code_size_ratio;
13035 g_free (mono_jit_stats.biggest_method);
13036 mono_jit_stats.biggest_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13038 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
13039 if (code_size_ratio > mono_jit_stats.max_code_size_ratio && mono_jit_stats.enabled) {
13040 mono_jit_stats.max_code_size_ratio = code_size_ratio;
13041 g_free (mono_jit_stats.max_ratio_method);
13042 mono_jit_stats.max_ratio_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13044 mono_jit_stats.native_code_size += cfg->code_len;
13046 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
13047 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
13048 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
13049 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
13057 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
13059 g_assert_not_reached ();
13063 #endif /* DISABLE_JIT */
13065 static MonoJitInfo*
13066 lookup_generic_method (MonoDomain *domain, MonoMethod *method)
13068 MonoMethod *open_method;
13070 if (!mono_method_is_generic_sharable_impl (method, FALSE))
13073 open_method = mono_method_get_declaring_generic_method (method);
13075 return mono_domain_lookup_shared_generic (domain, open_method);
13079 * LOCKING: Assumes domain->jit_code_hash_lock is held.
13081 static MonoJitInfo*
13082 lookup_method_inner (MonoDomain *domain, MonoMethod *method)
13084 MonoJitInfo *ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, method);
13089 return lookup_generic_method (domain, method);
13092 static MonoJitInfo*
13093 lookup_method (MonoDomain *domain, MonoMethod *method)
13097 mono_domain_jit_code_hash_lock (domain);
13098 info = lookup_method_inner (domain, method);
13099 mono_domain_jit_code_hash_unlock (domain);
13105 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt)
13108 gpointer code = NULL;
13110 MonoVTable *vtable;
13112 #ifdef MONO_USE_AOT_COMPILER
13113 if ((opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
13114 MonoDomain *domain = mono_domain_get ();
13116 mono_class_init (method->klass);
13118 mono_domain_lock (domain);
13119 if ((code = mono_aot_get_method (domain, method))) {
13120 mono_domain_unlock (domain);
13121 vtable = mono_class_vtable (domain, method->klass);
13123 mono_runtime_class_init (vtable);
13127 mono_domain_unlock (domain);
13131 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
13132 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
13134 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
13136 if (!piinfo->addr) {
13137 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
13138 piinfo->addr = mono_lookup_internal_call (method);
13139 else if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
13140 #ifdef PLATFORM_WIN32
13141 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);
13143 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);
13146 mono_lookup_pinvoke_call (method, NULL, NULL);
13148 nm = mono_marshal_get_native_wrapper (method, check_for_pending_exc, FALSE);
13149 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13151 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
13152 //mono_debug_add_wrapper (method, nm);
13153 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
13154 const char *name = method->name;
13157 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
13158 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
13159 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
13162 * We need to make sure this wrapper
13163 * is compiled because it might end up
13164 * in an (M)RGCTX if generic sharing
13165 * is enabled, and would be called
13166 * indirectly. If it were a
13167 * trampoline we'd try to patch that
13168 * indirect call, which is not
13171 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper_full (mi, TRUE));
13172 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
13173 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
13174 return mono_create_delegate_trampoline (method->klass);
13176 nm = mono_marshal_get_delegate_invoke (method, NULL);
13177 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13179 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
13180 nm = mono_marshal_get_delegate_begin_invoke (method);
13181 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13182 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
13183 nm = mono_marshal_get_delegate_end_invoke (method);
13184 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13191 g_error ("Attempting to JIT compile method '%s' while running with --aot-only.\n", mono_method_full_name (method, TRUE));
13193 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
13195 switch (cfg->exception_type) {
13196 case MONO_EXCEPTION_NONE:
13198 case MONO_EXCEPTION_TYPE_LOAD:
13199 case MONO_EXCEPTION_MISSING_FIELD:
13200 case MONO_EXCEPTION_MISSING_METHOD:
13201 case MONO_EXCEPTION_FILE_NOT_FOUND:
13202 case MONO_EXCEPTION_BAD_IMAGE: {
13203 /* Throw a type load exception if needed */
13204 MonoLoaderError *error = mono_loader_get_last_error ();
13208 ex = mono_loader_error_prepare_exception (error);
13210 if (cfg->exception_ptr) {
13211 ex = mono_class_get_exception_for_failure (cfg->exception_ptr);
13213 if (cfg->exception_type == MONO_EXCEPTION_MISSING_FIELD)
13214 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", cfg->exception_message);
13215 else if (cfg->exception_type == MONO_EXCEPTION_MISSING_METHOD)
13216 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", cfg->exception_message);
13217 else if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD)
13218 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", cfg->exception_message);
13219 else if (cfg->exception_type == MONO_EXCEPTION_FILE_NOT_FOUND)
13220 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FileNotFoundException", cfg->exception_message);
13221 else if (cfg->exception_type == MONO_EXCEPTION_BAD_IMAGE)
13222 ex = mono_get_exception_bad_image_format (cfg->exception_message);
13224 g_assert_not_reached ();
13227 mono_destroy_compile (cfg);
13228 mono_raise_exception (ex);
13231 case MONO_EXCEPTION_INVALID_PROGRAM: {
13232 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
13233 mono_destroy_compile (cfg);
13234 mono_raise_exception (ex);
13237 case MONO_EXCEPTION_UNVERIFIABLE_IL: {
13238 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
13239 mono_destroy_compile (cfg);
13240 mono_raise_exception (ex);
13243 case MONO_EXCEPTION_METHOD_ACCESS: {
13244 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", cfg->exception_message);
13245 mono_destroy_compile (cfg);
13246 mono_raise_exception (ex);
13249 case MONO_EXCEPTION_FIELD_ACCESS: {
13250 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FieldAccessException", cfg->exception_message);
13251 mono_destroy_compile (cfg);
13252 mono_raise_exception (ex);
13255 /* this can only be set if the security manager is active */
13256 case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
13257 MonoSecurityManager* secman = mono_security_manager_get_methods ();
13258 MonoObject *exc = NULL;
13261 args [0] = &cfg->exception_data;
13262 args [1] = &method;
13263 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
13265 mono_destroy_compile (cfg);
13268 mono_raise_exception ((MonoException*)exc);
13270 case MONO_EXCEPTION_OBJECT_SUPPLIED: {
13271 MonoException *exp = cfg->exception_ptr;
13272 MONO_GC_UNREGISTER_ROOT (cfg->exception_ptr);
13273 mono_destroy_compile (cfg);
13274 mono_raise_exception (exp);
13278 g_assert_not_reached ();
13281 mono_domain_lock (target_domain);
13283 /* Check if some other thread already did the job. In this case, we can
13284 discard the code this thread generated. */
13286 mono_domain_jit_code_hash_lock (target_domain);
13288 info = lookup_method_inner (target_domain, method);
13290 /* We can't use a domain specific method in another domain */
13291 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
13292 code = info->code_start;
13293 // printf("Discarding code for method %s\n", method->name);
13297 if (code == NULL) {
13298 mono_internal_hash_table_insert (&target_domain->jit_code_hash, cfg->jit_info->method, cfg->jit_info);
13299 mono_domain_jit_code_hash_unlock (target_domain);
13300 code = cfg->native_code;
13302 if (cfg->generic_sharing_context && mono_method_is_generic_sharable_impl (method, FALSE)) {
13303 /* g_print ("inserting method %s.%s.%s\n", method->klass->name_space, method->klass->name, method->name); */
13304 mono_domain_register_shared_generic (target_domain,
13305 mono_method_get_declaring_generic_method (method), cfg->jit_info);
13306 mono_stats.generics_shared_methods++;
13309 mono_domain_jit_code_hash_unlock (target_domain);
13312 mono_destroy_compile (cfg);
13314 if (domain_jit_info (target_domain)->jump_target_hash) {
13315 MonoJumpInfo patch_info;
13316 GSList *list, *tmp;
13317 list = g_hash_table_lookup (domain_jit_info (target_domain)->jump_target_hash, method);
13319 patch_info.next = NULL;
13320 patch_info.ip.i = 0;
13321 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
13322 patch_info.data.method = method;
13323 g_hash_table_remove (domain_jit_info (target_domain)->jump_target_hash, method);
13325 for (tmp = list; tmp; tmp = tmp->next)
13326 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
13327 g_slist_free (list);
13330 mono_domain_unlock (target_domain);
13332 vtable = mono_class_vtable (target_domain, method->klass);
13334 MonoException *exc;
13335 exc = mono_class_get_exception_for_failure (method->klass);
13337 mono_raise_exception (exc);
13339 mono_runtime_class_init (vtable);
13344 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
13346 MonoDomain *target_domain, *domain = mono_domain_get ();
13349 MonoJitICallInfo *callinfo = NULL;
13352 * ICALL wrappers are handled specially, since there is only one copy of them
13353 * shared by all appdomains.
13355 if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
13356 const char *icall_name;
13358 icall_name = method->name + strlen ("__icall_wrapper_");
13359 g_assert (icall_name);
13360 callinfo = mono_find_jit_icall_by_name (icall_name);
13361 g_assert (callinfo);
13363 /* Must be domain neutral since there is only one copy */
13364 opt |= MONO_OPT_SHARED;
13367 if (opt & MONO_OPT_SHARED)
13368 target_domain = mono_get_root_domain ();
13370 target_domain = domain;
13372 info = lookup_method (target_domain, method);
13374 /* We can't use a domain specific method in another domain */
13375 if (! ((domain != target_domain) && !info->domain_neutral)) {
13376 MonoVTable *vtable;
13378 mono_jit_stats.methods_lookups++;
13379 vtable = mono_class_vtable (domain, method->klass);
13380 mono_runtime_class_init (vtable);
13381 return mono_create_ftnptr (target_domain, info->code_start);
13385 p = mono_create_ftnptr (target_domain, mono_jit_compile_method_inner (method, target_domain, opt));
13389 if (!callinfo->wrapper) {
13390 callinfo->wrapper = p;
13391 mono_register_jit_icall_wrapper (callinfo, p);
13392 mono_debug_add_icall_wrapper (method, callinfo);
13394 mono_jit_unlock ();
13401 mono_jit_compile_method (MonoMethod *method)
13403 return mono_jit_compile_method_with_opt (method, default_opt);
13406 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13408 invalidated_delegate_trampoline (char *desc)
13410 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
13411 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
13417 * mono_jit_free_method:
13419 * Free all memory allocated by the JIT for METHOD.
13422 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
13424 MonoJitDynamicMethodInfo *ji;
13425 gboolean destroy = TRUE;
13427 g_assert (method->dynamic);
13429 mono_domain_lock (domain);
13430 ji = mono_dynamic_code_hash_lookup (domain, method);
13431 mono_domain_unlock (domain);
13435 mono_domain_lock (domain);
13436 g_hash_table_remove (domain_jit_info (domain)->dynamic_code_hash, method);
13437 mono_internal_hash_table_remove (&domain->jit_code_hash, method);
13438 g_hash_table_remove (domain_jit_info (domain)->jump_trampoline_hash, method);
13439 mono_domain_unlock (domain);
13441 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13442 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
13444 * Instead of freeing the code, change it to call an error routine
13445 * so people can fix their code.
13447 char *type = mono_type_full_name (&method->klass->byval_arg);
13448 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
13451 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
13457 * This needs to be done before freeing code_mp, since the code address is the
13458 * key in the table, so if we free the code_mp first, another thread can grab the
13459 * same code address and replace our entry in the table.
13461 mono_jit_info_table_remove (domain, ji->ji);
13464 mono_code_manager_destroy (ji->code_mp);
13469 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
13471 MonoDomain *target_domain;
13474 if (default_opt & MONO_OPT_SHARED)
13475 target_domain = mono_get_root_domain ();
13477 target_domain = domain;
13479 info = lookup_method (target_domain, method);
13481 /* We can't use a domain specific method in another domain */
13482 if (! ((domain != target_domain) && !info->domain_neutral)) {
13483 mono_jit_stats.methods_lookups++;
13484 return info->code_start;
13492 * mono_jit_runtime_invoke:
13493 * @method: the method to invoke
13494 * @obj: this pointer
13495 * @params: array of parameter values.
13496 * @exc: used to catch exceptions objects
13499 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
13501 MonoMethod *to_compile;
13502 MonoMethod *invoke;
13503 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
13504 void* compiled_method;
13505 MonoVTable *vtable;
13507 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
13508 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
13512 if (mono_method_needs_static_rgctx_invoke (method, FALSE))
13513 to_compile = mono_marshal_get_static_rgctx_invoke (method);
13515 to_compile = method;
13517 invoke = mono_marshal_get_runtime_invoke (method);
13518 runtime_invoke = mono_jit_compile_method (invoke);
13520 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
13521 * the helper method in System.Object and not the target class
13523 vtable = mono_class_vtable (mono_domain_get (), method->klass);
13525 mono_runtime_class_init (vtable);
13527 if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
13528 (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
13530 * Array Get/Set/Address methods. The JIT implements them using inline code
13531 * inside the runtime invoke wrappers, so no need to compile them.
13533 compiled_method = NULL;
13535 compiled_method = mono_jit_compile_method (to_compile);
13537 return runtime_invoke (obj, params, exc, compiled_method);
13540 #ifdef MONO_GET_CONTEXT
13541 #define GET_CONTEXT MONO_GET_CONTEXT
13544 #ifndef GET_CONTEXT
13545 #ifdef PLATFORM_WIN32
13546 #define GET_CONTEXT \
13547 struct sigcontext *ctx = (struct sigcontext*)_dummy;
13549 #ifdef MONO_ARCH_USE_SIGACTION
13550 #define GET_CONTEXT \
13551 void *ctx = context;
13552 #elif defined(__sparc__)
13553 #define GET_CONTEXT \
13554 void *ctx = sigctx;
13556 #define GET_CONTEXT \
13557 void **_p = (void **)&_dummy; \
13558 struct sigcontext *ctx = (struct sigcontext *)++_p;
13563 #ifdef MONO_ARCH_USE_SIGACTION
13564 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
13565 #elif defined(__sparc__)
13566 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, void *sigctx)
13568 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
13572 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
13574 MonoException *exc = NULL;
13575 #ifndef MONO_ARCH_USE_SIGACTION
13580 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
13581 if (mono_arch_is_int_overflow (ctx, info))
13582 exc = mono_get_exception_arithmetic ();
13584 exc = mono_get_exception_divide_by_zero ();
13586 exc = mono_get_exception_divide_by_zero ();
13589 mono_arch_handle_exception (ctx, exc, FALSE);
13593 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
13595 MonoException *exc;
13598 exc = mono_get_exception_execution_engine ("SIGILL");
13600 mono_arch_handle_exception (ctx, exc, FALSE);
13604 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
13606 #ifndef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13607 MonoException *exc = NULL;
13610 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13614 #ifdef MONO_ARCH_USE_SIGACTION
13615 if (debug_options.collect_pagefault_stats) {
13616 if (mono_aot_is_pagefault (info->si_addr)) {
13617 mono_aot_handle_pagefault (info->si_addr);
13623 /* The thread might no be registered with the runtime */
13624 if (!mono_domain_get () || !jit_tls)
13625 mono_handle_native_sigsegv (SIGSEGV, ctx);
13627 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
13629 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13630 if (mono_handle_soft_stack_ovf (jit_tls, ji, ctx, (guint8*)info->si_addr))
13633 /* The hard-guard page has been hit: there is not much we can do anymore
13634 * Print a hopefully clear message and abort.
13636 if (jit_tls->stack_size &&
13637 ABS ((guint8*)info->si_addr - ((guint8*)jit_tls->end_of_stack - jit_tls->stack_size)) < 32768) {
13638 const char *method;
13639 /* we don't do much now, but we can warn the user with a useful message */
13640 fprintf (stderr, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), (gpointer)info->si_addr);
13641 if (ji && ji->method)
13642 method = mono_method_full_name (ji->method, TRUE);
13644 method = "Unmanaged";
13645 fprintf (stderr, "At %s\n", method);
13648 mono_arch_handle_altstack_exception (ctx, info->si_addr, FALSE);
13653 mono_handle_native_sigsegv (SIGSEGV, ctx);
13656 mono_arch_handle_exception (ctx, exc, FALSE);
13660 #ifndef PLATFORM_WIN32
13663 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
13668 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13670 mono_handle_native_sigsegv (SIGABRT, ctx);
13675 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
13677 gboolean running_managed;
13678 MonoException *exc;
13679 MonoThread *thread = mono_thread_current ();
13684 if (thread->thread_dump_requested) {
13685 thread->thread_dump_requested = FALSE;
13687 mono_print_thread_dump (ctx);
13692 * This is an async signal, so the code below must not call anything which
13693 * is not async safe. That includes the pthread locking functions. If we
13694 * know that we interrupted managed code, then locking is safe.
13696 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13697 running_managed = ji != NULL;
13699 exc = mono_thread_request_interruption (running_managed);
13702 mono_arch_handle_exception (ctx, exc, FALSE);
13705 #if defined(__i386__) || defined(__x86_64__)
13706 #define FULL_STAT_PROFILER_BACKTRACE 1
13707 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
13708 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
13709 #if MONO_ARCH_STACK_GROWS_UP
13710 #define IS_BEFORE_ON_STACK <
13711 #define IS_AFTER_ON_STACK >
13713 #define IS_BEFORE_ON_STACK >
13714 #define IS_AFTER_ON_STACK <
13717 #define FULL_STAT_PROFILER_BACKTRACE 0
13720 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
13723 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13731 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13733 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
13736 if (call_chain_depth == 0) {
13737 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
13739 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13740 int current_frame_index = 1;
13741 MonoContext mono_context;
13742 #if FULL_STAT_PROFILER_BACKTRACE
13743 guchar *current_frame;
13744 guchar *stack_bottom;
13747 MonoDomain *domain;
13749 guchar *ips [call_chain_depth + 1];
13751 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
13752 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
13754 if (jit_tls != NULL) {
13755 #if FULL_STAT_PROFILER_BACKTRACE
13756 stack_bottom = jit_tls->end_of_stack;
13757 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
13758 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
13760 while ((current_frame_index <= call_chain_depth) &&
13761 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
13762 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
13763 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
13764 current_frame_index ++;
13765 stack_top = current_frame;
13766 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
13769 domain = mono_domain_get ();
13770 if (domain != NULL) {
13771 MonoLMF *lmf = NULL;
13774 MonoContext new_mono_context;
13776 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13777 &new_mono_context, NULL, &lmf, &native_offset, NULL);
13778 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
13779 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
13780 current_frame_index ++;
13781 mono_context = new_mono_context;
13782 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13783 &new_mono_context, NULL, &lmf, &native_offset, NULL);
13790 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
13797 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
13803 /* We use this signal to start the attach agent too */
13804 res = mono_attach_start ();
13808 printf ("Full thread dump:\n");
13810 mono_threads_request_thread_dump ();
13813 * print_thread_dump () skips the current thread, since sending a signal
13814 * to it would invoke the signal handler below the sigquit signal handler,
13815 * and signal handlers don't create an lmf, so the stack walk could not
13818 mono_print_thread_dump (ctx);
13822 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
13824 gboolean enabled = mono_trace_is_enabled ();
13826 mono_trace_enable (!enabled);
13832 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
13834 MonoException *exc;
13837 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
13839 mono_arch_handle_exception (ctx, exc, FALSE);
13842 #ifdef PLATFORM_MACOSX
13845 * This code disables the CrashReporter of MacOS X by installing
13846 * a dummy Mach exception handler.
13850 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
13854 exc_server (mach_msg_header_t *request_msg,
13855 mach_msg_header_t *reply_msg);
13858 * The exception message
13861 mach_msg_base_t msg; /* common mach message header */
13862 char payload [1024]; /* opaque */
13863 } mach_exception_msg_t;
13865 /* The exception port */
13866 static mach_port_t mach_exception_port = VM_MAP_NULL;
13869 * Implicitly called by exc_server. Must be public.
13871 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
13874 catch_exception_raise (
13875 mach_port_t exception_port,
13876 mach_port_t thread,
13878 exception_type_t exception,
13879 exception_data_t code,
13880 mach_msg_type_number_t code_count)
13882 /* consume the exception */
13883 return KERN_FAILURE;
13887 * Exception thread handler.
13891 mach_exception_thread (void *arg)
13894 mach_exception_msg_t request;
13895 mach_exception_msg_t reply;
13896 mach_msg_return_t result;
13898 /* receive from "mach_exception_port" */
13899 result = mach_msg (&request.msg.header,
13900 MACH_RCV_MSG | MACH_RCV_LARGE,
13903 mach_exception_port,
13904 MACH_MSG_TIMEOUT_NONE,
13907 g_assert (result == MACH_MSG_SUCCESS);
13909 /* dispatch to catch_exception_raise () */
13910 exc_server (&request.msg.header, &reply.msg.header);
13912 /* send back to sender */
13913 result = mach_msg (&reply.msg.header,
13915 reply.msg.header.msgh_size,
13918 MACH_MSG_TIMEOUT_NONE,
13921 g_assert (result == MACH_MSG_SUCCESS);
13927 macosx_register_exception_handler ()
13930 pthread_attr_t attr;
13933 if (mach_exception_port != VM_MAP_NULL)
13936 task = mach_task_self ();
13938 /* create the "mach_exception_port" with send & receive rights */
13939 g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
13940 &mach_exception_port) == KERN_SUCCESS);
13941 g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
13942 MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
13944 /* create the exception handler thread */
13945 g_assert (!pthread_attr_init (&attr));
13946 g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
13947 g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
13948 pthread_attr_destroy (&attr);
13951 * register "mach_exception_port" as a receiver for the
13952 * EXC_BAD_ACCESS exception
13954 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
13956 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
13957 mach_exception_port,
13959 MACHINE_THREAD_STATE) == KERN_SUCCESS);
13963 #ifndef PLATFORM_WIN32
13965 add_signal_handler (int signo, gpointer handler)
13967 struct sigaction sa;
13969 #ifdef MONO_ARCH_USE_SIGACTION
13970 sa.sa_sigaction = handler;
13971 sigemptyset (&sa.sa_mask);
13972 sa.sa_flags = SA_SIGINFO;
13973 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13974 if (signo == SIGSEGV)
13975 sa.sa_flags |= SA_ONSTACK;
13978 sa.sa_handler = handler;
13979 sigemptyset (&sa.sa_mask);
13982 g_assert (sigaction (signo, &sa, NULL) != -1);
13986 remove_signal_handler (int signo)
13988 struct sigaction sa;
13990 sa.sa_handler = SIG_DFL;
13991 sigemptyset (&sa.sa_mask);
13994 g_assert (sigaction (signo, &sa, NULL) != -1);
13999 mono_runtime_install_handlers (void)
14001 #ifdef PLATFORM_WIN32
14003 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
14004 win32_seh_set_handler(SIGILL, sigill_signal_handler);
14005 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
14006 if (debug_options.handle_sigint)
14007 win32_seh_set_handler(SIGINT, sigint_signal_handler);
14009 #else /* !PLATFORM_WIN32 */
14011 sigset_t signal_set;
14013 #if defined(PLATFORM_MACOSX) && !defined(__arm__)
14014 macosx_register_exception_handler ();
14017 if (debug_options.handle_sigint)
14018 add_signal_handler (SIGINT, sigint_signal_handler);
14020 add_signal_handler (SIGFPE, sigfpe_signal_handler);
14021 add_signal_handler (SIGQUIT, sigquit_signal_handler);
14022 add_signal_handler (SIGILL, sigill_signal_handler);
14023 add_signal_handler (SIGBUS, sigsegv_signal_handler);
14024 if (mono_jit_trace_calls != NULL)
14025 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
14027 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
14028 /* it seems to have become a common bug for some programs that run as parents
14029 * of many processes to block signal delivery for real time signals.
14030 * We try to detect and work around their breakage here.
14032 sigemptyset (&signal_set);
14033 sigaddset (&signal_set, mono_thread_get_abort_signal ());
14034 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
14036 signal (SIGPIPE, SIG_IGN);
14038 add_signal_handler (SIGABRT, sigabrt_signal_handler);
14040 /* catch SIGSEGV */
14041 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
14042 #endif /* PLATFORM_WIN32 */
14046 mono_runtime_cleanup_handlers (void)
14048 #ifdef PLATFORM_WIN32
14049 win32_seh_cleanup();
14051 if (debug_options.handle_sigint)
14052 remove_signal_handler (SIGINT);
14054 remove_signal_handler (SIGFPE);
14055 remove_signal_handler (SIGQUIT);
14056 remove_signal_handler (SIGILL);
14057 remove_signal_handler (SIGBUS);
14058 if (mono_jit_trace_calls != NULL)
14059 remove_signal_handler (SIGUSR2);
14061 remove_signal_handler (mono_thread_get_abort_signal ());
14063 remove_signal_handler (SIGABRT);
14065 remove_signal_handler (SIGSEGV);
14066 #endif /* PLATFORM_WIN32 */
14070 #ifdef HAVE_LINUX_RTC_H
14071 #include <linux/rtc.h>
14072 #include <sys/ioctl.h>
14074 static int rtc_fd = -1;
14077 enable_rtc_timer (gboolean enable)
14080 flags = fcntl (rtc_fd, F_GETFL);
14082 perror ("getflags");
14089 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
14090 perror ("setflags");
14097 #ifdef PLATFORM_WIN32
14098 static HANDLE win32_main_thread;
14099 static MMRESULT win32_timer;
14101 static void CALLBACK
14102 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
14106 context.ContextFlags = CONTEXT_CONTROL;
14107 if (GetThreadContext (win32_main_thread, &context)) {
14109 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
14111 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
14118 setup_stat_profiler (void)
14121 struct itimerval itval;
14122 static int inited = 0;
14123 #ifdef HAVE_LINUX_RTC_H
14124 const char *rtc_freq;
14125 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
14129 freq = atoi (rtc_freq);
14132 rtc_fd = open ("/dev/rtc", O_RDONLY);
14133 if (rtc_fd == -1) {
14134 perror ("open /dev/rtc");
14137 add_signal_handler (SIGPROF, sigprof_signal_handler);
14138 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
14139 perror ("set rtc freq");
14142 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
14143 perror ("start rtc");
14146 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
14150 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
14154 enable_rtc_timer (TRUE);
14161 itval.it_interval.tv_usec = 999;
14162 itval.it_interval.tv_sec = 0;
14163 itval.it_value = itval.it_interval;
14164 setitimer (ITIMER_PROF, &itval, NULL);
14168 add_signal_handler (SIGPROF, sigprof_signal_handler);
14169 #elif defined (PLATFORM_WIN32)
14170 static int inited = 0;
14177 if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
14180 if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
14183 if (timeBeginPeriod (1) != TIMERR_NOERROR)
14186 if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
14193 /* mono_jit_create_remoting_trampoline:
14194 * @method: pointer to the method info
14196 * Creates a trampoline which calls the remoting functions. This
14197 * is used in the vtable of transparent proxies.
14199 * Returns: a pointer to the newly created code
14202 mono_jit_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
14205 guint8 *addr = NULL;
14207 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && mono_method_signature (method)->generic_param_count) {
14208 return mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING,
14212 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
14213 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
14214 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
14215 addr = mono_compile_method (nm);
14217 addr = mono_compile_method (method);
14219 return mono_get_addr_from_ftnptr (addr);
14222 #ifdef MONO_ARCH_HAVE_IMT
14224 mini_get_imt_trampoline (void)
14226 static gpointer tramp = NULL;
14228 tramp = mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14233 #ifdef MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14235 mini_get_vtable_trampoline (void)
14237 static gpointer tramp = NULL;
14239 tramp = mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14245 mini_parse_debug_options (void)
14247 char *options = getenv ("MONO_DEBUG");
14248 gchar **args, **ptr;
14253 args = g_strsplit (options, ",", -1);
14255 for (ptr = args; ptr && *ptr; ptr++) {
14256 const char *arg = *ptr;
14258 if (!strcmp (arg, "handle-sigint"))
14259 debug_options.handle_sigint = TRUE;
14260 else if (!strcmp (arg, "keep-delegates"))
14261 debug_options.keep_delegates = TRUE;
14262 else if (!strcmp (arg, "collect-pagefault-stats"))
14263 debug_options.collect_pagefault_stats = TRUE;
14264 else if (!strcmp (arg, "break-on-unverified"))
14265 debug_options.break_on_unverified = TRUE;
14266 else if (!strcmp (arg, "no-gdb-backtrace"))
14267 debug_options.no_gdb_backtrace = TRUE;
14268 else if (!strcmp (arg, "dont-free-domains"))
14269 mono_dont_free_domains = TRUE;
14271 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
14272 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains'\n");
14281 mini_get_debug_options (void)
14283 return &debug_options;
14287 mini_create_jit_domain_info (MonoDomain *domain)
14289 MonoJitDomainInfo *info = g_new0 (MonoJitDomainInfo, 1);
14291 info->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14292 info->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14293 info->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14294 info->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14296 domain->runtime_info = info;
14300 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
14302 g_slist_free (value);
14306 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
14308 MonoJitDynamicMethodInfo *di = value;
14309 mono_code_manager_destroy (di->code_mp);
14314 mini_free_jit_domain_info (MonoDomain *domain)
14316 MonoJitDomainInfo *info = domain_jit_info (domain);
14318 if (info->jump_target_hash) {
14319 g_hash_table_foreach (info->jump_target_hash, delete_jump_list, NULL);
14320 g_hash_table_destroy (info->jump_target_hash);
14322 if (info->jump_target_got_slot_hash) {
14323 g_hash_table_foreach (info->jump_target_got_slot_hash, delete_jump_list, NULL);
14324 g_hash_table_destroy (info->jump_target_got_slot_hash);
14326 if (info->dynamic_code_hash) {
14327 g_hash_table_foreach (info->dynamic_code_hash, dynamic_method_info_free, NULL);
14328 g_hash_table_destroy (info->dynamic_code_hash);
14330 if (info->method_code_hash)
14331 g_hash_table_destroy (info->method_code_hash);
14332 g_hash_table_destroy (info->class_init_trampoline_hash);
14333 g_hash_table_destroy (info->jump_trampoline_hash);
14334 g_hash_table_destroy (info->jit_trampoline_hash);
14335 g_hash_table_destroy (info->delegate_trampoline_hash);
14337 g_free (domain->runtime_info);
14338 domain->runtime_info = NULL;
14342 mini_init (const char *filename, const char *runtime_version)
14344 MonoDomain *domain;
14346 MONO_PROBE_VES_INIT_BEGIN ();
14349 if (access ("/proc/self/maps", F_OK) != 0) {
14350 g_print ("Mono requires /proc to be mounted.\n");
14355 /* Happens when using the embedding interface */
14356 if (!default_opt_set)
14357 default_opt = mono_parse_default_optimizations (NULL);
14359 InitializeCriticalSection (&jit_mutex);
14361 if (!global_codeman)
14362 global_codeman = mono_code_manager_new ();
14363 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
14365 mono_arch_cpu_init ();
14369 mono_trampolines_init ();
14371 if (!g_thread_supported ())
14372 g_thread_init (NULL);
14374 if (getenv ("MONO_DEBUG") != NULL)
14375 mini_parse_debug_options ();
14377 mono_gc_base_init ();
14379 mono_jit_tls_id = TlsAlloc ();
14380 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
14382 #ifndef DISABLE_JIT
14386 if (default_opt & MONO_OPT_AOT)
14389 mono_runtime_install_handlers ();
14390 mono_threads_install_cleanup (mini_thread_cleanup);
14392 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
14393 // This is experimental code so provide an env var to switch it off
14394 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
14395 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
14397 check_for_pending_exc = FALSE;
14398 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc);
14402 #define JIT_TRAMPOLINES_WORK
14403 #ifdef JIT_TRAMPOLINES_WORK
14404 mono_install_compile_method (mono_jit_compile_method);
14405 mono_install_free_method (mono_jit_free_method);
14406 mono_install_trampoline (mono_create_jit_trampoline);
14407 mono_install_jump_trampoline (mono_create_jump_trampoline);
14408 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
14409 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
14410 mono_install_create_domain_hook (mini_create_jit_domain_info);
14411 mono_install_free_domain_hook (mini_free_jit_domain_info);
14413 #define JIT_INVOKE_WORKS
14414 #ifdef JIT_INVOKE_WORKS
14415 mono_install_runtime_invoke (mono_jit_runtime_invoke);
14417 mono_install_stack_walk (mono_jit_walk_stack);
14418 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
14419 mono_install_get_class_from_name (mono_aot_get_class_from_name);
14420 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
14422 if (debug_options.collect_pagefault_stats) {
14423 mono_aot_set_make_unreadable (TRUE);
14426 if (runtime_version)
14427 domain = mono_init_version (filename, runtime_version);
14429 domain = mono_init_from_assembly (filename, filename);
14431 if (mono_aot_only) {
14432 /* The IMT tables are very dynamic thus they are hard to AOT */
14433 mono_use_imt = FALSE;
14434 /* This helps catch code allocation requests */
14435 mono_code_manager_set_read_only (domain->code_mp);
14438 #ifdef MONO_ARCH_HAVE_IMT
14439 if (mono_use_imt) {
14440 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
14441 mono_install_imt_trampoline (mini_get_imt_trampoline ());
14442 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14443 mono_install_vtable_trampoline (mini_get_vtable_trampoline ());
14448 /* This must come after mono_init () in the aot-only case */
14449 mono_exceptions_init ();
14450 mono_install_handler (mono_get_throw_exception ());
14452 mono_icall_init ();
14454 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
14455 ves_icall_get_frame_info);
14456 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
14457 ves_icall_get_trace);
14458 mono_add_internal_call ("System.Exception::get_trace",
14459 ves_icall_System_Exception_get_trace);
14460 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
14461 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
14462 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
14463 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
14464 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
14465 mono_runtime_install_handlers);
14468 create_helper_signature ();
14470 #define JIT_CALLS_WORK
14471 #ifdef JIT_CALLS_WORK
14472 /* Needs to be called here since register_jit_icall depends on it */
14473 mono_marshal_init ();
14475 mono_arch_register_lowlevel_calls ();
14476 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
14477 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
14478 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
14479 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
14480 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
14481 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
14482 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
14484 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
14485 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
14486 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE);
14487 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
14488 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
14491 register_icall (mono_thread_get_undeniable_exception, "mono_thread_get_undeniable_exception", "object", FALSE);
14492 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
14493 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
14494 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
14495 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
14498 * NOTE, NOTE, NOTE, NOTE:
14499 * when adding emulation for some opcodes, remember to also add a dummy
14500 * rule to the burg files, because we need the arity information to be correct.
14502 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
14503 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
14504 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
14505 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
14506 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
14507 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
14508 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
14509 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
14512 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
14513 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
14514 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
14515 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
14518 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14519 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, FALSE);
14520 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14521 mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, FALSE);
14522 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14523 mono_register_opcode_emulation (OP_IDIV, "__emul_op_idiv", "int32 int32 int32", mono_idiv, FALSE);
14524 mono_register_opcode_emulation (OP_IDIV_UN, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14525 mono_register_opcode_emulation (OP_IREM, "__emul_op_irem", "int32 int32 int32", mono_irem, FALSE);
14526 mono_register_opcode_emulation (OP_IREM_UN, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14529 #ifdef MONO_ARCH_EMULATE_MUL_DIV
14530 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14531 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14532 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
14533 mono_register_opcode_emulation (OP_IMUL, "__emul_op_imul", "int32 int32 int32", mono_imul, TRUE);
14534 mono_register_opcode_emulation (OP_IMUL_OVF, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14535 mono_register_opcode_emulation (OP_IMUL_OVF_UN, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14537 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
14538 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, FALSE);
14541 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
14542 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
14543 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
14544 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
14546 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
14547 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
14549 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
14550 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14551 mono_register_opcode_emulation (OP_ICONV_TO_R_UN, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14553 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
14554 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
14556 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
14557 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
14559 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
14560 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
14562 #ifdef MONO_ARCH_EMULATE_FREM
14563 mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
14566 #ifdef MONO_ARCH_SOFT_FLOAT
14567 mono_register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, FALSE);
14568 mono_register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, FALSE);
14569 mono_register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, FALSE);
14570 mono_register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, FALSE);
14571 mono_register_opcode_emulation (CEE_CONV_R8, "__emul_conv_r8", "double int32", mono_conv_to_r8, FALSE);
14572 mono_register_opcode_emulation (OP_ICONV_TO_R8, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8, FALSE);
14573 mono_register_opcode_emulation (CEE_CONV_R4, "__emul_conv_r4", "double int32", mono_conv_to_r4, FALSE);
14574 mono_register_opcode_emulation (OP_ICONV_TO_R4, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4, FALSE);
14575 mono_register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, FALSE);
14576 mono_register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, FALSE);
14577 mono_register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, FALSE);
14578 mono_register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, FALSE);
14579 mono_register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, FALSE);
14580 mono_register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, FALSE);
14582 mono_register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, FALSE);
14583 mono_register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, FALSE);
14584 mono_register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, FALSE);
14585 mono_register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, FALSE);
14586 mono_register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, FALSE);
14587 mono_register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, FALSE);
14588 mono_register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, FALSE);
14589 mono_register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, FALSE);
14590 mono_register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, FALSE);
14591 mono_register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, FALSE);
14593 mono_register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, FALSE);
14594 mono_register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, FALSE);
14595 mono_register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, FALSE);
14596 mono_register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, FALSE);
14597 mono_register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, FALSE);
14599 register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
14600 register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
14601 register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
14604 #if SIZEOF_VOID_P == 4
14605 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
14608 /* other jit icalls */
14609 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
14610 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
14611 "ptr ptr ptr", FALSE);
14612 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
14613 register_icall (mono_ldtoken_wrapper_generic_shared, "mono_ldtoken_wrapper_generic_shared",
14614 "ptr ptr ptr ptr", FALSE);
14615 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
14616 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
14617 register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
14618 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
14619 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
14620 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
14621 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
14622 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
14623 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
14624 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
14625 register_icall (mono_ldvirtfn_gshared, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE);
14626 register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
14627 register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
14628 register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
14629 register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
14630 register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
14631 register_icall (mono_object_castclass, "mono_object_castclass", "object object ptr", FALSE);
14632 register_icall (mono_break, "mono_break", NULL, TRUE);
14633 register_icall (mono_create_corlib_exception_0, "mono_create_corlib_exception_0", "object int", TRUE);
14634 register_icall (mono_create_corlib_exception_1, "mono_create_corlib_exception_1", "object int object", TRUE);
14635 register_icall (mono_create_corlib_exception_2, "mono_create_corlib_exception_2", "object int object object", TRUE);
14636 register_icall (mono_array_new_1, "mono_array_new_1", "object ptr int", FALSE);
14637 register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
14640 mono_generic_sharing_init ();
14642 #ifdef MONO_ARCH_SIMD_INTRINSICS
14643 mono_simd_intrinsics_init ();
14646 if (mono_compile_aot)
14648 * Avoid running managed code when AOT compiling, since the platform
14649 * might only support aot-only execution.
14651 mono_runtime_set_no_exec (TRUE);
14653 #define JIT_RUNTIME_WORKS
14654 #ifdef JIT_RUNTIME_WORKS
14655 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
14656 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
14657 mono_thread_attach (domain);
14660 mono_profiler_runtime_initialized ();
14662 MONO_PROBE_VES_INIT_END ();
14667 MonoJitStats mono_jit_stats = {0};
14670 print_jit_stats (void)
14672 if (mono_jit_stats.enabled) {
14673 g_print ("Mono Jit statistics\n");
14674 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
14675 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
14676 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
14677 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
14678 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
14679 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
14680 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
14681 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
14682 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
14683 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
14684 mono_jit_stats.max_ratio_method);
14685 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats.biggest_method_size,
14686 mono_jit_stats.biggest_method);
14687 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
14688 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
14689 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
14690 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
14691 g_print ("Regvars: %ld\n", mono_jit_stats.regvars);
14692 g_print ("Locals stack size: %ld\n", mono_jit_stats.locals_stack_size);
14694 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
14695 g_print ("Delegates created: %ld\n", mono_stats.delegate_creations);
14696 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
14697 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
14698 g_print ("Generic vtables: %ld\n", mono_stats.generic_vtable_count);
14699 g_print ("Methods: %ld\n", mono_stats.method_count);
14700 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
14701 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
14702 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
14704 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
14705 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
14706 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
14707 mono_stats.inflated_method_count);
14708 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
14709 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
14710 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
14712 g_print ("Sharable generic methods: %ld\n", mono_stats.generics_sharable_methods);
14713 g_print ("Unsharable generic methods: %ld\n", mono_stats.generics_unsharable_methods);
14714 g_print ("Shared generic methods: %ld\n", mono_stats.generics_shared_methods);
14716 g_print ("Dynamic code allocs: %ld\n", mono_stats.dynamic_code_alloc_count);
14717 g_print ("Dynamic code bytes: %ld\n", mono_stats.dynamic_code_bytes_count);
14718 g_print ("Dynamic code frees: %ld\n", mono_stats.dynamic_code_frees_count);
14720 g_print ("IMT tables size: %ld\n", mono_stats.imt_tables_size);
14721 g_print ("IMT number of tables: %ld\n", mono_stats.imt_number_of_tables);
14722 g_print ("IMT number of methods: %ld\n", mono_stats.imt_number_of_methods);
14723 g_print ("IMT used slots: %ld\n", mono_stats.imt_used_slots);
14724 g_print ("IMT colliding slots: %ld\n", mono_stats.imt_slots_with_collisions);
14725 g_print ("IMT max collisions: %ld\n", mono_stats.imt_max_collisions_in_slot);
14726 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
14727 g_print ("IMT thunks size: %ld\n", mono_stats.imt_thunks_size);
14729 g_print ("JIT info table inserts: %ld\n", mono_stats.jit_info_table_insert_count);
14730 g_print ("JIT info table removes: %ld\n", mono_stats.jit_info_table_remove_count);
14731 g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
14733 g_print ("Hazardous pointers: %ld\n", mono_stats.hazardous_pointer_count);
14734 #ifdef HAVE_SGEN_GC
14735 g_print ("Minor GC collections: %ld\n", mono_stats.minor_gc_count);
14736 g_print ("Major GC collections: %ld\n", mono_stats.major_gc_count);
14737 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats.minor_gc_time_usecs / 1000.0);
14738 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats.major_gc_time_usecs / 1000.0);
14740 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
14741 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
14742 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
14743 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
14744 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
14745 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
14746 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
14748 if (debug_options.collect_pagefault_stats) {
14749 g_print ("AOT pagefaults : %d\n", mono_aot_get_n_pagefaults ());
14752 g_free (mono_jit_stats.max_ratio_method);
14753 mono_jit_stats.max_ratio_method = NULL;
14754 g_free (mono_jit_stats.biggest_method);
14755 mono_jit_stats.biggest_method = NULL;
14760 mini_cleanup (MonoDomain *domain)
14762 #ifdef HAVE_LINUX_RTC_H
14764 enable_rtc_timer (FALSE);
14768 * mono_runtime_cleanup() and mono_domain_finalize () need to
14769 * be called early since they need the execution engine still
14770 * fully working (mono_domain_finalize may invoke managed finalizers
14771 * and mono_runtime_cleanup will wait for other threads to finish).
14773 mono_domain_finalize (domain, 2000);
14775 /* This accesses metadata so needs to be called before runtime shutdown */
14776 print_jit_stats ();
14778 mono_runtime_cleanup (domain);
14780 mono_profiler_shutdown ();
14782 mono_icall_cleanup ();
14784 mono_runtime_cleanup_handlers ();
14786 mono_domain_free (domain, TRUE);
14788 mono_debugger_cleanup ();
14790 mono_trampolines_cleanup ();
14792 if (!mono_dont_free_global_codeman)
14793 mono_code_manager_destroy (global_codeman);
14794 g_hash_table_destroy (jit_icall_name_hash);
14795 g_free (emul_opcode_map);
14797 mono_arch_cleanup ();
14801 mono_trace_cleanup ();
14803 mono_counters_dump (-1, stdout);
14805 if (mono_inject_async_exc_method)
14806 mono_method_desc_free (mono_inject_async_exc_method);
14808 TlsFree(mono_jit_tls_id);
14810 DeleteCriticalSection (&jit_mutex);
14812 DeleteCriticalSection (&mono_delegate_section);
14816 mono_set_defaults (int verbose_level, guint32 opts)
14818 mini_verbose = verbose_level;
14819 default_opt = opts;
14820 default_opt_set = TRUE;
14824 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
14826 GHashTable *assemblies = (GHashTable*)user_data;
14827 MonoImage *image = mono_assembly_get_image (ass);
14828 MonoMethod *method, *invoke;
14831 if (g_hash_table_lookup (assemblies, ass))
14834 g_hash_table_insert (assemblies, ass, ass);
14836 if (mini_verbose > 0)
14837 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
14839 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
14840 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
14841 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
14845 if (mini_verbose > 1) {
14846 char * desc = mono_method_full_name (method, TRUE);
14847 g_print ("Compiling %d %s\n", count, desc);
14850 mono_compile_method (method);
14851 if (strcmp (method->name, "Finalize") == 0) {
14852 invoke = mono_marshal_get_runtime_invoke (method);
14853 mono_compile_method (invoke);
14855 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
14856 invoke = mono_marshal_get_remoting_invoke_with_check (method);
14857 mono_compile_method (invoke);
14861 /* Load and precompile referenced assemblies as well */
14862 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
14863 mono_assembly_load_reference (image, i);
14864 if (image->references [i])
14865 mono_precompile_assembly (image->references [i], assemblies);
14869 void mono_precompile_assemblies ()
14871 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
14873 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
14875 g_hash_table_destroy (assemblies);