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/rawbuffer.h>
59 #include <mono/metadata/security-core-clr.h>
60 #include <mono/metadata/verify.h>
61 #include <mono/metadata/verify-internals.h>
62 #include <mono/metadata/mempool-internals.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 GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(opcode) do { \
119 if (method->klass->valuetype) \
120 GENERIC_SHARING_FAILURE ((opcode)); \
122 #define GET_RGCTX(rgctx, context_used) do { \
123 MonoInst *this = NULL; \
124 g_assert (context_used); \
125 GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(*ip); \
126 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) && \
127 !((context_used) & MONO_GENERIC_CONTEXT_USED_METHOD)) \
128 NEW_ARGLOAD (cfg, this, 0); \
129 (rgctx) = get_runtime_generic_context (cfg, method, (context_used), this, ip); \
133 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
135 static void setup_stat_profiler (void);
136 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
137 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
138 static gpointer mono_jit_compile_method (MonoMethod *method);
139 inline static int mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip);
141 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
142 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
144 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
146 int mono_method_to_ir2 (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
147 MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
148 guint inline_offset, gboolean is_virtual_call);
150 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
151 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
152 guint inline_offset, gboolean is_virtual_call);
154 #ifdef MONO_ARCH_SOFT_FLOAT
156 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip);
159 /* helper methods signature */
160 /* FIXME: Make these static again */
161 MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
162 MonoMethodSignature *helper_sig_domain_get = NULL;
163 MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
164 MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
166 static guint32 default_opt = 0;
167 static gboolean default_opt_set = FALSE;
169 guint32 mono_jit_tls_id = -1;
171 #ifdef HAVE_KW_THREAD
172 static __thread gpointer mono_jit_tls MONO_TLS_FAST;
175 MonoTraceSpec *mono_jit_trace_calls = NULL;
176 gboolean mono_break_on_exc = FALSE;
177 gboolean mono_compile_aot = FALSE;
178 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
179 gboolean mono_aot_only = FALSE;
180 /* Whenever to use IMT */
181 #ifdef MONO_ARCH_HAVE_IMT
182 gboolean mono_use_imt = TRUE;
184 gboolean mono_use_imt = FALSE;
186 MonoMethodDesc *mono_inject_async_exc_method = NULL;
187 int mono_inject_async_exc_pos;
188 MonoMethodDesc *mono_break_at_bb_method = NULL;
189 int mono_break_at_bb_bb_num;
191 static int mini_verbose = 0;
193 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
194 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
195 static CRITICAL_SECTION jit_mutex;
197 static MonoCodeManager *global_codeman = NULL;
199 /* FIXME: Make this static again */
200 GHashTable *jit_icall_name_hash = NULL;
202 static MonoDebugOptions debug_options;
204 #ifdef VALGRIND_JIT_REGISTER_MAP
205 static int valgrind_register = 0;
209 * Table written to by the debugger with a 1-based index into the
210 * mono_breakpoint_info table, which contains changes made to
211 * the JIT instructions by the debugger.
214 mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
216 /* Whenever to check for pending exceptions in managed-to-native wrappers */
217 gboolean check_for_pending_exc = TRUE;
219 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
220 gboolean disable_vtypes_in_regs = FALSE;
223 /* Define this here, since many files reference it */
224 const guint8 mono_burg_arity [MBMAX_OPCODES] = {
229 mono_running_on_valgrind (void)
231 #ifdef HAVE_VALGRIND_MEMCHECK_H
232 if (RUNNING_ON_VALGRIND){
233 #ifdef VALGRIND_JIT_REGISTER_MAP
234 valgrind_register = TRUE;
250 find_tramp (gpointer key, gpointer value, gpointer user_data)
252 FindTrampUserData *ud = (FindTrampUserData*)user_data;
255 ud->method = (MonoMethod*)key;
259 G_GNUC_UNUSED static char*
260 get_method_from_ip (void *ip)
265 MonoDomain *domain = mono_domain_get ();
266 MonoDebugSourceLocation *location;
267 FindTrampUserData user_data;
269 ji = mono_jit_info_table_find (domain, ip);
272 user_data.method = NULL;
273 mono_domain_lock (domain);
274 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
275 mono_domain_unlock (domain);
276 if (user_data.method) {
277 char *mname = mono_method_full_name (user_data.method, TRUE);
278 res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
285 method = mono_method_full_name (ji->method, TRUE);
286 /* FIXME: unused ? */
287 location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
289 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);
291 mono_debug_free_source_location (location);
299 * @ip: an instruction pointer address
301 * This method is used from a debugger to get the name of the
302 * method at address @ip. This routine is typically invoked from
303 * a debugger like this:
305 * (gdb) print mono_pmip ($pc)
307 * Returns: the name of the method at address @ip.
312 return get_method_from_ip (ip);
316 * mono_print_method_from_ip
317 * @ip: an instruction pointer address
319 * This method is used from a debugger to get the name of the
320 * method at address @ip.
322 * This prints the name of the method at address @ip in the standard
323 * output. Unlike mono_pmip which returns a string, this routine
324 * prints the value on the standard output.
327 mono_print_method_from_ip (void *ip)
331 MonoDebugSourceLocation *source;
332 MonoDomain *domain = mono_domain_get ();
333 FindTrampUserData user_data;
335 ji = mono_jit_info_table_find (domain, ip);
338 user_data.method = NULL;
339 mono_domain_lock (domain);
340 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
341 mono_domain_unlock (domain);
342 if (user_data.method) {
343 char *mname = mono_method_full_name (user_data.method, TRUE);
344 printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
348 g_print ("No method at %p\n", ip);
351 method = mono_method_full_name (ji->method, TRUE);
352 source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
354 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);
357 g_print ("%s:%d\n", source->source_file, source->row);
359 mono_debug_free_source_location (source);
364 * mono_method_same_domain:
366 * Determine whenever two compiled methods are in the same domain, thus
367 * the address of the callee can be embedded in the caller.
369 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
371 if (!caller || !callee)
375 * If the call was made from domain-neutral to domain-specific
376 * code, we can't patch the call site.
378 if (caller->domain_neutral && !callee->domain_neutral)
381 if ((caller->method->klass == mono_defaults.appdomain_class) &&
382 (strstr (caller->method->name, "InvokeInDomain"))) {
383 /* The InvokeInDomain methods change the current appdomain */
391 * mono_global_codeman_reserve:
393 * Allocate code memory from the global code manager.
395 void *mono_global_codeman_reserve (int size)
400 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
402 if (!global_codeman) {
403 /* This can happen during startup */
404 global_codeman = mono_code_manager_new ();
405 return mono_code_manager_reserve (global_codeman, size);
409 ptr = mono_code_manager_reserve (global_codeman, size);
416 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
418 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
425 #define MONO_INIT_VARINFO(vi,id) do { \
426 (vi)->range.first_use.pos.bid = 0xffff; \
431 //#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
432 #define UNVERIFIED do { if (debug_options.break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
435 * Basic blocks have two numeric identifiers:
436 * dfn: Depth First Number
437 * block_num: unique ID assigned at bblock creation
439 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
440 #define ADD_BBLOCK(cfg,b) do { \
441 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
442 (b)->block_num = cfg->num_bblocks++; \
443 (b)->real_offset = real_offset; \
446 #define GET_BBLOCK(cfg,tblock,ip) do { \
447 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
449 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
450 (tblock) = NEW_BBLOCK (cfg); \
451 (tblock)->cil_code = (ip); \
452 ADD_BBLOCK (cfg, (tblock)); \
456 #define CHECK_BBLOCK(target,ip,tblock) do { \
457 if ((target) < (ip) && !(tblock)->code) { \
458 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
459 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)); \
463 #define NEW_ICONST(cfg,dest,val) do { \
464 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
465 (dest)->opcode = OP_ICONST; \
466 (dest)->inst_c0 = (val); \
467 (dest)->type = STACK_I4; \
470 #define NEW_PCONST(cfg,dest,val) do { \
471 MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
472 (dest)->inst_p0 = (val); \
473 (dest)->type = STACK_PTR; \
477 #ifdef MONO_ARCH_NEED_GOT_VAR
479 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
480 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
481 (dest)->inst_left = (gpointer)(el1); \
482 (dest)->inst_right = (gpointer)(el2); \
485 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
486 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
487 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
488 if (cfg->compile_aot) { \
489 MonoInst *group, *got_var, *got_loc; \
490 got_loc = mono_get_got_var (cfg); \
491 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
492 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
493 (dest)->inst_p0 = got_var; \
494 (dest)->inst_p1 = group; \
496 (dest)->inst_p0 = (cons); \
497 (dest)->inst_i1 = (gpointer)(patch_type); \
499 (dest)->type = STACK_PTR; \
502 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
503 MonoInst *group, *got_var, *got_loc; \
504 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
505 got_loc = mono_get_got_var (cfg); \
506 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
507 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
508 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
509 (dest)->inst_p0 = got_var; \
510 (dest)->inst_p1 = group; \
511 (dest)->type = (stack_type); \
512 (dest)->klass = (stack_class); \
517 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
518 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
519 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
520 (dest)->inst_p0 = (cons); \
521 (dest)->inst_i1 = (gpointer)(patch_type); \
522 (dest)->type = STACK_PTR; \
525 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
526 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
527 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
528 (dest)->inst_p1 = (gpointer)(patch_type); \
529 (dest)->type = (stack_type); \
530 (dest)->klass = (stack_class); \
535 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
537 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
539 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
541 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
543 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
545 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
547 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
549 #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)
551 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
553 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
554 if (cfg->compile_aot) { \
555 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
557 NEW_PCONST (cfg, args [0], (entry).blob); \
561 #define NEW_DOMAINCONST(cfg,dest) do { \
562 if (cfg->opt & MONO_OPT_SHARED) { \
563 /* avoid depending on undefined C behavior in sequence points */ \
564 MonoInst* __domain_var = mono_get_domainvar (cfg); \
565 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
567 NEW_PCONST (cfg, dest, (cfg)->domain); \
571 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
573 #define NEW_ARGLOAD(cfg,dest,num) do { \
574 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
575 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
576 (dest)->ssa_op = MONO_SSA_LOAD; \
577 (dest)->inst_i0 = arg_array [(num)]; \
578 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
579 type_to_eval_stack_type ((cfg), param_types [(num)], (dest)); \
580 (dest)->klass = (dest)->inst_i0->klass; \
583 #define NEW_LOCLOAD(cfg,dest,num) do { \
584 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
585 (dest)->ssa_op = MONO_SSA_LOAD; \
586 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
587 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
588 type_to_eval_stack_type ((cfg), header->locals [(num)], (dest)); \
589 (dest)->klass = (dest)->inst_i0->klass; \
592 #define NEW_LOCLOADA(cfg,dest,num) do { \
593 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
594 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
595 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
596 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
597 (dest)->type = STACK_MP; \
598 (dest)->klass = (dest)->inst_i0->klass; \
599 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
600 (cfg)->disable_ssa = TRUE; \
603 #define NEW_RETLOADA(cfg,dest) do { \
604 if (cfg->vret_addr) { \
605 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
606 (dest)->ssa_op = MONO_SSA_LOAD; \
607 (dest)->inst_i0 = cfg->vret_addr; \
608 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
609 (dest)->type = STACK_MP; \
610 (dest)->klass = (dest)->inst_i0->klass; \
612 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
613 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
614 (dest)->inst_i0 = (cfg)->ret; \
615 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
616 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
617 (dest)->type = STACK_MP; \
618 (dest)->klass = (dest)->inst_i0->klass; \
619 (cfg)->disable_ssa = TRUE; \
623 #define NEW_ARGLOADA(cfg,dest,num) do { \
624 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
625 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
626 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
627 (dest)->inst_i0 = arg_array [(num)]; \
628 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
629 (dest)->type = STACK_MP; \
630 (dest)->klass = (dest)->inst_i0->klass; \
631 (cfg)->disable_ssa = TRUE; \
634 #define NEW_TEMPLOAD(cfg,dest,num) do { \
635 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
636 (dest)->ssa_op = MONO_SSA_LOAD; \
637 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
638 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
639 type_to_eval_stack_type ((cfg), (dest)->inst_i0->inst_vtype, (dest)); \
640 (dest)->klass = (dest)->inst_i0->klass; \
643 #define NEW_TEMPLOADA(cfg,dest,num) do { \
644 MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
645 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
646 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
647 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
648 (dest)->type = STACK_MP; \
649 (dest)->klass = (dest)->inst_i0->klass; \
650 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
651 (cfg)->disable_ssa = TRUE; \
654 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
655 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
656 (dest)->inst_left = addr; \
657 (dest)->opcode = mini_type_to_ldind ((cfg), vtype); \
658 type_to_eval_stack_type ((cfg), vtype, (dest)); \
659 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
662 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
663 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
664 (dest)->inst_i0 = addr; \
665 (dest)->opcode = mini_type_to_stind ((cfg), vtype); \
666 (dest)->inst_i1 = (value); \
667 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
670 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
671 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
672 (dest)->ssa_op = MONO_SSA_STORE; \
673 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
674 (dest)->opcode = mini_type_to_stind ((cfg), (dest)->inst_i0->inst_vtype); \
675 (dest)->inst_i1 = (inst); \
676 (dest)->klass = (dest)->inst_i0->klass; \
679 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
680 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
681 (dest)->opcode = mini_type_to_stind ((cfg), header->locals [(num)]); \
682 (dest)->ssa_op = MONO_SSA_STORE; \
683 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
684 (dest)->inst_i1 = (inst); \
685 (dest)->klass = (dest)->inst_i0->klass; \
688 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
689 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
690 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
691 (dest)->opcode = mini_type_to_stind ((cfg), param_types [(num)]); \
692 (dest)->ssa_op = MONO_SSA_STORE; \
693 (dest)->inst_i0 = arg_array [(num)]; \
694 (dest)->inst_i1 = (inst); \
695 (dest)->klass = (dest)->inst_i0->klass; \
698 #define NEW_MEMCPY(cfg,dest,dst,src,memcpy_size,memcpy_align) do { \
699 MONO_INST_NEW (cfg, dest, OP_MEMCPY); \
700 (dest)->inst_left = (dst); \
701 (dest)->inst_right = (src); \
702 (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
703 (dest)->backend.memcpy_args->size = (memcpy_size); \
704 (dest)->backend.memcpy_args->align = (memcpy_align); \
707 #define NEW_MEMSET(cfg,dest,dst,imm,memcpy_size,memcpy_align) do { \
708 MONO_INST_NEW (cfg, dest, OP_MEMSET); \
709 (dest)->inst_left = (dst); \
710 (dest)->inst_imm = (imm); \
711 (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
712 (dest)->backend.memcpy_args->size = (memcpy_size); \
713 (dest)->backend.memcpy_args->align = (memcpy_align); \
716 #define NEW_DUMMY_USE(cfg,dest,load) do { \
717 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
718 (dest)->inst_left = (load); \
721 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
722 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_STORE); \
723 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
724 (dest)->klass = (dest)->inst_i0->klass; \
727 #define ADD_BINOP(op) do { \
728 MONO_INST_NEW (cfg, ins, (op)); \
730 ins->inst_i0 = sp [0]; \
731 ins->inst_i1 = sp [1]; \
733 type_from_op (ins); \
735 /* Have to insert a widening op */ \
736 /* FIXME: Need to add many more cases */ \
737 if (ins->inst_i0->type == STACK_PTR && ins->inst_i1->type == STACK_I4) { \
739 MONO_INST_NEW (cfg, widen, CEE_CONV_I); \
740 widen->inst_i0 = ins->inst_i1; \
741 ins->inst_i1 = widen; \
745 #define ADD_UNOP(op) do { \
746 MONO_INST_NEW (cfg, ins, (op)); \
748 ins->inst_i0 = sp [0]; \
750 type_from_op (ins); \
754 #define ADD_BINCOND(next_block) do { \
757 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
758 cmp->inst_i0 = sp [0]; \
759 cmp->inst_i1 = sp [1]; \
760 cmp->cil_code = ins->cil_code; \
761 type_from_op (cmp); \
763 ins->inst_i0 = cmp; \
764 MONO_ADD_INS (bblock, ins); \
765 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
766 GET_BBLOCK (cfg, tblock, target); \
767 link_bblock (cfg, bblock, tblock); \
768 ins->inst_true_bb = tblock; \
769 CHECK_BBLOCK (target, ip, tblock); \
770 if ((next_block)) { \
771 link_bblock (cfg, bblock, (next_block)); \
772 ins->inst_false_bb = (next_block); \
773 start_new_bblock = 1; \
775 GET_BBLOCK (cfg, tblock, ip); \
776 link_bblock (cfg, bblock, tblock); \
777 ins->inst_false_bb = tblock; \
778 start_new_bblock = 2; \
782 /* FIXME: handle float, long ... */
783 #define ADD_UNCOND(istrue) do { \
786 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
787 cmp->inst_i0 = sp [0]; \
788 switch (cmp->inst_i0->type) { \
790 cmp->inst_i1 = zero_int64; break; \
792 cmp->inst_i1 = zero_r8; break; \
795 cmp->inst_i1 = zero_ptr; break; \
797 cmp->inst_i1 = zero_obj; break; \
799 cmp->inst_i1 = zero_int32; \
801 cmp->cil_code = ins->cil_code; \
802 type_from_op (cmp); \
804 ins->inst_i0 = cmp; \
805 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
806 MONO_ADD_INS (bblock, ins); \
807 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
808 GET_BBLOCK (cfg, tblock, target); \
809 link_bblock (cfg, bblock, tblock); \
810 ins->inst_true_bb = tblock; \
811 CHECK_BBLOCK (target, ip, tblock); \
812 GET_BBLOCK (cfg, tblock, ip); \
813 link_bblock (cfg, bblock, tblock); \
814 ins->inst_false_bb = tblock; \
815 start_new_bblock = 2; \
818 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
819 MONO_INST_NEW ((cfg), (dest), CEE_LDELEMA); \
820 (dest)->inst_left = (sp) [0]; \
821 (dest)->inst_right = (sp) [1]; \
822 (dest)->type = STACK_MP; \
823 (dest)->klass = (k); \
824 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
827 #define NEW_GROUP(cfg,dest,el1,el2) do { \
828 MONO_INST_NEW ((cfg), (dest), OP_GROUP); \
829 (dest)->inst_left = (el1); \
830 (dest)->inst_right = (el2); \
835 compare_bblock (gconstpointer a, gconstpointer b)
837 const MonoBasicBlock *b1 = a;
838 const MonoBasicBlock *b2 = b;
840 return b2->cil_code - b1->cil_code;
845 * link_bblock: Links two basic blocks
847 * links two basic blocks in the control flow graph, the 'from'
848 * argument is the starting block and the 'to' argument is the block
849 * the control flow ends to after 'from'.
852 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
854 MonoBasicBlock **newa;
858 if (from->cil_code) {
860 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
862 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
865 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
867 g_print ("edge from entry to exit\n");
871 for (i = 0; i < from->out_count; ++i) {
872 if (to == from->out_bb [i]) {
878 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
879 for (i = 0; i < from->out_count; ++i) {
880 newa [i] = from->out_bb [i];
888 for (i = 0; i < to->in_count; ++i) {
889 if (from == to->in_bb [i]) {
895 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
896 for (i = 0; i < to->in_count; ++i) {
897 newa [i] = to->in_bb [i];
906 * mono_unlink_bblock:
908 * Unlink two basic blocks.
911 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
917 for (i = 0; i < from->out_count; ++i) {
918 if (to == from->out_bb [i]) {
925 for (i = 0; i < from->out_count; ++i) {
926 if (from->out_bb [i] != to)
927 from->out_bb [pos ++] = from->out_bb [i];
929 g_assert (pos == from->out_count - 1);
934 for (i = 0; i < to->in_count; ++i) {
935 if (from == to->in_bb [i]) {
942 for (i = 0; i < to->in_count; ++i) {
943 if (to->in_bb [i] != from)
944 to->in_bb [pos ++] = to->in_bb [i];
946 g_assert (pos == to->in_count - 1);
952 * mono_bblocks_linked:
954 * Return whenever BB1 and BB2 are linked in the CFG.
957 mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
961 for (i = 0; i < bb1->out_count; ++i) {
962 if (bb1->out_bb [i] == bb2)
970 * mono_find_block_region:
972 * We mark each basic block with a region ID. We use that to avoid BB
973 * optimizations when blocks are in different regions.
976 * A region token that encodes where this region is, and information
977 * about the clause owner for this block.
979 * The region encodes the try/catch/filter clause that owns this block
980 * as well as the type. -1 is a special value that represents a block
981 * that is in none of try/catch/filter.
984 mono_find_block_region (MonoCompile *cfg, int offset)
986 MonoMethod *method = cfg->method;
987 MonoMethodHeader *header = mono_method_get_header (method);
988 MonoExceptionClause *clause;
991 /* first search for handlers and filters */
992 for (i = 0; i < header->num_clauses; ++i) {
993 clause = &header->clauses [i];
994 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
995 (offset < (clause->handler_offset)))
996 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
998 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
999 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1000 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
1001 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
1002 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
1003 else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1004 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
1008 /* search the try blocks */
1009 for (i = 0; i < header->num_clauses; ++i) {
1010 clause = &header->clauses [i];
1011 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1012 return ((i + 1) << 8) | clause->flags;
1019 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
1021 MonoMethod *method = cfg->method;
1022 MonoMethodHeader *header = mono_method_get_header (method);
1023 MonoExceptionClause *clause;
1024 MonoBasicBlock *handler;
1028 for (i = 0; i < header->num_clauses; ++i) {
1029 clause = &header->clauses [i];
1030 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
1031 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
1032 if (clause->flags == type) {
1033 handler = cfg->cil_offset_to_bb [clause->handler_offset];
1035 res = g_list_append (res, handler);
1043 mono_find_spvar_for_region (MonoCompile *cfg, int region)
1045 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1049 mono_create_spvar_for_region (MonoCompile *cfg, int region)
1053 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1057 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1058 /* prevent it from being register allocated */
1059 var->flags |= MONO_INST_INDIRECT;
1061 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
1065 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
1067 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1071 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
1075 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1079 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
1080 /* prevent it from being register allocated */
1081 var->flags |= MONO_INST_INDIRECT;
1083 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
1089 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
1093 array [*dfn] = start;
1094 /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
1095 for (i = 0; i < start->out_count; ++i) {
1096 if (start->out_bb [i]->dfn)
1099 start->out_bb [i]->dfn = *dfn;
1100 start->out_bb [i]->df_parent = start;
1101 array [*dfn] = start->out_bb [i];
1102 df_visit (start->out_bb [i], dfn, array);
1106 static MonoBasicBlock*
1107 find_previous (MonoBasicBlock **bblocks, guint32 n_bblocks, MonoBasicBlock *start, const guchar *code)
1109 MonoBasicBlock *best = start;
1112 for (i = 0; i < n_bblocks; ++i) {
1114 MonoBasicBlock *bb = bblocks [i];
1116 if (bb->cil_code && bb->cil_code < code && bb->cil_code > best->cil_code)
1125 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
1134 * FIXME: take into account all the details:
1135 * second may have been the target of more than one bblock
1137 second->out_count = first->out_count;
1138 second->out_bb = first->out_bb;
1140 for (i = 0; i < first->out_count; ++i) {
1141 bb = first->out_bb [i];
1142 for (j = 0; j < bb->in_count; ++j) {
1143 if (bb->in_bb [j] == first)
1144 bb->in_bb [j] = second;
1148 first->out_count = 0;
1149 first->out_bb = NULL;
1150 link_bblock (cfg, first, second);
1152 second->last_ins = first->last_ins;
1154 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
1155 MONO_BB_FOR_EACH_INS (first, inst) {
1156 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
1157 g_print ("found %p: %s", inst->next->cil_code, code);
1159 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
1160 second->code = inst->next;
1162 first->last_ins = inst;
1163 second->next_bb = first->next_bb;
1164 first->next_bb = second;
1168 if (!second->code) {
1169 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
1175 mono_reverse_branch_op (guint32 opcode)
1177 static const int reverse_map [] = {
1178 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
1179 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
1181 static const int reverse_fmap [] = {
1182 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
1183 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
1185 static const int reverse_lmap [] = {
1186 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
1187 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
1189 static const int reverse_imap [] = {
1190 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
1191 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
1194 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
1195 opcode = reverse_map [opcode - CEE_BEQ];
1196 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
1197 opcode = reverse_fmap [opcode - OP_FBEQ];
1198 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
1199 opcode = reverse_lmap [opcode - OP_LBEQ];
1200 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
1201 opcode = reverse_imap [opcode - OP_IBEQ];
1203 g_assert_not_reached ();
1209 mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
1212 return OP_STORE_MEMBASE_REG;
1215 switch (type->type) {
1218 case MONO_TYPE_BOOLEAN:
1219 return OP_STOREI1_MEMBASE_REG;
1222 case MONO_TYPE_CHAR:
1223 return OP_STOREI2_MEMBASE_REG;
1226 return OP_STOREI4_MEMBASE_REG;
1230 case MONO_TYPE_FNPTR:
1231 return OP_STORE_MEMBASE_REG;
1232 case MONO_TYPE_CLASS:
1233 case MONO_TYPE_STRING:
1234 case MONO_TYPE_OBJECT:
1235 case MONO_TYPE_SZARRAY:
1236 case MONO_TYPE_ARRAY:
1237 return OP_STORE_MEMBASE_REG;
1240 return OP_STOREI8_MEMBASE_REG;
1242 return OP_STORER4_MEMBASE_REG;
1244 return OP_STORER8_MEMBASE_REG;
1245 case MONO_TYPE_VALUETYPE:
1246 if (type->data.klass->enumtype) {
1247 type = type->data.klass->enum_basetype;
1250 return OP_STOREV_MEMBASE;
1251 case MONO_TYPE_TYPEDBYREF:
1252 return OP_STOREV_MEMBASE;
1253 case MONO_TYPE_GENERICINST:
1254 type = &type->data.generic_class->container_class->byval_arg;
1257 case MONO_TYPE_MVAR:
1258 /* FIXME: all the arguments must be references for now,
1259 * later look inside cfg and see if the arg num is
1260 * really a reference
1262 g_assert (cfg->generic_sharing_context);
1263 return OP_STORE_MEMBASE_REG;
1265 g_error ("unknown type 0x%02x in type_to_store_membase", type->type);
1271 mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
1274 return OP_LOAD_MEMBASE;
1276 switch (mono_type_get_underlying_type (type)->type) {
1278 return OP_LOADI1_MEMBASE;
1280 case MONO_TYPE_BOOLEAN:
1281 return OP_LOADU1_MEMBASE;
1283 return OP_LOADI2_MEMBASE;
1285 case MONO_TYPE_CHAR:
1286 return OP_LOADU2_MEMBASE;
1288 return OP_LOADI4_MEMBASE;
1290 return OP_LOADU4_MEMBASE;
1294 case MONO_TYPE_FNPTR:
1295 return OP_LOAD_MEMBASE;
1296 case MONO_TYPE_CLASS:
1297 case MONO_TYPE_STRING:
1298 case MONO_TYPE_OBJECT:
1299 case MONO_TYPE_SZARRAY:
1300 case MONO_TYPE_ARRAY:
1301 return OP_LOAD_MEMBASE;
1304 return OP_LOADI8_MEMBASE;
1306 return OP_LOADR4_MEMBASE;
1308 return OP_LOADR8_MEMBASE;
1309 case MONO_TYPE_VALUETYPE:
1310 case MONO_TYPE_TYPEDBYREF:
1311 return OP_LOADV_MEMBASE;
1312 case MONO_TYPE_GENERICINST:
1313 if (mono_type_generic_inst_is_valuetype (type))
1314 return OP_LOADV_MEMBASE;
1316 return OP_LOAD_MEMBASE;
1319 case MONO_TYPE_MVAR:
1320 /* FIXME: all the arguments must be references for now,
1321 * later look inside cfg and see if the arg num is
1322 * really a reference
1324 g_assert (cfg->generic_sharing_context);
1325 return OP_LOAD_MEMBASE;
1327 g_error ("unknown type 0x%02x in type_to_load_membase", type->type);
1332 #ifdef MONO_ARCH_SOFT_FLOAT
1334 condbr_to_fp_br (int opcode)
1337 case CEE_BEQ: return OP_FBEQ;
1338 case CEE_BGE: return OP_FBGE;
1339 case CEE_BGT: return OP_FBGT;
1340 case CEE_BLE: return OP_FBLE;
1341 case CEE_BLT: return OP_FBLT;
1342 case CEE_BNE_UN: return OP_FBNE_UN;
1343 case CEE_BGE_UN: return OP_FBGE_UN;
1344 case CEE_BGT_UN: return OP_FBGT_UN;
1345 case CEE_BLE_UN: return OP_FBLE_UN;
1346 case CEE_BLT_UN: return OP_FBLT_UN;
1348 g_assert_not_reached ();
1354 * Returns the type used in the eval stack when @type is loaded.
1355 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1358 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
1362 inst->klass = klass = mono_class_from_mono_type (type);
1364 inst->type = STACK_MP;
1369 switch (type->type) {
1370 case MONO_TYPE_VOID:
1371 inst->type = STACK_INV;
1375 case MONO_TYPE_BOOLEAN:
1378 case MONO_TYPE_CHAR:
1381 inst->type = STACK_I4;
1386 case MONO_TYPE_FNPTR:
1387 inst->type = STACK_PTR;
1389 case MONO_TYPE_CLASS:
1390 case MONO_TYPE_STRING:
1391 case MONO_TYPE_OBJECT:
1392 case MONO_TYPE_SZARRAY:
1393 case MONO_TYPE_ARRAY:
1394 inst->type = STACK_OBJ;
1398 inst->type = STACK_I8;
1402 inst->type = STACK_R8;
1404 case MONO_TYPE_VALUETYPE:
1405 if (type->data.klass->enumtype) {
1406 type = type->data.klass->enum_basetype;
1409 inst->klass = klass;
1410 inst->type = STACK_VTYPE;
1413 case MONO_TYPE_TYPEDBYREF:
1414 inst->klass = mono_defaults.typed_reference_class;
1415 inst->type = STACK_VTYPE;
1417 case MONO_TYPE_GENERICINST:
1418 type = &type->data.generic_class->container_class->byval_arg;
1420 case MONO_TYPE_VAR :
1421 case MONO_TYPE_MVAR :
1422 /* FIXME: all the arguments must be references for now,
1423 * later look inside cfg and see if the arg num is
1424 * really a reference
1426 g_assert (cfg->generic_sharing_context);
1427 inst->type = STACK_OBJ;
1430 g_error ("unknown type 0x%02x in eval stack type", type->type);
1435 * The following tables are used to quickly validate the IL code in type_from_op ().
1438 bin_num_table [STACK_MAX] [STACK_MAX] = {
1439 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1440 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1441 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1442 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1443 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1444 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1445 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1446 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1451 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1454 /* reduce the size of this table */
1456 bin_int_table [STACK_MAX] [STACK_MAX] = {
1457 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1458 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1459 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1460 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1461 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1462 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1463 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1464 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1468 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1469 /* Inv i L p F & O vt */
1471 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
1472 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
1473 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
1474 {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
1475 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
1476 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
1477 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
1480 /* reduce the size of this table */
1482 shift_table [STACK_MAX] [STACK_MAX] = {
1483 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1484 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1485 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1486 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1487 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1488 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1489 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1490 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1494 * Tables to map from the non-specific opcode to the matching
1495 * type-specific opcode.
1497 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1498 static const guint16
1499 binops_op_map [STACK_MAX] = {
1500 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1503 /* handles from CEE_NEG to CEE_CONV_U8 */
1504 static const guint16
1505 unops_op_map [STACK_MAX] = {
1506 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1509 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1510 static const guint16
1511 ovfops_op_map [STACK_MAX] = {
1512 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
1515 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1516 static const guint16
1517 ovf2ops_op_map [STACK_MAX] = {
1518 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
1521 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1522 static const guint16
1523 ovf3ops_op_map [STACK_MAX] = {
1524 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
1527 /* handles from CEE_CEQ to CEE_CLT_UN */
1528 static const guint16
1529 ceqops_op_map [STACK_MAX] = {
1530 0, 0, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_LCEQ-OP_CEQ
1534 * Sets ins->type (the type on the eval stack) according to the
1535 * type of the opcode and the arguments to it.
1536 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1538 * FIXME: this function sets ins->type unconditionally in some cases, but
1539 * it should set it to invalid for some types (a conv.x on an object)
1542 type_from_op (MonoInst *ins) {
1543 switch (ins->opcode) {
1550 /* FIXME: check unverifiable args for STACK_MP */
1551 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1552 ins->opcode += binops_op_map [ins->type];
1559 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1560 ins->opcode += binops_op_map [ins->type];
1565 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1566 ins->opcode += binops_op_map [ins->type];
1570 /* FIXME: handle some specifics with ins->next->type */
1571 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1572 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))))
1573 ins->opcode = OP_LCOMPARE;
1576 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1577 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1584 ins->type = (bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] & 1) ? STACK_I4: STACK_INV;
1585 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1589 ins->type = neg_table [ins->inst_i0->type];
1590 ins->opcode += unops_op_map [ins->type];
1593 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1594 ins->type = ins->inst_i0->type;
1596 ins->type = STACK_INV;
1597 ins->opcode += unops_op_map [ins->type];
1603 ins->type = STACK_I4;
1604 ins->opcode += unops_op_map [ins->inst_i0->type];
1607 ins->type = STACK_R8;
1608 switch (ins->inst_i0->type) {
1613 ins->opcode = OP_LCONV_TO_R_UN;
1617 case CEE_CONV_OVF_I1:
1618 case CEE_CONV_OVF_U1:
1619 case CEE_CONV_OVF_I2:
1620 case CEE_CONV_OVF_U2:
1621 case CEE_CONV_OVF_I4:
1622 case CEE_CONV_OVF_U4:
1623 ins->type = STACK_I4;
1624 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1626 case CEE_CONV_OVF_I_UN:
1627 case CEE_CONV_OVF_U_UN:
1628 ins->type = STACK_PTR;
1629 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1631 case CEE_CONV_OVF_I1_UN:
1632 case CEE_CONV_OVF_I2_UN:
1633 case CEE_CONV_OVF_I4_UN:
1634 case CEE_CONV_OVF_U1_UN:
1635 case CEE_CONV_OVF_U2_UN:
1636 case CEE_CONV_OVF_U4_UN:
1637 ins->type = STACK_I4;
1638 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1641 ins->type = STACK_PTR;
1642 switch (ins->inst_i0->type) {
1647 #if SIZEOF_VOID_P == 8
1648 ins->opcode = OP_LCONV_TO_U;
1652 ins->opcode = OP_LCONV_TO_U;
1655 ins->opcode = OP_FCONV_TO_U;
1661 ins->type = STACK_I8;
1662 ins->opcode += unops_op_map [ins->inst_i0->type];
1664 case CEE_CONV_OVF_I8:
1665 case CEE_CONV_OVF_U8:
1666 ins->type = STACK_I8;
1667 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1669 case CEE_CONV_OVF_U8_UN:
1670 case CEE_CONV_OVF_I8_UN:
1671 ins->type = STACK_I8;
1672 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1676 ins->type = STACK_R8;
1677 ins->opcode += unops_op_map [ins->inst_i0->type];
1680 ins->type = STACK_R8;
1684 ins->type = STACK_I4;
1685 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1688 case CEE_CONV_OVF_I:
1689 case CEE_CONV_OVF_U:
1690 ins->type = STACK_PTR;
1691 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1694 case CEE_ADD_OVF_UN:
1696 case CEE_MUL_OVF_UN:
1698 case CEE_SUB_OVF_UN:
1699 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1700 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1701 if (ins->type == STACK_R8)
1702 ins->type = STACK_INV;
1705 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1712 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1715 /* map ldelem.x to the matching ldind.x opcode */
1717 ldelem_to_ldind [] = {
1731 /* map stelem.x to the matching stind.x opcode */
1733 stelem_to_stind [] = {
1745 #ifdef MONO_ARCH_SOFT_FLOAT
1747 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip)
1749 MonoInst *iargs [2];
1753 mono_emit_jit_icall (cfg, bblock, mono_fstore_r4, iargs, ip);
1757 handle_load_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, const unsigned char *ip)
1759 MonoInst *iargs [1];
1762 return mono_emit_jit_icall (cfg, bblock, mono_fload_r4, iargs, ip);
1765 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1766 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) { \
1768 NEW_LOCLOADA (cfg, (ins), (idx)); \
1769 temp = handle_load_float (cfg, bblock, (ins), (ip)); \
1770 NEW_TEMPLOAD (cfg, (ins), temp); \
1773 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1774 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) { \
1775 NEW_LOCLOADA (cfg, (ins), (idx)); \
1776 handle_store_float (cfg, bblock, (ins), *sp, (ip)); \
1777 MONO_INST_NEW (cfg, (ins), OP_NOP); \
1780 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1781 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1783 NEW_ARGLOADA (cfg, (ins), (idx)); \
1784 temp = handle_load_float (cfg, bblock, (ins), (ip)); \
1785 NEW_TEMPLOAD (cfg, (ins), temp); \
1788 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1789 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1790 NEW_ARGLOADA (cfg, (ins), (idx)); \
1791 handle_store_float (cfg, bblock, (ins), *sp, (ip)); \
1792 MONO_INST_NEW (cfg, (ins), OP_NOP); \
1796 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip) do { \
1797 if ((ins)->opcode == CEE_LDIND_R4) { \
1800 NEW_TEMPLOADA (cfg, (ins), (idx)); \
1801 temp = handle_load_float (cfg, (bblock), (ins), ip); \
1802 NEW_TEMPLOAD (cfg, (ins), (temp)); \
1806 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip) do { \
1807 if ((ins)->opcode == CEE_STIND_R4) { \
1809 NEW_TEMPLOADA (cfg, (ins), (idx)); \
1810 handle_store_float ((cfg), (bblock), (ins), (val), (ip)); \
1816 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1817 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1818 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip)
1819 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip)
1820 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip)
1821 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip)
1827 param_table [STACK_MAX] [STACK_MAX] = {
1832 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig)
1837 switch (args->type) {
1847 for (i = 0; i < sig->param_count; ++i) {
1848 switch (args [i].type) {
1852 if (!sig->params [i]->byref)
1856 if (sig->params [i]->byref)
1858 switch (sig->params [i]->type) {
1859 case MONO_TYPE_CLASS:
1860 case MONO_TYPE_STRING:
1861 case MONO_TYPE_OBJECT:
1862 case MONO_TYPE_SZARRAY:
1863 case MONO_TYPE_ARRAY:
1870 if (sig->params [i]->byref)
1872 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1881 /*if (!param_table [args [i].type] [sig->params [i]->type])
1889 mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
1891 if (cfg->generic_sharing_context && !type->byref) {
1892 /* FIXME: all the arguments must be references for now,
1893 * later look inside cfg and see if the arg num is
1894 * really a reference
1896 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1897 return CEE_LDIND_REF;
1899 return mono_type_to_ldind (type);
1903 mini_type_to_stind (MonoCompile* cfg, MonoType *type)
1905 if (cfg->generic_sharing_context && !type->byref) {
1906 /* FIXME: all the arguments must be references for now,
1907 * later look inside cfg and see if the arg num is
1908 * really a reference
1910 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1911 return CEE_STIND_REF;
1913 return mono_type_to_stind (type);
1917 mono_op_imm_to_op (int opcode)
1921 #if SIZEOF_VOID_P == 4
1937 #if SIZEOF_VOID_P == 4
1962 case OP_ISHR_UN_IMM:
1964 case OP_LSHR_UN_IMM:
1968 case OP_IDIV_UN_IMM:
1970 case OP_IREM_UN_IMM:
1975 #if SIZEOF_VOID_P == 4
1981 #if SIZEOF_VOID_P == 4
1998 case OP_COMPARE_IMM:
2000 case OP_ICOMPARE_IMM:
2002 case OP_LOCALLOC_IMM:
2005 printf ("%s\n", mono_inst_name (opcode));
2006 g_assert_not_reached ();
2012 * mono_decompose_op_imm:
2014 * Replace the OP_.._IMM INS with its non IMM variant.
2017 mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
2021 MONO_INST_NEW (cfg, temp, OP_ICONST);
2022 temp->inst_c0 = ins->inst_imm;
2023 temp->dreg = mono_regstate_next_int (cfg->rs);
2024 mono_bblock_insert_before_ins (bb, ins, temp);
2025 ins->opcode = mono_op_imm_to_op (ins->opcode);
2026 if (ins->opcode == OP_LOCALLOC)
2027 ins->sreg1 = temp->dreg;
2029 ins->sreg2 = temp->dreg;
2031 bb->max_vreg = MAX (bb->max_vreg, cfg->rs->next_vreg);
2035 * When we need a pointer to the current domain many times in a method, we
2036 * call mono_domain_get() once and we store the result in a local variable.
2037 * This function returns the variable that represents the MonoDomain*.
2039 inline static MonoInst *
2040 mono_get_domainvar (MonoCompile *cfg)
2042 if (!cfg->domainvar)
2043 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2044 return cfg->domainvar;
2048 * The got_var contains the address of the Global Offset Table when AOT
2051 inline static MonoInst *
2052 mono_get_got_var (MonoCompile *cfg)
2054 #ifdef MONO_ARCH_NEED_GOT_VAR
2055 if (!cfg->compile_aot)
2057 if (!cfg->got_var) {
2058 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2060 return cfg->got_var;
2067 mono_get_vtable_var (MonoCompile *cfg)
2069 g_assert (cfg->generic_sharing_context);
2071 if (!cfg->rgctx_var) {
2072 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2073 /* force the var to be stack allocated */
2074 cfg->rgctx_var->flags |= MONO_INST_INDIRECT;
2077 return cfg->rgctx_var;
2081 set_vreg_to_inst (MonoCompile *cfg, int vreg, MonoInst *inst)
2083 if (vreg >= cfg->vreg_to_inst_len) {
2084 MonoInst **tmp = cfg->vreg_to_inst;
2085 int size = cfg->vreg_to_inst_len;
2087 while (vreg >= cfg->vreg_to_inst_len)
2088 cfg->vreg_to_inst_len = cfg->vreg_to_inst_len ? cfg->vreg_to_inst_len * 2 : 32;
2089 cfg->vreg_to_inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * cfg->vreg_to_inst_len);
2091 memcpy (cfg->vreg_to_inst, tmp, size * sizeof (MonoInst*));
2093 cfg->vreg_to_inst [vreg] = inst;
2096 #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)))
2097 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
2100 mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode, int vreg)
2103 int num = cfg->num_varinfo;
2106 if ((num + 1) >= cfg->varinfo_count) {
2107 int orig_count = cfg->varinfo_count;
2108 cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 64;
2109 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
2110 cfg->vars = (MonoMethodVar *)g_realloc (cfg->vars, sizeof (MonoMethodVar) * cfg->varinfo_count);
2111 memset (&cfg->vars [orig_count], 0, (cfg->varinfo_count - orig_count) * sizeof (MonoMethodVar));
2114 mono_jit_stats.allocate_var++;
2116 MONO_INST_NEW (cfg, inst, opcode);
2117 inst->inst_c0 = num;
2118 inst->inst_vtype = type;
2119 inst->klass = mono_class_from_mono_type (type);
2120 type_to_eval_stack_type (cfg, type, inst);
2121 /* if set to 1 the variable is native */
2122 inst->backend.is_pinvoke = 0;
2125 cfg->varinfo [num] = inst;
2127 MONO_INIT_VARINFO (&cfg->vars [num], num);
2130 set_vreg_to_inst (cfg, vreg, inst);
2132 #if SIZEOF_VOID_P == 4
2133 #ifdef MONO_ARCH_SOFT_FLOAT
2134 regpair = mono_type_is_long (type) || mono_type_is_float (type);
2136 regpair = mono_type_is_long (type);
2146 * These two cannot be allocated using create_var_for_vreg since that would
2147 * put it into the cfg->varinfo array, confusing many parts of the JIT.
2151 * Set flags to VOLATILE so SSA skips it.
2154 if (cfg->verbose_level >= 4) {
2155 printf (" Create LVAR R%d (R%d, R%d)\n", inst->dreg, inst->dreg + 1, inst->dreg + 2);
2158 #ifdef MONO_ARCH_SOFT_FLOAT
2159 if (cfg->opt & MONO_OPT_SSA) {
2160 if (mono_type_is_float (type))
2161 inst->flags = MONO_INST_VOLATILE;
2165 /* Allocate a dummy MonoInst for the first vreg */
2166 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2167 tree->dreg = inst->dreg + 1;
2168 if (cfg->opt & MONO_OPT_SSA)
2169 tree->flags = MONO_INST_VOLATILE;
2170 tree->inst_c0 = num;
2171 tree->type = STACK_I4;
2172 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2173 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2175 set_vreg_to_inst (cfg, inst->dreg + 1, tree);
2177 /* Allocate a dummy MonoInst for the second vreg */
2178 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2179 tree->dreg = inst->dreg + 2;
2180 if (cfg->opt & MONO_OPT_SSA)
2181 tree->flags = MONO_INST_VOLATILE;
2182 tree->inst_c0 = num;
2183 tree->type = STACK_I4;
2184 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2185 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2187 set_vreg_to_inst (cfg, inst->dreg + 2, tree);
2191 if (cfg->verbose_level > 2)
2192 g_print ("created temp %d (R%d) of type %s\n", num, vreg, mono_type_get_name (type));
2197 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
2201 if (mono_type_is_long (type))
2202 dreg = mono_alloc_dreg (cfg, STACK_I8);
2203 #ifdef MONO_ARCH_SOFT_FLOAT
2204 else if (mono_type_is_float (type))
2205 dreg = mono_alloc_dreg (cfg, STACK_R8);
2208 /* All the others are unified */
2209 dreg = mono_alloc_preg (cfg);
2211 return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
2215 * Transform a MonoInst into a load from the variable of index var_index.
2218 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
2219 memset (dest, 0, sizeof (MonoInst));
2220 dest->ssa_op = MONO_SSA_LOAD;
2221 dest->inst_i0 = cfg->varinfo [var_index];
2222 dest->opcode = mini_type_to_ldind (cfg, dest->inst_i0->inst_vtype);
2223 type_to_eval_stack_type (cfg, dest->inst_i0->inst_vtype, dest);
2224 dest->klass = dest->inst_i0->klass;
2228 * Create a MonoInst that is a load from the variable of index var_index.
2231 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
2233 NEW_TEMPLOAD (cfg,dest,var_index);
2238 * Create a MonoInst that is a store of the given value into the variable of index var_index.
2241 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
2243 NEW_TEMPSTORE (cfg, dest, var_index, value);
2248 type_from_stack_type (MonoInst *ins) {
2249 switch (ins->type) {
2250 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
2251 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
2252 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
2253 case STACK_R8: return &mono_defaults.double_class->byval_arg;
2256 * this if used to be commented without any specific reason, but
2257 * it breaks #80235 when commented
2260 return &ins->klass->this_arg;
2262 return &mono_defaults.object_class->this_arg;
2264 /* ins->klass may not be set for ldnull.
2265 * Also, if we have a boxed valuetype, we want an object lass,
2266 * not the valuetype class
2268 if (ins->klass && !ins->klass->valuetype)
2269 return &ins->klass->byval_arg;
2270 return &mono_defaults.object_class->byval_arg;
2271 case STACK_VTYPE: return &ins->klass->byval_arg;
2273 g_error ("stack type %d to montype not handled\n", ins->type);
2279 mono_type_from_stack_type (MonoInst *ins) {
2280 return type_from_stack_type (ins);
2284 array_access_to_klass (int opcode, MonoInst *array_obj)
2288 return mono_defaults.byte_class;
2290 return mono_defaults.uint16_class;
2293 return mono_defaults.int_class;
2296 return mono_defaults.sbyte_class;
2299 return mono_defaults.int16_class;
2302 return mono_defaults.int32_class;
2304 return mono_defaults.uint32_class;
2307 return mono_defaults.int64_class;
2310 return mono_defaults.single_class;
2313 return mono_defaults.double_class;
2314 case CEE_LDELEM_REF:
2315 case CEE_STELEM_REF: {
2316 MonoClass *klass = array_obj->klass;
2317 /* FIXME: add assert */
2318 if (klass && klass->rank)
2319 return klass->element_class;
2320 return mono_defaults.object_class;
2323 g_assert_not_reached ();
2329 * mono_add_ins_to_end:
2331 * Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
2334 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
2339 MONO_ADD_INS (bb, inst);
2343 switch (bb->last_ins->opcode) {
2357 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2360 if (MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
2361 /* Need to insert the ins before the compare */
2362 if (bb->code == bb->last_ins) {
2363 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2367 if (bb->code->next == bb->last_ins) {
2368 /* Only two instructions */
2369 opcode = bb->code->opcode;
2371 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)) {
2373 mono_bblock_insert_before_ins (bb, bb->code, inst);
2375 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2378 opcode = bb->last_ins->prev->opcode;
2380 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)) {
2382 mono_bblock_insert_before_ins (bb, bb->last_ins->prev, inst);
2384 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2389 MONO_ADD_INS (bb, inst);
2397 * Replace INS with its decomposition which is stored in a series of bblocks starting
2398 * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS.
2399 * On return, it will be set to the last ins of the decomposition.
2402 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
2404 MonoInst *next = ins->next;
2406 if (next && next->opcode == OP_NOP) {
2407 /* Avoid NOPs following branches */
2408 ins->next = next->next;
2412 if (first_bb == last_bb) {
2414 * Only one replacement bb, merge the code into
2418 /* Delete links between the first_bb and its successors */
2419 while (first_bb->out_count)
2420 mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
2424 (*prev)->next = first_bb->code;
2425 first_bb->code->prev = (*prev);
2427 bb->code = first_bb->code;
2431 last_bb->last_ins->next = next;
2433 next->prev = last_bb->last_ins;
2435 bb->last_ins = last_bb->last_ins;
2436 *prev = last_bb->last_ins;
2439 MonoBasicBlock **tmp_bblocks, *tmp;
2445 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
2446 tmp->region = bb->region;
2448 /* Split the original bb */
2450 ins->next->prev = NULL;
2454 /* Merge the second part of the original bb into the last bb */
2455 if (last_bb->last_ins) {
2456 last_bb->last_ins->next = next;
2458 next->prev = last_bb->last_ins;
2460 last_bb->code = next;
2464 for (last = next; last->next != NULL; last = last->next)
2466 last_bb->last_ins = last;
2469 for (i = 0; i < bb->out_count; ++i)
2470 link_bblock (cfg, last_bb, bb->out_bb [i]);
2472 /* Merge the first (dummy) bb to the original bb */
2474 (*prev)->next = first_bb->code;
2475 first_bb->code->prev = (*prev);
2477 bb->code = first_bb->code;
2479 bb->last_ins = first_bb->last_ins;
2481 /* Delete the links between the original bb and its successors */
2482 tmp_bblocks = bb->out_bb;
2483 count = bb->out_count;
2484 for (i = 0; i < count; ++i)
2485 mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
2487 /* Add links between the original bb and the first_bb's successors */
2488 for (i = 0; i < first_bb->out_count; ++i) {
2489 MonoBasicBlock *out_bb = first_bb->out_bb [i];
2491 link_bblock (cfg, bb, out_bb);
2493 /* Delete links between the first_bb and its successors */
2494 for (i = 0; i < bb->out_count; ++i) {
2495 MonoBasicBlock *out_bb = bb->out_bb [i];
2497 mono_unlink_bblock (cfg, first_bb, out_bb);
2499 last_bb->next_bb = bb->next_bb;
2500 bb->next_bb = first_bb->next_bb;
2507 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
2509 MonoInst *inst, *load;
2511 NEW_TEMPLOAD (cfg, load, src);
2513 NEW_TEMPSTORE (cfg, inst, dest, load);
2514 /* FIXME: handle CEE_STIND_R4 */
2515 if (inst->opcode == CEE_STOBJ) {
2516 NEW_TEMPLOADA (cfg, inst, dest);
2517 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
2519 inst->cil_code = NULL;
2520 mono_add_ins_to_end (bb, inst);
2525 * This function is called to handle items that are left on the evaluation stack
2526 * at basic block boundaries. What happens is that we save the values to local variables
2527 * and we reload them later when first entering the target basic block (with the
2528 * handle_loaded_temps () function).
2529 * It is also used to handle items on the stack in store opcodes, since it is
2530 * possible that the variable to be stored into is already on the stack, in
2531 * which case its old value should be used.
2532 * A single joint point will use the same variables (stored in the array bb->out_stack or
2533 * bb->in_stack, if the basic block is before or after the joint point).
2534 * If the stack merge fails at a join point, cfg->unverifiable is set.
2537 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count)
2540 MonoBasicBlock *outb;
2541 MonoInst *inst, **locals;
2546 if (cfg->verbose_level > 3)
2547 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
2549 if (!bb->out_scount) {
2550 bb->out_scount = count;
2551 //g_print ("bblock %d has out:", bb->block_num);
2553 for (i = 0; i < bb->out_count; ++i) {
2554 outb = bb->out_bb [i];
2555 /* exception handlers are linked, but they should not be considered for stack args */
2556 if (outb->flags & BB_EXCEPTION_HANDLER)
2558 //g_print (" %d", outb->block_num);
2559 if (outb->in_stack) {
2561 bb->out_stack = outb->in_stack;
2567 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
2568 for (i = 0; i < count; ++i) {
2570 * try to reuse temps already allocated for this purpouse, if they occupy the same
2571 * stack slot and if they are of the same type.
2572 * This won't cause conflicts since if 'local' is used to
2573 * store one of the values in the in_stack of a bblock, then
2574 * the same variable will be used for the same outgoing stack
2576 * This doesn't work when inlining methods, since the bblocks
2577 * in the inlined methods do not inherit their in_stack from
2578 * the bblock they are inlined to. See bug #58863 for an
2580 * This hack is disabled since it also prevents proper tracking of types.
2583 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2585 if (cfg->inlined_method)
2586 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2588 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
2594 for (i = 0; i < bb->out_count; ++i) {
2595 outb = bb->out_bb [i];
2596 /* exception handlers are linked, but they should not be considered for stack args */
2597 if (outb->flags & BB_EXCEPTION_HANDLER)
2599 if (outb->in_scount) {
2600 if (outb->in_scount != bb->out_scount) {
2601 cfg->unverifiable = TRUE;
2604 continue; /* check they are the same locals */
2606 outb->in_scount = count;
2607 outb->in_stack = bb->out_stack;
2610 locals = bb->out_stack;
2611 for (i = 0; i < count; ++i) {
2612 /* add store ops at the end of the bb, before the branch */
2613 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
2614 if (inst->opcode == CEE_STOBJ) {
2615 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
2616 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
2618 inst->cil_code = sp [i]->cil_code;
2619 mono_add_ins_to_end (bb, inst);
2621 if (cfg->verbose_level > 3)
2622 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
2626 * It is possible that the out bblocks already have in_stack assigned, and
2627 * the in_stacks differ. In this case, we will store to all the different
2634 /* Find a bblock which has a different in_stack */
2636 while (bindex < bb->out_count) {
2637 outb = bb->out_bb [bindex];
2638 /* exception handlers are linked, but they should not be considered for stack args */
2639 if (outb->flags & BB_EXCEPTION_HANDLER) {
2643 if (outb->in_stack != locals) {
2645 * Instead of storing sp [i] to locals [i], we need to store
2646 * locals [i] to <new locals>[i], since the sp [i] tree can't
2647 * be shared between trees.
2649 for (i = 0; i < count; ++i)
2650 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
2651 locals = outb->in_stack;
2661 ret_type_to_call_opcode (MonoType *type, int calli, int virt, MonoGenericSharingContext *gsctx)
2664 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2667 type = mini_get_basic_type_from_generic (gsctx, type);
2668 switch (type->type) {
2669 case MONO_TYPE_VOID:
2670 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
2673 case MONO_TYPE_BOOLEAN:
2676 case MONO_TYPE_CHAR:
2679 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2683 case MONO_TYPE_FNPTR:
2684 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2685 case MONO_TYPE_CLASS:
2686 case MONO_TYPE_STRING:
2687 case MONO_TYPE_OBJECT:
2688 case MONO_TYPE_SZARRAY:
2689 case MONO_TYPE_ARRAY:
2690 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2693 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
2696 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
2697 case MONO_TYPE_VALUETYPE:
2698 if (type->data.klass->enumtype) {
2699 type = type->data.klass->enum_basetype;
2702 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2703 case MONO_TYPE_TYPEDBYREF:
2704 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2705 case MONO_TYPE_GENERICINST:
2706 type = &type->data.generic_class->container_class->byval_arg;
2709 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2715 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
2717 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2718 MonoJumpInfoBBTable *table;
2720 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
2722 table->table_size = num_blocks;
2724 ji->ip.label = label;
2725 ji->type = MONO_PATCH_INFO_SWITCH;
2726 ji->data.table = table;
2727 ji->next = cfg->patch_info;
2728 cfg->patch_info = ji;
2732 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
2734 if (cfg->compile_aot) {
2735 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
2736 jump_info_token->image = image;
2737 jump_info_token->token = token;
2738 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
2743 * When we add a tree of instructions, we need to ensure the instructions currently
2744 * on the stack are executed before (like, if we load a value from a local).
2745 * We ensure this by saving the currently loaded values to temps and rewriting the
2746 * instructions to load the values.
2747 * This is not done for opcodes that terminate a basic block (because it's handled already
2748 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
2751 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
2753 MonoInst *load, *store, *temp, *ins;
2755 while (stack < sp) {
2757 /* handle also other constants */
2758 if ((ins->opcode != OP_ICONST) &&
2759 /* temps never get written to again, so we can safely avoid duplicating them */
2760 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2761 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2762 temp->flags |= MONO_INST_IS_TEMP;
2763 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2764 store->cil_code = ins->cil_code;
2765 if (store->opcode == CEE_STOBJ) {
2766 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2767 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
2769 MONO_ADD_INS (bblock, store);
2770 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2771 load->cil_code = ins->cil_code;
2779 * target_type_is_incompatible:
2780 * @cfg: MonoCompile context
2782 * Check that the item @arg on the evaluation stack can be stored
2783 * in the target type (can be a local, or field, etc).
2784 * The cfg arg can be used to check if we need verification or just
2787 * Returns: non-0 value if arg can't be stored on a target.
2790 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2792 MonoType *simple_type;
2795 if (target->byref) {
2796 /* FIXME: check that the pointed to types match */
2797 if (arg->type == STACK_MP)
2798 return arg->klass != mono_class_from_mono_type (target);
2799 if (arg->type == STACK_PTR)
2803 simple_type = mono_type_get_underlying_type (target);
2804 switch (simple_type->type) {
2805 case MONO_TYPE_VOID:
2809 case MONO_TYPE_BOOLEAN:
2812 case MONO_TYPE_CHAR:
2815 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2819 /* STACK_MP is needed when setting pinned locals */
2820 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2825 case MONO_TYPE_FNPTR:
2826 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2829 case MONO_TYPE_OBJECT:
2830 if (arg->type != STACK_OBJ)
2833 case MONO_TYPE_STRING:
2834 if (arg->type != STACK_OBJ)
2836 /* ldnull has arg->klass unset */
2837 /*if (arg->klass && arg->klass != mono_defaults.string_class) {
2842 case MONO_TYPE_CLASS:
2843 case MONO_TYPE_SZARRAY:
2844 case MONO_TYPE_ARRAY:
2845 if (arg->type != STACK_OBJ)
2847 /* FIXME: check type compatibility */
2851 if (arg->type != STACK_I8)
2856 if (arg->type != STACK_R8)
2859 case MONO_TYPE_VALUETYPE:
2860 if (arg->type != STACK_VTYPE)
2862 klass = mono_class_from_mono_type (simple_type);
2863 if (klass != arg->klass)
2866 case MONO_TYPE_TYPEDBYREF:
2867 if (arg->type != STACK_VTYPE)
2869 klass = mono_class_from_mono_type (simple_type);
2870 if (klass != arg->klass)
2873 case MONO_TYPE_GENERICINST:
2874 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2875 klass = mono_class_from_mono_type (simple_type);
2876 if (klass->enumtype)
2877 return target_type_is_incompatible (cfg, klass->enum_basetype, arg);
2878 if (arg->type != STACK_VTYPE)
2880 if (klass != arg->klass)
2884 if (arg->type != STACK_OBJ)
2886 /* FIXME: check type compatibility */
2890 case MONO_TYPE_MVAR:
2891 /* FIXME: all the arguments must be references for now,
2892 * later look inside cfg and see if the arg num is
2893 * really a reference
2895 g_assert (cfg->generic_sharing_context);
2896 if (arg->type != STACK_OBJ)
2900 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2906 * Prepare arguments for passing to a function call.
2907 * Return a non-zero value if the arguments can't be passed to the given
2909 * The type checks are not yet complete and some conversions may need
2910 * casts on 32 or 64 bit architectures.
2912 * FIXME: implement this using target_type_is_incompatible ()
2915 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2917 MonoType *simple_type;
2921 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2925 for (i = 0; i < sig->param_count; ++i) {
2926 if (sig->params [i]->byref) {
2927 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2931 simple_type = sig->params [i];
2932 simple_type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, simple_type);
2934 switch (simple_type->type) {
2935 case MONO_TYPE_VOID:
2940 case MONO_TYPE_BOOLEAN:
2943 case MONO_TYPE_CHAR:
2946 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2952 case MONO_TYPE_FNPTR:
2953 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2956 case MONO_TYPE_CLASS:
2957 case MONO_TYPE_STRING:
2958 case MONO_TYPE_OBJECT:
2959 case MONO_TYPE_SZARRAY:
2960 case MONO_TYPE_ARRAY:
2961 if (args [i]->type != STACK_OBJ)
2966 if (args [i]->type != STACK_I8)
2971 if (args [i]->type != STACK_R8)
2974 case MONO_TYPE_VALUETYPE:
2975 if (simple_type->data.klass->enumtype) {
2976 simple_type = simple_type->data.klass->enum_basetype;
2979 if (args [i]->type != STACK_VTYPE)
2982 case MONO_TYPE_TYPEDBYREF:
2983 if (args [i]->type != STACK_VTYPE)
2986 case MONO_TYPE_GENERICINST:
2987 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2991 g_error ("unknown type 0x%02x in check_call_signature",
2999 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
3000 const guint8 *ip, gboolean to_end)
3002 MonoInst *temp, *store, *ins = (MonoInst*)call;
3003 MonoType *ret = sig->ret;
3005 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
3007 call->inst.type = STACK_OBJ;
3008 call->inst.opcode = OP_CALL;
3009 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
3011 type_to_eval_stack_type (cfg, ret, ins);
3012 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
3015 temp->flags |= MONO_INST_IS_TEMP;
3017 if (MONO_TYPE_ISSTRUCT (ret)) {
3018 MonoInst *loada, *dummy_store;
3021 * Emit a dummy store to the local holding the result so the
3022 * liveness info remains correct.
3024 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
3026 mono_add_ins_to_end (bblock, dummy_store);
3028 MONO_ADD_INS (bblock, dummy_store);
3030 /* we use this to allocate native sized structs */
3031 temp->backend.is_pinvoke = sig->pinvoke;
3033 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
3034 if (call->inst.opcode == OP_VCALL || call->inst.opcode == OP_VCALL_RGCTX)
3035 ins->inst_left = loada;
3037 ins->inst_right = loada; /* a virtual or indirect call */
3040 mono_add_ins_to_end (bblock, ins);
3042 MONO_ADD_INS (bblock, ins);
3044 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3045 store->cil_code = ip;
3047 #ifdef MONO_ARCH_SOFT_FLOAT
3048 if (store->opcode == CEE_STIND_R4) {
3049 /*FIXME implement proper support for to_end*/
3051 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3052 handle_store_float (cfg, bblock, store, ins, ip);
3056 mono_add_ins_to_end (bblock, store);
3058 MONO_ADD_INS (bblock, store);
3060 return temp->inst_c0;
3063 mono_add_ins_to_end (bblock, ins);
3065 MONO_ADD_INS (bblock, ins);
3070 inline static MonoCallInst *
3071 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3072 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
3077 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual, cfg->generic_sharing_context));
3079 #ifdef MONO_ARCH_SOFT_FLOAT
3080 /* we need to convert the r4 value to an int value */
3083 for (i = 0; i < sig->param_count; ++i) {
3084 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4) {
3085 MonoInst *iargs [1];
3087 iargs [0] = args [i + sig->hasthis];
3089 temp = mono_emit_jit_icall (cfg, bblock, mono_fload_r4_arg, iargs, ip);
3090 NEW_TEMPLOAD (cfg, arg, temp);
3091 args [i + sig->hasthis] = arg;
3097 call->inst.cil_code = ip;
3099 call->signature = sig;
3100 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
3101 type_to_eval_stack_type (cfg, sig->ret, &call->inst);
3103 for (arg = call->out_args; arg;) {
3104 MonoInst *narg = arg->next;
3109 mono_add_ins_to_end (bblock, arg);
3111 MONO_ADD_INS (bblock, arg);
3117 inline static MonoCallInst*
3118 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3119 MonoInst **args, MonoInst *addr, const guint8 *ip)
3121 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
3123 call->inst.inst_i0 = addr;
3128 inline static MonoCallInst*
3129 mono_emit_rgctx_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3130 MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3132 MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3135 switch (call->inst.opcode) {
3136 case OP_CALL_REG: call->inst.opcode = OP_CALL_REG_RGCTX; break;
3137 case OP_VOIDCALL_REG: call->inst.opcode = OP_VOIDCALL_REG_RGCTX; break;
3138 case OP_FCALL_REG: call->inst.opcode = OP_FCALL_REG_RGCTX; break;
3139 case OP_LCALL_REG: call->inst.opcode = OP_LCALL_REG_RGCTX; break;
3140 case OP_VCALL_REG: {
3143 NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3144 call->inst.inst_left = group;
3145 call->inst.opcode = OP_VCALL_REG_RGCTX;
3148 default: g_assert_not_reached ();
3151 if (call->inst.opcode != OP_VCALL_REG_RGCTX) {
3152 g_assert (!call->inst.inst_right);
3153 call->inst.inst_right = rgctx_arg;
3155 g_assert (!call->inst.inst_left->inst_right);
3156 call->inst.inst_left->inst_right = rgctx_arg;
3164 mono_emit_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3165 MonoInst **args, MonoInst *addr, const guint8 *ip)
3167 MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3169 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3173 mono_emit_rgctx_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3174 MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3176 MonoCallInst *call = mono_emit_rgctx_calli (cfg, bblock, sig, args, addr, rgctx_arg, ip);
3178 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3181 static MonoCallInst*
3182 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3183 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
3185 gboolean virtual = this != NULL;
3188 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
3190 if (this && sig->hasthis &&
3191 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
3192 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
3193 call->method = mono_marshal_get_remoting_invoke_with_check (method);
3195 call->method = method;
3197 call->inst.flags |= MONO_INST_HAS_METHOD;
3198 call->inst.inst_left = this;
3200 if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3201 /* Needed by the code generated in inssel.brg */
3202 mono_get_got_var (cfg);
3207 static MonoCallInst*
3208 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3209 MonoInst **args, const guint8 *ip, MonoInst *this)
3211 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3215 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3216 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3218 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
3220 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3224 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3225 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
3226 gboolean ret_object, gboolean to_end)
3228 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
3230 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
3234 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
3235 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
3241 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
3244 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
3248 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
3250 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
3253 g_warning ("unregistered JIT ICall");
3254 g_assert_not_reached ();
3257 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
3260 static MonoCallInst*
3261 mono_emit_rgctx_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3262 MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg, const guint8 *ip, MonoInst *this)
3264 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3266 g_assert (!(rgctx_arg && imt_arg));
3269 switch (call->inst.opcode) {
3270 case OP_CALL: call->inst.opcode = OP_CALL_RGCTX; break;
3271 case OP_VOIDCALL: call->inst.opcode = OP_VOIDCALL_RGCTX; break;
3272 case OP_FCALL: call->inst.opcode = OP_FCALL_RGCTX; break;
3273 case OP_LCALL: call->inst.opcode = OP_LCALL_RGCTX; break;
3274 case OP_VCALL: call->inst.opcode = OP_VCALL_RGCTX; break;
3275 default: g_assert_not_reached ();
3278 if (call->inst.opcode != OP_VCALL_RGCTX) {
3279 g_assert (!call->inst.inst_left);
3280 call->inst.inst_left = rgctx_arg;
3282 g_assert (!call->inst.inst_right);
3283 call->inst.inst_right = rgctx_arg;
3285 } else if (imt_arg) {
3286 switch (call->inst.opcode) {
3287 case OP_CALLVIRT: call->inst.opcode = OP_CALLVIRT_IMT; break;
3288 case OP_VOIDCALLVIRT: call->inst.opcode = OP_VOIDCALLVIRT_IMT; break;
3289 case OP_FCALLVIRT: call->inst.opcode = OP_FCALLVIRT_IMT; break;
3290 case OP_LCALLVIRT: call->inst.opcode = OP_LCALLVIRT_IMT; break;
3291 case OP_VCALLVIRT: {
3294 NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3295 call->inst.inst_left = group;
3296 call->inst.opcode = OP_VCALLVIRT_IMT;
3299 default: g_assert_not_reached ();
3302 if (call->inst.opcode != OP_VCALLVIRT_IMT) {
3303 g_assert (!call->inst.inst_right);
3304 call->inst.inst_right = imt_arg;
3306 g_assert (!call->inst.inst_left->inst_right);
3307 call->inst.inst_left->inst_right = imt_arg;
3315 mono_emit_rgctx_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3316 MonoMethodSignature *signature, MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg,
3317 const guint8 *ip, MonoInst *this)
3319 MonoCallInst *call = mono_emit_rgctx_method_call (cfg, bblock, method, signature, args, rgctx_arg, imt_arg, ip, this);
3321 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3325 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
3327 MonoInst *ins, *temp = NULL, *store, *load, *begin;
3328 MonoInst *last_arg = NULL;
3332 //g_print ("emulating: ");
3333 //mono_print_tree_nl (tree);
3334 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE, cfg->generic_sharing_context));
3335 ins = (MonoInst*)call;
3337 call->inst.cil_code = tree->cil_code;
3339 call->signature = info->sig;
3341 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
3343 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3344 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
3345 temp->flags |= MONO_INST_IS_TEMP;
3346 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3347 /* FIXME: handle CEE_STIND_R4 */
3348 store->cil_code = tree->cil_code;
3353 nargs = info->sig->param_count + info->sig->hasthis;
3355 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
3358 last_arg->next = store;
3361 begin = call->out_args;
3365 if (cfg->prev_ins) {
3367 * This assumes that that in a tree, emulate_opcode is called for a
3368 * node before it is called for its children. dec_foreach needs to
3369 * take this into account.
3371 store->next = cfg->prev_ins->next;
3372 cfg->prev_ins->next = begin;
3374 store->next = cfg->cbb->code;
3375 cfg->cbb->code = begin;
3378 call->fptr = mono_icall_get_wrapper (info);
3380 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3381 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3387 * This entry point could be used later for arbitrary method
3391 mini_redirect_call (int *temp, MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
3392 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3395 if (method->klass == mono_defaults.string_class) {
3396 /* managed string allocation support */
3397 if (strcmp (method->name, "InternalAllocateStr") == 0) {
3398 MonoInst *iargs [2];
3399 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3400 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, FALSE);
3403 NEW_VTABLECONST (cfg, iargs [0], vtable);
3404 iargs [1] = args [0];
3405 *temp = mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, this);
3412 static MonoMethodSignature *
3413 mono_get_array_new_va_signature (int arity)
3415 static GHashTable *sighash = NULL;
3416 MonoMethodSignature *res;
3421 sighash = g_hash_table_new (NULL, NULL);
3423 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
3428 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
3431 #ifdef MONO_ARCH_VARARG_ICALLS
3432 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
3433 res->call_convention = MONO_CALL_VARARG;
3436 #ifdef PLATFORM_WIN32
3437 res->call_convention = MONO_CALL_C;
3440 res->params [0] = &mono_defaults.int_class->byval_arg;
3441 for (i = 0; i < arity; i++)
3442 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
3444 res->ret = &mono_defaults.object_class->byval_arg;
3446 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
3453 mono_get_array_new_va_icall (int rank)
3455 MonoMethodSignature *esig;
3456 char icall_name [256];
3458 MonoJitICallInfo *info;
3460 /* Need to register the icall so it gets an icall wrapper */
3461 sprintf (icall_name, "ves_array_new_va_%d", rank);
3464 info = mono_find_jit_icall_by_name (icall_name);
3466 esig = mono_get_array_new_va_signature (rank);
3467 name = g_strdup (icall_name);
3468 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
3470 g_hash_table_insert (jit_icall_name_hash, name, name);
3478 get_memcpy_method (void)
3480 static MonoMethod *memcpy_method = NULL;
3481 if (!memcpy_method) {
3482 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3484 g_error ("Old corlib found. Install a new one");
3486 return memcpy_method;
3490 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
3491 MonoInst *iargs [3];
3494 MonoMethod *memcpy_method;
3498 * This check breaks with spilled vars... need to handle it during verification anyway.
3499 * g_assert (klass && klass == src->klass && klass == dest->klass);
3503 n = mono_class_native_size (klass, &align);
3505 n = mono_class_value_size (klass, &align);
3507 #if HAVE_WRITE_BARRIERS
3508 /* if native is true there should be no references in the struct */
3509 if (write_barrier && klass->has_references && !native) {
3512 NEW_PCONST (cfg, iargs [2], klass);
3514 mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
3519 /* FIXME: add write barrier handling */
3520 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
3522 if (dest->opcode == OP_LDADDR) {
3523 /* Keep liveness info correct */
3524 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
3525 MONO_ADD_INS (bblock, inst);
3527 NEW_MEMCPY (cfg, inst, dest, src, n, align);
3528 MONO_ADD_INS (bblock, inst);
3533 NEW_ICONST (cfg, iargs [2], n);
3535 memcpy_method = get_memcpy_method ();
3536 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
3540 get_memset_method (void)
3542 static MonoMethod *memset_method = NULL;
3543 if (!memset_method) {
3544 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3546 g_error ("Old corlib found. Install a new one");
3548 return memset_method;
3552 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
3554 MonoInst *iargs [3];
3555 MonoInst *ins, *zero_int32;
3558 MonoMethod *memset_method;
3560 NEW_ICONST (cfg, zero_int32, 0);
3562 mono_class_init (klass);
3563 n = mono_class_value_size (klass, &align);
3564 MONO_INST_NEW (cfg, ins, 0);
3566 ins->inst_left = dest;
3567 ins->inst_right = zero_int32;
3569 ins->opcode = CEE_STIND_I1;
3570 MONO_ADD_INS (bblock, ins);
3571 } else if ((n == 2) && (align >= 2)) {
3572 ins->opcode = CEE_STIND_I2;
3573 MONO_ADD_INS (bblock, ins);
3574 } else if ((n == 2) && (align >= 4)) {
3575 ins->opcode = CEE_STIND_I4;
3576 MONO_ADD_INS (bblock, ins);
3577 } else if (n <= sizeof (gpointer) * 5) {
3578 NEW_MEMSET (cfg, ins, dest, 0, n, align);
3579 MONO_ADD_INS (bblock, ins);
3581 memset_method = get_memset_method ();
3582 handle_loaded_temps (cfg, bblock, stack_start, sp);
3584 NEW_ICONST (cfg, iargs [1], 0);
3585 NEW_ICONST (cfg, iargs [2], n);
3586 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
3591 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
3593 MonoInst *iargs [2];
3596 if (cfg->opt & MONO_OPT_SHARED) {
3597 NEW_DOMAINCONST (cfg, iargs [0]);
3598 NEW_CLASSCONST (cfg, iargs [1], klass);
3600 alloc_ftn = mono_object_new;
3601 } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
3602 /* This happens often in argument checking code, eg. throw new FooException... */
3603 /* Avoid relocations by calling a helper function specialized to mscorlib */
3604 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3605 return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
3607 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3608 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3611 if (managed_alloc) {
3612 NEW_VTABLECONST (cfg, iargs [0], vtable);
3613 return mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, NULL);
3615 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
3617 guint32 lw = vtable->klass->instance_size;
3618 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
3619 NEW_ICONST (cfg, iargs [0], lw);
3620 NEW_VTABLECONST (cfg, iargs [1], vtable);
3623 NEW_VTABLECONST (cfg, iargs [0], vtable);
3626 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3630 handle_alloc_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *data_inst,
3631 gboolean for_box, const guchar *ip)
3633 MonoInst *iargs [2];
3634 MonoMethod *managed_alloc = NULL;
3637 FIXME: we cannot get managed_alloc here because we can't get
3638 the class's vtable (because it's not a closed class)
3640 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3641 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3644 if (cfg->opt & MONO_OPT_SHARED) {
3645 NEW_DOMAINCONST (cfg, iargs [0]);
3646 iargs [1] = data_inst;
3647 alloc_ftn = mono_object_new;
3649 g_assert (!cfg->compile_aot);
3651 if (managed_alloc) {
3652 iargs [0] = data_inst;
3653 return mono_emit_method_call_spilled (cfg, bblock, managed_alloc,
3654 mono_method_signature (managed_alloc), iargs, ip, NULL);
3657 iargs [0] = data_inst;
3658 alloc_ftn = mono_object_new_specific;
3661 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3665 handle_box_copy (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass, int temp)
3667 MonoInst *dest, *vtoffset, *add, *vstore;
3669 NEW_TEMPLOAD (cfg, dest, temp);
3670 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3671 MONO_INST_NEW (cfg, add, OP_PADD);
3672 add->inst_left = dest;
3673 add->inst_right = vtoffset;
3676 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3677 vstore->opcode = mini_type_to_stind (cfg, &klass->byval_arg);
3678 vstore->cil_code = ip;
3679 vstore->inst_left = add;
3680 vstore->inst_right = val;
3682 #ifdef MONO_ARCH_SOFT_FLOAT
3683 if (vstore->opcode == CEE_STIND_R4) {
3684 handle_store_float (cfg, bblock, add, val, ip);
3687 if (vstore->opcode == CEE_STOBJ) {
3688 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
3690 MONO_ADD_INS (bblock, vstore);
3692 NEW_TEMPLOAD (cfg, dest, temp);
3697 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
3702 if (mono_class_is_nullable (klass)) {
3703 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
3704 temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
3705 NEW_TEMPLOAD (cfg, dest, temp);
3709 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
3711 return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3715 handle_box_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip,
3716 MonoClass *klass, MonoInst *data_inst)
3720 g_assert (!mono_class_is_nullable (klass));
3722 temp = handle_alloc_from_inst (cfg, bblock, klass, data_inst, TRUE, ip);
3724 return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3728 handle_delegate_ctor (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *target, MonoMethod *method, unsigned char *ip)
3730 gpointer *trampoline;
3731 MonoInst *obj, *ins, *store, *offset_ins, *method_ins, *tramp_ins;
3734 temp = handle_alloc (cfg, bblock, klass, FALSE, ip);
3736 /* Inline the contents of mono_delegate_ctor */
3738 /* Set target field */
3739 /* Optimize away setting of NULL target */
3740 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
3741 NEW_TEMPLOAD (cfg, obj, temp);
3742 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, target));
3743 MONO_INST_NEW (cfg, ins, OP_PADD);
3744 ins->inst_left = obj;
3745 ins->inst_right = offset_ins;
3747 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3748 store->inst_left = ins;
3749 store->inst_right = target;
3750 mono_bblock_add_inst (bblock, store);
3753 /* Set method field */
3754 NEW_TEMPLOAD (cfg, obj, temp);
3755 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, method));
3756 MONO_INST_NEW (cfg, ins, OP_PADD);
3757 ins->inst_left = obj;
3758 ins->inst_right = offset_ins;
3760 NEW_METHODCONST (cfg, method_ins, method);
3762 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3763 store->inst_left = ins;
3764 store->inst_right = method_ins;
3765 mono_bblock_add_inst (bblock, store);
3767 /* Set invoke_impl field */
3768 NEW_TEMPLOAD (cfg, obj, temp);
3769 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, invoke_impl));
3770 MONO_INST_NEW (cfg, ins, OP_PADD);
3771 ins->inst_left = obj;
3772 ins->inst_right = offset_ins;
3774 trampoline = mono_create_delegate_trampoline (klass);
3775 NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_ABS, trampoline);
3777 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3778 store->inst_left = ins;
3779 store->inst_right = tramp_ins;
3780 mono_bblock_add_inst (bblock, store);
3782 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
3784 NEW_TEMPLOAD (cfg, obj, temp);
3790 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
3792 MonoJitICallInfo *info;
3794 info = mono_get_array_new_va_icall (rank);
3796 cfg->flags |= MONO_CFG_HAS_VARARGS;
3798 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
3799 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
3803 mono_emit_load_got_addr (MonoCompile *cfg)
3805 MonoInst *load, *store, *dummy_use;
3808 if (!cfg->got_var || cfg->got_var_allocated)
3811 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
3812 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
3814 /* Add it to the start of the first bblock */
3815 if (cfg->bb_entry->code) {
3816 store->next = cfg->bb_entry->code;
3817 cfg->bb_entry->code = store;
3820 MONO_ADD_INS (cfg->bb_entry, store);
3822 cfg->got_var_allocated = TRUE;
3825 * Add a dummy use to keep the got_var alive, since real uses might
3826 * only be generated in the decompose or instruction selection phases.
3827 * Add it to end_bblock, so the variable's lifetime covers the whole
3830 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
3831 NEW_DUMMY_USE (cfg, dummy_use, load);
3832 MONO_ADD_INS (cfg->bb_exit, dummy_use);
3835 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
3838 mini_class_is_system_array (MonoClass *klass)
3840 if (klass->parent == mono_defaults.array_class)
3847 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
3849 MonoMethodHeader *header = mono_method_get_header (method);
3850 MonoMethodSignature *signature = mono_method_signature (method);
3854 if (cfg->generic_sharing_context)
3857 if (method->inline_failure)
3860 #ifdef MONO_ARCH_HAVE_LMF_OPS
3861 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3862 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
3863 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
3867 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3868 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3869 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
3870 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
3871 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3872 (method->klass->marshalbyref) ||
3873 !header || header->num_clauses ||
3874 /* fixme: why cant we inline valuetype returns? */
3875 MONO_TYPE_ISSTRUCT (signature->ret))
3878 #ifdef MONO_ARCH_SOFT_FLOAT
3879 /* this complicates things, fix later */
3880 if (signature->ret->type == MONO_TYPE_R4)
3883 /* its not worth to inline methods with valuetype arguments?? */
3884 for (i = 0; i < signature->param_count; i++) {
3885 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
3888 #ifdef MONO_ARCH_SOFT_FLOAT
3889 /* this complicates things, fix later */
3890 if (!signature->params [i]->byref && signature->params [i]->type == MONO_TYPE_R4)
3895 /* also consider num_locals? */
3896 /* Do the size check early to avoid creating vtables */
3897 if (getenv ("MONO_INLINELIMIT")) {
3898 if (header->code_size >= atoi (getenv ("MONO_INLINELIMIT"))) {
3901 } else if (header->code_size >= INLINE_LENGTH_LIMIT)
3905 * if we can initialize the class of the method right away, we do,
3906 * otherwise we don't allow inlining if the class needs initialization,
3907 * since it would mean inserting a call to mono_runtime_class_init()
3908 * inside the inlined code
3910 if (!(cfg->opt & MONO_OPT_SHARED)) {
3911 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
3912 if (cfg->run_cctors && method->klass->has_cctor) {
3913 if (!method->klass->runtime_info)
3914 /* No vtable created yet */
3916 vtable = mono_class_vtable (cfg->domain, method->klass);
3919 /* This makes so that inline cannot trigger */
3920 /* .cctors: too many apps depend on them */
3921 /* running with a specific order... */
3922 if (! vtable->initialized)
3924 mono_runtime_class_init (vtable);
3926 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
3927 if (!method->klass->runtime_info)
3928 /* No vtable created yet */
3930 vtable = mono_class_vtable (cfg->domain, method->klass);
3933 if (!vtable->initialized)
3938 * If we're compiling for shared code
3939 * the cctor will need to be run at aot method load time, for example,
3940 * or at the end of the compilation of the inlining method.
3942 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3945 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
3948 * CAS - do not inline methods with declarative security
3949 * Note: this has to be before any possible return TRUE;
3951 if (mono_method_has_declsec (method))
3958 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
3960 if (vtable->initialized && !cfg->compile_aot)
3963 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3966 if (!mono_class_needs_cctor_run (vtable->klass, method))
3969 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3970 /* The initialization is already done before the method is called */
3977 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3981 MonoMethod *addr_method;
3984 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3987 MONO_INST_NEW (cfg, addr, CEE_LDELEMA);
3988 addr->inst_left = sp [0];
3989 addr->inst_right = sp [1];
3990 addr->type = STACK_MP;
3991 addr->klass = cmethod->klass->element_class;
3995 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3996 #if defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(MONO_ARCH_NO_EMULATE_MUL)
3997 /* OP_LDELEMA2D depends on OP_LMUL */
4000 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
4001 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
4002 addr->inst_left = sp [0];
4003 addr->inst_right = indexes;
4004 addr->type = STACK_MP;
4005 addr->klass = cmethod->klass->element_class;
4010 element_size = mono_class_array_element_size (cmethod->klass->element_class);
4011 addr_method = mono_marshal_get_array_address (rank, element_size);
4012 temp = mono_emit_method_call_spilled (cfg, bblock, addr_method, addr_method->signature, sp, ip, NULL);
4013 NEW_TEMPLOAD (cfg, addr, temp);
4018 static MonoJitICallInfo **emul_opcode_map = NULL;
4021 mono_find_jit_opcode_emulation (int opcode)
4023 g_assert (opcode >= 0 && opcode <= OP_LAST);
4024 if (emul_opcode_map)
4025 return emul_opcode_map [opcode];
4031 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4033 MonoInst *ins = NULL;
4035 static MonoClass *runtime_helpers_class = NULL;
4036 if (! runtime_helpers_class)
4037 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
4038 "System.Runtime.CompilerServices", "RuntimeHelpers");
4040 if (cmethod->klass == mono_defaults.string_class) {
4041 if (strcmp (cmethod->name, "get_Chars") == 0) {
4042 MONO_INST_NEW (cfg, ins, OP_GETCHR);
4043 ins->inst_i0 = args [0];
4044 ins->inst_i1 = args [1];
4046 } else if (strcmp (cmethod->name, "get_Length") == 0) {
4047 MONO_INST_NEW (cfg, ins, OP_STRLEN);
4048 ins->inst_i0 = args [0];
4050 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
4052 MONO_INST_NEW (cfg, get_addr, OP_STR_CHAR_ADDR);
4053 get_addr->inst_i0 = args [0];
4054 get_addr->inst_i1 = args [1];
4055 MONO_INST_NEW (cfg, ins, CEE_STIND_I2);
4056 ins->inst_i0 = get_addr;
4057 ins->inst_i1 = args [2];
4061 } else if (cmethod->klass == mono_defaults.object_class) {
4062 if (strcmp (cmethod->name, "GetType") == 0) {
4063 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
4064 ins->inst_i0 = args [0];
4066 /* The OP_GETHASHCODE rule depends on OP_MUL */
4067 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
4068 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
4069 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
4070 ins->inst_i0 = args [0];
4073 } else if (strcmp (cmethod->name, ".ctor") == 0) {
4074 MONO_INST_NEW (cfg, ins, OP_NOP);
4078 } else if (cmethod->klass == mono_defaults.array_class) {
4079 if (cmethod->name [0] != 'g')
4082 if (strcmp (cmethod->name, "get_Rank") == 0) {
4083 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
4084 ins->inst_i0 = args [0];
4086 } else if (strcmp (cmethod->name, "get_Length") == 0) {
4087 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
4088 ins->inst_i0 = args [0];
4092 } else if (cmethod->klass == runtime_helpers_class) {
4093 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
4094 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
4098 } else if (cmethod->klass == mono_defaults.thread_class) {
4099 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
4101 if (strcmp (cmethod->name, "MemoryBarrier") == 0) {
4102 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4105 } else if (mini_class_is_system_array (cmethod->klass) &&
4106 strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
4108 MonoInst *ldelem, *store, *load;
4109 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
4111 n = mini_type_to_stind (cfg, &eklass->byval_arg);
4116 NEW_LDELEMA (cfg, ldelem, sp, eklass);
4117 ldelem->flags |= MONO_INST_NORANGECHECK;
4118 MONO_INST_NEW (cfg, store, n);
4119 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &eklass->byval_arg));
4120 type_to_eval_stack_type (cfg, &eklass->byval_arg, load);
4121 load->inst_left = ldelem;
4122 store->inst_left = args [2];
4123 store->inst_right = load;
4125 } else if (cmethod->klass == mono_defaults.math_class) {
4127 * There is general branches code for Min/Max, but it does not work for
4129 * http://everything2.com/?node_id=1051618
4131 } else if (cmethod->klass->image == mono_defaults.corlib &&
4132 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4133 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4136 #if SIZEOF_VOID_P == 8
4137 if (strcmp (cmethod->name, "Read") == 0 && (fsig->params [0]->type == MONO_TYPE_I8)) {
4138 /* 64 bit reads are already atomic */
4139 MONO_INST_NEW (cfg, ins, CEE_LDIND_I8);
4140 ins->inst_i0 = args [0];
4144 #ifdef MONO_ARCH_HAVE_ATOMIC_ADD
4145 if (strcmp (cmethod->name, "Increment") == 0) {
4146 MonoInst *ins_iconst;
4149 if (fsig->params [0]->type == MONO_TYPE_I4)
4150 opcode = OP_ATOMIC_ADD_NEW_I4;
4151 else if (fsig->params [0]->type == MONO_TYPE_I8)
4152 opcode = OP_ATOMIC_ADD_NEW_I8;
4154 g_assert_not_reached ();
4156 #if SIZEOF_VOID_P == 4
4157 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4161 MONO_INST_NEW (cfg, ins, opcode);
4162 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4163 ins_iconst->inst_c0 = 1;
4165 ins->inst_i0 = args [0];
4166 ins->inst_i1 = ins_iconst;
4167 } else if (strcmp (cmethod->name, "Decrement") == 0) {
4168 MonoInst *ins_iconst;
4171 if (fsig->params [0]->type == MONO_TYPE_I4)
4172 opcode = OP_ATOMIC_ADD_NEW_I4;
4173 else if (fsig->params [0]->type == MONO_TYPE_I8)
4174 opcode = OP_ATOMIC_ADD_NEW_I8;
4176 g_assert_not_reached ();
4178 #if SIZEOF_VOID_P == 4
4179 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4183 MONO_INST_NEW (cfg, ins, opcode);
4184 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4185 ins_iconst->inst_c0 = -1;
4187 ins->inst_i0 = args [0];
4188 ins->inst_i1 = ins_iconst;
4189 } else if (strcmp (cmethod->name, "Add") == 0) {
4192 if (fsig->params [0]->type == MONO_TYPE_I4)
4193 opcode = OP_ATOMIC_ADD_NEW_I4;
4194 else if (fsig->params [0]->type == MONO_TYPE_I8)
4195 opcode = OP_ATOMIC_ADD_NEW_I8;
4197 g_assert_not_reached ();
4199 #if SIZEOF_VOID_P == 4
4200 if (opcode == OP_ATOMIC_ADD_NEW_I8)
4204 MONO_INST_NEW (cfg, ins, opcode);
4206 ins->inst_i0 = args [0];
4207 ins->inst_i1 = args [1];
4209 #endif /* MONO_ARCH_HAVE_ATOMIC_ADD */
4211 #ifdef MONO_ARCH_HAVE_ATOMIC_EXCHANGE
4212 if (strcmp (cmethod->name, "Exchange") == 0) {
4215 if (fsig->params [0]->type == MONO_TYPE_I4)
4216 opcode = OP_ATOMIC_EXCHANGE_I4;
4217 #if SIZEOF_VOID_P == 8
4218 else if ((fsig->params [0]->type == MONO_TYPE_I8) ||
4219 (fsig->params [0]->type == MONO_TYPE_I) ||
4220 (fsig->params [0]->type == MONO_TYPE_OBJECT))
4221 opcode = OP_ATOMIC_EXCHANGE_I8;
4223 else if ((fsig->params [0]->type == MONO_TYPE_I) ||
4224 (fsig->params [0]->type == MONO_TYPE_OBJECT))
4225 opcode = OP_ATOMIC_EXCHANGE_I4;
4230 #if SIZEOF_VOID_P == 4
4231 if (opcode == OP_ATOMIC_EXCHANGE_I8)
4235 MONO_INST_NEW (cfg, ins, opcode);
4237 ins->inst_i0 = args [0];
4238 ins->inst_i1 = args [1];
4240 #endif /* MONO_ARCH_HAVE_ATOMIC_EXCHANGE */
4242 #ifdef MONO_ARCH_HAVE_ATOMIC_CAS_IMM
4244 * Can't implement CompareExchange methods this way since they have
4245 * three arguments. We can implement one of the common cases, where the new
4246 * value is a constant.
4248 if ((strcmp (cmethod->name, "CompareExchange") == 0)) {
4249 if (fsig->params [1]->type == MONO_TYPE_I4 && args [2]->opcode == OP_ICONST) {
4250 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_IMM_I4);
4251 ins->inst_i0 = args [0];
4252 ins->inst_i1 = args [1];
4253 ins->backend.data = GINT_TO_POINTER (args [2]->inst_c0);
4255 /* The I8 case is hard to detect, since the arg might be a conv.i8 (iconst) tree */
4257 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS_IMM */
4261 } else if (cmethod->klass->image == mono_defaults.corlib) {
4262 if (cmethod->name [0] == 'B' && strcmp (cmethod->name, "Break") == 0
4263 && strcmp (cmethod->klass->name, "Debugger") == 0) {
4264 MONO_INST_NEW (cfg, ins, OP_BREAK);
4267 if (cmethod->name [0] == 'g' && strcmp (cmethod->name, "get_IsRunningOnWindows") == 0
4268 && strcmp (cmethod->klass->name, "Environment") == 0) {
4269 #ifdef PLATFORM_WIN32
4270 NEW_ICONST (cfg, ins, 1);
4272 NEW_ICONST (cfg, ins, 0);
4278 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
4282 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
4284 MonoInst *store, *temp;
4287 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
4289 if (!sig->hasthis && sig->param_count == 0)
4293 if (sp [0]->opcode == OP_ICONST) {
4296 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
4298 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4299 /* FIXME: handle CEE_STIND_R4 */
4300 store->cil_code = sp [0]->cil_code;
4301 MONO_ADD_INS (bblock, store);
4306 for (i = 0; i < sig->param_count; ++i) {
4307 if (sp [0]->opcode == OP_ICONST) {
4310 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
4312 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4313 store->cil_code = sp [0]->cil_code;
4314 /* FIXME: handle CEE_STIND_R4 */
4315 if (store->opcode == CEE_STOBJ) {
4316 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4317 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
4318 #ifdef MONO_ARCH_SOFT_FLOAT
4319 } else if (store->opcode == CEE_STIND_R4) {
4320 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4321 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
4324 MONO_ADD_INS (bblock, store);
4330 #define MONO_INLINE_CALLED_LIMITED_METHODS 0
4331 #define MONO_INLINE_CALLER_LIMITED_METHODS 0
4333 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4335 mono_inline_called_method_name_limit = NULL;
4336 static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
4337 char *called_method_name = mono_method_full_name (called_method, TRUE);
4340 if (mono_inline_called_method_name_limit == NULL) {
4341 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
4342 if (limit_string != NULL) {
4343 mono_inline_called_method_name_limit = limit_string;
4345 mono_inline_called_method_name_limit = (char *) "";
4349 strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
4350 g_free (called_method_name);
4352 //return (strncmp_result <= 0);
4353 return (strncmp_result == 0);
4357 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4359 mono_inline_caller_method_name_limit = NULL;
4360 static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
4361 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
4364 if (mono_inline_caller_method_name_limit == NULL) {
4365 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
4366 if (limit_string != NULL) {
4367 mono_inline_caller_method_name_limit = limit_string;
4369 mono_inline_caller_method_name_limit = (char *) "";
4373 strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
4374 g_free (caller_method_name);
4376 //return (strncmp_result <= 0);
4377 return (strncmp_result == 0);
4382 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
4383 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
4385 MonoInst *ins, *rvar = NULL;
4386 MonoMethodHeader *cheader;
4387 MonoBasicBlock *ebblock, *sbblock;
4388 int i, costs, new_locals_offset;
4389 MonoMethod *prev_inlined_method;
4390 MonoBasicBlock **prev_cil_offset_to_bb;
4391 unsigned char* prev_cil_start;
4392 guint32 prev_cil_offset_to_bb_len;
4394 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
4396 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4397 if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
4400 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4401 if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
4405 if (bblock->out_of_line && !inline_allways)
4408 if (cfg->verbose_level > 2)
4409 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4411 if (!cmethod->inline_info) {
4412 mono_jit_stats.inlineable_methods++;
4413 cmethod->inline_info = 1;
4415 /* allocate space to store the return value */
4416 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4417 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4420 /* allocate local variables */
4421 cheader = mono_method_get_header (cmethod);
4422 new_locals_offset = cfg->num_varinfo;
4423 for (i = 0; i < cheader->num_locals; ++i)
4424 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
4426 /* allocate starte and end blocks */
4427 sbblock = NEW_BBLOCK (cfg);
4428 sbblock->block_num = cfg->num_bblocks++;
4429 sbblock->real_offset = real_offset;
4431 ebblock = NEW_BBLOCK (cfg);
4432 ebblock->block_num = cfg->num_bblocks++;
4433 ebblock->real_offset = real_offset;
4435 prev_inlined_method = cfg->inlined_method;
4436 cfg->inlined_method = cmethod;
4437 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
4438 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
4439 prev_cil_start = cfg->cil_start;
4441 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
4443 cfg->inlined_method = prev_inlined_method;
4444 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
4445 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
4446 cfg->cil_start = prev_cil_start;
4448 if ((costs >= 0 && costs < 60) || inline_allways) {
4449 if (cfg->verbose_level > 2)
4450 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4452 mono_jit_stats.inlined_methods++;
4454 /* always add some code to avoid block split failures */
4455 MONO_INST_NEW (cfg, ins, OP_NOP);
4456 MONO_ADD_INS (bblock, ins);
4459 bblock->next_bb = sbblock;
4460 link_bblock (cfg, bblock, sbblock);
4463 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
4464 NEW_TEMPLOAD_SOFT_FLOAT (cfg, ebblock, ins, rvar->inst_c0, ip);
4470 if (cfg->verbose_level > 2)
4471 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
4472 cfg->exception_type = MONO_EXCEPTION_NONE;
4473 mono_loader_clear_error ();
4474 cmethod->inline_failure = TRUE;
4480 * Some of these comments may well be out-of-date.
4481 * Design decisions: we do a single pass over the IL code (and we do bblock
4482 * splitting/merging in the few cases when it's required: a back jump to an IL
4483 * address that was not already seen as bblock starting point).
4484 * Code is validated as we go (full verification is still better left to metadata/verify.c).
4485 * Complex operations are decomposed in simpler ones right away. We need to let the
4486 * arch-specific code peek and poke inside this process somehow (except when the
4487 * optimizations can take advantage of the full semantic info of coarse opcodes).
4488 * All the opcodes of the form opcode.s are 'normalized' to opcode.
4489 * MonoInst->opcode initially is the IL opcode or some simplification of that
4490 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
4491 * opcode with value bigger than OP_LAST.
4492 * At this point the IR can be handed over to an interpreter, a dumb code generator
4493 * or to the optimizing code generator that will translate it to SSA form.
4495 * Profiling directed optimizations.
4496 * We may compile by default with few or no optimizations and instrument the code
4497 * or the user may indicate what methods to optimize the most either in a config file
4498 * or through repeated runs where the compiler applies offline the optimizations to
4499 * each method and then decides if it was worth it.
4503 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
4504 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
4505 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
4506 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
4507 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
4508 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
4509 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
4510 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) {cfg->exception_ptr = klass; goto load_error;}
4512 /* offset from br.s -> br like opcodes */
4513 #define BIG_BRANCH_OFFSET 13
4515 static inline gboolean
4516 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
4518 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
4520 return b == NULL || b == bb;
4524 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
4526 unsigned char *ip = start;
4527 unsigned char *target;
4530 MonoBasicBlock *bblock;
4531 const MonoOpcode *opcode;
4534 cli_addr = ip - start;
4535 i = mono_opcode_value ((const guint8 **)&ip, end);
4538 opcode = &mono_opcodes [i];
4539 switch (opcode->argument) {
4540 case MonoInlineNone:
4543 case MonoInlineString:
4544 case MonoInlineType:
4545 case MonoInlineField:
4546 case MonoInlineMethod:
4549 case MonoShortInlineR:
4556 case MonoShortInlineVar:
4557 case MonoShortInlineI:
4560 case MonoShortInlineBrTarget:
4561 target = start + cli_addr + 2 + (signed char)ip [1];
4562 GET_BBLOCK (cfg, bblock, target);
4565 GET_BBLOCK (cfg, bblock, ip);
4567 case MonoInlineBrTarget:
4568 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
4569 GET_BBLOCK (cfg, bblock, target);
4572 GET_BBLOCK (cfg, bblock, ip);
4574 case MonoInlineSwitch: {
4575 guint32 n = read32 (ip + 1);
4578 cli_addr += 5 + 4 * n;
4579 target = start + cli_addr;
4580 GET_BBLOCK (cfg, bblock, target);
4582 for (j = 0; j < n; ++j) {
4583 target = start + cli_addr + (gint32)read32 (ip);
4584 GET_BBLOCK (cfg, bblock, target);
4594 g_assert_not_reached ();
4597 if (i == CEE_THROW) {
4598 unsigned char *bb_start = ip - 1;
4600 /* Find the start of the bblock containing the throw */
4602 while ((bb_start >= start) && !bblock) {
4603 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
4607 bblock->out_of_line = 1;
4617 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
4619 MonoInst *store, *temp, *load;
4621 if (ip_in_bb (cfg, bblock, ip_next) &&
4622 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
4625 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4626 temp->flags |= MONO_INST_IS_TEMP;
4627 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4628 /* FIXME: handle CEE_STIND_R4 */
4629 store->cil_code = ins->cil_code;
4630 MONO_ADD_INS (bblock, store);
4631 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4632 load->cil_code = ins->cil_code;
4636 static inline MonoMethod *
4637 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4641 if (m->wrapper_type != MONO_WRAPPER_NONE)
4642 return mono_method_get_wrapper_data (m, token);
4644 method = mono_get_method_full (m->klass->image, token, klass, context);
4649 static inline MonoMethod *
4650 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4652 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
4654 if (method && cfg && !cfg->generic_sharing_context && mono_class_is_open_constructed_type (&method->klass->byval_arg))
4660 static inline MonoClass*
4661 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
4665 if (method->wrapper_type != MONO_WRAPPER_NONE)
4666 klass = mono_method_get_wrapper_data (method, token);
4668 klass = mono_class_get_full (method->klass->image, token, context);
4670 mono_class_init (klass);
4675 * Returns TRUE if the JIT should abort inlining because "callee"
4676 * is influenced by security attributes.
4679 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
4683 if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
4687 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
4688 if (result == MONO_JIT_SECURITY_OK)
4691 if (result == MONO_JIT_LINKDEMAND_ECMA) {
4692 /* Generate code to throw a SecurityException before the actual call/link */
4693 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4696 NEW_ICONST (cfg, args [0], 4);
4697 NEW_METHODCONST (cfg, args [1], caller);
4698 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
4699 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
4700 /* don't hide previous results */
4701 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
4702 cfg->exception_data = result;
4710 method_access_exception (void)
4712 static MonoMethod *method = NULL;
4715 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4716 method = mono_class_get_method_from_name (secman->securitymanager,
4717 "MethodAccessException", 2);
4724 emit_throw_method_access_exception (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4725 MonoBasicBlock *bblock, unsigned char *ip)
4727 MonoMethod *thrower = method_access_exception ();
4730 NEW_METHODCONST (cfg, args [0], caller);
4731 NEW_METHODCONST (cfg, args [1], callee);
4732 mono_emit_method_call_spilled (cfg, bblock, thrower,
4733 mono_method_signature (thrower), args, ip, NULL);
4737 verification_exception (void)
4739 static MonoMethod *method = NULL;
4742 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4743 method = mono_class_get_method_from_name (secman->securitymanager,
4744 "VerificationException", 0);
4751 emit_throw_verification_exception (MonoCompile *cfg, MonoBasicBlock *bblock, unsigned char *ip)
4753 MonoMethod *thrower = verification_exception ();
4755 mono_emit_method_call_spilled (cfg, bblock, thrower,
4756 mono_method_signature (thrower),
4761 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4762 MonoBasicBlock *bblock, unsigned char *ip)
4764 MonoSecurityCoreCLRLevel caller_level = mono_security_core_clr_method_level (caller, TRUE);
4765 MonoSecurityCoreCLRLevel callee_level = mono_security_core_clr_method_level (callee, TRUE);
4766 gboolean is_safe = TRUE;
4768 if (!(caller_level >= callee_level ||
4769 caller_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL ||
4770 callee_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL)) {
4775 emit_throw_method_access_exception (cfg, caller, callee, bblock, ip);
4779 method_is_safe (MonoMethod *method)
4782 if (strcmp (method->name, "unsafeMethod") == 0)
4789 * Check that the IL instructions at ip are the array initialization
4790 * sequence and return the pointer to the data and the size.
4793 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoInst *newarr, int *out_size)
4796 * newarr[System.Int32]
4798 * ldtoken field valuetype ...
4799 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
4801 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
4802 MonoClass *klass = newarr->inst_newa_class;
4803 guint32 field_token = read32 (ip + 2);
4804 guint32 field_index = field_token & 0xffffff;
4805 guint32 token = read32 (ip + 7);
4807 const char *data_ptr;
4809 MonoMethod *cmethod;
4810 MonoClass *dummy_class;
4811 MonoClassField *field = mono_field_from_token (method->klass->image, field_token, &dummy_class, NULL);
4817 if (newarr->inst_newa_len->opcode != OP_ICONST)
4819 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
4822 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
4824 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
4825 case MONO_TYPE_BOOLEAN:
4829 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
4830 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
4831 case MONO_TYPE_CHAR:
4841 return NULL; /* stupid ARM FP swapped format */
4850 size *= newarr->inst_newa_len->inst_c0;
4851 if (size > mono_type_size (field->type, &dummy_align))
4854 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
4855 field_index = read32 (ip + 2) & 0xffffff;
4856 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
4857 data_ptr = mono_image_rva_map (method->klass->image, rva);
4858 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
4859 /* for aot code we do the lookup on load */
4860 if (aot && data_ptr)
4861 return GUINT_TO_POINTER (rva);
4868 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
4870 char *method_fname = mono_method_full_name (method, TRUE);
4873 if (mono_method_get_header (method)->code_size == 0)
4874 method_code = g_strdup ("method body is empty.");
4876 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
4877 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
4878 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
4879 g_free (method_fname);
4880 g_free (method_code);
4884 set_exception_object (MonoCompile *cfg, MonoException *exception)
4886 cfg->exception_type = MONO_EXCEPTION_OBJECT_SUPPLIED;
4887 MONO_GC_REGISTER_ROOT (cfg->exception_ptr);
4888 cfg->exception_ptr = exception;
4892 get_runtime_generic_context (MonoCompile *cfg, MonoMethod *method, int context_used, MonoInst *this, unsigned char *ip)
4894 g_assert (!method->klass->valuetype);
4896 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
4897 MonoInst *mrgctx_loc, *mrgctx_var;
4900 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
4902 mrgctx_loc = mono_get_vtable_var (cfg);
4903 NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
4906 } else if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4907 MonoInst *vtable_loc, *vtable_var;
4911 vtable_loc = mono_get_vtable_var (cfg);
4912 NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
4914 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
4915 MonoInst *mrgctx_var = vtable_var;
4917 g_assert (G_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable) == 0);
4919 MONO_INST_NEW (cfg, vtable_var, CEE_LDIND_I);
4920 vtable_var->cil_code = ip;
4921 vtable_var->inst_left = mrgctx_var;
4922 vtable_var->type = STACK_PTR;
4931 MONO_INST_NEW (cfg, vtable, CEE_LDIND_I);
4932 vtable->inst_left = this;
4933 vtable->type = STACK_PTR;
4940 get_runtime_generic_context_other_table_ptr (MonoCompile *cfg, MonoBasicBlock *bblock,
4941 MonoInst *rgc_ptr, guint32 slot, const unsigned char *ip)
4943 MonoMethodSignature *sig = helper_sig_rgctx_lazy_fetch_trampoline;
4944 guint8 *tramp = mono_create_rgctx_lazy_fetch_trampoline (slot);
4948 temp = mono_emit_native_call (cfg, bblock, tramp, sig, &rgc_ptr, ip, FALSE, FALSE);
4950 NEW_TEMPLOAD (cfg, field, temp);
4956 get_runtime_generic_context_ptr (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4957 MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, unsigned char *ip)
4959 guint32 slot = mono_method_lookup_or_register_other_info (method,
4960 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, &klass->byval_arg, rgctx_type, generic_context);
4962 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4966 get_runtime_generic_context_method (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4967 MonoMethod *cmethod, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, const unsigned char *ip)
4969 guint32 slot = mono_method_lookup_or_register_other_info (method,
4970 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, cmethod, rgctx_type, generic_context);
4972 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4976 get_runtime_generic_context_field (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4977 MonoClassField *field, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type,
4978 const unsigned char *ip)
4980 guint32 slot = mono_method_lookup_or_register_other_info (method,
4981 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, field, rgctx_type, generic_context);
4983 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4987 get_runtime_generic_context_method_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4988 MonoMethod *rgctx_method, MonoGenericContext *generic_context, MonoInst *rgctx, const unsigned char *ip)
4990 guint32 slot = mono_method_lookup_or_register_other_info (method,
4991 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, rgctx_method,
4992 MONO_RGCTX_INFO_METHOD_RGCTX, generic_context);
4994 return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4998 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5002 if (cfg->generic_sharing_context)
5003 type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, &klass->byval_arg);
5005 type = &klass->byval_arg;
5006 return MONO_TYPE_IS_REFERENCE (type);
5010 * Handles unbox of a Nullable<T>, returning a temp variable where the
5011 * result is stored. If a rgctx is passed, then shared generic code
5015 handle_unbox_nullable (MonoCompile* cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock* bblock,
5016 MonoInst* val, const guchar *ip, MonoClass* klass, MonoGenericContext *generic_context, MonoInst *rgctx)
5018 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
5019 MonoMethodSignature *signature = mono_method_signature (method);
5022 MonoInst *addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
5023 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
5025 return mono_emit_rgctx_calli_spilled (cfg, bblock, signature, &val, addr, NULL, ip);
5027 return mono_emit_method_call_spilled (cfg, bblock, method, signature, &val, ip, NULL);
5032 handle_box_nullable_from_inst (MonoCompile *cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock *bblock,
5033 MonoInst *val, const guchar *ip, MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx)
5035 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
5036 MonoInst *dest, *method_addr;
5039 g_assert (mono_class_is_nullable (klass));
5041 method_addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
5042 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
5043 temp = mono_emit_rgctx_calli_spilled (cfg, bblock, mono_method_signature (method), &val,
5044 method_addr, NULL, ip);
5045 NEW_TEMPLOAD (cfg, dest, temp);
5050 emit_castclass (MonoClass *klass, guint32 token, int context_used, gboolean inst_is_castclass, MonoCompile *cfg,
5051 MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
5052 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
5053 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
5055 MonoBasicBlock *bblock = *_bblock;
5056 unsigned char *ip = *_ip;
5057 MonoInst **sp = *_sp;
5058 int inline_costs = *_inline_costs;
5059 guint real_offset = *_real_offset;
5060 int return_value = 0;
5063 MonoInst *rgctx, *args [2];
5066 g_assert (!method->klass->valuetype);
5072 GET_RGCTX (rgctx, context_used);
5073 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
5074 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5076 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass, args, ip);
5077 NEW_TEMPLOAD (cfg, *sp, temp);
5082 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5084 MonoMethod *mono_castclass;
5085 MonoInst *iargs [1];
5086 MonoBasicBlock *ebblock;
5090 mono_castclass = mono_marshal_get_castclass (klass);
5093 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5094 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5096 g_assert (costs > 0);
5101 GET_BBLOCK (cfg, bblock, ip);
5102 ebblock->next_bb = bblock;
5103 link_bblock (cfg, ebblock, bblock);
5105 temp = iargs [0]->inst_i0->inst_c0;
5106 NEW_TEMPLOAD (cfg, *sp, temp);
5110 inline_costs += costs;
5114 /* Needed by the code generated in inssel.brg */
5115 mono_get_got_var (cfg);
5117 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5118 ins->type = STACK_OBJ;
5119 ins->inst_left = *sp;
5121 ins->inst_newa_class = klass;
5122 if (inst_is_castclass)
5123 ins->backend.record_cast_details = debug_options.better_cast_details;
5124 if (inst_is_castclass)
5125 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5135 *_inline_costs = inline_costs;
5136 *_real_offset = real_offset;
5137 return return_value;
5147 emit_unbox (MonoClass *klass, guint32 token, int context_used,
5148 MonoCompile *cfg, MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
5149 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
5150 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
5152 MonoBasicBlock *bblock = *_bblock;
5153 unsigned char *ip = *_ip;
5154 MonoInst **sp = *_sp;
5155 int inline_costs = *_inline_costs;
5156 guint real_offset = *_real_offset;
5157 int return_value = 0;
5159 MonoInst *add, *vtoffset, *ins;
5161 /* Needed by the code generated in inssel.brg */
5162 mono_get_got_var (cfg);
5165 MonoInst *rgctx, *element_class;
5167 /* This assertion is from the unboxcast insn */
5168 g_assert (klass->rank == 0);
5170 GET_RGCTX (rgctx, context_used);
5171 element_class = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
5172 klass->element_class, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5174 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST_REG);
5175 ins->type = STACK_OBJ;
5176 ins->inst_left = *sp;
5177 ins->inst_right = element_class;
5181 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5182 ins->type = STACK_OBJ;
5183 ins->inst_left = *sp;
5185 ins->inst_newa_class = klass;
5189 MONO_INST_NEW (cfg, add, OP_PADD);
5190 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5191 add->inst_left = ins;
5192 add->inst_right = vtoffset;
5193 add->type = STACK_MP;
5201 *_inline_costs = inline_costs;
5202 *_real_offset = real_offset;
5203 return return_value;
5210 mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method)
5212 MonoAssembly *assembly = method->klass->image->assembly;
5213 if (method->wrapper_type != MONO_WRAPPER_NONE)
5215 if (assembly->in_gac || assembly->image == mono_defaults.corlib)
5217 if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE)
5219 return mono_assembly_has_skip_verification (assembly);
5223 * mini_method_verify:
5225 * Verify the method using the new verfier.
5227 * Returns true if the method is invalid.
5230 mini_method_verify (MonoCompile *cfg, MonoMethod *method)
5233 gboolean is_fulltrust;
5234 MonoLoaderError *error;
5236 if (method->verification_success)
5239 is_fulltrust = mono_verifier_is_method_full_trust (method);
5241 if (!mono_verifier_is_enabled_for_method (method))
5244 res = mono_method_verify_with_current_settings (method, cfg->skip_visibility);
5246 if ((error = mono_loader_get_last_error ())) {
5247 cfg->exception_type = error->exception_type;
5249 mono_free_verify_list (res);
5254 for (tmp = res; tmp; tmp = tmp->next) {
5255 MonoVerifyInfoExtended *info = (MonoVerifyInfoExtended *)tmp->data;
5256 if (info->info.status == MONO_VERIFY_ERROR) {
5257 cfg->exception_type = info->exception_type;
5258 cfg->exception_message = g_strdup (info->info.message);
5259 mono_free_verify_list (res);
5262 if (info->info.status == MONO_VERIFY_NOT_VERIFIABLE && !is_fulltrust) {
5263 cfg->exception_type = info->exception_type;
5264 cfg->exception_message = g_strdup (info->info.message);
5265 mono_free_verify_list (res);
5269 mono_free_verify_list (res);
5271 method->verification_success = 1;
5276 * mono_method_to_ir: translates IL into basic blocks containing trees
5279 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
5280 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
5281 guint inline_offset, gboolean is_virtual_call)
5283 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
5284 MonoInst *ins, **sp, **stack_start;
5285 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
5286 MonoMethod *cmethod, *method_definition;
5287 MonoInst **arg_array;
5288 MonoMethodHeader *header;
5290 guint32 token, ins_flag;
5292 MonoClass *constrained_call = NULL;
5293 unsigned char *ip, *end, *target, *err_pos;
5294 static double r8_0 = 0.0;
5295 MonoMethodSignature *sig;
5296 MonoGenericContext *generic_context = NULL;
5297 MonoGenericContainer *generic_container = NULL;
5298 MonoType **param_types;
5299 GList *bb_recheck = NULL, *tmp;
5300 int i, n, start_new_bblock, ialign;
5301 int num_calls = 0, inline_costs = 0;
5302 int breakpoint_id = 0;
5304 guint real_offset, num_args;
5305 MonoBoolean security, pinvoke;
5306 MonoSecurityManager* secman = NULL;
5307 MonoDeclSecurityActions actions;
5308 GSList *class_inits = NULL;
5309 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
5312 /* serialization and xdomain stuff may need access to private fields and methods */
5313 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
5314 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
5315 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
5316 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
5317 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
5318 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
5320 /* turn off visibility checks for smcs */
5321 dont_verify |= mono_security_get_mode () == MONO_SECURITY_MODE_SMCS_HACK;
5323 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
5324 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
5325 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
5326 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
5328 image = method->klass->image;
5329 header = mono_method_get_header (method);
5330 generic_container = mono_method_get_generic_container (method);
5331 sig = mono_method_signature (method);
5332 num_args = sig->hasthis + sig->param_count;
5333 ip = (unsigned char*)header->code;
5334 cfg->cil_start = ip;
5335 end = ip + header->code_size;
5336 mono_jit_stats.cil_code_size += header->code_size;
5338 method_definition = method;
5339 while (method_definition->is_inflated) {
5340 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
5341 method_definition = imethod->declaring;
5344 /* SkipVerification is not allowed if core-clr is enabled */
5345 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
5347 dont_verify_stloc = TRUE;
5350 if (!dont_verify && mini_method_verify (cfg, method_definition))
5351 goto exception_exit;
5353 if (sig->is_inflated)
5354 generic_context = mono_method_get_context (method);
5355 else if (generic_container)
5356 generic_context = &generic_container->context;
5358 if (!cfg->generic_sharing_context)
5359 g_assert (!sig->has_type_parameters);
5361 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
5362 g_assert (method->is_inflated);
5363 g_assert (mono_method_get_context (method)->method_inst);
5365 if (method->is_inflated && mono_method_get_context (method)->method_inst)
5366 g_assert (sig->generic_param_count);
5368 if (cfg->method == method)
5371 real_offset = inline_offset;
5373 cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
5374 cfg->cil_offset_to_bb_len = header->code_size;
5376 if (cfg->verbose_level > 2)
5377 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
5379 dont_inline = g_list_prepend (dont_inline, method);
5380 if (cfg->method == method) {
5382 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
5383 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
5386 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
5387 start_bblock->cil_code = NULL;
5388 start_bblock->cil_length = 0;
5389 start_bblock->block_num = cfg->num_bblocks++;
5392 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
5393 end_bblock->cil_code = NULL;
5394 end_bblock->cil_length = 0;
5395 end_bblock->block_num = cfg->num_bblocks++;
5396 g_assert (cfg->num_bblocks == 2);
5398 arg_array = alloca (sizeof (MonoInst *) * num_args);
5399 for (i = num_args - 1; i >= 0; i--)
5400 arg_array [i] = cfg->varinfo [i];
5402 if (header->num_clauses) {
5403 cfg->spvars = g_hash_table_new (NULL, NULL);
5404 cfg->exvars = g_hash_table_new (NULL, NULL);
5406 /* handle exception clauses */
5407 for (i = 0; i < header->num_clauses; ++i) {
5408 MonoBasicBlock *try_bb;
5409 MonoExceptionClause *clause = &header->clauses [i];
5411 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
5412 try_bb->real_offset = clause->try_offset;
5413 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
5414 tblock->real_offset = clause->handler_offset;
5415 tblock->flags |= BB_EXCEPTION_HANDLER;
5417 link_bblock (cfg, try_bb, tblock);
5419 if (*(ip + clause->handler_offset) == CEE_POP)
5420 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
5422 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
5423 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
5424 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
5425 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5426 MONO_ADD_INS (tblock, ins);
5428 /* todo: is a fault block unsafe to optimize? */
5429 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
5430 tblock->flags |= BB_EXCEPTION_UNSAFE;
5434 /*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);
5436 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
5438 /* catch and filter blocks get the exception object on the stack */
5439 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
5440 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5441 MonoInst *load, *dummy_use;
5443 /* mostly like handle_stack_args (), but just sets the input args */
5444 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
5445 tblock->in_scount = 1;
5446 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5447 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5450 * Add a dummy use for the exvar so its liveness info will be
5453 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
5454 NEW_DUMMY_USE (cfg, dummy_use, load);
5455 MONO_ADD_INS (tblock, dummy_use);
5457 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5458 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
5459 tblock->real_offset = clause->data.filter_offset;
5460 tblock->in_scount = 1;
5461 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5462 /* The filter block shares the exvar with the handler block */
5463 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5464 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5465 MONO_ADD_INS (tblock, ins);
5469 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
5470 clause->data.catch_class &&
5471 cfg->generic_sharing_context &&
5472 mono_class_check_context_used (clause->data.catch_class)) {
5473 if (mono_method_get_context (method)->method_inst)
5474 GENERIC_SHARING_FAILURE (CEE_NOP);
5477 * In shared generic code with catch
5478 * clauses containing type variables
5479 * the exception handling code has to
5480 * be able to get to the rgctx.
5481 * Therefore we have to make sure that
5482 * the vtable/mrgctx argument (for
5483 * static or generic methods) or the
5484 * "this" argument (for non-static
5485 * methods) are live.
5487 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
5488 mini_method_get_context (method)->method_inst) {
5489 mono_get_vtable_var (cfg);
5491 MonoInst *this, *dummy_use;
5492 MonoType *this_type;
5494 if (method->klass->valuetype)
5495 this_type = &method->klass->this_arg;
5497 this_type = &method->klass->byval_arg;
5499 if (arg_array [0]->opcode == OP_ICONST) {
5500 this = arg_array [0];
5502 this = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
5503 this->ssa_op = MONO_SSA_LOAD;
5504 this->inst_i0 = arg_array [0];
5505 this->opcode = mini_type_to_ldind ((cfg), this->inst_i0->inst_vtype);
5506 type_to_eval_stack_type ((cfg), this_type, this);
5507 this->klass = this->inst_i0->klass;
5510 NEW_DUMMY_USE (cfg, dummy_use, this);
5511 MONO_ADD_INS (tblock, dummy_use);
5516 arg_array = alloca (sizeof (MonoInst *) * num_args);
5517 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
5520 /* FIRST CODE BLOCK */
5521 bblock = NEW_BBLOCK (cfg);
5522 bblock->cil_code = ip;
5524 ADD_BBLOCK (cfg, bblock);
5526 if (cfg->method == method) {
5527 breakpoint_id = mono_debugger_method_has_breakpoint (method);
5528 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
5529 MONO_INST_NEW (cfg, ins, OP_BREAK);
5530 MONO_ADD_INS (bblock, ins);
5534 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
5535 secman = mono_security_manager_get_methods ();
5537 security = (secman && mono_method_has_declsec (method));
5538 /* at this point having security doesn't mean we have any code to generate */
5539 if (security && (cfg->method == method)) {
5540 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
5541 * And we do not want to enter the next section (with allocation) if we
5542 * have nothing to generate */
5543 security = mono_declsec_get_demands (method, &actions);
5546 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
5547 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
5549 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5550 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5551 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
5553 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
5554 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5558 mono_custom_attrs_free (custom);
5561 custom = mono_custom_attrs_from_class (wrapped->klass);
5562 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5566 mono_custom_attrs_free (custom);
5569 /* not a P/Invoke after all */
5574 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || cfg->compile_aot || security || pinvoke) {
5575 /* we use a separate basic block for the initialization code */
5576 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
5577 init_localsbb->real_offset = real_offset;
5578 start_bblock->next_bb = init_localsbb;
5579 init_localsbb->next_bb = bblock;
5580 link_bblock (cfg, start_bblock, init_localsbb);
5581 link_bblock (cfg, init_localsbb, bblock);
5582 init_localsbb->block_num = cfg->num_bblocks++;
5584 start_bblock->next_bb = bblock;
5585 link_bblock (cfg, start_bblock, bblock);
5588 /* at this point we know, if security is TRUE, that some code needs to be generated */
5589 if (security && (cfg->method == method)) {
5592 mono_jit_stats.cas_demand_generation++;
5594 if (actions.demand.blob) {
5595 /* Add code for SecurityAction.Demand */
5596 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
5597 NEW_ICONST (cfg, args [1], actions.demand.size);
5598 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5599 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5601 if (actions.noncasdemand.blob) {
5602 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
5603 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
5604 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
5605 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
5606 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5607 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5609 if (actions.demandchoice.blob) {
5610 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
5611 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
5612 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
5613 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
5614 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
5618 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
5620 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
5623 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
5624 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
5625 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5626 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5627 if (!(method->klass && method->klass->image &&
5628 mono_security_core_clr_is_platform_image (method->klass->image))) {
5629 emit_throw_method_access_exception (cfg, method, wrapped, bblock, ip);
5633 if (!method_is_safe (method))
5634 emit_throw_verification_exception (cfg, bblock, ip);
5637 if (header->code_size == 0)
5640 if (get_basic_blocks (cfg, header, real_offset, ip, end, &err_pos)) {
5645 if (cfg->method == method)
5646 mono_debug_init_method (cfg, bblock, breakpoint_id);
5648 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
5650 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
5651 for (n = 0; n < sig->param_count; ++n)
5652 param_types [n + sig->hasthis] = sig->params [n];
5653 for (n = 0; n < header->num_locals; ++n) {
5654 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
5659 /* do this somewhere outside - not here */
5660 NEW_ICONST (cfg, zero_int32, 0);
5661 NEW_ICONST (cfg, zero_int64, 0);
5662 zero_int64->type = STACK_I8;
5663 NEW_PCONST (cfg, zero_ptr, 0);
5664 NEW_PCONST (cfg, zero_obj, 0);
5665 zero_obj->type = STACK_OBJ;
5667 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
5668 zero_r8->type = STACK_R8;
5669 zero_r8->inst_p0 = &r8_0;
5671 /* add a check for this != NULL to inlined methods */
5672 if (is_virtual_call) {
5673 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
5674 NEW_ARGLOAD (cfg, ins->inst_left, 0);
5676 MONO_ADD_INS (bblock, ins);
5679 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
5680 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
5683 start_new_bblock = 0;
5686 if (cfg->method == method)
5687 real_offset = ip - header->code;
5689 real_offset = inline_offset;
5694 if (start_new_bblock) {
5695 bblock->cil_length = ip - bblock->cil_code;
5696 if (start_new_bblock == 2) {
5697 g_assert (ip == tblock->cil_code);
5699 GET_BBLOCK (cfg, tblock, ip);
5701 bblock->next_bb = tblock;
5703 start_new_bblock = 0;
5704 for (i = 0; i < bblock->in_scount; ++i) {
5705 if (cfg->verbose_level > 3)
5706 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5707 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5710 g_slist_free (class_inits);
5713 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
5714 link_bblock (cfg, bblock, tblock);
5715 if (sp != stack_start) {
5716 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5718 CHECK_UNVERIFIABLE (cfg);
5720 bblock->next_bb = tblock;
5722 for (i = 0; i < bblock->in_scount; ++i) {
5723 if (cfg->verbose_level > 3)
5724 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
5725 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5728 g_slist_free (class_inits);
5733 bblock->real_offset = real_offset;
5735 if ((cfg->method == method) && cfg->coverage_info) {
5736 MonoInst *store, *one;
5737 guint32 cil_offset = ip - header->code;
5738 cfg->coverage_info->data [cil_offset].cil_code = ip;
5740 /* TODO: Use an increment here */
5741 NEW_ICONST (cfg, one, 1);
5744 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
5747 MONO_INST_NEW (cfg, store, CEE_STIND_I);
5748 store->inst_left = ins;
5749 store->inst_right = one;
5751 MONO_ADD_INS (bblock, store);
5754 if (cfg->verbose_level > 3)
5755 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
5759 MONO_INST_NEW (cfg, ins, OP_NOP);
5761 MONO_ADD_INS (bblock, ins);
5764 MONO_INST_NEW (cfg, ins, OP_BREAK);
5766 MONO_ADD_INS (bblock, ins);
5772 CHECK_STACK_OVF (1);
5773 n = (*ip)-CEE_LDARG_0;
5775 NEW_ARGLOAD (cfg, ins, n);
5776 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
5784 CHECK_STACK_OVF (1);
5785 n = (*ip)-CEE_LDLOC_0;
5787 NEW_LOCLOAD (cfg, ins, n);
5788 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
5797 n = (*ip)-CEE_STLOC_0;
5800 handle_loaded_temps (cfg, bblock, stack_start, sp);
5801 NEW_LOCSTORE (cfg, ins, n, *sp);
5802 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
5804 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
5805 if (ins->opcode == CEE_STOBJ) {
5806 NEW_LOCLOADA (cfg, ins, n);
5807 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5809 MONO_ADD_INS (bblock, ins);
5815 CHECK_STACK_OVF (1);
5817 NEW_ARGLOAD (cfg, ins, ip [1]);
5818 LDARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5824 CHECK_STACK_OVF (1);
5826 NEW_ARGLOADA (cfg, ins, ip [1]);
5835 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
5836 handle_loaded_temps (cfg, bblock, stack_start, sp);
5837 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
5839 STARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5840 if (ins->opcode == CEE_STOBJ) {
5841 NEW_ARGLOADA (cfg, ins, ip [1]);
5842 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5844 MONO_ADD_INS (bblock, ins);
5849 CHECK_STACK_OVF (1);
5850 CHECK_LOCAL (ip [1]);
5851 NEW_LOCLOAD (cfg, ins, ip [1]);
5852 LDLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5858 CHECK_STACK_OVF (1);
5859 CHECK_LOCAL (ip [1]);
5860 NEW_LOCLOADA (cfg, ins, ip [1]);
5868 handle_loaded_temps (cfg, bblock, stack_start, sp);
5869 CHECK_LOCAL (ip [1]);
5870 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
5871 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
5873 STLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5874 if (ins->opcode == CEE_STOBJ) {
5875 NEW_LOCLOADA (cfg, ins, ip [1]);
5876 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5878 MONO_ADD_INS (bblock, ins);
5883 CHECK_STACK_OVF (1);
5884 NEW_PCONST (cfg, ins, NULL);
5885 ins->type = STACK_OBJ;
5890 CHECK_STACK_OVF (1);
5891 NEW_ICONST (cfg, ins, -1);
5904 CHECK_STACK_OVF (1);
5905 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
5911 CHECK_STACK_OVF (1);
5913 NEW_ICONST (cfg, ins, *((signed char*)ip));
5919 CHECK_STACK_OVF (1);
5920 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
5926 CHECK_STACK_OVF (1);
5927 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5928 ins->type = STACK_I8;
5930 ins->inst_l = (gint64)read64 (ip);
5936 /* we should really allocate this only late in the compilation process */
5937 mono_domain_lock (cfg->domain);
5938 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
5939 mono_domain_unlock (cfg->domain);
5941 CHECK_STACK_OVF (1);
5942 MONO_INST_NEW (cfg, ins, OP_R4CONST);
5943 ins->type = STACK_R8;
5954 mono_domain_lock (cfg->domain);
5955 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
5956 mono_domain_unlock (cfg->domain);
5958 CHECK_STACK_OVF (1);
5959 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5960 ins->type = STACK_R8;
5970 MonoInst *temp, *store;
5972 CHECK_STACK_OVF (1);
5977 * small optimization: if the loaded value was from a local already,
5978 * just load it twice.
5980 if (ins->ssa_op == MONO_SSA_LOAD &&
5981 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
5983 MONO_INST_NEW (cfg, temp, 0);
5987 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5988 temp->flags |= MONO_INST_IS_TEMP;
5989 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5990 /* FIXME: handle CEE_STIND_R4 */
5991 if (store->opcode == CEE_STOBJ) {
5992 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
5993 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
5995 MONO_ADD_INS (bblock, store);
5997 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5999 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
6008 MONO_INST_NEW (cfg, ins, CEE_POP);
6009 MONO_ADD_INS (bblock, ins);
6016 if (stack_start != sp)
6018 MONO_INST_NEW (cfg, ins, OP_JMP);
6019 token = read32 (ip + 1);
6020 /* FIXME: check the signature matches */
6021 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
6026 if (cfg->generic_sharing_context && mono_method_check_context_used (cmethod))
6027 GENERIC_SHARING_FAILURE (CEE_JMP);
6029 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
6030 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6032 CHECK_CFG_EXCEPTION;
6035 ins->inst_p0 = cmethod;
6036 MONO_ADD_INS (bblock, ins);
6038 start_new_bblock = 1;
6042 case CEE_CALLVIRT: {
6043 MonoInst *addr = NULL;
6044 MonoMethodSignature *fsig = NULL;
6045 int temp, array_rank = 0;
6046 int virtual = *ip == CEE_CALLVIRT;
6048 gboolean pass_imt_from_rgctx = FALSE;
6049 MonoInst *imt_arg = NULL;
6050 gboolean pass_vtable = FALSE;
6051 gboolean pass_mrgctx = FALSE;
6052 MonoInst *vtable_arg = NULL;
6053 gboolean check_this = FALSE;
6056 token = read32 (ip + 1);
6058 if (*ip == CEE_CALLI) {
6063 if (method->wrapper_type != MONO_WRAPPER_NONE)
6064 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6066 fsig = mono_metadata_parse_signature (image, token);
6068 n = fsig->param_count + fsig->hasthis;
6070 MonoMethod *cil_method;
6072 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6073 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
6074 cil_method = cmethod;
6075 } else if (constrained_call) {
6076 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
6077 cil_method = cmethod;
6079 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
6080 cil_method = cmethod;
6085 if (!dont_verify && !cfg->skip_visibility) {
6086 MonoMethod *target_method = cil_method;
6087 if (method->is_inflated) {
6088 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
6090 if (!mono_method_can_access_method (method_definition, target_method) &&
6091 !mono_method_can_access_method (method, cil_method))
6092 METHOD_ACCESS_FAILURE;
6095 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
6096 ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
6098 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
6099 /* MS.NET seems to silently convert this to a callvirt */
6102 if (!cmethod->klass->inited){
6103 if (!mono_class_init (cmethod->klass))
6107 if (mono_method_signature (cmethod)->pinvoke) {
6108 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6109 fsig = mono_method_signature (wrapper);
6110 } else if (constrained_call) {
6111 fsig = mono_method_signature (cmethod);
6113 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
6116 mono_save_token_info (cfg, image, token, cmethod);
6118 n = fsig->param_count + fsig->hasthis;
6120 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
6121 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6123 CHECK_CFG_EXCEPTION;
6126 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
6127 mini_class_is_system_array (cmethod->klass)) {
6128 array_rank = cmethod->klass->rank;
6131 if (cmethod->string_ctor)
6132 g_assert_not_reached ();
6136 if (!cfg->generic_sharing_context && cmethod && cmethod->klass->generic_container)
6139 if (!cfg->generic_sharing_context && cmethod)
6140 g_assert (!mono_method_check_context_used (cmethod));
6144 //g_assert (!virtual || fsig->hasthis);
6148 if (constrained_call) {
6150 * We have the `constrained.' prefix opcode.
6152 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
6155 * The type parameter is instantiated as a valuetype,
6156 * but that type doesn't override the method we're
6157 * calling, so we need to box `this'.
6158 * sp [0] is a pointer to the data: we need the value
6159 * in handle_box (), so load it here.
6161 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &constrained_call->byval_arg));
6162 type_to_eval_stack_type (cfg, &constrained_call->byval_arg, load);
6163 load->inst_left = sp [0];
6164 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
6165 } else if (!constrained_call->valuetype) {
6169 * The type parameter is instantiated as a reference
6170 * type. We have a managed pointer on the stack, so
6171 * we need to dereference it here.
6174 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
6175 ins->inst_i0 = sp [0];
6176 ins->type = STACK_OBJ;
6177 ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
6179 } else if (cmethod->klass->valuetype)
6181 constrained_call = NULL;
6184 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
6187 if (cmethod && (cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
6188 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
6189 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6190 MonoGenericContext *context = mini_class_get_context (cmethod->klass);
6191 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6194 * Pass vtable iff target method might
6195 * be shared, which means that sharing
6196 * is enabled for its class and its
6197 * context is sharable (and it's not a
6200 if (sharing_enabled && context_sharable &&
6201 !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
6205 if (cmethod && mini_method_get_context (cmethod) &&
6206 mini_method_get_context (cmethod)->method_inst) {
6207 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6208 MonoGenericContext *context = mini_method_get_context (cmethod);
6209 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6211 g_assert (!pass_vtable);
6213 if (sharing_enabled && context_sharable)
6217 if (cfg->generic_sharing_context && cmethod) {
6218 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
6220 context_used = mono_method_check_context_used (cmethod);
6222 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
6223 /* Generic method interface
6224 calls are resolved via a
6225 helper function and don't
6227 if (!cmethod_context || !cmethod_context->method_inst)
6228 pass_imt_from_rgctx = TRUE;
6232 * If a shared method calls another
6233 * shared method then the caller must
6234 * have a generic sharing context
6235 * because the magic trampoline
6236 * requires it. FIXME: We shouldn't
6237 * have to force the vtable/mrgctx
6238 * variable here. Instead there
6239 * should be a flag in the cfg to
6240 * request a generic sharing context.
6242 if (context_used && method->flags & METHOD_ATTRIBUTE_STATIC)
6243 mono_get_vtable_var (cfg);
6250 GET_RGCTX (rgctx, context_used);
6251 vtable_arg = get_runtime_generic_context_ptr (cfg, method, context_used,
6252 bblock, cmethod->klass, generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
6254 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
6256 CHECK_TYPELOAD (cmethod->klass);
6257 NEW_VTABLECONST (cfg, vtable_arg, vtable);
6262 g_assert (!vtable_arg);
6267 GET_RGCTX (rgctx, context_used);
6268 vtable_arg = get_runtime_generic_context_method_rgctx (cfg, method,
6269 context_used, bblock, cmethod, generic_context, rgctx, ip);
6271 MonoMethodRuntimeGenericContext *mrgctx;
6273 mrgctx = mono_method_lookup_rgctx (mono_class_vtable (cfg->domain, cmethod->klass),
6274 mini_method_get_context (cmethod)->method_inst);
6276 cfg->disable_aot = TRUE;
6277 NEW_PCONST (cfg, vtable_arg, mrgctx);
6280 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
6281 (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) {
6288 if (pass_imt_from_rgctx) {
6291 g_assert (!pass_vtable);
6294 GET_RGCTX (rgctx, context_used);
6295 imt_arg = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6296 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6302 MONO_INST_NEW (cfg, check, OP_CHECK_THIS_PASSTHROUGH);
6303 check->cil_code = ip;
6304 check->inst_left = sp [0];
6305 check->type = sp [0]->type;
6309 if (cmethod && virtual &&
6310 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
6311 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
6312 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
6313 mono_method_signature (cmethod)->generic_param_count) {
6314 MonoInst *this_temp, *this_arg_temp, *store;
6315 MonoInst *iargs [4];
6317 g_assert (mono_method_signature (cmethod)->is_inflated);
6318 /* Prevent inlining of methods that contain indirect calls */
6321 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6322 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
6323 MONO_ADD_INS (bblock, store);
6325 /* FIXME: This should be a managed pointer */
6326 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
6328 /* Because of the PCONST below */
6329 cfg->disable_aot = TRUE;
6330 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
6334 GET_RGCTX (rgctx, context_used);
6335 iargs [1] = get_runtime_generic_context_method (cfg, method, context_used,
6337 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6338 NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6339 temp = mono_emit_jit_icall (cfg, bblock,
6340 mono_helper_compile_generic_method, iargs, ip);
6342 NEW_METHODCONST (cfg, iargs [1], cmethod);
6343 NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6344 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method,
6348 NEW_TEMPLOAD (cfg, addr, temp);
6349 NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
6351 if ((temp = mono_emit_calli_spilled (cfg, bblock, fsig, sp, addr, ip)) != -1) {
6352 NEW_TEMPLOAD (cfg, *sp, temp);
6361 /* FIXME: runtime generic context pointer for jumps? */
6362 /* FIXME: handle this for generic sharing eventually */
6363 if ((ins_flag & MONO_INST_TAILCALL) && !cfg->generic_sharing_context && !vtable_arg && cmethod && (*ip == CEE_CALL) &&
6364 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
6367 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6369 /* FIXME: This assumes the two methods has the same number and type of arguments */
6371 * We implement tail calls by storing the actual arguments into the
6372 * argument variables, then emitting a OP_JMP. Since the actual arguments
6373 * can refer to the arg variables, we have to spill them.
6375 handle_loaded_temps (cfg, bblock, sp, sp + n);
6376 for (i = 0; i < n; ++i) {
6377 /* Prevent argument from being register allocated */
6378 arg_array [i]->flags |= MONO_INST_VOLATILE;
6380 /* Check if argument is the same */
6382 * FIXME: This loses liveness info, so it can only be done if the
6383 * argument is not register allocated.
6385 NEW_ARGLOAD (cfg, ins, i);
6386 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
6389 NEW_ARGSTORE (cfg, ins, i, sp [i]);
6390 /* FIXME: handle CEE_STIND_R4 */
6391 if (ins->opcode == CEE_STOBJ) {
6392 NEW_ARGLOADA (cfg, ins, i);
6393 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
6396 MONO_ADD_INS (bblock, ins);
6398 MONO_INST_NEW (cfg, ins, OP_JMP);
6399 ins->inst_p0 = cmethod;
6400 ins->inst_p1 = arg_array [0];
6401 MONO_ADD_INS (bblock, ins);
6402 link_bblock (cfg, bblock, end_bblock);
6403 start_new_bblock = 1;
6404 /* skip CEE_RET as well */
6409 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
6410 if (MONO_TYPE_IS_VOID (fsig->ret)) {
6411 MONO_ADD_INS (bblock, ins);
6413 type_to_eval_stack_type (cfg, fsig->ret, ins);
6423 handle_loaded_temps (cfg, bblock, stack_start, sp);
6425 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && //!check_this &&
6426 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
6427 mono_method_check_inlining (cfg, cmethod) &&
6428 !g_list_find (dont_inline, cmethod)) {
6430 MonoBasicBlock *ebblock;
6431 gboolean allways = FALSE;
6433 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6434 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6435 /* Prevent inlining of methods that call wrappers */
6437 cmethod = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6441 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
6445 GET_BBLOCK (cfg, bblock, ip);
6446 ebblock->next_bb = bblock;
6447 link_bblock (cfg, ebblock, bblock);
6449 if (!MONO_TYPE_IS_VOID (fsig->ret))
6452 /* indicates start of a new block, and triggers a load of all
6453 stack arguments at bb boundarie */
6456 inline_costs += costs;
6462 inline_costs += 10 * num_calls++;
6464 /* tail recursion elimination */
6465 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET &&
6467 gboolean has_vtargs = FALSE;
6470 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6472 /* keep it simple */
6473 for (i = fsig->param_count - 1; i >= 0; i--) {
6474 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
6479 for (i = 0; i < n; ++i) {
6480 /* FIXME: handle CEE_STIND_R4 */
6481 NEW_ARGSTORE (cfg, ins, i, sp [i]);
6482 MONO_ADD_INS (bblock, ins);
6484 MONO_INST_NEW (cfg, ins, OP_BR);
6485 MONO_ADD_INS (bblock, ins);
6486 tblock = start_bblock->out_bb [0];
6487 link_bblock (cfg, bblock, tblock);
6488 ins->inst_target_bb = tblock;
6489 start_new_bblock = 1;
6491 /* skip the CEE_RET, too */
6492 if (ip_in_bb (cfg, bblock, ip + 5))
6501 if (ip_in_bb (cfg, bblock, ip + 5)
6502 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
6503 && (!MONO_TYPE_IS_VOID (fsig->ret) || (cmethod && cmethod->string_ctor))
6504 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET))
6505 /* No need to spill */
6510 /* FIXME: only do this for generic methods if
6511 they are not shared! */
6513 (cmethod->klass->valuetype ||
6514 (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst && !pass_mrgctx) ||
6515 ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
6516 mono_class_generic_sharing_enabled (cmethod->klass)) ||
6517 (!imt_arg && !mono_method_is_generic_sharable_impl (cmethod, TRUE) &&
6518 (!virtual || cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6519 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))))) {
6524 g_assert (cfg->generic_sharing_context && cmethod);
6528 * We are compiling a call to
6529 * non-shared generic code from shared
6530 * code, which means that we have to
6531 * look up the method in the rgctx and
6532 * do an indirect call.
6534 GET_RGCTX (rgctx, context_used);
6535 addr = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6536 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
6541 g_assert (!imt_arg);
6543 if (*ip == CEE_CALL) {
6544 g_assert (context_used);
6545 } else if (*ip == CEE_CALLI) {
6546 g_assert (!vtable_arg);
6548 g_assert (cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6549 !(cmethod->flags & METHOD_ATTRIBUTE_FINAL));
6552 /* Prevent inlining of methods with indirect calls */
6555 ins = (MonoInst*)mono_emit_rgctx_calli (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6558 temp = mono_emit_rgctx_calli_spilled (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6560 NEW_TEMPLOAD (cfg, *sp, temp);
6561 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
6565 } else if (array_rank) {
6568 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
6569 if (sp [fsig->param_count]->type == STACK_OBJ) {
6570 MonoInst *iargs [2];
6571 MonoInst *array, *to_store, *store;
6573 handle_loaded_temps (cfg, bblock, stack_start, sp);
6575 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6576 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
6577 MONO_ADD_INS (bblock, store);
6578 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
6580 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
6581 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
6582 /* FIXME: handle CEE_STIND_R4 */
6583 MONO_ADD_INS (bblock, store);
6584 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
6587 * We first save the args for the call so that the args are copied to the stack
6588 * and a new instruction tree for them is created. If we don't do this,
6589 * the same MonoInst is added to two different trees and this is not
6592 mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
6594 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
6595 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
6598 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
6599 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
6600 /* FIXME: handle CEE_STIND_R4 */
6601 if (ins->opcode == CEE_STOBJ) {
6602 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
6604 MONO_ADD_INS (bblock, ins);
6607 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
6608 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6609 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
6612 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
6613 if (!cmethod->klass->element_class->valuetype && !readonly) {
6615 //* Needed by the code generated in inssel.brg * /
6616 mono_get_got_var (cfg);
6618 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
6619 check->klass = cmethod->klass;
6620 check->inst_left = sp [0];
6621 check->type = STACK_OBJ;
6626 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6629 g_assert_not_reached ();
6633 /* Prevent inlining of methods which call other methods */
6635 if (mini_redirect_call (&temp, cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) {
6637 NEW_TEMPLOAD (cfg, *sp, temp);
6640 } else if (no_spill) {
6641 ins = (MonoInst*)mono_emit_rgctx_method_call (cfg, bblock, cmethod, fsig, sp,
6642 vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL);
6645 if ((temp = mono_emit_rgctx_method_call_spilled (cfg, bblock, cmethod, fsig, sp,
6646 vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL)) != -1) {
6648 NEW_TEMPLOAD (cfg, load, temp);
6650 #ifdef MONO_ARCH_SOFT_FLOAT
6651 if (load->opcode == CEE_LDIND_R4) {
6652 NEW_TEMPLOADA (cfg, load, temp);
6653 temp = handle_load_float (cfg, bblock, load, ip);
6654 NEW_TEMPLOAD (cfg, load, temp);
6667 if (cfg->method != method) {
6668 /* return from inlined method */
6673 //g_assert (returnvar != -1);
6674 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
6675 store->cil_code = sp [0]->cil_code;
6676 if (store->opcode == CEE_STOBJ) {
6677 g_assert_not_reached ();
6678 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6679 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6680 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
6681 #ifdef MONO_ARCH_SOFT_FLOAT
6682 } else if (store->opcode == CEE_STIND_R4) {
6683 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6684 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
6687 MONO_ADD_INS (bblock, store);
6691 g_assert (!return_var);
6694 MONO_INST_NEW (cfg, ins, OP_NOP);
6695 ins->opcode = mini_type_to_stind (cfg, mono_method_signature (method)->ret);
6696 if (ins->opcode == CEE_STOBJ) {
6697 NEW_RETLOADA (cfg, ins);
6698 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6699 handle_stobj (cfg, bblock, ins, *sp, ip, cfg->ret->klass, FALSE, FALSE, FALSE);
6701 ins->opcode = OP_SETRET;
6702 ins->inst_i0 = *sp;;
6703 ins->inst_i1 = NULL;
6704 MONO_ADD_INS (bblock, ins);
6708 if (sp != stack_start)
6710 MONO_INST_NEW (cfg, ins, OP_BR);
6712 ins->inst_target_bb = end_bblock;
6713 MONO_ADD_INS (bblock, ins);
6714 link_bblock (cfg, bblock, end_bblock);
6715 start_new_bblock = 1;
6719 MONO_INST_NEW (cfg, ins, OP_BR);
6721 MONO_ADD_INS (bblock, ins);
6722 target = ip + 1 + (signed char)(*ip);
6724 GET_BBLOCK (cfg, tblock, target);
6725 link_bblock (cfg, bblock, tblock);
6726 CHECK_BBLOCK (target, ip, tblock);
6727 ins->inst_target_bb = tblock;
6728 if (sp != stack_start) {
6729 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6731 CHECK_UNVERIFIABLE (cfg);
6733 start_new_bblock = 1;
6734 inline_costs += BRANCH_COST;
6740 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6742 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6744 target = ip + 1 + *(signed char*)ip;
6746 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
6747 if (sp != stack_start) {
6748 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6750 CHECK_UNVERIFIABLE (cfg);
6752 inline_costs += BRANCH_COST;
6766 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6768 target = ip + 1 + *(signed char*)ip;
6770 #ifdef MONO_ARCH_SOFT_FLOAT
6771 if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6772 ins->opcode = condbr_to_fp_br (ins->opcode);
6774 ins->inst_left = sp [0];
6775 ins->inst_right = sp [1];
6776 ins->type = STACK_I4;
6777 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6778 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6786 if (sp != stack_start) {
6787 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6789 CHECK_UNVERIFIABLE (cfg);
6791 inline_costs += BRANCH_COST;
6795 MONO_INST_NEW (cfg, ins, OP_BR);
6797 MONO_ADD_INS (bblock, ins);
6798 target = ip + 4 + (gint32)read32(ip);
6800 GET_BBLOCK (cfg, tblock, target);
6801 link_bblock (cfg, bblock, tblock);
6802 CHECK_BBLOCK (target, ip, tblock);
6803 ins->inst_target_bb = tblock;
6804 if (sp != stack_start) {
6805 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6807 CHECK_UNVERIFIABLE (cfg);
6809 start_new_bblock = 1;
6810 inline_costs += BRANCH_COST;
6816 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6818 MONO_INST_NEW (cfg, ins, *ip);
6820 target = ip + 4 + (gint32)read32(ip);
6822 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
6823 if (sp != stack_start) {
6824 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6826 CHECK_UNVERIFIABLE (cfg);
6828 inline_costs += BRANCH_COST;
6842 MONO_INST_NEW (cfg, ins, *ip);
6844 target = ip + 4 + (gint32)read32(ip);
6846 #ifdef MONO_ARCH_SOFT_FLOAT
6847 if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6848 ins->opcode = condbr_to_fp_br (ins->opcode);
6850 ins->inst_left = sp [0];
6851 ins->inst_right = sp [1];
6852 ins->type = STACK_I4;
6853 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6854 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6862 if (sp != stack_start) {
6863 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6865 CHECK_UNVERIFIABLE (cfg);
6867 inline_costs += BRANCH_COST;
6872 n = read32 (ip + 1);
6873 MONO_INST_NEW (cfg, ins, OP_SWITCH);
6875 ins->inst_left = *sp;
6876 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
6879 CHECK_OPSIZE (n * sizeof (guint32));
6880 target = ip + n * sizeof (guint32);
6881 MONO_ADD_INS (bblock, ins);
6882 GET_BBLOCK (cfg, tblock, target);
6883 link_bblock (cfg, bblock, tblock);
6884 ins->klass = GUINT_TO_POINTER (n);
6885 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
6886 ins->inst_many_bb [n] = tblock;
6888 for (i = 0; i < n; ++i) {
6889 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
6890 link_bblock (cfg, bblock, tblock);
6891 ins->inst_many_bb [i] = tblock;
6894 if (sp != stack_start) {
6895 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6897 CHECK_UNVERIFIABLE (cfg);
6899 /* Needed by the code generated in inssel.brg */
6900 mono_get_got_var (cfg);
6901 inline_costs += (BRANCH_COST * 2);
6915 MONO_INST_NEW (cfg, ins, *ip);
6919 ins->type = ldind_type [*ip - CEE_LDIND_I1];
6920 ins->flags |= ins_flag;
6922 if (ins->type == STACK_OBJ)
6923 ins->klass = mono_defaults.object_class;
6924 #ifdef MONO_ARCH_SOFT_FLOAT
6925 if (*ip == CEE_LDIND_R4) {
6928 temp = handle_load_float (cfg, bblock, ins->inst_i0, ip);
6929 NEW_TEMPLOAD (cfg, *sp, temp);
6943 #ifdef MONO_ARCH_SOFT_FLOAT
6944 if (*ip == CEE_STIND_R4) {
6946 handle_store_float (cfg, bblock, sp [0], sp [1], ip);
6951 #if HAVE_WRITE_BARRIERS
6952 if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [-1]->opcode == OP_PCONST) && (sp [-1]->inst_p0 == 0))) {
6953 /* insert call to write barrier */
6954 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
6956 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
6961 MONO_INST_NEW (cfg, ins, *ip);
6964 handle_loaded_temps (cfg, bblock, stack_start, sp);
6965 MONO_ADD_INS (bblock, ins);
6966 ins->inst_i0 = sp [0];
6967 ins->inst_i1 = sp [1];
6968 ins->flags |= ins_flag;
6976 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
6977 /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
6978 if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
6979 switch (ins->opcode) {
6981 ins->opcode = OP_IMUL_IMM;
6982 ins->inst_imm = ins->inst_right->inst_c0;
6985 ins->opcode = OP_LMUL_IMM;
6986 ins->inst_imm = ins->inst_right->inst_c0;
6989 g_assert_not_reached ();
6994 if (mono_find_jit_opcode_emulation (ins->opcode)) {
6996 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7014 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
7015 * later apply the speedup to the left shift as well
7018 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
7019 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
7020 ins->opcode = OP_LSHR_UN_32;
7021 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
7025 if (mono_find_jit_opcode_emulation (ins->opcode)) {
7027 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7041 case CEE_CONV_OVF_I8:
7042 case CEE_CONV_OVF_U8:
7047 #ifdef MONO_ARCH_SOFT_FLOAT
7049 * Its rather hard to emit the soft float code during the decompose
7050 * pass, so avoid it in some specific cases.
7052 if (ins->opcode == OP_LCONV_TO_R4) {
7055 ins->opcode = OP_LCONV_TO_R8;
7056 ins->type = STACK_R8;
7059 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7061 MONO_INST_NEW (cfg, conv, CEE_CONV_R4);
7062 conv->inst_left = sp [-1];
7063 conv->type = STACK_R8;
7071 if (mono_find_jit_opcode_emulation (ins->opcode)) {
7073 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7077 case CEE_CONV_OVF_I4:
7078 case CEE_CONV_OVF_I1:
7079 case CEE_CONV_OVF_I2:
7080 case CEE_CONV_OVF_I:
7081 case CEE_CONV_OVF_U:
7084 if (sp [-1]->type == STACK_R8) {
7085 ADD_UNOP (CEE_CONV_OVF_I8);
7093 case CEE_CONV_OVF_U1:
7094 case CEE_CONV_OVF_U2:
7095 case CEE_CONV_OVF_U4:
7098 if (sp [-1]->type == STACK_R8) {
7099 ADD_UNOP (CEE_CONV_OVF_U8);
7107 case CEE_CONV_OVF_I1_UN:
7108 case CEE_CONV_OVF_I2_UN:
7109 case CEE_CONV_OVF_I4_UN:
7110 case CEE_CONV_OVF_I8_UN:
7111 case CEE_CONV_OVF_U1_UN:
7112 case CEE_CONV_OVF_U2_UN:
7113 case CEE_CONV_OVF_U4_UN:
7114 case CEE_CONV_OVF_U8_UN:
7115 case CEE_CONV_OVF_I_UN:
7116 case CEE_CONV_OVF_U_UN:
7124 token = read32 (ip + 1);
7125 klass = mini_get_class (method, token, generic_context);
7126 CHECK_TYPELOAD (klass);
7128 if (generic_class_is_reference_type (cfg, klass)) {
7129 MonoInst *store, *load;
7130 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
7131 load->inst_i0 = sp [1];
7132 load->type = STACK_OBJ;
7133 load->klass = klass;
7134 load->flags |= ins_flag;
7135 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
7136 handle_loaded_temps (cfg, bblock, stack_start, sp);
7137 MONO_ADD_INS (bblock, store);
7138 store->inst_i0 = sp [0];
7139 store->inst_i1 = load;
7140 store->flags |= ins_flag;
7144 n = mono_class_value_size (klass, &align);
7145 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7147 NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, align);
7148 MONO_ADD_INS (bblock, copy);
7150 MonoMethod *memcpy_method = get_memcpy_method ();
7151 MonoInst *iargs [3];
7154 NEW_ICONST (cfg, iargs [2], n);
7156 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7163 MonoInst *iargs [3];
7171 token = read32 (ip + 1);
7172 klass = mini_get_class (method, token, generic_context);
7173 CHECK_TYPELOAD (klass);
7174 if (generic_class_is_reference_type (cfg, klass)) {
7175 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
7176 ins->inst_i0 = sp [0];
7177 ins->type = STACK_OBJ;
7179 ins->flags |= ins_flag;
7186 /* Optimize the common ldobj+stloc combination */
7196 loc_index = ip [5] - CEE_STLOC_0;
7203 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
7204 CHECK_LOCAL (loc_index);
7205 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
7207 /* FIXME: handle CEE_STIND_R4 */
7208 if (ins->opcode == CEE_STOBJ) {
7209 handle_loaded_temps (cfg, bblock, stack_start, sp);
7210 g_assert (ins->opcode == CEE_STOBJ);
7211 NEW_LOCLOADA (cfg, ins, loc_index);
7212 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7219 n = mono_class_value_size (klass, &align);
7220 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7221 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7222 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7224 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7225 MONO_ADD_INS (bblock, copy);
7227 MonoMethod *memcpy_method = get_memcpy_method ();
7229 NEW_ICONST (cfg, iargs [2], n);
7231 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7233 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7241 CHECK_STACK_OVF (1);
7243 n = read32 (ip + 1);
7245 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
7246 /* FIXME: moving GC */
7247 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
7248 ins->type = STACK_OBJ;
7249 ins->klass = mono_defaults.string_class;
7252 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
7254 MonoInst *iargs [1];
7256 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
7257 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
7258 NEW_TEMPLOAD (cfg, *sp, temp);
7262 if (cfg->opt & MONO_OPT_SHARED) {
7264 MonoInst *iargs [3];
7265 MonoInst* domain_var;
7267 if (cfg->compile_aot) {
7268 /* FIXME: bug when inlining methods from different assemblies (n is a token valid just in one). */
7269 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
7271 /* avoid depending on undefined C behavior in sequence points */
7272 domain_var = mono_get_domainvar (cfg);
7273 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7274 NEW_IMAGECONST (cfg, iargs [1], image);
7275 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
7276 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
7277 NEW_TEMPLOAD (cfg, *sp, temp);
7278 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7280 if (bblock->out_of_line) {
7281 MonoInst *iargs [2];
7284 if (cfg->method->klass->image == mono_defaults.corlib) {
7286 * Avoid relocations and save some code size by using a
7287 * version of helper_ldstr specialized to mscorlib.
7289 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
7290 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
7292 /* Avoid creating the string object */
7293 NEW_IMAGECONST (cfg, iargs [0], image);
7294 NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
7295 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
7297 NEW_TEMPLOAD (cfg, *sp, temp);
7300 if (cfg->compile_aot) {
7301 NEW_LDSTRCONST (cfg, ins, image, n);
7305 NEW_PCONST (cfg, ins, NULL);
7306 ins->type = STACK_OBJ;
7307 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7308 ins->klass = mono_defaults.string_class;
7318 MonoInst *iargs [2];
7319 MonoMethodSignature *fsig;
7324 token = read32 (ip + 1);
7325 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
7328 fsig = mono_method_get_signature (cmethod, image, token);
7330 mono_save_token_info (cfg, image, token, cmethod);
7332 if (!mono_class_init (cmethod->klass))
7335 if (cfg->generic_sharing_context)
7336 context_used = mono_method_check_context_used (cmethod);
7338 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
7339 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
7341 CHECK_CFG_EXCEPTION;
7342 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
7343 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
7346 n = fsig->param_count;
7350 * Generate smaller code for the common newobj <exception> instruction in
7351 * argument checking code.
7353 if (bblock->out_of_line && cmethod->klass->image == mono_defaults.corlib && n <= 2 &&
7354 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
7355 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
7356 MonoInst *iargs [3];
7361 NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
7364 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_0, iargs, ip);
7368 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_1, iargs, ip);
7373 temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_2, iargs, ip);
7376 g_assert_not_reached ();
7378 NEW_TEMPLOAD (cfg, ins, temp);
7386 /* move the args to allow room for 'this' in the first position */
7392 /* check_call_signature () requires sp[0] to be set */
7393 this_ins.type = STACK_OBJ;
7395 if (check_call_signature (cfg, fsig, sp))
7398 handle_loaded_temps (cfg, bblock, stack_start, sp);
7400 if (mini_class_is_system_array (cmethod->klass)) {
7401 g_assert (!context_used);
7403 NEW_METHODCONST (cfg, *sp, cmethod);
7405 if (fsig->param_count == 2)
7406 /* Avoid varargs in the common case */
7407 temp = mono_emit_jit_icall (cfg, bblock, mono_array_new_2, sp, ip);
7409 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
7410 } else if (cmethod->string_ctor) {
7411 g_assert (!context_used);
7413 /* we simply pass a null pointer */
7414 NEW_PCONST (cfg, *sp, NULL);
7415 /* now call the string ctor */
7416 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
7418 MonoInst* callvirt_this_arg = NULL;
7420 if (cmethod->klass->valuetype) {
7421 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
7422 temp = iargs [0]->inst_c0;
7424 NEW_TEMPLOADA (cfg, *sp, temp);
7426 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
7428 NEW_TEMPLOADA (cfg, *sp, temp);
7431 * The code generated by mini_emit_virtual_call () expects
7432 * iargs [0] to be a boxed instance, but luckily the vcall
7433 * will be transformed into a normal call there.
7435 } else if (context_used) {
7436 MonoInst *rgctx, *data;
7439 GET_RGCTX (rgctx, context_used);
7440 if (cfg->opt & MONO_OPT_SHARED)
7441 rgctx_info = MONO_RGCTX_INFO_KLASS;
7443 rgctx_info = MONO_RGCTX_INFO_VTABLE;
7444 data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
7445 cmethod->klass, generic_context, rgctx, rgctx_info, ip);
7447 temp = handle_alloc_from_inst (cfg, bblock, cmethod->klass, data, FALSE, ip);
7448 NEW_TEMPLOAD (cfg, *sp, temp);
7450 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7452 CHECK_TYPELOAD (cmethod->klass);
7453 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
7454 guint8 *tramp = mono_create_class_init_trampoline (vtable);
7455 mono_emit_native_call (cfg, bblock, tramp,
7456 helper_sig_class_init_trampoline,
7457 NULL, ip, FALSE, FALSE);
7458 if (cfg->verbose_level > 2)
7459 g_print ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
7460 class_inits = g_slist_prepend (class_inits, vtable);
7462 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
7463 NEW_TEMPLOAD (cfg, *sp, temp);
7466 /* Avoid virtual calls to ctors if possible */
7467 if (cmethod->klass->marshalbyref)
7468 callvirt_this_arg = sp [0];
7470 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used &&
7471 mono_method_check_inlining (cfg, cmethod) &&
7472 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
7473 !g_list_find (dont_inline, cmethod)) {
7475 MonoBasicBlock *ebblock;
7476 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
7481 GET_BBLOCK (cfg, bblock, ip);
7482 ebblock->next_bb = bblock;
7483 link_bblock (cfg, ebblock, bblock);
7485 NEW_TEMPLOAD (cfg, *sp, temp);
7488 /* indicates start of a new block, and triggers a load
7489 of all stack arguments at bb boundarie */
7492 inline_costs += costs;
7496 /* Prevent inlining of methods which call other methods */
7498 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
7500 } else if (context_used &&
7501 (cmethod->klass->valuetype ||
7502 !mono_method_is_generic_sharable_impl (cmethod, TRUE))) {
7503 MonoInst *rgctx, *cmethod_addr;
7505 g_assert (!callvirt_this_arg);
7507 GET_RGCTX (rgctx, context_used);
7508 cmethod_addr = get_runtime_generic_context_method (cfg, method, context_used,
7510 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
7512 mono_emit_calli_spilled (cfg, bblock, fsig, sp, cmethod_addr, ip);
7514 /* Prevent inlining of methods which call other methods */
7516 /* now call the actual ctor */
7517 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
7521 NEW_TEMPLOAD (cfg, *sp, temp);
7532 token = read32 (ip + 1);
7533 klass = mini_get_class (method, token, generic_context);
7534 CHECK_TYPELOAD (klass);
7535 if (sp [0]->type != STACK_OBJ)
7538 if (cfg->generic_sharing_context)
7539 context_used = mono_class_check_context_used (klass);
7541 /* Needed by the code generated in inssel.brg */
7543 mono_get_got_var (cfg);
7546 MonoInst *rgctx, *args [2];
7553 GET_RGCTX (rgctx, context_used);
7554 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
7555 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
7557 temp = mono_emit_jit_icall (cfg, bblock, mono_object_isinst, args, ip);
7558 NEW_TEMPLOAD (cfg, *sp, temp);
7563 } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7565 MonoMethod *mono_isinst;
7566 MonoInst *iargs [1];
7567 MonoBasicBlock *ebblock;
7571 mono_isinst = mono_marshal_get_isinst (klass);
7574 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
7575 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7577 g_assert (costs > 0);
7582 GET_BBLOCK (cfg, bblock, ip);
7583 ebblock->next_bb = bblock;
7584 link_bblock (cfg, ebblock, bblock);
7586 temp = iargs [0]->inst_i0->inst_c0;
7587 NEW_TEMPLOAD (cfg, *sp, temp);
7591 inline_costs += costs;
7593 MONO_INST_NEW (cfg, ins, *ip);
7594 ins->type = STACK_OBJ;
7595 ins->inst_left = *sp;
7596 ins->inst_newa_class = klass;
7598 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
7602 case CEE_UNBOX_ANY: {
7603 MonoInst *iargs [3];
7609 token = read32 (ip + 1);
7610 klass = mini_get_class (method, token, generic_context);
7611 CHECK_TYPELOAD (klass);
7613 if (cfg->generic_sharing_context)
7614 context_used = mono_class_check_context_used (klass);
7616 if (generic_class_is_reference_type (cfg, klass)) {
7617 switch (emit_castclass (klass, token, context_used, FALSE,
7618 cfg, method, arg_array, param_types, dont_inline, end, header,
7619 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7621 case -1: goto unverified;
7622 case -2: goto exception_exit;
7623 default: g_assert_not_reached ();
7628 if (mono_class_is_nullable (klass)) {
7630 MonoInst *rgctx = NULL;
7633 GET_RGCTX (rgctx, context_used);
7635 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7636 generic_context, rgctx);
7637 NEW_TEMPLOAD (cfg, *sp, v);
7643 switch (emit_unbox (klass, token, context_used,
7644 cfg, method, arg_array, param_types, dont_inline, end, header,
7645 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7647 case -1: goto unverified;
7648 case -2: goto exception_exit;
7649 default: g_assert_not_reached ();
7653 n = mono_class_value_size (klass, &align);
7654 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7655 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7656 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7658 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7659 MONO_ADD_INS (bblock, copy);
7661 MonoMethod *memcpy_method = get_memcpy_method ();
7663 NEW_ICONST (cfg, iargs [2], n);
7665 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7667 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7676 token = read32 (ip + 1);
7677 klass = mini_get_class (method, token, generic_context);
7678 CHECK_TYPELOAD (klass);
7680 if (cfg->generic_sharing_context)
7681 context_used = mono_class_check_context_used (klass);
7683 if (mono_class_is_nullable (klass)) {
7685 MonoInst *rgctx = NULL;
7688 GET_RGCTX (rgctx, context_used);
7689 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7690 generic_context, rgctx);
7691 NEW_TEMPLOAD (cfg, *sp, v);
7697 switch (emit_unbox (klass, token, context_used,
7698 cfg, method, arg_array, param_types, dont_inline, end, header,
7699 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7701 case -1: goto unverified;
7702 case -2: goto exception_exit;
7703 default: g_assert_not_reached ();
7714 token = read32 (ip + 1);
7715 klass = mini_get_class (method, token, generic_context);
7716 CHECK_TYPELOAD (klass);
7717 if (sp [0]->type != STACK_OBJ)
7720 if (cfg->generic_sharing_context)
7721 context_used = mono_class_check_context_used (klass);
7723 switch (emit_castclass (klass, token, context_used, TRUE,
7724 cfg, method, arg_array, param_types, dont_inline, end, header,
7725 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7727 case -1: goto unverified;
7728 case -2: goto exception_exit;
7729 default: g_assert_not_reached ();
7734 MONO_INST_NEW (cfg, ins, OP_THROW);
7736 ins->inst_left = *sp;
7738 bblock->out_of_line = TRUE;
7739 MONO_ADD_INS (bblock, ins);
7740 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
7741 ins->cil_code = ip - 1;
7742 MONO_ADD_INS (bblock, ins);
7745 link_bblock (cfg, bblock, end_bblock);
7746 start_new_bblock = 1;
7751 MonoInst *offset_ins;
7752 MonoClassField *field;
7753 MonoBasicBlock *ebblock;
7757 if (*ip == CEE_STFLD) {
7764 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
7766 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
7769 token = read32 (ip + 1);
7770 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7771 field = mono_method_get_wrapper_data (method, token);
7772 klass = field->parent;
7774 field = mono_field_from_token (image, token, &klass, generic_context);
7778 mono_class_init (klass);
7779 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7780 FIELD_ACCESS_FAILURE;
7782 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
7783 /* FIXME: mark instructions for use in SSA */
7784 if (*ip == CEE_STFLD) {
7785 if (target_type_is_incompatible (cfg, field->type, sp [1]))
7787 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7788 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
7789 MonoInst *iargs [5];
7792 NEW_CLASSCONST (cfg, iargs [1], klass);
7793 NEW_FIELDCONST (cfg, iargs [2], field);
7794 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
7798 if (cfg->opt & MONO_OPT_INLINE) {
7799 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
7800 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7801 g_assert (costs > 0);
7806 GET_BBLOCK (cfg, bblock, ip);
7807 ebblock->next_bb = bblock;
7808 link_bblock (cfg, ebblock, bblock);
7810 /* indicates start of a new block, and triggers a load
7811 of all stack arguments at bb boundarie */
7814 inline_costs += costs;
7817 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
7819 #if HAVE_WRITE_BARRIERS
7820 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
7821 /* insert call to write barrier */
7822 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
7823 MonoInst *iargs [2];
7824 NEW_ICONST (cfg, offset_ins, foffset);
7825 MONO_INST_NEW (cfg, ins, OP_PADD);
7826 ins->inst_left = *sp;
7827 ins->inst_right = offset_ins;
7828 ins->type = STACK_MP;
7829 ins->klass = mono_defaults.object_class;
7832 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
7834 #ifdef MONO_ARCH_SOFT_FLOAT
7835 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_R4) {
7836 NEW_ICONST (cfg, offset_ins, foffset);
7837 MONO_INST_NEW (cfg, ins, OP_PADD);
7838 ins->inst_left = *sp;
7839 ins->inst_right = offset_ins;
7840 ins->type = STACK_MP;
7841 ins->klass = mono_defaults.object_class;
7842 handle_store_float (cfg, bblock, ins, sp [1], ip);
7846 NEW_ICONST (cfg, offset_ins, foffset);
7847 MONO_INST_NEW (cfg, ins, OP_PADD);
7848 ins->inst_left = *sp;
7849 ins->inst_right = offset_ins;
7850 ins->type = STACK_MP;
7852 MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
7853 store->inst_left = ins;
7854 store->inst_right = sp [1];
7855 handle_loaded_temps (cfg, bblock, stack_start, sp);
7856 store->flags |= ins_flag;
7858 if (store->opcode == CEE_STOBJ) {
7859 handle_stobj (cfg, bblock, ins, sp [1], ip,
7860 mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
7862 MONO_ADD_INS (bblock, store);
7865 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7866 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
7867 MonoInst *iargs [4];
7871 NEW_CLASSCONST (cfg, iargs [1], klass);
7872 NEW_FIELDCONST (cfg, iargs [2], field);
7873 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
7874 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
7875 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock,
7876 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7877 g_assert (costs > 0);
7882 GET_BBLOCK (cfg, bblock, ip);
7883 ebblock->next_bb = bblock;
7884 link_bblock (cfg, ebblock, bblock);
7886 temp = iargs [0]->inst_i0->inst_c0;
7888 NEW_TEMPLOAD (cfg, *sp, temp);
7891 /* indicates start of a new block, and triggers a load of
7892 all stack arguments at bb boundarie */
7895 inline_costs += costs;
7898 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
7899 NEW_TEMPLOAD (cfg, *sp, temp);
7900 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
7904 NEW_ICONST (cfg, offset_ins, foffset);
7905 MONO_INST_NEW (cfg, ins, OP_PADD);
7906 ins->inst_left = *sp;
7907 ins->inst_right = offset_ins;
7908 ins->type = STACK_MP;
7910 if (*ip == CEE_LDFLDA) {
7911 ins->klass = mono_class_from_mono_type (field->type);
7915 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
7916 type_to_eval_stack_type (cfg, field->type, load);
7917 load->inst_left = ins;
7918 load->flags |= ins_flag;
7920 #ifdef MONO_ARCH_SOFT_FLOAT
7921 if (mini_type_to_ldind (cfg, field->type) == CEE_LDIND_R4) {
7923 temp = handle_load_float (cfg, bblock, ins, ip);
7924 NEW_TEMPLOAD (cfg, *sp, temp);
7938 MonoClassField *field;
7939 gboolean is_special_static;
7940 gpointer addr = NULL;
7943 token = read32 (ip + 1);
7944 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7945 field = mono_method_get_wrapper_data (method, token);
7946 klass = field->parent;
7949 field = mono_field_from_token (image, token, &klass, generic_context);
7952 mono_class_init (klass);
7953 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7954 FIELD_ACCESS_FAILURE;
7957 * We can only support shared generic static
7958 * field access on architectures where the
7959 * trampoline code has been extended to handle
7960 * the generic class init.
7962 #ifndef MONO_ARCH_VTABLE_REG
7963 GENERIC_SHARING_FAILURE (*ip);
7966 if (cfg->generic_sharing_context)
7967 context_used = mono_class_check_context_used (klass);
7969 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
7971 if ((*ip) == CEE_STSFLD)
7972 handle_loaded_temps (cfg, bblock, stack_start, sp);
7974 is_special_static = mono_class_field_is_special_static (field);
7976 if ((cfg->opt & MONO_OPT_SHARED) ||
7977 (cfg->compile_aot && is_special_static) ||
7978 (context_used && is_special_static)) {
7980 MonoInst *iargs [2];
7982 g_assert (field->parent);
7983 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {
7984 MonoInst *domain_var;
7985 /* avoid depending on undefined C behavior in sequence points */
7986 domain_var = mono_get_domainvar (cfg);
7987 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7989 NEW_DOMAINCONST (cfg, iargs [0]);
7994 GET_RGCTX (rgctx, context_used);
7995 iargs [1] = get_runtime_generic_context_field (cfg, method, context_used,
7997 generic_context, rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
7999 NEW_FIELDCONST (cfg, iargs [1], field);
8001 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
8002 NEW_TEMPLOAD (cfg, ins, temp);
8003 } else if (context_used) {
8004 MonoInst *rgctx, *static_data;
8007 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
8008 method->klass->name_space, method->klass->name, method->name,
8009 depth, field->offset);
8012 if (mono_class_needs_cctor_run (klass, method)) {
8013 MonoMethodSignature *sig = helper_sig_generic_class_init_trampoline;
8015 MonoInst *vtable, *rgctx;
8017 GET_RGCTX (rgctx, context_used);
8018 vtable = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8019 generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
8021 call = mono_emit_call_args (cfg, bblock, sig, NULL, FALSE, FALSE, ip, FALSE);
8022 call->inst.opcode = OP_TRAMPCALL_VTABLE;
8023 call->fptr = mono_create_generic_class_init_trampoline ();
8025 call->inst.inst_left = vtable;
8027 mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
8031 * The pointer we're computing here is
8033 * super_info.static_data + field->offset
8035 GET_RGCTX (rgctx, context_used);
8036 static_data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8037 generic_context, rgctx, MONO_RGCTX_INFO_STATIC_DATA, ip);
8039 if (field->offset == 0) {
8042 MonoInst *field_offset;
8044 NEW_ICONST (cfg, field_offset, field->offset);
8046 MONO_INST_NEW (cfg, ins, OP_PADD);
8047 ins->inst_left = static_data;
8048 ins->inst_right = field_offset;
8049 ins->type = STACK_PTR;
8055 vtable = mono_class_vtable (cfg->domain, klass);
8056 CHECK_TYPELOAD (klass);
8057 if (!is_special_static) {
8058 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
8059 guint8 *tramp = mono_create_class_init_trampoline (vtable);
8060 mono_emit_native_call (cfg, bblock, tramp,
8061 helper_sig_class_init_trampoline,
8062 NULL, ip, FALSE, FALSE);
8063 if (cfg->verbose_level > 2)
8064 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
8065 class_inits = g_slist_prepend (class_inits, vtable);
8067 if (cfg->run_cctors) {
8069 /* This makes so that inline cannot trigger */
8070 /* .cctors: too many apps depend on them */
8071 /* running with a specific order... */
8072 if (! vtable->initialized)
8074 ex = mono_runtime_class_init_full (vtable, FALSE);
8076 set_exception_object (cfg, ex);
8077 goto exception_exit;
8081 addr = (char*)vtable->data + field->offset;
8083 if (cfg->compile_aot)
8084 NEW_SFLDACONST (cfg, ins, field);
8086 NEW_PCONST (cfg, ins, addr);
8089 MonoInst *iargs [1];
8091 /* The special_static_fields
8092 * field is init'd in
8093 * mono_class_vtable, so it
8094 * needs to be called here.
8096 if (!(cfg->opt & MONO_OPT_SHARED)) {
8097 mono_class_vtable (cfg->domain, klass);
8098 CHECK_TYPELOAD (klass);
8100 mono_domain_lock (cfg->domain);
8101 if (cfg->domain->special_static_fields)
8102 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
8103 mono_domain_unlock (cfg->domain);
8106 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
8107 * This could be later optimized to do just a couple of
8108 * memory dereferences with constant offsets.
8110 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
8111 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
8112 NEW_TEMPLOAD (cfg, ins, temp);
8116 /* FIXME: mark instructions for use in SSA */
8117 if (*ip == CEE_LDSFLDA) {
8118 ins->klass = mono_class_from_mono_type (field->type);
8120 } else if (*ip == CEE_STSFLD) {
8124 MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
8125 store->inst_left = ins;
8126 store->inst_right = sp [0];
8127 store->flags |= ins_flag;
8130 #ifdef MONO_ARCH_SOFT_FLOAT
8131 if (store->opcode == CEE_STIND_R4)
8132 handle_store_float (cfg, bblock, ins, sp [0], ip);
8135 if (store->opcode == CEE_STOBJ) {
8136 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
8138 MONO_ADD_INS (bblock, store);
8140 gboolean is_const = FALSE;
8141 MonoVTable *vtable = NULL;
8144 vtable = mono_class_vtable (cfg->domain, klass);
8146 CHECK_TYPELOAD (klass);
8147 if (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
8148 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
8149 gpointer addr = (char*)vtable->data + field->offset;
8150 int ro_type = field->type->type;
8151 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
8152 ro_type = field->type->data.klass->enum_basetype->type;
8154 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
8157 case MONO_TYPE_BOOLEAN:
8159 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
8163 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
8166 case MONO_TYPE_CHAR:
8168 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
8172 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
8177 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
8181 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
8184 #ifndef HAVE_MOVING_COLLECTOR
8187 case MONO_TYPE_STRING:
8188 case MONO_TYPE_OBJECT:
8189 case MONO_TYPE_CLASS:
8190 case MONO_TYPE_SZARRAY:
8192 case MONO_TYPE_FNPTR:
8193 case MONO_TYPE_ARRAY:
8194 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
8195 type_to_eval_stack_type (cfg, field->type, *sp);
8201 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
8202 sp [0]->type = STACK_I8;
8203 sp [0]->inst_l = *((gint64 *)addr);
8208 case MONO_TYPE_VALUETYPE:
8217 CHECK_STACK_OVF (1);
8218 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
8219 type_to_eval_stack_type (cfg, field->type, load);
8220 load->inst_left = ins;
8221 load->flags |= ins_flag;
8222 #ifdef MONO_ARCH_SOFT_FLOAT
8223 if (load->opcode == CEE_LDIND_R4) {
8225 temp = handle_load_float (cfg, bblock, ins, ip);
8226 NEW_TEMPLOAD (cfg, load, temp);
8240 token = read32 (ip + 1);
8241 klass = mini_get_class (method, token, generic_context);
8242 CHECK_TYPELOAD (klass);
8243 n = mini_type_to_stind (cfg, &klass->byval_arg);
8244 /* FIXME: handle CEE_STIND_R4 */
8245 if (n == CEE_STOBJ) {
8246 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
8248 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
8250 MONO_INST_NEW (cfg, store, n);
8251 store->inst_left = sp [0];
8252 store->inst_right = sp [1];
8253 store->flags |= ins_flag;
8254 MONO_ADD_INS (bblock, store);
8267 token = read32 (ip + 1);
8268 klass = mini_get_class (method, token, generic_context);
8269 CHECK_TYPELOAD (klass);
8271 if (cfg->generic_sharing_context)
8272 context_used = mono_class_check_context_used (klass);
8274 if (generic_class_is_reference_type (cfg, klass)) {
8279 if (klass == mono_defaults.void_class)
8281 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
8283 /* frequent check in generic code: box (struct), brtrue */
8284 if (!mono_class_is_nullable (klass) &&
8285 ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
8286 /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
8287 MONO_INST_NEW (cfg, ins, CEE_POP);
8288 MONO_ADD_INS (bblock, ins);
8292 MONO_INST_NEW (cfg, ins, OP_BR);
8293 MONO_ADD_INS (bblock, ins);
8294 if (*ip == CEE_BRTRUE_S) {
8297 target = ip + 1 + (signed char)(*ip);
8302 target = ip + 4 + (gint)(read32 (ip));
8305 GET_BBLOCK (cfg, tblock, target);
8306 link_bblock (cfg, bblock, tblock);
8307 CHECK_BBLOCK (target, ip, tblock);
8308 ins->inst_target_bb = tblock;
8309 GET_BBLOCK (cfg, tblock, ip);
8310 link_bblock (cfg, bblock, tblock);
8311 if (sp != stack_start) {
8312 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
8314 CHECK_UNVERIFIABLE (cfg);
8316 start_new_bblock = 1;
8322 if (mono_class_is_nullable (klass)) {
8323 GET_RGCTX (rgctx, context_used);
8324 *sp++ = handle_box_nullable_from_inst (cfg, method, context_used, bblock, val,
8325 ip, klass, generic_context, rgctx);
8330 GET_RGCTX (rgctx, context_used);
8331 if (cfg->opt & MONO_OPT_SHARED)
8332 rgctx_info = MONO_RGCTX_INFO_KLASS;
8334 rgctx_info = MONO_RGCTX_INFO_VTABLE;
8335 data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8336 generic_context, rgctx, rgctx_info, ip);
8338 *sp++ = handle_box_from_inst (cfg, bblock, val, ip, klass, data);
8341 *sp++ = handle_box (cfg, bblock, val, ip, klass);
8352 token = read32 (ip + 1);
8354 /* allocate the domainvar - becaus this is used in decompose_foreach */
8355 if (cfg->opt & MONO_OPT_SHARED) {
8356 mono_get_domainvar (cfg);
8357 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
8358 cfg->domainvar->flags |= MONO_INST_VOLATILE;
8362 mono_get_got_var (cfg);
8364 klass = mini_get_class (method, token, generic_context);
8365 CHECK_TYPELOAD (klass);
8367 if (cfg->generic_sharing_context)
8368 context_used = mono_class_check_context_used (klass);
8371 MonoInst *rgctx, *args [3];
8375 /* FIXME: what about domain-neutral code? */
8376 NEW_DOMAINCONST (cfg, args [0]);
8379 GET_RGCTX (rgctx, context_used);
8380 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8381 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8386 temp = mono_emit_jit_icall (cfg, bblock, mono_array_new, args, ip);
8387 NEW_TEMPLOAD (cfg, ins, temp);
8389 MONO_INST_NEW (cfg, ins, *ip);
8390 ins->inst_newa_class = klass;
8391 ins->inst_newa_len = *sp;
8392 ins->type = STACK_OBJ;
8393 ins->klass = mono_array_class_get (klass, 1);
8399 * we store the object so calls to create the array are not interleaved
8400 * with the arguments of other calls.
8403 MonoInst *store, *temp, *load;
8404 const char *data_ptr;
8407 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8408 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8409 store->cil_code = ins->cil_code;
8410 MONO_ADD_INS (bblock, store);
8412 * we inline/optimize the initialization sequence if possible.
8413 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
8414 * for small sizes open code the memcpy
8415 * ensure the rva field is big enough
8417 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))) {
8418 MonoMethod *memcpy_method = get_memcpy_method ();
8419 MonoInst *data_offset, *add;
8420 MonoInst *iargs [3];
8421 NEW_ICONST (cfg, iargs [2], data_size);
8422 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8423 load->cil_code = ins->cil_code;
8424 NEW_ICONST (cfg, data_offset, G_STRUCT_OFFSET (MonoArray, vector));
8425 MONO_INST_NEW (cfg, add, OP_PADD);
8426 add->inst_left = load;
8427 add->inst_right = data_offset;
8429 if (cfg->compile_aot) {
8430 NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(data_ptr), STACK_PTR, NULL);
8432 NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
8434 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
8437 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8438 load->cil_code = ins->cil_code;
8446 if (sp [0]->type != STACK_OBJ)
8448 MONO_INST_NEW (cfg, ins, *ip);
8450 ins->inst_left = *sp;
8451 ins->type = STACK_PTR;
8458 if (sp [0]->type != STACK_OBJ)
8461 klass = mini_get_class (method, read32 (ip + 1), generic_context);
8462 CHECK_TYPELOAD (klass);
8463 /* we need to make sure that this array is exactly the type it needs
8464 * to be for correctness. the wrappers are lax with their usage
8465 * so we need to ignore them here
8467 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
8470 /* Needed by the code generated in inssel.brg */
8471 mono_get_got_var (cfg);
8473 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
8474 check->klass = mono_array_class_get (klass, 1);
8475 check->inst_left = sp [0];
8476 check->type = STACK_OBJ;
8481 mono_class_init (klass);
8482 NEW_LDELEMA (cfg, ins, sp, klass);
8486 case CEE_LDELEM_ANY: {
8490 if (sp [0]->type != STACK_OBJ)
8493 token = read32 (ip + 1);
8494 klass = mini_get_class (method, token, generic_context);
8495 CHECK_TYPELOAD (klass);
8496 mono_class_init (klass);
8497 NEW_LDELEMA (cfg, load, sp, klass);
8498 MONO_INST_NEW (cfg, ins, mini_type_to_ldind (cfg, &klass->byval_arg));
8499 ins->inst_left = load;
8501 type_to_eval_stack_type (cfg, &klass->byval_arg, ins);
8515 case CEE_LDELEM_REF: {
8519 * ldind.x (ldelema (array, index))
8520 * ldelema does the bounds check
8524 if (sp [0]->type != STACK_OBJ)
8526 klass = array_access_to_klass (*ip, sp [0]);
8527 NEW_LDELEMA (cfg, load, sp, klass);
8528 #ifdef MONO_ARCH_SOFT_FLOAT
8529 if (*ip == CEE_LDELEM_R4) {
8531 temp = handle_load_float (cfg, bblock, load, ip);
8532 NEW_TEMPLOAD (cfg, *sp, temp);
8538 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
8539 ins->inst_left = load;
8541 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
8552 case CEE_STELEM_R8: {
8556 * stind.x (ldelema (array, index), val)
8557 * ldelema does the bounds check
8561 if (sp [0]->type != STACK_OBJ)
8563 klass = array_access_to_klass (*ip, sp [0]);
8564 NEW_LDELEMA (cfg, load, sp, klass);
8565 #ifdef MONO_ARCH_SOFT_FLOAT
8566 if (*ip == CEE_STELEM_R4) {
8567 handle_store_float (cfg, bblock, load, sp [2], ip);
8572 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8573 ins->inst_left = load;
8574 ins->inst_right = sp [2];
8576 handle_loaded_temps (cfg, bblock, stack_start, sp);
8577 MONO_ADD_INS (bblock, ins);
8581 case CEE_STELEM_ANY: {
8585 * stind.x (ldelema (array, index), val)
8586 * ldelema does the bounds check
8590 if (sp [0]->type != STACK_OBJ)
8593 token = read32 (ip + 1);
8594 klass = mini_get_class (method, token, generic_context);
8595 CHECK_TYPELOAD (klass);
8596 mono_class_init (klass);
8597 if (generic_class_is_reference_type (cfg, klass)) {
8598 /* storing a NULL doesn't need any of the complex checks in stelemref */
8599 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8601 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8602 MONO_INST_NEW (cfg, ins, CEE_STIND_REF);
8603 ins->inst_left = load;
8604 ins->inst_right = sp [2];
8605 MONO_ADD_INS (bblock, ins);
8607 MonoMethod* helper = mono_marshal_get_stelemref ();
8608 MonoInst *iargs [3];
8609 handle_loaded_temps (cfg, bblock, stack_start, sp);
8615 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8618 NEW_LDELEMA (cfg, load, sp, klass);
8620 n = mini_type_to_stind (cfg, &klass->byval_arg);
8621 /* FIXME: CEE_STIND_R4 */
8623 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
8625 MONO_INST_NEW (cfg, ins, n);
8626 ins->inst_left = load;
8627 ins->inst_right = sp [2];
8628 handle_loaded_temps (cfg, bblock, stack_start, sp);
8629 MONO_ADD_INS (bblock, ins);
8636 case CEE_STELEM_REF: {
8637 MonoInst *iargs [3];
8638 MonoMethod* helper = mono_marshal_get_stelemref ();
8642 if (sp [0]->type != STACK_OBJ)
8644 if (sp [2]->type != STACK_OBJ)
8647 handle_loaded_temps (cfg, bblock, stack_start, sp);
8649 /* storing a NULL doesn't need any of the complex checks in stelemref */
8650 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8652 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8653 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8654 ins->inst_left = load;
8655 ins->inst_right = sp [2];
8656 MONO_ADD_INS (bblock, ins);
8662 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8669 case CEE_CKFINITE: {
8670 MonoInst *store, *temp;
8673 /* this instr. can throw exceptions as side effect,
8674 * so we cant eliminate dead code which contains CKFINITE opdodes.
8675 * Spilling to memory makes sure that we always perform
8679 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
8680 ins->inst_left = sp [-1];
8681 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
8683 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8684 MONO_ADD_INS (bblock, store);
8686 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
8695 token = read32 (ip + 1);
8696 klass = mono_class_get_full (image, token, generic_context);
8697 CHECK_TYPELOAD (klass);
8698 mono_class_init (klass);
8700 /* Needed by the code generated in inssel.brg */
8701 mono_get_got_var (cfg);
8703 if (cfg->generic_sharing_context) {
8704 context_used = mono_class_check_context_used (klass);
8705 if (context_used && cfg->compile_aot)
8706 GENERIC_SHARING_FAILURE (*ip);
8712 MONO_INST_NEW (cfg, ins, OP_REFANYVAL_REG);
8713 ins->type = STACK_MP;
8714 ins->inst_left = *sp;
8717 GET_RGCTX (rgctx, context_used);
8718 ins->inst_right = get_runtime_generic_context_ptr (cfg, method, context_used,
8719 bblock, klass, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8721 MONO_INST_NEW (cfg, ins, *ip);
8722 ins->type = STACK_MP;
8723 ins->inst_left = *sp;
8725 ins->inst_newa_class = klass;
8730 case CEE_MKREFANY: {
8736 token = read32 (ip + 1);
8737 klass = mono_class_get_full (image, token, generic_context);
8738 CHECK_TYPELOAD (klass);
8739 mono_class_init (klass);
8741 if (cfg->generic_sharing_context) {
8742 context_used = mono_class_check_context_used (klass);
8743 if (context_used && cfg->compile_aot)
8744 GENERIC_SHARING_FAILURE (CEE_MKREFANY);
8747 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
8749 MonoInst *rgctx, *klass_type, *klass_klass, *loc_load;
8751 GET_RGCTX (rgctx, context_used);
8752 klass_klass = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8753 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8754 GET_RGCTX (rgctx, context_used);
8755 klass_type = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8756 generic_context, rgctx, MONO_RGCTX_INFO_TYPE, ip);
8758 NEW_TEMPLOADA (cfg, loc_load, loc->inst_c0);
8760 MONO_INST_NEW (cfg, ins, OP_MKREFANY_REGS);
8761 NEW_GROUP (cfg, ins->inst_left, klass_type, klass_klass);
8762 NEW_GROUP (cfg, ins->inst_right, *sp, loc_load);
8764 MonoInst *klassconst;
8766 NEW_PCONST (cfg, klassconst, klass);
8768 MONO_INST_NEW (cfg, ins, *ip);
8769 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
8770 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
8773 MONO_ADD_INS (bblock, ins);
8775 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
8782 MonoClass *handle_class;
8784 CHECK_STACK_OVF (1);
8787 n = read32 (ip + 1);
8789 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
8790 handle = mono_method_get_wrapper_data (method, n);
8791 handle_class = mono_method_get_wrapper_data (method, n + 1);
8792 if (handle_class == mono_defaults.typehandle_class)
8793 handle = &((MonoClass*)handle)->byval_arg;
8796 handle = mono_ldtoken (image, n, &handle_class, generic_context);
8800 mono_class_init (handle_class);
8802 if (cfg->generic_sharing_context) {
8803 if (handle_class == mono_defaults.typehandle_class) {
8804 /* If we get a MONO_TYPE_CLASS
8805 then we need to provide the
8807 instantiation of it. */
8808 if (mono_type_get_type (handle) == MONO_TYPE_CLASS)
8811 context_used = mono_class_check_context_used (mono_class_from_mono_type (handle));
8812 } else if (handle_class == mono_defaults.fieldhandle_class)
8813 context_used = mono_class_check_context_used (((MonoClassField*)handle)->parent);
8814 else if (handle_class == mono_defaults.methodhandle_class)
8815 context_used = mono_method_check_context_used (handle);
8817 g_assert_not_reached ();
8820 if (cfg->opt & MONO_OPT_SHARED) {
8822 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
8823 int method_context_used;
8825 if (cfg->generic_sharing_context)
8826 method_context_used = mono_method_check_context_used (method);
8828 method_context_used = 0;
8830 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8832 NEW_IMAGECONST (cfg, iargs [0], image);
8833 NEW_ICONST (cfg, iargs [1], n);
8834 if (method_context_used) {
8837 GET_RGCTX (rgctx, method_context_used);
8838 iargs [2] = get_runtime_generic_context_method (cfg, method, method_context_used,
8840 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
8841 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper_generic_shared,
8844 NEW_PCONST (cfg, iargs [2], generic_context);
8845 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
8847 NEW_TEMPLOAD (cfg, res, temp);
8848 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8849 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
8850 MONO_ADD_INS (bblock, store);
8851 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8853 if ((ip + 10 < end) && ip_in_bb (cfg, bblock, ip + 5) &&
8854 handle_class == mono_defaults.typehandle_class &&
8855 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
8856 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
8857 (cmethod->klass == mono_defaults.monotype_class->parent) &&
8858 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
8859 MonoClass *tclass = mono_class_from_mono_type (handle);
8860 mono_class_init (tclass);
8864 g_assert (!cfg->compile_aot);
8866 GET_RGCTX (rgctx, context_used);
8867 ins = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, tclass,
8868 generic_context, rgctx, MONO_RGCTX_INFO_REFLECTION_TYPE, ip);
8869 } else if (cfg->compile_aot) {
8871 * FIXME: We would have to include the context into the
8872 * aot constant too (tests/generic-array-type.2.exe).
8874 if (generic_context)
8875 cfg->disable_aot = TRUE;
8876 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
8878 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
8880 ins->type = STACK_OBJ;
8881 ins->klass = cmethod->klass;
8884 MonoInst *store, *addr, *vtvar;
8889 g_assert (!cfg->compile_aot);
8891 GET_RGCTX (rgctx, context_used);
8892 if (handle_class == mono_defaults.typehandle_class) {
8893 ins = get_runtime_generic_context_ptr (cfg, method,
8894 context_used, bblock,
8895 mono_class_from_mono_type (handle), generic_context,
8896 rgctx, MONO_RGCTX_INFO_TYPE, ip);
8897 } else if (handle_class == mono_defaults.methodhandle_class) {
8898 ins = get_runtime_generic_context_method (cfg, method,
8899 context_used, bblock, handle, generic_context,
8900 rgctx, MONO_RGCTX_INFO_METHOD, ip);
8901 } else if (handle_class == mono_defaults.fieldhandle_class) {
8902 ins = get_runtime_generic_context_field (cfg, method,
8903 context_used, bblock, handle, generic_context,
8904 rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
8906 g_assert_not_reached ();
8909 else if (cfg->compile_aot) {
8910 NEW_LDTOKENCONST (cfg, ins, image, n);
8912 NEW_PCONST (cfg, ins, handle);
8914 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8915 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8916 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
8917 MONO_ADD_INS (bblock, store);
8918 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8934 case CEE_ADD_OVF_UN:
8936 case CEE_MUL_OVF_UN:
8938 case CEE_SUB_OVF_UN:
8941 if (mono_find_jit_opcode_emulation (ins->opcode)) {
8943 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
8947 case CEE_ENDFINALLY:
8948 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
8949 MONO_ADD_INS (bblock, ins);
8951 start_new_bblock = 1;
8954 * Control will leave the method so empty the stack, otherwise
8955 * the next basic block will start with a nonempty stack.
8957 while (sp != stack_start) {
8958 MONO_INST_NEW (cfg, ins, CEE_POP);
8961 MONO_ADD_INS (bblock, ins);
8968 if (*ip == CEE_LEAVE) {
8970 target = ip + 5 + (gint32)read32(ip + 1);
8973 target = ip + 2 + (signed char)(ip [1]);
8976 /* empty the stack */
8977 while (sp != stack_start) {
8978 MONO_INST_NEW (cfg, ins, CEE_POP);
8981 MONO_ADD_INS (bblock, ins);
8985 * If this leave statement is in a catch block, check for a
8986 * pending exception, and rethrow it if necessary.
8988 for (i = 0; i < header->num_clauses; ++i) {
8989 MonoExceptionClause *clause = &header->clauses [i];
8992 * Use <= in the final comparison to handle clauses with multiple
8993 * leave statements, like in bug #78024.
8994 * The ordering of the exception clauses guarantees that we find the
8997 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)) {
9001 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
9003 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_undeniable_exception, NULL, ip);
9004 NEW_TEMPLOAD (cfg, *sp, temp);
9006 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
9007 ins->inst_left = *sp;
9008 ins->inst_right = load;
9009 MONO_ADD_INS (bblock, ins);
9013 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
9015 for (tmp = handlers; tmp; tmp = tmp->next) {
9017 link_bblock (cfg, bblock, tblock);
9018 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
9019 ins->inst_target_bb = tblock;
9020 MONO_ADD_INS (bblock, ins);
9022 g_list_free (handlers);
9025 MONO_INST_NEW (cfg, ins, OP_BR);
9026 MONO_ADD_INS (bblock, ins);
9027 GET_BBLOCK (cfg, tblock, target);
9028 link_bblock (cfg, bblock, tblock);
9029 CHECK_BBLOCK (target, ip, tblock);
9030 ins->inst_target_bb = tblock;
9031 start_new_bblock = 1;
9033 if (*ip == CEE_LEAVE)
9042 MONO_INST_NEW (cfg, ins, *ip);
9044 handle_loaded_temps (cfg, bblock, stack_start, sp);
9045 MONO_ADD_INS (bblock, ins);
9047 ins->inst_i0 = sp [0];
9048 ins->inst_i1 = sp [1];
9056 /* trampoline mono specific opcodes */
9057 case MONO_CUSTOM_PREFIX: {
9059 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
9064 case CEE_MONO_ICALL: {
9067 MonoJitICallInfo *info;
9069 token = read32 (ip + 2);
9070 func = mono_method_get_wrapper_data (method, token);
9071 info = mono_find_jit_icall_by_addr (func);
9073 g_error ("An attempt has been made to perform an icall to address %p, "
9074 "but the address has not been registered as an icall\n", info);
9075 g_assert_not_reached ();
9078 CHECK_STACK (info->sig->param_count);
9079 sp -= info->sig->param_count;
9081 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
9082 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
9083 NEW_TEMPLOAD (cfg, *sp, temp);
9088 inline_costs += 10 * num_calls++;
9092 case CEE_MONO_LDPTR: {
9095 CHECK_STACK_OVF (1);
9097 token = read32 (ip + 2);
9099 ptr = mono_method_get_wrapper_data (method, token);
9100 if (cfg->compile_aot && (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)) {
9101 MonoMethod *wrapped = mono_marshal_method_from_wrapper (cfg->method);
9103 if (wrapped && ptr != NULL && mono_lookup_internal_call (wrapped) == ptr) {
9104 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, wrapped);
9110 if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
9111 MonoJitICallInfo *callinfo;
9112 const char *icall_name;
9114 icall_name = method->name + strlen ("__icall_wrapper_");
9115 g_assert (icall_name);
9116 callinfo = mono_find_jit_icall_by_name (icall_name);
9117 g_assert (callinfo);
9119 if (ptr == callinfo->func) {
9120 /* Will be transformed into an AOTCONST later */
9121 NEW_PCONST (cfg, ins, ptr);
9128 /* FIXME: Generalize this */
9129 if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
9130 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
9135 NEW_PCONST (cfg, ins, ptr);
9138 inline_costs += 10 * num_calls++;
9139 /* Can't embed random pointers into AOT code */
9140 cfg->disable_aot = 1;
9143 case CEE_MONO_ICALL_ADDR: {
9144 MonoMethod *cmethod;
9146 CHECK_STACK_OVF (1);
9148 token = read32 (ip + 2);
9150 cmethod = mono_method_get_wrapper_data (method, token);
9152 g_assert (cfg->compile_aot);
9154 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
9159 case CEE_MONO_VTADDR:
9162 MONO_INST_NEW (cfg, ins, OP_VTADDR);
9163 ins->type = STACK_MP;
9164 ins->inst_left = *sp;
9168 case CEE_MONO_NEWOBJ: {
9169 MonoInst *iargs [2];
9171 CHECK_STACK_OVF (1);
9173 token = read32 (ip + 2);
9174 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9175 mono_class_init (klass);
9176 NEW_DOMAINCONST (cfg, iargs [0]);
9177 NEW_CLASSCONST (cfg, iargs [1], klass);
9178 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
9179 NEW_TEMPLOAD (cfg, *sp, temp);
9182 inline_costs += 10 * num_calls++;
9185 case CEE_MONO_OBJADDR:
9188 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
9189 ins->type = STACK_MP;
9190 ins->inst_left = *sp;
9194 case CEE_MONO_LDNATIVEOBJ:
9197 token = read32 (ip + 2);
9198 klass = mono_method_get_wrapper_data (method, token);
9199 g_assert (klass->valuetype);
9200 mono_class_init (klass);
9201 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
9205 case CEE_MONO_RETOBJ:
9206 g_assert (cfg->ret);
9207 g_assert (mono_method_signature (method)->pinvoke);
9212 token = read32 (ip + 2);
9213 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9215 NEW_RETLOADA (cfg, ins);
9216 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
9218 if (sp != stack_start)
9221 MONO_INST_NEW (cfg, ins, OP_BR);
9222 ins->inst_target_bb = end_bblock;
9223 MONO_ADD_INS (bblock, ins);
9224 link_bblock (cfg, bblock, end_bblock);
9225 start_new_bblock = 1;
9228 case CEE_MONO_CISINST:
9229 case CEE_MONO_CCASTCLASS: {
9234 token = read32 (ip + 2);
9235 /* Needed by the code generated in inssel.brg */
9236 mono_get_got_var (cfg);
9240 * The code generated for CCASTCLASS has too much register pressure
9241 * (obj+vtable+ibitmap_byte_reg+iid_reg), leading to the usual
9242 * branches-inside-bblocks problem.
9244 cfg->disable_aot = TRUE;
9247 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9248 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
9249 ins->type = STACK_I4;
9250 ins->inst_left = *sp;
9251 ins->inst_newa_class = klass;
9252 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
9256 case CEE_MONO_SAVE_LMF:
9257 case CEE_MONO_RESTORE_LMF:
9258 #ifdef MONO_ARCH_HAVE_LMF_OPS
9259 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
9260 MONO_ADD_INS (bblock, ins);
9261 cfg->need_lmf_area = TRUE;
9265 case CEE_MONO_CLASSCONST:
9266 CHECK_STACK_OVF (1);
9268 token = read32 (ip + 2);
9269 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
9272 inline_costs += 10 * num_calls++;
9274 case CEE_MONO_NOT_TAKEN:
9275 bblock->out_of_line = TRUE;
9279 CHECK_STACK_OVF (1);
9281 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
9282 ins->inst_offset = (gint32)read32 (ip + 2);
9283 ins->type = STACK_PTR;
9288 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
9297 /* somewhat similar to LDTOKEN */
9298 MonoInst *addr, *vtvar;
9299 CHECK_STACK_OVF (1);
9300 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
9302 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
9303 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
9304 ins->inst_left = addr;
9305 MONO_ADD_INS (bblock, ins);
9306 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
9319 * The following transforms:
9320 * CEE_CEQ into OP_CEQ
9321 * CEE_CGT into OP_CGT
9322 * CEE_CGT_UN into OP_CGT_UN
9323 * CEE_CLT into OP_CLT
9324 * CEE_CLT_UN into OP_CLT_UN
9326 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
9328 MONO_INST_NEW (cfg, ins, cmp->opcode);
9330 cmp->inst_i0 = sp [0];
9331 cmp->inst_i1 = sp [1];
9334 ins->type = STACK_I4;
9336 #if MONO_ARCH_SOFT_FLOAT
9337 if (sp [0]->type == STACK_R8) {
9338 cmp->type = STACK_I4;
9339 *sp++ = emit_tree (cfg, bblock, cmp, ip + 2);
9344 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
9345 cmp->opcode = OP_LCOMPARE;
9347 cmp->opcode = OP_COMPARE;
9349 /* spill it to reduce the expression complexity
9350 * and workaround bug 54209
9352 if (cmp->inst_left->type == STACK_I8) {
9354 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
9361 MonoMethod *cil_method, *ctor_method;
9363 gboolean is_shared = FALSE;
9365 CHECK_STACK_OVF (1);
9367 n = read32 (ip + 2);
9368 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9371 mono_class_init (cmethod->klass);
9373 if (cfg->generic_sharing_context)
9374 context_used = mono_method_check_context_used (cmethod);
9376 if (mono_class_generic_sharing_enabled (cmethod->klass)) {
9377 if ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) &&
9378 (cmethod->klass->generic_class ||
9379 cmethod->klass->generic_container)) {
9382 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst)
9386 cil_method = cmethod;
9387 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
9388 METHOD_ACCESS_FAILURE;
9389 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9390 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9392 CHECK_CFG_EXCEPTION;
9393 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9394 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9398 * Optimize the common case of ldftn+delegate creation
9400 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE) && !defined(HAVE_WRITE_BARRIERS)
9401 /* FIXME: SGEN support */
9402 /* FIXME: handle shared static generic methods */
9403 /* FIXME: handle this in shared code */
9404 if (!is_shared && !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)) {
9405 MonoInst *target_ins;
9408 if (cfg->verbose_level > 3)
9409 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
9410 target_ins = sp [-1];
9412 *sp = handle_delegate_ctor (cfg, bblock, ctor_method->klass, target_ins, cmethod, ip);
9419 handle_loaded_temps (cfg, bblock, stack_start, sp);
9425 cmethod = mono_marshal_get_static_rgctx_invoke (cmethod);
9427 GET_RGCTX (rgctx, context_used);
9428 argconst = get_runtime_generic_context_method (cfg, method, context_used,
9430 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9431 } else if (is_shared) {
9432 NEW_METHODCONST (cfg, argconst, mono_marshal_get_static_rgctx_invoke (cmethod));
9434 NEW_METHODCONST (cfg, argconst, cmethod);
9436 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
9437 NEW_TEMPLOAD (cfg, *sp, temp);
9441 inline_costs += 10 * num_calls++;
9444 case CEE_LDVIRTFTN: {
9450 n = read32 (ip + 2);
9451 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9454 mono_class_init (cmethod->klass);
9456 if (cfg->generic_sharing_context)
9457 context_used = mono_method_check_context_used (cmethod);
9459 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9460 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9462 CHECK_CFG_EXCEPTION;
9463 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9464 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9467 handle_loaded_temps (cfg, bblock, stack_start, sp);
9474 GET_RGCTX (rgctx, context_used);
9475 args [1] = get_runtime_generic_context_method (cfg, method, context_used,
9477 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9478 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn_gshared, args, ip);
9480 NEW_METHODCONST (cfg, args [1], cmethod);
9481 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
9483 NEW_TEMPLOAD (cfg, *sp, temp);
9487 inline_costs += 10 * num_calls++;
9491 CHECK_STACK_OVF (1);
9493 n = read16 (ip + 2);
9495 NEW_ARGLOAD (cfg, ins, n);
9496 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
9501 CHECK_STACK_OVF (1);
9503 n = read16 (ip + 2);
9505 NEW_ARGLOADA (cfg, ins, n);
9512 handle_loaded_temps (cfg, bblock, stack_start, sp);
9514 n = read16 (ip + 2);
9516 NEW_ARGSTORE (cfg, ins, n, *sp);
9517 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
9519 STARG_SOFT_FLOAT (cfg, ins, n, ip);
9520 if (ins->opcode == CEE_STOBJ) {
9521 NEW_ARGLOADA (cfg, ins, n);
9522 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9524 MONO_ADD_INS (bblock, ins);
9528 CHECK_STACK_OVF (1);
9530 n = read16 (ip + 2);
9532 NEW_LOCLOAD (cfg, ins, n);
9533 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
9538 CHECK_STACK_OVF (1);
9540 n = read16 (ip + 2);
9542 NEW_LOCLOADA (cfg, ins, n);
9550 n = read16 (ip + 2);
9552 handle_loaded_temps (cfg, bblock, stack_start, sp);
9553 NEW_LOCSTORE (cfg, ins, n, *sp);
9554 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
9556 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
9557 if (ins->opcode == CEE_STOBJ) {
9558 NEW_LOCLOADA (cfg, ins, n);
9559 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9561 MONO_ADD_INS (bblock, ins);
9568 if (sp != stack_start)
9570 if (cfg->method != method)
9572 * Inlining this into a loop in a parent could lead to
9573 * stack overflows which is different behavior than the
9574 * non-inlined case, thus disable inlining in this case.
9576 goto inline_failure;
9577 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
9578 ins->inst_left = *sp;
9579 ins->type = STACK_PTR;
9581 cfg->flags |= MONO_CFG_HAS_ALLOCA;
9582 if (header->init_locals)
9583 ins->flags |= MONO_INST_INIT;
9587 /* FIXME: set init flag if locals init is set in this method */
9589 case CEE_ENDFILTER: {
9590 MonoExceptionClause *clause, *nearest;
9591 int cc, nearest_num;
9595 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
9597 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
9598 ins->inst_left = *sp;
9599 MONO_ADD_INS (bblock, ins);
9600 start_new_bblock = 1;
9605 for (cc = 0; cc < header->num_clauses; ++cc) {
9606 clause = &header->clauses [cc];
9607 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
9608 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
9609 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
9615 if ((ip - header->code) != nearest->handler_offset)
9620 case CEE_UNALIGNED_:
9621 ins_flag |= MONO_INST_UNALIGNED;
9622 /* FIXME: record alignment? we can assume 1 for now */
9627 ins_flag |= MONO_INST_VOLATILE;
9631 ins_flag |= MONO_INST_TAILCALL;
9632 cfg->flags |= MONO_CFG_HAS_TAIL;
9633 /* Can't inline tail calls at this time */
9634 inline_costs += 100000;
9641 token = read32 (ip + 2);
9642 klass = mini_get_class (method, token, generic_context);
9643 CHECK_TYPELOAD (klass);
9645 if (generic_class_is_reference_type (cfg, klass)) {
9646 MonoInst *store, *load;
9647 NEW_PCONST (cfg, load, NULL);
9648 load->type = STACK_OBJ;
9649 load->klass = klass;
9650 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
9651 handle_loaded_temps (cfg, bblock, stack_start, sp);
9652 MONO_ADD_INS (bblock, store);
9653 store->inst_i0 = sp [0];
9654 store->inst_i1 = load;
9656 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
9661 case CEE_CONSTRAINED_:
9662 /* FIXME: implement */
9664 token = read32 (ip + 2);
9665 constrained_call = mono_class_get_full (image, token, generic_context);
9666 CHECK_TYPELOAD (constrained_call);
9671 MonoInst *iargs [3];
9674 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
9676 NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, 0);
9677 MONO_ADD_INS (bblock, copy);
9684 handle_loaded_temps (cfg, bblock, stack_start, sp);
9685 if (ip [1] == CEE_CPBLK) {
9686 MonoMethod *memcpy_method = get_memcpy_method ();
9687 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
9689 MonoMethod *memset_method = get_memset_method ();
9690 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
9699 ins_flag |= MONO_INST_NOTYPECHECK;
9701 ins_flag |= MONO_INST_NORANGECHECK;
9702 /* we ignore the no-nullcheck for now since we
9703 * really do it explicitly only when doing callvirt->call
9709 int handler_offset = -1;
9711 for (i = 0; i < header->num_clauses; ++i) {
9712 MonoExceptionClause *clause = &header->clauses [i];
9713 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
9714 handler_offset = clause->handler_offset;
9719 bblock->flags |= BB_EXCEPTION_UNSAFE;
9721 g_assert (handler_offset != -1);
9723 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
9724 MONO_INST_NEW (cfg, ins, OP_RETHROW);
9725 ins->inst_left = load;
9726 MONO_ADD_INS (bblock, ins);
9728 link_bblock (cfg, bblock, end_bblock);
9729 start_new_bblock = 1;
9734 CHECK_STACK_OVF (1);
9736 token = read32 (ip + 2);
9737 /* FIXXME: handle generics. */
9738 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
9739 MonoType *type = mono_type_create_from_typespec (image, token);
9740 token = mono_type_size (type, &ialign);
9742 MonoClass *klass = mono_class_get_full (image, token, generic_context);
9743 CHECK_TYPELOAD (klass);
9744 mono_class_init (klass);
9745 token = mono_class_value_size (klass, &align);
9747 NEW_ICONST (cfg, ins, token);
9751 case CEE_REFANYTYPE:
9753 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
9755 ins->type = STACK_MP;
9756 ins->inst_left = *sp;
9757 ins->type = STACK_VTYPE;
9758 ins->klass = mono_defaults.typehandle_class;
9767 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
9772 g_error ("opcode 0x%02x not handled", *ip);
9775 if (start_new_bblock != 1)
9778 bblock->cil_length = ip - bblock->cil_code;
9779 bblock->next_bb = end_bblock;
9781 if (cfg->method == method && cfg->domainvar) {
9783 MonoInst *get_domain;
9785 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
9788 MONO_INST_NEW_CALL (cfg, call, OP_CALL);
9789 call->signature = helper_sig_domain_get;
9790 call->inst.type = STACK_PTR;
9791 call->fptr = mono_domain_get;
9792 get_domain = (MonoInst*)call;
9795 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
9796 MONO_ADD_INS (init_localsbb, store);
9799 if (cfg->method == method && cfg->got_var)
9800 mono_emit_load_got_addr (cfg);
9802 if (header->init_locals) {
9804 cfg->ip = header->code;
9805 for (i = 0; i < header->num_locals; ++i) {
9806 MonoType *ptype = header->locals [i];
9807 int t = ptype->type;
9808 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
9809 t = ptype->data.klass->enum_basetype->type;
9811 NEW_PCONST (cfg, ins, NULL);
9812 NEW_LOCSTORE (cfg, store, i, ins);
9813 MONO_ADD_INS (init_localsbb, store);
9814 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
9815 NEW_ICONST (cfg, ins, 0);
9816 NEW_LOCSTORE (cfg, store, i, ins);
9817 MONO_ADD_INS (init_localsbb, store);
9818 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
9819 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9820 ins->type = STACK_I8;
9822 NEW_LOCSTORE (cfg, store, i, ins);
9823 MONO_ADD_INS (init_localsbb, store);
9824 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
9825 #ifdef MONO_ARCH_SOFT_FLOAT
9826 /* FIXME: handle init of R4 */
9828 MONO_INST_NEW (cfg, ins, OP_R8CONST);
9829 ins->type = STACK_R8;
9830 ins->inst_p0 = (void*)&r8_0;
9831 NEW_LOCSTORE (cfg, store, i, ins);
9832 MONO_ADD_INS (init_localsbb, store);
9834 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
9835 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
9836 NEW_LOCLOADA (cfg, ins, i);
9837 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
9839 NEW_PCONST (cfg, ins, NULL);
9840 NEW_LOCSTORE (cfg, store, i, ins);
9841 MONO_ADD_INS (init_localsbb, store);
9848 /* resolve backward branches in the middle of an existing basic block */
9849 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
9851 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
9852 tblock = find_previous (cfg->cil_offset_to_bb, header->code_size, start_bblock, bblock->cil_code);
9853 if (tblock != start_bblock) {
9855 split_bblock (cfg, tblock, bblock);
9856 l = bblock->cil_code - header->code;
9857 bblock->cil_length = tblock->cil_length - l;
9858 tblock->cil_length = l;
9860 g_print ("recheck failed.\n");
9864 if (cfg->method == method) {
9866 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9867 bb->region = mono_find_block_region (cfg, bb->real_offset);
9869 mono_create_spvar_for_region (cfg, bb->region);
9870 if (cfg->verbose_level > 2)
9871 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
9875 g_slist_free (class_inits);
9876 dont_inline = g_list_remove (dont_inline, method);
9878 if (inline_costs < 0) {
9881 /* Method is too large */
9882 mname = mono_method_full_name (method, TRUE);
9883 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
9884 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
9889 return inline_costs;
9892 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
9893 g_slist_free (class_inits);
9894 dont_inline = g_list_remove (dont_inline, method);
9898 g_slist_free (class_inits);
9899 dont_inline = g_list_remove (dont_inline, method);
9903 g_slist_free (class_inits);
9904 dont_inline = g_list_remove (dont_inline, method);
9905 cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
9909 g_slist_free (class_inits);
9910 dont_inline = g_list_remove (dont_inline, method);
9911 set_exception_type_from_invalid_il (cfg, method, ip);
9916 mono_print_tree (MonoInst *tree) {
9922 arity = mono_burg_arity [tree->opcode];
9924 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
9926 switch (tree->opcode) {
9928 printf ("[%d]", (int)tree->inst_c0);
9931 printf ("[%lld]", (long long)tree->inst_l);
9934 printf ("[%f]", *(double*)tree->inst_p0);
9937 printf ("[%f]", *(float*)tree->inst_p0);
9941 printf ("[%d]", (int)tree->inst_c0);
9944 if (tree->inst_offset < 0)
9945 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9947 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9950 printf ("[%s]", mono_arch_regname (tree->dreg));
9953 printf ("[%s]", tree->inst_newa_class->name);
9954 mono_print_tree (tree->inst_newa_len);
9965 case OP_VOIDCALLVIRT:
9966 case OP_TRAMPCALL_VTABLE: {
9967 MonoCallInst *call = (MonoCallInst*)tree;
9969 printf ("[%s]", call->method->name);
9970 else if (call->fptr) {
9971 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
9973 printf ("[%s]", info->name);
9979 printf ("[%d (", (int)tree->inst_c0);
9980 for (i = 0; i < tree->inst_phi_args [0]; i++) {
9983 printf ("%d", tree->inst_phi_args [i + 1]);
9994 case OP_LOAD_MEMBASE:
9995 case OP_LOADI4_MEMBASE:
9996 case OP_LOADU4_MEMBASE:
9997 case OP_LOADU1_MEMBASE:
9998 case OP_LOADI1_MEMBASE:
9999 case OP_LOADU2_MEMBASE:
10000 case OP_LOADI2_MEMBASE:
10001 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
10004 case OP_CALL_HANDLER:
10005 printf ("[B%d]", tree->inst_target_bb->block_num);
10009 case CEE_CASTCLASS:
10015 case OP_VOIDCALL_REG:
10016 mono_print_tree (tree->inst_left);
10028 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
10029 mono_print_tree (tree->inst_left);
10032 if (!mono_arch_print_tree(tree, arity)) {
10034 mono_print_tree (tree->inst_left);
10036 mono_print_tree (tree->inst_right);
10047 mono_print_tree_nl (MonoInst *tree)
10049 mono_print_tree (tree);
10054 create_helper_signature (void)
10056 helper_sig_domain_get = mono_create_icall_signature ("ptr");
10057 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
10058 helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
10059 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
10063 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
10066 MonoMethod *wrapper;
10067 gconstpointer trampoline;
10068 MonoDomain *domain = mono_get_root_domain ();
10070 if (callinfo->wrapper) {
10071 return callinfo->wrapper;
10074 if (callinfo->trampoline)
10075 return callinfo->trampoline;
10078 * We use the lock on the root domain instead of the JIT lock to protect
10079 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
10081 mono_domain_lock (domain);
10083 if (callinfo->trampoline) {
10084 mono_domain_unlock (domain);
10085 return callinfo->trampoline;
10088 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
10089 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
10092 trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
10093 mono_register_jit_icall_wrapper (callinfo, trampoline);
10095 callinfo->trampoline = trampoline;
10097 mono_domain_unlock (domain);
10099 return callinfo->trampoline;
10103 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
10105 if (!domain->dynamic_code_hash)
10106 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
10107 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
10110 static MonoJitDynamicMethodInfo*
10111 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
10113 MonoJitDynamicMethodInfo *res;
10115 if (domain->dynamic_code_hash)
10116 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
10124 GList *active, *inactive;
10129 compare_by_interval_start_pos_func (gconstpointer a, gconstpointer b)
10131 MonoMethodVar *v1 = (MonoMethodVar*)a;
10132 MonoMethodVar *v2 = (MonoMethodVar*)b;
10136 else if (v1->interval->range && v2->interval->range)
10137 return v1->interval->range->from - v2->interval->range->from;
10138 else if (v1->interval->range)
10144 #ifndef DISABLE_JIT
10147 #define LSCAN_DEBUG(a) do { a; } while (0)
10149 #define LSCAN_DEBUG(a)
10153 mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10155 int i, slot, offset, size;
10157 MonoMethodVar *vmv;
10160 GList *vars = NULL, *l, *unhandled;
10161 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10165 LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg->method, TRUE)));
10167 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10168 vtype_stack_slots = NULL;
10171 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10172 for (i = 0; i < cfg->num_varinfo; ++i)
10175 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10176 inst = cfg->varinfo [i];
10177 vmv = MONO_VARINFO (cfg, i);
10179 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10182 vars = g_list_prepend (vars, vmv);
10185 vars = g_list_sort (g_list_copy (vars), compare_by_interval_start_pos_func);
10190 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10191 MonoMethodVar *current = unhandled->data;
10193 if (current->interval->range) {
10194 g_assert (current->interval->range->from >= i);
10195 i = current->interval->range->from;
10202 for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10203 MonoMethodVar *current = unhandled->data;
10206 inst = cfg->varinfo [vmv->idx];
10208 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10209 * pinvoke wrappers when they call functions returning structures */
10210 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10211 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10215 size = mono_type_size (inst->inst_vtype, &ialign);
10219 t = mono_type_get_underlying_type (inst->inst_vtype);
10221 case MONO_TYPE_GENERICINST:
10222 if (!mono_type_generic_inst_is_valuetype (t)) {
10223 slot_info = &scalar_stack_slots [t->type];
10227 case MONO_TYPE_VALUETYPE:
10228 if (!vtype_stack_slots)
10229 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10230 for (i = 0; i < nvtypes; ++i)
10231 if (t->data.klass == vtype_stack_slots [i].vtype)
10234 slot_info = &vtype_stack_slots [i];
10236 g_assert (nvtypes < 256);
10237 vtype_stack_slots [nvtypes].vtype = t->data.klass;
10238 slot_info = &vtype_stack_slots [nvtypes];
10242 case MONO_TYPE_CLASS:
10243 case MONO_TYPE_OBJECT:
10244 case MONO_TYPE_ARRAY:
10245 case MONO_TYPE_SZARRAY:
10246 case MONO_TYPE_STRING:
10247 case MONO_TYPE_PTR:
10250 #if SIZEOF_VOID_P == 4
10254 /* Share non-float stack slots of the same size */
10255 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10259 slot_info = &scalar_stack_slots [t->type];
10263 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10267 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10269 if (!current->interval->range) {
10270 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
10274 inst->flags |= MONO_INST_IS_DEAD;
10279 pos = current->interval->range->from;
10281 LSCAN_DEBUG (printf ("process R%d ", inst->dreg));
10282 if (current->interval->range)
10283 LSCAN_DEBUG (mono_linterval_print (current->interval));
10284 LSCAN_DEBUG (printf ("\n"));
10286 /* Check for intervals in active which expired or inactive */
10288 /* FIXME: Optimize this */
10291 for (l = slot_info->active; l != NULL; l = l->next) {
10292 MonoMethodVar *v = (MonoMethodVar*)l->data;
10294 if (v->interval->last_range->to < pos) {
10295 slot_info->active = g_list_delete_link (slot_info->active, l);
10296 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10297 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10301 else if (!mono_linterval_covers (v->interval, pos)) {
10302 slot_info->inactive = g_list_append (slot_info->inactive, v);
10303 slot_info->active = g_list_delete_link (slot_info->active, l);
10304 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg->varinfo [v->idx]->dreg));
10311 /* Check for intervals in inactive which expired or active */
10313 /* FIXME: Optimize this */
10316 for (l = slot_info->inactive; l != NULL; l = l->next) {
10317 MonoMethodVar *v = (MonoMethodVar*)l->data;
10319 if (v->interval->last_range->to < pos) {
10320 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10321 // FIXME: Enabling this seems to cause impossible to debug crashes
10322 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10323 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10327 else if (mono_linterval_covers (v->interval, pos)) {
10328 slot_info->active = g_list_append (slot_info->active, v);
10329 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10330 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg->varinfo [v->idx]->dreg));
10338 * This also handles the case when the variable is used in an
10339 * exception region, as liveness info is not computed there.
10342 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10345 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10346 if (slot_info->slots) {
10347 slot = GPOINTER_TO_INT (slot_info->slots->data);
10349 slot_info->slots = slot_info->slots->next;
10352 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
10354 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10360 static int count = 0;
10363 if (count == atoi (getenv ("COUNT3")))
10364 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10365 if (count > atoi (getenv ("COUNT3")))
10368 mono_print_tree_nl (inst);
10373 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst->dreg, mono_type_full_name (t), slot));
10375 if (slot == 0xffffff) {
10377 * Allways allocate valuetypes to sizeof (gpointer) to allow more
10378 * efficient copying (and to work around the fact that OP_MEMCPY
10379 * and OP_MEMSET ignores alignment).
10381 if (MONO_TYPE_ISSTRUCT (t))
10382 align = sizeof (gpointer);
10386 offset += align - 1;
10387 offset &= ~(align - 1);
10391 offset += align - 1;
10392 offset &= ~(align - 1);
10397 if (*stack_align == 0)
10398 *stack_align = align;
10401 offsets [vmv->idx] = slot;
10403 g_list_free (vars);
10404 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10405 if (scalar_stack_slots [i].active)
10406 g_list_free (scalar_stack_slots [i].active);
10408 for (i = 0; i < nvtypes; ++i) {
10409 if (vtype_stack_slots [i].active)
10410 g_list_free (vtype_stack_slots [i].active);
10413 mono_jit_stats.locals_stack_size += offset;
10415 *stack_size = offset;
10420 * mono_allocate_stack_slots_full:
10422 * Allocate stack slots for all non register allocated variables using a
10423 * linear scan algorithm.
10424 * Returns: an array of stack offsets.
10425 * STACK_SIZE is set to the amount of stack space needed.
10426 * STACK_ALIGN is set to the alignment needed by the locals area.
10429 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10431 int i, slot, offset, size;
10433 MonoMethodVar *vmv;
10436 GList *vars = NULL, *l;
10437 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10441 if ((cfg->num_varinfo > 0) && MONO_VARINFO (cfg, 0)->interval)
10442 return mono_allocate_stack_slots_full2 (cfg, backward, stack_size, stack_align);
10444 scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10445 vtype_stack_slots = NULL;
10448 offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10449 for (i = 0; i < cfg->num_varinfo; ++i)
10452 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10453 inst = cfg->varinfo [i];
10454 vmv = MONO_VARINFO (cfg, i);
10456 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10459 vars = g_list_prepend (vars, vmv);
10462 vars = mono_varlist_sort (cfg, vars, 0);
10465 for (l = vars; l; l = l->next) {
10467 inst = cfg->varinfo [vmv->idx];
10469 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10470 * pinvoke wrappers when they call functions returning structures */
10471 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10472 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10476 size = mono_type_size (inst->inst_vtype, &ialign);
10480 t = mono_type_get_underlying_type (inst->inst_vtype);
10482 slot_info = &scalar_stack_slots [MONO_TYPE_I];
10485 case MONO_TYPE_GENERICINST:
10486 if (!mono_type_generic_inst_is_valuetype (t)) {
10487 slot_info = &scalar_stack_slots [t->type];
10491 case MONO_TYPE_VALUETYPE:
10492 if (!vtype_stack_slots)
10493 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10494 for (i = 0; i < nvtypes; ++i)
10495 if (t->data.klass == vtype_stack_slots [i].vtype)
10498 slot_info = &vtype_stack_slots [i];
10500 g_assert (nvtypes < 256);
10501 vtype_stack_slots [nvtypes].vtype = t->data.klass;
10502 slot_info = &vtype_stack_slots [nvtypes];
10506 case MONO_TYPE_CLASS:
10507 case MONO_TYPE_OBJECT:
10508 case MONO_TYPE_ARRAY:
10509 case MONO_TYPE_SZARRAY:
10510 case MONO_TYPE_STRING:
10511 case MONO_TYPE_PTR:
10514 #if SIZEOF_VOID_P == 4
10519 /* Share non-float stack slots of the same size */
10520 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10523 slot_info = &scalar_stack_slots [t->type];
10528 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10529 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10531 /* expire old intervals in active */
10532 while (slot_info->active) {
10533 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
10535 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
10538 //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);
10540 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
10541 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
10545 * This also handles the case when the variable is used in an
10546 * exception region, as liveness info is not computed there.
10549 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10552 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10553 if (slot_info->slots) {
10554 slot = GPOINTER_TO_INT (slot_info->slots->data);
10556 slot_info->slots = slot_info->slots->next;
10559 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10564 static int count = 0;
10568 if (count == atoi (getenv ("COUNT")))
10569 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10570 if (count > atoi (getenv ("COUNT")))
10573 mono_print_tree_nl (inst);
10578 if (cfg->disable_reuse_stack_slots)
10581 if (slot == 0xffffff) {
10583 * Allways allocate valuetypes to sizeof (gpointer) to allow more
10584 * efficient copying (and to work around the fact that OP_MEMCPY
10585 * and OP_MEMSET ignores alignment).
10587 if (MONO_TYPE_ISSTRUCT (t))
10588 align = sizeof (gpointer);
10592 offset += align - 1;
10593 offset &= ~(align - 1);
10597 offset += align - 1;
10598 offset &= ~(align - 1);
10603 if (*stack_align == 0)
10604 *stack_align = align;
10607 offsets [vmv->idx] = slot;
10609 g_list_free (vars);
10610 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10611 if (scalar_stack_slots [i].active)
10612 g_list_free (scalar_stack_slots [i].active);
10614 for (i = 0; i < nvtypes; ++i) {
10615 if (vtype_stack_slots [i].active)
10616 g_list_free (vtype_stack_slots [i].active);
10619 mono_jit_stats.locals_stack_size += offset;
10621 *stack_size = offset;
10626 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
10628 return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
10634 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10636 g_assert_not_reached ();
10640 #endif /* DISABLE_JIT */
10643 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
10645 MonoJitICallInfo *info;
10646 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
10648 if (!emul_opcode_map)
10649 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
10651 g_assert (!sig->hasthis);
10652 g_assert (sig->param_count < 3);
10654 info = mono_register_jit_icall (func, name, sig, no_throw);
10656 emul_opcode_map [opcode] = info;
10660 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
10662 MonoMethodSignature *sig;
10665 sig = mono_create_icall_signature (sigstr);
10669 mono_register_jit_icall (func, name, sig, save);
10673 decompose_foreach (MonoInst *tree, gpointer data)
10675 static MonoJitICallInfo *newarr_info = NULL;
10676 static MonoJitICallInfo *newarr_specific_info = NULL;
10677 MonoJitICallInfo *info;
10680 switch (tree->opcode) {
10682 MonoCompile *cfg = data;
10683 MonoInst *iargs [3];
10685 if (!newarr_info) {
10686 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
10687 g_assert (newarr_info);
10688 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
10689 g_assert (newarr_specific_info);
10692 if (cfg->opt & MONO_OPT_SHARED) {
10693 NEW_DOMAINCONST (cfg, iargs [0]);
10694 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
10695 iargs [2] = tree->inst_newa_len;
10697 info = newarr_info;
10700 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
10703 NEW_VTABLECONST (cfg, iargs [0], vtable);
10704 iargs [1] = tree->inst_newa_len;
10706 info = newarr_specific_info;
10709 mono_emulate_opcode (cfg, tree, iargs, info);
10711 /* Need to decompose arguments after the the opcode is decomposed */
10712 for (i = 0; i < info->sig->param_count; ++i)
10713 dec_foreach (iargs [i], cfg);
10716 #ifdef MONO_ARCH_SOFT_FLOAT
10727 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10728 MonoCompile *cfg = data;
10729 MonoInst *iargs [2];
10731 iargs [0] = tree->inst_i0;
10732 iargs [1] = tree->inst_i1;
10734 mono_emulate_opcode (cfg, tree, iargs, info);
10736 dec_foreach (iargs [0], cfg);
10737 dec_foreach (iargs [1], cfg);
10740 g_assert_not_reached ();
10749 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10750 MonoCompile *cfg = data;
10751 MonoInst *iargs [2];
10753 /* the args are in the compare opcode ... */
10754 iargs [0] = tree->inst_i0;
10755 iargs [1] = tree->inst_i1;
10757 mono_emulate_opcode (cfg, tree, iargs, info);
10759 dec_foreach (iargs [0], cfg);
10760 dec_foreach (iargs [1], cfg);
10763 g_assert_not_reached ();
10775 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
10777 switch (mono_burg_arity [tree->opcode]) {
10780 mono_inst_foreach (tree->inst_left, func, data);
10783 mono_inst_foreach (tree->inst_left, func, data);
10784 mono_inst_foreach (tree->inst_right, func, data);
10787 g_assert_not_reached ();
10794 mono_print_bb_code (MonoBasicBlock *bb)
10798 MONO_BB_FOR_EACH_INS (bb, c) {
10799 mono_print_tree (c);
10805 print_dfn (MonoCompile *cfg) {
10808 MonoBasicBlock *bb;
10811 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
10813 for (i = 0; i < cfg->num_bblocks; ++i) {
10814 bb = cfg->bblocks [i];
10815 /*if (bb->cil_code) {
10816 char* code1, *code2;
10817 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
10818 if (bb->last_ins->cil_code)
10819 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
10821 code2 = g_strdup ("");
10823 code1 [strlen (code1) - 1] = 0;
10824 code = g_strdup_printf ("%s -> %s", code1, code2);
10828 code = g_strdup ("\n");
10829 g_print ("\nBB%d (%d) (len: %d): %s", bb->block_num, i, bb->cil_length, code);
10830 MONO_BB_FOR_EACH_INS (bb, c) {
10832 mono_print_ins_index (-1, c);
10834 mono_print_tree (c);
10839 g_print ("\tprev:");
10840 for (j = 0; j < bb->in_count; ++j) {
10841 g_print (" BB%d", bb->in_bb [j]->block_num);
10843 g_print ("\t\tsucc:");
10844 for (j = 0; j < bb->out_count; ++j) {
10845 g_print (" BB%d", bb->out_bb [j]->block_num);
10847 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
10850 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
10852 if (bb->dominators)
10853 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
10855 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
10863 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
10865 MONO_ADD_INS (bb, inst);
10869 mono_bblock_insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10873 bb->code = ins_to_insert;
10875 /* Link with next */
10876 ins_to_insert->next = ins;
10878 ins->prev = ins_to_insert;
10880 if (bb->last_ins == NULL)
10881 bb->last_ins = ins_to_insert;
10883 /* Link with next */
10884 ins_to_insert->next = ins->next;
10886 ins->next->prev = ins_to_insert;
10888 /* Link with previous */
10889 ins->next = ins_to_insert;
10890 ins_to_insert->prev = ins;
10892 if (bb->last_ins == ins)
10893 bb->last_ins = ins_to_insert;
10898 mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10903 bb->code = ins_to_insert;
10904 ins_to_insert->next = ins;
10905 if (bb->last_ins == NULL)
10906 bb->last_ins = ins_to_insert;
10908 /* Link with previous */
10910 ins->prev->next = ins_to_insert;
10911 ins_to_insert->prev = ins->prev;
10913 /* Link with next */
10914 ins->prev = ins_to_insert;
10915 ins_to_insert->next = ins;
10917 if (bb->code == ins)
10918 bb->code = ins_to_insert;
10923 * mono_verify_bblock:
10925 * Verify that the next and prev pointers are consistent inside the instructions in BB.
10928 mono_verify_bblock (MonoBasicBlock *bb)
10930 MonoInst *ins, *prev;
10933 for (ins = bb->code; ins; ins = ins->next) {
10934 g_assert (ins->prev == prev);
10938 g_assert (!bb->last_ins->next);
10944 * Perform consistency checks on the JIT data structures and the IR
10947 mono_verify_cfg (MonoCompile *cfg)
10949 MonoBasicBlock *bb;
10951 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
10952 mono_verify_bblock (bb);
10956 mono_destroy_compile (MonoCompile *cfg)
10958 //mono_mempool_stats (cfg->mempool);
10959 mono_free_loop_info (cfg);
10961 mono_regstate_free (cfg->rs);
10963 g_hash_table_destroy (cfg->spvars);
10965 g_hash_table_destroy (cfg->exvars);
10966 mono_mempool_destroy (cfg->mempool);
10967 g_list_free (cfg->ldstr_list);
10968 g_hash_table_destroy (cfg->token_info_hash);
10969 if (cfg->abs_patches)
10970 g_hash_table_destroy (cfg->abs_patches);
10972 g_free (cfg->varinfo);
10973 g_free (cfg->vars);
10974 g_free (cfg->exception_message);
10978 #ifdef HAVE_KW_THREAD
10979 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
10980 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
10982 * When this is defined, the current lmf is stored in this tls variable instead of in
10985 static __thread gpointer mono_lmf MONO_TLS_FAST;
10990 mono_get_jit_tls_key (void)
10992 return mono_jit_tls_id;
10996 mono_get_jit_tls_offset (void)
10998 #ifdef HAVE_KW_THREAD
11000 MONO_THREAD_VAR_OFFSET (mono_jit_tls, offset);
11008 mono_get_lmf_tls_offset (void)
11010 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11012 MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
11020 mono_get_lmf_addr_tls_offset (void)
11023 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
11028 mono_get_lmf (void)
11030 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11033 MonoJitTlsData *jit_tls;
11035 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11036 return jit_tls->lmf;
11038 g_assert_not_reached ();
11044 mono_get_lmf_addr (void)
11046 #ifdef HAVE_KW_THREAD
11047 return mono_lmf_addr;
11049 MonoJitTlsData *jit_tls;
11051 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11052 return &jit_tls->lmf;
11054 g_assert_not_reached ();
11059 /* Called by native->managed wrappers */
11061 mono_jit_thread_attach (MonoDomain *domain)
11063 #ifdef HAVE_KW_THREAD
11064 if (!mono_lmf_addr) {
11065 mono_thread_attach (domain);
11068 if (!TlsGetValue (mono_jit_tls_id))
11069 mono_thread_attach (domain);
11071 if (mono_domain_get () != domain)
11072 mono_domain_set (domain, TRUE);
11076 * mono_thread_abort:
11077 * @obj: exception object
11079 * abort the thread, print exception information and stack trace
11082 mono_thread_abort (MonoObject *obj)
11084 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
11086 /* handle_remove should be eventually called for this thread, too
11087 g_free (jit_tls);*/
11089 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANLED_POLICY_LEGACY) ||
11090 (obj->vtable->klass == mono_defaults.threadabortexception_class)) {
11091 mono_thread_exit ();
11093 exit (mono_environment_exitcode_get ());
11098 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
11100 MonoJitTlsData *jit_tls;
11103 jit_tls = TlsGetValue (mono_jit_tls_id);
11107 jit_tls = g_new0 (MonoJitTlsData, 1);
11109 TlsSetValue (mono_jit_tls_id, jit_tls);
11111 #ifdef HAVE_KW_THREAD
11112 mono_jit_tls = jit_tls;
11115 jit_tls->abort_func = abort_func;
11116 jit_tls->end_of_stack = stack_start;
11118 lmf = g_new0 (MonoLMF, 1);
11119 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
11120 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf);
11125 jit_tls->first_lmf = lmf;
11127 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11128 /* jit_tls->lmf is unused */
11130 mono_lmf_addr = &mono_lmf;
11132 #if defined(HAVE_KW_THREAD)
11133 mono_lmf_addr = &jit_tls->lmf;
11136 jit_tls->lmf = lmf;
11139 mono_arch_setup_jit_tls_data (jit_tls);
11140 mono_setup_altstack (jit_tls);
11146 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
11148 MonoThread *thread;
11149 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
11150 thread = mono_thread_current ();
11151 mono_debugger_thread_created (tid, thread, jit_tls);
11153 thread->jit_data = jit_tls;
11156 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
11159 mono_thread_abort_dummy (MonoObject *obj)
11161 if (mono_thread_attach_aborted_cb)
11162 mono_thread_attach_aborted_cb (obj);
11164 mono_thread_abort (obj);
11168 mono_thread_attach_cb (gsize tid, gpointer stack_start)
11170 MonoThread *thread;
11171 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
11172 thread = mono_thread_current ();
11173 mono_debugger_thread_created (tid, thread, (MonoJitTlsData *) jit_tls);
11175 thread->jit_data = jit_tls;
11176 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
11177 setup_stat_profiler ();
11181 mini_thread_cleanup (MonoThread *thread)
11183 MonoJitTlsData *jit_tls = thread->jit_data;
11186 mono_debugger_thread_cleanup (jit_tls);
11187 mono_arch_free_jit_tls_data (jit_tls);
11189 mono_free_altstack (jit_tls);
11190 g_free (jit_tls->first_lmf);
11192 thread->jit_data = NULL;
11193 TlsSetValue (mono_jit_tls_id, NULL);
11198 mono_create_tls_get (MonoCompile *cfg, int offset)
11200 #ifdef MONO_ARCH_HAVE_TLS_GET
11206 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
11207 ins->dreg = cfg->new_ir ? mono_alloc_preg (cfg) : mono_regstate_next_int (cfg->rs);
11208 ins->inst_offset = offset;
11216 mono_get_jit_tls_intrinsic (MonoCompile *cfg)
11218 return mono_create_tls_get (cfg, mono_get_jit_tls_offset ());
11222 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
11224 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
11228 ji->data.target = target;
11229 ji->next = cfg->patch_info;
11231 cfg->patch_info = ji;
11235 mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target)
11237 MonoJumpInfo *ji = g_new0 (MonoJumpInfo, 1);
11241 ji->data.target = target;
11248 mono_remove_patch_info (MonoCompile *cfg, int ip)
11250 MonoJumpInfo **ji = &cfg->patch_info;
11253 if ((*ji)->ip.i == ip)
11256 ji = &((*ji)->next);
11261 * mono_patch_info_dup_mp:
11263 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
11266 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
11268 MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
11269 memcpy (res, patch_info, sizeof (MonoJumpInfo));
11271 switch (patch_info->type) {
11272 case MONO_PATCH_INFO_RVA:
11273 case MONO_PATCH_INFO_LDSTR:
11274 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11275 case MONO_PATCH_INFO_LDTOKEN:
11276 case MONO_PATCH_INFO_DECLSEC:
11277 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
11278 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
11280 case MONO_PATCH_INFO_SWITCH:
11281 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
11282 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
11284 case MONO_PATCH_INFO_RGCTX_FETCH:
11285 res->data.rgctx_entry = mono_mempool_alloc (mp, sizeof (MonoJumpInfoRgctxEntry));
11286 memcpy (res->data.rgctx_entry, patch_info->data.rgctx_entry, sizeof (MonoJumpInfoRgctxEntry));
11287 res->data.rgctx_entry->data = mono_patch_info_dup_mp (mp, res->data.rgctx_entry->data);
11297 mono_patch_info_hash (gconstpointer data)
11299 const MonoJumpInfo *ji = (MonoJumpInfo*)data;
11301 switch (ji->type) {
11302 case MONO_PATCH_INFO_RVA:
11303 case MONO_PATCH_INFO_LDSTR:
11304 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11305 case MONO_PATCH_INFO_LDTOKEN:
11306 case MONO_PATCH_INFO_DECLSEC:
11307 return (ji->type << 8) | ji->data.token->token;
11308 case MONO_PATCH_INFO_VTABLE:
11309 case MONO_PATCH_INFO_CLASS:
11310 case MONO_PATCH_INFO_IID:
11311 case MONO_PATCH_INFO_ADJUSTED_IID:
11312 case MONO_PATCH_INFO_CLASS_INIT:
11313 case MONO_PATCH_INFO_METHODCONST:
11314 case MONO_PATCH_INFO_METHOD:
11315 case MONO_PATCH_INFO_METHOD_JUMP:
11316 case MONO_PATCH_INFO_IMAGE:
11317 case MONO_PATCH_INFO_INTERNAL_METHOD:
11318 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
11319 case MONO_PATCH_INFO_FIELD:
11320 case MONO_PATCH_INFO_SFLDA:
11321 return (ji->type << 8) | (gssize)ji->data.target;
11323 return (ji->type << 8);
11328 * mono_patch_info_equal:
11330 * This might fail to recognize equivalent patches, i.e. floats, so its only
11331 * usable in those cases where this is not a problem, i.e. sharing GOT slots
11335 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
11337 const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
11338 const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
11340 if (ji1->type != ji2->type)
11343 switch (ji1->type) {
11344 case MONO_PATCH_INFO_RVA:
11345 case MONO_PATCH_INFO_LDSTR:
11346 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11347 case MONO_PATCH_INFO_LDTOKEN:
11348 case MONO_PATCH_INFO_DECLSEC:
11349 if ((ji1->data.token->image != ji2->data.token->image) ||
11350 (ji1->data.token->token != ji2->data.token->token))
11354 if (ji1->data.target != ji2->data.target)
11363 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
11365 unsigned char *ip = patch_info->ip.i + code;
11366 gconstpointer target = NULL;
11368 switch (patch_info->type) {
11369 case MONO_PATCH_INFO_BB:
11370 g_assert (patch_info->data.bb->native_offset);
11371 target = patch_info->data.bb->native_offset + code;
11373 case MONO_PATCH_INFO_ABS:
11374 target = patch_info->data.target;
11376 case MONO_PATCH_INFO_LABEL:
11377 target = patch_info->data.inst->inst_c0 + code;
11379 case MONO_PATCH_INFO_IP:
11382 case MONO_PATCH_INFO_METHOD_REL:
11383 target = code + patch_info->data.offset;
11385 case MONO_PATCH_INFO_INTERNAL_METHOD: {
11386 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11388 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
11389 g_assert_not_reached ();
11391 target = mono_icall_get_wrapper (mi);
11394 case MONO_PATCH_INFO_METHOD_JUMP:
11395 target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
11397 case MONO_PATCH_INFO_METHOD:
11398 if (patch_info->data.method == method) {
11401 /* get the trampoline to the method from the domain */
11402 if (method && method->wrapper_type == MONO_WRAPPER_STATIC_RGCTX_INVOKE) {
11403 target = mono_create_jit_trampoline_in_domain (mono_domain_get (),
11404 patch_info->data.method);
11406 target = mono_create_jit_trampoline (patch_info->data.method);
11410 case MONO_PATCH_INFO_SWITCH: {
11411 gpointer *jump_table;
11414 if (method && method->dynamic) {
11415 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11417 mono_domain_lock (domain);
11419 jump_table = mono_mempool_alloc (domain->mp, sizeof (gpointer) * patch_info->data.table->table_size);
11421 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11422 mono_domain_unlock (domain);
11425 for (i = 0; i < patch_info->data.table->table_size; i++)
11426 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
11427 target = jump_table;
11430 case MONO_PATCH_INFO_METHODCONST:
11431 case MONO_PATCH_INFO_CLASS:
11432 case MONO_PATCH_INFO_IMAGE:
11433 case MONO_PATCH_INFO_FIELD:
11434 target = patch_info->data.target;
11436 case MONO_PATCH_INFO_IID:
11437 mono_class_init (patch_info->data.klass);
11438 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
11440 case MONO_PATCH_INFO_ADJUSTED_IID:
11441 mono_class_init (patch_info->data.klass);
11442 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
11444 case MONO_PATCH_INFO_VTABLE:
11445 target = mono_class_vtable (domain, patch_info->data.klass);
11448 case MONO_PATCH_INFO_CLASS_INIT: {
11449 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.klass);
11452 target = mono_create_class_init_trampoline (vtable);
11455 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
11456 target = mono_create_delegate_trampoline (patch_info->data.klass);
11458 case MONO_PATCH_INFO_SFLDA: {
11459 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
11462 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
11463 /* Done by the generated code */
11467 mono_runtime_class_init (vtable);
11469 target = (char*)vtable->data + patch_info->data.field->offset;
11472 case MONO_PATCH_INFO_RVA:
11473 target = mono_image_rva_map (patch_info->data.token->image, patch_info->data.token->token);
11475 case MONO_PATCH_INFO_R4:
11476 case MONO_PATCH_INFO_R8:
11477 target = patch_info->data.target;
11479 case MONO_PATCH_INFO_EXC_NAME:
11480 target = patch_info->data.name;
11482 case MONO_PATCH_INFO_LDSTR:
11484 mono_ldstr (domain, patch_info->data.token->image,
11485 mono_metadata_token_index (patch_info->data.token->token));
11487 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
11489 MonoClass *handle_class;
11491 handle = mono_ldtoken (patch_info->data.token->image,
11492 patch_info->data.token->token, &handle_class, NULL);
11493 mono_class_init (handle_class);
11494 mono_class_init (mono_class_from_mono_type (handle));
11497 mono_type_get_object (domain, handle);
11500 case MONO_PATCH_INFO_LDTOKEN: {
11502 MonoClass *handle_class;
11504 handle = mono_ldtoken (patch_info->data.token->image,
11505 patch_info->data.token->token, &handle_class, NULL);
11506 mono_class_init (handle_class);
11511 case MONO_PATCH_INFO_DECLSEC:
11512 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
11514 case MONO_PATCH_INFO_ICALL_ADDR:
11515 target = mono_lookup_internal_call (patch_info->data.method);
11516 /* run_cctors == 0 -> AOT */
11517 if (!target && run_cctors)
11518 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info->data.method, TRUE));
11520 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
11521 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11523 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info->data.name);
11524 g_assert_not_reached ();
11529 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
11530 target = mono_thread_interruption_request_flag ();
11532 case MONO_PATCH_INFO_METHOD_RGCTX:
11533 target = mono_method_lookup_rgctx (mono_class_vtable (domain, patch_info->data.method->klass), mini_method_get_context (patch_info->data.method)->method_inst);
11535 case MONO_PATCH_INFO_BB_OVF:
11536 case MONO_PATCH_INFO_EXC_OVF:
11537 case MONO_PATCH_INFO_GOT_OFFSET:
11538 case MONO_PATCH_INFO_NONE:
11540 case MONO_PATCH_INFO_RGCTX_FETCH: {
11541 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
11544 switch (entry->data->type) {
11545 case MONO_PATCH_INFO_CLASS:
11546 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));
11548 case MONO_PATCH_INFO_METHOD:
11549 case MONO_PATCH_INFO_METHODCONST:
11550 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));
11552 case MONO_PATCH_INFO_FIELD:
11553 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));
11556 g_assert_not_reached ();
11560 target = mono_create_rgctx_lazy_fetch_trampoline (slot);
11563 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
11564 target = mono_create_generic_class_init_trampoline ();
11567 g_assert_not_reached ();
11570 return (gpointer)target;
11574 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
11575 MonoJitICallInfo *info;
11577 decompose_foreach (tree, cfg);
11579 switch (mono_burg_arity [tree->opcode]) {
11582 dec_foreach (tree->inst_left, cfg);
11584 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11585 MonoInst *iargs [2];
11587 iargs [0] = tree->inst_left;
11589 mono_emulate_opcode (cfg, tree, iargs, info);
11595 #ifdef MONO_ARCH_BIGMUL_INTRINS
11596 if (tree->opcode == OP_LMUL
11597 && (cfg->opt & MONO_OPT_INTRINS)
11598 && (tree->inst_left->opcode == CEE_CONV_I8
11599 || tree->inst_left->opcode == CEE_CONV_U8)
11600 && tree->inst_left->inst_left->type == STACK_I4
11601 && (tree->inst_right->opcode == CEE_CONV_I8
11602 || tree->inst_right->opcode == CEE_CONV_U8)
11603 && tree->inst_right->inst_left->type == STACK_I4
11604 && tree->inst_left->opcode == tree->inst_right->opcode) {
11605 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
11606 tree->inst_left = tree->inst_left->inst_left;
11607 tree->inst_right = tree->inst_right->inst_left;
11608 dec_foreach (tree, cfg);
11611 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11612 MonoInst *iargs [2];
11614 iargs [0] = tree->inst_i0;
11615 iargs [1] = tree->inst_i1;
11617 mono_emulate_opcode (cfg, tree, iargs, info);
11619 dec_foreach (iargs [0], cfg);
11620 dec_foreach (iargs [1], cfg);
11623 dec_foreach (tree->inst_left, cfg);
11624 dec_foreach (tree->inst_right, cfg);
11628 g_assert_not_reached ();
11633 decompose_pass (MonoCompile *cfg) {
11634 MonoBasicBlock *bb;
11636 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11639 cfg->prev_ins = NULL;
11640 MONO_BB_FOR_EACH_INS (cfg->cbb, tree) {
11641 dec_foreach (tree, cfg);
11642 cfg->prev_ins = tree;
11648 mono_compile_create_vars (MonoCompile *cfg)
11650 MonoMethodSignature *sig;
11651 MonoMethodHeader *header;
11654 header = mono_method_get_header (cfg->method);
11656 sig = mono_method_signature (cfg->method);
11658 if (!MONO_TYPE_IS_VOID (sig->ret)) {
11660 cfg->ret = mono_compile_create_var (cfg, sig->ret, OP_ARG);
11661 /* Inhibit optimizations */
11662 cfg->ret->flags |= MONO_INST_VOLATILE;
11664 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
11665 cfg->ret->opcode = OP_RETARG;
11666 cfg->ret->inst_vtype = sig->ret;
11667 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
11670 if (cfg->verbose_level > 2)
11671 g_print ("creating vars\n");
11673 cfg->args = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + sig->hasthis) * sizeof (MonoInst*));
11676 cfg->args [0] = mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
11678 for (i = 0; i < sig->param_count; ++i) {
11679 cfg->args [i + sig->hasthis] = mono_compile_create_var (cfg, sig->params [i], OP_ARG);
11680 if (sig->params [i]->byref) {
11681 if (!cfg->new_ir) cfg->disable_ssa = TRUE;
11685 if (cfg->new_ir && cfg->verbose_level > 2) {
11687 printf ("\treturn : ");
11688 mono_print_ins (cfg->ret);
11691 if (sig->hasthis) {
11692 printf ("\tthis: ");
11693 mono_print_ins (cfg->args [0]);
11696 for (i = 0; i < sig->param_count; ++i) {
11697 printf ("\targ [%d]: ", i);
11698 mono_print_ins (cfg->args [i + sig->hasthis]);
11702 cfg->locals_start = cfg->num_varinfo;
11703 cfg->locals = mono_mempool_alloc0 (cfg->mempool, header->num_locals * sizeof (MonoInst*));
11705 if (cfg->verbose_level > 2)
11706 g_print ("creating locals\n");
11708 for (i = 0; i < header->num_locals; ++i)
11709 cfg->locals [i] = mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
11711 if (cfg->verbose_level > 2)
11712 g_print ("locals done\n");
11714 mono_arch_create_vars (cfg);
11718 mono_print_code (MonoCompile *cfg, const char* msg)
11720 MonoBasicBlock *bb;
11722 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11723 MonoInst *tree = bb->code;
11726 mono_print_bb (bb, msg);
11731 g_print ("%s CODE BLOCK %d (nesting %d):\n", msg, bb->block_num, bb->nesting);
11733 MONO_BB_FOR_EACH_INS (bb, tree) {
11734 mono_print_tree (tree);
11741 #ifndef DISABLE_JIT
11743 extern const char * const mono_burg_rule_string [];
11746 emit_state (MonoCompile *cfg, MBState *state, int goal)
11748 MBState *kids [10];
11749 int ern = mono_burg_rule (state, goal);
11750 const guint16 *nts = mono_burg_nts_data + mono_burg_nts [ern];
11752 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
11756 // state->reg1 = state->reg2; /* chain rule */
11758 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11761 state->reg1 = mono_regstate_next_int (cfg->rs);
11762 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
11764 case MB_NTERM_lreg:
11765 state->reg1 = mono_regstate_next_int (cfg->rs);
11766 state->reg2 = mono_regstate_next_int (cfg->rs);
11768 case MB_NTERM_freg:
11769 #ifdef MONO_ARCH_SOFT_FLOAT
11770 state->reg1 = mono_regstate_next_int (cfg->rs);
11771 state->reg2 = mono_regstate_next_int (cfg->rs);
11773 state->reg1 = mono_regstate_next_float (cfg->rs);
11777 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11779 * Enabling this might cause bugs to surface in the local register
11780 * allocators on some architectures like x86.
11782 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
11783 /* Do not optimize away reg-reg moves */
11784 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
11785 state->right->reg1 = state->left->tree->dreg;
11794 mono_burg_kids (state, ern, kids);
11796 emit_state (cfg, kids [0], nts [0]);
11798 emit_state (cfg, kids [1], nts [1]);
11800 emit_state (cfg, kids [2], nts [2]);
11802 g_assert (!nts [4]);
11803 emit_state (cfg, kids [3], nts [3]);
11809 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
11810 mono_burg_emit (ern, state, state->tree, cfg);
11813 #define DEBUG_SELECTION
11816 mini_select_instructions (MonoCompile *cfg)
11818 MonoBasicBlock *bb;
11820 cfg->state_pool = mono_mempool_new ();
11821 cfg->rs = mono_regstate_new ();
11823 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11824 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
11825 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
11827 /* we are careful when inverting, since bugs like #59580
11828 * could show up when dealing with NaNs.
11830 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
11831 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
11832 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
11833 bb->last_ins->inst_false_bb = tmp;
11835 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
11839 MONO_INST_NEW (cfg, ins, OP_BR);
11840 ins->inst_target_bb = bb->last_ins->inst_false_bb;
11841 MONO_ADD_INS (bb, ins);
11846 #ifdef DEBUG_SELECTION
11847 if (cfg->verbose_level >= 4) {
11848 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11850 g_print ("DUMP BLOCK %d:\n", bb->block_num);
11851 MONO_BB_FOR_EACH_INS (bb, tree) {
11852 mono_print_tree (tree);
11859 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11860 MonoInst *tree = bb->code, *next;
11866 bb->last_ins = NULL;
11869 mono_regstate_reset (cfg->rs);
11871 #ifdef DEBUG_SELECTION
11872 if (cfg->verbose_level >= 3)
11873 g_print ("LABEL BLOCK %d:\n", bb->block_num);
11875 for (; tree; tree = next) {
11877 #ifdef DEBUG_SELECTION
11878 if (cfg->verbose_level >= 3) {
11879 mono_print_tree (tree);
11884 cfg->ip = tree->cil_code;
11885 if (!(mbstate = mono_burg_label (tree, cfg))) {
11886 g_warning ("unable to label tree %p", tree);
11887 mono_print_tree (tree);
11889 g_assert_not_reached ();
11891 emit_state (cfg, mbstate, MB_NTERM_stmt);
11893 bb->max_vreg = cfg->rs->next_vreg;
11896 bb->last_ins->next = NULL;
11898 mono_mempool_empty (cfg->state_pool);
11900 mono_mempool_destroy (cfg->state_pool);
11906 * mono_normalize_opcodes:
11908 * Replace CEE_ and OP_ opcodes with the corresponding OP_I or OP_L opcodes.
11911 static gint16 *remap_table;
11913 #if SIZEOF_VOID_P == 8
11914 #define REMAP_OPCODE(opcode) OP_L ## opcode
11916 #define REMAP_OPCODE(opcode) OP_I ## opcode
11919 static G_GNUC_UNUSED void
11920 mono_normalize_opcodes (MonoCompile *cfg, MonoBasicBlock *bb)
11924 if (!remap_table) {
11925 remap_table = g_new0 (gint16, OP_LAST);
11927 #if SIZEOF_VOID_P == 8
11928 remap_table [CEE_CONV_U8] = OP_ZEXT_I4;
11929 remap_table [CEE_CONV_U] = OP_ZEXT_I4;
11930 remap_table [CEE_CONV_I8] = OP_SEXT_I4;
11931 remap_table [CEE_CONV_I] = OP_SEXT_I4;
11932 remap_table [CEE_CONV_OVF_U4] = OP_LCONV_TO_OVF_U4;
11933 remap_table [CEE_CONV_OVF_I4_UN] = OP_LCONV_TO_OVF_I4_UN;
11936 remap_table [CEE_CONV_R4] = OP_ICONV_TO_R4;
11937 remap_table [CEE_CONV_R8] = OP_ICONV_TO_R8;
11938 remap_table [CEE_CONV_I4] = OP_MOVE;
11939 remap_table [CEE_CONV_U4] = OP_MOVE;
11940 remap_table [CEE_CONV_I1] = REMAP_OPCODE (CONV_TO_I1);
11941 remap_table [CEE_CONV_I2] = REMAP_OPCODE (CONV_TO_I2);
11942 remap_table [CEE_CONV_U1] = REMAP_OPCODE (CONV_TO_U1);
11943 remap_table [CEE_CONV_U2] = REMAP_OPCODE (CONV_TO_U2);
11944 remap_table [CEE_CONV_R_UN] = REMAP_OPCODE (CONV_TO_R_UN);
11945 remap_table [CEE_ADD] = REMAP_OPCODE (ADD);
11946 remap_table [CEE_SUB] = REMAP_OPCODE (SUB);
11947 remap_table [CEE_MUL] = REMAP_OPCODE (MUL);
11948 remap_table [CEE_DIV] = REMAP_OPCODE (DIV);
11949 remap_table [CEE_REM] = REMAP_OPCODE (REM);
11950 remap_table [CEE_DIV_UN] = REMAP_OPCODE (DIV_UN);
11951 remap_table [CEE_REM_UN] = REMAP_OPCODE (REM_UN);
11952 remap_table [CEE_AND] = REMAP_OPCODE (AND);
11953 remap_table [CEE_OR] = REMAP_OPCODE (OR);
11954 remap_table [CEE_XOR] = REMAP_OPCODE (XOR);
11955 remap_table [CEE_SHL] = REMAP_OPCODE (SHL);
11956 remap_table [CEE_SHR] = REMAP_OPCODE (SHR);
11957 remap_table [CEE_SHR_UN] = REMAP_OPCODE (SHR_UN);
11958 remap_table [CEE_NOT] = REMAP_OPCODE (NOT);
11959 remap_table [CEE_NEG] = REMAP_OPCODE (NEG);
11960 remap_table [CEE_CALL] = OP_CALL;
11961 remap_table [CEE_BEQ] = REMAP_OPCODE (BEQ);
11962 remap_table [CEE_BNE_UN] = REMAP_OPCODE (BNE_UN);
11963 remap_table [CEE_BLT] = REMAP_OPCODE (BLT);
11964 remap_table [CEE_BLT_UN] = REMAP_OPCODE (BLT_UN);
11965 remap_table [CEE_BGT] = REMAP_OPCODE (BGT);
11966 remap_table [CEE_BGT_UN] = REMAP_OPCODE (BGT_UN);
11967 remap_table [CEE_BGE] = REMAP_OPCODE (BGE);
11968 remap_table [CEE_BGE_UN] = REMAP_OPCODE (BGE_UN);
11969 remap_table [CEE_BLE] = REMAP_OPCODE (BLE);
11970 remap_table [CEE_BLE_UN] = REMAP_OPCODE (BLE_UN);
11971 remap_table [CEE_ADD_OVF] = REMAP_OPCODE (ADD_OVF);
11972 remap_table [CEE_ADD_OVF_UN] = REMAP_OPCODE (ADD_OVF_UN);
11973 remap_table [CEE_SUB_OVF] = REMAP_OPCODE (SUB_OVF);
11974 remap_table [CEE_SUB_OVF_UN] = REMAP_OPCODE (SUB_OVF_UN);
11975 remap_table [CEE_MUL_OVF] = REMAP_OPCODE (MUL_OVF);
11976 remap_table [CEE_MUL_OVF_UN] = REMAP_OPCODE (MUL_OVF_UN);
11979 MONO_BB_FOR_EACH_INS (bb, ins) {
11980 int remapped = remap_table [ins->opcode];
11982 ins->opcode = remapped;
11987 mono_codegen (MonoCompile *cfg)
11989 MonoJumpInfo *patch_info;
11990 MonoBasicBlock *bb;
11991 int i, max_epilog_size;
11994 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11995 cfg->spill_count = 0;
11996 /* we reuse dfn here */
11997 /* bb->dfn = bb_count++; */
11998 #ifdef MONO_ARCH_ENABLE_NORMALIZE_OPCODES
12000 mono_normalize_opcodes (cfg, bb);
12003 mono_arch_lowering_pass (cfg, bb);
12005 if (cfg->opt & MONO_OPT_PEEPHOLE)
12006 mono_arch_peephole_pass_1 (cfg, bb);
12008 if (!cfg->globalra)
12009 mono_local_regalloc (cfg, bb);
12011 if (cfg->opt & MONO_OPT_PEEPHOLE)
12012 mono_arch_peephole_pass_2 (cfg, bb);
12015 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
12016 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
12018 code = mono_arch_emit_prolog (cfg);
12020 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
12021 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
12023 cfg->code_len = code - cfg->native_code;
12024 cfg->prolog_end = cfg->code_len;
12026 mono_debug_open_method (cfg);
12028 /* emit code all basic blocks */
12029 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12030 bb->native_offset = cfg->code_len;
12031 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
12032 mono_arch_output_basic_block (cfg, bb);
12034 if (bb == cfg->bb_exit) {
12035 cfg->epilog_begin = cfg->code_len;
12037 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
12038 code = cfg->native_code + cfg->code_len;
12039 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
12040 cfg->code_len = code - cfg->native_code;
12041 g_assert (cfg->code_len < cfg->code_size);
12044 mono_arch_emit_epilog (cfg);
12048 mono_arch_emit_exceptions (cfg);
12050 max_epilog_size = 0;
12052 code = cfg->native_code + cfg->code_len;
12054 /* we always allocate code in cfg->domain->code_mp to increase locality */
12055 cfg->code_size = cfg->code_len + max_epilog_size;
12056 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
12058 if (cfg->method->dynamic) {
12059 guint unwindlen = 0;
12060 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12061 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12063 /* Allocate the code into a separate memory pool so it can be freed */
12064 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
12065 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
12066 mono_domain_lock (cfg->domain);
12067 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
12068 mono_domain_unlock (cfg->domain);
12070 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size + unwindlen);
12072 guint unwindlen = 0;
12073 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12074 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12076 mono_domain_lock (cfg->domain);
12077 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size + unwindlen);
12078 mono_domain_unlock (cfg->domain);
12081 memcpy (code, cfg->native_code, cfg->code_len);
12082 g_free (cfg->native_code);
12083 cfg->native_code = code;
12084 code = cfg->native_code + cfg->code_len;
12086 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
12087 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
12088 switch (patch_info->type) {
12089 case MONO_PATCH_INFO_ABS: {
12090 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
12093 * Change patches of type MONO_PATCH_INFO_ABS into patches describing the
12094 * absolute address.
12097 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
12098 // FIXME: CLEAN UP THIS MESS.
12099 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
12100 strstr (cfg->method->name, info->name)) {
12102 * This is an icall wrapper, and this is a call to the
12103 * wrapped function.
12105 if (cfg->compile_aot) {
12106 patch_info->type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
12107 patch_info->data.name = info->name;
12110 /* for these array methods we currently register the same function pointer
12111 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
12112 * will return the incorrect one depending on the order they are registered.
12113 * See tests/test-arr.cs
12115 if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
12116 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
12117 patch_info->data.name = info->name;
12122 if (patch_info->type == MONO_PATCH_INFO_ABS && !cfg->new_ir) {
12123 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
12125 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
12126 patch_info->data.klass = vtable->klass;
12130 if (patch_info->type == MONO_PATCH_INFO_ABS) {
12131 MonoClass *klass = mono_find_delegate_trampoline_by_addr (patch_info->data.target);
12133 patch_info->type = MONO_PATCH_INFO_DELEGATE_TRAMPOLINE;
12134 patch_info->data.klass = klass;
12138 if (patch_info->type == MONO_PATCH_INFO_ABS) {
12139 if (cfg->abs_patches) {
12140 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, patch_info->data.target);
12142 patch_info->type = abs_ji->type;
12143 patch_info->data.target = abs_ji->data.target;
12150 case MONO_PATCH_INFO_SWITCH: {
12152 if (cfg->method->dynamic) {
12153 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12155 mono_domain_lock (cfg->domain);
12156 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12157 mono_domain_unlock (cfg->domain);
12160 if (!cfg->compile_aot && !cfg->new_ir)
12161 /* In the aot case, the patch already points to the correct location */
12162 patch_info->ip.i = patch_info->ip.label->inst_c0;
12163 for (i = 0; i < patch_info->data.table->table_size; i++) {
12164 /* Might be NULL if the switch is eliminated */
12165 if (patch_info->data.table->table [i]) {
12166 g_assert (patch_info->data.table->table [i]->native_offset);
12167 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
12172 patch_info->data.table->table = (MonoBasicBlock**)table;
12175 case MONO_PATCH_INFO_METHOD_JUMP: {
12177 MonoDomain *domain = cfg->domain;
12178 unsigned char *ip = cfg->native_code + patch_info->ip.i;
12180 mono_domain_lock (domain);
12181 if (!domain->jump_target_hash)
12182 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
12183 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
12184 list = g_slist_prepend (list, ip);
12185 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
12186 mono_domain_unlock (domain);
12195 #ifdef VALGRIND_JIT_REGISTER_MAP
12196 if (valgrind_register){
12197 char* nm = mono_method_full_name (cfg->method, TRUE);
12198 VALGRIND_JIT_REGISTER_MAP (nm, cfg->native_code, cfg->native_code + cfg->code_len);
12203 if (cfg->verbose_level > 0) {
12204 char* nm = mono_method_full_name (cfg->method, TRUE);
12205 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
12207 cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
12212 gboolean is_generic = FALSE;
12214 if (cfg->method->is_inflated || mono_method_get_generic_container (cfg->method) ||
12215 cfg->method->klass->generic_container || cfg->method->klass->generic_class) {
12219 if (cfg->generic_sharing_context)
12220 g_assert (is_generic);
12223 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
12224 mono_arch_save_unwind_info (cfg);
12227 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
12229 if (cfg->method->dynamic) {
12230 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12232 mono_domain_lock (cfg->domain);
12233 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12234 mono_domain_unlock (cfg->domain);
12237 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
12239 mono_debug_close_method (cfg);
12240 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12241 mono_arch_unwindinfo_install_unwind_info (&cfg->arch.unwindinfo, cfg->native_code, cfg->code_len);
12245 static MonoGenericInst*
12246 get_object_generic_inst (int type_argc)
12248 MonoType **type_argv;
12251 type_argv = alloca (sizeof (MonoType*) * type_argc);
12253 for (i = 0; i < type_argc; ++i)
12254 type_argv [i] = &mono_defaults.object_class->byval_arg;
12256 return mono_metadata_get_generic_inst (type_argc, type_argv);
12260 * mini_method_compile:
12261 * @method: the method to compile
12262 * @opts: the optimization flags to use
12263 * @domain: the domain where the method will be compiled in
12264 * @run_cctors: whether we should run type ctors if possible
12265 * @compile_aot: whether this is an AOT compilation
12266 * @parts: debug flag
12268 * Returns: a MonoCompile* pointer. Caller must check the exception_type
12269 * field in the returned struct to see if compilation succeded.
12272 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
12274 MonoMethodHeader *header;
12277 MonoJitInfo *jinfo;
12278 int dfn, i, code_size_ratio;
12279 gboolean deadce_has_run = FALSE;
12280 gboolean try_generic_shared;
12281 MonoMethod *method_to_compile, *method_to_register;
12282 int generic_info_size;
12284 mono_jit_stats.methods_compiled++;
12285 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
12286 mono_profiler_method_jit (method);
12287 if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
12288 MONO_PROBE_METHOD_COMPILE_BEGIN (method);
12291 /* We are passed the original generic method definition */
12292 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12293 (opts & MONO_OPT_GSHARED) && (method->is_generic || method->klass->generic_container);
12295 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12296 (opts & MONO_OPT_GSHARED) && mono_method_is_generic_sharable_impl (method, FALSE);
12298 if (opts & MONO_OPT_GSHARED) {
12299 if (try_generic_shared)
12300 mono_stats.generics_sharable_methods++;
12301 else if (mono_method_is_generic_impl (method))
12302 mono_stats.generics_unsharable_methods++;
12306 if (try_generic_shared) {
12307 MonoMethod *declaring_method;
12308 MonoGenericContext *shared_context;
12311 declaring_method = method;
12313 declaring_method = mono_method_get_declaring_generic_method (method);
12314 if (method->klass->generic_class)
12315 g_assert (method->klass->generic_class->container_class == declaring_method->klass);
12317 g_assert (method->klass == declaring_method->klass);
12320 if (declaring_method->is_generic)
12321 shared_context = &(mono_method_get_generic_container (declaring_method)->context);
12323 shared_context = &declaring_method->klass->generic_container->context;
12325 method_to_compile = mono_class_inflate_generic_method (declaring_method, shared_context);
12326 g_assert (method_to_compile);
12328 method_to_compile = method;
12331 cfg = g_new0 (MonoCompile, 1);
12332 cfg->method = method_to_compile;
12333 cfg->mempool = mono_mempool_new ();
12335 cfg->prof_options = mono_profiler_get_events ();
12336 cfg->run_cctors = run_cctors;
12337 cfg->domain = domain;
12338 cfg->verbose_level = mini_verbose;
12339 cfg->compile_aot = compile_aot;
12340 cfg->skip_visibility = method->skip_visibility;
12341 cfg->orig_method = method;
12342 if (try_generic_shared)
12343 cfg->generic_sharing_context = (MonoGenericSharingContext*)&cfg->generic_sharing_context;
12344 cfg->token_info_hash = g_hash_table_new (NULL, NULL);
12346 if (cfg->compile_aot && !try_generic_shared && (method->is_generic || method->klass->generic_container)) {
12347 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED;
12351 /* The debugger has no liveness information, so avoid sharing registers/stack slots */
12352 if (mono_debug_using_mono_debugger () || debug_options.mdb_optimizations) {
12353 cfg->disable_reuse_registers = TRUE;
12354 cfg->disable_reuse_stack_slots = TRUE;
12356 * This decreases the change the debugger will read registers/stack slots which are
12357 * not yet initialized.
12359 cfg->disable_initlocals_opt = TRUE;
12361 /* Temporarily disable this when running in the debugger until we have support
12362 * for this in the debugger. */
12363 cfg->disable_omit_fp = TRUE;
12365 // cfg->opt |= MONO_OPT_SHARED;
12366 cfg->opt &= ~MONO_OPT_INLINE;
12367 cfg->opt &= ~MONO_OPT_COPYPROP;
12368 cfg->opt &= ~MONO_OPT_CONSPROP;
12369 cfg->opt &= ~MONO_OPT_GSHARED;
12372 header = mono_method_get_header (method_to_compile);
12374 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
12375 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
12376 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12377 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12378 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12379 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12383 if (getenv ("MONO_VERBOSE_METHOD")) {
12384 if (strcmp (cfg->method->name, getenv ("MONO_VERBOSE_METHOD")) == 0)
12385 cfg->verbose_level = 4;
12388 ip = (guint8 *)header->code;
12390 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
12392 if (cfg->verbose_level > 2) {
12393 if (cfg->generic_sharing_context)
12394 g_print ("converting shared method %s\n", mono_method_full_name (method, TRUE));
12396 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
12399 if (cfg->opt & (MONO_OPT_ABCREM | MONO_OPT_SSAPRE))
12400 cfg->opt |= MONO_OPT_SSA;
12403 static int count = 0;
12407 if (getenv ("MONO_COUNT")) {
12408 if (count == atoi (getenv ("MONO_COUNT"))) {
12409 printf ("LAST: %s\n", mono_method_full_name (method, TRUE));
12410 //cfg->verbose_level = 5;
12412 if (count <= atoi (getenv ("MONO_COUNT")))
12413 cfg->new_ir = TRUE;
12416 * Passing/returning vtypes in registers in managed methods is an ABI change
12417 * from the old JIT.
12419 disable_vtypes_in_regs = TRUE;
12422 cfg->new_ir = TRUE;
12426 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")))
12427 cfg->globalra = TRUE;
12430 //cfg->globalra = TRUE;
12432 //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
12433 // cfg->globalra = TRUE;
12436 static int count = 0;
12439 if (getenv ("COUNT2")) {
12440 if (count == atoi (getenv ("COUNT2")))
12441 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
12442 if (count > atoi (getenv ("COUNT2")))
12443 cfg->globalra = FALSE;
12447 if (header->clauses)
12448 cfg->globalra = FALSE;
12450 if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
12451 /* The code in the prolog clobbers caller saved registers */
12452 cfg->globalra = FALSE;
12454 // FIXME: Disable globalra in case of tracing/profiling
12456 if (cfg->method->save_lmf)
12457 /* The LMF saving code might clobber caller saved registers */
12458 cfg->globalra = FALSE;
12461 if (!strcmp (cfg->method->name, "CompareInternal"))
12462 cfg->globalra = FALSE;
12465 if (strstr (cfg->method->name, "LoadData"))
12466 cfg->new_ir = FALSE;
12470 cfg->rs = mono_regstate_new ();
12471 cfg->next_vreg = cfg->rs->next_vreg;
12474 /* FIXME: Fix SSA to handle branches inside bblocks */
12475 if (cfg->opt & MONO_OPT_SSA)
12476 cfg->enable_extended_bblocks = FALSE;
12479 * FIXME: This confuses liveness analysis because variables which are assigned after
12480 * a branch inside a bblock become part of the kill set, even though the assignment
12481 * might not get executed. This causes the optimize_initlocals pass to delete some
12482 * assignments which are needed.
12483 * Also, the mono_if_conversion pass needs to be modified to recognize the code
12486 //cfg->enable_extended_bblocks = TRUE;
12489 * create MonoInst* which represents arguments and local variables
12491 mono_compile_create_vars (cfg);
12494 /* SSAPRE is not supported on linear IR */
12495 cfg->opt &= ~MONO_OPT_SSAPRE;
12497 i = mono_method_to_ir2 (cfg, method_to_compile, NULL, NULL, NULL, NULL, NULL, 0, FALSE);
12500 i = mono_method_to_ir (cfg, method_to_compile, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE);
12504 if (try_generic_shared && cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
12506 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12507 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12510 mono_destroy_compile (cfg);
12511 try_generic_shared = FALSE;
12512 goto restart_compile;
12514 g_assert (cfg->exception_type != MONO_EXCEPTION_GENERIC_SHARING_FAILED);
12516 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12517 MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12518 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12519 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12520 /* cfg contains the details of the failure, so let the caller cleanup */
12524 mono_jit_stats.basic_blocks += cfg->num_bblocks;
12525 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
12527 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
12530 mono_decompose_long_opts (cfg);
12532 /* Should be done before branch opts */
12533 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
12534 mono_local_cprop2 (cfg);
12537 if (cfg->opt & MONO_OPT_BRANCH)
12538 mono_optimize_branches (cfg);
12541 /* This must be done _before_ global reg alloc and _after_ decompose */
12542 mono_handle_global_vregs (cfg);
12543 mono_local_deadce (cfg);
12544 mono_if_conversion (cfg);
12547 if ((cfg->opt & MONO_OPT_SSAPRE) || cfg->globalra)
12548 mono_remove_critical_edges (cfg);
12550 /* Depth-first ordering on basic blocks */
12551 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12554 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12555 if (cfg->num_bblocks != dfn + 1) {
12556 MonoBasicBlock *bb;
12558 cfg->num_bblocks = dfn + 1;
12560 /* remove unreachable code, because the code in them may be
12561 * inconsistent (access to dead variables for example) */
12562 for (bb = cfg->bb_entry; bb;) {
12563 MonoBasicBlock *bbn = bb->next_bb;
12566 * FIXME: Can't use the second case in methods with clauses, since the
12567 * bblocks inside the clauses are not processed during dfn computation.
12569 if (((header->clauses && (bbn && bbn->region == -1 && bbn->in_count == 0)) ||
12570 (!header->clauses && (bbn && bbn->region == -1 && !bbn->dfn))) &&
12571 bbn != cfg->bb_exit) {
12572 if (cfg->verbose_level > 1)
12573 g_print ("found unreachable code in BB%d\n", bbn->block_num);
12574 /* There may exist unreachable branches to this bb */
12575 bb->next_bb = bbn->next_bb;
12576 mono_nullify_basic_block (bbn);
12583 if (((cfg->num_varinfo > 2000) || (cfg->num_bblocks > 1000)) && !cfg->compile_aot) {
12585 * we disable some optimizations if there are too many variables
12586 * because JIT time may become too expensive. The actual number needs
12587 * to be tweaked and eventually the non-linear algorithms should be fixed.
12589 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
12590 cfg->disable_ssa = TRUE;
12593 if (cfg->opt & MONO_OPT_LOOP) {
12594 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
12595 mono_compute_natural_loops (cfg);
12598 /* after method_to_ir */
12600 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12601 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12605 //#define DEBUGSSA "logic_run"
12606 #define DEBUGSSA_CLASS "Tests"
12609 if (!header->num_clauses && !cfg->disable_ssa) {
12610 mono_local_cprop (cfg);
12612 #ifndef DISABLE_SSA
12614 mono_ssa_compute2 (cfg);
12616 mono_ssa_compute (cfg);
12620 if (cfg->opt & MONO_OPT_SSA) {
12621 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
12622 #ifndef DISABLE_SSA
12624 mono_local_cprop (cfg);
12626 mono_ssa_compute2 (cfg);
12628 mono_ssa_compute (cfg);
12631 if (cfg->verbose_level >= 2) {
12638 /* after SSA translation */
12640 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12641 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12645 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
12646 if (cfg->comp_done & MONO_COMP_SSA) {
12647 #ifndef DISABLE_SSA
12649 mono_ssa_cprop2 (cfg);
12651 mono_ssa_cprop (cfg);
12655 mono_local_cprop (cfg);
12659 #ifndef DISABLE_SSA
12660 if (cfg->comp_done & MONO_COMP_SSA) {
12661 //mono_ssa_strength_reduction (cfg);
12663 if (cfg->opt & MONO_OPT_SSAPRE) {
12664 mono_perform_ssapre (cfg);
12665 //mono_local_cprop (cfg);
12668 if (cfg->opt & MONO_OPT_DEADCE) {
12670 mono_ssa_deadce2 (cfg);
12672 mono_ssa_deadce (cfg);
12673 deadce_has_run = TRUE;
12677 if ((cfg->flags & (MONO_CFG_HAS_LDELEMA|MONO_CFG_HAS_CHECK_THIS)) && (cfg->opt & MONO_OPT_ABCREM))
12678 mono_perform_abc_removal2 (cfg);
12680 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
12681 mono_perform_abc_removal (cfg);
12685 mono_ssa_remove2 (cfg);
12686 mono_local_cprop2 (cfg);
12687 mono_handle_global_vregs (cfg);
12688 mono_local_deadce (cfg);
12691 mono_ssa_remove (cfg);
12693 if (cfg->opt & MONO_OPT_BRANCH) {
12694 MonoBasicBlock *bb;
12696 mono_optimize_branches (cfg);
12698 /* Have to recompute cfg->bblocks and bb->dfn */
12699 if (cfg->globalra) {
12700 mono_remove_critical_edges (cfg);
12702 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12705 /* Depth-first ordering on basic blocks */
12706 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12709 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12710 cfg->num_bblocks = dfn + 1;
12716 /* after SSA removal */
12718 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12719 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12724 #ifdef MONO_ARCH_SOFT_FLOAT
12725 mono_handle_soft_float (cfg);
12727 mono_decompose_vtype_opts (cfg);
12728 if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
12729 mono_decompose_array_access_opts (cfg);
12732 if (!cfg->new_ir) {
12733 if (cfg->verbose_level > 4)
12734 mono_print_code (cfg, "BEFORE DECOMPOSE");
12736 decompose_pass (cfg);
12739 if (cfg->got_var) {
12742 g_assert (cfg->got_var_allocated);
12745 * Allways allocate the GOT var to a register, because keeping it
12746 * in memory will increase the number of live temporaries in some
12747 * code created by inssel.brg, leading to the well known spills+
12748 * branches problem. Testcase: mcs crash in
12749 * System.MonoCustomAttrs:GetCustomAttributes.
12751 regs = mono_arch_get_global_int_regs (cfg);
12753 cfg->got_var->opcode = OP_REGVAR;
12754 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
12755 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
12757 g_list_free (regs);
12760 /* todo: remove code when we have verified that the liveness for try/catch blocks
12764 * Currently, this can't be commented out since exception blocks are not
12765 * processed during liveness analysis.
12767 mono_liveness_handle_exception_clauses (cfg);
12769 if (cfg->globalra) {
12770 MonoBasicBlock *bb;
12772 /* Have to do this before regalloc since it can create vregs */
12773 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12774 mono_arch_lowering_pass (cfg, bb);
12776 mono_global_regalloc (cfg);
12779 if ((cfg->opt & MONO_OPT_LINEARS) && !cfg->globalra) {
12780 GList *vars, *regs;
12782 /* For now, compute aliasing info only if needed for deadce... */
12783 if (!cfg->new_ir && (cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
12784 cfg->aliasing_info = mono_build_aliasing_information (cfg);
12787 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
12788 cfg->comp_done &= ~MONO_COMP_LIVENESS;
12789 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
12790 mono_analyze_liveness (cfg);
12792 if (cfg->aliasing_info != NULL) {
12793 mono_aliasing_deadce (cfg->aliasing_info);
12794 deadce_has_run = TRUE;
12797 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
12798 regs = mono_arch_get_global_int_regs (cfg);
12800 regs = g_list_delete_link (regs, regs);
12801 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
12804 if (cfg->aliasing_info != NULL) {
12805 mono_destroy_aliasing_information (cfg->aliasing_info);
12806 cfg->aliasing_info = NULL;
12810 //mono_print_code (cfg);
12814 /* variables are allocated after decompose, since decompose could create temps */
12815 if (!cfg->globalra)
12816 mono_arch_allocate_vars (cfg);
12818 if (!cfg->new_ir && cfg->opt & MONO_OPT_CFOLD)
12819 mono_constant_fold (cfg);
12822 MonoBasicBlock *bb;
12823 gboolean need_local_opts;
12825 if (!cfg->globalra) {
12826 mono_spill_global_vars (cfg, &need_local_opts);
12828 if (need_local_opts || cfg->compile_aot) {
12829 /* To optimize code created by spill_global_vars */
12830 mono_local_cprop2 (cfg);
12831 mono_local_deadce (cfg);
12835 /* Add branches between non-consecutive bblocks */
12836 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12837 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
12838 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
12839 /* we are careful when inverting, since bugs like #59580
12840 * could show up when dealing with NaNs.
12842 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
12843 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
12844 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
12845 bb->last_ins->inst_false_bb = tmp;
12847 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
12849 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
12850 inst->opcode = OP_BR;
12851 inst->inst_target_bb = bb->last_ins->inst_false_bb;
12852 mono_bblock_add_inst (bb, inst);
12857 if (cfg->verbose_level >= 4 && !cfg->globalra) {
12858 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12859 MonoInst *tree = bb->code;
12860 g_print ("DUMP BLOCK %d:\n", bb->block_num);
12863 for (; tree; tree = tree->next) {
12864 mono_print_ins_index (-1, tree);
12870 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12871 bb->max_vreg = cfg->next_vreg;
12875 mini_select_instructions (cfg);
12877 mono_codegen (cfg);
12878 if (cfg->verbose_level >= 2) {
12879 char *id = mono_method_full_name (cfg->method, FALSE);
12880 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
12884 if (cfg->generic_sharing_context)
12885 generic_info_size = sizeof (MonoGenericJitInfo);
12887 generic_info_size = 0;
12889 if (cfg->method->dynamic) {
12890 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12891 generic_info_size);
12893 /* we access cfg->domain->mp */
12894 mono_domain_lock (cfg->domain);
12895 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) +
12896 (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12897 generic_info_size);
12898 mono_domain_unlock (cfg->domain);
12901 if (cfg->generic_sharing_context) {
12902 MonoGenericContext object_context;
12904 g_assert (!method_to_compile->klass->generic_class);
12905 if (method_to_compile->klass->generic_container) {
12906 int type_argc = method_to_compile->klass->generic_container->type_argc;
12908 object_context.class_inst = get_object_generic_inst (type_argc);
12910 object_context.class_inst = NULL;
12913 if (mini_method_get_context (method_to_compile)->method_inst) {
12914 int type_argc = mini_method_get_context (method_to_compile)->method_inst->type_argc;
12916 object_context.method_inst = get_object_generic_inst (type_argc);
12918 object_context.method_inst = NULL;
12921 g_assert (object_context.class_inst || object_context.method_inst);
12923 method_to_register = mono_class_inflate_generic_method (method_to_compile, &object_context);
12925 g_assert (method == method_to_compile);
12926 method_to_register = method;
12929 jinfo->method = method_to_register;
12930 jinfo->code_start = cfg->native_code;
12931 jinfo->code_size = cfg->code_len;
12932 jinfo->used_regs = cfg->used_int_regs;
12933 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
12934 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
12935 jinfo->num_clauses = header->num_clauses;
12937 if (cfg->generic_sharing_context) {
12939 MonoGenericJitInfo *gi;
12941 jinfo->has_generic_jit_info = 1;
12943 gi = mono_jit_info_get_generic_jit_info (jinfo);
12946 gi->generic_sharing_context = cfg->generic_sharing_context;
12949 * Non-generic static methods only get a "this" info
12950 * if they use the rgctx variable (which they are
12951 * forced to if they have any open catch clauses).
12953 if (cfg->rgctx_var ||
12954 (!(method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) &&
12955 !mini_method_get_context (method_to_compile)->method_inst)) {
12958 if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
12959 mini_method_get_context (method_to_compile)->method_inst) {
12960 inst = cfg->rgctx_var;
12961 g_assert (inst->opcode == OP_REGOFFSET);
12963 inst = cfg->args [0];
12966 if (inst->opcode == OP_REGVAR) {
12967 gi->this_in_reg = 1;
12968 gi->this_reg = inst->dreg;
12970 //g_print ("this in reg %d\n", inst->dreg);
12972 g_assert (inst->opcode == OP_REGOFFSET);
12974 g_assert (inst->inst_basereg == X86_EBP);
12975 #elif defined(__x86_64__)
12976 g_assert (inst->inst_basereg == X86_EBP || inst->inst_basereg == X86_ESP);
12978 g_assert (inst->inst_offset >= G_MININT32 && inst->inst_offset <= G_MAXINT32);
12980 gi->this_in_reg = 0;
12981 gi->this_reg = inst->inst_basereg;
12982 gi->this_offset = inst->inst_offset;
12984 //g_print ("this at offset %d from reg %d\n", gi->this_offset, gi->this_reg);
12991 if (header->num_clauses) {
12994 for (i = 0; i < header->num_clauses; i++) {
12995 MonoExceptionClause *ec = &header->clauses [i];
12996 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
12997 MonoBasicBlock *tblock;
13000 ei->flags = ec->flags;
13002 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
13003 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
13005 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
13006 tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
13008 ei->data.filter = cfg->native_code + tblock->native_offset;
13010 ei->data.catch_class = ec->data.catch_class;
13013 tblock = cfg->cil_offset_to_bb [ec->try_offset];
13015 ei->try_start = cfg->native_code + tblock->native_offset;
13016 g_assert (tblock->native_offset);
13017 tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
13019 ei->try_end = cfg->native_code + tblock->native_offset;
13020 g_assert (tblock->native_offset);
13021 tblock = cfg->cil_offset_to_bb [ec->handler_offset];
13023 ei->handler_start = cfg->native_code + tblock->native_offset;
13027 cfg->jit_info = jinfo;
13028 #if defined(__arm__)
13029 mono_arch_fixup_jinfo (cfg);
13032 if (!cfg->compile_aot) {
13033 mono_domain_lock (cfg->domain);
13034 mono_jit_info_table_add (cfg->domain, jinfo);
13036 if (cfg->method->dynamic)
13037 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
13038 mono_domain_unlock (cfg->domain);
13041 /* collect statistics */
13042 mono_perfcounters->jit_methods++;
13043 mono_perfcounters->jit_bytes += header->code_size;
13044 mono_jit_stats.allocated_code_size += cfg->code_len;
13045 code_size_ratio = cfg->code_len;
13046 if (code_size_ratio > mono_jit_stats.biggest_method_size && mono_jit_stats.enabled) {
13047 mono_jit_stats.biggest_method_size = code_size_ratio;
13048 g_free (mono_jit_stats.biggest_method);
13049 mono_jit_stats.biggest_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13051 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
13052 if (code_size_ratio > mono_jit_stats.max_code_size_ratio && mono_jit_stats.enabled) {
13053 mono_jit_stats.max_code_size_ratio = code_size_ratio;
13054 g_free (mono_jit_stats.max_ratio_method);
13055 mono_jit_stats.max_ratio_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13057 mono_jit_stats.native_code_size += cfg->code_len;
13059 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
13060 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
13061 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
13062 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
13070 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
13072 g_assert_not_reached ();
13076 #endif /* DISABLE_JIT */
13078 static MonoJitInfo*
13079 lookup_generic_method (MonoDomain *domain, MonoMethod *method)
13081 MonoMethod *open_method;
13083 if (!mono_method_is_generic_sharable_impl (method, FALSE))
13086 open_method = mono_method_get_declaring_generic_method (method);
13088 return mono_domain_lookup_shared_generic (domain, open_method);
13092 * LOCKING: Assumes domain->jit_code_hash_lock is held.
13094 static MonoJitInfo*
13095 lookup_method_inner (MonoDomain *domain, MonoMethod *method)
13097 MonoJitInfo *ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, method);
13102 return lookup_generic_method (domain, method);
13105 static MonoJitInfo*
13106 lookup_method (MonoDomain *domain, MonoMethod *method)
13110 mono_domain_jit_code_hash_lock (domain);
13111 info = lookup_method_inner (domain, method);
13112 mono_domain_jit_code_hash_unlock (domain);
13118 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt)
13121 gpointer code = NULL;
13123 MonoVTable *vtable;
13125 #ifdef MONO_USE_AOT_COMPILER
13126 if ((opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
13127 MonoDomain *domain = mono_domain_get ();
13129 mono_class_init (method->klass);
13131 mono_domain_lock (domain);
13132 if ((code = mono_aot_get_method (domain, method))) {
13133 mono_domain_unlock (domain);
13134 vtable = mono_class_vtable (domain, method->klass);
13136 mono_runtime_class_init (vtable);
13140 mono_domain_unlock (domain);
13144 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
13145 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
13147 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
13149 if (!piinfo->addr) {
13150 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
13151 piinfo->addr = mono_lookup_internal_call (method);
13152 else if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
13153 #ifdef PLATFORM_WIN32
13154 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);
13156 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);
13159 mono_lookup_pinvoke_call (method, NULL, NULL);
13161 nm = mono_marshal_get_native_wrapper (method, check_for_pending_exc, FALSE);
13162 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13164 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
13165 //mono_debug_add_wrapper (method, nm);
13166 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
13167 const char *name = method->name;
13170 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
13171 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
13172 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
13174 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper (mi));
13175 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
13176 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
13177 return mono_create_delegate_trampoline (method->klass);
13179 nm = mono_marshal_get_delegate_invoke (method, NULL);
13180 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13182 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
13183 nm = mono_marshal_get_delegate_begin_invoke (method);
13184 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13185 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
13186 nm = mono_marshal_get_delegate_end_invoke (method);
13187 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13194 g_error ("Attempting to JIT compile method '%s' while running with --aot-only.\n", mono_method_full_name (method, TRUE));
13196 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
13198 switch (cfg->exception_type) {
13199 case MONO_EXCEPTION_NONE:
13201 case MONO_EXCEPTION_TYPE_LOAD:
13202 case MONO_EXCEPTION_MISSING_FIELD:
13203 case MONO_EXCEPTION_MISSING_METHOD:
13204 case MONO_EXCEPTION_FILE_NOT_FOUND:
13205 case MONO_EXCEPTION_BAD_IMAGE: {
13206 /* Throw a type load exception if needed */
13207 MonoLoaderError *error = mono_loader_get_last_error ();
13211 ex = mono_loader_error_prepare_exception (error);
13213 if (cfg->exception_ptr) {
13214 ex = mono_class_get_exception_for_failure (cfg->exception_ptr);
13216 if (cfg->exception_type == MONO_EXCEPTION_MISSING_FIELD)
13217 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", cfg->exception_message);
13218 else if (cfg->exception_type == MONO_EXCEPTION_MISSING_METHOD)
13219 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", cfg->exception_message);
13220 else if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD)
13221 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", cfg->exception_message);
13222 else if (cfg->exception_type == MONO_EXCEPTION_FILE_NOT_FOUND)
13223 ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FileNotFoundException", cfg->exception_message);
13224 else if (cfg->exception_type == MONO_EXCEPTION_BAD_IMAGE)
13225 ex = mono_get_exception_bad_image_format (cfg->exception_message);
13227 g_assert_not_reached ();
13230 mono_destroy_compile (cfg);
13231 mono_raise_exception (ex);
13234 case MONO_EXCEPTION_INVALID_PROGRAM: {
13235 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
13236 mono_destroy_compile (cfg);
13237 mono_raise_exception (ex);
13240 case MONO_EXCEPTION_UNVERIFIABLE_IL: {
13241 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
13242 mono_destroy_compile (cfg);
13243 mono_raise_exception (ex);
13246 case MONO_EXCEPTION_METHOD_ACCESS: {
13247 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", cfg->exception_message);
13248 mono_destroy_compile (cfg);
13249 mono_raise_exception (ex);
13252 case MONO_EXCEPTION_FIELD_ACCESS: {
13253 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FieldAccessException", cfg->exception_message);
13254 mono_destroy_compile (cfg);
13255 mono_raise_exception (ex);
13258 /* this can only be set if the security manager is active */
13259 case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
13260 MonoSecurityManager* secman = mono_security_manager_get_methods ();
13261 MonoObject *exc = NULL;
13264 args [0] = &cfg->exception_data;
13265 args [1] = &method;
13266 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
13268 mono_destroy_compile (cfg);
13271 mono_raise_exception ((MonoException*)exc);
13273 case MONO_EXCEPTION_OBJECT_SUPPLIED: {
13274 MonoException *exp = cfg->exception_ptr;
13275 MONO_GC_UNREGISTER_ROOT (cfg->exception_ptr);
13276 mono_destroy_compile (cfg);
13277 mono_raise_exception (exp);
13281 g_assert_not_reached ();
13284 mono_domain_lock (target_domain);
13286 /* Check if some other thread already did the job. In this case, we can
13287 discard the code this thread generated. */
13289 mono_domain_jit_code_hash_lock (target_domain);
13291 info = lookup_method_inner (target_domain, method);
13293 /* We can't use a domain specific method in another domain */
13294 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
13295 code = info->code_start;
13296 // printf("Discarding code for method %s\n", method->name);
13300 if (code == NULL) {
13301 mono_internal_hash_table_insert (&target_domain->jit_code_hash, cfg->jit_info->method, cfg->jit_info);
13302 mono_domain_jit_code_hash_unlock (target_domain);
13303 code = cfg->native_code;
13305 if (cfg->generic_sharing_context && mono_method_is_generic_sharable_impl (method, FALSE)) {
13306 /* g_print ("inserting method %s.%s.%s\n", method->klass->name_space, method->klass->name, method->name); */
13307 mono_domain_register_shared_generic (target_domain,
13308 mono_method_get_declaring_generic_method (method), cfg->jit_info);
13309 mono_stats.generics_shared_methods++;
13312 mono_domain_jit_code_hash_unlock (target_domain);
13315 mono_destroy_compile (cfg);
13317 if (target_domain->jump_target_hash) {
13318 MonoJumpInfo patch_info;
13319 GSList *list, *tmp;
13320 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
13322 patch_info.next = NULL;
13323 patch_info.ip.i = 0;
13324 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
13325 patch_info.data.method = method;
13326 g_hash_table_remove (target_domain->jump_target_hash, method);
13328 for (tmp = list; tmp; tmp = tmp->next)
13329 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
13330 g_slist_free (list);
13333 mono_domain_unlock (target_domain);
13335 vtable = mono_class_vtable (target_domain, method->klass);
13337 MonoException *exc;
13338 exc = mono_class_get_exception_for_failure (method->klass);
13340 mono_raise_exception (exc);
13342 mono_runtime_class_init (vtable);
13347 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
13349 MonoDomain *target_domain, *domain = mono_domain_get ();
13352 MonoJitICallInfo *callinfo = NULL;
13355 * ICALL wrappers are handled specially, since there is only one copy of them
13356 * shared by all appdomains.
13358 if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
13359 const char *icall_name;
13361 icall_name = method->name + strlen ("__icall_wrapper_");
13362 g_assert (icall_name);
13363 callinfo = mono_find_jit_icall_by_name (icall_name);
13364 g_assert (callinfo);
13366 /* Must be domain neutral since there is only one copy */
13367 opt |= MONO_OPT_SHARED;
13370 if (opt & MONO_OPT_SHARED)
13371 target_domain = mono_get_root_domain ();
13373 target_domain = domain;
13375 info = lookup_method (target_domain, method);
13377 /* We can't use a domain specific method in another domain */
13378 if (! ((domain != target_domain) && !info->domain_neutral)) {
13379 MonoVTable *vtable;
13381 mono_jit_stats.methods_lookups++;
13382 vtable = mono_class_vtable (domain, method->klass);
13383 mono_runtime_class_init (vtable);
13384 return mono_create_ftnptr (target_domain, info->code_start);
13388 p = mono_create_ftnptr (target_domain, mono_jit_compile_method_inner (method, target_domain, opt));
13392 if (!callinfo->wrapper) {
13393 callinfo->wrapper = p;
13394 mono_register_jit_icall_wrapper (callinfo, p);
13395 mono_debug_add_icall_wrapper (method, callinfo);
13397 mono_jit_unlock ();
13404 mono_jit_compile_method (MonoMethod *method)
13406 return mono_jit_compile_method_with_opt (method, default_opt);
13409 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13411 invalidated_delegate_trampoline (char *desc)
13413 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
13414 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
13420 * mono_jit_free_method:
13422 * Free all memory allocated by the JIT for METHOD.
13425 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
13427 MonoJitDynamicMethodInfo *ji;
13428 gboolean destroy = TRUE;
13430 g_assert (method->dynamic);
13432 mono_domain_lock (domain);
13433 ji = mono_dynamic_code_hash_lookup (domain, method);
13434 mono_domain_unlock (domain);
13438 mono_domain_lock (domain);
13439 g_hash_table_remove (domain->dynamic_code_hash, method);
13440 mono_internal_hash_table_remove (&domain->jit_code_hash, method);
13441 g_hash_table_remove (domain->jump_trampoline_hash, method);
13442 mono_domain_unlock (domain);
13444 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13445 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
13447 * Instead of freeing the code, change it to call an error routine
13448 * so people can fix their code.
13450 char *type = mono_type_full_name (&method->klass->byval_arg);
13451 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
13454 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
13460 * This needs to be done before freeing code_mp, since the code address is the
13461 * key in the table, so if we free the code_mp first, another thread can grab the
13462 * same code address and replace our entry in the table.
13464 mono_jit_info_table_remove (domain, ji->ji);
13467 mono_code_manager_destroy (ji->code_mp);
13472 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
13474 MonoDomain *target_domain;
13477 if (default_opt & MONO_OPT_SHARED)
13478 target_domain = mono_get_root_domain ();
13480 target_domain = domain;
13482 info = lookup_method (target_domain, method);
13484 /* We can't use a domain specific method in another domain */
13485 if (! ((domain != target_domain) && !info->domain_neutral)) {
13486 mono_jit_stats.methods_lookups++;
13487 return info->code_start;
13495 * mono_jit_runtime_invoke:
13496 * @method: the method to invoke
13497 * @obj: this pointer
13498 * @params: array of parameter values.
13499 * @exc: used to catch exceptions objects
13502 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
13504 MonoMethod *to_compile;
13505 MonoMethod *invoke;
13506 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
13507 void* compiled_method;
13508 MonoVTable *vtable;
13510 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
13511 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
13515 if (((method->flags & METHOD_ATTRIBUTE_STATIC) ||
13516 (method->is_inflated && mono_method_get_context (method)->method_inst)) &&
13517 mono_class_generic_sharing_enabled (method->klass) &&
13518 mono_method_is_generic_sharable_impl (method, FALSE)) {
13519 to_compile = mono_marshal_get_static_rgctx_invoke (method);
13521 to_compile = method;
13524 invoke = mono_marshal_get_runtime_invoke (method);
13525 runtime_invoke = mono_jit_compile_method (invoke);
13527 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
13528 * the helper method in System.Object and not the target class
13530 vtable = mono_class_vtable (mono_domain_get (), method->klass);
13532 mono_runtime_class_init (vtable);
13534 if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
13535 (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
13537 * Array Get/Set/Address methods. The JIT implements them using inline code
13538 * inside the runtime invoke wrappers, so no need to compile them.
13540 compiled_method = NULL;
13542 compiled_method = mono_jit_compile_method (to_compile);
13544 return runtime_invoke (obj, params, exc, compiled_method);
13547 #ifdef MONO_GET_CONTEXT
13548 #define GET_CONTEXT MONO_GET_CONTEXT
13551 #ifndef GET_CONTEXT
13552 #ifdef PLATFORM_WIN32
13553 #define GET_CONTEXT \
13554 struct sigcontext *ctx = (struct sigcontext*)_dummy;
13556 #ifdef MONO_ARCH_USE_SIGACTION
13557 #define GET_CONTEXT \
13558 void *ctx = context;
13559 #elif defined(__sparc__)
13560 #define GET_CONTEXT \
13561 void *ctx = sigctx;
13563 #define GET_CONTEXT \
13564 void **_p = (void **)&_dummy; \
13565 struct sigcontext *ctx = (struct sigcontext *)++_p;
13570 #ifdef MONO_ARCH_USE_SIGACTION
13571 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
13572 #elif defined(__sparc__)
13573 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, void *sigctx)
13575 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
13579 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
13581 MonoException *exc = NULL;
13582 #ifndef MONO_ARCH_USE_SIGACTION
13587 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
13588 if (mono_arch_is_int_overflow (ctx, info))
13589 exc = mono_get_exception_arithmetic ();
13591 exc = mono_get_exception_divide_by_zero ();
13593 exc = mono_get_exception_divide_by_zero ();
13596 mono_arch_handle_exception (ctx, exc, FALSE);
13600 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
13602 MonoException *exc;
13605 exc = mono_get_exception_execution_engine ("SIGILL");
13607 mono_arch_handle_exception (ctx, exc, FALSE);
13611 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
13613 #ifndef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13614 MonoException *exc = NULL;
13617 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13621 #ifdef MONO_ARCH_USE_SIGACTION
13622 if (debug_options.collect_pagefault_stats) {
13623 if (mono_raw_buffer_is_pagefault (info->si_addr)) {
13624 mono_raw_buffer_handle_pagefault (info->si_addr);
13627 if (mono_aot_is_pagefault (info->si_addr)) {
13628 mono_aot_handle_pagefault (info->si_addr);
13634 /* The thread might no be registered with the runtime */
13635 if (!mono_domain_get () || !jit_tls)
13636 mono_handle_native_sigsegv (SIGSEGV, ctx);
13638 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
13640 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13641 /* we got a stack overflow in the soft-guard pages
13642 * There are two cases:
13643 * 1) managed code caused the overflow: we unprotect the soft-guard page
13644 * and let the arch-specific code trigger the exception handling mechanism
13645 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
13646 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
13647 * and hope we can continue with those enabled, at least until the hard-guard page
13648 * is hit. The alternative to continuing here is to just print a message and abort.
13649 * We may add in the future the code to protect the pages again in the codepath
13650 * when we return from unmanaged to managed code.
13652 if (jit_tls->stack_ovf_guard_size && (guint8*)info->si_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
13653 (guint8*)info->si_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
13654 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
13656 mono_arch_handle_altstack_exception (ctx, info->si_addr, TRUE);
13658 /* We print a message: after this even managed stack overflows
13659 * may crash the runtime
13661 fprintf (stderr, "Stack overflow in unmanaged: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), (gpointer)info->si_addr);
13665 /* The hard-guard page has been hit: there is not much we can do anymore
13666 * Print a hopefully clear message and abort.
13668 if (jit_tls->stack_size &&
13669 ABS ((guint8*)info->si_addr - ((guint8*)jit_tls->end_of_stack - jit_tls->stack_size)) < 32768) {
13670 const char *method;
13671 /* we don't do much now, but we can warn the user with a useful message */
13672 fprintf (stderr, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), (gpointer)info->si_addr);
13673 if (ji && ji->method)
13674 method = mono_method_full_name (ji->method, TRUE);
13676 method = "Unmanaged";
13677 fprintf (stderr, "At %s\n", method);
13680 mono_arch_handle_altstack_exception (ctx, info->si_addr, FALSE);
13685 mono_handle_native_sigsegv (SIGSEGV, ctx);
13688 mono_arch_handle_exception (ctx, exc, FALSE);
13692 #ifndef PLATFORM_WIN32
13695 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
13700 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13702 mono_handle_native_sigsegv (SIGABRT, ctx);
13707 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
13709 gboolean running_managed;
13710 MonoException *exc;
13711 MonoThread *thread = mono_thread_current ();
13716 if (thread->thread_dump_requested) {
13717 thread->thread_dump_requested = FALSE;
13719 mono_print_thread_dump (ctx);
13724 * This is an async signal, so the code below must not call anything which
13725 * is not async safe. That includes the pthread locking functions. If we
13726 * know that we interrupted managed code, then locking is safe.
13728 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13729 running_managed = ji != NULL;
13731 exc = mono_thread_request_interruption (running_managed);
13734 mono_arch_handle_exception (ctx, exc, FALSE);
13737 #if defined(__i386__) || defined(__x86_64__)
13738 #define FULL_STAT_PROFILER_BACKTRACE 1
13739 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
13740 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
13741 #if MONO_ARCH_STACK_GROWS_UP
13742 #define IS_BEFORE_ON_STACK <
13743 #define IS_AFTER_ON_STACK >
13745 #define IS_BEFORE_ON_STACK >
13746 #define IS_AFTER_ON_STACK <
13749 #define FULL_STAT_PROFILER_BACKTRACE 0
13752 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
13755 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13763 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13765 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
13768 if (call_chain_depth == 0) {
13769 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
13771 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13772 int current_frame_index = 1;
13773 MonoContext mono_context;
13774 #if FULL_STAT_PROFILER_BACKTRACE
13775 guchar *current_frame;
13776 guchar *stack_bottom;
13779 MonoDomain *domain;
13781 guchar *ips [call_chain_depth + 1];
13783 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
13784 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
13786 if (jit_tls != NULL) {
13787 #if FULL_STAT_PROFILER_BACKTRACE
13788 stack_bottom = jit_tls->end_of_stack;
13789 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
13790 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
13792 while ((current_frame_index <= call_chain_depth) &&
13793 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
13794 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
13795 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
13796 current_frame_index ++;
13797 stack_top = current_frame;
13798 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
13801 domain = mono_domain_get ();
13802 if (domain != NULL) {
13803 MonoLMF *lmf = NULL;
13806 MonoContext new_mono_context;
13808 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13809 &new_mono_context, NULL, &lmf, &native_offset, NULL);
13810 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
13811 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
13812 current_frame_index ++;
13813 mono_context = new_mono_context;
13814 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13815 &new_mono_context, NULL, &lmf, &native_offset, NULL);
13822 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
13829 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
13833 printf ("Full thread dump:\n");
13835 mono_threads_request_thread_dump ();
13838 * print_thread_dump () skips the current thread, since sending a signal
13839 * to it would invoke the signal handler below the sigquit signal handler,
13840 * and signal handlers don't create an lmf, so the stack walk could not
13843 mono_print_thread_dump (ctx);
13847 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
13849 gboolean enabled = mono_trace_is_enabled ();
13851 mono_trace_enable (!enabled);
13857 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
13859 MonoException *exc;
13862 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
13864 mono_arch_handle_exception (ctx, exc, FALSE);
13867 #ifdef PLATFORM_MACOSX
13870 * This code disables the CrashReporter of MacOS X by installing
13871 * a dummy Mach exception handler.
13875 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
13879 exc_server (mach_msg_header_t *request_msg,
13880 mach_msg_header_t *reply_msg);
13883 * The exception message
13886 mach_msg_base_t msg; /* common mach message header */
13887 char payload [1024]; /* opaque */
13888 } mach_exception_msg_t;
13890 /* The exception port */
13891 static mach_port_t mach_exception_port = VM_MAP_NULL;
13894 * Implicitly called by exc_server. Must be public.
13896 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
13899 catch_exception_raise (
13900 mach_port_t exception_port,
13901 mach_port_t thread,
13903 exception_type_t exception,
13904 exception_data_t code,
13905 mach_msg_type_number_t code_count)
13907 /* consume the exception */
13908 return KERN_FAILURE;
13912 * Exception thread handler.
13916 mach_exception_thread (void *arg)
13919 mach_exception_msg_t request;
13920 mach_exception_msg_t reply;
13921 mach_msg_return_t result;
13923 /* receive from "mach_exception_port" */
13924 result = mach_msg (&request.msg.header,
13925 MACH_RCV_MSG | MACH_RCV_LARGE,
13928 mach_exception_port,
13929 MACH_MSG_TIMEOUT_NONE,
13932 g_assert (result == MACH_MSG_SUCCESS);
13934 /* dispatch to catch_exception_raise () */
13935 exc_server (&request.msg.header, &reply.msg.header);
13937 /* send back to sender */
13938 result = mach_msg (&reply.msg.header,
13940 reply.msg.header.msgh_size,
13943 MACH_MSG_TIMEOUT_NONE,
13946 g_assert (result == MACH_MSG_SUCCESS);
13952 macosx_register_exception_handler ()
13955 pthread_attr_t attr;
13958 if (mach_exception_port != VM_MAP_NULL)
13961 task = mach_task_self ();
13963 /* create the "mach_exception_port" with send & receive rights */
13964 g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
13965 &mach_exception_port) == KERN_SUCCESS);
13966 g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
13967 MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
13969 /* create the exception handler thread */
13970 g_assert (!pthread_attr_init (&attr));
13971 g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
13972 g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
13973 pthread_attr_destroy (&attr);
13976 * register "mach_exception_port" as a receiver for the
13977 * EXC_BAD_ACCESS exception
13979 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
13981 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
13982 mach_exception_port,
13984 MACHINE_THREAD_STATE) == KERN_SUCCESS);
13988 #ifndef PLATFORM_WIN32
13990 add_signal_handler (int signo, gpointer handler)
13992 struct sigaction sa;
13994 #ifdef MONO_ARCH_USE_SIGACTION
13995 sa.sa_sigaction = handler;
13996 sigemptyset (&sa.sa_mask);
13997 sa.sa_flags = SA_SIGINFO;
13998 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13999 if (signo == SIGSEGV)
14000 sa.sa_flags |= SA_ONSTACK;
14003 sa.sa_handler = handler;
14004 sigemptyset (&sa.sa_mask);
14007 g_assert (sigaction (signo, &sa, NULL) != -1);
14011 remove_signal_handler (int signo)
14013 struct sigaction sa;
14015 sa.sa_handler = SIG_DFL;
14016 sigemptyset (&sa.sa_mask);
14019 g_assert (sigaction (signo, &sa, NULL) != -1);
14024 mono_runtime_install_handlers (void)
14026 #ifdef PLATFORM_WIN32
14028 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
14029 win32_seh_set_handler(SIGILL, sigill_signal_handler);
14030 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
14031 if (debug_options.handle_sigint)
14032 win32_seh_set_handler(SIGINT, sigint_signal_handler);
14034 #else /* !PLATFORM_WIN32 */
14037 #if defined(PLATFORM_MACOSX) && !defined(__arm__)
14038 macosx_register_exception_handler ();
14041 if (debug_options.handle_sigint)
14042 add_signal_handler (SIGINT, sigint_signal_handler);
14044 add_signal_handler (SIGFPE, sigfpe_signal_handler);
14045 add_signal_handler (SIGQUIT, sigquit_signal_handler);
14046 add_signal_handler (SIGILL, sigill_signal_handler);
14047 add_signal_handler (SIGBUS, sigsegv_signal_handler);
14048 if (mono_jit_trace_calls != NULL)
14049 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
14051 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
14052 signal (SIGPIPE, SIG_IGN);
14054 add_signal_handler (SIGABRT, sigabrt_signal_handler);
14056 /* catch SIGSEGV */
14057 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
14058 #endif /* PLATFORM_WIN32 */
14062 mono_runtime_cleanup_handlers (void)
14064 #ifdef PLATFORM_WIN32
14065 win32_seh_cleanup();
14067 if (debug_options.handle_sigint)
14068 remove_signal_handler (SIGINT);
14070 remove_signal_handler (SIGFPE);
14071 remove_signal_handler (SIGQUIT);
14072 remove_signal_handler (SIGILL);
14073 remove_signal_handler (SIGBUS);
14074 if (mono_jit_trace_calls != NULL)
14075 remove_signal_handler (SIGUSR2);
14077 remove_signal_handler (mono_thread_get_abort_signal ());
14079 remove_signal_handler (SIGABRT);
14081 remove_signal_handler (SIGSEGV);
14082 #endif /* PLATFORM_WIN32 */
14086 #ifdef HAVE_LINUX_RTC_H
14087 #include <linux/rtc.h>
14088 #include <sys/ioctl.h>
14090 static int rtc_fd = -1;
14093 enable_rtc_timer (gboolean enable)
14096 flags = fcntl (rtc_fd, F_GETFL);
14098 perror ("getflags");
14105 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
14106 perror ("setflags");
14113 #ifdef PLATFORM_WIN32
14114 static HANDLE win32_main_thread;
14115 static MMRESULT win32_timer;
14117 static void CALLBACK
14118 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
14122 context.ContextFlags = CONTEXT_CONTROL;
14123 if (GetThreadContext (win32_main_thread, &context)) {
14125 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
14127 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
14134 setup_stat_profiler (void)
14137 struct itimerval itval;
14138 static int inited = 0;
14139 #ifdef HAVE_LINUX_RTC_H
14140 const char *rtc_freq;
14141 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
14145 freq = atoi (rtc_freq);
14148 rtc_fd = open ("/dev/rtc", O_RDONLY);
14149 if (rtc_fd == -1) {
14150 perror ("open /dev/rtc");
14153 add_signal_handler (SIGPROF, sigprof_signal_handler);
14154 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
14155 perror ("set rtc freq");
14158 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
14159 perror ("start rtc");
14162 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
14166 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
14170 enable_rtc_timer (TRUE);
14177 itval.it_interval.tv_usec = 999;
14178 itval.it_interval.tv_sec = 0;
14179 itval.it_value = itval.it_interval;
14180 setitimer (ITIMER_PROF, &itval, NULL);
14184 add_signal_handler (SIGPROF, sigprof_signal_handler);
14185 #elif defined (PLATFORM_WIN32)
14186 static int inited = 0;
14193 if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
14196 if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
14199 if (timeBeginPeriod (1) != TIMERR_NOERROR)
14202 if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
14209 /* mono_jit_create_remoting_trampoline:
14210 * @method: pointer to the method info
14212 * Creates a trampoline which calls the remoting functions. This
14213 * is used in the vtable of transparent proxies.
14215 * Returns: a pointer to the newly created code
14218 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
14221 guint8 *addr = NULL;
14223 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
14224 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
14225 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
14226 addr = mono_compile_method (nm);
14228 addr = mono_compile_method (method);
14230 return mono_get_addr_from_ftnptr (addr);
14233 #ifdef MONO_ARCH_HAVE_IMT
14235 mini_get_imt_trampoline (void)
14237 static gpointer tramp = NULL;
14239 tramp = mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14244 #ifdef MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14246 mini_get_vtable_trampoline (void)
14248 static gpointer tramp = NULL;
14250 tramp = mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14256 mini_parse_debug_options (void)
14258 char *options = getenv ("MONO_DEBUG");
14259 gchar **args, **ptr;
14264 args = g_strsplit (options, ",", -1);
14266 for (ptr = args; ptr && *ptr; ptr++) {
14267 const char *arg = *ptr;
14269 if (!strcmp (arg, "handle-sigint"))
14270 debug_options.handle_sigint = TRUE;
14271 else if (!strcmp (arg, "keep-delegates"))
14272 debug_options.keep_delegates = TRUE;
14273 else if (!strcmp (arg, "collect-pagefault-stats"))
14274 debug_options.collect_pagefault_stats = TRUE;
14275 else if (!strcmp (arg, "break-on-unverified"))
14276 debug_options.break_on_unverified = TRUE;
14277 else if (!strcmp (arg, "no-gdb-backtrace"))
14278 debug_options.no_gdb_backtrace = TRUE;
14279 else if (!strcmp (arg, "dont-free-domains"))
14280 mono_dont_free_domains = TRUE;
14282 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
14283 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains'\n");
14292 mini_get_debug_options (void)
14294 return &debug_options;
14298 mini_create_jit_domain_info (MonoDomain *domain)
14300 MonoJitDomainInfo *info = g_new0 (MonoJitDomainInfo, 1);
14302 domain->runtime_info = info;
14306 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
14308 g_slist_free (value);
14312 mini_free_jit_domain_info (MonoDomain *domain)
14314 MonoJitDomainInfo *info = jit_domain_info (domain);
14316 if (info->jump_target_got_slot_hash) {
14317 g_hash_table_foreach (info->jump_target_got_slot_hash, delete_jump_list, NULL);
14318 g_hash_table_destroy (info->jump_target_got_slot_hash);
14320 g_free (domain->runtime_info);
14324 mini_init (const char *filename, const char *runtime_version)
14326 MonoDomain *domain;
14328 MONO_PROBE_VES_INIT_BEGIN ();
14331 if (access ("/proc/self/maps", F_OK) != 0) {
14332 g_print ("Mono requires /proc to be mounted.\n");
14337 /* Happens when using the embedding interface */
14338 if (!default_opt_set)
14339 default_opt = mono_parse_default_optimizations (NULL);
14341 InitializeCriticalSection (&jit_mutex);
14343 if (!global_codeman)
14344 global_codeman = mono_code_manager_new ();
14345 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
14347 mono_arch_cpu_init ();
14351 mono_trampolines_init ();
14353 if (!g_thread_supported ())
14354 g_thread_init (NULL);
14356 if (getenv ("MONO_DEBUG") != NULL)
14357 mini_parse_debug_options ();
14359 mono_gc_base_init ();
14361 mono_jit_tls_id = TlsAlloc ();
14362 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
14364 #ifndef DISABLE_JIT
14368 if (default_opt & MONO_OPT_AOT)
14371 mono_runtime_install_handlers ();
14372 mono_threads_install_cleanup (mini_thread_cleanup);
14374 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
14375 // This is experimental code so provide an env var to switch it off
14376 if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
14377 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
14379 check_for_pending_exc = FALSE;
14380 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc);
14384 #define JIT_TRAMPOLINES_WORK
14385 #ifdef JIT_TRAMPOLINES_WORK
14386 mono_install_compile_method (mono_jit_compile_method);
14387 mono_install_free_method (mono_jit_free_method);
14388 mono_install_trampoline (mono_create_jit_trampoline);
14389 mono_install_jump_trampoline (mono_create_jump_trampoline);
14390 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
14391 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
14392 mono_install_create_domain_hook (mini_create_jit_domain_info);
14393 mono_install_free_domain_hook (mini_free_jit_domain_info);
14395 #define JIT_INVOKE_WORKS
14396 #ifdef JIT_INVOKE_WORKS
14397 mono_install_runtime_invoke (mono_jit_runtime_invoke);
14399 mono_install_stack_walk (mono_jit_walk_stack);
14400 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
14401 mono_install_get_class_from_name (mono_aot_get_class_from_name);
14402 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
14404 if (debug_options.collect_pagefault_stats) {
14405 mono_raw_buffer_set_make_unreadable (TRUE);
14406 mono_aot_set_make_unreadable (TRUE);
14409 if (runtime_version)
14410 domain = mono_init_version (filename, runtime_version);
14412 domain = mono_init_from_assembly (filename, filename);
14414 if (mono_aot_only) {
14415 /* The IMT tables are very dynamic thus they are hard to AOT */
14416 mono_use_imt = FALSE;
14417 /* This helps catch code allocation requests */
14418 mono_code_manager_set_read_only (domain->code_mp);
14421 #ifdef MONO_ARCH_HAVE_IMT
14422 if (mono_use_imt) {
14423 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
14424 mono_install_imt_trampoline (mini_get_imt_trampoline ());
14425 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14426 mono_install_vtable_trampoline (mini_get_vtable_trampoline ());
14431 /* This must come after mono_init () in the aot-only case */
14432 mono_exceptions_init ();
14433 mono_install_handler (mono_get_throw_exception ());
14435 mono_icall_init ();
14437 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
14438 ves_icall_get_frame_info);
14439 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
14440 ves_icall_get_trace);
14441 mono_add_internal_call ("System.Exception::get_trace",
14442 ves_icall_System_Exception_get_trace);
14443 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
14444 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
14445 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
14446 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
14447 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
14448 mono_runtime_install_handlers);
14451 create_helper_signature ();
14453 #define JIT_CALLS_WORK
14454 #ifdef JIT_CALLS_WORK
14455 /* Needs to be called here since register_jit_icall depends on it */
14456 mono_marshal_init ();
14458 mono_arch_register_lowlevel_calls ();
14459 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
14460 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
14461 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
14462 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
14463 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
14464 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
14465 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
14467 register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
14468 register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
14469 register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE);
14470 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
14471 register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
14474 register_icall (mono_thread_get_undeniable_exception, "mono_thread_get_undeniable_exception", "object", FALSE);
14475 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
14476 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
14477 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
14478 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
14481 * NOTE, NOTE, NOTE, NOTE:
14482 * when adding emulation for some opcodes, remember to also add a dummy
14483 * rule to the burg files, because we need the arity information to be correct.
14485 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
14486 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
14487 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
14488 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
14489 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
14490 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
14491 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
14492 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
14495 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
14496 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
14497 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
14498 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
14501 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14502 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, FALSE);
14503 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14504 mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, FALSE);
14505 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14506 mono_register_opcode_emulation (OP_IDIV, "__emul_op_idiv", "int32 int32 int32", mono_idiv, FALSE);
14507 mono_register_opcode_emulation (OP_IDIV_UN, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14508 mono_register_opcode_emulation (OP_IREM, "__emul_op_irem", "int32 int32 int32", mono_irem, FALSE);
14509 mono_register_opcode_emulation (OP_IREM_UN, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14512 #ifdef MONO_ARCH_EMULATE_MUL_DIV
14513 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14514 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14515 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
14516 mono_register_opcode_emulation (OP_IMUL, "__emul_op_imul", "int32 int32 int32", mono_imul, TRUE);
14517 mono_register_opcode_emulation (OP_IMUL_OVF, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14518 mono_register_opcode_emulation (OP_IMUL_OVF_UN, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14520 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
14521 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, FALSE);
14524 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
14525 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
14526 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
14527 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
14529 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
14530 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
14532 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
14533 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14534 mono_register_opcode_emulation (OP_ICONV_TO_R_UN, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14536 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
14537 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
14539 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
14540 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
14542 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
14543 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
14545 #ifdef MONO_ARCH_EMULATE_FREM
14546 mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
14549 #ifdef MONO_ARCH_SOFT_FLOAT
14550 mono_register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, FALSE);
14551 mono_register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, FALSE);
14552 mono_register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, FALSE);
14553 mono_register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, FALSE);
14554 mono_register_opcode_emulation (CEE_CONV_R8, "__emul_conv_r8", "double int32", mono_conv_to_r8, FALSE);
14555 mono_register_opcode_emulation (OP_ICONV_TO_R8, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8, FALSE);
14556 mono_register_opcode_emulation (CEE_CONV_R4, "__emul_conv_r4", "double int32", mono_conv_to_r4, FALSE);
14557 mono_register_opcode_emulation (OP_ICONV_TO_R4, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4, FALSE);
14558 mono_register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, FALSE);
14559 mono_register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, FALSE);
14560 mono_register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, FALSE);
14561 mono_register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, FALSE);
14562 mono_register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, FALSE);
14563 mono_register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, FALSE);
14565 mono_register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, FALSE);
14566 mono_register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, FALSE);
14567 mono_register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, FALSE);
14568 mono_register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, FALSE);
14569 mono_register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, FALSE);
14570 mono_register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, FALSE);
14571 mono_register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, FALSE);
14572 mono_register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, FALSE);
14573 mono_register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, FALSE);
14574 mono_register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, FALSE);
14576 mono_register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, FALSE);
14577 mono_register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, FALSE);
14578 mono_register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, FALSE);
14579 mono_register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, FALSE);
14580 mono_register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, FALSE);
14582 register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
14583 register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
14584 register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
14587 #if SIZEOF_VOID_P == 4
14588 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
14591 /* other jit icalls */
14592 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
14593 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
14594 "ptr ptr ptr", FALSE);
14595 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
14596 register_icall (mono_ldtoken_wrapper_generic_shared, "mono_ldtoken_wrapper_generic_shared",
14597 "ptr ptr ptr ptr", FALSE);
14598 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
14599 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
14600 register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
14601 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
14602 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
14603 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
14604 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
14605 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
14606 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
14607 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
14608 register_icall (mono_ldvirtfn_gshared, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE);
14609 register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
14610 register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
14611 register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
14612 register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
14613 register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
14614 register_icall (mono_object_castclass, "mono_object_castclass", "object object ptr", FALSE);
14615 register_icall (mono_break, "mono_break", NULL, TRUE);
14616 register_icall (mono_create_corlib_exception_0, "mono_create_corlib_exception_0", "object int", TRUE);
14617 register_icall (mono_create_corlib_exception_1, "mono_create_corlib_exception_1", "object int object", TRUE);
14618 register_icall (mono_create_corlib_exception_2, "mono_create_corlib_exception_2", "object int object object", TRUE);
14619 register_icall (mono_array_new_1, "mono_array_new_1", "object ptr int", FALSE);
14620 register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
14623 mono_generic_sharing_init ();
14625 if (mono_compile_aot)
14627 * Avoid running managed code when AOT compiling, since the platform
14628 * might only support aot-only execution.
14630 mono_runtime_set_no_exec (TRUE);
14632 #define JIT_RUNTIME_WORKS
14633 #ifdef JIT_RUNTIME_WORKS
14634 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
14635 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
14636 mono_thread_attach (domain);
14639 mono_profiler_runtime_initialized ();
14641 MONO_PROBE_VES_INIT_END ();
14646 MonoJitStats mono_jit_stats = {0};
14649 print_jit_stats (void)
14651 if (mono_jit_stats.enabled) {
14652 g_print ("Mono Jit statistics\n");
14653 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
14654 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
14655 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
14656 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
14657 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
14658 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
14659 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
14660 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
14661 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
14662 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
14663 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
14664 mono_jit_stats.max_ratio_method);
14665 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats.biggest_method_size,
14666 mono_jit_stats.biggest_method);
14667 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
14668 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
14669 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
14670 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
14671 g_print ("Regvars: %ld\n", mono_jit_stats.regvars);
14672 g_print ("Locals stack size: %ld\n", mono_jit_stats.locals_stack_size);
14674 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
14675 g_print ("Delegates created: %ld\n", mono_stats.delegate_creations);
14676 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
14677 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
14678 g_print ("Generic vtables: %ld\n", mono_stats.generic_vtable_count);
14679 g_print ("Methods: %ld\n", mono_stats.method_count);
14680 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
14681 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
14682 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
14684 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
14685 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
14686 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
14687 mono_stats.inflated_method_count);
14688 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
14689 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
14690 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
14692 g_print ("Sharable generic methods: %ld\n", mono_stats.generics_sharable_methods);
14693 g_print ("Unsharable generic methods: %ld\n", mono_stats.generics_unsharable_methods);
14694 g_print ("Shared generic methods: %ld\n", mono_stats.generics_shared_methods);
14696 g_print ("Dynamic code allocs: %ld\n", mono_stats.dynamic_code_alloc_count);
14697 g_print ("Dynamic code bytes: %ld\n", mono_stats.dynamic_code_bytes_count);
14698 g_print ("Dynamic code frees: %ld\n", mono_stats.dynamic_code_frees_count);
14700 g_print ("IMT tables size: %ld\n", mono_stats.imt_tables_size);
14701 g_print ("IMT number of tables: %ld\n", mono_stats.imt_number_of_tables);
14702 g_print ("IMT number of methods: %ld\n", mono_stats.imt_number_of_methods);
14703 g_print ("IMT used slots: %ld\n", mono_stats.imt_used_slots);
14704 g_print ("IMT colliding slots: %ld\n", mono_stats.imt_slots_with_collisions);
14705 g_print ("IMT max collisions: %ld\n", mono_stats.imt_max_collisions_in_slot);
14706 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
14707 g_print ("IMT thunks size: %ld\n", mono_stats.imt_thunks_size);
14709 g_print ("JIT info table inserts: %ld\n", mono_stats.jit_info_table_insert_count);
14710 g_print ("JIT info table removes: %ld\n", mono_stats.jit_info_table_remove_count);
14711 g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
14713 g_print ("Hazardous pointers: %ld\n", mono_stats.hazardous_pointer_count);
14714 #ifdef HAVE_SGEN_GC
14715 g_print ("Minor GC collections: %ld\n", mono_stats.minor_gc_count);
14716 g_print ("Major GC collections: %ld\n", mono_stats.major_gc_count);
14717 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats.minor_gc_time_usecs / 1000.0);
14718 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats.major_gc_time_usecs / 1000.0);
14720 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
14721 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
14722 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
14723 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
14724 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
14725 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
14726 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
14728 if (debug_options.collect_pagefault_stats) {
14729 g_print ("Metadata pagefaults : %d\n", mono_raw_buffer_get_n_pagefaults ());
14730 g_print ("AOT pagefaults : %d\n", mono_aot_get_n_pagefaults ());
14733 g_free (mono_jit_stats.max_ratio_method);
14734 mono_jit_stats.max_ratio_method = NULL;
14735 g_free (mono_jit_stats.biggest_method);
14736 mono_jit_stats.biggest_method = NULL;
14741 mini_cleanup (MonoDomain *domain)
14743 #ifdef HAVE_LINUX_RTC_H
14745 enable_rtc_timer (FALSE);
14749 * mono_runtime_cleanup() and mono_domain_finalize () need to
14750 * be called early since they need the execution engine still
14751 * fully working (mono_domain_finalize may invoke managed finalizers
14752 * and mono_runtime_cleanup will wait for other threads to finish).
14754 mono_domain_finalize (domain, 2000);
14756 /* This accesses metadata so needs to be called before runtime shutdown */
14757 print_jit_stats ();
14759 mono_runtime_cleanup (domain);
14761 mono_profiler_shutdown ();
14763 mono_icall_cleanup ();
14765 mono_runtime_cleanup_handlers ();
14767 mono_domain_free (domain, TRUE);
14769 mono_debugger_cleanup ();
14771 mono_trampolines_cleanup ();
14773 mono_code_manager_destroy (global_codeman);
14774 g_hash_table_destroy (jit_icall_name_hash);
14775 g_free (emul_opcode_map);
14777 mono_arch_cleanup ();
14781 mono_trace_cleanup ();
14783 mono_counters_dump (-1, stdout);
14785 if (mono_inject_async_exc_method)
14786 mono_method_desc_free (mono_inject_async_exc_method);
14788 TlsFree(mono_jit_tls_id);
14790 DeleteCriticalSection (&jit_mutex);
14792 DeleteCriticalSection (&mono_delegate_section);
14796 mono_set_defaults (int verbose_level, guint32 opts)
14798 mini_verbose = verbose_level;
14799 default_opt = opts;
14800 default_opt_set = TRUE;
14804 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
14806 GHashTable *assemblies = (GHashTable*)user_data;
14807 MonoImage *image = mono_assembly_get_image (ass);
14808 MonoMethod *method, *invoke;
14811 if (g_hash_table_lookup (assemblies, ass))
14814 g_hash_table_insert (assemblies, ass, ass);
14816 if (mini_verbose > 0)
14817 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
14819 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
14820 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
14821 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
14825 if (mini_verbose > 1) {
14826 char * desc = mono_method_full_name (method, TRUE);
14827 g_print ("Compiling %d %s\n", count, desc);
14830 mono_compile_method (method);
14831 if (strcmp (method->name, "Finalize") == 0) {
14832 invoke = mono_marshal_get_runtime_invoke (method);
14833 mono_compile_method (invoke);
14835 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
14836 invoke = mono_marshal_get_remoting_invoke_with_check (method);
14837 mono_compile_method (invoke);
14841 /* Load and precompile referenced assemblies as well */
14842 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
14843 mono_assembly_load_reference (image, i);
14844 if (image->references [i])
14845 mono_precompile_assembly (image->references [i], assemblies);
14849 void mono_precompile_assemblies ()
14851 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
14853 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
14855 g_hash_table_destroy (assemblies);